1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.deviceinfo; 18 19 import android.accounts.Account; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.text.TextUtils; 23 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceScreen; 26 27 import com.android.settings.R; 28 import com.android.settings.accounts.AccountDashboardFragment; 29 import com.android.settings.accounts.AccountFeatureProvider; 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.core.SubSettingLauncher; 32 import com.android.settings.overlay.FeatureFactory; 33 34 public class BrandedAccountPreferenceController extends BasePreferenceController { 35 private final AccountFeatureProvider mAccountFeatureProvider; 36 private Account[] mAccounts; 37 BrandedAccountPreferenceController(Context context, String key)38 public BrandedAccountPreferenceController(Context context, String key) { 39 super(context, key); 40 mAccountFeatureProvider = FeatureFactory.getFeatureFactory().getAccountFeatureProvider(); 41 mAccounts = mAccountFeatureProvider.getAccounts(mContext); 42 } 43 44 @Override getAvailabilityStatus()45 public int getAvailabilityStatus() { 46 if (!mContext.getResources().getBoolean( 47 R.bool.config_show_branded_account_in_device_info)) { 48 return UNSUPPORTED_ON_DEVICE; 49 } 50 if (mAccounts != null && mAccounts.length > 0) { 51 return AVAILABLE; 52 } 53 return DISABLED_FOR_USER; 54 } 55 56 @Override displayPreference(PreferenceScreen screen)57 public void displayPreference(PreferenceScreen screen) { 58 super.displayPreference(screen); 59 final Preference accountPreference = screen.findPreference(getPreferenceKey()); 60 if (accountPreference != null && (mAccounts == null || mAccounts.length == 0)) { 61 screen.removePreference(accountPreference); 62 return; 63 } 64 65 if (mAccounts.length == 1) { 66 accountPreference.setSummary(mAccounts[0].name); 67 } else { 68 accountPreference.setSummary(getAccountSummary(mAccounts.length)); 69 } 70 } 71 72 @Override handlePreferenceTreeClick(Preference preference)73 public boolean handlePreferenceTreeClick(Preference preference) { 74 if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) { 75 return false; 76 } 77 78 new SubSettingLauncher(mContext) 79 .setDestination(AccountDashboardFragment.class.getName()) 80 .setTitleRes(R.string.account_dashboard_title) 81 .setSourceMetricsCategory(SettingsEnums.DEVICEINFO) 82 .launch(); 83 return true; 84 } 85 86 @Override updateState(Preference preference)87 public void updateState(Preference preference) { 88 super.updateState(preference); 89 mAccounts = mAccountFeatureProvider.getAccounts(mContext); 90 if (mAccounts == null || mAccounts.length == 0) { 91 preference.setVisible(false); 92 } 93 } 94 getAccountSummary(int accountNo)95 private String getAccountSummary(int accountNo) { 96 return mContext.getResources() 97 .getString(R.string.my_device_info_account_preference_summary, accountNo); 98 } 99 } 100