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();
        } 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() {
        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);
    }

    private void startActivity(JSONObject options) {
        String packageName = options.optString("package", "");
        int flags = options.optInt("flags", 0);
        JSONObject extras = options.optJSONObject("extras");

        Context ctx = cordova.getActivity().getApplicationContext();
        Intent intent;

        if (packageName.isEmpty()) {
            intent = new Intent(ctx, cordova.getActivity().getClass());
        } else {
            intent = new Intent();
            intent.setPackage(packageName);
        }

        intent.setAction(options.optString("action", Intent.ACTION_MAIN));

        if (flags > 0) {
            intent.setFlags(flags);
        }

        if (extras != null) {
            for (String key : extras.keys()) {
                intent.putExtra(key, extras.get(key));
            }
        }

        ctx.startActivity(intent);
    }

}
@@ -2774,887 +2774,5 @@
  %7D%0A%0A
-    private void startActivity(JSONObject options) %7B%0A        String packageName = options.optString(%22package%22, %22%22);%0A        int flags = options.optInt(%22flags%22, 0);%0A        JSONObject extras = options.optJSONObject(%22extras%22);%0A%0A        Context ctx = cordova.getActivity().getApplicationContext();%0A        Intent intent;%0A%0A        if (packageName.isEmpty()) %7B%0A            intent = new Intent(ctx, cordova.getActivity().getClass());%0A        %7D else %7B%0A            intent = new Intent();%0A            intent.setPackage(packageName);%0A        %7D%0A%0A        intent.setAction(options.optString(%22action%22, Intent.ACTION_MAIN));%0A%0A        if (flags %3E 0) %7B%0A            intent.setFlags(flags);%0A        %7D%0A%0A        if (extras != null) %7B%0A            for (String key : extras.keys()) %7B%0A                intent.putExtra(key, extras.get(key));%0A            %7D%0A        %7D%0A%0A        ctx.startActivity(intent);%0A    %7D%0A%0A
 %7D

remove startActivity