@@ -30,7 +30,6 @@ Licensed to the Apache Software Foundation (ASF) under one
30
30
import android .os .Build ;
31
31
import android .os .PowerManager ;
32
32
import android .view .View ;
33
- import android .view .Window ;
34
33
35
34
import org .apache .cordova .CallbackContext ;
36
35
import org .apache .cordova .CordovaInterface ;
@@ -39,121 +38,114 @@ Licensed to the Apache Software Foundation (ASF) under one
39
38
import org .apache .cordova .PluginResult ;
40
39
import org .apache .cordova .PluginResult .Status ;
41
40
42
- import java .lang .ref .WeakReference ;
43
41
import java .util .List ;
44
42
45
43
import static android .content .Context .ACTIVITY_SERVICE ;
46
44
import static android .content .Context .POWER_SERVICE ;
45
+ import static android .os .Build .VERSION .SDK_INT ;
47
46
import static android .view .WindowManager .LayoutParams .FLAG_ALLOW_LOCK_WHILE_SCREEN_ON ;
48
47
import static android .view .WindowManager .LayoutParams .FLAG_DISMISS_KEYGUARD ;
49
48
import static android .view .WindowManager .LayoutParams .FLAG_SHOW_WHEN_LOCKED ;
50
49
import static android .view .WindowManager .LayoutParams .FLAG_TURN_SCREEN_ON ;
51
50
51
+ /**
52
+ * Implements extended functions around the main purpose
53
+ * of infinite execution in the background.
54
+ */
52
55
class BackgroundExt {
53
56
54
- // Weak reference to the cordova interface passed by the plugin
55
- private final WeakReference < CordovaInterface > cordova ;
57
+ // Reference to the cordova interface passed by the plugin
58
+ private final CordovaInterface cordova ;
56
59
57
- // Weak reference to the cordova web view passed by the plugin
58
- private final WeakReference < CordovaWebView > webView ;
60
+ // Reference to the cordova web view passed by the plugin
61
+ private final CordovaWebView webView ;
59
62
63
+ // To keep the device awake
60
64
private PowerManager .WakeLock wakeLock ;
61
65
62
66
/**
63
67
* Initialize the extension to perform non-background related tasks.
64
68
*
65
69
* @param plugin The cordova plugin.
66
70
*/
67
- private BackgroundExt (CordovaPlugin plugin ) {
68
- this .cordova = new WeakReference <CordovaInterface >(plugin .cordova );
69
- this .webView = new WeakReference <CordovaWebView >(plugin .webView );
71
+ BackgroundExt (CordovaPlugin plugin )
72
+ {
73
+ this .cordova = plugin .cordova ;
74
+ this .webView = plugin .webView ;
70
75
}
71
76
72
77
/**
73
- * Executes the request asynchronous .
78
+ * Executes the request within a thread .
74
79
*
75
- * @param plugin The cordova plugin.
76
80
* @param action The action to execute.
77
81
* @param callback The callback context used when
78
82
* calling back into JavaScript.
79
83
*/
80
- @ SuppressWarnings ("UnusedParameters" )
81
- static void execute (CordovaPlugin plugin , final String action ,
82
- final CallbackContext callback ) {
83
-
84
- final BackgroundExt ext = new BackgroundExt (plugin );
85
-
86
- plugin .cordova .getThreadPool ().execute (new Runnable () {
87
- @ Override
88
- public void run () {
89
- ext .execute (action , callback );
90
- }
91
- });
84
+ void executeAsync (String action , CallbackContext callback )
85
+ {
86
+ cordova .getThreadPool ().execute (() -> execute (action , callback ));
92
87
}
93
88
94
- // codebeat:disable[ABC]
95
-
96
89
/**
97
90
* Executes the request.
98
91
*
99
92
* @param action The action to execute.
100
93
* @param callback The callback context used when
101
94
* calling back into JavaScript.
102
95
*/
103
- private void execute (String action , CallbackContext callback ) {
104
-
105
- if (action .equalsIgnoreCase ("optimizations" )) {
106
- disableWebViewOptimizations ();
107
- }
108
-
109
- if (action .equalsIgnoreCase ("background" )) {
110
- moveToBackground ();
111
- }
112
-
113
- if (action .equalsIgnoreCase ("foreground" )) {
114
- moveToForeground ();
115
- }
116
-
117
- if (action .equalsIgnoreCase ("tasklist" )) {
118
- excludeFromTaskList ();
119
- }
120
-
121
- if (action .equalsIgnoreCase ("dimmed" )) {
122
- isDimmed (callback );
123
- }
124
-
125
- if (action .equalsIgnoreCase ("wakeup" )) {
126
- wakeup ();
127
- }
128
-
129
- if (action .equalsIgnoreCase ("unlock" )) {
130
- wakeup ();
131
- unlock ();
96
+ private void execute (String action , CallbackContext callback )
97
+ {
98
+ switch (action )
99
+ {
100
+ case "optimizations" :
101
+ disableWebViewOptimizations ();
102
+ break ;
103
+ case "background" :
104
+ moveToBackground ();
105
+ break ;
106
+ case "foreground" :
107
+ moveToForeground ();
108
+ break ;
109
+ case "tasklist" :
110
+ excludeFromTaskList ();
111
+ break ;
112
+ case "dimmed" :
113
+ isDimmed (callback );
114
+ break ;
115
+ case "wakeup" :
116
+ wakeup ();
117
+ break ;
118
+ case "unlock" :
119
+ wakeup ();
120
+ unlock ();
121
+ break ;
132
122
}
133
123
}
134
124
135
- // codebeat:enable[ABC]
136
-
137
125
/**
138
- * Move app to background.
126
+ * Moves the app to the background.
139
127
*/
140
- private void moveToBackground () {
128
+ private void moveToBackground ()
129
+ {
141
130
Intent intent = new Intent (Intent .ACTION_MAIN );
142
131
143
132
intent .addCategory (Intent .CATEGORY_HOME );
133
+
144
134
getApp ().startActivity (intent );
145
135
}
146
136
147
137
/**
148
- * Move app to foreground.
138
+ * Moves the app to the foreground.
149
139
*/
150
- private void moveToForeground () {
140
+ private void moveToForeground ()
141
+ {
151
142
Activity app = getApp ();
152
143
Intent intent = getLaunchIntent ();
153
144
154
145
intent .addFlags (
155
146
Intent .FLAG_ACTIVITY_REORDER_TO_FRONT |
156
- Intent .FLAG_ACTIVITY_SINGLE_TOP );
147
+ Intent .FLAG_ACTIVITY_SINGLE_TOP |
148
+ Intent .FLAG_ACTIVITY_CLEAR_TOP );
157
149
158
150
app .startActivity (intent );
159
151
}
@@ -166,18 +158,15 @@ private void disableWebViewOptimizations() {
166
158
public void run () {
167
159
try {
168
160
Thread .sleep (1000 );
169
- getApp ().runOnUiThread (new Runnable () {
170
- @ Override
171
- public void run () {
172
- View view = webView .get ().getEngine ().getView ();
173
-
174
- try {
175
- Class .forName ("org.crosswalk.engine.XWalkCordovaView" )
176
- .getMethod ("onShow" )
177
- .invoke (view );
178
- } catch (Exception e ){
179
- view .dispatchWindowVisibilityChanged (View .VISIBLE );
180
- }
161
+ getApp ().runOnUiThread (() -> {
162
+ View view = webView .getEngine ().getView ();
163
+
164
+ try {
165
+ Class .forName ("org.crosswalk.engine.XWalkCordovaView" )
166
+ .getMethod ("onShow" )
167
+ .invoke (view );
168
+ } catch (Exception e ){
169
+ view .dispatchWindowVisibilityChanged (View .VISIBLE );
181
170
}
182
171
});
183
172
} catch (InterruptedException e ) {
@@ -190,13 +179,14 @@ public void run() {
190
179
}
191
180
192
181
/**
193
- * Exclude the app from the recent tasks list.
182
+ * Excludes the app from the recent tasks list.
194
183
*/
195
184
@ TargetApi (Build .VERSION_CODES .LOLLIPOP )
196
- private void excludeFromTaskList () {
185
+ private void excludeFromTaskList ()
186
+ {
197
187
ActivityManager am = (ActivityManager ) getService (ACTIVITY_SERVICE );
198
188
199
- if (am == null || Build . VERSION . SDK_INT < 21 )
189
+ if (am == null || SDK_INT < 21 )
200
190
return ;
201
191
202
192
List <AppTask > tasks = am .getAppTasks ();
@@ -208,24 +198,29 @@ private void excludeFromTaskList() {
208
198
}
209
199
210
200
/**
211
- * Invoke the callback with information if the screen is on.
201
+ * Invokes the callback with information if the screen is on.
212
202
*
213
203
* @param callback The callback to invoke.
214
204
*/
215
205
@ SuppressWarnings ("deprecation" )
216
- private void isDimmed (CallbackContext callback ) {
217
- PluginResult result = new PluginResult (Status .OK , isDimmed ());
218
- callback .sendPluginResult (result );
206
+ private void isDimmed (CallbackContext callback )
207
+ {
208
+ boolean status = isDimmed ();
209
+ PluginResult res = new PluginResult (Status .OK , status );
210
+
211
+ callback .sendPluginResult (res );
219
212
}
220
213
221
214
/**
222
- * If the screen is active.
215
+ * Returns if the screen is active.
223
216
*/
224
217
@ SuppressWarnings ("deprecation" )
225
- private boolean isDimmed () {
218
+ private boolean isDimmed ()
219
+ {
226
220
PowerManager pm = (PowerManager ) getService (POWER_SERVICE );
227
221
228
- if (Build .VERSION .SDK_INT < 20 ) {
222
+ if (SDK_INT < 20 )
223
+ {
229
224
return !pm .isScreenOn ();
230
225
}
231
226
@@ -235,7 +230,8 @@ private boolean isDimmed() {
235
230
/**
236
231
* Wakes up the device if the screen isn't still on.
237
232
*/
238
- private void wakeup () {
233
+ private void wakeup ()
234
+ {
239
235
try {
240
236
acquireWakeLock ();
241
237
} catch (Exception e ) {
@@ -246,72 +242,74 @@ private void wakeup() {
246
242
/**
247
243
* Unlocks the device even with password protection.
248
244
*/
249
- private void unlock () {
250
- Intent intent = getLaunchIntent ();
251
- getApp ().startActivity (intent );
245
+ private void unlock ()
246
+ {
247
+ getApp ().runOnUiThread (() -> {
248
+ addSreenAndKeyguardFlags ();
249
+ getApp ().startActivity (getLaunchIntent ());
250
+ });
252
251
}
253
252
254
253
/**
255
- * Acquire a wake lock to wake up the device.
254
+ * Acquires a wake lock to wake up the device.
256
255
*/
257
- private void acquireWakeLock () {
256
+ @ SuppressWarnings ("deprecation" )
257
+ private void acquireWakeLock ()
258
+ {
258
259
PowerManager pm = (PowerManager ) getService (POWER_SERVICE );
259
260
260
261
releaseWakeLock ();
261
262
262
- if (!isDimmed ()) {
263
+ if (!isDimmed ())
263
264
return ;
264
- }
265
265
266
266
int level = PowerManager .SCREEN_DIM_WAKE_LOCK |
267
267
PowerManager .ACQUIRE_CAUSES_WAKEUP ;
268
268
269
- wakeLock = pm .newWakeLock (level , "BackgroundModeExt " );
269
+ wakeLock = pm .newWakeLock (level , "backgroundmode:wakelock " );
270
270
wakeLock .setReferenceCounted (false );
271
271
wakeLock .acquire (1000 );
272
272
}
273
273
274
274
/**
275
275
* Releases the previously acquire wake lock.
276
276
*/
277
- private void releaseWakeLock () {
277
+ private void releaseWakeLock ()
278
+ {
278
279
if (wakeLock != null && wakeLock .isHeld ()) {
279
280
wakeLock .release ();
280
281
wakeLock = null ;
281
282
}
282
283
}
283
284
284
285
/**
285
- * Add required flags to the window to unlock/wakeup the device.
286
+ * Adds required flags to the window to unlock/wakeup the device.
286
287
*/
287
- static void addWindowFlags (Activity app ) {
288
- final Window window = app .getWindow ();
288
+ private void addSreenAndKeyguardFlags ()
289
+ {
290
+ getApp ().getWindow ().addFlags (FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD );
291
+ }
289
292
290
- app .runOnUiThread (new Runnable () {
291
- public void run () {
292
- window .addFlags (
293
- FLAG_ALLOW_LOCK_WHILE_SCREEN_ON |
294
- FLAG_SHOW_WHEN_LOCKED |
295
- FLAG_TURN_SCREEN_ON |
296
- FLAG_DISMISS_KEYGUARD
297
- );
298
- }
299
- });
293
+ /**
294
+ * Removes required flags to the window to unlock/wakeup the device.
295
+ */
296
+ static void clearKeyguardFlags (Activity app )
297
+ {
298
+ app .runOnUiThread (() -> app .getWindow ().clearFlags (FLAG_DISMISS_KEYGUARD ));
300
299
}
301
300
302
301
/**
303
- * The activity referenced by cordova.
304
- *
305
- * @return The main activity of the app.
302
+ * Returns the activity referenced by cordova.
306
303
*/
307
304
Activity getApp () {
308
- return cordova .get (). getActivity ();
305
+ return cordova .getActivity ();
309
306
}
310
307
311
308
/**
312
- * The launch intent for the main activity.
309
+ * Gets the launch intent for the main activity.
313
310
*/
314
- private Intent getLaunchIntent () {
311
+ private Intent getLaunchIntent ()
312
+ {
315
313
Context app = getApp ().getApplicationContext ();
316
314
String pkgName = app .getPackageName ();
317
315
@@ -322,11 +320,9 @@ private Intent getLaunchIntent() {
322
320
* Get the requested system service by name.
323
321
*
324
322
* @param name The name of the service.
325
- *
326
- * @return The service instance.
327
323
*/
328
- private Object getService (String name ) {
324
+ private Object getService (String name )
325
+ {
329
326
return getApp ().getSystemService (name );
330
327
}
331
-
332
328
}
0 commit comments