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