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.car.settings.accounts;
18 
19 import android.accounts.Account;
20 import android.car.drivingstate.CarUxRestrictions;
21 import android.content.Context;
22 import android.os.UserHandle;
23 
24 import androidx.annotation.CallSuper;
25 import androidx.preference.Preference;
26 
27 import com.android.car.settings.common.FragmentController;
28 import com.android.car.settings.common.Logger;
29 import com.android.car.settings.common.PreferenceController;
30 import com.android.settingslib.accounts.AuthenticatorHelper;
31 
32 /** Controller for the preference that shows the details of an account. */
33 public class AccountDetailsPreferenceController extends PreferenceController<Preference> {
34     private static final Logger LOG = new Logger(AccountDetailsPreferenceController.class);
35 
36     private Account mAccount;
37     private UserHandle mUserHandle;
38 
AccountDetailsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)39     public AccountDetailsPreferenceController(Context context, String preferenceKey,
40             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
41         super(context, preferenceKey, fragmentController, uxRestrictions);
42     }
43 
44     /** Sets the account that the details are shown for. */
setAccount(Account account)45     public void setAccount(Account account) {
46         mAccount = account;
47     }
48 
49     /** Returns the account the details are shown for. */
getAccount()50     public Account getAccount() {
51         return mAccount;
52     }
53 
54     /** Sets the UserHandle used by the controller. */
setUserHandle(UserHandle userHandle)55     public void setUserHandle(UserHandle userHandle) {
56         mUserHandle = userHandle;
57     }
58 
59     /** Returns the UserHandle used by the controller. */
getUserHandle()60     public UserHandle getUserHandle() {
61         return mUserHandle;
62     }
63 
64     @Override
getPreferenceType()65     protected Class<Preference> getPreferenceType() {
66         return Preference.class;
67     }
68 
69     /**
70      * Verifies that the controller was properly initialized with
71      * {@link #setAccount(Account)} and {@link #setUserHandle(UserHandle)}.
72      *
73      * @throws IllegalStateException if the account or user handle are {@code null}
74      */
75     @Override
76     @CallSuper
checkInitialized()77     protected void checkInitialized() {
78         LOG.v("checkInitialized");
79         if (mAccount == null) {
80             throw new IllegalStateException(
81                     "AccountDetailsPreferenceController must be initialized by calling "
82                             + "setAccount(Account)");
83         }
84         if (mUserHandle == null) {
85             throw new IllegalStateException(
86                     "AccountDetailsPreferenceController must be initialized by calling "
87                             + "setUserHandle(UserHandle)");
88         }
89     }
90 
91     @Override
92     @CallSuper
updateState(Preference preference)93     protected void updateState(Preference preference) {
94         preference.setTitle(mAccount.name);
95         // Get the icon corresponding to the account's type and set it.
96         AuthenticatorHelper helper = new AuthenticatorHelper(getContext(), mUserHandle, null);
97         preference.setIcon(helper.getDrawableForType(getContext(), mAccount.type));
98     }
99 }
100