Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.util; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 18 | |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 19 | import android.accounts.Account; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 20 | import android.app.Activity; |
Walter Jang | f2cad22 | 2016-07-14 19:49:06 +0000 | [diff] [blame] | 21 | import android.app.Fragment; |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 22 | import android.content.ActivityNotFoundException; |
Wenyi Wang | b318676 | 2016-04-17 17:29:31 -0700 | [diff] [blame] | 23 | import android.content.AsyncTaskLoader; |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 27 | import android.content.IntentFilter; |
Wenyi Wang | b318676 | 2016-04-17 17:29:31 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 29 | import android.provider.ContactsContract.Contacts; |
| 30 | import android.provider.ContactsContract.Intents; |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 31 | import android.support.v4.content.LocalBroadcastManager; |
Wenyi Wang | cfe9465 | 2016-10-03 21:05:04 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 33 | import android.util.Log; |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 34 | import android.widget.Toast; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 35 | |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 36 | import com.android.contacts.R; |
Gary Mai | 363af60 | 2016-09-28 10:01:23 -0700 | [diff] [blame] | 37 | import com.android.contacts.activities.ContactEditorActivity; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 38 | import com.android.contacts.list.AccountFilterActivity; |
| 39 | import com.android.contacts.list.ContactListFilter; |
| 40 | import com.android.contacts.list.ContactListFilterController; |
| 41 | import com.android.contacts.model.AccountTypeManager; |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 42 | import com.android.contacts.model.Contact; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 43 | import com.android.contacts.model.account.AccountDisplayInfo; |
| 44 | import com.android.contacts.model.account.AccountDisplayInfoFactory; |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 45 | import com.android.contacts.model.account.AccountInfo; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 46 | import com.android.contacts.model.account.AccountType; |
| 47 | import com.android.contacts.model.account.AccountWithDataSet; |
| 48 | import com.android.contacts.preference.ContactsPreferences; |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 49 | import com.android.contacts.util.concurrent.ContactsExecutors; |
| 50 | import com.android.contacts.util.concurrent.ListenableFutureLoader; |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 51 | import com.android.contactsbind.ObjectFactory; |
Gary Mai | ac33359 | 2016-09-28 17:27:40 -0700 | [diff] [blame] | 52 | |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 53 | import com.google.common.base.Function; |
| 54 | import com.google.common.base.Predicate; |
Wenyi Wang | b318676 | 2016-04-17 17:29:31 -0700 | [diff] [blame] | 55 | import com.google.common.collect.Lists; |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 56 | import com.google.common.util.concurrent.Futures; |
| 57 | import com.google.common.util.concurrent.ListenableFuture; |
Wenyi Wang | b318676 | 2016-04-17 17:29:31 -0700 | [diff] [blame] | 58 | |
| 59 | import java.util.ArrayList; |
| 60 | import java.util.List; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 61 | |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 62 | import javax.annotation.Nullable; |
| 63 | |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Utility class for account filter manipulation. |
| 66 | */ |
| 67 | public class AccountFilterUtil { |
| 68 | private static final String TAG = AccountFilterUtil.class.getSimpleName(); |
| 69 | |
Walter Jang | f2cad22 | 2016-07-14 19:49:06 +0000 | [diff] [blame] | 70 | /** |
| 71 | * Launches account filter setting Activity using |
| 72 | * {@link Fragment#startActivityForResult(Intent, int)}. |
| 73 | * |
| 74 | * @param requestCode requestCode for {@link Activity#startActivityForResult(Intent, int)} |
| 75 | * @param currentFilter currently-selected filter, so that it can be displayed as activated. |
| 76 | */ |
| 77 | public static void startAccountFilterActivityForResult( |
| 78 | Fragment fragment, int requestCode, ContactListFilter currentFilter) { |
| 79 | final Activity activity = fragment.getActivity(); |
| 80 | if (activity != null) { |
| 81 | final Intent intent = new Intent(activity, AccountFilterActivity.class); |
| 82 | fragment.startActivityForResult(intent, requestCode); |
| 83 | } else { |
| 84 | Log.w(TAG, "getActivity() returned null. Ignored"); |
| 85 | } |
| 86 | } |
Wenyi Wang | 1aa9d9f | 2016-04-22 16:31:51 -0700 | [diff] [blame] | 87 | |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 88 | /** |
Walter Jang | f2cad22 | 2016-07-14 19:49:06 +0000 | [diff] [blame] | 89 | * Useful method to handle onActivityResult() for |
| 90 | * {@link #startAccountFilterActivityForResult(Fragment, int, ContactListFilter)}. |
| 91 | * |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 92 | * This will update filter via a given ContactListFilterController. |
| 93 | */ |
| 94 | public static void handleAccountFilterResult( |
| 95 | ContactListFilterController filterController, int resultCode, Intent data) { |
| 96 | if (resultCode == Activity.RESULT_OK) { |
| 97 | final ContactListFilter filter = (ContactListFilter) |
Walter Jang | f2cad22 | 2016-07-14 19:49:06 +0000 | [diff] [blame] | 98 | data.getParcelableExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 99 | if (filter == null) { |
| 100 | return; |
| 101 | } |
| 102 | if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) { |
| 103 | filterController.selectCustomFilter(); |
| 104 | } else { |
Wenyi Wang | 1cfe9c6 | 2016-06-01 20:49:13 -0700 | [diff] [blame] | 105 | filterController.setContactListFilter(filter, /* persistent */ |
| 106 | filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
Wenyi Wang | b318676 | 2016-04-17 17:29:31 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Loads a list of contact list filters |
| 113 | */ |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 114 | public static class FilterLoader extends ListenableFutureLoader<List<ContactListFilter>> { |
| 115 | private AccountTypeManager mAccountTypeManager; |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 116 | private DeviceLocalAccountTypeFactory mDeviceLocalFactory; |
Wenyi Wang | b318676 | 2016-04-17 17:29:31 -0700 | [diff] [blame] | 117 | |
| 118 | public FilterLoader(Context context) { |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 119 | super(context, new IntentFilter(AccountTypeManager.BROADCAST_ACCOUNTS_CHANGED)); |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 120 | mAccountTypeManager = AccountTypeManager.getInstance(context); |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 121 | mDeviceLocalFactory = ObjectFactory.getDeviceLocalAccountTypeFactory(context); |
Wenyi Wang | b318676 | 2016-04-17 17:29:31 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 124 | |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 125 | @Override |
| 126 | protected ListenableFuture<List<ContactListFilter>> loadData() { |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 127 | return Futures.transform(mAccountTypeManager.filterAccountsAsync( |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 128 | AccountTypeManager.writableFilter()), |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 129 | new Function<List<AccountInfo>, List<ContactListFilter>>() { |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 130 | @Override |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 131 | public List<ContactListFilter> apply(List<AccountInfo> input) { |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 132 | return getFiltersForAccounts(input); |
| 133 | } |
| 134 | }, ContactsExecutors.getDefaultThreadPoolExecutor()); |
| 135 | } |
| 136 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 137 | private List<ContactListFilter> getFiltersForAccounts(List<AccountInfo> accounts) { |
| 138 | final ArrayList<ContactListFilter> accountFilters = new ArrayList<>(); |
| 139 | AccountInfo.sortAccounts(getDefaultAccount(getContext()), accounts); |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 140 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 141 | for (AccountInfo accountInfo : accounts) { |
| 142 | final AccountType accountType = accountInfo.getType(); |
| 143 | final AccountWithDataSet account = accountInfo.getAccount(); |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 144 | if ((accountType.isExtension() || |
| 145 | DeviceLocalAccountTypeFactory.Util.isLocalAccountType( |
| 146 | mDeviceLocalFactory, account.type)) && |
| 147 | !account.hasData(getContext())) { |
| 148 | // Hide extensions and device accounts with no raw_contacts. |
| 149 | continue; |
| 150 | } |
| 151 | final Drawable icon = accountType != null ? |
| 152 | accountType.getDisplayIcon(getContext()) : null; |
| 153 | if (DeviceLocalAccountTypeFactory.Util.isLocalAccountType( |
| 154 | mDeviceLocalFactory, account.type)) { |
| 155 | accountFilters.add(ContactListFilter.createDeviceContactsFilter(icon, account)); |
| 156 | } else { |
| 157 | accountFilters.add(ContactListFilter.createAccountFilter( |
| 158 | account.type, account.name, account.dataSet, icon)); |
| 159 | } |
| 160 | } |
| 161 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 162 | return accountFilters; |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 163 | } |
Wenyi Wang | b318676 | 2016-04-17 17:29:31 -0700 | [diff] [blame] | 164 | } |
Wenyi Wang | aa0e6ff | 2016-07-06 17:22:42 -0700 | [diff] [blame] | 165 | |
| 166 | private static AccountWithDataSet getDefaultAccount(Context context) { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 167 | return new ContactsPreferences(context).getDefaultAccount(); |
Wenyi Wang | aa0e6ff | 2016-07-06 17:22:42 -0700 | [diff] [blame] | 168 | } |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Returns a {@link ContactListFilter} of type |
| 172 | * {@link ContactListFilter#FILTER_TYPE_ALL_ACCOUNTS}, or if a custom "Contacts to display" |
| 173 | * filter has been set, then one of type {@link ContactListFilter#FILTER_TYPE_CUSTOM}. |
| 174 | */ |
| 175 | public static ContactListFilter createContactsFilter(Context context) { |
| 176 | final int filterType = |
| 177 | ContactListFilterController.getInstance(context).isCustomFilterPersisted() |
| 178 | ? ContactListFilter.FILTER_TYPE_CUSTOM |
| 179 | : ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS; |
| 180 | return ContactListFilter.createFilterWithType(filterType); |
| 181 | } |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Start editor intent; and if filter is an account filter, we pass account info to editor so |
| 185 | * as to create a contact in that account. |
| 186 | */ |
| 187 | public static void startEditorIntent(Context context, Intent src, ContactListFilter filter) { |
| 188 | final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI); |
| 189 | intent.putExtras(src); |
| 190 | |
| 191 | // If we are in account view, we pass the account explicitly in order to |
| 192 | // create contact in the account. This will prevent the default account dialog |
| 193 | // from being displayed. |
Marcus Hagerott | 935b56a | 2016-09-07 11:59:35 -0700 | [diff] [blame] | 194 | if (!isAllContactsFilter(filter) && filter.accountName != null |
| 195 | && filter.accountType != null) { |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 196 | final Account account = new Account(filter.accountName, filter.accountType); |
| 197 | intent.putExtra(Intents.Insert.EXTRA_ACCOUNT, account); |
| 198 | intent.putExtra(Intents.Insert.EXTRA_DATA_SET, filter.dataSet); |
Marcus Hagerott | 935b56a | 2016-09-07 11:59:35 -0700 | [diff] [blame] | 199 | } else if (isDeviceContactsFilter(filter)) { |
| 200 | // It's OK to add this even though it's an implicit intent. If a different app |
| 201 | // receives the intent it should just ignore the flag. |
Gary Mai | 363af60 | 2016-09-28 10:01:23 -0700 | [diff] [blame] | 202 | intent.putExtra(ContactEditorActivity.EXTRA_SAVE_TO_DEVICE_FLAG, true); |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | try { |
| 206 | ImplicitIntentsUtil.startActivityInApp(context, intent); |
| 207 | } catch (ActivityNotFoundException ex) { |
| 208 | Toast.makeText(context, R.string.missing_app, Toast.LENGTH_SHORT).show(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | public static boolean isAllContactsFilter(ContactListFilter filter) { |
| 213 | return filter != null && filter.isContactsFilterType(); |
| 214 | } |
| 215 | |
| 216 | public static boolean isDeviceContactsFilter(ContactListFilter filter) { |
| 217 | return filter.filterType == ContactListFilter.FILTER_TYPE_DEVICE_CONTACTS; |
| 218 | } |
Wenyi Wang | cfe9465 | 2016-10-03 21:05:04 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * Returns action bar title for filter and returns default title "Contacts" if filter is empty. |
| 222 | */ |
| 223 | public static String getActionBarTitleForFilter(Context context, ContactListFilter filter) { |
| 224 | if (filter.filterType == ContactListFilter.FILTER_TYPE_DEVICE_CONTACTS) { |
| 225 | return context.getString(R.string.account_phone); |
Marcus Hagerott | f745113 | 2016-10-20 14:32:16 -0700 | [diff] [blame] | 226 | } else if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT && |
| 227 | !TextUtils.isEmpty(filter.accountName)) { |
Wenyi Wang | cfe9465 | 2016-10-03 21:05:04 -0700 | [diff] [blame] | 228 | return getActionBarTitleForAccount(context, filter); |
| 229 | } |
| 230 | return context.getString(R.string.contactsList); |
| 231 | } |
| 232 | |
| 233 | private static String getActionBarTitleForAccount(Context context, ContactListFilter filter) { |
| 234 | final AccountDisplayInfoFactory factory = |
| 235 | AccountDisplayInfoFactory.forAllAccounts(context); |
| 236 | final AccountDisplayInfo account = factory.getAccountDisplayInfoFor(filter); |
| 237 | if (account.hasGoogleAccountType()) { |
| 238 | return context.getString(R.string.title_from_google); |
| 239 | } |
| 240 | return account.withFormattedName(context, R.string.title_from_other_accounts) |
| 241 | .getNameLabel().toString(); |
| 242 | } |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 243 | } |