cordova-plugin-run-in-backg.../src/android/BackgroundExt.java

333 lines
9.4 KiB
Java
Raw Normal View History

2017-01-30 10:36:10 +00:00
/*
Copyright 2013-2017 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package de.appplant.cordova.plugin.background;
2017-02-02 12:05:43 +00:00
import android.annotation.TargetApi;
2017-01-30 10:36:10 +00:00
import android.app.Activity;
import android.app.ActivityManager;
2017-02-02 12:05:43 +00:00
import android.app.ActivityManager.AppTask;
2017-02-10 12:10:46 +00:00
import android.content.Context;
2017-01-30 10:36:10 +00:00
import android.content.Intent;
import android.os.Build;
2017-02-06 14:59:57 +00:00
import android.os.PowerManager;
2017-01-30 10:36:10 +00:00
import android.view.View;
2017-02-10 12:10:46 +00:00
import android.view.Window;
2017-01-30 10:36:10 +00:00
2017-02-06 14:59:57 +00:00
import org.apache.cordova.CallbackContext;
2017-01-30 10:36:10 +00:00
import org.apache.cordova.CordovaInterface;
2017-02-06 14:59:57 +00:00
import org.apache.cordova.CordovaPlugin;
2017-01-30 10:36:10 +00:00
import org.apache.cordova.CordovaWebView;
2017-02-06 14:59:57 +00:00
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
2017-01-30 10:36:10 +00:00
import java.lang.ref.WeakReference;
import java.util.List;
2017-02-06 14:59:57 +00:00
import static android.content.Context.ACTIVITY_SERVICE;
import static android.content.Context.POWER_SERVICE;
2017-02-10 12:10:46 +00:00
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;
2017-02-06 14:59:57 +00:00
2017-01-30 11:05:25 +00:00
class BackgroundExt {
2017-01-30 10:36:10 +00:00
// Weak reference to the cordova interface passed by the plugin
private final WeakReference<CordovaInterface> cordova;
// Weak reference to the cordova web view passed by the plugin
private final WeakReference<CordovaWebView> webView;
2017-02-10 12:10:46 +00:00
private PowerManager.WakeLock wakeLock;
2017-01-30 10:36:10 +00:00
/**
* Initialize the extension to perform non-background related tasks.
*
2017-02-06 14:59:57 +00:00
* @param plugin The cordova plugin.
2017-01-30 10:36:10 +00:00
*/
2017-02-06 14:59:57 +00:00
private BackgroundExt(CordovaPlugin plugin) {
this.cordova = new WeakReference<CordovaInterface>(plugin.cordova);
this.webView = new WeakReference<CordovaWebView>(plugin.webView);
2017-01-30 10:36:10 +00:00
}
2017-01-30 11:05:25 +00:00
/**
2017-02-06 14:59:57 +00:00
* Executes the request asynchronous.
2017-01-30 11:05:25 +00:00
*
2017-02-06 14:59:57 +00:00
* @param plugin The cordova plugin.
* @param action The action to execute.
* @param callback The callback context used when
* calling back into JavaScript.
2017-01-30 11:05:25 +00:00
*/
2017-02-06 14:59:57 +00:00
@SuppressWarnings("UnusedParameters")
static void execute (CordovaPlugin plugin, final String action,
final CallbackContext callback) {
2017-01-30 11:05:25 +00:00
2017-02-06 14:59:57 +00:00
final BackgroundExt ext = new BackgroundExt(plugin);
plugin.cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
ext.execute(action, callback);
}
});
}
2017-02-07 07:53:15 +00:00
// codebeat:disable[ABC]
2017-02-06 14:59:57 +00:00
/**
* Executes the request.
*
* @param action The action to execute.
* @param callback The callback context used when
* calling back into JavaScript.
*/
private void execute (String action, CallbackContext callback) {
2017-01-30 11:05:25 +00:00
if (action.equalsIgnoreCase("optimizations")) {
2017-02-06 14:59:57 +00:00
disableWebViewOptimizations();
2017-01-30 11:05:25 +00:00
}
if (action.equalsIgnoreCase("background")) {
2017-02-06 14:59:57 +00:00
moveToBackground();
2017-01-30 11:05:25 +00:00
}
if (action.equalsIgnoreCase("foreground")) {
2017-02-06 14:59:57 +00:00
moveToForeground();
2017-01-30 11:05:25 +00:00
}
if (action.equalsIgnoreCase("tasklist")) {
2017-02-06 14:59:57 +00:00
excludeFromTaskList();
}
if (action.equalsIgnoreCase("dimmed")) {
isDimmed(callback);
2017-01-30 11:05:25 +00:00
}
2017-02-10 12:10:46 +00:00
if (action.equalsIgnoreCase("wakeup")) {
wakeup();
}
if (action.equalsIgnoreCase("unlock")) {
wakeup();
unlock();
}
2017-01-30 11:05:25 +00:00
}
2017-02-07 07:53:15 +00:00
// codebeat:enable[ABC]
2017-01-30 10:36:10 +00:00
/**
* Move app to background.
*/
2017-01-30 11:05:25 +00:00
private void moveToBackground() {
2017-01-30 10:36:10 +00:00
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
2017-02-10 12:10:46 +00:00
getApp().startActivity(intent);
2017-01-30 10:36:10 +00:00
}
/**
* Move app to foreground.
*/
2017-01-30 11:05:25 +00:00
private void moveToForeground() {
2017-02-10 12:10:46 +00:00
Activity app = getApp();
Intent intent = getLaunchIntent();
2017-01-30 10:36:10 +00:00
2017-02-10 12:10:46 +00:00
intent.addFlags(
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
2017-01-30 10:36:10 +00:00
app.startActivity(intent);
}
/**
* Enable GPS position tracking while in background.
*/
2017-01-30 11:05:25 +00:00
private void disableWebViewOptimizations() {
2017-01-30 10:36:10 +00:00
Thread thread = new Thread(){
public void run() {
try {
Thread.sleep(1000);
2017-02-10 12:10:46 +00:00
getApp().runOnUiThread(new Runnable() {
2017-01-30 10:36:10 +00:00
@Override
public void run() {
View view = webView.get().getEngine().getView();
try {
2017-02-06 12:34:27 +00:00
Class.forName("org.crosswalk.engine.XWalkCordovaView")
2017-02-10 12:10:46 +00:00
.getMethod("onShow")
.invoke(view);
2017-01-30 10:36:10 +00:00
} catch (Exception e){
view.dispatchWindowVisibilityChanged(View.VISIBLE);
}
}
});
} catch (InterruptedException e) {
// do nothing
}
}
};
thread.start();
}
/**
* Exclude the app from the recent tasks list.
*/
2017-02-02 12:05:43 +00:00
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
2017-01-30 11:05:25 +00:00
private void excludeFromTaskList() {
2017-02-06 14:59:57 +00:00
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
2017-01-30 10:36:10 +00:00
if (am == null || Build.VERSION.SDK_INT < 21)
return;
2017-02-02 12:05:43 +00:00
List<AppTask> tasks = am.getAppTasks();
2017-01-30 10:36:10 +00:00
2017-02-02 12:05:43 +00:00
if (tasks == null || tasks.isEmpty())
return;
2017-01-30 10:36:10 +00:00
2017-02-02 12:05:43 +00:00
tasks.get(0).setExcludeFromRecents(true);
2017-01-30 10:36:10 +00:00
}
2017-02-06 14:59:57 +00:00
/**
* Invoke the callback with information if the screen is on.
*
* @param callback The callback to invoke.
*/
@SuppressWarnings("deprecation")
2017-02-10 12:10:46 +00:00
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() {
2017-02-06 14:59:57 +00:00
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
if (Build.VERSION.SDK_INT < 20) {
2017-02-10 12:10:46 +00:00
return !pm.isScreenOn();
2017-02-06 14:59:57 +00:00
}
2017-02-10 12:10:46 +00:00
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
);
}
});
2017-02-06 14:59:57 +00:00
}
2017-01-30 10:36:10 +00:00
/**
* The activity referenced by cordova.
*
* @return The main activity of the app.
*/
2017-02-10 12:10:46 +00:00
Activity getApp() {
2017-01-30 10:36:10 +00:00
return cordova.get().getActivity();
}
2017-02-10 12:10:46 +00:00
/**
* The launch intent for the main activity.
*/
private Intent getLaunchIntent() {
Context app = getApp().getApplicationContext();
String pkgName = app.getPackageName();
return app.getPackageManager().getLaunchIntentForPackage(pkgName);
}
2017-02-06 14:59:57 +00:00
/**
* Get the requested system service by name.
*
* @param name The name of the service.
*
* @return The service instance.
*/
2017-02-10 12:10:46 +00:00
private Object getService(String name) {
return getApp().getSystemService(name);
2017-02-06 14:59:57 +00:00
}
2017-02-06 12:34:27 +00:00
}