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