Skip to content

Commit

Permalink
Add wakeUp and unlock functions
Browse files Browse the repository at this point in the history
  • Loading branch information
katzer committed Feb 10, 2017
1 parent ff9f063 commit 0f94708
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## ChangeLog
#### Version 0.7.3 (not yet released)
- Check if screen is off on Android
- Wake-up device on Android
- Unlock device on Android

#### Version 0.7.2 (02.02.2017)
- Fixed app freeze on iOS using wkwebview-engine
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ cordova.plugins.backgroundMode.isScreenOff(function(bool) {
});
```

### Unlock and wake-up
A wake-up turns on the screen while unlocking moves the app to foreground even the device is locked.

```js
// Turn screen on
cordova.plugins.backgroundMode.wakeUp();
// Turn screen on and show app even locked
cordova.plugins.backgroundMode.unlock();
```

### Notification
To indicate that the app is executing tasks in background and being paused would disrupt the user, the plug-in has to create a notification while in background - like a download progress bar.

Expand Down
140 changes: 118 additions & 22 deletions src/android/BackgroundExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.AppTask;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import android.view.View;
import android.view.Window;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
Expand All @@ -42,6 +44,10 @@ Licensed to the Apache Software Foundation (ASF) under one

import static android.content.Context.ACTIVITY_SERVICE;
import static android.content.Context.POWER_SERVICE;
import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;

class BackgroundExt {

Expand All @@ -51,6 +57,8 @@ class BackgroundExt {
// Weak reference to the cordova web view passed by the plugin
private final WeakReference<CordovaWebView> webView;

private PowerManager.WakeLock wakeLock;

/**
* Initialize the extension to perform non-background related tasks.
*
Expand Down Expand Up @@ -113,6 +121,15 @@ private void execute (String action, CallbackContext callback) {
if (action.equalsIgnoreCase("dimmed")) {
isDimmed(callback);
}

if (action.equalsIgnoreCase("wakeup")) {
wakeup();
}

if (action.equalsIgnoreCase("unlock")) {
wakeup();
unlock();
}
}

// codebeat:enable[ABC]
Expand All @@ -124,22 +141,19 @@ private void moveToBackground() {
Intent intent = new Intent(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_HOME);
getActivity().startActivity(intent);
getApp().startActivity(intent);
}

/**
* Move app to foreground.
*/
private void moveToForeground() {
Activity app = getActivity();
String pkgName = app.getPackageName();

Intent intent = app
.getPackageManager()
.getLaunchIntentForPackage(pkgName);
Activity app = getApp();
Intent intent = getLaunchIntent();

intent.addFlags( Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
Intent.FLAG_ACTIVITY_SINGLE_TOP);

app.startActivity(intent);
}
Expand All @@ -152,15 +166,15 @@ private void disableWebViewOptimizations() {
public void run() {
try {
Thread.sleep(1000);
getActivity().runOnUiThread(new Runnable() {
getApp().runOnUiThread(new Runnable() {
@Override
public void run() {
View view = webView.get().getEngine().getView();

try {
Class.forName("org.crosswalk.engine.XWalkCordovaView")
.getMethod("onShow")
.invoke(view);
.getMethod("onShow")
.invoke(view);
} catch (Exception e){
view.dispatchWindowVisibilityChanged(View.VISIBLE);
}
Expand Down Expand Up @@ -199,38 +213,120 @@ private void excludeFromTaskList() {
* @param callback The callback to invoke.
*/
@SuppressWarnings("deprecation")
private void isDimmed (CallbackContext callback) {
private void isDimmed(CallbackContext callback) {
PluginResult result = new PluginResult(Status.OK, isDimmed());
callback.sendPluginResult(result);
}

/**
* If the screen is active.
*/
@SuppressWarnings("deprecation")
private boolean isDimmed() {
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
boolean isDimmed;

if (Build.VERSION.SDK_INT < 20) {
isDimmed = !pm.isScreenOn();
} else {
isDimmed = !pm.isInteractive();
return !pm.isScreenOn();
}

PluginResult result = new PluginResult(Status.OK, isDimmed);
callback.sendPluginResult(result);
return !pm.isInteractive();
}

/**
* Wakes up the device if the screen isn't still on.
*/
private void wakeup() {
try {
acquireWakeLock();
} catch (Exception e) {
releaseWakeLock();
}
}

/**
* Unlocks the device even with password protection.
*/
private void unlock() {
Intent intent = getLaunchIntent();
getApp().startActivity(intent);
}

/**
* Acquire a wake lock to wake up the device.
*/
private void acquireWakeLock() {
PowerManager pm = (PowerManager) getService(POWER_SERVICE);

releaseWakeLock();

if (!isDimmed()) {
return;
}

int level = PowerManager.SCREEN_DIM_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP;

wakeLock = pm.newWakeLock(level, "BackgroundModeExt");
wakeLock.setReferenceCounted(false);
wakeLock.acquire(1000);
}

/**
* Releases the previously acquire wake lock.
*/
private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}
}

/**
* Add required flags to the window to unlock/wakeup the device.
*/
static void addWindowFlags(Activity app) {
final Window window = app.getWindow();

app.runOnUiThread(new Runnable() {
public void run() {
window.addFlags(
FLAG_ALLOW_LOCK_WHILE_SCREEN_ON |
FLAG_SHOW_WHEN_LOCKED |
FLAG_TURN_SCREEN_ON |
FLAG_DISMISS_KEYGUARD
);
}
});
}

/**
* The activity referenced by cordova.
*
* @return The main activity of the app.
*/
Activity getActivity() {
Activity getApp() {
return cordova.get().getActivity();
}

/**
* The launch intent for the main activity.
*/
private Intent getLaunchIntent() {
Context app = getApp().getApplicationContext();
String pkgName = app.getPackageName();

return app.getPackageManager().getLaunchIntentForPackage(pkgName);
}

/**
* Get the requested system service by name.
*
* @param name The name of the service.
*
* @return The service instance.
*/
private Object getService (String name) {
return getActivity().getSystemService(name);
private Object getService(String name) {
return getApp().getSystemService(name);
}

}
11 changes: 8 additions & 3 deletions src/android/BackgroundMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.json.JSONException;
import org.json.JSONObject;

import de.appplant.cordova.plugin.background.ForegroundService.ForegroundBinder;

import static android.content.Context.BIND_AUTO_CREATE;

public class BackgroundMode extends CordovaPlugin {
Expand Down Expand Up @@ -65,9 +67,7 @@ private enum Event {
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ForegroundService.ForegroundBinder binder =
(ForegroundService.ForegroundBinder) service;

ForegroundBinder binder = (ForegroundBinder) service;
BackgroundMode.this.service = binder.getService();
}

Expand All @@ -77,6 +77,11 @@ public void onServiceDisconnected(ComponentName name) {
}
};

@Override
protected void pluginInitialize() {
BackgroundExt.addWindowFlags(cordova.getActivity());
}

// codebeat:disable[ABC]

/**
Expand Down
4 changes: 2 additions & 2 deletions src/android/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ private void keepAwake() {
startForeground(NOTIFICATION_ID, makeNotification());
}

PowerManager powerMgr = (PowerManager)
PowerManager pm = (PowerManager)
getSystemService(POWER_SERVICE);

wakeLock = powerMgr.newWakeLock(
wakeLock = pm.newWakeLock(
PARTIAL_WAKE_LOCK, "BackgroundMode");

wakeLock.acquire();
Expand Down
22 changes: 22 additions & 0 deletions www/background-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ exports.isScreenOff = function (fn) {
}
};

/**
* Wake up the device.
*
* @return [ Void ]
*/
exports.wakeUp = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'wakeup', []);
}
};

/**
* Wake up and unlock the device.
*
* @return [ Void ]
*/
exports.unlock = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'unlock', []);
}
};

/**
* If the mode is enabled or disabled.
*
Expand Down

0 comments on commit 0f94708

Please sign in to comment.