blob: 2b16a983b8439840509197a98eead7aa38f2b5d2 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
2 * Copyright (C) 2009 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
17package com.android.dialer;
18
Chiao Cheng94b10b52012-08-17 16:59:12 -070019import android.app.Activity;
Yorke Lee03459442013-10-30 18:29:32 -070020import android.app.LoaderManager.LoaderCallbacks;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.content.ContentResolver;
22import android.content.ContentUris;
23import android.content.ContentValues;
24import android.content.Context;
25import android.content.Intent;
Yorke Lee03459442013-10-30 18:29:32 -070026import android.content.Loader;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.content.res.Resources;
28import android.database.Cursor;
29import android.graphics.drawable.Drawable;
30import android.net.Uri;
31import android.os.AsyncTask;
32import android.os.Bundle;
33import android.provider.CallLog;
34import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.ContactsContract.CommonDataKinds.Phone;
36import android.provider.ContactsContract.Contacts;
Yorke Lee03459442013-10-30 18:29:32 -070037import android.provider.ContactsContract.DisplayNameSources;
38import android.provider.ContactsContract.Intents.Insert;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.provider.VoicemailContract.Voicemails;
40import android.telephony.PhoneNumberUtils;
41import android.telephony.TelephonyManager;
42import android.text.TextUtils;
43import android.util.Log;
44import android.view.ActionMode;
45import android.view.KeyEvent;
46import android.view.LayoutInflater;
47import android.view.Menu;
48import android.view.MenuItem;
49import android.view.View;
50import android.widget.ImageButton;
51import android.widget.ImageView;
52import android.widget.ListView;
53import android.widget.TextView;
54import android.widget.Toast;
55
Chiao Cheng35071c02012-10-15 18:36:24 -070056import com.android.contacts.common.ContactPhotoManager;
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070057import com.android.contacts.common.CallUtil;
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070058import com.android.contacts.common.ClipboardUtils;
Chiao Cheng35071c02012-10-15 18:36:24 -070059import com.android.contacts.common.GeoUtil;
Yorke Lee03459442013-10-30 18:29:32 -070060import com.android.contacts.common.model.Contact;
61import com.android.contacts.common.model.ContactLoader;
Yorke Lee58ebe5d2013-09-09 10:19:40 -070062import com.android.contacts.common.util.UriUtils;
Chiao Cheng91197042012-08-24 14:19:37 -070063import com.android.dialer.BackScrollManager.ScrollableHeader;
Chiao Cheng94b10b52012-08-17 16:59:12 -070064import com.android.dialer.calllog.CallDetailHistoryAdapter;
65import com.android.dialer.calllog.CallTypeHelper;
66import com.android.dialer.calllog.ContactInfo;
67import com.android.dialer.calllog.ContactInfoHelper;
68import com.android.dialer.calllog.PhoneNumberHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070069import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070070import com.android.dialer.util.AsyncTaskExecutor;
71import com.android.dialer.util.AsyncTaskExecutors;
Chiao Cheng94b10b52012-08-17 16:59:12 -070072import com.android.dialer.voicemail.VoicemailPlaybackFragment;
73import com.android.dialer.voicemail.VoicemailStatusHelper;
74import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
75import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
76
77import java.util.List;
78
79/**
80 * Displays the details of a specific call log entry.
81 * <p>
82 * This activity can be either started with the URI of a single call log entry, or with the
83 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
84 */
85public class CallDetailActivity extends Activity implements ProximitySensorAware {
86 private static final String TAG = "CallDetail";
87
Yorke Lee03459442013-10-30 18:29:32 -070088 private static final int LOADER_ID = 0;
89 private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
90
Chiao Cheng35071c02012-10-15 18:36:24 -070091 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
92 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
93
Chiao Cheng94b10b52012-08-17 16:59:12 -070094 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
95 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
96 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
97 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
98
99 /** The enumeration of {@link AsyncTask} objects used in this class. */
100 public enum Tasks {
101 MARK_VOICEMAIL_READ,
102 DELETE_VOICEMAIL_AND_FINISH,
103 REMOVE_FROM_CALL_LOG_AND_FINISH,
104 UPDATE_PHONE_CALL_DETAILS,
105 }
106
107 /** A long array extra containing ids of call log entries to display. */
108 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
109 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
110 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
111 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
112 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
113 /** If the activity was triggered from a notification. */
114 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
115
116 private CallTypeHelper mCallTypeHelper;
117 private PhoneNumberHelper mPhoneNumberHelper;
118 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
119 private TextView mHeaderTextView;
120 private View mHeaderOverlayView;
121 private ImageView mMainActionView;
122 private ImageButton mMainActionPushLayerView;
123 private ImageView mContactBackgroundView;
124 private AsyncTaskExecutor mAsyncTaskExecutor;
125 private ContactInfoHelper mContactInfoHelper;
126
127 private String mNumber = null;
128 private String mDefaultCountryIso;
129
130 /* package */ LayoutInflater mInflater;
131 /* package */ Resources mResources;
132 /** Helper to load contact photos. */
133 private ContactPhotoManager mContactPhotoManager;
134 /** Helper to make async queries to content resolver. */
135 private CallDetailActivityQueryHandler mAsyncQueryHandler;
136 /** Helper to get voicemail status messages. */
137 private VoicemailStatusHelper mVoicemailStatusHelper;
138 // Views related to voicemail status message.
139 private View mStatusMessageView;
140 private TextView mStatusMessageText;
141 private TextView mStatusMessageAction;
142
143 /** Whether we should show "edit number before call" in the options menu. */
144 private boolean mHasEditNumberBeforeCallOption;
145 /** Whether we should show "trash" in the options menu. */
146 private boolean mHasTrashOption;
147 /** Whether we should show "remove from call log" in the options menu. */
148 private boolean mHasRemoveFromCallLogOption;
149
150 private ProximitySensorManager mProximitySensorManager;
151 private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
152
153 /**
154 * The action mode used when the phone number is selected. This will be non-null only when the
155 * phone number is selected.
156 */
157 private ActionMode mPhoneNumberActionMode;
158
159 private CharSequence mPhoneNumberLabelToCopy;
160 private CharSequence mPhoneNumberToCopy;
161
162 /** Listener to changes in the proximity sensor state. */
163 private class ProximitySensorListener implements ProximitySensorManager.Listener {
164 /** Used to show a blank view and hide the action bar. */
165 private final Runnable mBlankRunnable = new Runnable() {
166 @Override
167 public void run() {
168 View blankView = findViewById(R.id.blank);
169 blankView.setVisibility(View.VISIBLE);
170 getActionBar().hide();
171 }
172 };
173 /** Used to remove the blank view and show the action bar. */
174 private final Runnable mUnblankRunnable = new Runnable() {
175 @Override
176 public void run() {
177 View blankView = findViewById(R.id.blank);
178 blankView.setVisibility(View.GONE);
179 getActionBar().show();
180 }
181 };
182
183 @Override
184 public synchronized void onNear() {
185 clearPendingRequests();
186 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
187 }
188
189 @Override
190 public synchronized void onFar() {
191 clearPendingRequests();
192 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
193 }
194
195 /** Removed any delayed requests that may be pending. */
196 public synchronized void clearPendingRequests() {
197 View blankView = findViewById(R.id.blank);
198 blankView.removeCallbacks(mBlankRunnable);
199 blankView.removeCallbacks(mUnblankRunnable);
200 }
201
202 /** Post a {@link Runnable} with a delay on the main thread. */
203 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
204 // Post these instead of executing immediately so that:
205 // - They are guaranteed to be executed on the main thread.
206 // - If the sensor values changes rapidly for some time, the UI will not be
207 // updated immediately.
208 View blankView = findViewById(R.id.blank);
209 blankView.postDelayed(runnable, delayMillis);
210 }
211 }
212
213 static final String[] CALL_LOG_PROJECTION = new String[] {
214 CallLog.Calls.DATE,
215 CallLog.Calls.DURATION,
216 CallLog.Calls.NUMBER,
217 CallLog.Calls.TYPE,
218 CallLog.Calls.COUNTRY_ISO,
219 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700220 CallLog.Calls.NUMBER_PRESENTATION,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700221 };
222
223 static final int DATE_COLUMN_INDEX = 0;
224 static final int DURATION_COLUMN_INDEX = 1;
225 static final int NUMBER_COLUMN_INDEX = 2;
226 static final int CALL_TYPE_COLUMN_INDEX = 3;
227 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
228 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700229 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700230
231 private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
232 @Override
233 public void onClick(View view) {
234 if (finishPhoneNumerSelectedActionModeIfShown()) {
235 return;
236 }
237 startActivity(((ViewEntry) view.getTag()).primaryIntent);
238 }
239 };
240
241 private final View.OnClickListener mSecondaryActionListener = new View.OnClickListener() {
242 @Override
243 public void onClick(View view) {
244 if (finishPhoneNumerSelectedActionModeIfShown()) {
245 return;
246 }
247 startActivity(((ViewEntry) view.getTag()).secondaryIntent);
248 }
249 };
250
251 private final View.OnLongClickListener mPrimaryLongClickListener =
252 new View.OnLongClickListener() {
253 @Override
254 public boolean onLongClick(View v) {
255 if (finishPhoneNumerSelectedActionModeIfShown()) {
256 return true;
257 }
258 startPhoneNumberSelectedActionMode(v);
259 return true;
260 }
261 };
262
Yorke Lee03459442013-10-30 18:29:32 -0700263 private final LoaderCallbacks<Contact> mLoaderCallbacks = new LoaderCallbacks<Contact>() {
264 @Override
265 public void onLoaderReset(Loader<Contact> loader) {
266 }
267
268 @Override
269 public void onLoadFinished(Loader<Contact> loader, Contact data) {
270 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
271 intent.setType(Contacts.CONTENT_ITEM_TYPE);
272 if (data.getDisplayNameSource() >= DisplayNameSources.ORGANIZATION) {
273 intent.putExtra(Insert.NAME, data.getDisplayName());
274 }
275 intent.putExtra(Insert.DATA, data.getContentValues());
276 bindContactPhotoAction(intent, R.drawable.ic_add_contact_holo_dark,
277 getString(R.string.description_add_contact));
278 }
279
280 @Override
281 public Loader<Contact> onCreateLoader(int id, Bundle args) {
282 final Uri contactUri = args.getParcelable(BUNDLE_CONTACT_URI_EXTRA);
283 if (contactUri == null) {
284 Log.wtf(TAG, "No contact lookup uri provided.");
285 }
286 return new ContactLoader(CallDetailActivity.this, contactUri,
287 false /* loadGroupMetaData */, false /* loadInvitableAccountTypes */,
288 false /* postViewNotification */, true /* computeFormattedPhoneNumber */);
289 }
290 };
291
Chiao Cheng94b10b52012-08-17 16:59:12 -0700292 @Override
293 protected void onCreate(Bundle icicle) {
294 super.onCreate(icicle);
295
296 setContentView(R.layout.call_detail);
297
298 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
299 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
300 mResources = getResources();
301
302 mCallTypeHelper = new CallTypeHelper(getResources());
303 mPhoneNumberHelper = new PhoneNumberHelper(mResources);
304 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Chiao Chengfb0a9342013-09-13 17:27:42 -0700305 new PhoneNumberUtilsWrapper());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700306 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
307 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
308 mHeaderTextView = (TextView) findViewById(R.id.header_text);
309 mHeaderOverlayView = findViewById(R.id.photo_text_bar);
310 mStatusMessageView = findViewById(R.id.voicemail_status);
311 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
312 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
313 mMainActionView = (ImageView) findViewById(R.id.main_action);
314 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
315 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Chiao Cheng35071c02012-10-15 18:36:24 -0700316 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700317 mContactPhotoManager = ContactPhotoManager.getInstance(this);
318 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700319 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700320 getActionBar().setDisplayHomeAsUpEnabled(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321 optionallyHandleVoicemail();
322 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
323 closeSystemDialogs();
324 }
325 }
326
327 @Override
328 public void onResume() {
329 super.onResume();
330 updateData(getCallLogEntryUris());
331 }
332
333 /**
334 * Handle voicemail playback or hide voicemail ui.
335 * <p>
336 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
337 * playback. If it doesn't, then hide the voicemail ui.
338 */
339 private void optionallyHandleVoicemail() {
340 View voicemailContainer = findViewById(R.id.voicemail_container);
341 if (hasVoicemail()) {
342 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
343 // to play and optionally start the playback.
344 // Do a query to fetch the voicemail status messages.
345 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
346 Bundle fragmentArguments = new Bundle();
347 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
348 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
349 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
350 }
351 playbackFragment.setArguments(fragmentArguments);
352 voicemailContainer.setVisibility(View.VISIBLE);
353 getFragmentManager().beginTransaction()
Yorke Leeaa536fd2013-07-29 11:31:04 -0700354 .add(R.id.voicemail_container, playbackFragment)
355 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700356 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
357 markVoicemailAsRead(getVoicemailUri());
358 } else {
359 // No voicemail uri: hide the status view.
360 mStatusMessageView.setVisibility(View.GONE);
361 voicemailContainer.setVisibility(View.GONE);
362 }
363 }
364
365 private boolean hasVoicemail() {
366 return getVoicemailUri() != null;
367 }
368
369 private Uri getVoicemailUri() {
370 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
371 }
372
373 private void markVoicemailAsRead(final Uri voicemailUri) {
374 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
375 @Override
376 public Void doInBackground(Void... params) {
377 ContentValues values = new ContentValues();
378 values.put(Voicemails.IS_READ, true);
379 getContentResolver().update(voicemailUri, values,
380 Voicemails.IS_READ + " = 0", null);
381 return null;
382 }
383 });
384 }
385
386 /**
387 * Returns the list of URIs to show.
388 * <p>
389 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
390 * a list of ids in the call log added as an extra on the URI.
391 * <p>
392 * If both are available, the data on the intent takes precedence.
393 */
394 private Uri[] getCallLogEntryUris() {
395 Uri uri = getIntent().getData();
396 if (uri != null) {
397 // If there is a data on the intent, it takes precedence over the extra.
398 return new Uri[]{ uri };
399 }
400 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
401 Uri[] uris = new Uri[ids.length];
402 for (int index = 0; index < ids.length; ++index) {
403 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
404 }
405 return uris;
406 }
407
408 @Override
409 public boolean onKeyDown(int keyCode, KeyEvent event) {
410 switch (keyCode) {
411 case KeyEvent.KEYCODE_CALL: {
412 // Make sure phone isn't already busy before starting direct call
413 TelephonyManager tm = (TelephonyManager)
414 getSystemService(Context.TELEPHONY_SERVICE);
415 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700416 startActivity(CallUtil.getCallIntent(
417 Uri.fromParts(CallUtil.SCHEME_TEL, mNumber, null)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700418 return true;
419 }
420 }
421 }
422
423 return super.onKeyDown(keyCode, event);
424 }
425
426 /**
427 * Update user interface with details of given call.
428 *
429 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
430 */
431 private void updateData(final Uri... callUris) {
432 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
433 @Override
434 public PhoneCallDetails[] doInBackground(Void... params) {
435 // TODO: All phone calls correspond to the same person, so we can make a single
436 // lookup.
437 final int numCalls = callUris.length;
438 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
439 try {
440 for (int index = 0; index < numCalls; ++index) {
441 details[index] = getPhoneCallDetailsForUri(callUris[index]);
442 }
443 return details;
444 } catch (IllegalArgumentException e) {
445 // Something went wrong reading in our primary data.
446 Log.w(TAG, "invalid URI starting call details", e);
447 return null;
448 }
449 }
450
451 @Override
452 public void onPostExecute(PhoneCallDetails[] details) {
453 if (details == null) {
454 // Somewhere went wrong: we're going to bail out and show error to users.
455 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
456 Toast.LENGTH_SHORT).show();
457 finish();
458 return;
459 }
460
461 // We know that all calls are from the same number and the same contact, so pick the
462 // first.
463 PhoneCallDetails firstDetails = details[0];
464 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700465 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700466 final Uri contactUri = firstDetails.contactUri;
467 final Uri photoUri = firstDetails.photoUri;
468
469 // Set the details header, based on the first phone call.
470 mPhoneCallDetailsHelper.setCallDetailsHeader(mHeaderTextView, firstDetails);
471
472 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700473 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700474 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
475 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
476 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
477 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700478
479 // Let user view contact details if they exist, otherwise add option to create new
480 // contact from this number.
481 final Intent mainActionIntent;
482 final int mainActionIcon;
483 final String mainActionDescription;
484
485 final CharSequence nameOrNumber;
486 if (!TextUtils.isEmpty(firstDetails.name)) {
487 nameOrNumber = firstDetails.name;
488 } else {
489 nameOrNumber = firstDetails.number;
490 }
491
Yorke Lee58ebe5d2013-09-09 10:19:40 -0700492 if (contactUri != null && !UriUtils.isEncodedContactUri(contactUri)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700493 mainActionIntent = new Intent(Intent.ACTION_VIEW, contactUri);
494 // This will launch People's detail contact screen, so we probably want to
495 // treat it as a separate People task.
496 mainActionIntent.setFlags(
497 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
498 mainActionIcon = R.drawable.ic_contacts_holo_dark;
499 mainActionDescription =
500 getString(R.string.description_view_contact, nameOrNumber);
Yorke Lee03459442013-10-30 18:29:32 -0700501 } else if (UriUtils.isEncodedContactUri(contactUri)) {
502 final Bundle bundle = new Bundle(1);
503 bundle.putParcelable(BUNDLE_CONTACT_URI_EXTRA, contactUri);
504 getLoaderManager().initLoader(LOADER_ID, bundle, mLoaderCallbacks);
505 mainActionIntent = null;
506 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
507 mainActionDescription = getString(R.string.description_add_contact);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700508 } else if (isVoicemailNumber) {
509 mainActionIntent = null;
510 mainActionIcon = 0;
511 mainActionDescription = null;
512 } else if (isSipNumber) {
513 // TODO: This item is currently disabled for SIP addresses, because
514 // the Insert.PHONE extra only works correctly for PSTN numbers.
515 //
516 // To fix this for SIP addresses, we need to:
517 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
518 // the current number is a SIP address
519 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
520 // updating the SipAddress field
521 // and then we can remove the "!isSipNumber" check above.
522 mainActionIntent = null;
523 mainActionIcon = 0;
524 mainActionDescription = null;
525 } else if (canPlaceCallsTo) {
526 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
527 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
528 mainActionIntent.putExtra(Insert.PHONE, mNumber);
529 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
530 mainActionDescription = getString(R.string.description_add_contact);
531 } else {
Yorke Lee03459442013-10-30 18:29:32 -0700532 // If we cannot call the number, when we probably cannot add it as a contact
533 // either. This is usually the case of private, unknown, or payphone numbers.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700534 mainActionIntent = null;
535 mainActionIcon = 0;
536 mainActionDescription = null;
537 }
538
Yorke Lee03459442013-10-30 18:29:32 -0700539 bindContactPhotoAction(mainActionIntent, mainActionIcon, mainActionDescription);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700540
541 // This action allows to call the number that places the call.
542 if (canPlaceCallsTo) {
543 final CharSequence displayNumber =
544 mPhoneNumberHelper.getDisplayNumber(
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700545 firstDetails.number,
546 firstDetails.numberPresentation,
547 firstDetails.formattedNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700548
549 ViewEntry entry = new ViewEntry(
550 getString(R.string.menu_callNumber,
Chiao Cheng35071c02012-10-15 18:36:24 -0700551 forceLeftToRight(displayNumber)),
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700552 CallUtil.getCallIntent(mNumber),
Chiao Cheng94b10b52012-08-17 16:59:12 -0700553 getString(R.string.description_call, nameOrNumber));
554
555 // Only show a label if the number is shown and it is not a SIP address.
556 if (!TextUtils.isEmpty(firstDetails.name)
557 && !TextUtils.isEmpty(firstDetails.number)
558 && !PhoneNumberUtils.isUriNumber(firstDetails.number.toString())) {
559 entry.label = Phone.getTypeLabel(mResources, firstDetails.numberType,
560 firstDetails.numberLabel);
561 }
562
563 // The secondary action allows to send an SMS to the number that placed the
564 // call.
Chiao Chengfb0a9342013-09-13 17:27:42 -0700565 if (phoneUtils.canSendSmsTo(mNumber, numberPresentation)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700566 entry.setSecondaryAction(
Yorke Leeaa536fd2013-07-29 11:31:04 -0700567 R.drawable.ic_text_holo_light,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700568 new Intent(Intent.ACTION_SENDTO,
569 Uri.fromParts("sms", mNumber, null)),
570 getString(R.string.description_send_text_message, nameOrNumber));
571 }
572
573 configureCallButton(entry);
574 mPhoneNumberToCopy = displayNumber;
575 mPhoneNumberLabelToCopy = entry.label;
576 } else {
577 disableCallButton();
578 mPhoneNumberToCopy = null;
579 mPhoneNumberLabelToCopy = null;
580 }
581
582 mHasEditNumberBeforeCallOption =
583 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
584 mHasTrashOption = hasVoicemail();
585 mHasRemoveFromCallLogOption = !hasVoicemail();
586 invalidateOptionsMenu();
587
588 ListView historyList = (ListView) findViewById(R.id.history);
589 historyList.setAdapter(
590 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
591 mCallTypeHelper, details, hasVoicemail(), canPlaceCallsTo,
592 findViewById(R.id.controls)));
593 BackScrollManager.bind(
594 new ScrollableHeader() {
595 private View mControls = findViewById(R.id.controls);
596 private View mPhoto = findViewById(R.id.contact_background_sizer);
597 private View mHeader = findViewById(R.id.photo_text_bar);
Yorke Leeac09b512013-09-09 08:51:54 -0700598 private View mSeparator = findViewById(R.id.separator);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700599
600 @Override
601 public void setOffset(int offset) {
602 mControls.setY(-offset);
603 }
604
605 @Override
606 public int getMaximumScrollableHeaderOffset() {
607 // We can scroll the photo out, but we should keep the header if
608 // present.
609 if (mHeader.getVisibility() == View.VISIBLE) {
610 return mPhoto.getHeight() - mHeader.getHeight();
611 } else {
612 // If the header is not present, we should also scroll out the
613 // separator line.
614 return mPhoto.getHeight() + mSeparator.getHeight();
615 }
616 }
617 },
618 historyList);
619 loadContactPhotos(photoUri);
620 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
621 }
622 }
623 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
624 }
625
Yorke Lee03459442013-10-30 18:29:32 -0700626 private void bindContactPhotoAction(final Intent actionIntent, int actionIcon,
627 String actionDescription) {
628 if (actionIntent == null) {
629 mMainActionView.setVisibility(View.INVISIBLE);
630 mMainActionPushLayerView.setVisibility(View.GONE);
631 mHeaderTextView.setVisibility(View.INVISIBLE);
632 mHeaderOverlayView.setVisibility(View.INVISIBLE);
633 } else {
634 mMainActionView.setVisibility(View.VISIBLE);
635 mMainActionView.setImageResource(actionIcon);
636 mMainActionPushLayerView.setVisibility(View.VISIBLE);
637 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
638 @Override
639 public void onClick(View v) {
640 startActivity(actionIntent);
641 }
642 });
643 mMainActionPushLayerView.setContentDescription(actionDescription);
644 mHeaderTextView.setVisibility(View.VISIBLE);
645 mHeaderOverlayView.setVisibility(View.VISIBLE);
646 }
647 }
648
Chiao Cheng94b10b52012-08-17 16:59:12 -0700649 /** Return the phone call details for a given call log URI. */
650 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
651 ContentResolver resolver = getContentResolver();
652 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
653 try {
654 if (callCursor == null || !callCursor.moveToFirst()) {
655 throw new IllegalArgumentException("Cannot find content: " + callUri);
656 }
657
658 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700659 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
660 final int numberPresentation = callCursor.getInt(
661 NUMBER_PRESENTATION_COLUMN_INDEX);
662 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
663 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
664 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700665 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
666 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
667
668 if (TextUtils.isEmpty(countryIso)) {
669 countryIso = mDefaultCountryIso;
670 }
671
672 // Formatted phone number.
673 final CharSequence formattedNumber;
674 // Read contact specifics.
675 final CharSequence nameText;
676 final int numberType;
677 final CharSequence numberLabel;
678 final Uri photoUri;
679 final Uri lookupUri;
680 // If this is not a regular number, there is no point in looking it up in the contacts.
681 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700682 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700683 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700684 ? mContactInfoHelper.lookupNumber(number, countryIso)
685 : null;
686 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700687 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
688 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700689 nameText = "";
690 numberType = 0;
691 numberLabel = "";
692 photoUri = null;
693 lookupUri = null;
694 } else {
695 formattedNumber = info.formattedNumber;
696 nameText = info.name;
697 numberType = info.type;
698 numberLabel = info.label;
699 photoUri = info.photoUri;
700 lookupUri = info.lookupUri;
701 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700702 return new PhoneCallDetails(number, numberPresentation,
703 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700704 new int[]{ callType }, date, duration,
705 nameText, numberType, numberLabel, lookupUri, photoUri);
706 } finally {
707 if (callCursor != null) {
708 callCursor.close();
709 }
710 }
711 }
712
713 /** Load the contact photos and places them in the corresponding views. */
714 private void loadContactPhotos(Uri photoUri) {
715 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri,
716 mContactBackgroundView.getWidth(), true);
717 }
718
719 static final class ViewEntry {
720 public final String text;
721 public final Intent primaryIntent;
722 /** The description for accessibility of the primary action. */
723 public final String primaryDescription;
724
725 public CharSequence label = null;
726 /** Icon for the secondary action. */
727 public int secondaryIcon = 0;
728 /** Intent for the secondary action. If not null, an icon must be defined. */
729 public Intent secondaryIntent = null;
730 /** The description for accessibility of the secondary action. */
731 public String secondaryDescription = null;
732
733 public ViewEntry(String text, Intent intent, String description) {
734 this.text = text;
735 primaryIntent = intent;
736 primaryDescription = description;
737 }
738
739 public void setSecondaryAction(int icon, Intent intent, String description) {
740 secondaryIcon = icon;
741 secondaryIntent = intent;
742 secondaryDescription = description;
743 }
744 }
745
746 /** Disables the call button area, e.g., for private numbers. */
747 private void disableCallButton() {
748 findViewById(R.id.call_and_sms).setVisibility(View.GONE);
749 }
750
751 /** Configures the call button area using the given entry. */
752 private void configureCallButton(ViewEntry entry) {
753 View convertView = findViewById(R.id.call_and_sms);
754 convertView.setVisibility(View.VISIBLE);
755
756 ImageView icon = (ImageView) convertView.findViewById(R.id.call_and_sms_icon);
757 View divider = convertView.findViewById(R.id.call_and_sms_divider);
758 TextView text = (TextView) convertView.findViewById(R.id.call_and_sms_text);
759
760 View mainAction = convertView.findViewById(R.id.call_and_sms_main_action);
761 mainAction.setOnClickListener(mPrimaryActionListener);
762 mainAction.setTag(entry);
763 mainAction.setContentDescription(entry.primaryDescription);
764 mainAction.setOnLongClickListener(mPrimaryLongClickListener);
765
766 if (entry.secondaryIntent != null) {
767 icon.setOnClickListener(mSecondaryActionListener);
768 icon.setImageResource(entry.secondaryIcon);
769 icon.setVisibility(View.VISIBLE);
770 icon.setTag(entry);
771 icon.setContentDescription(entry.secondaryDescription);
772 divider.setVisibility(View.VISIBLE);
773 } else {
774 icon.setVisibility(View.GONE);
775 divider.setVisibility(View.GONE);
776 }
777 text.setText(entry.text);
778
779 TextView label = (TextView) convertView.findViewById(R.id.call_and_sms_label);
780 if (TextUtils.isEmpty(entry.label)) {
781 label.setVisibility(View.GONE);
782 } else {
783 label.setText(entry.label);
784 label.setVisibility(View.VISIBLE);
785 }
786 }
787
788 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
789 if (statusCursor == null) {
790 mStatusMessageView.setVisibility(View.GONE);
791 return;
792 }
793 final StatusMessage message = getStatusMessage(statusCursor);
794 if (message == null || !message.showInCallDetails()) {
795 mStatusMessageView.setVisibility(View.GONE);
796 return;
797 }
798
799 mStatusMessageView.setVisibility(View.VISIBLE);
800 mStatusMessageText.setText(message.callDetailsMessageId);
801 if (message.actionMessageId != -1) {
802 mStatusMessageAction.setText(message.actionMessageId);
803 }
804 if (message.actionUri != null) {
805 mStatusMessageAction.setClickable(true);
806 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
807 @Override
808 public void onClick(View v) {
809 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
810 }
811 });
812 } else {
813 mStatusMessageAction.setClickable(false);
814 }
815 }
816
817 private StatusMessage getStatusMessage(Cursor statusCursor) {
818 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
819 if (messages.size() == 0) {
820 return null;
821 }
822 // There can only be a single status message per source package, so num of messages can
823 // at most be 1.
824 if (messages.size() > 1) {
825 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
826 " Will use the first one.", messages.size()));
827 }
828 return messages.get(0);
829 }
830
831 @Override
832 public boolean onCreateOptionsMenu(Menu menu) {
833 getMenuInflater().inflate(R.menu.call_details_options, menu);
834 return super.onCreateOptionsMenu(menu);
835 }
836
837 @Override
838 public boolean onPrepareOptionsMenu(Menu menu) {
839 // This action deletes all elements in the group from the call log.
840 // We don't have this action for voicemails, because you can just use the trash button.
841 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
842 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
843 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
844 return super.onPrepareOptionsMenu(menu);
845 }
846
Chiao Cheng94b10b52012-08-17 16:59:12 -0700847 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
848 final StringBuilder callIds = new StringBuilder();
849 for (Uri callUri : getCallLogEntryUris()) {
850 if (callIds.length() != 0) {
851 callIds.append(",");
852 }
853 callIds.append(ContentUris.parseId(callUri));
854 }
855 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
856 new AsyncTask<Void, Void, Void>() {
857 @Override
858 public Void doInBackground(Void... params) {
859 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
860 Calls._ID + " IN (" + callIds + ")", null);
861 return null;
862 }
863
864 @Override
865 public void onPostExecute(Void result) {
866 finish();
867 }
868 });
869 }
870
871 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700872 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700873 }
874
875 public void onMenuTrashVoicemail(MenuItem menuItem) {
876 final Uri voicemailUri = getVoicemailUri();
877 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
878 new AsyncTask<Void, Void, Void>() {
879 @Override
880 public Void doInBackground(Void... params) {
881 getContentResolver().delete(voicemailUri, null, null);
882 return null;
883 }
884 @Override
885 public void onPostExecute(Void result) {
886 finish();
887 }
888 });
889 }
890
Chiao Cheng94b10b52012-08-17 16:59:12 -0700891 /** Invoked when the user presses the home button in the action bar. */
892 private void onHomeSelected() {
893 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
894 // This will open the call log even if the detail view has been opened directly.
895 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
896 startActivity(intent);
897 finish();
898 }
899
900 @Override
901 protected void onPause() {
902 // Immediately stop the proximity sensor.
903 disableProximitySensor(false);
904 mProximitySensorListener.clearPendingRequests();
905 super.onPause();
906 }
907
908 @Override
909 public void enableProximitySensor() {
910 mProximitySensorManager.enable();
911 }
912
913 @Override
914 public void disableProximitySensor(boolean waitForFarState) {
915 mProximitySensorManager.disable(waitForFarState);
916 }
917
918 /**
919 * If the phone number is selected, unselect it and return {@code true}.
920 * Otherwise, just {@code false}.
921 */
922 private boolean finishPhoneNumerSelectedActionModeIfShown() {
923 if (mPhoneNumberActionMode == null) return false;
924 mPhoneNumberActionMode.finish();
925 return true;
926 }
927
928 private void startPhoneNumberSelectedActionMode(View targetView) {
929 mPhoneNumberActionMode = startActionMode(new PhoneNumberActionModeCallback(targetView));
930 }
931
932 private void closeSystemDialogs() {
933 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
934 }
935
936 private class PhoneNumberActionModeCallback implements ActionMode.Callback {
937 private final View mTargetView;
938 private final Drawable mOriginalViewBackground;
939
940 public PhoneNumberActionModeCallback(View targetView) {
941 mTargetView = targetView;
942
943 // Highlight the phone number view. Remember the old background, and put a new one.
944 mOriginalViewBackground = mTargetView.getBackground();
945 mTargetView.setBackgroundColor(getResources().getColor(R.color.item_selected));
946 }
947
948 @Override
949 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
950 if (TextUtils.isEmpty(mPhoneNumberToCopy)) return false;
951
952 getMenuInflater().inflate(R.menu.call_details_cab, menu);
953 return true;
954 }
955
956 @Override
957 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
958 return true;
959 }
960
961 @Override
962 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
963 switch (item.getItemId()) {
964 case R.id.copy_phone_number:
965 ClipboardUtils.copyText(CallDetailActivity.this, mPhoneNumberLabelToCopy,
966 mPhoneNumberToCopy, true);
967 mode.finish(); // Close the CAB
968 return true;
969 }
970 return false;
971 }
972
973 @Override
974 public void onDestroyActionMode(ActionMode mode) {
975 mPhoneNumberActionMode = null;
976
977 // Restore the view background.
978 mTargetView.setBackground(mOriginalViewBackground);
979 }
980 }
Chiao Cheng35071c02012-10-15 18:36:24 -0700981
982 /** Returns the given text, forced to be left-to-right. */
983 private static CharSequence forceLeftToRight(CharSequence text) {
984 StringBuilder sb = new StringBuilder();
985 sb.append(LEFT_TO_RIGHT_EMBEDDING);
986 sb.append(text);
987 sb.append(POP_DIRECTIONAL_FORMATTING);
988 return sb.toString();
989 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700990}