From 1292f8145c0f0740419e6aea816c4de760c22440 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 29 Oct 2023 17:07:48 +0100 Subject: [PATCH] Add auto-sync on suspend Trigger auto-sync when the app becomes invisible to the user, ex: when navigating away or when locking the device. --- .../java/com/orgzly/android/prefs/AppPreferences.java | 6 ++++++ app/src/main/java/com/orgzly/android/sync/AutoSync.kt | 10 ++++++++-- .../java/com/orgzly/android/ui/main/MainActivity.java | 7 +++++++ .../com/orgzly/android/ui/share/ShareActivity.java | 7 +++++++ app/src/main/res/values/prefs_keys.xml | 3 +++ app/src/main/res/values/strings.xml | 3 +++ app/src/main/res/xml/prefs_screen_auto_sync.xml | 7 +++++++ 7 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/orgzly/android/prefs/AppPreferences.java b/app/src/main/java/com/orgzly/android/prefs/AppPreferences.java index 1d8a416d8..54fc6208a 100644 --- a/app/src/main/java/com/orgzly/android/prefs/AppPreferences.java +++ b/app/src/main/java/com/orgzly/android/prefs/AppPreferences.java @@ -1057,6 +1057,12 @@ public static boolean syncOnResume(Context context) { context.getResources().getBoolean(R.bool.pref_default_auto_sync_on_resume)); } + public static boolean syncOnSuspend(Context context) { + return getDefaultSharedPreferences(context).getBoolean( + context.getResources().getString(R.string.pref_key_auto_sync_on_suspend), + context.getResources().getBoolean(R.bool.pref_default_auto_sync_on_suspend)); + } + /* * Notes clipboard */ diff --git a/app/src/main/java/com/orgzly/android/sync/AutoSync.kt b/app/src/main/java/com/orgzly/android/sync/AutoSync.kt index ff63a3f6f..119139da9 100644 --- a/app/src/main/java/com/orgzly/android/sync/AutoSync.kt +++ b/app/src/main/java/com/orgzly/android/sync/AutoSync.kt @@ -30,6 +30,11 @@ class AutoSync @Inject constructor(val context: Application, val dataRepository: if (AppPreferences.syncOnResume(context)) { startSync() } + + Type.APP_SUSPENDED -> + if (AppPreferences.syncOnSuspend(context)) { + startSync() + } } } } @@ -43,10 +48,11 @@ class AutoSync @Inject constructor(val context: Application, val dataRepository: enum class Type { NOTE_CREATED, DATA_MODIFIED, - APP_RESUMED + APP_RESUMED, + APP_SUSPENDED, } companion object { private val TAG = AutoSync::class.java.name } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/orgzly/android/ui/main/MainActivity.java b/app/src/main/java/com/orgzly/android/ui/main/MainActivity.java index 59db11cbe..2cb9d9617 100644 --- a/app/src/main/java/com/orgzly/android/ui/main/MainActivity.java +++ b/app/src/main/java/com/orgzly/android/ui/main/MainActivity.java @@ -505,6 +505,13 @@ protected void onPause() { } } + @Override + protected void onStop() { + super.onStop(); + + autoSync.trigger(AutoSync.Type.APP_SUSPENDED); + } + @Override protected void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/com/orgzly/android/ui/share/ShareActivity.java b/app/src/main/java/com/orgzly/android/ui/share/ShareActivity.java index e40d63aca..7e0284192 100644 --- a/app/src/main/java/com/orgzly/android/ui/share/ShareActivity.java +++ b/app/src/main/java/com/orgzly/android/ui/share/ShareActivity.java @@ -253,6 +253,13 @@ protected void onResume() { } } + @Override + protected void onStop() { + super.onStop(); + + autoSync.trigger(AutoSync.Type.APP_SUSPENDED); + } + public static PendingIntent createNewNotePendingIntent(Context context, String category, SavedSearch savedSearch) { Intent resultIntent = createNewNoteIntent(context); diff --git a/app/src/main/res/values/prefs_keys.xml b/app/src/main/res/values/prefs_keys.xml index d933de383..4265f49be 100644 --- a/app/src/main/res/values/prefs_keys.xml +++ b/app/src/main/res/values/prefs_keys.xml @@ -281,6 +281,9 @@ pref_key_auto_sync_on_resume false + pref_key_auto_sync_on_suspend + false + pref_key_auto_sync_on_repo_change false diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f1e20765b..b5af73869 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -550,6 +550,9 @@ App started or resumed Sync whenever app comes to the foreground + App suspended + Sync whenever app goes to the background + Repositories modified (not implemented yet) Sync whenever an update in repositories is detected diff --git a/app/src/main/res/xml/prefs_screen_auto_sync.xml b/app/src/main/res/xml/prefs_screen_auto_sync.xml index f8d64f8ec..775ae84b0 100644 --- a/app/src/main/res/xml/prefs_screen_auto_sync.xml +++ b/app/src/main/res/xml/prefs_screen_auto_sync.xml @@ -35,6 +35,13 @@ android:summary="@string/pref_summary_on_resume_sync" android:defaultValue="@bool/pref_default_auto_sync_on_resume"/> + +