mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 06:39:54 +01:00
Screen lock: Add a fallback in case alarm manager didn't run during sleep
This commit is contained in:
@@ -63,6 +63,7 @@ public class LockManagerImpl implements LockManager, Service, EventListener {
|
|||||||
private volatile boolean lockableSetting = false;
|
private volatile boolean lockableSetting = false;
|
||||||
private volatile int timeoutMinutes;
|
private volatile int timeoutMinutes;
|
||||||
private int activitiesRunning = 0;
|
private int activitiesRunning = 0;
|
||||||
|
private long idleTime;
|
||||||
private final MutableLiveData<Boolean> lockable = new MutableLiveData<>();
|
private final MutableLiveData<Boolean> lockable = new MutableLiveData<>();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@@ -101,6 +102,11 @@ public class LockManagerImpl implements LockManager, Service, EventListener {
|
|||||||
@UiThread
|
@UiThread
|
||||||
@Override
|
@Override
|
||||||
public void onActivityStart() {
|
public void onActivityStart() {
|
||||||
|
if (!locked && activitiesRunning == 0 && timeoutEnabled() &&
|
||||||
|
timedOut()) {
|
||||||
|
// lock the app in case the alarm wasn't run during sleep
|
||||||
|
setLocked(true);
|
||||||
|
}
|
||||||
activitiesRunning++;
|
activitiesRunning++;
|
||||||
alarmManager.cancel(lockIntent);
|
alarmManager.cancel(lockIntent);
|
||||||
}
|
}
|
||||||
@@ -109,12 +115,14 @@ public class LockManagerImpl implements LockManager, Service, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onActivityStop() {
|
public void onActivityStop() {
|
||||||
activitiesRunning--;
|
activitiesRunning--;
|
||||||
if (activitiesRunning == 0 && !locked &&
|
if (activitiesRunning == 0) {
|
||||||
timeoutMinutes != timeoutNever && lockable.getValue()) {
|
idleTime = elapsedRealtime();
|
||||||
alarmManager.cancel(lockIntent);
|
if (!locked && timeoutEnabled()) {
|
||||||
long triggerAt =
|
alarmManager.cancel(lockIntent);
|
||||||
elapsedRealtime() + MINUTES.toMillis(timeoutMinutes);
|
long triggerAt =
|
||||||
alarmManager.set(ELAPSED_REALTIME, triggerAt, lockIntent);
|
elapsedRealtime() + MINUTES.toMillis(timeoutMinutes);
|
||||||
|
alarmManager.set(ELAPSED_REALTIME, triggerAt, lockIntent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +146,8 @@ public class LockManagerImpl implements LockManager, Service, EventListener {
|
|||||||
if (locked && !hasScreenLock(appContext)) {
|
if (locked && !hasScreenLock(appContext)) {
|
||||||
lockable.postValue(false);
|
lockable.postValue(false);
|
||||||
locked = false;
|
locked = false;
|
||||||
|
} else if (!locked && activitiesRunning == 0 && timeoutEnabled()) {
|
||||||
|
setLocked(true);
|
||||||
}
|
}
|
||||||
return locked;
|
return locked;
|
||||||
}
|
}
|
||||||
@@ -179,4 +189,12 @@ public class LockManagerImpl implements LockManager, Service, EventListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean timeoutEnabled() {
|
||||||
|
return timeoutMinutes != timeoutNever && lockable.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean timedOut() {
|
||||||
|
return elapsedRealtime() - idleTime > MINUTES.toMillis(timeoutMinutes);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user