Skip to content

Commit 62d5b8b

Browse files
authored
Merge pull request JaneaSystems#7 from okhiroyuki/feature/add-reset
Feature/add reset
2 parents d6b1642 + 3e1765d commit 62d5b8b

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.util.Log;
1616
import android.app.Activity;
1717
import android.content.Context;
18+
import android.content.Intent;
1819
import android.content.res.AssetManager;
1920
import android.content.pm.PackageInfo;
2021
import android.content.pm.PackageManager;
@@ -50,6 +51,7 @@ public class NodeJS extends CordovaPlugin {
5051

5152
private static final String SHARED_PREFS = "NODEJS_MOBILE_PREFS";
5253
private static final String LAST_UPDATED_TIME = "NODEJS_MOBILE_APK_LastUpdateTime";
54+
private static final String FORCE_RESET = "NODEJS_MOBILE_RESET";
5355
private long lastUpdateTime = 1;
5456
private long previousLastUpdateTime = 0;
5557

@@ -107,7 +109,7 @@ public void pluginInitialize() {
107109
}
108110

109111
private void asyncInit() {
110-
if (wasAPKUpdated() || isEmptyNodeModules()) {
112+
if (wasAPKUpdated() || isReset()) {
111113
try {
112114
initSemaphore.acquire();
113115
new Thread(new Runnable() {
@@ -151,6 +153,8 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
151153
String scriptBody = data.getString(0);
152154
JSONObject startOptions = data.getJSONObject(1);
153155
this.startEngineWithScript(scriptBody, startOptions, callbackContext);
156+
} else if (action.equals("reset")) {
157+
this.setReset();
154158
} else {
155159
Log.e(LOGTAG, "Invalid action: " + action);
156160
return false;
@@ -367,6 +371,28 @@ private boolean isEmptyNodeModules(){
367371
return !nodejsModulesFolder.exists();
368372
}
369373

374+
private void setReset() {
375+
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
376+
SharedPreferences.Editor editor = prefs.edit();
377+
editor.putBoolean(FORCE_RESET, true);
378+
editor.commit();
379+
doColdRestart();
380+
}
381+
382+
private void clearReset() {
383+
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
384+
SharedPreferences.Editor editor = prefs.edit();
385+
editor.remove(FORCE_RESET);
386+
editor.commit();
387+
}
388+
389+
private boolean isReset() {
390+
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
391+
boolean result = prefs.getBoolean(FORCE_RESET, false);
392+
clearReset();
393+
return result;
394+
}
395+
370396
private boolean wasAPKUpdated() {
371397
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
372398
this.previousLastUpdateTime = prefs.getLong(LAST_UPDATED_TIME, 0);
@@ -578,4 +604,54 @@ private static boolean getOptionRedirectOutputToLogcat(final JSONObject startOpt
578604
}
579605
return result;
580606
}
607+
608+
/**
609+
* Performs a full cold app restart - restarts application
610+
* https://stackoverflow.com/a/22345538/777265
611+
*/
612+
protected void doColdRestart() {
613+
String baseError = "Unable to cold restart application: ";
614+
try {
615+
Log.d(LOGTAG, "Cold restarting application");
616+
Context c = context;
617+
//check if the context is given
618+
if (c != null) {
619+
//fetch the packagemanager so we can get the default launch activity
620+
// (you can replace this intent with any other activity if you want
621+
PackageManager pm = c.getPackageManager();
622+
//check if we got the PackageManager
623+
if (pm != null) {
624+
//create the intent with the default start activity for your application
625+
Intent mStartActivity = pm.getLaunchIntentForPackage(
626+
c.getPackageName()
627+
);
628+
if (mStartActivity != null) {
629+
//mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
630+
//create a pending intent so the application is restarted after System.exit(0) was called.
631+
// We use an AlarmManager to call this intent in 100ms
632+
// int mPendingIntentId = 223344;
633+
// PendingIntent mPendingIntent = PendingIntent
634+
// .getActivity(c, mPendingIntentId, mStartActivity,
635+
// PendingIntent.FLAG_CANCEL_CURRENT);
636+
// AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
637+
// mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
638+
mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
639+
c.getApplicationContext().startActivity(mStartActivity);
640+
641+
Log.i(LOGTAG,"Killing application for cold restart");
642+
//kill the application
643+
System.exit(0);
644+
} else {
645+
Log.e(LOGTAG, baseError + "StartActivity is null");
646+
}
647+
} else {
648+
Log.e(LOGTAG, baseError + "PackageManager is null");
649+
}
650+
} else {
651+
Log.e(LOGTAG, baseError + "Context is null");
652+
}
653+
} catch (Exception ex) {
654+
ex.printStackTrace();
655+
}
656+
}
581657
}

www/nodejs_apis.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ function startWithScript(script, callback, options) {
142142
startEngine('startEngineWithScript', [script, options], callback);
143143
};
144144

145+
function reset(callback) {
146+
startEngine('reset', [], callback);
147+
};
148+
145149
const eventChannel = new EventChannel(EVENT_CHANNEL);
146150
registerChannel(eventChannel);
147151

148152
module.exports = exports = {
149153
start,
150154
startWithScript,
155+
reset,
151156
channel: eventChannel
152157
};

0 commit comments

Comments
 (0)