Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.model; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 18 | |
| 19 | import android.accounts.Account; |
Ihab Awad | 413589f | 2014-07-02 14:00:28 -0700 | [diff] [blame] | 20 | import android.accounts.AccountManager; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 21 | import android.accounts.OnAccountsUpdateListener; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 27 | import android.content.SharedPreferences; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 28 | import android.content.SyncStatusObserver; |
| 29 | import android.content.pm.PackageManager; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 30 | import android.database.ContentObserver; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 31 | import android.net.Uri; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 32 | import android.os.Handler; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 33 | import android.os.Looper; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 34 | import android.provider.ContactsContract; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 35 | import android.support.v4.content.ContextCompat; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 36 | import android.support.v4.content.LocalBroadcastManager; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 37 | import android.text.TextUtils; |
| 38 | import android.util.Log; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 39 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 40 | import com.android.contacts.Experiments; |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 41 | import com.android.contacts.R; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 42 | import com.android.contacts.list.ContactListFilterController; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 43 | import com.android.contacts.model.account.AccountComparator; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 44 | import com.android.contacts.model.account.AccountType; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 45 | import com.android.contacts.model.account.AccountTypeProvider; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 46 | import com.android.contacts.model.account.AccountTypeWithDataSet; |
| 47 | import com.android.contacts.model.account.AccountWithDataSet; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 48 | import com.android.contacts.model.account.FallbackAccountType; |
| 49 | import com.android.contacts.model.account.GoogleAccountType; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 50 | import com.android.contacts.model.dataitem.DataKind; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 51 | import com.android.contacts.util.concurrent.ContactsExecutors; |
Marcus Hagerott | 511504f | 2016-11-15 13:58:34 -0800 | [diff] [blame] | 52 | import com.android.contactsbind.experiments.Flags; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 53 | import com.google.common.base.Function; |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 54 | import com.google.common.base.Predicate; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 55 | import com.google.common.base.Predicates; |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 56 | import com.google.common.collect.Collections2; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 57 | import com.google.common.util.concurrent.FutureCallback; |
| 58 | import com.google.common.util.concurrent.Futures; |
| 59 | import com.google.common.util.concurrent.ListenableFuture; |
| 60 | import com.google.common.util.concurrent.ListeningExecutorService; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 61 | |
Gary Mai | ac33359 | 2016-09-28 17:27:40 -0700 | [diff] [blame] | 62 | import java.util.ArrayList; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 63 | import java.util.Collections; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 64 | import java.util.List; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 65 | import java.util.concurrent.Callable; |
| 66 | import java.util.concurrent.Executor; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 67 | |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 68 | import javax.annotation.Nullable; |
| 69 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Singleton holder for all parsed {@link AccountType} available on the |
| 72 | * system, typically filled through {@link PackageManager} queries. |
| 73 | */ |
| 74 | public abstract class AccountTypeManager { |
| 75 | static final String TAG = "AccountTypeManager"; |
| 76 | |
| 77 | private static final Object mInitializationLock = new Object(); |
| 78 | private static AccountTypeManager mAccountTypeManager; |
| 79 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 80 | public static final String BROADCAST_ACCOUNTS_CHANGED = AccountTypeManager.class.getName() + |
| 81 | ".AccountsChanged"; |
| 82 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Requests the singleton instance of {@link AccountTypeManager} with data bound from |
| 85 | * the available authenticators. This method can safely be called from the UI thread. |
| 86 | */ |
| 87 | public static AccountTypeManager getInstance(Context context) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 88 | if (!hasRequiredPermissions(context)) { |
| 89 | // Hopefully any component that depends on the values returned by this class |
| 90 | // will be restarted if the permissions change. |
| 91 | return EMPTY; |
| 92 | } |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 93 | synchronized (mInitializationLock) { |
| 94 | if (mAccountTypeManager == null) { |
| 95 | context = context.getApplicationContext(); |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 96 | mAccountTypeManager = new AccountTypeManagerImpl(context); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | return mAccountTypeManager; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Set the instance of account type manager. This is only for and should only be used by unit |
| 104 | * tests. While having this method is not ideal, it's simpler than the alternative of |
| 105 | * holding this as a service in the ContactsApplication context class. |
| 106 | * |
| 107 | * @param mockManager The mock AccountTypeManager. |
| 108 | */ |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 109 | public static void setInstanceForTest(AccountTypeManager mockManager) { |
| 110 | synchronized (mInitializationLock) { |
| 111 | mAccountTypeManager = mockManager; |
| 112 | } |
| 113 | } |
| 114 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 115 | private static final AccountTypeManager EMPTY = new AccountTypeManager() { |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 116 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public List<AccountWithDataSet> getAccounts(boolean contactWritableOnly) { |
| 119 | return Collections.emptyList(); |
| 120 | } |
| 121 | |
| 122 | @Override |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 123 | public List<AccountWithDataSet> getAccounts(Predicate<AccountWithDataSet> filter) { |
| 124 | return Collections.emptyList(); |
| 125 | } |
| 126 | |
| 127 | @Override |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 128 | public ListenableFuture<List<AccountWithDataSet>> getAllAccountsAsync() { |
| 129 | return Futures.immediateFuture(Collections.<AccountWithDataSet>emptyList()); |
| 130 | } |
| 131 | |
| 132 | @Override |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 133 | public ListenableFuture<List<AccountWithDataSet>> filterAccountsByTypeAsync( |
| 134 | Predicate<AccountType> type) { |
| 135 | return Futures.immediateFuture(Collections.<AccountWithDataSet>emptyList()); |
| 136 | } |
| 137 | |
| 138 | @Override |
Gary Mai | ac33359 | 2016-09-28 17:27:40 -0700 | [diff] [blame] | 139 | public List<AccountWithDataSet> getGroupWritableAccounts() { |
| 140 | return Collections.emptyList(); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 144 | public Account getDefaultGoogleAccount() { |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | @Override |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 149 | public AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet) { |
| 150 | return null; |
| 151 | } |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Returns the list of all accounts (if contactWritableOnly is false) or just the list of |
| 156 | * contact writable accounts (if contactWritableOnly is true). |
| 157 | */ |
| 158 | // TODO: Consider splitting this into getContactWritableAccounts() and getAllAccounts() |
| 159 | public abstract List<AccountWithDataSet> getAccounts(boolean contactWritableOnly); |
| 160 | |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 161 | public abstract List<AccountWithDataSet> getAccounts(Predicate<AccountWithDataSet> filter); |
| 162 | |
Wenyi Wang | aa0e6ff | 2016-07-06 17:22:42 -0700 | [diff] [blame] | 163 | /** |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 164 | * Loads accounts in background and returns future that will complete with list of all accounts |
| 165 | */ |
| 166 | public abstract ListenableFuture<List<AccountWithDataSet>> getAllAccountsAsync(); |
| 167 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 168 | public abstract ListenableFuture<List<AccountWithDataSet>> filterAccountsByTypeAsync( |
| 169 | Predicate<AccountType> type); |
| 170 | |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 171 | /** |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 172 | * Returns the list of accounts that are group writable. |
| 173 | */ |
| 174 | public abstract List<AccountWithDataSet> getGroupWritableAccounts(); |
| 175 | |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Returns the default google account. |
| 178 | */ |
| 179 | public abstract Account getDefaultGoogleAccount(); |
| 180 | |
Marcus Hagerott | 396aab7 | 2016-12-12 12:00:21 -0800 | [diff] [blame] | 181 | /** |
| 182 | * Returns the Google Accounts. |
| 183 | * |
| 184 | * <p>This method exists in addition to filterAccountsByTypeAsync because it should be safe |
| 185 | * to call synchronously. |
| 186 | * </p> |
| 187 | */ |
| 188 | public List<AccountWithDataSet> getWritableGoogleAccounts() { |
| 189 | // This implementation may block and should be overridden by the Impl class |
| 190 | return Futures.getUnchecked(filterAccountsByTypeAsync(new Predicate<AccountType>() { |
| 191 | @Override |
| 192 | public boolean apply(@Nullable AccountType input) { |
| 193 | return input.areContactsWritable() && |
| 194 | GoogleAccountType.ACCOUNT_TYPE.equals(input.accountType); |
| 195 | |
| 196 | } |
| 197 | })); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns true if there are real accounts (not "local" account) in the list of accounts. |
| 202 | */ |
| 203 | public boolean hasNonLocalAccount() { |
| 204 | final List<AccountWithDataSet> allAccounts = getAccounts(/* contactWritableOnly */ false); |
| 205 | if (allAccounts == null || allAccounts.size() == 0) { |
| 206 | return false; |
| 207 | } |
| 208 | if (allAccounts.size() > 1) { |
| 209 | return true; |
| 210 | } |
| 211 | return !allAccounts.get(0).isNullAccount(); |
| 212 | } |
| 213 | |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 214 | static Account getDefaultGoogleAccount(AccountManager accountManager, |
| 215 | SharedPreferences prefs, String defaultAccountKey) { |
| 216 | // Get all the google accounts on the device |
| 217 | final Account[] accounts = accountManager.getAccountsByType( |
| 218 | GoogleAccountType.ACCOUNT_TYPE); |
| 219 | if (accounts == null || accounts.length == 0) { |
| 220 | return null; |
| 221 | } |
| 222 | |
| 223 | // Get the default account from preferences |
| 224 | final String defaultAccount = prefs.getString(defaultAccountKey, null); |
| 225 | final AccountWithDataSet accountWithDataSet = defaultAccount == null ? null : |
| 226 | AccountWithDataSet.unstringify(defaultAccount); |
| 227 | |
| 228 | // Look for an account matching the one from preferences |
| 229 | if (accountWithDataSet != null) { |
| 230 | for (int i = 0; i < accounts.length; i++) { |
| 231 | if (TextUtils.equals(accountWithDataSet.name, accounts[i].name) |
| 232 | && TextUtils.equals(accountWithDataSet.type, accounts[i].type)) { |
| 233 | return accounts[i]; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Just return the first one |
| 239 | return accounts[0]; |
| 240 | } |
| 241 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 242 | public abstract AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet); |
| 243 | |
| 244 | public final AccountType getAccountType(String accountType, String dataSet) { |
| 245 | return getAccountType(AccountTypeWithDataSet.get(accountType, dataSet)); |
| 246 | } |
| 247 | |
| 248 | public final AccountType getAccountTypeForAccount(AccountWithDataSet account) { |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 249 | if (account != null) { |
| 250 | return getAccountType(account.getAccountTypeWithDataSet()); |
| 251 | } |
| 252 | return getAccountType(null, null); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 256 | * Find the best {@link DataKind} matching the requested |
| 257 | * {@link AccountType#accountType}, {@link AccountType#dataSet}, and {@link DataKind#mimeType}. |
| 258 | * If no direct match found, we try searching {@link FallbackAccountType}. |
| 259 | */ |
| 260 | public DataKind getKindOrFallback(AccountType type, String mimeType) { |
| 261 | return type == null ? null : type.getKindForMimetype(mimeType); |
| 262 | } |
| 263 | |
| 264 | /** |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 265 | * @param contactWritableOnly if true, it only returns ones that support writing contacts. |
| 266 | * @return true when this instance contains the given account. |
| 267 | */ |
| 268 | public boolean contains(AccountWithDataSet account, boolean contactWritableOnly) { |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 269 | for (AccountWithDataSet account_2 : getAccounts(contactWritableOnly)) { |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 270 | if (account.equals(account_2)) { |
| 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | return false; |
| 275 | } |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 276 | |
Marcus Hagerott | 596eb7e | 2016-10-18 10:25:12 -0700 | [diff] [blame] | 277 | public boolean hasGoogleAccount() { |
| 278 | return getDefaultGoogleAccount() != null; |
| 279 | } |
| 280 | |
Marcus Hagerott | f0e140a | 2016-12-05 16:59:48 -0800 | [diff] [blame] | 281 | /** |
| 282 | * Sorts the accounts in-place such that defaultAccount is first in the list and the rest |
| 283 | * of the accounts are ordered in manner that is useful for display purposes |
| 284 | */ |
| 285 | public static void sortAccounts(AccountWithDataSet defaultAccount, |
| 286 | List<AccountWithDataSet> accounts) { |
| 287 | Collections.sort(accounts, new AccountComparator(defaultAccount)); |
| 288 | } |
| 289 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 290 | private static boolean hasRequiredPermissions(Context context) { |
| 291 | final boolean canGetAccounts = ContextCompat.checkSelfPermission(context, |
| 292 | android.Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED; |
| 293 | final boolean canReadContacts = ContextCompat.checkSelfPermission(context, |
| 294 | android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED; |
| 295 | return canGetAccounts && canReadContacts; |
| 296 | } |
| 297 | |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 298 | public static Predicate<AccountWithDataSet> nonNullAccountFilter() { |
| 299 | return new Predicate<AccountWithDataSet>() { |
| 300 | @Override |
| 301 | public boolean apply(@Nullable AccountWithDataSet account) { |
| 302 | return account != null && account.name != null && account.type != null; |
| 303 | } |
| 304 | }; |
| 305 | } |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 306 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 307 | public static Predicate<AccountWithDataSet> adaptTypeFilter( |
| 308 | final Predicate<AccountType> typeFilter, final AccountTypeProvider provider) { |
| 309 | return new Predicate<AccountWithDataSet>() { |
| 310 | @Override |
| 311 | public boolean apply(@Nullable AccountWithDataSet input) { |
| 312 | return typeFilter.apply(provider.getTypeForAccount(input)); |
| 313 | } |
| 314 | }; |
| 315 | } |
| 316 | |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 317 | public static Predicate<AccountType> writableFilter() { |
| 318 | return new Predicate<AccountType>() { |
| 319 | @Override |
| 320 | public boolean apply(@Nullable AccountType account) { |
| 321 | return account.areContactsWritable(); |
Wenyi Wang | aa0e6ff | 2016-07-06 17:22:42 -0700 | [diff] [blame] | 322 | } |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 323 | }; |
Wenyi Wang | aa0e6ff | 2016-07-06 17:22:42 -0700 | [diff] [blame] | 324 | } |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 325 | |
| 326 | public static Predicate<AccountType> groupWritableFilter() { |
| 327 | return new Predicate<AccountType>() { |
| 328 | @Override |
| 329 | public boolean apply(@Nullable AccountType account) { |
| 330 | return account.isGroupMembershipEditable(); |
| 331 | } |
| 332 | }; |
| 333 | } |
Wenyi Wang | aa0e6ff | 2016-07-06 17:22:42 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 336 | class AccountTypeManagerImpl extends AccountTypeManager |
| 337 | implements OnAccountsUpdateListener, SyncStatusObserver { |
| 338 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 339 | private Context mContext; |
Ihab Awad | 413589f | 2014-07-02 14:00:28 -0700 | [diff] [blame] | 340 | private AccountManager mAccountManager; |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 341 | private DeviceLocalAccountLocator mLocalAccountLocator; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 342 | private AccountTypeProvider mTypeProvider; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 343 | private ListeningExecutorService mExecutor; |
| 344 | private Executor mMainThreadExecutor; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 345 | |
| 346 | private AccountType mFallbackAccountType; |
| 347 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 348 | private ListenableFuture<List<AccountWithDataSet>> mLocalAccountsFuture; |
| 349 | private ListenableFuture<AccountTypeProvider> mAccountTypesFuture; |
| 350 | |
| 351 | private FutureCallback<Object> mAccountsUpdateCallback = new FutureCallback<Object>() { |
| 352 | @Override |
| 353 | public void onSuccess(@Nullable Object result) { |
| 354 | onAccountsUpdatedInternal(); |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public void onFailure(Throwable t) { |
| 359 | } |
| 360 | }; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 361 | |
| 362 | private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 363 | |
| 364 | private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 365 | @Override |
| 366 | public void onReceive(Context context, Intent intent) { |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 367 | reloadAccountTypes(); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 368 | } |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 369 | }; |
| 370 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 371 | /** |
| 372 | * Internal constructor that only performs initial parsing. |
| 373 | */ |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 374 | public AccountTypeManagerImpl(Context context) { |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 375 | mContext = context; |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 376 | mLocalAccountLocator = DeviceLocalAccountLocator.create(context); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 377 | mTypeProvider = new AccountTypeProvider(context); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 378 | mFallbackAccountType = new FallbackAccountType(context); |
| 379 | |
Ihab Awad | 413589f | 2014-07-02 14:00:28 -0700 | [diff] [blame] | 380 | mAccountManager = AccountManager.get(mContext); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 381 | |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 382 | mExecutor = ContactsExecutors.getDefaultThreadPoolExecutor(); |
| 383 | mMainThreadExecutor = ContactsExecutors.newHandlerExecutor(mMainThreadHandler); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 384 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 385 | // Request updates when packages or accounts change |
| 386 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 387 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 388 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 389 | filter.addDataScheme("package"); |
| 390 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| 391 | IntentFilter sdFilter = new IntentFilter(); |
| 392 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 393 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| 394 | mContext.registerReceiver(mBroadcastReceiver, sdFilter); |
| 395 | |
| 396 | // Request updates when locale is changed so that the order of each field will |
| 397 | // be able to be changed on the locale change. |
| 398 | filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 399 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| 400 | |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 401 | mAccountManager.addOnAccountsUpdatedListener(this, mMainThreadHandler, false); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 402 | |
| 403 | ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, this); |
| 404 | |
Marcus Hagerott | 511504f | 2016-11-15 13:58:34 -0800 | [diff] [blame] | 405 | if (Flags.getInstance().getBoolean(Experiments.OEM_CP2_DEVICE_ACCOUNT_DETECTION_ENABLED)) { |
| 406 | // Observe changes to RAW_CONTACTS so that we will update the list of "Device" accounts |
| 407 | // if a new device contact is added. |
| 408 | mContext.getContentResolver().registerContentObserver( |
| 409 | ContactsContract.RawContacts.CONTENT_URI, /* notifyDescendents */ true, |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 410 | new ContentObserver(mMainThreadHandler) { |
Marcus Hagerott | 511504f | 2016-11-15 13:58:34 -0800 | [diff] [blame] | 411 | @Override |
| 412 | public boolean deliverSelfNotifications() { |
| 413 | return true; |
| 414 | } |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 415 | |
Marcus Hagerott | 511504f | 2016-11-15 13:58:34 -0800 | [diff] [blame] | 416 | @Override |
| 417 | public void onChange(boolean selfChange) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 418 | reloadLocalAccounts(); |
Marcus Hagerott | 511504f | 2016-11-15 13:58:34 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void onChange(boolean selfChange, Uri uri) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 423 | reloadLocalAccounts(); |
Marcus Hagerott | 511504f | 2016-11-15 13:58:34 -0800 | [diff] [blame] | 424 | } |
| 425 | }); |
| 426 | } |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 427 | loadAccountTypes(); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void onStatusChanged(int which) { |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 432 | reloadAccountTypes(); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* This notification will arrive on the background thread */ |
| 436 | public void onAccountsUpdated(Account[] accounts) { |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 437 | onAccountsUpdatedInternal(); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 440 | private void onAccountsUpdatedInternal() { |
| 441 | ContactListFilterController.getInstance(mContext).checkFilterValidity(true); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 442 | LocalBroadcastManager.getInstance(mContext).sendBroadcast( |
| 443 | new Intent(BROADCAST_ACCOUNTS_CHANGED)); |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 444 | } |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 445 | |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 446 | private synchronized void startLoadingIfNeeded() { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 447 | if (mTypeProvider == null && mAccountTypesFuture == null) { |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 448 | reloadAccountTypes(); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 449 | } |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 450 | if (mLocalAccountsFuture == null) { |
| 451 | reloadLocalAccounts(); |
| 452 | } |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 455 | private void loadAccountTypes() { |
| 456 | mTypeProvider = new AccountTypeProvider(mContext); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 457 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 458 | mAccountTypesFuture = mExecutor.submit(new Callable<AccountTypeProvider>() { |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 459 | @Override |
| 460 | public AccountTypeProvider call() throws Exception { |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 461 | // This will request the AccountType for each Account |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 462 | getAccountsFromProvider(mTypeProvider); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 463 | return mTypeProvider; |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 464 | } |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 465 | }); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 468 | private synchronized void reloadAccountTypes() { |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 469 | loadAccountTypes(); |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 470 | Futures.addCallback(mAccountTypesFuture, mAccountsUpdateCallback, mMainThreadExecutor); |
| 471 | } |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 472 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 473 | private synchronized void loadLocalAccounts() { |
| 474 | mLocalAccountsFuture = mExecutor.submit(new Callable<List<AccountWithDataSet>>() { |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 475 | @Override |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 476 | public List<AccountWithDataSet> call() throws Exception { |
| 477 | return mLocalAccountLocator.getDeviceLocalAccounts(); |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 478 | } |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 479 | }); |
| 480 | } |
| 481 | |
| 482 | private void reloadLocalAccounts() { |
| 483 | loadLocalAccounts(); |
| 484 | Futures.addCallback(mLocalAccountsFuture, mAccountsUpdateCallback, mMainThreadExecutor); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /** |
Gary Mai | ac33359 | 2016-09-28 17:27:40 -0700 | [diff] [blame] | 488 | * Return list of all known or contact writable {@link AccountWithDataSet}'s. |
| 489 | * {@param contactWritableOnly} whether to restrict to contact writable accounts only |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 490 | */ |
| 491 | @Override |
| 492 | public List<AccountWithDataSet> getAccounts(boolean contactWritableOnly) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 493 | final Predicate<AccountType> filter = contactWritableOnly ? |
| 494 | writableFilter() : Predicates.<AccountType>alwaysTrue(); |
| 495 | // TODO: Shouldn't have a synchronous version for getting all accounts |
| 496 | return Futures.getUnchecked(filterAccountsByTypeAsync(filter)); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 499 | @Override |
| 500 | public List<AccountWithDataSet> getAccounts(Predicate<AccountWithDataSet> filter) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 501 | // TODO: Shouldn't have a synchronous version for getting all accounts |
| 502 | return Futures.getUnchecked(filterAccountsAsync(filter)); |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 505 | @Override |
| 506 | public ListenableFuture<List<AccountWithDataSet>> getAllAccountsAsync() { |
| 507 | startLoadingIfNeeded(); |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 508 | return filterAccountsAsync(Predicates.<AccountWithDataSet>alwaysTrue()); |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 511 | @Override |
| 512 | public ListenableFuture<List<AccountWithDataSet>> filterAccountsByTypeAsync( |
| 513 | final Predicate<AccountType> typeFilter) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 514 | // Ensure that mTypeProvider is initialized so that the reference will be the same |
| 515 | // here as in the call to filterAccountsAsync |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 516 | startLoadingIfNeeded(); |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 517 | return filterAccountsAsync(adaptTypeFilter(typeFilter, mTypeProvider)); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 520 | private ListenableFuture<List<AccountWithDataSet>> filterAccountsAsync( |
| 521 | final Predicate<AccountWithDataSet> filter) { |
| 522 | startLoadingIfNeeded(); |
| 523 | final ListenableFuture<List<AccountWithDataSet>> accountsFromTypes = |
| 524 | Futures.transform(Futures.nonCancellationPropagating(mAccountTypesFuture), |
| 525 | new Function<AccountTypeProvider, List<AccountWithDataSet>>() { |
| 526 | @Override |
| 527 | public List<AccountWithDataSet> apply(AccountTypeProvider provider) { |
| 528 | return getAccountsFromProvider(provider); |
| 529 | } |
| 530 | }); |
| 531 | |
| 532 | final ListenableFuture<List<List<AccountWithDataSet>>> all = |
| 533 | Futures.successfulAsList(accountsFromTypes, mLocalAccountsFuture); |
| 534 | |
| 535 | return Futures.transform(all, new Function<List<List<AccountWithDataSet>>, |
| 536 | List<AccountWithDataSet>>() { |
| 537 | @Nullable |
| 538 | @Override |
| 539 | public List<AccountWithDataSet> apply(@Nullable List<List<AccountWithDataSet>> input) { |
| 540 | // The first result list is from the account types. Check if there is a Google |
| 541 | // account in this list and if there is exclude the null account |
| 542 | final Predicate<AccountWithDataSet> appliedFilter = |
| 543 | hasWritableGoogleAccount(input.get(0)) ? |
| 544 | Predicates.and(nonNullAccountFilter(), filter) : |
| 545 | filter; |
| 546 | List<AccountWithDataSet> result = new ArrayList<>(); |
| 547 | for (List<AccountWithDataSet> list : input) { |
| 548 | if (list != null) { |
| 549 | result.addAll(Collections2.filter(list, appliedFilter)); |
| 550 | } |
| 551 | } |
| 552 | return result; |
| 553 | } |
| 554 | }); |
| 555 | } |
| 556 | |
| 557 | private List<AccountWithDataSet> getAccountsFromProvider(AccountTypeProvider cache) { |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 558 | final List<AccountWithDataSet> result = new ArrayList<>(); |
| 559 | final Account[] accounts = mAccountManager.getAccounts(); |
| 560 | for (Account account : accounts) { |
| 561 | final List<AccountType> types = cache.getAccountTypes(account.type); |
| 562 | for (AccountType type : types) { |
| 563 | result.add(new AccountWithDataSet(account.name, account.type, type.dataSet)); |
| 564 | } |
| 565 | } |
| 566 | return result; |
| 567 | } |
| 568 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 569 | private boolean hasWritableGoogleAccount(List<AccountWithDataSet> accounts) { |
| 570 | if (accounts == null) { |
| 571 | return false; |
| 572 | } |
| 573 | AccountType type; |
| 574 | for (AccountWithDataSet account : accounts) { |
| 575 | if (GoogleAccountType.ACCOUNT_TYPE.equals(account.type) && account.dataSet == null) { |
| 576 | return true; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 577 | } |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 578 | } |
| 579 | return false; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 583 | /** |
| 584 | * Return the list of all known, group writable {@link AccountWithDataSet}'s. |
| 585 | */ |
| 586 | public List<AccountWithDataSet> getGroupWritableAccounts() { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 587 | return Futures.getUnchecked(filterAccountsByTypeAsync(groupWritableFilter())); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /** |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 591 | * Returns the default google account specified in preferences, the first google account |
| 592 | * if it is not specified in preferences or is no longer on the device, and null otherwise. |
| 593 | */ |
| 594 | @Override |
| 595 | public Account getDefaultGoogleAccount() { |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 596 | final SharedPreferences sharedPreferences = |
| 597 | mContext.getSharedPreferences(mContext.getPackageName(), Context.MODE_PRIVATE); |
| 598 | final String defaultAccountKey = |
| 599 | mContext.getResources().getString(R.string.contact_editor_default_account_key); |
Marcus Hagerott | 396aab7 | 2016-12-12 12:00:21 -0800 | [diff] [blame] | 600 | return getDefaultGoogleAccount(mAccountManager, sharedPreferences, defaultAccountKey); |
| 601 | } |
| 602 | |
| 603 | @Override |
| 604 | public List<AccountWithDataSet> getWritableGoogleAccounts() { |
| 605 | final Account[] googleAccounts = |
| 606 | mAccountManager.getAccountsByType(GoogleAccountType.ACCOUNT_TYPE); |
| 607 | final List<AccountWithDataSet> result = new ArrayList<>(); |
| 608 | for (Account account : googleAccounts) { |
| 609 | // Accounts with a dataSet (e.g. Google plus accounts) are not writable. |
| 610 | result.add(new AccountWithDataSet(account.name, account.type, null)); |
| 611 | } |
| 612 | return result; |
| 613 | } |
| 614 | |
| 615 | @Override |
| 616 | public boolean hasNonLocalAccount() { |
| 617 | final Account[] accounts = mAccountManager.getAccounts(); |
| 618 | return accounts != null && accounts.length > 0; |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /** |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 622 | * Find the best {@link DataKind} matching the requested |
| 623 | * {@link AccountType#accountType}, {@link AccountType#dataSet}, and {@link DataKind#mimeType}. |
| 624 | * If no direct match found, we try searching {@link FallbackAccountType}. |
| 625 | */ |
| 626 | @Override |
| 627 | public DataKind getKindOrFallback(AccountType type, String mimeType) { |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 628 | DataKind kind = null; |
| 629 | |
| 630 | // Try finding account type and kind matching request |
| 631 | if (type != null) { |
| 632 | kind = type.getKindForMimetype(mimeType); |
| 633 | } |
| 634 | |
| 635 | if (kind == null) { |
| 636 | // Nothing found, so try fallback as last resort |
| 637 | kind = mFallbackAccountType.getKindForMimetype(mimeType); |
| 638 | } |
| 639 | |
| 640 | if (kind == null) { |
Chiao Cheng | 5df7376 | 2012-12-12 11:28:35 -0800 | [diff] [blame] | 641 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 642 | Log.d(TAG, "Unknown type=" + type + ", mime=" + mimeType); |
| 643 | } |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | return kind; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Return {@link AccountType} for the given account type and data set. |
| 651 | */ |
| 652 | @Override |
| 653 | public AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet) { |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 654 | return mTypeProvider.getType( |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame] | 655 | accountTypeWithDataSet.accountType, accountTypeWithDataSet.dataSet); |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 656 | } |
Chiao Cheng | 6c712f4 | 2012-11-26 15:35:28 -0800 | [diff] [blame] | 657 | } |