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; 21 import android.content.Context; 22 import android.graphics.drawable.Drawable; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 26 import com.android.settingslib.accounts.AuthenticatorHelper; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * Helper class for managing accounts that belong to a single user. 33 */ 34 class AccountManagerHelper { 35 private final Context mContext; 36 private final AuthenticatorHelper.OnAccountsUpdateListener mUpdateListener; 37 private final AuthenticatorHelper mAuthenticatorHelper; 38 39 private final UserHandle mCurrentUserHandle; 40 AccountManagerHelper(Context context, AuthenticatorHelper.OnAccountsUpdateListener listener)41 public AccountManagerHelper(Context context, 42 AuthenticatorHelper.OnAccountsUpdateListener listener) { 43 mContext = context; 44 mUpdateListener = listener; 45 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 46 mCurrentUserHandle = userManager.getUserInfo(UserHandle.myUserId()).getUserHandle(); 47 48 // Listen to account updates for this user. 49 mAuthenticatorHelper = 50 new AuthenticatorHelper(mContext, mCurrentUserHandle, mUpdateListener); 51 } 52 53 /** 54 * Starts listening to account updates. Every registered listener should be unregistered. 55 */ startListeningToAccountUpdates()56 public void startListeningToAccountUpdates() { 57 mAuthenticatorHelper.listenToAccountUpdates(); 58 } 59 60 /** 61 * Stops listening to account updates. Should be called on cleanup/destroy. 62 */ stopListeningToAccountUpdates()63 public void stopListeningToAccountUpdates() { 64 mAuthenticatorHelper.stopListeningToAccountUpdates(); 65 } 66 67 /** 68 * Returns all the enabled accounts that exist for the current user. These include 1st and 3rd 69 * party accounts. 70 * 71 * @return List of {@code Account} for the currently logged in user. 72 */ getAccountsForCurrentUser()73 public List<Account> getAccountsForCurrentUser() { 74 List<Account> accounts = new ArrayList<>(); 75 76 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes(); 77 for (int i = 0; i < accountTypes.length; i++) { 78 String accountType = accountTypes[i]; 79 Account[] accountsForType = AccountManager.get(mContext) 80 .getAccountsByTypeAsUser(accountType, mCurrentUserHandle); 81 for (Account account : accountsForType) { 82 accounts.add(account); 83 } 84 } 85 return accounts; 86 } 87 88 /** 89 * Returns whether the given account is in the list of accounts for the current user. 90 * Useful for checking whether an account has been deleted. 91 * 92 * @param account Account which existence we're checking for. 93 * @return {@code true} if it exists, {@code false} if it doesn't. 94 */ accountExists(Account account)95 public boolean accountExists(Account account) { 96 if (account == null) { 97 return false; 98 } 99 100 Account[] accounts = AccountManager.get(mContext).getAccountsByTypeAsUser( 101 account.type, mCurrentUserHandle); 102 for (Account other : accounts) { 103 if (other.equals(account)) { 104 return true; 105 } 106 } 107 return false; 108 } 109 110 /** 111 * Wrapper for {@code AuthenticatorHelper.getDrawableForType}. 112 * Gets an icon associated with a particular account type. 113 * 114 * @param accountType the type of account 115 * @return a drawable for the icon 116 */ getDrawableForType(final String accountType)117 public Drawable getDrawableForType(final String accountType) { 118 return mAuthenticatorHelper.getDrawableForType(mContext, accountType); 119 } 120 121 /** 122 * Wrapper for {@code AuthenticatorHelper.getLabelForType}. 123 * Gets the label associated with a particular account type. If none found, return {@code null}. 124 * 125 * @param accountType the type of account 126 * @return a CharSequence for the label or null if one cannot be found. 127 */ getLabelForType(final String accountType)128 public CharSequence getLabelForType(final String accountType) { 129 return mAuthenticatorHelper.getLabelForType(mContext, accountType); 130 } 131 } 132