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