package edu.berkeley.eecs.emission.cordova.launchnative;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;

public class LaunchNative extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (action.equals("launch")) {
            java.lang.Package pkg = cordova.getClass().getPackage();
            System.out.println("cordova has package name "+pkg);
            String activityName = data.getString(0);
            String fqcn = pkg.getName()+"."+activityName;
            System.out.println("FQCN = "+pkg.getName() + fqcn);
            try {
                Class fqClass = Class.forName(fqcn);
                Intent startIntent = new Intent(cordova.getActivity(), fqClass);
                cordova.getActivity().startActivity(startIntent);
                callbackContext.success("Launched activity " + activityName);
            } catch (ClassNotFoundException e) {
                callbackContext.error("Class "+fqcn+" not found, unable to launch");
            }
            return true;
        } else {
            return false;
        }
    }
}

package edu.berkeley.eecs.emission.cordova.launchnative;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;

public class LaunchNative extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (action.equals("launch")) {
            String pkgName = cordova.getActivity().getPackageName();
            System.out.println("cordova has package name "+pkgName);
            String activityName = data.getString(0);
            String fqcn = pkgName+"."+activityName;
            System.out.println("FQCN = "+ fqcn);
            try {
                Class fqClass = Class.forName(fqcn);
                Intent startIntent = new Intent(cordova.getActivity(), fqClass);
                cordova.getActivity().startActivity(startIntent);
                callbackContext.success("Launched activity " + activityName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                callbackContext.error("Class "+fqcn+" not found, unable to launch");
            }
            return true;
        } else {
            return false;
        }
    }
}

Read the correct package name
Read the correct package name

We can't read the package name from the cordova interface since that is in the
org.apache.cordova package. Instead, we get it from the cordova activity. So as
long as the new screen is in the same package as the cordova activity, we are
fine.
