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.content.Context; 21 import android.os.Bundle; 22 import android.support.v7.preference.Preference; 23 import android.support.v7.preference.PreferenceScreen; 24 25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 26 import com.android.settings.R; 27 import com.android.settings.accounts.AccountDetailDashboardFragment; 28 import com.android.settings.accounts.AccountFeatureProvider; 29 import com.android.settings.core.BasePreferenceController; 30 import com.android.settings.core.SubSettingLauncher; 31 import com.android.settings.overlay.FeatureFactory; 32 33 public class BrandedAccountPreferenceController extends BasePreferenceController { 34 private static final String KEY_PREFERENCE_TITLE = "branded_account"; 35 private final Account[] mAccounts; 36 BrandedAccountPreferenceController(Context context)37 public BrandedAccountPreferenceController(Context context) { 38 super(context, KEY_PREFERENCE_TITLE); 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 (mAccounts != null && mAccounts.length > 0) { 47 return AVAILABLE; 48 } 49 return DISABLED_FOR_USER; 50 } 51 52 @Override displayPreference(PreferenceScreen screen)53 public void displayPreference(PreferenceScreen screen) { 54 super.displayPreference(screen); 55 final AccountFeatureProvider accountFeatureProvider = FeatureFactory.getFactory( 56 mContext).getAccountFeatureProvider(); 57 final Preference accountPreference = screen.findPreference(KEY_PREFERENCE_TITLE); 58 if (accountPreference != null && (mAccounts == null || mAccounts.length == 0)) { 59 screen.removePreference(accountPreference); 60 return; 61 } 62 63 accountPreference.setSummary(mAccounts[0].name); 64 accountPreference.setOnPreferenceClickListener(preference -> { 65 final Bundle args = new Bundle(); 66 args.putParcelable(AccountDetailDashboardFragment.KEY_ACCOUNT, 67 mAccounts[0]); 68 args.putParcelable(AccountDetailDashboardFragment.KEY_USER_HANDLE, 69 android.os.Process.myUserHandle()); 70 args.putString(AccountDetailDashboardFragment.KEY_ACCOUNT_TYPE, 71 accountFeatureProvider.getAccountType()); 72 73 new SubSettingLauncher(mContext) 74 .setDestination(AccountDetailDashboardFragment.class.getName()) 75 .setTitle(R.string.account_sync_title) 76 .setArguments(args) 77 .setSourceMetricsCategory(MetricsEvent.DEVICEINFO) 78 .launch(); 79 return true; 80 }); 81 } 82 } 83