Migration is added to enable background sync

This commit is contained in:
MasterWanna 2021-05-02 17:35:34 +08:00
parent 0483063a10
commit a8593aabc1
4 changed files with 20 additions and 35 deletions

View file

@ -44,23 +44,12 @@ public class SyncWorker extends Worker {
return Result.success();
}
public static void update(@NonNull Context context, @NonNull String preferenceValue) {
public static void update(@NonNull Context context) {
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(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, work);
Log.i(TAG, "Registering worker running each " + repeatInterval + " " + unit);
}
PeriodicWorkRequest work = new PeriodicWorkRequest.Builder(SyncWorker.class, 15, TimeUnit.MINUTES)
.setConstraints(constraints).build();
WorkManager.getInstance(context.getApplicationContext()).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, work);
Log.i(TAG, "Registering worker running each " + 15 + " " + TimeUnit.MINUTES);
}
private static void deregister(@NonNull Context context) {

View file

@ -8,8 +8,6 @@ import androidx.preference.PreferenceManager;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;
// CS304 issue link : https://github.com/stefan-niedermann/nextcloud-notes/issues/1168
public class Migration_21_22 extends Migration {
@NonNull
private final Context context;
@ -18,17 +16,18 @@ public class Migration_21_22 extends Migration {
super(21, 22);
this.context = context;
}
/**
* Enabling Set backgroundSync to every 15 minutes, and wifiOnly to true
* @param database no use just implement
* Enabling backgroundSync, set from {@link String} values to {@link Boolean} value true
* https://github.com/stefan-niedermann/nextcloud-deck/issues/531
*/
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("wifiOnly", true);
editor.putString("backgroundSync", "15_minutes");
if (sharedPreferences.contains("backgroundSync")) {
editor.remove("backgroundSync");
}
editor.putBoolean("backgroundSync", true);
editor.apply();
}
}

View file

@ -31,6 +31,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
private BrandedSwitchPreference wifiOnlyPref;
private BrandedSwitchPreference gridViewPref;
private BrandedSwitchPreference preventScreenCapturePref;
private BrandedSwitchPreference backgroundSyncPref;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
@ -91,11 +92,11 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
return true;
});
final ListPreference syncPref = findPreference(getString(R.string.pref_key_background_sync));
assert syncPref != null;
syncPref.setOnPreferenceChangeListener((preference, newValue) -> {
Log.i(TAG, "syncPref: " + preference + " - newValue: " + newValue);
SyncWorker.update(requireContext(), newValue.toString());
backgroundSyncPref = findPreference(getString(R.string.pref_key_background_sync));
assert backgroundSyncPref != null;
backgroundSyncPref.setOnPreferenceChangeListener((preference, newValue) -> {
Log.i(TAG, "backgroundSync: " + newValue);
SyncWorker.update(requireContext());
return true;
});
}

View file

@ -11,17 +11,13 @@
android:icon="@drawable/ic_network_wifi_grey600_24dp"
android:key="@string/pref_key_wifi_only"
android:layout="@layout/item_pref"
android:title="@string/settings_wifi_only"
app:defaultValue="true" />
android:title="@string/settings_wifi_only" />
<ListPreference
android:defaultValue="@string/pref_value_sync_15_minutes"
android:entries="@array/sync_entries"
android:entryValues="@array/sync_values"
<it.niedermann.owncloud.notes.branding.BrandedSwitchPreference
android:defaultValue="true"
android:icon="@drawable/ic_sync_black_24dp"
android:key="@string/pref_key_background_sync"
android:layout="@layout/item_pref"
android:summary="%s"
android:title="@string/settings_background_sync" />
</it.niedermann.owncloud.notes.branding.BrandedPreferenceCategory>