From f9383c47dbe4a502632561eb893e6799cd05bddc Mon Sep 17 00:00:00 2001 From: stefan-niedermann Date: Thu, 30 Jan 2020 18:19:48 +0100 Subject: [PATCH] #656 Background synchronization Add preference to UI --- .../activity/NotesListViewActivity.java | 3 -- .../android/fragment/PreferencesFragment.java | 18 +++++++--- .../notes/persistence/SyncWorker.java | 35 ++++++++++++++----- .../drawable/ic_network_wifi_grey600_24dp.xml | 6 ++++ app/src/main/res/values/arrays.xml | 6 ++++ app/src/main/res/values/strings.xml | 16 +++++++++ app/src/main/res/xml/preferences.xml | 12 ++++++- 7 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 app/src/main/res/drawable/ic_network_wifi_grey600_24dp.xml diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/activity/NotesListViewActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/android/activity/NotesListViewActivity.java index 79a68891..718194d4 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/activity/NotesListViewActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/activity/NotesListViewActivity.java @@ -68,7 +68,6 @@ import it.niedermann.owncloud.notes.persistence.LoadNotesListTask; import it.niedermann.owncloud.notes.persistence.LoadNotesListTask.NotesLoadedListener; import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper; import it.niedermann.owncloud.notes.persistence.NoteServerSyncHelper; -import it.niedermann.owncloud.notes.persistence.SyncWorker; import it.niedermann.owncloud.notes.util.ExceptionHandler; import it.niedermann.owncloud.notes.util.NoteUtil; @@ -182,8 +181,6 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap setupNavigationList(categoryAdapterSelectedItem); setupNavigationMenu(); setupNotesList(); - - SyncWorker.register(this); } @Override diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/PreferencesFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/PreferencesFragment.java index 757af1bc..7f5df51d 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/PreferencesFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/PreferencesFragment.java @@ -5,11 +5,12 @@ import android.os.Bundle; import android.util.Log; import androidx.annotation.Nullable; -import androidx.preference.Preference; +import androidx.preference.ListPreference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.SwitchPreference; import it.niedermann.owncloud.notes.R; +import it.niedermann.owncloud.notes.persistence.SyncWorker; import it.niedermann.owncloud.notes.util.Notes; public class PreferencesFragment extends PreferenceFragmentCompat { @@ -25,8 +26,8 @@ public class PreferencesFragment extends PreferenceFragmentCompat { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.preferences); - final SwitchPreference themePref = (SwitchPreference) findPreference(getString(R.string.pref_key_theme)); - themePref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> { + final SwitchPreference themePref = findPreference(getString(R.string.pref_key_theme)); + themePref.setOnPreferenceChangeListener((preference, newValue) -> { Boolean darkTheme = (Boolean) newValue; Notes.setAppTheme(darkTheme); getActivity().setResult(Activity.RESULT_OK); @@ -34,11 +35,18 @@ public class PreferencesFragment extends PreferenceFragmentCompat { return true; }); - final SwitchPreference wifiOnlyPref = (SwitchPreference) findPreference(getString(R.string.pref_key_wifi_only)); - wifiOnlyPref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> { + final SwitchPreference wifiOnlyPref = findPreference(getString(R.string.pref_key_wifi_only)); + wifiOnlyPref.setOnPreferenceChangeListener((preference, newValue) -> { Boolean syncOnWifiOnly = (Boolean) newValue; Log.v(TAG, "syncOnWifiOnly: " + syncOnWifiOnly); return true; }); + + final ListPreference syncPref = findPreference(getString(R.string.pref_key_background_sync)); + syncPref.setOnPreferenceChangeListener((preference, newValue) -> { + Log.v(TAG, "syncPref: " + preference + " - newValue: " + newValue); + SyncWorker.update(getContext(), newValue.toString()); + return true; + }); } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/SyncWorker.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/SyncWorker.java index 273e6094..89442a88 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/SyncWorker.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/SyncWorker.java @@ -16,14 +16,19 @@ import com.nextcloud.android.sso.AccountImporter; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; import com.nextcloud.android.sso.model.SingleSignOnAccount; -import it.niedermann.owncloud.notes.model.LocalAccount; +import java.util.concurrent.TimeUnit; -import static java.util.concurrent.TimeUnit.MINUTES; +import it.niedermann.owncloud.notes.R; +import it.niedermann.owncloud.notes.model.LocalAccount; public class SyncWorker extends Worker { - private static final String WORKER_TAG = "background_synchronization"; private static final String TAG = SyncWorker.class.getCanonicalName(); + private static final String WORKER_TAG = "background_synchronization"; + + private static final Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); public SyncWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); @@ -47,12 +52,24 @@ public class SyncWorker extends Worker { return Result.success(); } - public static void register(@NonNull Context context) { - Log.v(TAG, "Registering worker running each " + 15 + " " + MINUTES); - WorkManager - .getInstance(context.getApplicationContext()) - .enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, new PeriodicWorkRequest.Builder(SyncWorker.class, 15, MINUTES) - .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.UNMETERED).build()).build()); + public static void update(Context context, String preferenceValue) { + deregister(context); + if (!context.getString(R.string.pref_value_sync_off).equals(preferenceValue)) { + int repeatInterval = 15; + TimeUnit unit = TimeUnit.MINUTES; + if (context.getString(R.string.pref_value_sync_1_hour).equals(preferenceValue)) { + repeatInterval = 1; + unit = TimeUnit.HOURS; + } else if (context.getString(R.string.pref_value_sync_6_hours).equals(preferenceValue)) { + repeatInterval = 6; + unit = TimeUnit.HOURS; + } + PeriodicWorkRequest work = new PeriodicWorkRequest.Builder(SyncWorker.class, repeatInterval, unit) + .setConstraints(constraints).build(); + WorkManager.getInstance(context.getApplicationContext()).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, work); + Log.v(TAG, "Registering worker running each " + repeatInterval + " " + unit); + + } } private static void deregister(@NonNull Context context) { diff --git a/app/src/main/res/drawable/ic_network_wifi_grey600_24dp.xml b/app/src/main/res/drawable/ic_network_wifi_grey600_24dp.xml new file mode 100644 index 00000000..1c3b9a41 --- /dev/null +++ b/app/src/main/res/drawable/ic_network_wifi_grey600_24dp.xml @@ -0,0 +1,6 @@ + + + + diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 1d4ed668..cc342086 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -10,4 +10,10 @@ @string/pref_value_font_size_medium @string/pref_value_font_size_large + + @string/pref_value_sync_off + @string/pref_value_sync_15_minutes + @string/pref_value_sync_1_hour + @string/pref_value_sync_6_hours + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 78f63ec4..5ab97e3f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,6 +43,7 @@ Monospace font Font size Sync only on Wi-Fi + Background synchronization @@ -112,15 +113,22 @@ fontSize wifiOnly lastNoteMode + backgroundSync edit preview last small medium large + off + 15_minutes + 1_hour + 6_hours + Light Normal Sync on Wi-Fi and mobile data + Error Close Copy @@ -161,6 +169,14 @@ Large + + + Off + 15 minutes + 1 hour + 6 hours + + %d selected diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index d1668a87..b13422db 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -37,9 +37,19 @@ + +