Swtich to use DefaultAccount related API to get and set default account for handling contacts.

Test: atest SettingsRoboTests:com.android.settings.applications.contacts.ContactsStorageSettingsTest
atest SettingsRoboTests:com.android.settings.applications.contacts.ContactsStoragePreferenceControllerTest
Bug: 368641291
Flag: com.android.settings.flags.enable_contacts_default_account_in_settings
Change-Id: I1164c120881714d051a6acc34e75d2b5de0f01b5
This commit is contained in:
Dongzhuo Zhang
2024-10-18 00:55:34 +00:00
parent 286dcff126
commit 8e3e3ea973
5 changed files with 657 additions and 424 deletions

View File

@@ -1,4 +1,3 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
@@ -17,25 +16,27 @@
package com.android.settings.applications.contacts;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.provider.ContactsContract.RawContacts.DefaultAccount;
import static android.provider.Settings.ACTION_ADD_ACCOUNT;
import static android.provider.Settings.EXTRA_ACCOUNT_TYPES;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.ContactsContract.Settings;
import android.provider.ContactsContract.RawContacts.DefaultAccount.DefaultAccountAndState;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceScreen;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.accounts.AddAccountSettings;
import com.android.settings.dashboard.DashboardFragment;
@@ -46,6 +47,7 @@ import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -59,7 +61,7 @@ public class ContactsStorageSettings extends DashboardFragment
private static final String TAG = "ContactsStorageSettings";
private static final String PREF_KEY_ADD_ACCOUNT = "add_account";
private static final String PREF_KEY_DEVICE_ONLY = "device_only_account_preference";
private final Map<String, Account> mAccountMap = new HashMap<>();
private final Map<String, DefaultAccountAndState> mAccountMap = new HashMap<>();
private AuthenticatorHelper mAuthenticatorHelper;
@Override
@@ -73,13 +75,18 @@ public class ContactsStorageSettings extends DashboardFragment
@Override
public void onRadioButtonClicked(@NonNull SelectorWithWidgetPreference selectedPref) {
final String selectedPreferenceKey = selectedPref.getKey();
// Check if current provider is different from the selected provider.
// Check if current account is different from the selected account.
for (String preferenceKey : mAccountMap.keySet()) {
if (selectedPreferenceKey.equals(preferenceKey)) {
selectedPref.setChecked(true);
//TODO: Call DefaultAccount.setDefaultAccountForNewContacts once
// the implementation is ready.
Settings.setDefaultAccount(getContentResolver(), mAccountMap.get(preferenceKey));
try {
DefaultAccount.setDefaultAccountForNewContacts(getContentResolver(),
mAccountMap.get(preferenceKey));
selectedPref.setChecked(true);
} catch (RuntimeException e) {
Toast.makeText(getContext(),
R.string.contacts_storage_set_default_account_error_message,
Toast.LENGTH_SHORT).show();
}
} else {
SelectorWithWidgetPreference unSelectedPreference =
getPreferenceScreen().findPreference(preferenceKey);
@@ -92,10 +99,7 @@ public class ContactsStorageSettings extends DashboardFragment
public boolean onPreferenceClick(@NonNull Preference preference) {
if (PREF_KEY_ADD_ACCOUNT.equals(preference.getKey())) {
Resources resources = Resources.getSystem();
String[] accountTypesArray =
resources.getStringArray(
com.android.internal.R.array.config_rawContactsEligibleDefaultAccountTypes);
String[] accountTypesArray = getEligibleAccountTypes();
Intent intent = new Intent(ACTION_ADD_ACCOUNT);
intent.setClass(getContext(), AddAccountSettings.class);
intent.putExtra(EXTRA_ACCOUNT_TYPES, accountTypesArray);
@@ -108,7 +112,7 @@ public class ContactsStorageSettings extends DashboardFragment
@Override
public void onCreatePreferences(@NonNull Bundle savedInstanceState,
@NonNull String rootKey) {
@NonNull String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
refreshUI();
}
@@ -119,48 +123,60 @@ public class ContactsStorageSettings extends DashboardFragment
// when creating eligible account preferences.
mAccountMap.clear();
final PreferenceScreen screen = getPreferenceScreen();
AccountManager accountManager = AccountManager.get(getPrefContext());
//TODO: Call DefaultAccount.getDefaultAccountForNewContacts once
// implementation is ready.
Account[] accounts = accountManager.getAccounts();
for (int i = 0; i < accounts.length; i++) {
screen.addPreference(buildAccountPreference(accounts[i], i));
List<Account> accounts = DefaultAccount.getEligibleCloudAccounts(getContentResolver());
for (int i = 0; i < accounts.size(); i++) {
screen.addPreference(buildAccountPreference(accounts.get(i), /*order=*/i));
}
// If there's no eligible account types, the "Add Account" preference should
// not be shown to the users.
if (getEligibleAccountTypes().length > 0) {
screen.addPreference(buildAddAccountPreference(accounts.isEmpty()));
}
screen.addPreference(buildAddAccountPreference(accounts.length == 0));
setupDeviceOnlyPreference();
//TODO: Call DefaultAccount.ListEligibleCloudAccounts once the
// implementation is ready. And differentiate device only account vs account not set case.
Account currentDefaultAccount = Settings.getDefaultAccount(getContentResolver());
String preferenceKey = currentDefaultAccount != null ?
String.valueOf(currentDefaultAccount.hashCode()) : PREF_KEY_DEVICE_ONLY;
SelectorWithWidgetPreference preference = getPreferenceScreen().findPreference(
preferenceKey);
if (preference != null) {
preference.setChecked(true);
}
setDefaultAccountPreference();
}
private void setupDeviceOnlyPreference() {
SelectorWithWidgetPreference preference = findPreference(PREF_KEY_DEVICE_ONLY);
if (preference != null) {
preference.setOnClickListener(this);
mAccountMap.put(PREF_KEY_DEVICE_ONLY, null);
mAccountMap.put(PREF_KEY_DEVICE_ONLY, DefaultAccountAndState.ofLocal());
}
}
private void setDefaultAccountPreference() {
DefaultAccountAndState currentDefaultAccountAndState =
DefaultAccount.getDefaultAccountForNewContacts(getContentResolver());
String preferenceKey = getAccountHashCode(currentDefaultAccountAndState);
Account currentDefaultAccount = currentDefaultAccountAndState.getAccount();
// Set the current default account preference to be checked if found among existing
// preferences. If not, then create a new preference for default account.
SelectorWithWidgetPreference preference = null;
if (mAccountMap.containsKey(preferenceKey)) {
preference = getPreferenceScreen().findPreference(preferenceKey);
} else if (preferenceKey != null && currentDefaultAccount != null) {
preference = buildAccountPreference(currentDefaultAccount, mAccountMap.size());
getPreferenceScreen().addPreference(preference);
}
if (preference != null) {
preference.setChecked(true);
}
}
//TODO: Add preference category on account preferences.
private Preference buildAccountPreference(Account account, int order) {
private SelectorWithWidgetPreference buildAccountPreference(Account account, int order) {
SelectorWithWidgetPreference preference = new SelectorWithWidgetPreference(
getPrefContext());
DefaultAccountAndState accountAndState = DefaultAccountAndState.ofCloud(account);
String preferenceKey = getAccountHashCode(accountAndState);
preference.setTitle(mAuthenticatorHelper.getLabelForType(getPrefContext(), account.type));
preference.setIcon(mAuthenticatorHelper.getDrawableForType(getPrefContext(), account.type));
preference.setSummary(account.name);
preference.setKey(String.valueOf(account.hashCode()));
preference.setKey(preferenceKey);
preference.setOnClickListener(this);
preference.setOrder(order);
mAccountMap.put(String.valueOf(account.hashCode()), account);
mAccountMap.put(preferenceKey, accountAndState);
return preference;
}
@@ -178,6 +194,29 @@ public class ContactsStorageSettings extends DashboardFragment
return preference;
}
private @Nullable String getAccountHashCode(DefaultAccountAndState currentDefaultAccountAndState) {
Account currentDefaultAccount = currentDefaultAccountAndState.getAccount();
if (currentDefaultAccount != null && (currentDefaultAccountAndState.getState()
== DefaultAccountAndState.DEFAULT_ACCOUNT_STATE_CLOUD
|| currentDefaultAccountAndState.getState()
== DefaultAccountAndState.DEFAULT_ACCOUNT_STATE_SIM)) {
return String.valueOf(currentDefaultAccount.hashCode());
} else if (currentDefaultAccountAndState.getState()
== DefaultAccountAndState.DEFAULT_ACCOUNT_STATE_LOCAL) {
return PREF_KEY_DEVICE_ONLY;
} else {
// If the account is not set or in error state, it should just return null and won't
// set the checked status in radio button.
return null;
}
}
@VisibleForTesting
String[] getEligibleAccountTypes() {
return Resources.getSystem().getStringArray(
com.android.internal.R.array.config_rawContactsEligibleDefaultAccountTypes);
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.contacts_storage_settings;