forked from huangang/cordova-plugin-restartapp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RestartApp.java
122 lines (104 loc) · 3.97 KB
/
RestartApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.apache.cordova.restartapp;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.app.PendingIntent;
import android.app.Activity;
import android.app.AlarmManager;
import android.content.SharedPreferences;
import android.content.Intent;
import android.content.Context;
import android.content.pm.PackageManager;
import app.mws.my.guard.b.MainActivity;
public class RestartApp extends CordovaPlugin {
/**
* Tag for debug log messages
*/
public static final String TAG = "RestartApp";
public static RestartApp instance = null;
protected Context applicationContext;
protected SharedPreferences sharedPref;
protected CallbackContext currentContext;
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
Log.d(TAG, "initialize()");
instance = this;
applicationContext = this.cordova.getActivity().getApplicationContext();
sharedPref = cordova.getActivity().getSharedPreferences(TAG, Activity.MODE_PRIVATE);
super.initialize(cordova, webView);
}
public static RestartApp getInstance(){
return instance;
}
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
currentContext = callbackContext;
try {
if (action.equals("restartApp")) {
this.restart();
}
} catch(Exception e ) {
handleError("Exception occurred: ".concat(e.getMessage()));
return false;
}
return true;
}
protected void restart() throws Exception{
String baseError = "Unable to cold restart application: ";
try {
// this works for me (line 24 modify required)
Intent intent = new Intent(applicationContext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
applicationContext.startActivity(intent);
Runtime.getRuntime().exit(0);
} catch (Exception ex) {
handleError(baseError+ ex.getMessage());
}
}
public String escapeDoubleQuotes(String string){
String escapedString = string.replace("\"", "\\\"");
escapedString = escapedString.replace("%22", "\\%22");
return escapedString;
}
public void executeGlobalJavascript(final String jsString){
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
webView.loadUrl("javascript:" + jsString);
}
});
}
/**
* Handles an error while executing a plugin API method in the specified context.
* Calls the registered Javascript plugin error handler callback.
* @param errorMsg Error message to pass to the JS error handler
*/
public void handleError(String errorMsg, CallbackContext context){
try {
logError(errorMsg);
context.error(errorMsg);
} catch (Exception e) {
logError(e.toString());
}
}
/**
* Handles an error while executing a plugin API method in the current context.
* Calls the registered Javascript plugin error handler callback.
* @param errorMsg Error message to pass to the JS error handler
*/
public void handleError(String errorMsg) {
handleError(errorMsg, currentContext);
}
public void logInfo(String msg){
Log.i(TAG, msg);
executeGlobalJavascript("console.info(\""+TAG+"[native]: "+escapeDoubleQuotes(msg)+"\")");
}
public void logError(String msg){
Log.e(TAG, msg);
executeGlobalJavascript("console.error(\""+TAG+"[native]: "+escapeDoubleQuotes(msg)+"\")");
}
}