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)); 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, public void storeOrUpdateKeyValue(@NonNull String accountName,
@NonNull String key, @NonNull String key,
@Nullable String newValue) { @Nullable String newValue) {

View file

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