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