Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
bacf419
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
293d624
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
b322319
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
011546e
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
0339e09
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
87ccbc6
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
0899594
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
0901974
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
9b966e7
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
c06fc36
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
fec1daa
More info on logcat (enable action is invalid for some reason)
Sep 20, 2019
4d289c5
Converting lambdas to regular interface implementation so we are able…
Sep 20, 2019
48d82b7
Better logs and if structure that makes me read less code
Sep 24, 2019
f0cb07d
Better logs, removing unused assigment
Sep 24, 2019
f022c87
Even more logs, let's find out what is happening
Sep 24, 2019
2828584
Even more logs
Sep 24, 2019
3c1864d
Ops, wrong class name on logs
Sep 24, 2019
74e419b
Ignore case and spaces on action
Sep 24, 2019
43bf37a
Log....
Sep 24, 2019
1a3348c
String identities were not the same, equality was failing
Sep 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions src/android/BackgroundMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.content.ServiceConnection;
import android.os.IBinder;

import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
Expand Down Expand Up @@ -88,33 +90,41 @@ public void onServiceDisconnected (ComponentName name)
* @return Returning false results in a "MethodNotFound" error.
*/
@Override
public boolean execute (String action, JSONArray args,
CallbackContext callback)
public boolean execute(String action, JSONArray args, CallbackContext callback)
{
boolean validAction = true;
Log.i("BGCORDOVA", "BackgroundMode Executing action: '"+action+"'");

switch (action)
{
case "configure":
configure(args.optJSONObject(0), args.optBoolean(1));
break;
case "enable":
enableMode();
break;
case "disable":
disableMode();
break;
default:
validAction = false;
if(action.equals("configure")){
JSONObject optJSONObject = args.optJSONObject(0);
boolean optBoolean = args.optBoolean(1);

Log.i("BGCORDOVA", "BackgroundMode Configure optJSONObject: '"+args.optJSONObject(0)+"' optBoolean: '"+optBoolean+"'");

configure(optJSONObject, optBoolean);
callback.success();
return true;
}

if(action.equals("enable")){

Log.i("BGCORDOVA", "BackgroundMode Enable");

enableMode();
callback.success();
return true;
}

if (validAction) {
if(action.equals("disable")){

Log.i("BGCORDOVA", "BackgroundMode Disable");
disableMode();
callback.success();
} else {
callback.error("Invalid action: " + action);
return true;
}

return validAction;
Log.e("BGCORDOVA", "BackgroundMode Invalid action: '" + action + "' args: '" + args + "' callback: '" + callback +"'");
callback.error("Invalid action: " + action + " args: " + args + " callback: " + callback);
return false;
}

/**
Expand All @@ -125,6 +135,7 @@ public boolean execute (String action, JSONArray args,
@Override
public void onPause(boolean multitasking)
{
Log.i("BGCORDOVA", "BackgroundMode onPause");
try {
inBackground = true;
startService();
Expand All @@ -137,7 +148,8 @@ public void onPause(boolean multitasking)
* Called when the activity is no longer visible to the user.
*/
@Override
public void onStop () {
public void onStop() {
Log.i("BGCORDOVA", "BackgroundMode onStop");
clearKeyguardFlags(cordova.getActivity());
}

Expand All @@ -147,8 +159,9 @@ public void onStop () {
* @param multitasking Flag indicating if multitasking is turned on for app.
*/
@Override
public void onResume (boolean multitasking)
public void onResume(boolean multitasking)
{
Log.i("BGCORDOVA", "BackgroundMode onResume");
inBackground = false;
stopService();
}
Expand Down Expand Up @@ -234,6 +247,7 @@ private void updateNotification(JSONObject settings)
*/
private void startService()
{
Log.d("BGCORDOVA", "BackgroundMode startService");
Activity context = cordova.getActivity();

if (isDisabled || isBind)
Expand All @@ -258,6 +272,7 @@ private void startService()
*/
private void stopService()
{
Log.d("BGCORDOVA", "BackgroundMode stopService");
Activity context = cordova.getActivity();
Intent intent = new Intent(context, ForegroundService.class);

Expand All @@ -278,6 +293,7 @@ private void stopService()
*/
private void fireEvent (Event event, String params)
{
Log.d("BGCORDOVA", "BackgroundMode fireEvent event: '"+event+"' params: '"+params+"'");
String eventName = event.name().toLowerCase();
Boolean active = event == Event.ACTIVATE;

Expand All @@ -292,6 +308,8 @@ private void fireEvent (Event event, String params)

final String js = str;

cordova.getActivity().runOnUiThread(() -> webView.loadUrl("javascript:" + js));
cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() {
webView.loadUrl("javascript:" + js);
}});
}
}
144 changes: 93 additions & 51 deletions src/android/BackgroundModeExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.os.Build;
import android.os.PowerManager;
import android.view.View;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface;

import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
Expand Down Expand Up @@ -80,52 +84,76 @@ public class BackgroundModeExt extends CordovaPlugin {
* @return Returning false results in a "MethodNotFound" error.
*/
@Override
public boolean execute (String action, JSONArray args,
CallbackContext callback)
public boolean execute(String action, JSONArray args, CallbackContext callback)
{
boolean validAction = true;
Log.d("BGCORDOVA", "BackgroundModeExt Executing action: '"+action+"'");

switch (action)
{
case "battery":
disableBatteryOptimizations();
break;
case "webview":
disableWebViewOptimizations();
break;
case "appstart":
openAppStart(args.opt(0));
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;
default:
validAction = false;
if(action.equals("battery")) {
Log.d("BGCORDOVA", "BackgroundModeExt battery");
disableBatteryOptimizations();
callback.success();
return true;
}

if(action.equals("webview")){
Log.d("BGCORDOVA", "BackgroundModeExt webview");
disableWebViewOptimizations();
callback.success();
return true;
}

if (validAction) {
if(action.equals("appstart")){
Log.d("BGCORDOVA", "BackgroundModeExt appstart");
openAppStart(args.opt(0));
callback.success();
} else {
callback.error("Invalid action: " + action);
return true;
}

return validAction;
if(action.equals("background")){
Log.d("BGCORDOVA", "BackgroundModeExt background");
moveToBackground();
callback.success();
return true;
}

if(action.equals("foreground")){
Log.d("BGCORDOVA", "BackgroundModeExt foreground");
moveToBackground();
callback.success();
return true;
}

if(action.equals("tasklist")){
Log.d("BGCORDOVA", "BackgroundModeExt tasklist");
excludeFromTaskList();
callback.success();
return true;
}

if(action.equals("dimmed")){
Log.d("BGCORDOVA", "BackgroundModeExt dimmed");
isDimmed(callback);
callback.success();
return true;
}

if(action.equals("wakeup")){
Log.d("BGCORDOVA", "BackgroundModeExt wakeup");
wakeup();
callback.success();
return true;
}

if(action.equals("unlock")){
Log.d("BGCORDOVA", "BackgroundModeExt unlock");
wakeup();
unlock();
callback.success();
}

Log.e("BGCORDOVA", "BackgroundModeExt Invalid action: '" + action + "' args: '" + args + "' callback: '" + callback + "'");
callback.error("Invalid action: " + action + " args: " + args + " callback: " + callback);
return false;
}

/**
Expand Down Expand Up @@ -165,7 +193,8 @@ private void disableWebViewOptimizations() {
public void run() {
try {
Thread.sleep(1000);
getApp().runOnUiThread(() -> {
getApp().runOnUiThread(new Runnable() { @Override public void run() {

View view = webView.getEngine().getView();

try {
Expand All @@ -175,7 +204,7 @@ public void run() {
} catch (Exception e){
view.dispatchWindowVisibilityChanged(View.VISIBLE);
}
});
}});
} catch (InterruptedException e) {
// do nothing
}
Expand Down Expand Up @@ -217,10 +246,10 @@ private void disableBatteryOptimizations()
*/
private void openAppStart (Object arg)
{
Activity activity = cordova.getActivity();
final Activity activity = cordova.getActivity();
PackageManager pm = activity.getPackageManager();

for (Intent intent : getAppStartIntents())
for (final Intent intent : getAppStartIntents())
{
if (pm.resolveActivity(intent, MATCH_DEFAULT_ONLY) != null)
{
Expand All @@ -234,10 +263,12 @@ private void openAppStart (Object arg)
break;
}

AlertDialog.Builder dialog = new AlertDialog.Builder(activity, Theme_DeviceDefault_Light_Dialog);
final AlertDialog.Builder dialog = new AlertDialog.Builder(activity, Theme_DeviceDefault_Light_Dialog);

dialog.setPositiveButton(ok, (o, d) -> activity.startActivity(intent));
dialog.setNegativeButton(cancel, (o, d) -> {});
dialog.setPositiveButton(ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {
activity.startActivity(intent);
}});
dialog.setNegativeButton(cancel, null);
dialog.setCancelable(true);

if (spec != null && spec.has("title"))
Expand All @@ -254,7 +285,10 @@ private void openAppStart (Object arg)
dialog.setMessage("missing text");
}

activity.runOnUiThread(dialog::show);

activity.runOnUiThread(new Runnable() { @Override public void run() {
dialog.show();
}});

break;
}
Expand Down Expand Up @@ -368,23 +402,31 @@ private void releaseWakeLock()
*/
private void addSreenAndKeyguardFlags()
{
getApp().runOnUiThread(() -> getApp().getWindow().addFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
final Activity app = getApp();
app.runOnUiThread(new Runnable() { @Override public void run() {
app.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 clearScreenAndKeyguardFlags()
{
getApp().runOnUiThread(() -> getApp().getWindow().clearFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
final Activity app = getApp();
app.runOnUiThread(new Runnable() { @Override public void run() {
app.getWindow().clearFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD);
}});
}

/**
* Removes required flags to the window to unlock/wakeup the device.
*/
static void clearKeyguardFlags (Activity app)
static void clearKeyguardFlags (final Activity app)
{
app.runOnUiThread(() -> app.getWindow().clearFlags(FLAG_DISMISS_KEYGUARD));
app.runOnUiThread(new Runnable() { @Override public void run() {
app.getWindow().clearFlags(FLAG_DISMISS_KEYGUARD);
}});
}

/**
Expand Down
Loading