Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | package com.android.contacts.common.list; |
| 17 | |
| 18 | import android.content.ContentUris; |
| 19 | import android.content.Context; |
| 20 | import android.content.CursorLoader; |
| 21 | import android.content.SharedPreferences; |
| 22 | import android.database.Cursor; |
| 23 | import android.net.Uri; |
| 24 | import android.net.Uri.Builder; |
| 25 | import android.preference.PreferenceManager; |
| 26 | import android.provider.ContactsContract; |
| 27 | import android.provider.ContactsContract.Contacts; |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 28 | import android.provider.ContactsContract.Data; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 29 | import android.provider.ContactsContract.Directory; |
Yorke Lee | a12a04c | 2014-07-01 10:39:42 -0700 | [diff] [blame] | 30 | import android.provider.ContactsContract.SearchSnippets; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
| 32 | import android.view.View; |
| 33 | |
Walter Jang | fddbb83 | 2016-08-16 13:12:27 -0700 | [diff] [blame] | 34 | import com.android.contacts.common.Experiments; |
Ricky Wai | dffb27d | 2015-12-08 16:58:27 +0000 | [diff] [blame] | 35 | import com.android.contacts.common.compat.ContactsCompat; |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 36 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 37 | import com.android.contacts.common.preference.ContactsPreferences; |
Walter Jang | e837ae3 | 2016-08-15 12:50:37 -0700 | [diff] [blame] | 38 | import com.android.contactsbind.experiments.Flags; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
| 42 | |
| 43 | /** |
| 44 | * A cursor adapter for the {@link ContactsContract.Contacts#CONTENT_TYPE} content type. |
| 45 | */ |
| 46 | public class DefaultContactListAdapter extends ContactListAdapter { |
| 47 | |
Yorke Lee | aed0a9e | 2014-07-08 08:03:56 -0700 | [diff] [blame] | 48 | public static final char SNIPPET_START_MATCH = '['; |
| 49 | public static final char SNIPPET_END_MATCH = ']'; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 50 | |
Walter Jang | 5a8f91c | 2016-03-30 11:21:55 -0700 | [diff] [blame] | 51 | // Contacts contacted within the last 3 days (in seconds) |
| 52 | private static final long LAST_TIME_USED_3_DAYS_SEC = 3L * 24 * 60 * 60; |
| 53 | |
| 54 | // Contacts contacted within the last 7 days (in seconds) |
| 55 | private static final long LAST_TIME_USED_7_DAYS_SEC = 7L * 24 * 60 * 60; |
| 56 | |
| 57 | // Contacts contacted within the last 14 days (in seconds) |
| 58 | private static final long LAST_TIME_USED_14_DAYS_SEC = 14L * 24 * 60 * 60; |
| 59 | |
| 60 | // Contacts contacted within the last 30 days (in seconds) |
| 61 | private static final long LAST_TIME_USED_30_DAYS_SEC = 30L * 24 * 60 * 60; |
| 62 | |
| 63 | private static final String TIME_SINCE_LAST_USED_SEC = |
| 64 | "(strftime('%s', 'now') - " + Contacts.LAST_TIME_CONTACTED + "/1000)"; |
| 65 | |
| 66 | private static final String STREQUENT_SORT = |
| 67 | "(CASE WHEN " + TIME_SINCE_LAST_USED_SEC + " < " + LAST_TIME_USED_3_DAYS_SEC + |
| 68 | " THEN 0 " + |
| 69 | " WHEN " + TIME_SINCE_LAST_USED_SEC + " < " + LAST_TIME_USED_7_DAYS_SEC + |
| 70 | " THEN 1 " + |
| 71 | " WHEN " + TIME_SINCE_LAST_USED_SEC + " < " + LAST_TIME_USED_14_DAYS_SEC + |
| 72 | " THEN 2 " + |
| 73 | " WHEN " + TIME_SINCE_LAST_USED_SEC + " < " + LAST_TIME_USED_30_DAYS_SEC + |
| 74 | " THEN 3 " + |
| 75 | " ELSE 4 END), " + |
| 76 | Contacts.TIMES_CONTACTED + " DESC, " + |
| 77 | Contacts.STARRED + " DESC"; |
Walter Jang | 6474383 | 2016-03-17 21:01:07 -0700 | [diff] [blame] | 78 | |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 79 | public DefaultContactListAdapter(Context context) { |
| 80 | super(context); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void configureLoader(CursorLoader loader, long directoryId) { |
Wenyi Wang | be88bed | 2016-05-13 12:04:14 -0700 | [diff] [blame] | 85 | if (loader instanceof FavoritesAndContactsLoader) { |
| 86 | ((FavoritesAndContactsLoader) loader).setLoadFavorites(shouldIncludeFavorites()); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Walter Jang | 1f82861 | 2016-02-09 16:15:37 -0800 | [diff] [blame] | 89 | String sortOrder = null; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 90 | if (isSearchMode()) { |
| 91 | String query = getQueryString(); |
Walter Jang | 1f82861 | 2016-02-09 16:15:37 -0800 | [diff] [blame] | 92 | if (query == null) query = ""; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 93 | query = query.trim(); |
| 94 | if (TextUtils.isEmpty(query)) { |
| 95 | // Regardless of the directory, we don't want anything returned, |
| 96 | // so let's just send a "nothing" query to the local directory. |
| 97 | loader.setUri(Contacts.CONTENT_URI); |
| 98 | loader.setProjection(getProjection(false)); |
| 99 | loader.setSelection("0"); |
Walter Jang | 08379b1 | 2016-06-14 15:35:28 -0700 | [diff] [blame] | 100 | } else if (isGroupMembersFilter()) { |
| 101 | final ContactListFilter filter = getFilter(); |
| 102 | configureUri(loader, directoryId, filter); |
Walter Jang | 1842e14 | 2016-03-31 09:03:18 -0700 | [diff] [blame] | 103 | // TODO: This is not the normal type to filter URI so we load the non-search |
| 104 | // projection. Consider creating a specific group member adapter to make it more |
| 105 | // clear. |
Walter Jang | 08379b1 | 2016-06-14 15:35:28 -0700 | [diff] [blame] | 106 | loader.setProjection(getProjection(/* forSearch */ false)); |
| 107 | loader.setSelection( |
| 108 | Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?1 OR " + |
| 109 | Contacts.DISPLAY_NAME_ALTERNATIVE + " LIKE ?1"); |
| 110 | final String[] args = new String[1]; |
| 111 | args[0] = query + "%"; |
| 112 | loader.setSelectionArgs(args); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 113 | } else { |
Ricky Wai | dffb27d | 2015-12-08 16:58:27 +0000 | [diff] [blame] | 114 | final Builder builder = ContactsCompat.getContentUri().buildUpon(); |
Walter Jang | 5456440 | 2016-01-18 11:56:19 -0800 | [diff] [blame] | 115 | appendSearchParameters(builder, query, directoryId); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 116 | loader.setUri(builder.build()); |
| 117 | loader.setProjection(getProjection(true)); |
Walter Jang | c462653 | 2016-06-21 14:06:51 -0700 | [diff] [blame] | 118 | sortOrder = STREQUENT_SORT; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 119 | } |
| 120 | } else { |
Walter Jang | 1f82861 | 2016-02-09 16:15:37 -0800 | [diff] [blame] | 121 | final ContactListFilter filter = getFilter(); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 122 | configureUri(loader, directoryId, filter); |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 123 | if (filter != null |
| 124 | && filter.filterType == ContactListFilter.FILTER_TYPE_DEVICE_CONTACTS) { |
| 125 | loader.setProjection(getDataProjectionForContacts(false)); |
| 126 | } else { |
| 127 | loader.setProjection(getProjection(false)); |
| 128 | } |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 129 | configureSelection(loader, directoryId, filter); |
| 130 | } |
| 131 | |
Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 132 | if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) { |
Walter Jang | 1f82861 | 2016-02-09 16:15:37 -0800 | [diff] [blame] | 133 | if (sortOrder == null) { |
| 134 | sortOrder = Contacts.SORT_KEY_PRIMARY; |
| 135 | } else { |
| 136 | sortOrder += ", " + Contacts.SORT_KEY_PRIMARY; |
| 137 | } |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 138 | } else { |
Walter Jang | 1f82861 | 2016-02-09 16:15:37 -0800 | [diff] [blame] | 139 | if (sortOrder == null) { |
| 140 | sortOrder = Contacts.SORT_KEY_ALTERNATIVE; |
| 141 | } else { |
| 142 | sortOrder += ", " + Contacts.SORT_KEY_ALTERNATIVE; |
| 143 | } |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 144 | } |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 145 | loader.setSortOrder(sortOrder); |
| 146 | } |
| 147 | |
Walter Jang | 08379b1 | 2016-06-14 15:35:28 -0700 | [diff] [blame] | 148 | private boolean isGroupMembersFilter() { |
| 149 | final ContactListFilter filter = getFilter(); |
| 150 | return filter != null && filter.filterType == ContactListFilter.FILTER_TYPE_GROUP_MEMBERS; |
| 151 | } |
| 152 | |
Walter Jang | 5456440 | 2016-01-18 11:56:19 -0800 | [diff] [blame] | 153 | private void appendSearchParameters(Builder builder, String query, long directoryId) { |
| 154 | builder.appendPath(query); // Builder will encode the query |
| 155 | builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 156 | String.valueOf(directoryId)); |
| 157 | if (directoryId != Directory.DEFAULT && directoryId != Directory.LOCAL_INVISIBLE) { |
| 158 | builder.appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, |
| 159 | String.valueOf(getDirectoryResultLimit(getDirectoryById(directoryId)))); |
| 160 | } |
Walter Jang | 8f01264 | 2016-02-12 17:43:27 -0800 | [diff] [blame] | 161 | builder.appendQueryParameter(SearchSnippets.DEFERRED_SNIPPETING_KEY, "1"); |
Walter Jang | 5456440 | 2016-01-18 11:56:19 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 164 | protected void configureUri(CursorLoader loader, long directoryId, ContactListFilter filter) { |
| 165 | Uri uri = Contacts.CONTENT_URI; |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 166 | if (filter != null) { |
| 167 | if (filter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) { |
| 168 | String lookupKey = getSelectedContactLookupKey(); |
| 169 | if (lookupKey != null) { |
| 170 | uri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey); |
| 171 | } else { |
| 172 | uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, getSelectedContactId()); |
| 173 | } |
| 174 | } else if (filter.filterType == ContactListFilter.FILTER_TYPE_DEVICE_CONTACTS) { |
| 175 | uri = Data.CONTENT_URI; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | if (directoryId == Directory.DEFAULT && isSectionHeaderDisplayEnabled()) { |
| 180 | uri = ContactListAdapter.buildSectionIndexerUri(uri); |
| 181 | } |
| 182 | |
| 183 | // The "All accounts" filter is the same as the entire contents of Directory.DEFAULT |
| 184 | if (filter != null |
| 185 | && filter.filterType != ContactListFilter.FILTER_TYPE_CUSTOM |
| 186 | && filter.filterType != ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) { |
| 187 | final Uri.Builder builder = uri.buildUpon(); |
| 188 | builder.appendQueryParameter( |
| 189 | ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT)); |
Walter Jang | 08379b1 | 2016-06-14 15:35:28 -0700 | [diff] [blame] | 190 | if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT |
| 191 | || filter.filterType == ContactListFilter.FILTER_TYPE_GROUP_MEMBERS) { |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 192 | filter.addAccountQueryParameterToUrl(builder); |
| 193 | } |
| 194 | uri = builder.build(); |
| 195 | } |
| 196 | |
| 197 | loader.setUri(uri); |
| 198 | } |
| 199 | |
| 200 | private void configureSelection( |
| 201 | CursorLoader loader, long directoryId, ContactListFilter filter) { |
| 202 | if (filter == null) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | if (directoryId != Directory.DEFAULT) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | StringBuilder selection = new StringBuilder(); |
| 211 | List<String> selectionArgs = new ArrayList<String>(); |
| 212 | |
| 213 | switch (filter.filterType) { |
| 214 | case ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS: { |
| 215 | // We have already added directory=0 to the URI, which takes care of this |
| 216 | // filter |
| 217 | break; |
| 218 | } |
| 219 | case ContactListFilter.FILTER_TYPE_SINGLE_CONTACT: { |
| 220 | // We have already added the lookup key to the URI, which takes care of this |
| 221 | // filter |
| 222 | break; |
| 223 | } |
| 224 | case ContactListFilter.FILTER_TYPE_STARRED: { |
| 225 | selection.append(Contacts.STARRED + "!=0"); |
| 226 | break; |
| 227 | } |
| 228 | case ContactListFilter.FILTER_TYPE_WITH_PHONE_NUMBERS_ONLY: { |
| 229 | selection.append(Contacts.HAS_PHONE_NUMBER + "=1"); |
| 230 | break; |
| 231 | } |
| 232 | case ContactListFilter.FILTER_TYPE_CUSTOM: { |
| 233 | selection.append(Contacts.IN_VISIBLE_GROUP + "=1"); |
| 234 | if (isCustomFilterForPhoneNumbersOnly()) { |
| 235 | selection.append(" AND " + Contacts.HAS_PHONE_NUMBER + "=1"); |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | case ContactListFilter.FILTER_TYPE_ACCOUNT: { |
| 240 | // We use query parameters for account filter, so no selection to add here. |
| 241 | break; |
| 242 | } |
Walter Jang | 08379b1 | 2016-06-14 15:35:28 -0700 | [diff] [blame] | 243 | case ContactListFilter.FILTER_TYPE_GROUP_MEMBERS: { |
Walter Jang | 08379b1 | 2016-06-14 15:35:28 -0700 | [diff] [blame] | 244 | break; |
| 245 | } |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 246 | case ContactListFilter.FILTER_TYPE_DEVICE_CONTACTS: { |
Marcus Hagerott | 6caf23f | 2016-08-18 15:02:42 -0700 | [diff] [blame] | 247 | if (filter.accountType != null) { |
| 248 | selection.append(ContactsContract.RawContacts.ACCOUNT_TYPE) |
| 249 | .append("=?"); |
| 250 | selectionArgs.add(filter.accountType); |
| 251 | if (filter.accountName != null) { |
| 252 | selection.append(" AND ").append(ContactsContract.RawContacts.ACCOUNT_NAME) |
| 253 | .append(("=?")); |
| 254 | selectionArgs.add(filter.accountName); |
| 255 | } |
| 256 | } else { |
| 257 | selection.append(AccountWithDataSet.LOCAL_ACCOUNT_SELECTION); |
| 258 | } |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 259 | break; |
| 260 | } |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 261 | } |
| 262 | loader.setSelection(selection.toString()); |
| 263 | loader.setSelectionArgs(selectionArgs.toArray(new String[0])); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | protected void bindView(View itemView, int partition, Cursor cursor, int position) { |
Brian Attwell | a5ad557 | 2014-09-15 11:18:03 -0700 | [diff] [blame] | 268 | super.bindView(itemView, partition, cursor, position); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 269 | final ContactListItemView view = (ContactListItemView)itemView; |
| 270 | |
| 271 | view.setHighlightedPrefix(isSearchMode() ? getUpperCaseQueryString() : null); |
| 272 | |
| 273 | if (isSelectionVisible()) { |
| 274 | view.setActivated(isSelectedContact(partition, cursor)); |
| 275 | } |
| 276 | |
| 277 | bindSectionHeaderAndDivider(view, position, cursor); |
| 278 | |
| 279 | if (isQuickContactEnabled()) { |
| 280 | bindQuickContact(view, partition, cursor, ContactQuery.CONTACT_PHOTO_ID, |
| 281 | ContactQuery.CONTACT_PHOTO_URI, ContactQuery.CONTACT_ID, |
Yorke Lee | 9df5e19 | 2014-02-12 14:58:25 -0800 | [diff] [blame] | 282 | ContactQuery.CONTACT_LOOKUP_KEY, ContactQuery.CONTACT_DISPLAY_NAME); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 283 | } else { |
| 284 | if (getDisplayPhotos()) { |
| 285 | bindPhoto(view, partition, cursor); |
| 286 | } |
| 287 | } |
| 288 | |
Brian Attwell | 2ea151c | 2014-09-03 19:53:26 -0700 | [diff] [blame] | 289 | bindNameAndViewId(view, cursor); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 290 | bindPresenceAndStatusMessage(view, cursor); |
| 291 | |
| 292 | if (isSearchMode()) { |
| 293 | bindSearchSnippet(view, cursor); |
| 294 | } else { |
| 295 | view.setSnippet(null); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private boolean isCustomFilterForPhoneNumbersOnly() { |
| 300 | // TODO: this flag should not be stored in shared prefs. It needs to be in the db. |
| 301 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); |
| 302 | return prefs.getBoolean(ContactsPreferences.PREF_DISPLAY_ONLY_PHONES, |
| 303 | ContactsPreferences.PREF_DISPLAY_ONLY_PHONES_DEFAULT); |
| 304 | } |
| 305 | } |