diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 3ccb5cd1..3316a017 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -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; @@ -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; } /** @@ -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(); @@ -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()); } @@ -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(); } @@ -234,6 +247,7 @@ private void updateNotification(JSONObject settings) */ private void startService() { + Log.d("BGCORDOVA", "BackgroundMode startService"); Activity context = cordova.getActivity(); if (isDisabled || isBind) @@ -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); @@ -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; @@ -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); + }}); } } diff --git a/src/android/BackgroundModeExt.java b/src/android/BackgroundModeExt.java index ed765984..5990bd04 100644 --- a/src/android/BackgroundModeExt.java +++ b/src/android/BackgroundModeExt.java @@ -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; @@ -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; } /** @@ -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 { @@ -175,7 +204,7 @@ public void run() { } catch (Exception e){ view.dispatchWindowVisibilityChanged(View.VISIBLE); } - }); + }}); } catch (InterruptedException e) { // do nothing } @@ -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) { @@ -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")) @@ -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; } @@ -368,7 +402,10 @@ 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); + }}); } /** @@ -376,15 +413,20 @@ private void addSreenAndKeyguardFlags() */ 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); + }}); } /** diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index 806c9568..871b38df 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -36,6 +36,8 @@ Licensed to the Apache Software Foundation (ASF) under one import android.os.PowerManager; import android.app.NotificationChannel; +import android.util.Log; + import org.json.JSONObject; import static android.os.PowerManager.PARTIAL_WAKE_LOCK; @@ -125,9 +127,11 @@ public int onStartCommand (Intent intent, int flags, int startId) { @SuppressLint("WakelockTimeout") private void keepAwake() { + Log.d("BGCORDOVA", "ForegroundService keepAwake"); JSONObject settings = BackgroundMode.getSettings(); boolean isSilent = settings.optBoolean("silent", false); + Log.d("BGCORDOVA", "ForegroundService isSilent: '"+isSilent+"'"); if (!isSilent) { startForeground(NOTIFICATION_ID, makeNotification()); } @@ -171,22 +175,23 @@ private Notification makeNotification() */ private Notification makeNotification (JSONObject settings) { + Log.d("BGCORDOVA", "ForegroundService makeNotification Build.VERSION.SDK_INT:'"+Build.VERSION.SDK_INT+"'"); // use channelid for Oreo and higher String CHANNEL_ID = "cordova-plugin-background-mode-id"; if(Build.VERSION.SDK_INT >= 26){ - // The user-visible name of the channel. - CharSequence name = "cordova-plugin-background-mode"; - // The user-visible description of the channel. - String description = "cordova-plugin-background-moden notification"; + // The user-visible name of the channel. + CharSequence name = "cordova-plugin-background-mode"; + // The user-visible description of the channel. + String description = "cordova-plugin-background-moden notification"; - int importance = NotificationManager.IMPORTANCE_LOW; + int importance = NotificationManager.IMPORTANCE_LOW; - NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,importance); + NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,importance); - // Configure the notification channel. - mChannel.setDescription(description); + // Configure the notification channel. + mChannel.setDescription(description); - getNotificationManager().createNotificationChannel(mChannel); + getNotificationManager().createNotificationChannel(mChannel); } String title = settings.optString("title", NOTIFICATION_TITLE); String text = settings.optString("text", NOTIFICATION_TEXT); @@ -228,6 +233,7 @@ private Notification makeNotification (JSONObject settings) notification.setContentIntent(contentIntent); } + Log.d("BGCORDOVA", "ForegroundService will build notification title: '"+title+"' text: '"+text+ "'" ); return notification.build(); }