#656 Background synchronization

Add preference to UI
This commit is contained in:
stefan-niedermann 2020-01-30 18:19:48 +01:00
parent a48fc3ce3e
commit f9383c47db
7 changed files with 78 additions and 18 deletions

View file

@ -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

View file

@ -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;
});
}
}

View file

@ -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) {

View file

@ -0,0 +1,6 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#757575" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha=".3" android:fillColor="#FF000000" android:pathData="M12.01,21.49L23.64,7c-0.45,-0.34 -4.93,-4 -11.64,-4C5.28,3 0.81,6.66 0.36,7l11.63,14.49 0.01,0.01 0.01,-0.01z"/>
<path android:fillColor="#FF000000" android:pathData="M3.53,10.95l8.46,10.54 0.01,0.01 0.01,-0.01 8.46,-10.54C20.04,10.62 16.81,8 12,8c-4.81,0 -8.04,2.62 -8.47,2.95z"/>
</vector>

View file

@ -10,4 +10,10 @@
<item>@string/pref_value_font_size_medium</item>
<item>@string/pref_value_font_size_large</item>
</string-array>
<string-array name="sync_values">
<item>@string/pref_value_sync_off</item>
<item>@string/pref_value_sync_15_minutes</item>
<item>@string/pref_value_sync_1_hour</item>
<item>@string/pref_value_sync_6_hours</item>
</string-array>
</resources>

View file

@ -43,6 +43,7 @@
<string name="settings_font_title">Monospace font</string>
<string name="settings_font_size">Font size</string>
<string name="settings_wifi_only">Sync only on Wi-Fi</string>
<string name="settings_background_sync">Background synchronization</string>
<!-- Certificates -->
@ -112,15 +113,22 @@
<string name="pref_key_font_size" translatable="false">fontSize</string>
<string name="pref_key_wifi_only" translatable="false">wifiOnly</string>
<string name="pref_key_last_note_mode" translatable="false">lastNoteMode</string>
<string name="pref_key_background_sync" translatable="false">backgroundSync</string>
<string name="pref_value_mode_edit" translatable="false">edit</string>
<string name="pref_value_mode_preview" translatable="false">preview</string>
<string name="pref_value_mode_last" translatable="false">last</string>
<string name="pref_value_font_size_small" translatable="false">small</string>
<string name="pref_value_font_size_medium" translatable="false">medium</string>
<string name="pref_value_font_size_large" translatable="false">large</string>
<string name="pref_value_sync_off" translatable="false">off</string>
<string name="pref_value_sync_15_minutes" translatable="false">15_minutes</string>
<string name="pref_value_sync_1_hour" translatable="false">1_hour</string>
<string name="pref_value_sync_6_hours" translatable="false">6_hours</string>
<!-- These values should not be translateable. They should be migrated at some point. -->
<string name="pref_value_theme_light">Light</string>
<string name="pref_value_font_normal">Normal</string>
<string name="pref_value_wifi_and_mobile">Sync on Wi-Fi and mobile data</string>
<string name="simple_error">Error</string>
<string name="simple_close">Close</string>
<string name="simple_copy">Copy</string>
@ -161,6 +169,14 @@
<item>Large</item>
</string-array>
<!-- Array: background synchronization -->
<string-array name="sync_entries">
<item>Off</item>
<item>15 minutes</item>
<item>1 hour</item>
<item>6 hours</item>
</string-array>
<!-- Plurals -->
<plurals name="ab_selected">
<item quantity="one">%d selected</item>

View file

@ -37,9 +37,19 @@
<SwitchPreference
android:defaultValue="@string/pref_value_wifi_and_mobile"
android:icon="@drawable/ic_sync_black_24dp"
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" />
<ListPreference
android:defaultValue="@string/pref_value_sync_off"
android:entries="@array/sync_entries"
android:entryValues="@array/sync_values"
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" />
</PreferenceScreen>