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 import android.text.TextUtils;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settings.accounts.AccountDetailDashboardFragment;
30 import com.android.settings.accounts.AccountFeatureProvider;
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settings.core.SubSettingLauncher;
33 import com.android.settings.overlay.FeatureFactory;
34 
35 public class BrandedAccountPreferenceController extends BasePreferenceController {
36     private final AccountFeatureProvider mAccountFeatureProvider;
37     private Account[] mAccounts;
38 
BrandedAccountPreferenceController(Context context, String key)39     public BrandedAccountPreferenceController(Context context, String key) {
40         super(context, key);
41         mAccountFeatureProvider = FeatureFactory.getFactory(mContext).getAccountFeatureProvider();
42         mAccounts = mAccountFeatureProvider.getAccounts(mContext);
43     }
44 
45     @Override
getAvailabilityStatus()46     public int getAvailabilityStatus() {
47         if (!mContext.getResources().getBoolean(
48                 R.bool.config_show_branded_account_in_device_info)) {
49             return UNSUPPORTED_ON_DEVICE;
50         }
51         if (mAccounts != null && mAccounts.length > 0) {
52             return AVAILABLE;
53         }
54         return DISABLED_FOR_USER;
55     }
56 
57     @Override
displayPreference(PreferenceScreen screen)58     public void displayPreference(PreferenceScreen screen) {
59         super.displayPreference(screen);
60         final Preference accountPreference = screen.findPreference(getPreferenceKey());
61         if (accountPreference != null && (mAccounts == null || mAccounts.length == 0)) {
62             screen.removePreference(accountPreference);
63             return;
64         }
65 
66         accountPreference.setSummary(mAccounts[0].name);
67     }
68 
69     @Override
handlePreferenceTreeClick(Preference preference)70     public boolean handlePreferenceTreeClick(Preference preference) {
71         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
72             return false;
73         }
74 
75         final Bundle args = new Bundle();
76         args.putParcelable(AccountDetailDashboardFragment.KEY_ACCOUNT,
77                 mAccounts[0]);
78         args.putParcelable(AccountDetailDashboardFragment.KEY_USER_HANDLE,
79                 android.os.Process.myUserHandle());
80         args.putString(AccountDetailDashboardFragment.KEY_ACCOUNT_TYPE,
81                 mAccountFeatureProvider.getAccountType());
82 
83         new SubSettingLauncher(mContext)
84                 .setDestination(AccountDetailDashboardFragment.class.getName())
85                 .setTitleRes(R.string.account_sync_title)
86                 .setArguments(args)
87                 .setSourceMetricsCategory(SettingsEnums.DEVICEINFO)
88                 .launch();
89         return true;
90     }
91 
92     @Override
updateState(Preference preference)93     public void updateState(Preference preference) {
94         super.updateState(preference);
95         mAccounts = mAccountFeatureProvider.getAccounts(mContext);
96         if (mAccounts == null || mAccounts.length == 0) {
97             preference.setVisible(false);
98         }
99     }
100 }
101