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.os.Bundle; 23 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceScreen; 26 27 import com.android.settings.R; 28 import com.android.settings.accounts.AccountDetailDashboardFragment; 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 Account[] mAccounts; 36 BrandedAccountPreferenceController(Context context, String key)37 public BrandedAccountPreferenceController(Context context, String key) { 38 super(context, key); 39 final AccountFeatureProvider accountFeatureProvider = FeatureFactory.getFactory( 40 mContext).getAccountFeatureProvider(); 41 mAccounts = accountFeatureProvider.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 AccountFeatureProvider accountFeatureProvider = FeatureFactory.getFactory( 60 mContext).getAccountFeatureProvider(); 61 final Preference accountPreference = screen.findPreference(getPreferenceKey()); 62 if (accountPreference != null && (mAccounts == null || mAccounts.length == 0)) { 63 screen.removePreference(accountPreference); 64 return; 65 } 66 67 accountPreference.setSummary(mAccounts[0].name); 68 accountPreference.setOnPreferenceClickListener(preference -> { 69 final Bundle args = new Bundle(); 70 args.putParcelable(AccountDetailDashboardFragment.KEY_ACCOUNT, 71 mAccounts[0]); 72 args.putParcelable(AccountDetailDashboardFragment.KEY_USER_HANDLE, 73 android.os.Process.myUserHandle()); 74 args.putString(AccountDetailDashboardFragment.KEY_ACCOUNT_TYPE, 75 accountFeatureProvider.getAccountType()); 76 77 new SubSettingLauncher(mContext) 78 .setDestination(AccountDetailDashboardFragment.class.getName()) 79 .setTitleRes(R.string.account_sync_title) 80 .setArguments(args) 81 .setSourceMetricsCategory(SettingsEnums.DEVICEINFO) 82 .launch(); 83 return true; 84 }); 85 } 86 } 87