blob: f4ca21305b6ef1013d6498adce561dfa827f9e8f [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
54import com.android.contacts.BackScrollManager;
55import com.android.contacts.BackScrollManager.ScrollableHeader;
56import com.android.contacts.ContactPhotoManager;
57import com.android.contacts.ContactsUtils;
58import com.android.contacts.ProximitySensorAware;
59import com.android.contacts.ProximitySensorManager;
60import com.android.contacts.R;
61import 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;
66import com.android.contacts.format.FormatUtils;
67import com.android.contacts.util.AsyncTaskExecutor;
68import com.android.contacts.util.AsyncTaskExecutors;
69import com.android.contacts.util.ClipboardUtils;
70import com.android.contacts.util.Constants;
71import com.android.dialer.voicemail.VoicemailPlaybackFragment;
72import com.android.dialer.voicemail.VoicemailStatusHelper;
73import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
74import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
75
76import java.util.List;
77
78/**
79 * Displays the details of a specific call log entry.
80 * <p>
81 * This activity can be either started with the URI of a single call log entry, or with the
82 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
83 */
84public class CallDetailActivity extends Activity implements ProximitySensorAware {
85 private static final String TAG = "CallDetail";
86
87 /** 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,
213 };
214
215 static final int DATE_COLUMN_INDEX = 0;
216 static final int DURATION_COLUMN_INDEX = 1;
217 static final int NUMBER_COLUMN_INDEX = 2;
218 static final int CALL_TYPE_COLUMN_INDEX = 3;
219 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
220 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
221
222 private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
223 @Override
224 public void onClick(View view) {
225 if (finishPhoneNumerSelectedActionModeIfShown()) {
226 return;
227 }
228 startActivity(((ViewEntry) view.getTag()).primaryIntent);
229 }
230 };
231
232 private final View.OnClickListener mSecondaryActionListener = new View.OnClickListener() {
233 @Override
234 public void onClick(View view) {
235 if (finishPhoneNumerSelectedActionModeIfShown()) {
236 return;
237 }
238 startActivity(((ViewEntry) view.getTag()).secondaryIntent);
239 }
240 };
241
242 private final View.OnLongClickListener mPrimaryLongClickListener =
243 new View.OnLongClickListener() {
244 @Override
245 public boolean onLongClick(View v) {
246 if (finishPhoneNumerSelectedActionModeIfShown()) {
247 return true;
248 }
249 startPhoneNumberSelectedActionMode(v);
250 return true;
251 }
252 };
253
254 @Override
255 protected void onCreate(Bundle icicle) {
256 super.onCreate(icicle);
257
258 setContentView(R.layout.call_detail);
259
260 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
261 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
262 mResources = getResources();
263
264 mCallTypeHelper = new CallTypeHelper(getResources());
265 mPhoneNumberHelper = new PhoneNumberHelper(mResources);
266 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
267 mPhoneNumberHelper);
268 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
269 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
270 mHeaderTextView = (TextView) findViewById(R.id.header_text);
271 mHeaderOverlayView = findViewById(R.id.photo_text_bar);
272 mStatusMessageView = findViewById(R.id.voicemail_status);
273 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
274 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
275 mMainActionView = (ImageView) findViewById(R.id.main_action);
276 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
277 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
278 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
279 mContactPhotoManager = ContactPhotoManager.getInstance(this);
280 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
281 mContactInfoHelper = new ContactInfoHelper(this, ContactsUtils.getCurrentCountryIso(this));
282 configureActionBar();
283 optionallyHandleVoicemail();
284 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
285 closeSystemDialogs();
286 }
287 }
288
289 @Override
290 public void onResume() {
291 super.onResume();
292 updateData(getCallLogEntryUris());
293 }
294
295 /**
296 * Handle voicemail playback or hide voicemail ui.
297 * <p>
298 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
299 * playback. If it doesn't, then hide the voicemail ui.
300 */
301 private void optionallyHandleVoicemail() {
302 View voicemailContainer = findViewById(R.id.voicemail_container);
303 if (hasVoicemail()) {
304 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
305 // to play and optionally start the playback.
306 // Do a query to fetch the voicemail status messages.
307 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
308 Bundle fragmentArguments = new Bundle();
309 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
310 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
311 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
312 }
313 playbackFragment.setArguments(fragmentArguments);
314 voicemailContainer.setVisibility(View.VISIBLE);
315 getFragmentManager().beginTransaction()
316 .add(R.id.voicemail_container, playbackFragment).commitAllowingStateLoss();
317 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
318 markVoicemailAsRead(getVoicemailUri());
319 } else {
320 // No voicemail uri: hide the status view.
321 mStatusMessageView.setVisibility(View.GONE);
322 voicemailContainer.setVisibility(View.GONE);
323 }
324 }
325
326 private boolean hasVoicemail() {
327 return getVoicemailUri() != null;
328 }
329
330 private Uri getVoicemailUri() {
331 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
332 }
333
334 private void markVoicemailAsRead(final Uri voicemailUri) {
335 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
336 @Override
337 public Void doInBackground(Void... params) {
338 ContentValues values = new ContentValues();
339 values.put(Voicemails.IS_READ, true);
340 getContentResolver().update(voicemailUri, values,
341 Voicemails.IS_READ + " = 0", null);
342 return null;
343 }
344 });
345 }
346
347 /**
348 * Returns the list of URIs to show.
349 * <p>
350 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
351 * a list of ids in the call log added as an extra on the URI.
352 * <p>
353 * If both are available, the data on the intent takes precedence.
354 */
355 private Uri[] getCallLogEntryUris() {
356 Uri uri = getIntent().getData();
357 if (uri != null) {
358 // If there is a data on the intent, it takes precedence over the extra.
359 return new Uri[]{ uri };
360 }
361 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
362 Uri[] uris = new Uri[ids.length];
363 for (int index = 0; index < ids.length; ++index) {
364 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
365 }
366 return uris;
367 }
368
369 @Override
370 public boolean onKeyDown(int keyCode, KeyEvent event) {
371 switch (keyCode) {
372 case KeyEvent.KEYCODE_CALL: {
373 // Make sure phone isn't already busy before starting direct call
374 TelephonyManager tm = (TelephonyManager)
375 getSystemService(Context.TELEPHONY_SERVICE);
376 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
377 startActivity(ContactsUtils.getCallIntent(
378 Uri.fromParts(Constants.SCHEME_TEL, mNumber, null)));
379 return true;
380 }
381 }
382 }
383
384 return super.onKeyDown(keyCode, event);
385 }
386
387 /**
388 * Update user interface with details of given call.
389 *
390 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
391 */
392 private void updateData(final Uri... callUris) {
393 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
394 @Override
395 public PhoneCallDetails[] doInBackground(Void... params) {
396 // TODO: All phone calls correspond to the same person, so we can make a single
397 // lookup.
398 final int numCalls = callUris.length;
399 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
400 try {
401 for (int index = 0; index < numCalls; ++index) {
402 details[index] = getPhoneCallDetailsForUri(callUris[index]);
403 }
404 return details;
405 } catch (IllegalArgumentException e) {
406 // Something went wrong reading in our primary data.
407 Log.w(TAG, "invalid URI starting call details", e);
408 return null;
409 }
410 }
411
412 @Override
413 public void onPostExecute(PhoneCallDetails[] details) {
414 if (details == null) {
415 // Somewhere went wrong: we're going to bail out and show error to users.
416 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
417 Toast.LENGTH_SHORT).show();
418 finish();
419 return;
420 }
421
422 // We know that all calls are from the same number and the same contact, so pick the
423 // first.
424 PhoneCallDetails firstDetails = details[0];
425 mNumber = firstDetails.number.toString();
426 final Uri contactUri = firstDetails.contactUri;
427 final Uri photoUri = firstDetails.photoUri;
428
429 // Set the details header, based on the first phone call.
430 mPhoneCallDetailsHelper.setCallDetailsHeader(mHeaderTextView, firstDetails);
431
432 // Cache the details about the phone number.
433 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
434 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
435 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
436
437 // Let user view contact details if they exist, otherwise add option to create new
438 // contact from this number.
439 final Intent mainActionIntent;
440 final int mainActionIcon;
441 final String mainActionDescription;
442
443 final CharSequence nameOrNumber;
444 if (!TextUtils.isEmpty(firstDetails.name)) {
445 nameOrNumber = firstDetails.name;
446 } else {
447 nameOrNumber = firstDetails.number;
448 }
449
450 if (contactUri != null) {
451 mainActionIntent = new Intent(Intent.ACTION_VIEW, contactUri);
452 // This will launch People's detail contact screen, so we probably want to
453 // treat it as a separate People task.
454 mainActionIntent.setFlags(
455 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
456 mainActionIcon = R.drawable.ic_contacts_holo_dark;
457 mainActionDescription =
458 getString(R.string.description_view_contact, nameOrNumber);
459 } else if (isVoicemailNumber) {
460 mainActionIntent = null;
461 mainActionIcon = 0;
462 mainActionDescription = null;
463 } else if (isSipNumber) {
464 // TODO: This item is currently disabled for SIP addresses, because
465 // the Insert.PHONE extra only works correctly for PSTN numbers.
466 //
467 // To fix this for SIP addresses, we need to:
468 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
469 // the current number is a SIP address
470 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
471 // updating the SipAddress field
472 // and then we can remove the "!isSipNumber" check above.
473 mainActionIntent = null;
474 mainActionIcon = 0;
475 mainActionDescription = null;
476 } else if (canPlaceCallsTo) {
477 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
478 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
479 mainActionIntent.putExtra(Insert.PHONE, mNumber);
480 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
481 mainActionDescription = getString(R.string.description_add_contact);
482 } else {
483 // If we cannot call the number, when we probably cannot add it as a contact either.
484 // This is usually the case of private, unknown, or payphone numbers.
485 mainActionIntent = null;
486 mainActionIcon = 0;
487 mainActionDescription = null;
488 }
489
490 if (mainActionIntent == null) {
491 mMainActionView.setVisibility(View.INVISIBLE);
492 mMainActionPushLayerView.setVisibility(View.GONE);
493 mHeaderTextView.setVisibility(View.INVISIBLE);
494 mHeaderOverlayView.setVisibility(View.INVISIBLE);
495 } else {
496 mMainActionView.setVisibility(View.VISIBLE);
497 mMainActionView.setImageResource(mainActionIcon);
498 mMainActionPushLayerView.setVisibility(View.VISIBLE);
499 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
500 @Override
501 public void onClick(View v) {
502 startActivity(mainActionIntent);
503 }
504 });
505 mMainActionPushLayerView.setContentDescription(mainActionDescription);
506 mHeaderTextView.setVisibility(View.VISIBLE);
507 mHeaderOverlayView.setVisibility(View.VISIBLE);
508 }
509
510 // This action allows to call the number that places the call.
511 if (canPlaceCallsTo) {
512 final CharSequence displayNumber =
513 mPhoneNumberHelper.getDisplayNumber(
514 firstDetails.number, firstDetails.formattedNumber);
515
516 ViewEntry entry = new ViewEntry(
517 getString(R.string.menu_callNumber,
518 FormatUtils.forceLeftToRight(displayNumber)),
519 ContactsUtils.getCallIntent(mNumber),
520 getString(R.string.description_call, nameOrNumber));
521
522 // Only show a label if the number is shown and it is not a SIP address.
523 if (!TextUtils.isEmpty(firstDetails.name)
524 && !TextUtils.isEmpty(firstDetails.number)
525 && !PhoneNumberUtils.isUriNumber(firstDetails.number.toString())) {
526 entry.label = Phone.getTypeLabel(mResources, firstDetails.numberType,
527 firstDetails.numberLabel);
528 }
529
530 // The secondary action allows to send an SMS to the number that placed the
531 // call.
532 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
533 entry.setSecondaryAction(
534 R.drawable.ic_text_holo_dark,
535 new Intent(Intent.ACTION_SENDTO,
536 Uri.fromParts("sms", mNumber, null)),
537 getString(R.string.description_send_text_message, nameOrNumber));
538 }
539
540 configureCallButton(entry);
541 mPhoneNumberToCopy = displayNumber;
542 mPhoneNumberLabelToCopy = entry.label;
543 } else {
544 disableCallButton();
545 mPhoneNumberToCopy = null;
546 mPhoneNumberLabelToCopy = null;
547 }
548
549 mHasEditNumberBeforeCallOption =
550 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
551 mHasTrashOption = hasVoicemail();
552 mHasRemoveFromCallLogOption = !hasVoicemail();
553 invalidateOptionsMenu();
554
555 ListView historyList = (ListView) findViewById(R.id.history);
556 historyList.setAdapter(
557 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
558 mCallTypeHelper, details, hasVoicemail(), canPlaceCallsTo,
559 findViewById(R.id.controls)));
560 BackScrollManager.bind(
561 new ScrollableHeader() {
562 private View mControls = findViewById(R.id.controls);
563 private View mPhoto = findViewById(R.id.contact_background_sizer);
564 private View mHeader = findViewById(R.id.photo_text_bar);
565 private View mSeparator = findViewById(R.id.blue_separator);
566
567 @Override
568 public void setOffset(int offset) {
569 mControls.setY(-offset);
570 }
571
572 @Override
573 public int getMaximumScrollableHeaderOffset() {
574 // We can scroll the photo out, but we should keep the header if
575 // present.
576 if (mHeader.getVisibility() == View.VISIBLE) {
577 return mPhoto.getHeight() - mHeader.getHeight();
578 } else {
579 // If the header is not present, we should also scroll out the
580 // separator line.
581 return mPhoto.getHeight() + mSeparator.getHeight();
582 }
583 }
584 },
585 historyList);
586 loadContactPhotos(photoUri);
587 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
588 }
589 }
590 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
591 }
592
593 /** Return the phone call details for a given call log URI. */
594 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
595 ContentResolver resolver = getContentResolver();
596 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
597 try {
598 if (callCursor == null || !callCursor.moveToFirst()) {
599 throw new IllegalArgumentException("Cannot find content: " + callUri);
600 }
601
602 // Read call log specifics.
603 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
604 long date = callCursor.getLong(DATE_COLUMN_INDEX);
605 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
606 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
607 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
608 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
609
610 if (TextUtils.isEmpty(countryIso)) {
611 countryIso = mDefaultCountryIso;
612 }
613
614 // Formatted phone number.
615 final CharSequence formattedNumber;
616 // Read contact specifics.
617 final CharSequence nameText;
618 final int numberType;
619 final CharSequence numberLabel;
620 final Uri photoUri;
621 final Uri lookupUri;
622 // If this is not a regular number, there is no point in looking it up in the contacts.
623 ContactInfo info =
624 mPhoneNumberHelper.canPlaceCallsTo(number)
625 && !mPhoneNumberHelper.isVoicemailNumber(number)
626 ? mContactInfoHelper.lookupNumber(number, countryIso)
627 : null;
628 if (info == null) {
629 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number, null);
630 nameText = "";
631 numberType = 0;
632 numberLabel = "";
633 photoUri = null;
634 lookupUri = null;
635 } else {
636 formattedNumber = info.formattedNumber;
637 nameText = info.name;
638 numberType = info.type;
639 numberLabel = info.label;
640 photoUri = info.photoUri;
641 lookupUri = info.lookupUri;
642 }
643 return new PhoneCallDetails(number, formattedNumber, countryIso, geocode,
644 new int[]{ callType }, date, duration,
645 nameText, numberType, numberLabel, lookupUri, photoUri);
646 } finally {
647 if (callCursor != null) {
648 callCursor.close();
649 }
650 }
651 }
652
653 /** Load the contact photos and places them in the corresponding views. */
654 private void loadContactPhotos(Uri photoUri) {
655 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri,
656 mContactBackgroundView.getWidth(), true);
657 }
658
659 static final class ViewEntry {
660 public final String text;
661 public final Intent primaryIntent;
662 /** The description for accessibility of the primary action. */
663 public final String primaryDescription;
664
665 public CharSequence label = null;
666 /** Icon for the secondary action. */
667 public int secondaryIcon = 0;
668 /** Intent for the secondary action. If not null, an icon must be defined. */
669 public Intent secondaryIntent = null;
670 /** The description for accessibility of the secondary action. */
671 public String secondaryDescription = null;
672
673 public ViewEntry(String text, Intent intent, String description) {
674 this.text = text;
675 primaryIntent = intent;
676 primaryDescription = description;
677 }
678
679 public void setSecondaryAction(int icon, Intent intent, String description) {
680 secondaryIcon = icon;
681 secondaryIntent = intent;
682 secondaryDescription = description;
683 }
684 }
685
686 /** Disables the call button area, e.g., for private numbers. */
687 private void disableCallButton() {
688 findViewById(R.id.call_and_sms).setVisibility(View.GONE);
689 }
690
691 /** Configures the call button area using the given entry. */
692 private void configureCallButton(ViewEntry entry) {
693 View convertView = findViewById(R.id.call_and_sms);
694 convertView.setVisibility(View.VISIBLE);
695
696 ImageView icon = (ImageView) convertView.findViewById(R.id.call_and_sms_icon);
697 View divider = convertView.findViewById(R.id.call_and_sms_divider);
698 TextView text = (TextView) convertView.findViewById(R.id.call_and_sms_text);
699
700 View mainAction = convertView.findViewById(R.id.call_and_sms_main_action);
701 mainAction.setOnClickListener(mPrimaryActionListener);
702 mainAction.setTag(entry);
703 mainAction.setContentDescription(entry.primaryDescription);
704 mainAction.setOnLongClickListener(mPrimaryLongClickListener);
705
706 if (entry.secondaryIntent != null) {
707 icon.setOnClickListener(mSecondaryActionListener);
708 icon.setImageResource(entry.secondaryIcon);
709 icon.setVisibility(View.VISIBLE);
710 icon.setTag(entry);
711 icon.setContentDescription(entry.secondaryDescription);
712 divider.setVisibility(View.VISIBLE);
713 } else {
714 icon.setVisibility(View.GONE);
715 divider.setVisibility(View.GONE);
716 }
717 text.setText(entry.text);
718
719 TextView label = (TextView) convertView.findViewById(R.id.call_and_sms_label);
720 if (TextUtils.isEmpty(entry.label)) {
721 label.setVisibility(View.GONE);
722 } else {
723 label.setText(entry.label);
724 label.setVisibility(View.VISIBLE);
725 }
726 }
727
728 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
729 if (statusCursor == null) {
730 mStatusMessageView.setVisibility(View.GONE);
731 return;
732 }
733 final StatusMessage message = getStatusMessage(statusCursor);
734 if (message == null || !message.showInCallDetails()) {
735 mStatusMessageView.setVisibility(View.GONE);
736 return;
737 }
738
739 mStatusMessageView.setVisibility(View.VISIBLE);
740 mStatusMessageText.setText(message.callDetailsMessageId);
741 if (message.actionMessageId != -1) {
742 mStatusMessageAction.setText(message.actionMessageId);
743 }
744 if (message.actionUri != null) {
745 mStatusMessageAction.setClickable(true);
746 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
747 @Override
748 public void onClick(View v) {
749 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
750 }
751 });
752 } else {
753 mStatusMessageAction.setClickable(false);
754 }
755 }
756
757 private StatusMessage getStatusMessage(Cursor statusCursor) {
758 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
759 if (messages.size() == 0) {
760 return null;
761 }
762 // There can only be a single status message per source package, so num of messages can
763 // at most be 1.
764 if (messages.size() > 1) {
765 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
766 " Will use the first one.", messages.size()));
767 }
768 return messages.get(0);
769 }
770
771 @Override
772 public boolean onCreateOptionsMenu(Menu menu) {
773 getMenuInflater().inflate(R.menu.call_details_options, menu);
774 return super.onCreateOptionsMenu(menu);
775 }
776
777 @Override
778 public boolean onPrepareOptionsMenu(Menu menu) {
779 // This action deletes all elements in the group from the call log.
780 // We don't have this action for voicemails, because you can just use the trash button.
781 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
782 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
783 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
784 return super.onPrepareOptionsMenu(menu);
785 }
786
787 @Override
788 public boolean onMenuItemSelected(int featureId, MenuItem item) {
789 switch (item.getItemId()) {
790 case android.R.id.home: {
791 onHomeSelected();
792 return true;
793 }
794
795 // All the options menu items are handled by onMenu... methods.
796 default:
797 throw new IllegalArgumentException();
798 }
799 }
800
801 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
802 final StringBuilder callIds = new StringBuilder();
803 for (Uri callUri : getCallLogEntryUris()) {
804 if (callIds.length() != 0) {
805 callIds.append(",");
806 }
807 callIds.append(ContentUris.parseId(callUri));
808 }
809 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
810 new AsyncTask<Void, Void, Void>() {
811 @Override
812 public Void doInBackground(Void... params) {
813 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
814 Calls._ID + " IN (" + callIds + ")", null);
815 return null;
816 }
817
818 @Override
819 public void onPostExecute(Void result) {
820 finish();
821 }
822 });
823 }
824
825 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
826 startActivity(new Intent(Intent.ACTION_DIAL, ContactsUtils.getCallUri(mNumber)));
827 }
828
829 public void onMenuTrashVoicemail(MenuItem menuItem) {
830 final Uri voicemailUri = getVoicemailUri();
831 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
832 new AsyncTask<Void, Void, Void>() {
833 @Override
834 public Void doInBackground(Void... params) {
835 getContentResolver().delete(voicemailUri, null, null);
836 return null;
837 }
838 @Override
839 public void onPostExecute(Void result) {
840 finish();
841 }
842 });
843 }
844
845 private void configureActionBar() {
846 ActionBar actionBar = getActionBar();
847 if (actionBar != null) {
848 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
849 }
850 }
851
852 /** Invoked when the user presses the home button in the action bar. */
853 private void onHomeSelected() {
854 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
855 // This will open the call log even if the detail view has been opened directly.
856 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
857 startActivity(intent);
858 finish();
859 }
860
861 @Override
862 protected void onPause() {
863 // Immediately stop the proximity sensor.
864 disableProximitySensor(false);
865 mProximitySensorListener.clearPendingRequests();
866 super.onPause();
867 }
868
869 @Override
870 public void enableProximitySensor() {
871 mProximitySensorManager.enable();
872 }
873
874 @Override
875 public void disableProximitySensor(boolean waitForFarState) {
876 mProximitySensorManager.disable(waitForFarState);
877 }
878
879 /**
880 * If the phone number is selected, unselect it and return {@code true}.
881 * Otherwise, just {@code false}.
882 */
883 private boolean finishPhoneNumerSelectedActionModeIfShown() {
884 if (mPhoneNumberActionMode == null) return false;
885 mPhoneNumberActionMode.finish();
886 return true;
887 }
888
889 private void startPhoneNumberSelectedActionMode(View targetView) {
890 mPhoneNumberActionMode = startActionMode(new PhoneNumberActionModeCallback(targetView));
891 }
892
893 private void closeSystemDialogs() {
894 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
895 }
896
897 private class PhoneNumberActionModeCallback implements ActionMode.Callback {
898 private final View mTargetView;
899 private final Drawable mOriginalViewBackground;
900
901 public PhoneNumberActionModeCallback(View targetView) {
902 mTargetView = targetView;
903
904 // Highlight the phone number view. Remember the old background, and put a new one.
905 mOriginalViewBackground = mTargetView.getBackground();
906 mTargetView.setBackgroundColor(getResources().getColor(R.color.item_selected));
907 }
908
909 @Override
910 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
911 if (TextUtils.isEmpty(mPhoneNumberToCopy)) return false;
912
913 getMenuInflater().inflate(R.menu.call_details_cab, menu);
914 return true;
915 }
916
917 @Override
918 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
919 return true;
920 }
921
922 @Override
923 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
924 switch (item.getItemId()) {
925 case R.id.copy_phone_number:
926 ClipboardUtils.copyText(CallDetailActivity.this, mPhoneNumberLabelToCopy,
927 mPhoneNumberToCopy, true);
928 mode.finish(); // Close the CAB
929 return true;
930 }
931 return false;
932 }
933
934 @Override
935 public void onDestroyActionMode(ActionMode mode) {
936 mPhoneNumberActionMode = null;
937
938 // Restore the view background.
939 mTargetView.setBackground(mOriginalViewBackground);
940 }
941 }
942}