Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [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.Intent; |
| 19 | import android.content.Loader; |
| 20 | import android.database.Cursor; |
| 21 | import android.net.Uri; |
| 22 | import android.os.Bundle; |
Jay Shrauner | 745da9e | 2013-11-06 13:25:23 -0800 | [diff] [blame] | 23 | import android.text.TextUtils; |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | import android.view.LayoutInflater; |
| 26 | import android.view.MenuItem; |
| 27 | import android.view.View; |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 28 | import android.view.ViewGroup; |
| 29 | |
| 30 | import com.android.contacts.common.R; |
| 31 | import com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener; |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Fragment containing a phone number list for picking. |
| 35 | */ |
| 36 | public class PhoneNumberPickerFragment extends ContactEntryListFragment<ContactEntryListAdapter> |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 37 | implements OnShortcutIntentCreatedListener, PhoneNumberListAdapter.Listener { |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 38 | private static final String TAG = PhoneNumberPickerFragment.class.getSimpleName(); |
| 39 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 40 | private static final String KEY_SHORTCUT_ACTION = "shortcutAction"; |
| 41 | |
| 42 | private OnPhoneNumberPickerActionListener mListener; |
| 43 | private String mShortcutAction; |
| 44 | |
| 45 | private ContactListFilter mFilter; |
| 46 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 47 | private static final String KEY_FILTER = "filter"; |
| 48 | |
| 49 | /** true if the loader has started at least once. */ |
| 50 | private boolean mLoaderStarted; |
| 51 | |
| 52 | private boolean mUseCallableUri; |
| 53 | |
| 54 | private ContactListItemView.PhotoPosition mPhotoPosition = |
Fabrice Di Meglio | 29a5cf9 | 2013-04-03 20:59:09 -0700 | [diff] [blame] | 55 | ContactListItemView.getDefaultPhotoPosition(false /* normal/non opposite */); |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 56 | |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Handles a click on the video call icon for a row in the list. |
| 59 | * |
| 60 | * @param position The position in the list where the click ocurred. |
| 61 | */ |
| 62 | @Override |
| 63 | public void onVideoCallIconClicked(int position) { |
| 64 | callNumber(position, true /* isVideoCall */); |
| 65 | } |
| 66 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 67 | public PhoneNumberPickerFragment() { |
| 68 | setQuickContactEnabled(false); |
| 69 | setPhotoLoaderEnabled(true); |
| 70 | setSectionHeaderDisplayEnabled(true); |
Jay Shrauner | 138515a | 2013-07-26 09:32:27 -0700 | [diff] [blame] | 71 | setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE); |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 72 | |
| 73 | // Show nothing instead of letting caller Activity show something. |
| 74 | setHasOptionsMenu(true); |
| 75 | } |
| 76 | |
Jay Shrauner | 138515a | 2013-07-26 09:32:27 -0700 | [diff] [blame] | 77 | public void setDirectorySearchEnabled(boolean flag) { |
| 78 | setDirectorySearchMode(flag ? DirectoryListLoader.SEARCH_MODE_DEFAULT |
| 79 | : DirectoryListLoader.SEARCH_MODE_NONE); |
| 80 | } |
| 81 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 82 | public void setOnPhoneNumberPickerActionListener(OnPhoneNumberPickerActionListener listener) { |
| 83 | this.mListener = listener; |
| 84 | } |
| 85 | |
Yorke Lee | 6491384 | 2013-08-28 12:14:51 -0700 | [diff] [blame] | 86 | public OnPhoneNumberPickerActionListener getOnPhoneNumberPickerListener() { |
| 87 | return mListener; |
| 88 | } |
| 89 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 90 | @Override |
| 91 | protected void onCreateView(LayoutInflater inflater, ViewGroup container) { |
| 92 | super.onCreateView(inflater, container); |
| 93 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 94 | setVisibleScrollbarEnabled(getVisibleScrollbarEnabled()); |
| 95 | } |
| 96 | |
| 97 | protected boolean getVisibleScrollbarEnabled() { |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | @Override |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 102 | public void restoreSavedState(Bundle savedState) { |
| 103 | super.restoreSavedState(savedState); |
| 104 | |
| 105 | if (savedState == null) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | mFilter = savedState.getParcelable(KEY_FILTER); |
| 110 | mShortcutAction = savedState.getString(KEY_SHORTCUT_ACTION); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onSaveInstanceState(Bundle outState) { |
| 115 | super.onSaveInstanceState(outState); |
| 116 | outState.putParcelable(KEY_FILTER, mFilter); |
| 117 | outState.putString(KEY_SHORTCUT_ACTION, mShortcutAction); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean onOptionsItemSelected(MenuItem item) { |
| 122 | final int itemId = item.getItemId(); |
| 123 | if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled() |
| 124 | if (mListener != null) { |
| 125 | mListener.onHomeInActionBarSelected(); |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | return super.onOptionsItemSelected(item); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @param shortcutAction either {@link Intent#ACTION_CALL} or |
| 134 | * {@link Intent#ACTION_SENDTO} or null. |
| 135 | */ |
| 136 | public void setShortcutAction(String shortcutAction) { |
| 137 | this.mShortcutAction = shortcutAction; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | protected void onItemClick(int position, long id) { |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 142 | callNumber(position, false /* isVideoCall */); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Initiates a call to the number at the specified position. |
| 147 | * |
| 148 | * @param position The position. |
| 149 | * @param isVideoCall {@code true} if the call should be initiated as a video call, |
| 150 | * {@code false} otherwise. |
| 151 | */ |
| 152 | private void callNumber(int position, boolean isVideoCall) { |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 153 | final Uri phoneUri = getPhoneUri(position); |
| 154 | |
| 155 | if (phoneUri != null) { |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 156 | pickPhoneNumber(phoneUri, isVideoCall); |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 157 | } else { |
Jay Shrauner | 138515a | 2013-07-26 09:32:27 -0700 | [diff] [blame] | 158 | final String number = getPhoneNumber(position); |
Jay Shrauner | 745da9e | 2013-11-06 13:25:23 -0800 | [diff] [blame] | 159 | if (!TextUtils.isEmpty(number)) { |
Jay Shrauner | 8708070 | 2013-08-29 11:35:53 -0700 | [diff] [blame] | 160 | cacheContactInfo(position); |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 161 | mListener.onPickPhoneNumber(number, isVideoCall, |
Yorke Lee | 4ae9ca3 | 2015-09-15 12:53:37 -0700 | [diff] [blame] | 162 | getCallInitiationType(true /* isRemoteDirectory */)); |
Jay Shrauner | 138515a | 2013-07-26 09:32:27 -0700 | [diff] [blame] | 163 | } else { |
| 164 | Log.w(TAG, "Item at " + position + " was clicked before" |
| 165 | + " adapter is ready. Ignoring"); |
| 166 | } |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Jay Shrauner | 8708070 | 2013-08-29 11:35:53 -0700 | [diff] [blame] | 170 | protected void cacheContactInfo(int position) { |
| 171 | // Not implemented. Hook for child classes |
| 172 | } |
| 173 | |
Jay Shrauner | 138515a | 2013-07-26 09:32:27 -0700 | [diff] [blame] | 174 | protected String getPhoneNumber(int position) { |
| 175 | final PhoneNumberListAdapter adapter = (PhoneNumberListAdapter) getAdapter(); |
| 176 | return adapter.getPhoneNumber(position); |
| 177 | } |
| 178 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 179 | protected Uri getPhoneUri(int position) { |
| 180 | final PhoneNumberListAdapter adapter = (PhoneNumberListAdapter) getAdapter(); |
| 181 | return adapter.getDataUri(position); |
| 182 | } |
| 183 | |
Tyler Gunn | c405932 | 2015-03-20 09:07:53 -0700 | [diff] [blame] | 184 | protected String getLookupKey(int position) { |
| 185 | final PhoneNumberListAdapter adapter = (PhoneNumberListAdapter) getAdapter(); |
| 186 | return adapter.getLookupKey(position); |
| 187 | } |
| 188 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 189 | @Override |
| 190 | protected void startLoading() { |
| 191 | mLoaderStarted = true; |
| 192 | super.startLoading(); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
| 197 | super.onLoadFinished(loader, data); |
| 198 | |
| 199 | // disable scroll bar if there is no data |
Walter Jang | f1b1596 | 2016-03-25 12:26:06 -0700 | [diff] [blame] | 200 | setVisibleScrollbarEnabled(data != null && !data.isClosed() && data.getCount() > 0); |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | public void setUseCallableUri(boolean useCallableUri) { |
| 204 | mUseCallableUri = useCallableUri; |
| 205 | } |
| 206 | |
| 207 | public boolean usesCallableUri() { |
| 208 | return mUseCallableUri; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | protected ContactEntryListAdapter createListAdapter() { |
| 213 | PhoneNumberListAdapter adapter = new PhoneNumberListAdapter(getActivity()); |
| 214 | adapter.setDisplayPhotos(true); |
| 215 | adapter.setUseCallableUri(mUseCallableUri); |
| 216 | return adapter; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | protected void configureAdapter() { |
| 221 | super.configureAdapter(); |
| 222 | |
| 223 | final ContactEntryListAdapter adapter = getAdapter(); |
| 224 | if (adapter == null) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if (!isSearchMode() && mFilter != null) { |
| 229 | adapter.setFilter(mFilter); |
| 230 | } |
| 231 | |
| 232 | setPhotoPosition(adapter); |
| 233 | } |
| 234 | |
| 235 | protected void setPhotoPosition(ContactEntryListAdapter adapter) { |
| 236 | ((PhoneNumberListAdapter) adapter).setPhotoPosition(mPhotoPosition); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | protected View inflateView(LayoutInflater inflater, ViewGroup container) { |
| 241 | return inflater.inflate(R.layout.contact_list_content, null); |
| 242 | } |
| 243 | |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 244 | public void pickPhoneNumber(Uri uri, boolean isVideoCall) { |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 245 | if (mShortcutAction == null) { |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 246 | mListener.onPickDataUri(uri, isVideoCall, |
Yorke Lee | 4ae9ca3 | 2015-09-15 12:53:37 -0700 | [diff] [blame] | 247 | getCallInitiationType(false /* isRemoteDirectory */)); |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 248 | } else { |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 249 | startPhoneNumberShortcutIntent(uri, isVideoCall); |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 253 | protected void startPhoneNumberShortcutIntent(Uri uri, boolean isVideoCall) { |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 254 | ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this); |
| 255 | builder.createPhoneNumberShortcutIntent(uri, mShortcutAction); |
| 256 | } |
| 257 | |
Yorke Lee | 4ae9ca3 | 2015-09-15 12:53:37 -0700 | [diff] [blame] | 258 | @Override |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 259 | public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) { |
| 260 | mListener.onShortcutIntentCreated(shortcutIntent); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void onPickerResult(Intent data) { |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 265 | mListener.onPickDataUri(data.getData(), false /* isVideoCall */, |
Yorke Lee | 4ae9ca3 | 2015-09-15 12:53:37 -0700 | [diff] [blame] | 266 | getCallInitiationType(false /* isRemoteDirectory */)); |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 269 | public void setFilter(ContactListFilter filter) { |
| 270 | if ((mFilter == null && filter == null) || |
| 271 | (mFilter != null && mFilter.equals(filter))) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | mFilter = filter; |
| 276 | if (mLoaderStarted) { |
| 277 | reloadData(); |
| 278 | } |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | public void setPhotoPosition(ContactListItemView.PhotoPosition photoPosition) { |
| 282 | mPhotoPosition = photoPosition; |
| 283 | |
| 284 | final PhoneNumberListAdapter adapter = (PhoneNumberListAdapter) getAdapter(); |
| 285 | if (adapter != null) { |
| 286 | adapter.setPhotoPosition(photoPosition); |
| 287 | } |
| 288 | } |
Tyler Gunn | c405932 | 2015-03-20 09:07:53 -0700 | [diff] [blame] | 289 | |
| 290 | /** |
Yorke Lee | 4ae9ca3 | 2015-09-15 12:53:37 -0700 | [diff] [blame] | 291 | * @param isRemoteDirectory {@code true} if the call was initiated using a contact/phone number |
| 292 | * not in the local contacts database |
| 293 | */ |
| 294 | protected int getCallInitiationType(boolean isRemoteDirectory) { |
| 295 | return OnPhoneNumberPickerActionListener.CALL_INITIATION_UNKNOWN; |
| 296 | } |
Chiao Cheng | fed477c | 2012-12-04 17:40:46 -0800 | [diff] [blame] | 297 | } |