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