Skip to content

Commit 6b522e9

Browse files
committed
Some fixes and enhancements for Android
1 parent b5950b5 commit 6b522e9

File tree

3 files changed

+247
-215
lines changed

3 files changed

+247
-215
lines changed

src/android/BackgroundExt.java

Lines changed: 112 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Licensed to the Apache Software Foundation (ASF) under one
3030
import android.os.Build;
3131
import android.os.PowerManager;
3232
import android.view.View;
33-
import android.view.Window;
3433

3534
import org.apache.cordova.CallbackContext;
3635
import org.apache.cordova.CordovaInterface;
@@ -39,121 +38,114 @@ Licensed to the Apache Software Foundation (ASF) under one
3938
import org.apache.cordova.PluginResult;
4039
import org.apache.cordova.PluginResult.Status;
4140

42-
import java.lang.ref.WeakReference;
4341
import java.util.List;
4442

4543
import static android.content.Context.ACTIVITY_SERVICE;
4644
import static android.content.Context.POWER_SERVICE;
45+
import static android.os.Build.VERSION.SDK_INT;
4746
import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
4847
import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
4948
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
5049
import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
5150

51+
/**
52+
* Implements extended functions around the main purpose
53+
* of infinite execution in the background.
54+
*/
5255
class BackgroundExt {
5356

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;
5659

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;
5962

63+
// To keep the device awake
6064
private PowerManager.WakeLock wakeLock;
6165

6266
/**
6367
* Initialize the extension to perform non-background related tasks.
6468
*
6569
* @param plugin The cordova plugin.
6670
*/
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;
7075
}
7176

7277
/**
73-
* Executes the request asynchronous.
78+
* Executes the request within a thread.
7479
*
75-
* @param plugin The cordova plugin.
7680
* @param action The action to execute.
7781
* @param callback The callback context used when
7882
* calling back into JavaScript.
7983
*/
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));
9287
}
9388

94-
// codebeat:disable[ABC]
95-
9689
/**
9790
* Executes the request.
9891
*
9992
* @param action The action to execute.
10093
* @param callback The callback context used when
10194
* calling back into JavaScript.
10295
*/
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;
132122
}
133123
}
134124

135-
// codebeat:enable[ABC]
136-
137125
/**
138-
* Move app to background.
126+
* Moves the app to the background.
139127
*/
140-
private void moveToBackground() {
128+
private void moveToBackground()
129+
{
141130
Intent intent = new Intent(Intent.ACTION_MAIN);
142131

143132
intent.addCategory(Intent.CATEGORY_HOME);
133+
144134
getApp().startActivity(intent);
145135
}
146136

147137
/**
148-
* Move app to foreground.
138+
* Moves the app to the foreground.
149139
*/
150-
private void moveToForeground() {
140+
private void moveToForeground()
141+
{
151142
Activity app = getApp();
152143
Intent intent = getLaunchIntent();
153144

154145
intent.addFlags(
155146
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
156-
Intent.FLAG_ACTIVITY_SINGLE_TOP);
147+
Intent.FLAG_ACTIVITY_SINGLE_TOP |
148+
Intent.FLAG_ACTIVITY_CLEAR_TOP);
157149

158150
app.startActivity(intent);
159151
}
@@ -166,18 +158,15 @@ private void disableWebViewOptimizations() {
166158
public void run() {
167159
try {
168160
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);
181170
}
182171
});
183172
} catch (InterruptedException e) {
@@ -190,13 +179,14 @@ public void run() {
190179
}
191180

192181
/**
193-
* Exclude the app from the recent tasks list.
182+
* Excludes the app from the recent tasks list.
194183
*/
195184
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
196-
private void excludeFromTaskList() {
185+
private void excludeFromTaskList()
186+
{
197187
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
198188

199-
if (am == null || Build.VERSION.SDK_INT < 21)
189+
if (am == null || SDK_INT < 21)
200190
return;
201191

202192
List<AppTask> tasks = am.getAppTasks();
@@ -208,24 +198,29 @@ private void excludeFromTaskList() {
208198
}
209199

210200
/**
211-
* Invoke the callback with information if the screen is on.
201+
* Invokes the callback with information if the screen is on.
212202
*
213203
* @param callback The callback to invoke.
214204
*/
215205
@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);
219212
}
220213

221214
/**
222-
* If the screen is active.
215+
* Returns if the screen is active.
223216
*/
224217
@SuppressWarnings("deprecation")
225-
private boolean isDimmed() {
218+
private boolean isDimmed()
219+
{
226220
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
227221

228-
if (Build.VERSION.SDK_INT < 20) {
222+
if (SDK_INT < 20)
223+
{
229224
return !pm.isScreenOn();
230225
}
231226

@@ -235,7 +230,8 @@ private boolean isDimmed() {
235230
/**
236231
* Wakes up the device if the screen isn't still on.
237232
*/
238-
private void wakeup() {
233+
private void wakeup()
234+
{
239235
try {
240236
acquireWakeLock();
241237
} catch (Exception e) {
@@ -246,72 +242,74 @@ private void wakeup() {
246242
/**
247243
* Unlocks the device even with password protection.
248244
*/
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+
});
252251
}
253252

254253
/**
255-
* Acquire a wake lock to wake up the device.
254+
* Acquires a wake lock to wake up the device.
256255
*/
257-
private void acquireWakeLock() {
256+
@SuppressWarnings("deprecation")
257+
private void acquireWakeLock()
258+
{
258259
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
259260

260261
releaseWakeLock();
261262

262-
if (!isDimmed()) {
263+
if (!isDimmed())
263264
return;
264-
}
265265

266266
int level = PowerManager.SCREEN_DIM_WAKE_LOCK |
267267
PowerManager.ACQUIRE_CAUSES_WAKEUP;
268268

269-
wakeLock = pm.newWakeLock(level, "BackgroundModeExt");
269+
wakeLock = pm.newWakeLock(level, "backgroundmode:wakelock");
270270
wakeLock.setReferenceCounted(false);
271271
wakeLock.acquire(1000);
272272
}
273273

274274
/**
275275
* Releases the previously acquire wake lock.
276276
*/
277-
private void releaseWakeLock() {
277+
private void releaseWakeLock()
278+
{
278279
if (wakeLock != null && wakeLock.isHeld()) {
279280
wakeLock.release();
280281
wakeLock = null;
281282
}
282283
}
283284

284285
/**
285-
* Add required flags to the window to unlock/wakeup the device.
286+
* Adds required flags to the window to unlock/wakeup the device.
286287
*/
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+
}
289292

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));
300299
}
301300

302301
/**
303-
* The activity referenced by cordova.
304-
*
305-
* @return The main activity of the app.
302+
* Returns the activity referenced by cordova.
306303
*/
307304
Activity getApp() {
308-
return cordova.get().getActivity();
305+
return cordova.getActivity();
309306
}
310307

311308
/**
312-
* The launch intent for the main activity.
309+
* Gets the launch intent for the main activity.
313310
*/
314-
private Intent getLaunchIntent() {
311+
private Intent getLaunchIntent()
312+
{
315313
Context app = getApp().getApplicationContext();
316314
String pkgName = app.getPackageName();
317315

@@ -322,11 +320,9 @@ private Intent getLaunchIntent() {
322320
* Get the requested system service by name.
323321
*
324322
* @param name The name of the service.
325-
*
326-
* @return The service instance.
327323
*/
328-
private Object getService(String name) {
324+
private Object getService(String name)
325+
{
329326
return getApp().getSystemService(name);
330327
}
331-
332328
}

0 commit comments

Comments
 (0)