forked from PeterCxy/Shelter
FreezeService: migrate to AlarmManager to ensure wakeup
Co-authored-by: parmaster84 <parmaster84@proton.me>
This commit is contained in:
parent
990cf39ed9
commit
fe762cba28
|
@ -1,4 +1,3 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="DesignSurface">
|
<component name="DesignSurface">
|
||||||
<option name="filePathToZoomLevelMap">
|
<option name="filePathToZoomLevelMap">
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package net.typeblog.shelter.services;
|
package net.typeblog.shelter.services;
|
||||||
|
|
||||||
|
import android.app.AlarmManager;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
@ -11,9 +12,7 @@ import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.Looper;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
@ -72,8 +71,9 @@ public class FreezeService extends Service {
|
||||||
|
|
||||||
// Delay the work so that it can be canceled if the screen
|
// Delay the work so that it can be canceled if the screen
|
||||||
// gets unlocked before the delay passes
|
// gets unlocked before the delay passes
|
||||||
mHandler.postDelayed(mFreezeWork,
|
mAlarmManager.set(AlarmManager.RTC_WAKEUP,
|
||||||
((long) SettingsManager.getInstance().getAutoFreezeDelay()) * 1000);
|
System.currentTimeMillis() + ((long) SettingsManager.getInstance().getAutoFreezeDelay()) * 1000,
|
||||||
|
null, mFreezeWork, null);
|
||||||
registerReceiver(mUnlockReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
|
registerReceiver(mUnlockReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -83,7 +83,7 @@ public class FreezeService extends Service {
|
||||||
private BroadcastReceiver mUnlockReceiver = new BroadcastReceiver() {
|
private BroadcastReceiver mUnlockReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
mHandler.removeCallbacks(mFreezeWork);
|
mAlarmManager.cancel(mFreezeWork);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -96,9 +96,10 @@ public class FreezeService extends Service {
|
||||||
private Map<String, UsageStats> mUsageStats = new HashMap<>();
|
private Map<String, UsageStats> mUsageStats = new HashMap<>();
|
||||||
private long mScreenLockTime = -1;
|
private long mScreenLockTime = -1;
|
||||||
|
|
||||||
// The handler and the delayed work to handle
|
// Delayed work
|
||||||
private Handler mHandler = new Handler(Looper.getMainLooper());
|
private AlarmManager mAlarmManager;
|
||||||
private Runnable mFreezeWork = () -> {
|
|
||||||
|
private AlarmManager.OnAlarmListener mFreezeWork = () -> {
|
||||||
synchronized (FreezeService.class) {
|
synchronized (FreezeService.class) {
|
||||||
// Cancel the unlock receiver first - the delay has passed if this work is executed
|
// Cancel the unlock receiver first - the delay has passed if this work is executed
|
||||||
unregisterReceiver(mUnlockReceiver);
|
unregisterReceiver(mUnlockReceiver);
|
||||||
|
@ -128,6 +129,7 @@ public class FreezeService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
mAlarmManager = getSystemService(AlarmManager.class);
|
||||||
// This is the only thing that we do
|
// This is the only thing that we do
|
||||||
registerReceiver(mLockReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
|
registerReceiver(mLockReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
|
||||||
// Use foreground notification to keep this service alive until screen is locked
|
// Use foreground notification to keep this service alive until screen is locked
|
||||||
|
|
Loading…
Reference in a new issue