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