Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 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.accounts.AccountManager; |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 21 | import android.car.drivingstate.CarUxRestrictions; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 22 | import android.car.userlib.CarUserManagerHelper; |
| 23 | import android.content.Context; |
| 24 | import android.content.pm.UserInfo; |
| 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.os.UserHandle; |
| 27 | |
| 28 | import androidx.collection.ArrayMap; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 29 | import androidx.preference.Preference; |
| 30 | import androidx.preference.PreferenceCategory; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 31 | |
| 32 | import com.android.car.settings.R; |
| 33 | import com.android.car.settings.common.FragmentController; |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 34 | import com.android.car.settings.common.PreferenceController; |
Peter Li | 97efb4a | 2019-10-15 16:24:34 -0700 | [diff] [blame] | 35 | import com.android.car.ui.preference.CarUiPreference; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 36 | import com.android.settingslib.accounts.AuthenticatorHelper; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.Arrays; |
| 40 | import java.util.Collections; |
| 41 | import java.util.Comparator; |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 42 | import java.util.HashSet; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 43 | import java.util.List; |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 44 | import java.util.Set; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Controller for listing accounts. |
| 48 | * |
| 49 | * <p>Largely derived from {@link com.android.settings.accounts.AccountPreferenceController} |
| 50 | */ |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 51 | public class AccountListPreferenceController extends |
| 52 | PreferenceController<PreferenceCategory> implements |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 53 | AuthenticatorHelper.OnAccountsUpdateListener, |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 54 | CarUserManagerHelper.OnUsersUpdateListener { |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 55 | private static final String NO_ACCOUNT_PREF_KEY = "no_accounts_added"; |
| 56 | |
| 57 | private final UserInfo mUserInfo; |
| 58 | private final CarUserManagerHelper mCarUserManagerHelper; |
| 59 | private final ArrayMap<String, Preference> mPreferences = new ArrayMap<>(); |
| 60 | private AuthenticatorHelper mAuthenticatorHelper; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 61 | private String[] mAuthorities; |
Heemin Seog | 77ffa52 | 2019-08-26 15:40:48 -0700 | [diff] [blame] | 62 | private boolean mListenerRegistered = false; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 63 | |
| 64 | public AccountListPreferenceController(Context context, String preferenceKey, |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 65 | FragmentController fragmentController, CarUxRestrictions uxRestrictions) { |
| 66 | super(context, preferenceKey, fragmentController, uxRestrictions); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 67 | mCarUserManagerHelper = new CarUserManagerHelper(context); |
| 68 | mUserInfo = mCarUserManagerHelper.getCurrentProcessUserInfo(); |
| 69 | mAuthenticatorHelper = new AuthenticatorHelper(context, |
| 70 | mUserInfo.getUserHandle(), /* listener= */ this); |
| 71 | } |
| 72 | |
| 73 | /** Sets the account authorities that are available. */ |
| 74 | public void setAuthorities(String[] authorities) { |
| 75 | mAuthorities = authorities; |
| 76 | } |
| 77 | |
| 78 | @Override |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 79 | protected Class<PreferenceCategory> getPreferenceType() { |
| 80 | return PreferenceCategory.class; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 84 | protected void updateState(PreferenceCategory preference) { |
| 85 | forceUpdateAccountsCategory(); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 89 | protected int getAvailabilityStatus() { |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 90 | return mCarUserManagerHelper.canCurrentProcessModifyAccounts() ? AVAILABLE |
| 91 | : DISABLED_FOR_USER; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Registers the account update and user update callbacks. |
| 96 | */ |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 97 | @Override |
| 98 | protected void onStartInternal() { |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 99 | mAuthenticatorHelper.listenToAccountUpdates(); |
| 100 | mCarUserManagerHelper.registerOnUsersUpdateListener(this); |
Heemin Seog | 77ffa52 | 2019-08-26 15:40:48 -0700 | [diff] [blame] | 101 | mListenerRegistered = true; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Unregisters the account update and user update callbacks. |
| 106 | */ |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 107 | @Override |
| 108 | protected void onStopInternal() { |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 109 | mAuthenticatorHelper.stopListeningToAccountUpdates(); |
| 110 | mCarUserManagerHelper.unregisterOnUsersUpdateListener(this); |
Heemin Seog | 77ffa52 | 2019-08-26 15:40:48 -0700 | [diff] [blame] | 111 | mListenerRegistered = false; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onAccountsUpdate(UserHandle userHandle) { |
| 116 | if (userHandle.equals(mUserInfo.getUserHandle())) { |
| 117 | forceUpdateAccountsCategory(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void onUsersUpdate() { |
| 123 | forceUpdateAccountsCategory(); |
| 124 | } |
| 125 | |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 126 | private boolean onAccountPreferenceClicked(AccountPreference preference) { |
| 127 | // Show the account's details when an account is clicked on. |
| 128 | getFragmentController().launchFragment(AccountDetailsFragment.newInstance( |
| 129 | preference.getAccount(), preference.getLabel(), mUserInfo)); |
| 130 | return true; |
| 131 | } |
| 132 | |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 133 | /** Forces a refresh of the account preferences. */ |
| 134 | private void forceUpdateAccountsCategory() { |
| 135 | // Set the category title and include the user's name |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 136 | getPreference().setTitle( |
| 137 | getContext().getString(R.string.account_list_title, mUserInfo.name)); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 138 | |
| 139 | // Recreate the authentication helper to refresh the list of enabled accounts |
Heemin Seog | 77ffa52 | 2019-08-26 15:40:48 -0700 | [diff] [blame] | 140 | mAuthenticatorHelper.stopListeningToAccountUpdates(); |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 141 | mAuthenticatorHelper = new AuthenticatorHelper(getContext(), mUserInfo.getUserHandle(), |
| 142 | this); |
Heemin Seog | 77ffa52 | 2019-08-26 15:40:48 -0700 | [diff] [blame] | 143 | if (mListenerRegistered) { |
| 144 | mAuthenticatorHelper.listenToAccountUpdates(); |
| 145 | } |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 146 | |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 147 | Set<String> preferencesToRemove = new HashSet<>(mPreferences.keySet()); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 148 | List<? extends Preference> preferences = getAccountPreferences(preferencesToRemove); |
| 149 | // Add all preferences that aren't already shown. Manually set the order so that existing |
| 150 | // preferences are reordered correctly. |
| 151 | for (int i = 0; i < preferences.size(); i++) { |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 152 | Preference pref = preferences.get(i); |
| 153 | pref.setOrder(i); |
| 154 | mPreferences.put(pref.getKey(), pref); |
| 155 | getPreference().addPreference(pref); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 158 | for (String key : preferencesToRemove) { |
| 159 | getPreference().removePreference(mPreferences.get(key)); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 160 | mPreferences.remove(key); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns a list of preferences corresponding to the accounts for the current user. |
| 166 | * |
| 167 | * <p> Derived from |
| 168 | * {@link com.android.settings.accounts.AccountPreferenceController#getAccountTypePreferences} |
| 169 | * |
| 170 | * @param preferencesToRemove the current preferences shown; only preferences to be removed will |
| 171 | * remain after method execution |
| 172 | */ |
| 173 | private List<? extends Preference> getAccountPreferences( |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 174 | Set<String> preferencesToRemove) { |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 175 | String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes(); |
| 176 | ArrayList<AccountPreference> accountPreferences = |
| 177 | new ArrayList<>(accountTypes.length); |
| 178 | |
| 179 | for (int i = 0; i < accountTypes.length; i++) { |
| 180 | String accountType = accountTypes[i]; |
| 181 | // Skip showing any account that does not have any of the requested authorities |
| 182 | if (!accountTypeHasAnyRequestedAuthorities(accountType)) { |
| 183 | continue; |
| 184 | } |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 185 | CharSequence label = mAuthenticatorHelper.getLabelForType(getContext(), accountType); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 186 | if (label == null) { |
| 187 | continue; |
| 188 | } |
| 189 | |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 190 | Account[] accounts = AccountManager.get(getContext()) |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 191 | .getAccountsByTypeAsUser(accountType, mUserInfo.getUserHandle()); |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 192 | Drawable icon = mAuthenticatorHelper.getDrawableForType(getContext(), accountType); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 193 | |
| 194 | // Add a preference row for each individual account |
| 195 | for (Account account : accounts) { |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 196 | String key = AccountPreference.buildKey(account); |
| 197 | AccountPreference preference = (AccountPreference) mPreferences.getOrDefault(key, |
| 198 | new AccountPreference(getContext(), account, label, icon)); |
| 199 | preference.setOnPreferenceClickListener( |
| 200 | (Preference pref) -> onAccountPreferenceClicked((AccountPreference) pref)); |
| 201 | |
| 202 | accountPreferences.add(preference); |
| 203 | preferencesToRemove.remove(key); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 204 | } |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 205 | mAuthenticatorHelper.preloadDrawableForType(getContext(), accountType); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // If there are no accounts, return the "no account added" preference. |
| 209 | if (accountPreferences.isEmpty()) { |
| 210 | preferencesToRemove.remove(NO_ACCOUNT_PREF_KEY); |
Roshan Agrawal | a33c612 | 2018-12-11 17:56:16 -0800 | [diff] [blame] | 211 | return Arrays.asList(mPreferences.getOrDefault(NO_ACCOUNT_PREF_KEY, |
| 212 | createNoAccountsAddedPreference())); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | Collections.sort(accountPreferences, Comparator.comparing( |
| 216 | (AccountPreference a) -> a.getSummary().toString()) |
| 217 | .thenComparing((AccountPreference a) -> a.getTitle().toString())); |
| 218 | |
| 219 | return accountPreferences; |
| 220 | } |
| 221 | |
| 222 | private Preference createNoAccountsAddedPreference() { |
Peter Li | 97efb4a | 2019-10-15 16:24:34 -0700 | [diff] [blame] | 223 | CarUiPreference emptyPreference = new CarUiPreference(getContext()); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 224 | emptyPreference.setTitle(R.string.no_accounts_added); |
| 225 | emptyPreference.setKey(NO_ACCOUNT_PREF_KEY); |
| 226 | emptyPreference.setSelectable(false); |
| 227 | |
| 228 | return emptyPreference; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Returns whether the account type has any of the authorities requested by the caller. |
| 233 | * |
| 234 | * <p> Derived from {@link AccountPreferenceController#accountTypeHasAnyRequestedAuthorities} |
| 235 | */ |
| 236 | private boolean accountTypeHasAnyRequestedAuthorities(String accountType) { |
| 237 | if (mAuthorities == null || mAuthorities.length == 0) { |
| 238 | // No authorities required |
| 239 | return true; |
| 240 | } |
| 241 | ArrayList<String> authoritiesForType = |
| 242 | mAuthenticatorHelper.getAuthoritiesForAccountType(accountType); |
| 243 | if (authoritiesForType == null) { |
| 244 | return false; |
| 245 | } |
| 246 | for (int j = 0; j < mAuthorities.length; j++) { |
| 247 | if (authoritiesForType.contains(mAuthorities[j])) { |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
Peter Li | 6d4565e | 2019-12-10 16:28:36 -0800 | [diff] [blame] | 254 | private static class AccountPreference extends CarUiPreference { |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 255 | /** Account that this Preference represents. */ |
| 256 | private final Account mAccount; |
Roshan Agrawal | a2df3f1 | 2018-11-06 13:52:37 -0800 | [diff] [blame] | 257 | private final CharSequence mLabel; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 258 | |
| 259 | private AccountPreference(Context context, Account account, CharSequence label, |
| 260 | Drawable icon) { |
| 261 | super(context); |
| 262 | mAccount = account; |
Roshan Agrawal | a2df3f1 | 2018-11-06 13:52:37 -0800 | [diff] [blame] | 263 | mLabel = label; |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 264 | |
| 265 | setKey(buildKey(account)); |
| 266 | setTitle(account.name); |
| 267 | setSummary(label); |
| 268 | setIcon(icon); |
Peter Li | 6d4565e | 2019-12-10 16:28:36 -0800 | [diff] [blame] | 269 | setShowChevron(false); |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Build a unique preference key based on the account. |
| 274 | */ |
| 275 | public static String buildKey(Account account) { |
| 276 | return String.valueOf(account.hashCode()); |
| 277 | } |
| 278 | |
| 279 | public Account getAccount() { |
| 280 | return mAccount; |
| 281 | } |
Roshan Agrawal | a2df3f1 | 2018-11-06 13:52:37 -0800 | [diff] [blame] | 282 | |
| 283 | public CharSequence getLabel() { |
| 284 | return mLabel; |
| 285 | } |
Roshan Agrawal | 16bf44c | 2018-10-23 17:17:03 -0700 | [diff] [blame] | 286 | } |
| 287 | } |