From 0cd25dfc74802d9c67493950c304f6076bdade89 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Wed, 28 Apr 2021 16:08:21 -0400 Subject: [PATCH 1/5] Expedite when we promote scanning servicxe to the foreground to avoid crashes --- .../main/java/org/altbeacon/beacon/service/BeaconService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/org/altbeacon/beacon/service/BeaconService.java b/lib/src/main/java/org/altbeacon/beacon/service/BeaconService.java index d5180283..d174a0e7 100644 --- a/lib/src/main/java/org/altbeacon/beacon/service/BeaconService.java +++ b/lib/src/main/java/org/altbeacon/beacon/service/BeaconService.java @@ -196,6 +196,7 @@ else if (msg.what == MSG_SYNC_SETTINGS) { @MainThread @Override public void onCreate() { + this.startForegroundIfConfigured(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { bluetoothCrashResolver = new BluetoothCrashResolver(this); bluetoothCrashResolver.start(); @@ -246,7 +247,6 @@ public void onCreate() { } catch (Exception e) { LogManager.e(e, TAG, "Cannot get simulated Scan data. Make sure your org.altbeacon.beacon.SimulatedScanData class defines a field with the signature 'public static List beacons'"); } - this.startForegroundIfConfigured(); } From bc008b70b2a09566c2018898688882a0d801e174 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Wed, 28 Apr 2021 16:36:18 -0400 Subject: [PATCH 2/5] Update changelog for beta release --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6c4b458..7dcdbfff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +### 2.19-beta / 2021-04028 + +- Reduce crashes on starting foreground service (#1024, David G. Young) +- Reduce minSdk to 14 (#1023 David G. Young) +- Add experimental LiveData interface (#1025, David G. Young) + ### 2.18 / 2021-04-14 - Remove dependency on androidx.localbroadcastmanager.content.LocalBroadcastManager (#1022, David G. Young) From f83c8bd2e2ed79365dba2aa6cf3f6fb67972dbda Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Wed, 28 Apr 2021 18:57:19 -0400 Subject: [PATCH 3/5] Select specific version of ktx to make maven happy --- lib/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/build.gradle b/lib/build.gradle index 70767912..fe03ecad 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -68,7 +68,7 @@ dependencies { androidTestImplementation 'androidx.test:rules:1.1.0' androidTestImplementation 'org.apache.commons:commons-math3:3.6.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' - implementation "androidx.core:core-ktx:+" + implementation "androidx.core:core-ktx:1.3.2" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } From 587729930a5b32f5e88b14eccaa2e0a1633a7f87 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Tue, 11 May 2021 16:34:22 -0400 Subject: [PATCH 4/5] Add DozeDetector for future use in library --- .../altbeacon/beacon/utils/DozeDetector.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java diff --git a/lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java b/lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java new file mode 100644 index 00000000..e4a59afa --- /dev/null +++ b/lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java @@ -0,0 +1,88 @@ +package org.altbeacon.beacon.utils; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.IntentFilter; +import android.os.Build; +import android.os.PowerManager; + +import org.altbeacon.beacon.logging.LogManager; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class DozeDetector { + private static final String TAG = DozeDetector.class.getSimpleName(); + + public boolean isInDozeMode(Context context) { + return isInFullDozeMode(context) == true || isInLightDozeMode(context) == true; + } + public boolean isInLightDozeMode(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + try { + Method isLightDeviceIdleModeMethod = pm.getClass().getDeclaredMethod("isLightDeviceIdleMode"); + boolean result = (boolean)isLightDeviceIdleModeMethod.invoke(pm); + LogManager.d(TAG, "Light Doze mode? pm.isLightDeviceIdleMode: " + result); + return result; + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + LogManager.d(TAG, "Reflection failed for isLightDeviceIdleMode: " + e.toString(), e); + } + } + else { + LogManager.d(TAG, "We can't be in doze mode as we are pre-Android M"); + } + return false; + } + public boolean isInFullDozeMode(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + + if (pm == null) { + LogManager.d(TAG, "Can't get PowerManager to check doze mode."); + return false; + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + LogManager.d(TAG, "Full Doze mode? pm.isDeviceIdleMode()="+pm.isDeviceIdleMode()); + if (pm.isDeviceIdleMode()) { + return true; + } + } + + LogManager.d(TAG, "Doze mode? pm.isPowerSaveMode()="+pm.isPowerSaveMode()); + return pm.isPowerSaveMode(); + } + else { + LogManager.d(TAG, "We can't be in doze mode as we are pre-Android M"); + } + return false; + } + + public void registerDozeCallbacks(Context context, BroadcastReceiver receiver) { + IntentFilter filter = new IntentFilter(); + filter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); + context.registerReceiver(receiver, filter); + + filter = new IntentFilter(); + String action = getLightIdleModeChangeAction(); + filter.addAction(action); + context.registerReceiver(receiver, filter); + } + + public String getLightIdleModeChangeAction() { + String action = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGE"; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + try { + Object reflectionAction =PowerManager.class.getField("ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED").get(null); + if (reflectionAction != null && reflectionAction instanceof String) { + action = (String) reflectionAction; + } + } catch (Exception e) { + LogManager.d(TAG, "Cannot get LIGHT_DEVICE_IDLE_MODE_CHANGE action: " + e.toString(), e); + } + } + return action; + } + +} From 8b4563f1cee5ad2d864b0cb415c4cb0de511ce54 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Tue, 11 May 2021 16:40:47 -0400 Subject: [PATCH 5/5] Add DozeDetector for future use in libraryY --- .../main/java/org/altbeacon/beacon/utils/DozeDetector.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java b/lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java index e4a59afa..d3c4fc3a 100644 --- a/lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java +++ b/lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java @@ -11,6 +11,12 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +/** + * Utility class for detecting when an Android device enters and leaves various Doze modes + * April 02, 2019 + * (c) 2019 David G. Young + * Apache 2 License + */ public class DozeDetector { private static final String TAG = DozeDetector.class.getSimpleName();