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-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.util.List;
|
|
|
|
|
2017-02-06 14:59:57 +00:00
|
|
|
import static android.content.Context.ACTIVITY_SERVICE;
|
|
|
|
import static android.content.Context.POWER_SERVICE;
|
2019-02-03 14:58:40 +00:00
|
|
|
import static android.os.Build.VERSION.SDK_INT;
|
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
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
/**
|
|
|
|
* Implements extended functions around the main purpose
|
|
|
|
* of infinite execution in the background.
|
|
|
|
*/
|
2017-01-30 11:05:25 +00:00
|
|
|
class BackgroundExt {
|
2017-01-30 10:36:10 +00:00
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
// Reference to the cordova interface passed by the plugin
|
|
|
|
private final CordovaInterface cordova;
|
2017-01-30 10:36:10 +00:00
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
// Reference to the cordova web view passed by the plugin
|
|
|
|
private final CordovaWebView webView;
|
2017-01-30 10:36:10 +00:00
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
// To keep the device awake
|
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
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
BackgroundExt(CordovaPlugin plugin)
|
|
|
|
{
|
|
|
|
this.cordova = plugin.cordova;
|
|
|
|
this.webView = plugin.webView;
|
2017-01-30 10:36:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 11:05:25 +00:00
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Executes the request within a thread.
|
2017-01-30 11:05:25 +00:00
|
|
|
*
|
2017-02-06 14:59:57 +00:00
|
|
|
* @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
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
void executeAsync (String action, CallbackContext callback)
|
|
|
|
{
|
|
|
|
cordova.getThreadPool().execute(() -> execute(action, callback));
|
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.
|
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
private void execute (String action, CallbackContext callback)
|
|
|
|
{
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case "optimizations":
|
|
|
|
disableWebViewOptimizations();
|
|
|
|
break;
|
|
|
|
case "background":
|
|
|
|
moveToBackground();
|
|
|
|
break;
|
|
|
|
case "foreground":
|
|
|
|
moveToForeground();
|
|
|
|
break;
|
|
|
|
case "tasklist":
|
|
|
|
excludeFromTaskList();
|
|
|
|
break;
|
|
|
|
case "dimmed":
|
|
|
|
isDimmed(callback);
|
|
|
|
break;
|
|
|
|
case "wakeup":
|
|
|
|
wakeup();
|
|
|
|
break;
|
|
|
|
case "unlock":
|
|
|
|
wakeup();
|
|
|
|
unlock();
|
|
|
|
break;
|
2017-02-10 12:10:46 +00:00
|
|
|
}
|
2017-01-30 11:05:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:36:10 +00:00
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Moves the app to the background.
|
2017-01-30 10:36:10 +00:00
|
|
|
*/
|
2019-02-03 14:58:40 +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);
|
2019-02-03 14:58:40 +00:00
|
|
|
|
2017-02-10 12:10:46 +00:00
|
|
|
getApp().startActivity(intent);
|
2017-01-30 10:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Moves the app to the foreground.
|
2017-01-30 10:36:10 +00:00
|
|
|
*/
|
2019-02-03 14:58:40 +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 |
|
2019-02-03 14:58:40 +00:00
|
|
|
Intent.FLAG_ACTIVITY_SINGLE_TOP |
|
|
|
|
Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
2017-01-30 10:36:10 +00:00
|
|
|
|
2019-02-03 15:17:15 +00:00
|
|
|
clearSreenAndKeyguardFlags();
|
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);
|
2019-02-03 14:58:40 +00:00
|
|
|
getApp().runOnUiThread(() -> {
|
|
|
|
View view = webView.getEngine().getView();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Class.forName("org.crosswalk.engine.XWalkCordovaView")
|
|
|
|
.getMethod("onShow")
|
|
|
|
.invoke(view);
|
|
|
|
} catch (Exception e){
|
|
|
|
view.dispatchWindowVisibilityChanged(View.VISIBLE);
|
2017-01-30 10:36:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Excludes the app from the recent tasks list.
|
2017-01-30 10:36:10 +00:00
|
|
|
*/
|
2017-02-02 12:05:43 +00:00
|
|
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
2019-02-03 14:58:40 +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
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
if (am == null || SDK_INT < 21)
|
2017-01-30 10:36:10 +00:00
|
|
|
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
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Invokes the callback with information if the screen is on.
|
2017-02-06 14:59:57 +00:00
|
|
|
*
|
|
|
|
* @param callback The callback to invoke.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("deprecation")
|
2019-02-03 14:58:40 +00:00
|
|
|
private void isDimmed (CallbackContext callback)
|
|
|
|
{
|
|
|
|
boolean status = isDimmed();
|
|
|
|
PluginResult res = new PluginResult(Status.OK, status);
|
|
|
|
|
|
|
|
callback.sendPluginResult(res);
|
2017-02-10 12:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Returns if the screen is active.
|
2017-02-10 12:10:46 +00:00
|
|
|
*/
|
|
|
|
@SuppressWarnings("deprecation")
|
2019-02-03 14:58:40 +00:00
|
|
|
private boolean isDimmed()
|
|
|
|
{
|
2017-02-06 14:59:57 +00:00
|
|
|
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
if (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.
|
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
private void wakeup()
|
|
|
|
{
|
2017-02-10 12:10:46 +00:00
|
|
|
try {
|
|
|
|
acquireWakeLock();
|
|
|
|
} catch (Exception e) {
|
|
|
|
releaseWakeLock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlocks the device even with password protection.
|
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
private void unlock()
|
|
|
|
{
|
2019-02-03 15:17:15 +00:00
|
|
|
addSreenAndKeyguardFlags();
|
|
|
|
getApp().startActivity(getLaunchIntent());
|
2017-02-10 12:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Acquires a wake lock to wake up the device.
|
2017-02-10 12:10:46 +00:00
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
private void acquireWakeLock()
|
|
|
|
{
|
2017-02-10 12:10:46 +00:00
|
|
|
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
|
|
|
|
|
|
|
releaseWakeLock();
|
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
if (!isDimmed())
|
2017-02-10 12:10:46 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int level = PowerManager.SCREEN_DIM_WAKE_LOCK |
|
|
|
|
PowerManager.ACQUIRE_CAUSES_WAKEUP;
|
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
wakeLock = pm.newWakeLock(level, "backgroundmode:wakelock");
|
2017-02-10 12:10:46 +00:00
|
|
|
wakeLock.setReferenceCounted(false);
|
|
|
|
wakeLock.acquire(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases the previously acquire wake lock.
|
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
private void releaseWakeLock()
|
|
|
|
{
|
2017-02-10 12:10:46 +00:00
|
|
|
if (wakeLock != null && wakeLock.isHeld()) {
|
|
|
|
wakeLock.release();
|
|
|
|
wakeLock = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Adds required flags to the window to unlock/wakeup the device.
|
2017-02-10 12:10:46 +00:00
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
private void addSreenAndKeyguardFlags()
|
|
|
|
{
|
2019-02-03 15:17:15 +00:00
|
|
|
getApp().runOnUiThread(() -> getApp().getWindow().addFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears required flags to the window to unlock/wakeup the device.
|
|
|
|
*/
|
|
|
|
private void clearSreenAndKeyguardFlags()
|
|
|
|
{
|
|
|
|
getApp().runOnUiThread(() -> getApp().getWindow().clearFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
|
2019-02-03 14:58:40 +00:00
|
|
|
}
|
2017-02-10 12:10:46 +00:00
|
|
|
|
2019-02-03 14:58:40 +00:00
|
|
|
/**
|
|
|
|
* Removes required flags to the window to unlock/wakeup the device.
|
|
|
|
*/
|
|
|
|
static void clearKeyguardFlags (Activity app)
|
|
|
|
{
|
|
|
|
app.runOnUiThread(() -> app.getWindow().clearFlags(FLAG_DISMISS_KEYGUARD));
|
2017-02-06 14:59:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:36:10 +00:00
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Returns the activity referenced by cordova.
|
2017-01-30 10:36:10 +00:00
|
|
|
*/
|
2017-02-10 12:10:46 +00:00
|
|
|
Activity getApp() {
|
2019-02-03 14:58:40 +00:00
|
|
|
return cordova.getActivity();
|
2017-01-30 10:36:10 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 12:10:46 +00:00
|
|
|
/**
|
2019-02-03 14:58:40 +00:00
|
|
|
* Gets the launch intent for the main activity.
|
2017-02-10 12:10:46 +00:00
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
private Intent getLaunchIntent()
|
|
|
|
{
|
2017-02-10 12:10:46 +00:00
|
|
|
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.
|
|
|
|
*/
|
2019-02-03 14:58:40 +00:00
|
|
|
private Object getService(String name)
|
|
|
|
{
|
2017-02-10 12:10:46 +00:00
|
|
|
return getApp().getSystemService(name);
|
2017-02-06 14:59:57 +00:00
|
|
|
}
|
2017-02-06 12:34:27 +00:00
|
|
|
}
|