Files
app_Settings/src/com/android/settings/applications/contacts/ContactsStoragePreferenceController.java
Dongzhuo Zhang 2f16e5824e Few update on contacts storage settings.
1. Add account preference category to contacts storage settings page
2. Preload account icon in settings constructor
3. Re-fetch account in controller to refresh the default account after user sets a different default account.

Test: atest SettingsRoboTests:com.android.settings.applications.contacts.ContactsStorageSettingsTest
atest SettingsRoboTests:com.android.settings.applications.contacts.ContactsStoragePreferenceControllerTest
Bug: 368641291
Flag: android.provider.new_default_account_api_enabled

Change-Id: I3ca2a7a0905118c7a3a47fe4573781ae86c246bd
2024-11-05 23:25:02 +00:00

97 lines
4.3 KiB
Java

/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.contacts;
import static android.provider.ContactsContract.RawContacts.DefaultAccount;
import android.accounts.Account;
import android.content.Context;
import android.os.UserHandle;
import android.provider.ContactsContract.RawContacts.DefaultAccount.DefaultAccountAndState;
import android.provider.Flags;
import android.util.Log;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.accounts.AuthenticatorHelper;
/**
* A preference controller handling the logic for updating summary of contacts default account.
*/
public class ContactsStoragePreferenceController extends BasePreferenceController {
private static final String TAG = "ContactsStorageController";
private final AuthenticatorHelper mAuthenticatorHelper;
private DefaultAccountAndState mCurrentDefaultAccountAndState;
public ContactsStoragePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mAuthenticatorHelper = new AuthenticatorHelper(mContext,
new UserHandle(UserHandle.myUserId()), null);
try {
mCurrentDefaultAccountAndState =
DefaultAccount.getDefaultAccountForNewContacts(mContext.getContentResolver());
} catch (IllegalStateException e) {
Log.e(TAG, "The default account is in an invalid state: " + e);
} catch (RuntimeException e) {
Log.e(TAG, "Failed to look up the default account: " + e);
}
}
@Override
public int getAvailabilityStatus() {
return (Flags.newDefaultAccountApiEnabled()
&& mCurrentDefaultAccountAndState != null) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
@Override
public CharSequence getSummary() {
if (mCurrentDefaultAccountAndState != null) {
// Re-fetch account in controller to refresh the latest set default account.
mCurrentDefaultAccountAndState =
DefaultAccount.getDefaultAccountForNewContacts(mContext.getContentResolver());
int currentDefaultAccountState = mCurrentDefaultAccountAndState.getState();
Account currentDefaultAccount = mCurrentDefaultAccountAndState.getAccount();
if (currentDefaultAccountState
== DefaultAccountAndState.DEFAULT_ACCOUNT_STATE_NOT_SET) {
return mContext.getResources().getString(
R.string.contacts_storage_no_account_set_summary);
} else if (currentDefaultAccountState
== DefaultAccountAndState.DEFAULT_ACCOUNT_STATE_LOCAL) {
return mContext.getResources().getString(
R.string.contacts_storage_local_account_summary);
} else if (currentDefaultAccountState
== DefaultAccountAndState.DEFAULT_ACCOUNT_STATE_SIM) {
return mContext.getResources().getString(
R.string.sim_card_label);
} else if (currentDefaultAccountState
== DefaultAccountAndState.DEFAULT_ACCOUNT_STATE_CLOUD) {
String accountTypeLabel = (String) mAuthenticatorHelper.getLabelForType(mContext,
currentDefaultAccount.type);
// If there's no account type, or the account type is the same as the
// current default account name, just return the account name.
if (accountTypeLabel == null || accountTypeLabel.equals(
currentDefaultAccount.name)) {
return currentDefaultAccount.name;
}
return accountTypeLabel + " | " + currentDefaultAccount.name;
}
}
return "";
}
}