Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -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.list; |
| 17 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 18 | import android.app.Activity; |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 19 | import android.content.ContentResolver; |
Dmitri Plotnikov | 7d40224 | 2011-01-09 16:52:08 -0800 | [diff] [blame] | 20 | import android.content.ContentUris; |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 21 | import android.content.Loader; |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 22 | import android.content.SharedPreferences; |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 23 | import android.content.SharedPreferences.Editor; |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 24 | import android.database.Cursor; |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 26 | import android.os.AsyncTask; |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 27 | import android.os.Bundle; |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 28 | import android.os.Handler; |
| 29 | import android.os.Message; |
| 30 | import android.preference.PreferenceManager; |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 31 | import android.provider.ContactsContract; |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 32 | import android.provider.ContactsContract.Contacts; |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 33 | import android.provider.ContactsContract.Directory; |
| 34 | import android.text.TextUtils; |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 35 | import android.util.Log; |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 36 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 37 | import com.android.common.widget.CompositeCursorAdapter.Partition; |
Chiao Cheng | 50b7bd3 | 2012-11-30 11:11:59 -0800 | [diff] [blame] | 38 | import com.android.contacts.common.list.AutoScrollListView; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 39 | import com.android.contacts.common.list.ContactListAdapter; |
| 40 | import com.android.contacts.common.list.ContactListFilter; |
| 41 | import com.android.contacts.common.list.DirectoryPartition; |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 42 | import com.android.contacts.common.logging.ListEvent.ListType; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 43 | import com.android.contacts.common.util.ContactLoaderUtils; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 44 | |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 45 | import java.util.List; |
| 46 | |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 47 | /** |
Dmitri Plotnikov | 416b569 | 2010-04-27 18:14:58 -0700 | [diff] [blame] | 48 | * Fragment containing a contact list used for browsing (as compared to |
| 49 | * picking a contact with one of the PICK intents). |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 50 | */ |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 51 | public abstract class ContactBrowseListFragment extends |
Walter Jang | e9ea4f0 | 2016-05-10 09:39:46 -0700 | [diff] [blame] | 52 | MultiSelectContactsListFragment<ContactListAdapter> { |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 53 | |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 54 | private static final String TAG = "ContactList"; |
| 55 | |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 56 | private static final String KEY_SELECTED_URI = "selectedUri"; |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 57 | private static final String KEY_SELECTION_VERIFIED = "selectionVerified"; |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 58 | private static final String KEY_FILTER = "filter"; |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 59 | private static final String KEY_LAST_SELECTED_POSITION = "lastSelected"; |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 60 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 61 | private static final String PERSISTENT_SELECTION_PREFIX = "defaultContactBrowserSelection"; |
| 62 | |
| 63 | /** |
| 64 | * The id for a delayed message that triggers automatic selection of the first |
| 65 | * found contact in search mode. |
| 66 | */ |
| 67 | private static final int MESSAGE_AUTOSELECT_FIRST_FOUND_CONTACT = 1; |
| 68 | |
| 69 | /** |
| 70 | * The delay that is used for automatically selecting the first found contact. |
| 71 | */ |
| 72 | private static final int DELAY_AUTOSELECT_FIRST_FOUND_CONTACT_MILLIS = 500; |
| 73 | |
| 74 | /** |
| 75 | * The minimum number of characters in the search query that is required |
| 76 | * before we automatically select the first found contact. |
| 77 | */ |
| 78 | private static final int AUTOSELECT_FIRST_FOUND_CONTACT_MIN_QUERY_LENGTH = 2; |
| 79 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 80 | private SharedPreferences mPrefs; |
| 81 | private Handler mHandler; |
Dmitri Plotnikov | 3c69e21 | 2010-09-27 11:43:39 -0700 | [diff] [blame] | 82 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 83 | private boolean mStartedLoading; |
| 84 | private boolean mSelectionRequired; |
| 85 | private boolean mSelectionToScreenRequested; |
| 86 | private boolean mSmoothScrollRequested; |
| 87 | private boolean mSelectionPersistenceRequested; |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 88 | private Uri mSelectedContactUri; |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 89 | private long mSelectedContactDirectoryId; |
| 90 | private String mSelectedContactLookupKey; |
Dmitri Plotnikov | 7d40224 | 2011-01-09 16:52:08 -0800 | [diff] [blame] | 91 | private long mSelectedContactId; |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 92 | private boolean mSelectionVerified; |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 93 | private int mLastSelectedPosition = -1; |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 94 | private boolean mRefreshingContactUri; |
yaolu | 9d99859 | 2016-10-14 23:21:00 -0700 | [diff] [blame] | 95 | private ContactListFilter mFilter; |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 96 | private String mPersistentSelectionPrefix = PERSISTENT_SELECTION_PREFIX; |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 97 | |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 98 | protected OnContactBrowserActionListener mListener; |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 99 | private ContactLookupTask mContactLookupTask; |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 100 | |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 101 | private final class ContactLookupTask extends AsyncTask<Void, Void, Uri> { |
Dmitri Plotnikov | 5ffd929 | 2010-07-12 17:04:20 -0700 | [diff] [blame] | 102 | |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 103 | private final Uri mUri; |
| 104 | private boolean mIsCancelled; |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 105 | |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 106 | public ContactLookupTask(Uri uri) { |
| 107 | mUri = uri; |
Dmitri Plotnikov | 5ffd929 | 2010-07-12 17:04:20 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 111 | protected Uri doInBackground(Void... args) { |
| 112 | Cursor cursor = null; |
| 113 | try { |
| 114 | final ContentResolver resolver = getContext().getContentResolver(); |
| 115 | final Uri uriCurrentFormat = ContactLoaderUtils.ensureIsContactUri(resolver, mUri); |
| 116 | cursor = resolver.query(uriCurrentFormat, |
| 117 | new String[] { Contacts._ID, Contacts.LOOKUP_KEY }, null, null, null); |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 118 | |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 119 | if (cursor != null && cursor.moveToFirst()) { |
| 120 | final long contactId = cursor.getLong(0); |
| 121 | final String lookupKey = cursor.getString(1); |
| 122 | if (contactId != 0 && !TextUtils.isEmpty(lookupKey)) { |
| 123 | return Contacts.getLookupUri(contactId, lookupKey); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | Log.e(TAG, "Error: No contact ID or lookup key for contact " + mUri); |
| 128 | return null; |
Jay Shrauner | 02ecc3f | 2014-12-12 11:29:16 -0800 | [diff] [blame] | 129 | } catch (Exception e) { |
| 130 | Log.e(TAG, "Error loading the contact: " + mUri, e); |
| 131 | return null; |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 132 | } finally { |
| 133 | if (cursor != null) { |
| 134 | cursor.close(); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public void cancel() { |
| 140 | super.cancel(true); |
| 141 | // Use a flag to keep track of whether the {@link AsyncTask} was cancelled or not in |
| 142 | // order to ensure onPostExecute() is not executed after the cancel request. The flag is |
| 143 | // necessary because {@link AsyncTask} still calls onPostExecute() if the cancel request |
| 144 | // came after the worker thread was finished. |
| 145 | mIsCancelled = true; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | protected void onPostExecute(Uri uri) { |
| 150 | // Make sure the {@link Fragment} is at least still attached to the {@link Activity} |
Katherine Kuan | db90ba1 | 2011-11-10 20:57:26 -0800 | [diff] [blame] | 151 | // before continuing. Null URIs should still be allowed so that the list can be |
| 152 | // refreshed and a default contact can be selected (i.e. the case of deleted |
| 153 | // contacts). |
| 154 | if (mIsCancelled || !isAdded()) { |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 155 | return; |
| 156 | } |
Katherine Kuan | db90ba1 | 2011-11-10 20:57:26 -0800 | [diff] [blame] | 157 | onContactUriQueryFinished(uri); |
Dmitri Plotnikov | 5ffd929 | 2010-07-12 17:04:20 -0700 | [diff] [blame] | 158 | } |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Dmitri Plotnikov | 5251884 | 2011-01-19 10:39:58 -0800 | [diff] [blame] | 161 | private boolean mDelaySelection; |
| 162 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 163 | private Handler getHandler() { |
| 164 | if (mHandler == null) { |
| 165 | mHandler = new Handler() { |
| 166 | @Override |
| 167 | public void handleMessage(Message msg) { |
| 168 | switch (msg.what) { |
| 169 | case MESSAGE_AUTOSELECT_FIRST_FOUND_CONTACT: |
| 170 | selectDefaultContact(); |
| 171 | break; |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | }; |
| 175 | } |
| 176 | return mHandler; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void onAttach(Activity activity) { |
| 181 | super.onAttach(activity); |
| 182 | mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 183 | restoreFilter(); |
| 184 | restoreSelectedUri(false); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Dmitri Plotnikov | fa49a66 | 2011-01-13 11:04:56 -0800 | [diff] [blame] | 187 | @Override |
Makoto Onuki | 1db00f6 | 2011-10-19 12:40:12 -0700 | [diff] [blame] | 188 | protected void setSearchMode(boolean flag) { |
Dmitri Plotnikov | fa49a66 | 2011-01-13 11:04:56 -0800 | [diff] [blame] | 189 | if (isSearchMode() != flag) { |
| 190 | if (!flag) { |
| 191 | restoreSelectedUri(true); |
| 192 | } |
| 193 | super.setSearchMode(flag); |
| 194 | } |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Wenyi Wang | df3e849 | 2016-09-09 11:10:07 -0700 | [diff] [blame] | 197 | public void updateListFilter(ContactListFilter filter, boolean restoreSelectedUri) { |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 198 | if (mFilter == null && filter == null) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | if (mFilter != null && mFilter.equals(filter)) { |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 203 | setLogListEvents(false); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | |
Dmitri Plotnikov | 8daa179 | 2010-12-20 17:50:57 -0800 | [diff] [blame] | 207 | Log.v(TAG, "New filter: " + filter); |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 208 | |
Wenyi Wang | df3e849 | 2016-09-09 11:10:07 -0700 | [diff] [blame] | 209 | setListType(filter.toListType()); |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 210 | setLogListEvents(true); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 211 | mFilter = filter; |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 212 | mLastSelectedPosition = -1; |
Wenyi Wang | 40e7910 | 2016-06-01 20:43:56 -0700 | [diff] [blame] | 213 | |
Dmitri Plotnikov | a5a2744 | 2010-12-27 21:06:38 -0800 | [diff] [blame] | 214 | if (restoreSelectedUri) { |
| 215 | mSelectedContactUri = null; |
| 216 | restoreSelectedUri(true); |
| 217 | } |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 218 | reloadData(); |
| 219 | } |
| 220 | |
| 221 | public ContactListFilter getFilter() { |
| 222 | return mFilter; |
| 223 | } |
| 224 | |
Dmitri Plotnikov | 68e32a3 | 2010-05-06 16:15:14 -0700 | [diff] [blame] | 225 | @Override |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 226 | public void restoreSavedState(Bundle savedState) { |
| 227 | super.restoreSavedState(savedState); |
| 228 | |
| 229 | if (savedState == null) { |
| 230 | return; |
| 231 | } |
| 232 | |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 233 | mFilter = savedState.getParcelable(KEY_FILTER); |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 234 | mSelectedContactUri = savedState.getParcelable(KEY_SELECTED_URI); |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 235 | mSelectionVerified = savedState.getBoolean(KEY_SELECTION_VERIFIED); |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 236 | mLastSelectedPosition = savedState.getInt(KEY_LAST_SELECTED_POSITION); |
Dmitri Plotnikov | 580286b | 2010-08-14 18:06:18 -0700 | [diff] [blame] | 237 | parseSelectedContactUri(); |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void onSaveInstanceState(Bundle outState) { |
| 242 | super.onSaveInstanceState(outState); |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 243 | outState.putParcelable(KEY_FILTER, mFilter); |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 244 | outState.putParcelable(KEY_SELECTED_URI, mSelectedContactUri); |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 245 | outState.putBoolean(KEY_SELECTION_VERIFIED, mSelectionVerified); |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 246 | outState.putInt(KEY_LAST_SELECTED_POSITION, mLastSelectedPosition); |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 249 | protected void refreshSelectedContactUri() { |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 250 | if (mContactLookupTask != null) { |
| 251 | mContactLookupTask.cancel(); |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 252 | } |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 253 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 254 | if (!isSelectionVisible()) { |
| 255 | return; |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 256 | } |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 257 | |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 258 | mRefreshingContactUri = true; |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 259 | |
| 260 | if (mSelectedContactUri == null) { |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 261 | onContactUriQueryFinished(null); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (mSelectedContactDirectoryId != Directory.DEFAULT |
| 266 | && mSelectedContactDirectoryId != Directory.LOCAL_INVISIBLE) { |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 267 | onContactUriQueryFinished(mSelectedContactUri); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 268 | } else { |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 269 | mContactLookupTask = new ContactLookupTask(mSelectedContactUri); |
| 270 | mContactLookupTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 271 | } |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 274 | protected void onContactUriQueryFinished(Uri uri) { |
| 275 | mRefreshingContactUri = false; |
| 276 | mSelectedContactUri = uri; |
| 277 | parseSelectedContactUri(); |
| 278 | checkSelection(); |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 281 | public Uri getSelectedContactUri() { |
| 282 | return mSelectedContactUri; |
| 283 | } |
| 284 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 285 | /** |
| 286 | * Sets the new selection for the list. |
| 287 | */ |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 288 | public void setSelectedContactUri(Uri uri) { |
Katherine Kuan | 0b3af92 | 2011-12-28 14:35:21 -0800 | [diff] [blame] | 289 | setSelectedContactUri(uri, true, false /* no smooth scroll */, true, false); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Dmitri Plotnikov | 5251884 | 2011-01-19 10:39:58 -0800 | [diff] [blame] | 292 | @Override |
| 293 | public void setQueryString(String queryString, boolean delaySelection) { |
| 294 | mDelaySelection = delaySelection; |
| 295 | super.setQueryString(queryString, delaySelection); |
| 296 | } |
| 297 | |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 298 | /** |
Katherine Kuan | e1cabcd | 2011-11-11 20:40:20 -0800 | [diff] [blame] | 299 | * Sets whether or not a contact selection must be made. |
| 300 | * @param required if true, we need to check if the selection is present in |
| 301 | * the list and if not notify the listener so that it can load a |
| 302 | * different list. |
| 303 | * TODO: Figure out how to reconcile this with {@link #setSelectedContactUri}, |
| 304 | * without causing unnecessary loading of the list if the selected contact URI is |
| 305 | * the same as before. |
| 306 | */ |
| 307 | public void setSelectionRequired(boolean required) { |
| 308 | mSelectionRequired = required; |
| 309 | } |
| 310 | |
| 311 | /** |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 312 | * Sets the new contact selection. |
| 313 | * |
| 314 | * @param uri the new selection |
| 315 | * @param required if true, we need to check if the selection is present in |
| 316 | * the list and if not notify the listener so that it can load a |
| 317 | * different list |
| 318 | * @param smoothScroll if true, the UI will roll smoothly to the new |
| 319 | * selection |
| 320 | * @param persistent if true, the selection will be stored in shared |
| 321 | * preferences. |
| 322 | * @param willReloadData if true, the selection will be remembered but not |
| 323 | * actually shown, because we are expecting that the data will be |
| 324 | * reloaded momentarily |
| 325 | */ |
| 326 | private void setSelectedContactUri(Uri uri, boolean required, boolean smoothScroll, |
| 327 | boolean persistent, boolean willReloadData) { |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 328 | mSmoothScrollRequested = smoothScroll; |
| 329 | mSelectionToScreenRequested = true; |
| 330 | |
Dmitri Plotnikov | 174fdc2 | 2010-10-29 13:55:01 -0700 | [diff] [blame] | 331 | if ((mSelectedContactUri == null && uri != null) |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 332 | || (mSelectedContactUri != null && !mSelectedContactUri.equals(uri))) { |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 333 | mSelectionVerified = false; |
| 334 | mSelectionRequired = required; |
| 335 | mSelectionPersistenceRequested = persistent; |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 336 | mSelectedContactUri = uri; |
Dmitri Plotnikov | 580286b | 2010-08-14 18:06:18 -0700 | [diff] [blame] | 337 | parseSelectedContactUri(); |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 338 | |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 339 | if (!willReloadData) { |
| 340 | // Configure the adapter to show the selection based on the |
| 341 | // lookup key extracted from the URI |
| 342 | ContactListAdapter adapter = getAdapter(); |
| 343 | if (adapter != null) { |
Dmitri Plotnikov | 7d40224 | 2011-01-09 16:52:08 -0800 | [diff] [blame] | 344 | adapter.setSelectedContact(mSelectedContactDirectoryId, |
| 345 | mSelectedContactLookupKey, mSelectedContactId); |
Dmitri Plotnikov | 2f28eda | 2010-12-15 16:49:01 -0800 | [diff] [blame] | 346 | getListView().invalidateViews(); |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 350 | // Also, launch a loader to pick up a new lookup URI in case it has changed |
| 351 | refreshSelectedContactUri(); |
Dmitri Plotnikov | cfc6489 | 2010-07-09 17:55:09 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Dmitri Plotnikov | 580286b | 2010-08-14 18:06:18 -0700 | [diff] [blame] | 355 | private void parseSelectedContactUri() { |
| 356 | if (mSelectedContactUri != null) { |
Dmitri Plotnikov | 90715b3 | 2010-11-23 10:57:23 -0800 | [diff] [blame] | 357 | String directoryParam = |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 358 | mSelectedContactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY); |
Dmitri Plotnikov | 90715b3 | 2010-11-23 10:57:23 -0800 | [diff] [blame] | 359 | mSelectedContactDirectoryId = TextUtils.isEmpty(directoryParam) ? Directory.DEFAULT |
| 360 | : Long.parseLong(directoryParam); |
| 361 | if (mSelectedContactUri.toString().startsWith(Contacts.CONTENT_LOOKUP_URI.toString())) { |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 362 | List<String> pathSegments = mSelectedContactUri.getPathSegments(); |
| 363 | mSelectedContactLookupKey = Uri.encode(pathSegments.get(2)); |
Dmitri Plotnikov | b906cbe | 2011-01-18 11:14:05 -0800 | [diff] [blame] | 364 | if (pathSegments.size() == 4) { |
Dmitri Plotnikov | 0c70809 | 2011-01-16 16:40:34 -0800 | [diff] [blame] | 365 | mSelectedContactId = ContentUris.parseId(mSelectedContactUri); |
| 366 | } |
Dmitri Plotnikov | fa49a66 | 2011-01-13 11:04:56 -0800 | [diff] [blame] | 367 | } else if (mSelectedContactUri.toString().startsWith(Contacts.CONTENT_URI.toString()) && |
| 368 | mSelectedContactUri.getPathSegments().size() >= 2) { |
Dmitri Plotnikov | 90715b3 | 2010-11-23 10:57:23 -0800 | [diff] [blame] | 369 | mSelectedContactLookupKey = null; |
Dmitri Plotnikov | 7d40224 | 2011-01-09 16:52:08 -0800 | [diff] [blame] | 370 | mSelectedContactId = ContentUris.parseId(mSelectedContactUri); |
| 371 | } else { |
| 372 | Log.e(TAG, "Unsupported contact URI: " + mSelectedContactUri); |
| 373 | mSelectedContactLookupKey = null; |
| 374 | mSelectedContactId = 0; |
Dmitri Plotnikov | 580286b | 2010-08-14 18:06:18 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Dmitri Plotnikov | 580286b | 2010-08-14 18:06:18 -0700 | [diff] [blame] | 377 | } else { |
| 378 | mSelectedContactDirectoryId = Directory.DEFAULT; |
| 379 | mSelectedContactLookupKey = null; |
Dmitri Plotnikov | 7d40224 | 2011-01-09 16:52:08 -0800 | [diff] [blame] | 380 | mSelectedContactId = 0; |
Dmitri Plotnikov | 580286b | 2010-08-14 18:06:18 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 384 | @Override |
Walter Jang | e9ea4f0 | 2016-05-10 09:39:46 -0700 | [diff] [blame] | 385 | public ContactListAdapter getAdapter() { |
| 386 | return (ContactListAdapter) super.getAdapter(); |
| 387 | } |
| 388 | |
| 389 | @Override |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 390 | protected void configureAdapter() { |
| 391 | super.configureAdapter(); |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 392 | |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 393 | ContactListAdapter adapter = getAdapter(); |
| 394 | if (adapter == null) { |
| 395 | return; |
| 396 | } |
| 397 | |
Isaac Katzenelson | 8fdf890 | 2011-08-10 21:00:54 -0700 | [diff] [blame] | 398 | boolean searchMode = isSearchMode(); |
| 399 | if (!searchMode && mFilter != null) { |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 400 | adapter.setFilter(mFilter); |
Dmitri Plotnikov | 3c8c3e8 | 2010-12-22 17:09:21 -0800 | [diff] [blame] | 401 | if (mSelectionRequired |
| 402 | || mFilter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) { |
Dmitri Plotnikov | 7d40224 | 2011-01-09 16:52:08 -0800 | [diff] [blame] | 403 | adapter.setSelectedContact( |
| 404 | mSelectedContactDirectoryId, mSelectedContactLookupKey, mSelectedContactId); |
Dmitri Plotnikov | 93a05db | 2010-12-22 15:23:54 -0800 | [diff] [blame] | 405 | } |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 406 | } |
Dave Santoro | 0a4d225 | 2011-05-20 11:40:34 -0700 | [diff] [blame] | 407 | |
Wenyi Wang | 6575129 | 2016-09-01 15:23:25 -0700 | [diff] [blame] | 408 | adapter.setIncludeFavorites(!searchMode && mFilter.isContactsFilterType()); |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | @Override |
Dmitri Plotnikov | 174fdc2 | 2010-10-29 13:55:01 -0700 | [diff] [blame] | 412 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
| 413 | super.onLoadFinished(loader, data); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 414 | mSelectionVerified = false; |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 415 | |
| 416 | // Refresh the currently selected lookup in case it changed while we were sleeping |
| 417 | refreshSelectedContactUri(); |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Dmitri Plotnikov | 0cd0130 | 2010-12-17 18:41:57 -0800 | [diff] [blame] | 420 | @Override |
Dianne Hackborn | 4ef95cc | 2010-12-16 00:44:33 -0800 | [diff] [blame] | 421 | public void onLoaderReset(Loader<Cursor> loader) { |
| 422 | } |
| 423 | |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 424 | private void checkSelection() { |
Dmitri Plotnikov | 174fdc2 | 2010-10-29 13:55:01 -0700 | [diff] [blame] | 425 | if (mSelectionVerified) { |
| 426 | return; |
| 427 | } |
| 428 | |
Dmitri Plotnikov | 93a05db | 2010-12-22 15:23:54 -0800 | [diff] [blame] | 429 | if (mRefreshingContactUri) { |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | if (isLoadingDirectoryList()) { |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
| 437 | ContactListAdapter adapter = getAdapter(); |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 438 | if (adapter == null) { |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
Dmitri Plotnikov | 93a05db | 2010-12-22 15:23:54 -0800 | [diff] [blame] | 442 | boolean directoryLoading = true; |
| 443 | int count = adapter.getPartitionCount(); |
| 444 | for (int i = 0; i < count; i++) { |
| 445 | Partition partition = adapter.getPartition(i); |
| 446 | if (partition instanceof DirectoryPartition) { |
| 447 | DirectoryPartition directory = (DirectoryPartition) partition; |
| 448 | if (directory.getDirectoryId() == mSelectedContactDirectoryId) { |
| 449 | directoryLoading = directory.isLoading(); |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (directoryLoading) { |
| 456 | return; |
| 457 | } |
| 458 | |
Dmitri Plotnikov | 7d40224 | 2011-01-09 16:52:08 -0800 | [diff] [blame] | 459 | adapter.setSelectedContact( |
| 460 | mSelectedContactDirectoryId, mSelectedContactLookupKey, mSelectedContactId); |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 461 | |
Katherine Kuan | e1cabcd | 2011-11-11 20:40:20 -0800 | [diff] [blame] | 462 | final int selectedPosition = adapter.getSelectedContactPosition(); |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 463 | if (selectedPosition != -1) { |
| 464 | mLastSelectedPosition = selectedPosition; |
| 465 | } else { |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 466 | if (isSearchMode()) { |
Dmitri Plotnikov | 5251884 | 2011-01-19 10:39:58 -0800 | [diff] [blame] | 467 | if (mDelaySelection) { |
| 468 | selectFirstFoundContactAfterDelay(); |
| 469 | if (mListener != null) { |
| 470 | mListener.onSelectionChange(); |
| 471 | } |
| 472 | return; |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 473 | } |
Dmitri Plotnikov | 5251884 | 2011-01-19 10:39:58 -0800 | [diff] [blame] | 474 | } else if (mSelectionRequired) { |
Dmitri Plotnikov | 8fe7821 | 2011-01-16 17:58:42 -0800 | [diff] [blame] | 475 | // A specific contact was requested, but it's not in the loaded list. |
| 476 | |
| 477 | // Try reconfiguring and reloading the list that will hopefully contain |
| 478 | // the requested contact. Only take one attempt to avoid an infinite loop |
| 479 | // in case the contact cannot be found at all. |
| 480 | mSelectionRequired = false; |
| 481 | |
| 482 | // If we were looking at a different specific contact, just reload |
Chiao Cheng | 3af2d48 | 2012-11-28 15:11:36 -0800 | [diff] [blame] | 483 | // FILTER_TYPE_ALL_ACCOUNTS is needed for the case where a new contact is added |
| 484 | // on a tablet and the loader is returning a stale list. In this case, the contact |
| 485 | // will not be found until the next load. b/7621855 This will only fix the most |
| 486 | // common case where all accounts are shown. It will not fix the one account case. |
| 487 | // TODO: we may want to add more FILTER_TYPEs or relax this check to fix all other |
| 488 | // FILTER_TYPE cases. |
Dmitri Plotnikov | 8fe7821 | 2011-01-16 17:58:42 -0800 | [diff] [blame] | 489 | if (mFilter != null |
Chiao Cheng | 3af2d48 | 2012-11-28 15:11:36 -0800 | [diff] [blame] | 490 | && (mFilter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT |
| 491 | || mFilter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS)) { |
Dmitri Plotnikov | 8fe7821 | 2011-01-16 17:58:42 -0800 | [diff] [blame] | 492 | reloadData(); |
| 493 | } else { |
| 494 | // Otherwise, call the listener, which will adjust the filter. |
| 495 | notifyInvalidSelection(); |
| 496 | } |
| 497 | return; |
Dmitri Plotnikov | 5251884 | 2011-01-19 10:39:58 -0800 | [diff] [blame] | 498 | } else if (mFilter != null |
Dmitri Plotnikov | 8fe7821 | 2011-01-16 17:58:42 -0800 | [diff] [blame] | 499 | && mFilter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) { |
Dmitri Plotnikov | 5251884 | 2011-01-19 10:39:58 -0800 | [diff] [blame] | 500 | // If we were trying to load a specific contact, but that contact no longer |
| 501 | // exists, call the listener, which will adjust the filter. |
Dmitri Plotnikov | 8fe7821 | 2011-01-16 17:58:42 -0800 | [diff] [blame] | 502 | notifyInvalidSelection(); |
| 503 | return; |
| 504 | } |
| 505 | |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 506 | saveSelectedUri(null); |
| 507 | selectDefaultContact(); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | mSelectionRequired = false; |
| 511 | mSelectionVerified = true; |
| 512 | |
| 513 | if (mSelectionPersistenceRequested) { |
| 514 | saveSelectedUri(mSelectedContactUri); |
| 515 | mSelectionPersistenceRequested = false; |
| 516 | } |
| 517 | |
| 518 | if (mSelectionToScreenRequested) { |
Katherine Kuan | e1cabcd | 2011-11-11 20:40:20 -0800 | [diff] [blame] | 519 | requestSelectionToScreen(selectedPosition); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Dmitri Plotnikov | 2f28eda | 2010-12-15 16:49:01 -0800 | [diff] [blame] | 522 | getListView().invalidateViews(); |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 523 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 524 | if (mListener != null) { |
| 525 | mListener.onSelectionChange(); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Automatically selects the first found contact in search mode. The selection |
| 531 | * is updated after a delay to allow the user to type without to much UI churn |
| 532 | * and to save bandwidth on directory queries. |
| 533 | */ |
| 534 | public void selectFirstFoundContactAfterDelay() { |
| 535 | Handler handler = getHandler(); |
| 536 | handler.removeMessages(MESSAGE_AUTOSELECT_FIRST_FOUND_CONTACT); |
| 537 | |
| 538 | String queryString = getQueryString(); |
| 539 | if (queryString != null |
| 540 | && queryString.length() >= AUTOSELECT_FIRST_FOUND_CONTACT_MIN_QUERY_LENGTH) { |
| 541 | handler.sendEmptyMessageDelayed(MESSAGE_AUTOSELECT_FIRST_FOUND_CONTACT, |
| 542 | DELAY_AUTOSELECT_FIRST_FOUND_CONTACT_MILLIS); |
| 543 | } else { |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 544 | setSelectedContactUri(null, false, false, false, false); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | protected void selectDefaultContact() { |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 549 | Uri contactUri = null; |
Dmitri Plotnikov | 07bf52d | 2011-02-22 18:46:40 -0800 | [diff] [blame] | 550 | ContactListAdapter adapter = getAdapter(); |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 551 | if (mLastSelectedPosition != -1) { |
Dmitri Plotnikov | 07bf52d | 2011-02-22 18:46:40 -0800 | [diff] [blame] | 552 | int count = adapter.getCount(); |
| 553 | int pos = mLastSelectedPosition; |
| 554 | if (pos >= count && count > 0) { |
| 555 | pos = count - 1; |
| 556 | } |
| 557 | contactUri = adapter.getContactUri(pos); |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | if (contactUri == null) { |
Dmitri Plotnikov | 07bf52d | 2011-02-22 18:46:40 -0800 | [diff] [blame] | 561 | contactUri = adapter.getFirstContactUri(); |
Dmitri Plotnikov | 682e152 | 2011-01-16 17:04:32 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | setSelectedContactUri(contactUri, false, mSmoothScrollRequested, false, false); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Katherine Kuan | e1cabcd | 2011-11-11 20:40:20 -0800 | [diff] [blame] | 567 | protected void requestSelectionToScreen(int selectedPosition) { |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 568 | if (selectedPosition != -1) { |
Dmitri Plotnikov | 753f8f6 | 2010-12-15 11:10:27 -0800 | [diff] [blame] | 569 | AutoScrollListView listView = (AutoScrollListView)getListView(); |
| 570 | listView.requestPositionToScreen( |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 571 | selectedPosition + listView.getHeaderViewsCount(), mSmoothScrollRequested); |
| 572 | mSelectionToScreenRequested = false; |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 573 | } |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Dmitri Plotnikov | 174fdc2 | 2010-10-29 13:55:01 -0700 | [diff] [blame] | 576 | @Override |
| 577 | public boolean isLoading() { |
Dmitri Plotnikov | 892fb51 | 2010-12-15 16:25:16 -0800 | [diff] [blame] | 578 | return mRefreshingContactUri || super.isLoading(); |
Dmitri Plotnikov | 174fdc2 | 2010-10-29 13:55:01 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 581 | @Override |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 582 | protected void startLoading() { |
| 583 | mStartedLoading = true; |
| 584 | mSelectionVerified = false; |
| 585 | super.startLoading(); |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Dmitri Plotnikov | a5a2744 | 2010-12-27 21:06:38 -0800 | [diff] [blame] | 588 | public void reloadDataAndSetSelectedUri(Uri uri) { |
| 589 | setSelectedContactUri(uri, true, true, true, true); |
| 590 | reloadData(); |
| 591 | } |
| 592 | |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 593 | @Override |
| 594 | public void reloadData() { |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 595 | if (mStartedLoading) { |
| 596 | mSelectionVerified = false; |
Dmitri Plotnikov | 04bad51 | 2011-01-17 12:43:01 -0800 | [diff] [blame] | 597 | mLastSelectedPosition = -1; |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 598 | super.reloadData(); |
| 599 | } |
Dmitri Plotnikov | 7ccdc1f | 2010-08-02 18:18:01 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Dmitri Plotnikov | 416b569 | 2010-04-27 18:14:58 -0700 | [diff] [blame] | 602 | public void setOnContactListActionListener(OnContactBrowserActionListener listener) { |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 603 | mListener = listener; |
| 604 | } |
| 605 | |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 606 | public void viewContact(int position, Uri contactUri, boolean isEnterpriseContact) { |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 607 | setSelectedContactUri(contactUri, false, false, true, false); |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 608 | if (mListener != null) mListener.onViewContactAction(position, contactUri, |
| 609 | isEnterpriseContact); |
Dmitri Plotnikov | 1323d69 | 2010-04-27 14:54:27 -0700 | [diff] [blame] | 610 | } |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 611 | |
Dmitri Plotnikov | 1323d69 | 2010-04-27 14:54:27 -0700 | [diff] [blame] | 612 | public void deleteContact(Uri contactUri) { |
Dmitri Plotnikov | 7152124 | 2010-11-17 18:47:14 -0800 | [diff] [blame] | 613 | if (mListener != null) mListener.onDeleteContactAction(contactUri); |
Dmitri Plotnikov | 1323d69 | 2010-04-27 14:54:27 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 616 | private void notifyInvalidSelection() { |
Dmitri Plotnikov | 7152124 | 2010-11-17 18:47:14 -0800 | [diff] [blame] | 617 | if (mListener != null) mListener.onInvalidSelection(); |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Dmitri Plotnikov | e3fbfd9 | 2010-04-28 11:20:52 -0700 | [diff] [blame] | 620 | @Override |
| 621 | protected void finish() { |
| 622 | super.finish(); |
Dmitri Plotnikov | 7152124 | 2010-11-17 18:47:14 -0800 | [diff] [blame] | 623 | if (mListener != null) mListener.onFinishAction(); |
Dmitri Plotnikov | e3fbfd9 | 2010-04-28 11:20:52 -0700 | [diff] [blame] | 624 | } |
Dmitri Plotnikov | 8c6955f | 2010-09-08 20:46:36 -0700 | [diff] [blame] | 625 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 626 | private void saveSelectedUri(Uri contactUri) { |
Dmitri Plotnikov | fa49a66 | 2011-01-13 11:04:56 -0800 | [diff] [blame] | 627 | if (isSearchMode()) { |
Dmitri Plotnikov | 8c6955f | 2010-09-08 20:46:36 -0700 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | |
Dmitri Plotnikov | fa49a66 | 2011-01-13 11:04:56 -0800 | [diff] [blame] | 631 | ContactListFilter.storeToPreferences(mPrefs, mFilter); |
| 632 | |
| 633 | Editor editor = mPrefs.edit(); |
| 634 | if (contactUri == null) { |
| 635 | editor.remove(getPersistentSelectionKey()); |
| 636 | } else { |
| 637 | editor.putString(getPersistentSelectionKey(), contactUri.toString()); |
| 638 | } |
| 639 | editor.apply(); |
| 640 | } |
| 641 | |
| 642 | private void restoreSelectedUri(boolean willReloadData) { |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 643 | // The meaning of mSelectionRequired is that we need to show some |
| 644 | // selection other than the previous selection saved in shared preferences |
| 645 | if (mSelectionRequired) { |
Dmitri Plotnikov | 8c6955f | 2010-09-08 20:46:36 -0700 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 649 | String selectedUri = mPrefs.getString(getPersistentSelectionKey(), null); |
| 650 | if (selectedUri == null) { |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 651 | setSelectedContactUri(null, false, false, false, willReloadData); |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 652 | } else { |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 653 | setSelectedContactUri(Uri.parse(selectedUri), false, false, false, willReloadData); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | private void saveFilter() { |
Dmitri Plotnikov | fa49a66 | 2011-01-13 11:04:56 -0800 | [diff] [blame] | 658 | ContactListFilter.storeToPreferences(mPrefs, mFilter); |
Dmitri Plotnikov | 264023c | 2010-12-15 12:04:13 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | private void restoreFilter() { |
Daisuke Miyakawa | d4ed72e | 2011-08-12 18:16:01 -0700 | [diff] [blame] | 662 | mFilter = ContactListFilter.restoreDefaultPreferences(mPrefs); |
Dmitri Plotnikov | 8c6955f | 2010-09-08 20:46:36 -0700 | [diff] [blame] | 663 | } |
Dmitri Plotnikov | 6e850a0 | 2010-10-28 10:46:21 -0700 | [diff] [blame] | 664 | |
Dmitri Plotnikov | d959104 | 2010-11-23 09:25:03 -0800 | [diff] [blame] | 665 | private String getPersistentSelectionKey() { |
| 666 | if (mFilter == null) { |
| 667 | return mPersistentSelectionPrefix; |
| 668 | } else { |
| 669 | return mPersistentSelectionPrefix + "-" + mFilter.getId(); |
| 670 | } |
Dmitri Plotnikov | 30cc853 | 2010-11-11 20:30:48 -0800 | [diff] [blame] | 671 | } |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 672 | } |