Detect screen status

This commit is contained in:
Sebastián Katzer
2017-02-06 15:59:57 +01:00
parent 8064b8853f
commit 131df973fb
5 changed files with 133 additions and 55 deletions

View File

@@ -25,18 +25,24 @@ import android.annotation.TargetApi;
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 org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.List;
import static android.content.Context.ACTIVITY_SERVICE;
import static android.content.Context.POWER_SERVICE;
class BackgroundExt {
// Weak reference to the cordova interface passed by the plugin
@@ -48,40 +54,62 @@ class BackgroundExt {
/**
* Initialize the extension to perform non-background related tasks.
*
* @param cordova The cordova interface.
* @param webView The cordova web view.
* @param plugin The cordova plugin.
*/
private BackgroundExt(CordovaInterface cordova, CordovaWebView webView) {
this.cordova = new WeakReference<CordovaInterface>(cordova);
this.webView = new WeakReference<CordovaWebView>(webView);
private BackgroundExt(CordovaPlugin plugin) {
this.cordova = new WeakReference<CordovaInterface>(plugin.cordova);
this.webView = new WeakReference<CordovaWebView>(plugin.webView);
}
/**
* Executes the request asynchronous.
*
* @param plugin The cordova plugin.
* @param action The action to execute.
* @param callback The callback context used when
* calling back into JavaScript.
*/
@SuppressWarnings("UnusedParameters")
static void execute (CordovaPlugin plugin, final String action,
final CallbackContext callback) {
final BackgroundExt ext = new BackgroundExt(plugin);
plugin.cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
ext.execute(action, callback);
}
});
}
/**
* Executes the request.
*
* @param action The action to execute.
* @param cordova The cordova interface.
* @param webView The cordova web view.
* @param action The action to execute.
* @param callback The callback context used when
* calling back into JavaScript.
*/
static void execute(String action, CordovaInterface cordova,
CordovaWebView webView) {
BackgroundExt ext = new BackgroundExt(cordova, webView);
private void execute (String action, CallbackContext callback) {
if (action.equalsIgnoreCase("optimizations")) {
ext.disableWebViewOptimizations();
disableWebViewOptimizations();
}
if (action.equalsIgnoreCase("background")) {
ext.moveToBackground();
moveToBackground();
}
if (action.equalsIgnoreCase("foreground")) {
ext.moveToForeground();
moveToForeground();
}
if (action.equalsIgnoreCase("tasklist")) {
ext.excludeFromTaskList();
excludeFromTaskList();
}
if (action.equalsIgnoreCase("dimmed")) {
isDimmed(callback);
}
}
@@ -148,8 +176,7 @@ class BackgroundExt {
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void excludeFromTaskList() {
ActivityManager am = (ActivityManager) getActivity()
.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
if (am == null || Build.VERSION.SDK_INT < 21)
return;
@@ -162,6 +189,26 @@ class BackgroundExt {
tasks.get(0).setExcludeFromRecents(true);
}
/**
* Invoke the callback with information if the screen is on.
*
* @param callback The callback to invoke.
*/
@SuppressWarnings("deprecation")
private void isDimmed (CallbackContext callback) {
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
boolean isDimmed;
if (Build.VERSION.SDK_INT < 20) {
isDimmed = !pm.isScreenOn();
} else {
isDimmed = !pm.isInteractive();
}
PluginResult result = new PluginResult(Status.OK, isDimmed);
callback.sendPluginResult(result);
}
/**
* The activity referenced by cordova.
*
@@ -171,4 +218,15 @@ class BackgroundExt {
return cordova.get().getActivity();
}
/**
* 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);
}
}

View File

@@ -23,7 +23,6 @@ package de.appplant.cordova.plugin.background;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
@@ -34,6 +33,8 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import static android.content.Context.BIND_AUTO_CREATE;
public class BackgroundMode extends CordovaPlugin {
// Event types for callbacks
@@ -93,25 +94,24 @@ public class BackgroundMode extends CordovaPlugin {
CallbackContext callback) throws JSONException {
if (action.equalsIgnoreCase("configure")) {
JSONObject settings = args.getJSONObject(0);
boolean update = args.getBoolean(1);
configure(settings, update);
configure(args.getJSONObject(0), args.getBoolean(1));
callback.success();
return true;
}
else
if (action.equalsIgnoreCase("enable")) {
enableMode();
callback.success();
return true;
}
else
if (action.equalsIgnoreCase("disable")) {
disableMode();
}
else {
BackgroundExt.execute(action, cordova, webView);
callback.success();
return true;
}
callback.success();
BackgroundExt.execute(this, action, callback);
return true;
}
@@ -221,15 +221,11 @@ public class BackgroundMode extends CordovaPlugin {
if (isDisabled || isBind)
return;
Intent intent = new Intent(
context, ForegroundService.class);
Intent intent = new Intent(context, ForegroundService.class);
try {
context.bindService(intent,
connection, Context.BIND_AUTO_CREATE);
context.bindService(intent, connection, BIND_AUTO_CREATE);
fireEvent(Event.ACTIVATE, null);
context.startService(intent);
} catch (Exception e) {
fireEvent(Event.FAILURE, String.format("'%s'", e.getMessage()));
@@ -244,15 +240,12 @@ public class BackgroundMode extends CordovaPlugin {
*/
private void stopService() {
Activity context = cordova.getActivity();
Intent intent = new Intent(
context, ForegroundService.class);
Intent intent = new Intent(context, ForegroundService.class);
if (!isBind)
return;
fireEvent(Event.DEACTIVATE, null);
context.unbindService(connection);
context.stopService(intent);