blob: 695f8aa0102a85235ed350706e7342a79ce2dee1 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Chiao Cheng94b10b52012-08-17 16:59:12 -070019import android.app.Activity;
20import android.content.ContentResolver;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.Intent;
25import android.content.res.Resources;
26import android.database.Cursor;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.net.Uri;
28import android.os.AsyncTask;
29import android.os.Bundle;
30import android.provider.CallLog;
31import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.provider.VoicemailContract.Voicemails;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.telephony.TelephonyManager;
35import android.text.TextUtils;
36import android.util.Log;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.view.KeyEvent;
38import android.view.LayoutInflater;
39import android.view.Menu;
40import android.view.MenuItem;
41import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.widget.ListView;
Tyler Gunn18164c82014-06-09 10:20:57 -070043import android.widget.QuickContactBadge;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.widget.TextView;
45import android.widget.Toast;
46
Chiao Cheng35071c02012-10-15 18:36:24 -070047import com.android.contacts.common.ContactPhotoManager;
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070048import com.android.contacts.common.CallUtil;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080049import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070050import com.android.contacts.common.GeoUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import com.android.dialer.calllog.CallDetailHistoryAdapter;
52import com.android.dialer.calllog.CallTypeHelper;
53import com.android.dialer.calllog.ContactInfo;
54import com.android.dialer.calllog.ContactInfoHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -080055import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070056import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070057import com.android.dialer.util.AsyncTaskExecutor;
58import com.android.dialer.util.AsyncTaskExecutors;
Yorke Lee39213592014-04-07 15:39:48 -070059import com.android.dialer.util.DialerUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060import com.android.dialer.voicemail.VoicemailPlaybackFragment;
61import com.android.dialer.voicemail.VoicemailStatusHelper;
62import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
63import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
64
65import java.util.List;
66
67/**
68 * Displays the details of a specific call log entry.
69 * <p>
70 * This activity can be either started with the URI of a single call log entry, or with the
71 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
72 */
73public class CallDetailActivity extends Activity implements ProximitySensorAware {
74 private static final String TAG = "CallDetail";
75
Yorke Lee03459442013-10-30 18:29:32 -070076 private static final int LOADER_ID = 0;
77 private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
78
Chiao Cheng35071c02012-10-15 18:36:24 -070079 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
80 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
81
Chiao Cheng94b10b52012-08-17 16:59:12 -070082 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
83 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
84 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
85 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
86
87 /** The enumeration of {@link AsyncTask} objects used in this class. */
88 public enum Tasks {
89 MARK_VOICEMAIL_READ,
90 DELETE_VOICEMAIL_AND_FINISH,
91 REMOVE_FROM_CALL_LOG_AND_FINISH,
92 UPDATE_PHONE_CALL_DETAILS,
93 }
94
95 /** A long array extra containing ids of call log entries to display. */
96 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
97 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
98 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
99 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
100 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
101 /** If the activity was triggered from a notification. */
102 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
103
104 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800105 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700106 private QuickContactBadge mQuickContactBadge;
107 private TextView mCallerName;
108 private TextView mCallerNumber;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700109 private AsyncTaskExecutor mAsyncTaskExecutor;
110 private ContactInfoHelper mContactInfoHelper;
111
112 private String mNumber = null;
113 private String mDefaultCountryIso;
114
115 /* package */ LayoutInflater mInflater;
116 /* package */ Resources mResources;
117 /** Helper to load contact photos. */
118 private ContactPhotoManager mContactPhotoManager;
119 /** Helper to make async queries to content resolver. */
120 private CallDetailActivityQueryHandler mAsyncQueryHandler;
121 /** Helper to get voicemail status messages. */
122 private VoicemailStatusHelper mVoicemailStatusHelper;
123 // Views related to voicemail status message.
124 private View mStatusMessageView;
125 private TextView mStatusMessageText;
126 private TextView mStatusMessageAction;
127
128 /** Whether we should show "edit number before call" in the options menu. */
129 private boolean mHasEditNumberBeforeCallOption;
130 /** Whether we should show "trash" in the options menu. */
131 private boolean mHasTrashOption;
132 /** Whether we should show "remove from call log" in the options menu. */
133 private boolean mHasRemoveFromCallLogOption;
134
135 private ProximitySensorManager mProximitySensorManager;
136 private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
137
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138 /** Listener to changes in the proximity sensor state. */
139 private class ProximitySensorListener implements ProximitySensorManager.Listener {
140 /** Used to show a blank view and hide the action bar. */
141 private final Runnable mBlankRunnable = new Runnable() {
142 @Override
143 public void run() {
144 View blankView = findViewById(R.id.blank);
145 blankView.setVisibility(View.VISIBLE);
146 getActionBar().hide();
147 }
148 };
149 /** Used to remove the blank view and show the action bar. */
150 private final Runnable mUnblankRunnable = new Runnable() {
151 @Override
152 public void run() {
153 View blankView = findViewById(R.id.blank);
154 blankView.setVisibility(View.GONE);
155 getActionBar().show();
156 }
157 };
158
159 @Override
160 public synchronized void onNear() {
161 clearPendingRequests();
162 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
163 }
164
165 @Override
166 public synchronized void onFar() {
167 clearPendingRequests();
168 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
169 }
170
171 /** Removed any delayed requests that may be pending. */
172 public synchronized void clearPendingRequests() {
173 View blankView = findViewById(R.id.blank);
174 blankView.removeCallbacks(mBlankRunnable);
175 blankView.removeCallbacks(mUnblankRunnable);
176 }
177
178 /** Post a {@link Runnable} with a delay on the main thread. */
179 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
180 // Post these instead of executing immediately so that:
181 // - They are guaranteed to be executed on the main thread.
182 // - If the sensor values changes rapidly for some time, the UI will not be
183 // updated immediately.
184 View blankView = findViewById(R.id.blank);
185 blankView.postDelayed(runnable, delayMillis);
186 }
187 }
188
189 static final String[] CALL_LOG_PROJECTION = new String[] {
190 CallLog.Calls.DATE,
191 CallLog.Calls.DURATION,
192 CallLog.Calls.NUMBER,
193 CallLog.Calls.TYPE,
194 CallLog.Calls.COUNTRY_ISO,
195 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700196 CallLog.Calls.NUMBER_PRESENTATION,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700197 };
198
199 static final int DATE_COLUMN_INDEX = 0;
200 static final int DURATION_COLUMN_INDEX = 1;
201 static final int NUMBER_COLUMN_INDEX = 2;
202 static final int CALL_TYPE_COLUMN_INDEX = 3;
203 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
204 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700205 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700206
Chiao Cheng94b10b52012-08-17 16:59:12 -0700207 @Override
208 protected void onCreate(Bundle icicle) {
209 super.onCreate(icicle);
210
211 setContentView(R.layout.call_detail);
212
213 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
214 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
215 mResources = getResources();
216
217 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee24ec3192013-11-19 13:52:45 -0800218 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700219 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
220 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700221 mStatusMessageView = findViewById(R.id.voicemail_status);
222 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
223 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Tyler Gunn18164c82014-06-09 10:20:57 -0700224 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
225 mQuickContactBadge.setOverlay(null);
226 mCallerName = (TextView) findViewById(R.id.caller_name);
227 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Chiao Cheng35071c02012-10-15 18:36:24 -0700228 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700229 mContactPhotoManager = ContactPhotoManager.getInstance(this);
230 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700231 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700232 getActionBar().setDisplayHomeAsUpEnabled(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700233 optionallyHandleVoicemail();
234 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
235 closeSystemDialogs();
236 }
237 }
238
239 @Override
240 public void onResume() {
241 super.onResume();
242 updateData(getCallLogEntryUris());
243 }
244
245 /**
246 * Handle voicemail playback or hide voicemail ui.
247 * <p>
248 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
249 * playback. If it doesn't, then hide the voicemail ui.
250 */
251 private void optionallyHandleVoicemail() {
252 View voicemailContainer = findViewById(R.id.voicemail_container);
253 if (hasVoicemail()) {
254 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
255 // to play and optionally start the playback.
256 // Do a query to fetch the voicemail status messages.
257 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
258 Bundle fragmentArguments = new Bundle();
259 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
260 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
261 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
262 }
263 playbackFragment.setArguments(fragmentArguments);
264 voicemailContainer.setVisibility(View.VISIBLE);
265 getFragmentManager().beginTransaction()
Yorke Leeaa536fd2013-07-29 11:31:04 -0700266 .add(R.id.voicemail_container, playbackFragment)
267 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700268 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
269 markVoicemailAsRead(getVoicemailUri());
270 } else {
271 // No voicemail uri: hide the status view.
272 mStatusMessageView.setVisibility(View.GONE);
273 voicemailContainer.setVisibility(View.GONE);
274 }
275 }
276
277 private boolean hasVoicemail() {
278 return getVoicemailUri() != null;
279 }
280
281 private Uri getVoicemailUri() {
282 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
283 }
284
285 private void markVoicemailAsRead(final Uri voicemailUri) {
286 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
287 @Override
288 public Void doInBackground(Void... params) {
289 ContentValues values = new ContentValues();
290 values.put(Voicemails.IS_READ, true);
291 getContentResolver().update(voicemailUri, values,
292 Voicemails.IS_READ + " = 0", null);
293 return null;
294 }
295 });
296 }
297
298 /**
299 * Returns the list of URIs to show.
300 * <p>
301 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
302 * a list of ids in the call log added as an extra on the URI.
303 * <p>
304 * If both are available, the data on the intent takes precedence.
305 */
306 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700307 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700308 if (uri != null) {
309 // If there is a data on the intent, it takes precedence over the extra.
310 return new Uri[]{ uri };
311 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700312 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
313 final int numIds = ids == null ? 0 : ids.length;
314 final Uri[] uris = new Uri[numIds];
315 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
317 }
318 return uris;
319 }
320
321 @Override
322 public boolean onKeyDown(int keyCode, KeyEvent event) {
323 switch (keyCode) {
324 case KeyEvent.KEYCODE_CALL: {
325 // Make sure phone isn't already busy before starting direct call
326 TelephonyManager tm = (TelephonyManager)
327 getSystemService(Context.TELEPHONY_SERVICE);
328 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700329 DialerUtils.startActivityWithErrorToast(this,
330 CallUtil.getCallIntent(Uri.fromParts(CallUtil.SCHEME_TEL, mNumber,
331 null)),
332 R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700333 return true;
334 }
335 }
336 }
337
338 return super.onKeyDown(keyCode, event);
339 }
340
341 /**
342 * Update user interface with details of given call.
343 *
344 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
345 */
346 private void updateData(final Uri... callUris) {
347 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
348 @Override
349 public PhoneCallDetails[] doInBackground(Void... params) {
350 // TODO: All phone calls correspond to the same person, so we can make a single
351 // lookup.
352 final int numCalls = callUris.length;
353 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
354 try {
355 for (int index = 0; index < numCalls; ++index) {
356 details[index] = getPhoneCallDetailsForUri(callUris[index]);
357 }
358 return details;
359 } catch (IllegalArgumentException e) {
360 // Something went wrong reading in our primary data.
361 Log.w(TAG, "invalid URI starting call details", e);
362 return null;
363 }
364 }
365
366 @Override
367 public void onPostExecute(PhoneCallDetails[] details) {
368 if (details == null) {
369 // Somewhere went wrong: we're going to bail out and show error to users.
370 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
371 Toast.LENGTH_SHORT).show();
372 finish();
373 return;
374 }
375
376 // We know that all calls are from the same number and the same contact, so pick the
377 // first.
378 PhoneCallDetails firstDetails = details[0];
379 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700380 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700381 final Uri contactUri = firstDetails.contactUri;
382 final Uri photoUri = firstDetails.photoUri;
383
Chiao Cheng94b10b52012-08-17 16:59:12 -0700384 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700385 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700386 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
387 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
388 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
389 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390
Tyler Gunn18164c82014-06-09 10:20:57 -0700391 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700392
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800393 final CharSequence displayNumber =
394 mPhoneNumberHelper.getDisplayNumber(
395 firstDetails.number,
396 firstDetails.numberPresentation,
397 firstDetails.formattedNumber);
398
Tyler Gunn18164c82014-06-09 10:20:57 -0700399 if (!TextUtils.isEmpty(firstDetails.name)) {
400 mCallerName.setText(firstDetails.name);
401 mCallerNumber.setText(callLocationOrType + " " + displayNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700402 } else {
Tyler Gunn18164c82014-06-09 10:20:57 -0700403 mCallerName.setText(displayNumber);
404 if (!TextUtils.isEmpty(callLocationOrType)) {
405 mCallerNumber.setText(callLocationOrType);
406 mCallerNumber.setVisibility(View.VISIBLE);
407 } else {
408 mCallerNumber.setVisibility(View.GONE);
409 }
410
Chiao Cheng94b10b52012-08-17 16:59:12 -0700411 }
412
413 mHasEditNumberBeforeCallOption =
414 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
415 mHasTrashOption = hasVoicemail();
416 mHasRemoveFromCallLogOption = !hasVoicemail();
417 invalidateOptionsMenu();
418
419 ListView historyList = (ListView) findViewById(R.id.history);
420 historyList.setAdapter(
421 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
Tyler Gunn18164c82014-06-09 10:20:57 -0700422 mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423
Tyler Gunn18164c82014-06-09 10:20:57 -0700424 String lookupKey = contactUri == null ? null
425 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800426
427 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
428
429 final int contactType =
430 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
431 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
432 ContactPhotoManager.TYPE_DEFAULT;
433
Tyler Gunn18164c82014-06-09 10:20:57 -0700434 String nameForDefaultImage;
435 if (TextUtils.isEmpty(firstDetails.name)) {
436 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(firstDetails.number,
437 firstDetails.numberPresentation,
438 firstDetails.formattedNumber).toString();
439 } else {
440 nameForDefaultImage = firstDetails.name.toString();
441 }
442
443 loadContactPhotos(
444 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700445 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
446 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700447
448 /**
449 * Determines the location geocode text for a call, or the phone number type
450 * (if available).
451 *
452 * @param details The call details.
453 * @return The phone number type or location.
454 */
455 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
456 if (!TextUtils.isEmpty(details.name)) {
457 return Phone.getTypeLabel(mResources, details.numberType,
458 details.numberLabel);
459 } else {
460 return details.geocode;
461 }
462 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700463 }
464 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
465 }
466
467 /** Return the phone call details for a given call log URI. */
468 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
469 ContentResolver resolver = getContentResolver();
470 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
471 try {
472 if (callCursor == null || !callCursor.moveToFirst()) {
473 throw new IllegalArgumentException("Cannot find content: " + callUri);
474 }
475
476 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700477 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
478 final int numberPresentation = callCursor.getInt(
479 NUMBER_PRESENTATION_COLUMN_INDEX);
480 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
481 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
482 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700483 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
484 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
485
486 if (TextUtils.isEmpty(countryIso)) {
487 countryIso = mDefaultCountryIso;
488 }
489
490 // Formatted phone number.
491 final CharSequence formattedNumber;
492 // Read contact specifics.
493 final CharSequence nameText;
494 final int numberType;
495 final CharSequence numberLabel;
496 final Uri photoUri;
497 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800498 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700499 // If this is not a regular number, there is no point in looking it up in the contacts.
500 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700501 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700502 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700503 ? mContactInfoHelper.lookupNumber(number, countryIso)
504 : null;
505 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700506 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
507 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700508 nameText = "";
509 numberType = 0;
510 numberLabel = "";
511 photoUri = null;
512 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800513 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700514 } else {
515 formattedNumber = info.formattedNumber;
516 nameText = info.name;
517 numberType = info.type;
518 numberLabel = info.label;
519 photoUri = info.photoUri;
520 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800521 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700522 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700523 return new PhoneCallDetails(number, numberPresentation,
524 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700525 new int[]{ callType }, date, duration,
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800526 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700527 } finally {
528 if (callCursor != null) {
529 callCursor.close();
530 }
531 }
532 }
533
534 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700535 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
536 String lookupKey, int contactType) {
537
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800538 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700539 contactType, true /* isCircular */);
540
541 mQuickContactBadge.assignContactUri(contactUri);
542 mQuickContactBadge.setContentDescription(
543 mResources.getString(R.string.description_contact_details, displayName));
544
545 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
546 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700547 }
548
549 static final class ViewEntry {
550 public final String text;
551 public final Intent primaryIntent;
552 /** The description for accessibility of the primary action. */
553 public final String primaryDescription;
554
555 public CharSequence label = null;
556 /** Icon for the secondary action. */
557 public int secondaryIcon = 0;
558 /** Intent for the secondary action. If not null, an icon must be defined. */
559 public Intent secondaryIntent = null;
560 /** The description for accessibility of the secondary action. */
561 public String secondaryDescription = null;
562
563 public ViewEntry(String text, Intent intent, String description) {
564 this.text = text;
565 primaryIntent = intent;
566 primaryDescription = description;
567 }
568
569 public void setSecondaryAction(int icon, Intent intent, String description) {
570 secondaryIcon = icon;
571 secondaryIntent = intent;
572 secondaryDescription = description;
573 }
574 }
575
Chiao Cheng94b10b52012-08-17 16:59:12 -0700576 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
577 if (statusCursor == null) {
578 mStatusMessageView.setVisibility(View.GONE);
579 return;
580 }
581 final StatusMessage message = getStatusMessage(statusCursor);
582 if (message == null || !message.showInCallDetails()) {
583 mStatusMessageView.setVisibility(View.GONE);
584 return;
585 }
586
587 mStatusMessageView.setVisibility(View.VISIBLE);
588 mStatusMessageText.setText(message.callDetailsMessageId);
589 if (message.actionMessageId != -1) {
590 mStatusMessageAction.setText(message.actionMessageId);
591 }
592 if (message.actionUri != null) {
593 mStatusMessageAction.setClickable(true);
594 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
595 @Override
596 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700597 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
598 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700599 }
600 });
601 } else {
602 mStatusMessageAction.setClickable(false);
603 }
604 }
605
606 private StatusMessage getStatusMessage(Cursor statusCursor) {
607 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
608 if (messages.size() == 0) {
609 return null;
610 }
611 // There can only be a single status message per source package, so num of messages can
612 // at most be 1.
613 if (messages.size() > 1) {
614 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
615 " Will use the first one.", messages.size()));
616 }
617 return messages.get(0);
618 }
619
620 @Override
621 public boolean onCreateOptionsMenu(Menu menu) {
622 getMenuInflater().inflate(R.menu.call_details_options, menu);
623 return super.onCreateOptionsMenu(menu);
624 }
625
626 @Override
627 public boolean onPrepareOptionsMenu(Menu menu) {
628 // This action deletes all elements in the group from the call log.
629 // We don't have this action for voicemails, because you can just use the trash button.
630 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
631 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
632 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
633 return super.onPrepareOptionsMenu(menu);
634 }
635
Chiao Cheng94b10b52012-08-17 16:59:12 -0700636 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
637 final StringBuilder callIds = new StringBuilder();
638 for (Uri callUri : getCallLogEntryUris()) {
639 if (callIds.length() != 0) {
640 callIds.append(",");
641 }
642 callIds.append(ContentUris.parseId(callUri));
643 }
644 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
645 new AsyncTask<Void, Void, Void>() {
646 @Override
647 public Void doInBackground(Void... params) {
648 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
649 Calls._ID + " IN (" + callIds + ")", null);
650 return null;
651 }
652
653 @Override
654 public void onPostExecute(Void result) {
655 finish();
656 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700657 }
658 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700659 }
660
661 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700662 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700663 }
664
665 public void onMenuTrashVoicemail(MenuItem menuItem) {
666 final Uri voicemailUri = getVoicemailUri();
667 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
668 new AsyncTask<Void, Void, Void>() {
669 @Override
670 public Void doInBackground(Void... params) {
671 getContentResolver().delete(voicemailUri, null, null);
672 return null;
673 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700674
Chiao Cheng94b10b52012-08-17 16:59:12 -0700675 @Override
676 public void onPostExecute(Void result) {
677 finish();
678 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700679 }
680 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700681 }
682
683 @Override
684 protected void onPause() {
685 // Immediately stop the proximity sensor.
686 disableProximitySensor(false);
687 mProximitySensorListener.clearPendingRequests();
688 super.onPause();
689 }
690
691 @Override
692 public void enableProximitySensor() {
693 mProximitySensorManager.enable();
694 }
695
696 @Override
697 public void disableProximitySensor(boolean waitForFarState) {
698 mProximitySensorManager.disable(waitForFarState);
699 }
700
Chiao Cheng94b10b52012-08-17 16:59:12 -0700701 private void closeSystemDialogs() {
702 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
703 }
704
Chiao Cheng35071c02012-10-15 18:36:24 -0700705 /** Returns the given text, forced to be left-to-right. */
706 private static CharSequence forceLeftToRight(CharSequence text) {
707 StringBuilder sb = new StringBuilder();
708 sb.append(LEFT_TO_RIGHT_EMBEDDING);
709 sb.append(text);
710 sb.append(POP_DIRECTIONAL_FORMATTING);
711 return sb.toString();
712 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713}