package by.chemerisuk.cordova.coreextensions;

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

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;

import android.content.Context;
import android.content.Intent;
import android.view.WindowManager.LayoutParams;


public class CoreAndroidExtensions extends CordovaPlugin {
    public static final String PLUGIN_NAME = "CoreAndroidExtensions";

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action            The action to execute.
     * @param args              JSONArry of arguments for the plugin.
     * @param callbackContext   The callback context from which we were invoked.
     * @return                  A PluginResult object with a status and message.
     */
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("minimizeApp")) {
            this.minimizeApp(args.optBoolean(0, false));
        } else if (action.equals("resumeApp")) {
            this.resumeApp(args.optBoolean(0, false));
        }

        callbackContext.success();

        return true;
    }

    /**
     * Called when the system is about to start resuming a previous activity.
     *
     * @param multitasking
     *      Flag indicating if multitasking is turned on for app
     */
    @Override
    public void onPause(boolean multitasking) {
        clearWindowFlags();
    }

    private void minimizeApp(boolean moveBack) {
        // try to send it back and back to previous app
        if (moveBack) {
            moveBack = cordova.getActivity().moveTaskToBack(true);
        }

        // if not possible jump to home
        if (!moveBack) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            cordova.getActivity().startActivity(intent);
        }
    }

    private void resumeApp(boolean force) {
        Context ctx = cordova.getActivity().getApplicationContext();
        Intent intent = new Intent(ctx, cordova.getActivity().getClass());

        intent.setAction(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_NO_ANIMATION);

        ctx.startActivity(intent);

        if (force) {
            setupWindowFlags();
        }
    }

    private void setupWindowFlags() {
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                cordova.getActivity().getWindow().addFlags(
                    LayoutParams.FLAG_SHOW_WHEN_LOCKED | LayoutParams.FLAG_DISMISS_KEYGUARD | LayoutParams.FLAG_TURN_SCREEN_ON);
            }
        });
    }

    private void clearWindowFlags() {
        cordova.getActivity().getWindow().clearFlags(
            LayoutParams.FLAG_SHOW_WHEN_LOCKED | LayoutParams.FLAG_DISMISS_KEYGUARD | LayoutParams.FLAG_TURN_SCREEN_ON);
    }

}
@@ -465,16 +465,133 @@
 nsions%22;
+%0A    public static final int UNINSTALL_REQUEST_CODE = 5523345;%0A%0A    private CallbackContext uninstallCallbackContext;
 %0A%0A    /*
@@ -1154,32 +1154,72 @@
 lean(0, false));
+%0A%0A            callbackContext.success();
 %0A        %7D else 
@@ -1311,53 +1311,289 @@
 ));%0A
-        %7D%0A%0A        callbackContext.success();
+%0A            callbackContext.success();%0A        %7D else if (action.equals(%22uninstallApp%22)) %7B%0A            this.uninstallApp(args.getString(0), callbackContext);%0A        %7D else if (action.equals(%22detectApp%22)) %7B%0A            this.detectApp(args.getString(0), callbackContext);%0A        %7D
 %0A%0A  
@@ -2868,32 +2868,908 @@
        %7D%0A    %7D%0A%0A
+    private void uninstallApp(String packageName, CallbackContext callbackContext) %7B%0A        Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);%0A        intent.setData(Uri.parse(%22package:%22 + packageName));%0A        intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);%0A        this.cordova.startActivityForResult(this, intent, UNINSTALL_REQUEST_CODE);%0A        this.uninstallCallbackContext = callbackContext;%0A    %7D%0A%0A    private void detectApp(String packageName, CallbackContext callbackContext) %7B%0A        Context ctx = this.cordova.getActivity().getApplicationContext();%0A        PackageManager pm = ctx.getPackageManager();%0A%0A        try %7B%0A            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);%0A%0A            callbackContext.success(1);%0A        %7D catch(PackageManager.NameNotFoundException e) %7B%0A            callbackContext.success(0);%0A        %7D%0A    %7D%0A%0A
     private void
@@ -4325,9 +4325,446 @@
 %0A    %7D%0A%0A
+    public void onActivityResult(int requestCode, int resultCode, Intent intent) %7B%0A        if (requestCode == UNINSTALL_REQUEST_CODE) %7B%0A            if (uninstallCallbackContext != null) %7B%0A                if (resultCode == Activity.RESULT_OK) %7B%0A                    uninstallCallbackContext.success();%0A                %7D else %7B%0A                    uninstallCallbackContext.error(resultCode);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A
 %7D

add new methods uninstallApp and detectApp