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