Merge pull request #10636 from nextcloud/fix/contacts-backup-switch

Persist contacts/calendar backup preferences
This commit is contained in:
Álvaro Brey 2022-08-19 15:37:42 +02:00 committed by GitHub
commit 1600d9ead2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 52 deletions

View file

@ -65,6 +65,10 @@ public class ArbitraryDataProvider {
storeOrUpdateKeyValue(accountName, key, String.valueOf(newValue));
}
public void storeOrUpdateKeyValue(final String accountName, final String key, final boolean newValue) {
storeOrUpdateKeyValue(accountName, key, String.valueOf(newValue));
}
public void storeOrUpdateKeyValue(@NonNull String accountName,
@NonNull String key,
@Nullable String newValue) {

View file

@ -37,6 +37,7 @@ import android.widget.Toast;
import com.nextcloud.client.account.User;
import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.jobs.BackgroundJobManager;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.java.util.Optional;
import com.owncloud.android.R;
import com.owncloud.android.databinding.BackupFragmentBinding;
@ -44,6 +45,7 @@ import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.operations.RefreshFolderOperation;
import com.owncloud.android.ui.activity.ContactsPreferenceActivity;
import com.owncloud.android.ui.activity.SettingsActivity;
@ -82,6 +84,10 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
private static final String KEY_CALENDAR_MONTH = "CALENDAR_MONTH";
private static final String KEY_CALENDAR_YEAR = "CALENDAR_YEAR";
public static final String PREFERENCE_CONTACTS_BACKUP_ENABLED = "PREFERENCE_CONTACTS_BACKUP_ENABLED";
public static final String PREFERENCE_CALENDAR_BACKUP_ENABLED = "PREFERENCE_CALENDAR_BACKUP_ENABLED";
private BackupFragmentBinding binding;
@Inject BackgroundJobManager backgroundJobManager;
@ -90,6 +96,7 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
@Inject ThemeUtils themeUtils;
@Inject ThemeCheckableUtils themeCheckableUtils;
@Inject ThemeButtonUtils themeButtonUtils;
@Inject ArbitraryDataProvider arbitraryDataProvider;
private Date selectedDate;
private boolean calendarPickerOpen;
@ -99,7 +106,6 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
private CompoundButton.OnCheckedChangeListener dailyBackupCheckedChangeListener;
private CompoundButton.OnCheckedChangeListener contactsCheckedListener;
private CompoundButton.OnCheckedChangeListener calendarCheckedListener;
private ArbitraryDataProvider arbitraryDataProvider;
private User user;
private boolean showSidebar = true;
@ -111,6 +117,22 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
return fragment;
}
private boolean isCalendarBackupEnabled() {
return arbitraryDataProvider.getBooleanValue(user, PREFERENCE_CALENDAR_BACKUP_ENABLED);
}
private void setCalendarBackupEnabled(final boolean enabled) {
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), PREFERENCE_CALENDAR_BACKUP_ENABLED, enabled);
}
private boolean isContactsBackupEnabled() {
return arbitraryDataProvider.getBooleanValue(user, PREFERENCE_CONTACTS_BACKUP_ENABLED);
}
private void setContactsBackupEnabled(final boolean enabled) {
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), PREFERENCE_CONTACTS_BACKUP_ENABLED, enabled);
}
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -140,7 +162,6 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
themeToolbarUtils.tintBackButton(actionBar, getContext());
}
arbitraryDataProvider = new ArbitraryDataProvider(getContext().getContentResolver());
themeCheckableUtils.tintSwitch(binding.contacts, themeColorUtils);
themeCheckableUtils.tintSwitch(binding.calendar, themeColorUtils);
@ -148,46 +169,15 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
binding.dailyBackup.setChecked(arbitraryDataProvider.getBooleanValue(user,
PREFERENCE_CONTACTS_AUTOMATIC_BACKUP));
binding.contacts.setChecked(checkContactBackupPermission());
binding.calendar.setChecked(checkCalendarBackupPermission(getContext()));
binding.contacts.setChecked(isContactsBackupEnabled() && checkContactBackupPermission());
binding.calendar.setChecked(isCalendarBackupEnabled() && checkCalendarBackupPermission(getContext()));
dailyBackupCheckedChangeListener = (buttonView, isChecked) -> {
if (checkAndAskForContactsReadPermission()) {
setAutomaticBackup(isChecked);
}
};
contactsCheckedListener = (buttonView, isChecked) -> {
if (isChecked) {
if (checkAndAskForContactsReadPermission()) {
binding.backupNow.setVisibility(View.VISIBLE);
}
} else {
if (!binding.calendar.isChecked()) {
binding.backupNow.setVisibility(View.INVISIBLE);
}
}
};
binding.contacts.setOnCheckedChangeListener(contactsCheckedListener);
setupCheckListeners();
calendarCheckedListener = (buttonView, isChecked) -> {
if (isChecked) {
if (checkAndAskForCalendarReadPermission()) {
binding.backupNow.setVisibility(View.VISIBLE);
}
} else {
if (!binding.contacts.isChecked()) {
binding.backupNow.setVisibility(View.INVISIBLE);
}
}
};
setBackupNowButtonVisibility();
binding.calendar.setOnCheckedChangeListener(calendarCheckedListener);
binding.dailyBackup.setOnCheckedChangeListener(dailyBackupCheckedChangeListener);
binding.backupNow.setOnClickListener(v -> backup());
binding.backupNow.setEnabled(checkBackupNowPermission());
binding.backupNow.setVisibility(checkBackupNowPermission() ? View.VISIBLE : View.GONE);
binding.backupNow.setOnClickListener(v -> backupNow());
binding.contactsDatepicker.setOnClickListener(v -> openCleanDate());
@ -222,6 +212,50 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
return view;
}
private void setupCheckListeners() {
dailyBackupCheckedChangeListener = (buttonView, isChecked) -> {
if (checkAndAskForContactsReadPermission()) {
setAutomaticBackup(isChecked);
}
};
binding.dailyBackup.setOnCheckedChangeListener(dailyBackupCheckedChangeListener);
contactsCheckedListener = (buttonView, isChecked) -> {
if (isChecked) {
if (checkAndAskForContactsReadPermission()) {
setContactsBackupEnabled(true);
}
} else {
setContactsBackupEnabled(false);
}
setBackupNowButtonVisibility();
setAutomaticBackup(binding.dailyBackup.isChecked());
};
binding.contacts.setOnCheckedChangeListener(contactsCheckedListener);
calendarCheckedListener = (buttonView, isChecked) -> {
if (isChecked) {
if (checkAndAskForCalendarReadPermission()) {
setCalendarBackupEnabled(true);
}
} else {
setCalendarBackupEnabled(false);
}
setBackupNowButtonVisibility();
setAutomaticBackup(binding.dailyBackup.isChecked());
};
binding.calendar.setOnCheckedChangeListener(calendarCheckedListener);
}
private void setBackupNowButtonVisibility() {
if (binding.contacts.isChecked() || binding.calendar.isChecked()) {
binding.backupNow.setVisibility(View.VISIBLE);
} else {
binding.backupNow.setVisibility(View.INVISIBLE);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@ -329,6 +363,7 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
if (Manifest.permission.READ_CONTACTS.equalsIgnoreCase(permissions[index])) {
if (grantResults[index] >= 0) {
// if approved, exit for loop
setContactsBackupEnabled(true);
break;
}
@ -355,21 +390,21 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
binding.calendar.setOnCheckedChangeListener(null);
binding.calendar.setChecked(false);
binding.calendar.setOnCheckedChangeListener(calendarCheckedListener);
binding.backupNow.setVisibility(checkBackupNowPermission() ? View.VISIBLE : View.GONE);
} else {
setCalendarBackupEnabled(true);
}
}
binding.backupNow.setVisibility(checkBackupNowPermission() ? View.VISIBLE : View.GONE);
binding.backupNow.setEnabled(checkBackupNowPermission());
setBackupNowButtonVisibility();
setAutomaticBackup(binding.dailyBackup.isChecked());
}
public void backup() {
if (binding.contacts.isChecked() && checkAndAskForContactsReadPermission()) {
public void backupNow() {
if (isContactsBackupEnabled() && checkContactBackupPermission()) {
startContactsBackupJob();
}
if (binding.calendar.isChecked() && checkAndAskForCalendarReadPermission()) {
if (isCalendarBackupEnabled() && checkCalendarBackupPermission(requireContext())) {
startCalendarBackupJob();
}
@ -409,9 +444,22 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
}
User user = optionalUser.get();
if (enabled) {
backgroundJobManager.schedulePeriodicContactsBackup(user);
backgroundJobManager.schedulePeriodicCalendarBackup(user);
if (isContactsBackupEnabled()) {
Log_OC.d(TAG, "Scheduling contacts backup job");
backgroundJobManager.schedulePeriodicContactsBackup(user);
} else {
Log_OC.d(TAG, "Cancelling contacts backup job");
backgroundJobManager.cancelPeriodicContactsBackup(user);
}
if (isCalendarBackupEnabled()) {
Log_OC.d(TAG, "Scheduling calendar backup job");
backgroundJobManager.schedulePeriodicCalendarBackup(user);
} else {
Log_OC.d(TAG, "Cancelling calendar backup job");
backgroundJobManager.cancelPeriodicCalendarBackup(user);
}
} else {
Log_OC.d(TAG, "Cancelling all backup jobs");
backgroundJobManager.cancelPeriodicContactsBackup(user);
backgroundJobManager.cancelPeriodicCalendarBackup(user);
}
@ -449,11 +497,6 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
}
}
private boolean checkBackupNowPermission() {
return (checkCalendarBackupPermission(getContext()) && binding.calendar.isChecked()) ||
(checkContactBackupPermission() && binding.contacts.isChecked());
}
private boolean checkCalendarBackupPermission(final Context context) {
return PermissionUtil.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) && PermissionUtil.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR);
}