package com.applurk.plugin;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaInterface;

import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.os.PowerManager;

import android.util.Log;
import android.provider.Settings;
import android.widget.Toast;

import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.app.KeyguardManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ScreenLocker extends CordovaPlugin {
    public static final String TAG = "ScreenLocker";
    public static final String ACTION_UNLOCK = "unlock";
    public static final String ACTION_UNLOCK_WINDOW = "unlock_window";
    public static final String ACTION_LOCK = "lock";

    /**
     * Constructor.
     */
    public ScreenLocker() {
    }

    /**
     * Sets the context of the Command. This can then be used to do things like
     * get file paths associated with the Activity.
     *
     * @param cordova The context of the main Activity.
     * @param webView The CordovaWebView Cordova is running in.
     */
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        Log.v(TAG, "Init ScreenLocker");
    }

    @Override
    public boolean execute(final String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        boolean result = false;
// Shows a toast
        Log.v(TAG, "ScreenLocker received:" + action);

        try {
            JSONObject arg_object = args.getJSONObject(0);
            if (ACTION_LOCK.equals(action)) {
//                Lock device
                Log.v(TAG, "Lock action not implemented, please wait for a new plugin version");
//                WindowManager wm = (WindowManager) this.cordova.getActivity().getSystemService(this.cordova.getActivity().WINDOW_SERVICE);
//                DevicePolicyManager mDPM;
//                mDPM = (DevicePolicyManager) this.cordova.getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
//                mDPM.lockNow();
//                Log.v(TAG, "ScreenLocker received SUCCESS:" + action);
//                callbackContext.success();
//                result = true;
                callbackContext.error("Not implemented");
                result = false;
            } else if (ACTION_UNLOCK_WINDOW.equals(action)) {
//                Unlock
//                http://developer.android.com/reference/android/app/Activity.html#getWindow()
                Log.v(TAG, "ScreenLocker received ACTION_UNLOCK_WINDOW");
                cordova.getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Window window = cordova.getActivity().getWindow();
                        window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
                        window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                        window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
                        Log.v(TAG, "ScreenLocker received SUCCESS:" + action);
                        callbackContext.success();
                    }
                });

                result = true;
            } else if (ACTION_UNLOCK.equals(action)) {
//                Unlock
//                http://developer.android.com/reference/android/app/Activity.html#getWindow()
                Log.v(TAG, "ScreenLocker received ACTION_UNLOCK");
                cordova.getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Window window = cordova.getActivity().getWindow();
                    	window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                    	window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
                    	window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
		
                    	//KeyguardManager keyguardManager = (KeyguardManager) cordova.getActivity().getSystemService(Context.KEYGUARD_SERVICE);
                        //KeyguardManager.KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("Tag");
                        //keyguardLock.disableKeyguard();
                        
                        PowerManager powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE);
                        PowerManager.WakeLock wakeLock = powerManager.newWakeLock((PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE), "TAG");
                        if(wakeLock.isHeld()) {
                            wakeLock.release();
                        }
                        wakeLock.acquire(5);

                        Log.v(TAG, "ScreenLocker received SUCCESS:" + action);
                        callbackContext.success();
                    }
                });

                result = true;
            } else {
                callbackContext.error("Invalid action");
                result = false;
            }
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
            callbackContext.error(e.getMessage());
            result = false;
        }

        return result;
    }
}
@@ -1606,25 +1606,8 @@
 se;%0A
-// Shows a toast%0A
     
@@ -1731,16 +1731,84 @@
 ect(0);%0A
+            final int  acquireTime = arg_object.getInt(%22timeout%22);%0A%0A
         
@@ -1849,38 +1849,8 @@
 ) %7B%0A
-//                Lock device%0A
     
@@ -1946,501 +1946,8 @@
 %22);%0A
-//                WindowManager wm = (WindowManager) this.cordova.getActivity().getSystemService(this.cordova.getActivity().WINDOW_SERVICE);%0A//                DevicePolicyManager mDPM;%0A//                mDPM = (DevicePolicyManager) this.cordova.getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);%0A//                mDPM.lockNow();%0A//                Log.v(TAG, %22ScreenLocker received SUCCESS:%22 + action);%0A//                callbackContext.success();%0A//                result = true;%0A
     
@@ -2098,128 +2098,8 @@
 ) %7B%0A
-//                Unlock%0A//                http://developer.android.com/reference/android/app/Activity.html#getWindow()%0A
     
@@ -2874,128 +2874,8 @@
 ) %7B%0A
-//                Unlock%0A//                http://developer.android.com/reference/android/app/Activity.html#getWindow()%0A
     
@@ -3417,346 +3417,9 @@
 N);%0A
-%09%09%0A                    %09//KeyguardManager keyguardManager = (KeyguardManager) cordova.getActivity().getSystemService(Context.KEYGUARD_SERVICE);%0A                        //KeyguardManager.KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock(%22Tag%22);%0A                        //keyguardLock.disableKeyguard();%0A                        
 %0A
+
     
@@ -3901,13 +3901,22 @@
 ire(
-5
+acquireTime
 );%0A
-%0A
     

Support WakeLock parameter 'acquire'