From c0c44052de32378d93006b45a1ba7478bb99ddb3 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Sat, 7 Sep 2024 13:00:38 +0200 Subject: [PATCH] Lock screen if last use is more than 24 hours ago Resolves https://github.com/MarcusWolschon/osmeditor4android/issues/2487 --- src/main/java/de/blau/android/Logic.java | 10 ++++ src/main/java/de/blau/android/Main.java | 52 +++++++++---------- .../de/blau/android/prefs/Preferences.java | 8 +-- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/main/java/de/blau/android/Logic.java b/src/main/java/de/blau/android/Logic.java index a1c0cd7d3..2b95b0cd9 100644 --- a/src/main/java/de/blau/android/Logic.java +++ b/src/main/java/de/blau/android/Logic.java @@ -206,6 +206,11 @@ public enum CursorPaddirection { */ private static final int MAX_RELATION_SELECTION_DEPTH = 5; + /** + * 24 hours in ms + */ + private static final long ONE_DAY_MS = 24 * 3600 * 1000L; + /** * Stores the {@link Preferences} as soon as they are available. */ @@ -4028,6 +4033,11 @@ void loadEditingState(@NonNull Main main, boolean setViewBox) { if (setViewBox) { editState.setViewBox(this, main.getMap()); } + File editStateFile = main.getFileStreamPath(EDITSTATE_FILENAME); + if (System.currentTimeMillis() - editStateFile.lastModified() > ONE_DAY_MS) { + Log.w(DEBUG_TAG, "App hasn't been run in a long time, locking"); + main.lock(); + } } editingStateRead = true; } diff --git a/src/main/java/de/blau/android/Main.java b/src/main/java/de/blau/android/Main.java index 8105cd5ed..1aa0531d1 100644 --- a/src/main/java/de/blau/android/Main.java +++ b/src/main/java/de/blau/android/Main.java @@ -4538,37 +4538,35 @@ private void showFollowButton() { } } + /** + * Runnable for locking + */ + private Runnable autoLock = () -> lock(); + /** * Lock screen if we are in a mode in which that can reasonably be done */ - private Runnable autoLock = new Runnable() { // NOSONAR - @Override - public void run() { - if (!App.getLogic().isLocked()) { - EasyEditManager manager = getEasyEditManager(); - boolean elementSelected = manager.inElementSelectedMode() || manager.inNewNoteSelectedMode(); - if (!manager.isProcessingAction() || elementSelected) { - View lock = getLock(); - if (lock != null) { - lock.performClick(); - } - if (elementSelected) { - App.getLogic().deselectAll(); - map.deselectObjects(); - manager.finish(); - } - } else { - // can't lock now, reschedule - if (prefs != null) { - int delay = prefs.getAutolockDelay(); - if (delay > 0) { - map.postDelayed(autoLock, delay); - } - } - } + public void lock() { + if (App.getLogic().isLocked()) { + return; + } + EasyEditManager manager = getEasyEditManager(); + boolean elementSelected = manager.inElementSelectedMode() || manager.inNewNoteSelectedMode(); + if (!manager.isProcessingAction() || elementSelected) { + View lock = getLock(); + if (lock != null) { + lock.performClick(); + } + if (elementSelected) { + App.getLogic().deselectAll(); + map.deselectObjects(); + manager.finish(); } + } else { + // can't lock now, reschedule + scheduleAutoLock(); } - }; + } /** * Schedule automatic locking of the screen in a configurable time in the future @@ -4576,7 +4574,7 @@ public void run() { public void scheduleAutoLock() { map.removeCallbacks(autoLock); if (prefs != null) { - int delay = prefs.getAutolockDelay(); + long delay = prefs.getAutolockDelay(); if (delay > 0) { map.postDelayed(autoLock, delay); } diff --git a/src/main/java/de/blau/android/prefs/Preferences.java b/src/main/java/de/blau/android/prefs/Preferences.java index 9ada327e3..96ef30311 100755 --- a/src/main/java/de/blau/android/prefs/Preferences.java +++ b/src/main/java/de/blau/android/prefs/Preferences.java @@ -1173,12 +1173,12 @@ public int getNotificationCacheSize() { } /** - * Get the number of seconds we should wait before locking the display + * Get the number of milliseconds we should wait before locking the display * - * @return delay in seconds till we auto-lock + * @return delay in milliseconds till we auto-lock */ - public int getAutolockDelay() { - return 1000 * autoLockDelay; + public long getAutolockDelay() { + return 1000L * autoLockDelay; } /**