Disable the sync now menu while no any synced item

- Check the number of the switch on for all SyncStateSwitchPreferences to
  enable/disable the sync now menu item.

Fixes: 147030748
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.accounts
Change-Id: I88e4b042e6d236b98866b9e8bff9ec2c64b41b87
This commit is contained in:
Sunny Shao
2020-03-16 22:16:33 +08:00
parent 856079384a
commit 0030d78b62
3 changed files with 77 additions and 10 deletions

View File

@@ -42,6 +42,7 @@ import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
@@ -221,9 +222,8 @@ public class AccountSyncSettings extends AccountPreferenceBase {
// Note that this also counts accounts that are not currently displayed
boolean syncActive = !ContentResolver.getCurrentSyncsAsUser(
mUserHandle.getIdentifier()).isEmpty();
menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive);
menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive).setEnabled(enabledSyncNowMenu());
menu.findItem(MENU_SYNC_CANCEL_ID).setVisible(syncActive);
}
@Override
@@ -562,6 +562,23 @@ public class AccountSyncSettings extends AccountPreferenceBase {
return R.string.help_url_accounts;
}
@VisibleForTesting
boolean enabledSyncNowMenu() {
boolean enabled = false;
for (int i = 0, count = getPreferenceScreen().getPreferenceCount(); i < count; i++) {
final Preference pref = getPreferenceScreen().getPreference(i);
if (!(pref instanceof SyncStateSwitchPreference)) {
continue;
}
final SyncStateSwitchPreference syncPref = (SyncStateSwitchPreference) pref;
if (syncPref.isChecked()) {
enabled = true;
break;
}
}
return enabled;
}
private static String formatSyncDate(Context context, Date date) {
return DateUtils.formatDateTime(context, date.getTime(),
DateUtils.FORMAT_SHOW_DATE