blob: 6a736dba98874f68c4f3843d64db5c792c5cee2f [file] [log] [blame]
Chiao Chengfed477c2012-12-04 17:40:46 -08001/*
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 */
Gary Mai69c182a2016-12-05 13:07:03 -080016package com.android.contacts.list;
Chiao Chengfed477c2012-12-04 17:40:46 -080017
18import android.content.Intent;
19import android.content.Loader;
20import android.database.Cursor;
21import android.net.Uri;
22import android.os.Bundle;
Jay Shrauner745da9e2013-11-06 13:25:23 -080023import android.text.TextUtils;
Chiao Chengfed477c2012-12-04 17:40:46 -080024import android.util.Log;
25import android.view.LayoutInflater;
26import android.view.MenuItem;
27import android.view.View;
Chiao Chengfed477c2012-12-04 17:40:46 -080028import android.view.ViewGroup;
29
Arthur Wang3f6a2442016-12-05 14:51:59 -080030import com.android.contacts.R;
Gary Mai69c182a2016-12-05 13:07:03 -080031import com.android.contacts.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
Chiao Chengfed477c2012-12-04 17:40:46 -080032
33/**
34 * Fragment containing a phone number list for picking.
35 */
36public class PhoneNumberPickerFragment extends ContactEntryListFragment<ContactEntryListAdapter>
Tyler Gunn001d9742015-12-18 13:57:02 -080037 implements OnShortcutIntentCreatedListener, PhoneNumberListAdapter.Listener {
Chiao Chengfed477c2012-12-04 17:40:46 -080038 private static final String TAG = PhoneNumberPickerFragment.class.getSimpleName();
39
Chiao Chengfed477c2012-12-04 17:40:46 -080040 private static final String KEY_SHORTCUT_ACTION = "shortcutAction";
41
42 private OnPhoneNumberPickerActionListener mListener;
43 private String mShortcutAction;
44
45 private ContactListFilter mFilter;
46
Chiao Chengfed477c2012-12-04 17:40:46 -080047 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 Meglio29a5cf92013-04-03 20:59:09 -070055 ContactListItemView.getDefaultPhotoPosition(false /* normal/non opposite */);
Chiao Chengfed477c2012-12-04 17:40:46 -080056
Tyler Gunn001d9742015-12-18 13:57:02 -080057 /**
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 Chengfed477c2012-12-04 17:40:46 -080067 public PhoneNumberPickerFragment() {
68 setQuickContactEnabled(false);
69 setPhotoLoaderEnabled(true);
70 setSectionHeaderDisplayEnabled(true);
Jay Shrauner138515a2013-07-26 09:32:27 -070071 setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE);
Chiao Chengfed477c2012-12-04 17:40:46 -080072
73 // Show nothing instead of letting caller Activity show something.
74 setHasOptionsMenu(true);
75 }
76
Jay Shrauner138515a2013-07-26 09:32:27 -070077 public void setDirectorySearchEnabled(boolean flag) {
78 setDirectorySearchMode(flag ? DirectoryListLoader.SEARCH_MODE_DEFAULT
79 : DirectoryListLoader.SEARCH_MODE_NONE);
80 }
81
Chiao Chengfed477c2012-12-04 17:40:46 -080082 public void setOnPhoneNumberPickerActionListener(OnPhoneNumberPickerActionListener listener) {
83 this.mListener = listener;
84 }
85
Yorke Lee64913842013-08-28 12:14:51 -070086 public OnPhoneNumberPickerActionListener getOnPhoneNumberPickerListener() {
87 return mListener;
88 }
89
Chiao Chengfed477c2012-12-04 17:40:46 -080090 @Override
91 protected void onCreateView(LayoutInflater inflater, ViewGroup container) {
92 super.onCreateView(inflater, container);
93
Chiao Chengfed477c2012-12-04 17:40:46 -080094 setVisibleScrollbarEnabled(getVisibleScrollbarEnabled());
95 }
96
97 protected boolean getVisibleScrollbarEnabled() {
98 return true;
99 }
100
101 @Override
Chiao Chengfed477c2012-12-04 17:40:46 -0800102 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 Gunn001d9742015-12-18 13:57:02 -0800142 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 Chengfed477c2012-12-04 17:40:46 -0800153 final Uri phoneUri = getPhoneUri(position);
154
155 if (phoneUri != null) {
Tyler Gunn001d9742015-12-18 13:57:02 -0800156 pickPhoneNumber(phoneUri, isVideoCall);
Chiao Chengfed477c2012-12-04 17:40:46 -0800157 } else {
Jay Shrauner138515a2013-07-26 09:32:27 -0700158 final String number = getPhoneNumber(position);
Jay Shrauner745da9e2013-11-06 13:25:23 -0800159 if (!TextUtils.isEmpty(number)) {
Jay Shrauner87080702013-08-29 11:35:53 -0700160 cacheContactInfo(position);
Tyler Gunn001d9742015-12-18 13:57:02 -0800161 mListener.onPickPhoneNumber(number, isVideoCall,
Yorke Lee4ae9ca32015-09-15 12:53:37 -0700162 getCallInitiationType(true /* isRemoteDirectory */));
Jay Shrauner138515a2013-07-26 09:32:27 -0700163 } else {
164 Log.w(TAG, "Item at " + position + " was clicked before"
165 + " adapter is ready. Ignoring");
166 }
Chiao Chengfed477c2012-12-04 17:40:46 -0800167 }
168 }
169
Jay Shrauner87080702013-08-29 11:35:53 -0700170 protected void cacheContactInfo(int position) {
171 // Not implemented. Hook for child classes
172 }
173
Jay Shrauner138515a2013-07-26 09:32:27 -0700174 protected String getPhoneNumber(int position) {
175 final PhoneNumberListAdapter adapter = (PhoneNumberListAdapter) getAdapter();
176 return adapter.getPhoneNumber(position);
177 }
178
Chiao Chengfed477c2012-12-04 17:40:46 -0800179 protected Uri getPhoneUri(int position) {
180 final PhoneNumberListAdapter adapter = (PhoneNumberListAdapter) getAdapter();
181 return adapter.getDataUri(position);
182 }
183
Tyler Gunnc4059322015-03-20 09:07:53 -0700184 protected String getLookupKey(int position) {
185 final PhoneNumberListAdapter adapter = (PhoneNumberListAdapter) getAdapter();
186 return adapter.getLookupKey(position);
187 }
188
Chiao Chengfed477c2012-12-04 17:40:46 -0800189 @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 Jangf1b15962016-03-25 12:26:06 -0700200 setVisibleScrollbarEnabled(data != null && !data.isClosed() && data.getCount() > 0);
Chiao Chengfed477c2012-12-04 17:40:46 -0800201 }
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 Gunn001d9742015-12-18 13:57:02 -0800244 public void pickPhoneNumber(Uri uri, boolean isVideoCall) {
Chiao Chengfed477c2012-12-04 17:40:46 -0800245 if (mShortcutAction == null) {
Tyler Gunn001d9742015-12-18 13:57:02 -0800246 mListener.onPickDataUri(uri, isVideoCall,
Yorke Lee4ae9ca32015-09-15 12:53:37 -0700247 getCallInitiationType(false /* isRemoteDirectory */));
Chiao Chengfed477c2012-12-04 17:40:46 -0800248 } else {
Tyler Gunn001d9742015-12-18 13:57:02 -0800249 startPhoneNumberShortcutIntent(uri, isVideoCall);
Chiao Chengfed477c2012-12-04 17:40:46 -0800250 }
251 }
252
Tyler Gunn001d9742015-12-18 13:57:02 -0800253 protected void startPhoneNumberShortcutIntent(Uri uri, boolean isVideoCall) {
Chiao Chengfed477c2012-12-04 17:40:46 -0800254 ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this);
255 builder.createPhoneNumberShortcutIntent(uri, mShortcutAction);
256 }
257
Yorke Lee4ae9ca32015-09-15 12:53:37 -0700258 @Override
Chiao Chengfed477c2012-12-04 17:40:46 -0800259 public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
260 mListener.onShortcutIntentCreated(shortcutIntent);
261 }
262
263 @Override
264 public void onPickerResult(Intent data) {
Tyler Gunn001d9742015-12-18 13:57:02 -0800265 mListener.onPickDataUri(data.getData(), false /* isVideoCall */,
Yorke Lee4ae9ca32015-09-15 12:53:37 -0700266 getCallInitiationType(false /* isRemoteDirectory */));
Chiao Chengfed477c2012-12-04 17:40:46 -0800267 }
268
Chiao Chengfed477c2012-12-04 17:40:46 -0800269 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 Chengfed477c2012-12-04 17:40:46 -0800279 }
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 Gunnc4059322015-03-20 09:07:53 -0700289
290 /**
Yorke Lee4ae9ca32015-09-15 12:53:37 -0700291 * @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 Chengfed477c2012-12-04 17:40:46 -0800297}