Skip to content

Commit

Permalink
Merge pull request #1024 from AltBeacon/expedite-foreground-promotion
Browse files Browse the repository at this point in the history
Expedite foreground promotion
  • Loading branch information
davidgyoung authored Jun 7, 2021
2 parents af7bb5f + 8b4563f commit 280f86b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<Beacon> beacons'");
}
this.startForegroundIfConfigured();
}


Expand Down
94 changes: 94 additions & 0 deletions lib/src/main/java/org/altbeacon/beacon/utils/DozeDetector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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;

/**
* 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();

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;
}

}

0 comments on commit 280f86b

Please sign in to comment.