blob: 42e4659f2d267ac76a7b72856097f4e408e1359d [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;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070020import android.content.ComponentName;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import 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;
Nancy Chen87ba4892014-06-11 17:56:07 -070028import android.graphics.drawable.Drawable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.net.Uri;
30import android.os.AsyncTask;
31import android.os.Bundle;
32import android.provider.CallLog;
33import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.VoicemailContract.Voicemails;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070036import android.telecomm.PhoneAccount;
37import android.telecomm.TelecommManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.telephony.TelephonyManager;
39import android.text.TextUtils;
40import android.util.Log;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.view.KeyEvent;
42import android.view.LayoutInflater;
43import android.view.Menu;
44import android.view.MenuItem;
45import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.widget.ListView;
Tyler Gunn18164c82014-06-09 10:20:57 -070047import android.widget.QuickContactBadge;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.widget.TextView;
49import android.widget.Toast;
50
Chiao Cheng35071c02012-10-15 18:36:24 -070051import com.android.contacts.common.ContactPhotoManager;
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070052import com.android.contacts.common.CallUtil;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080053import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070054import com.android.contacts.common.GeoUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055import com.android.dialer.calllog.CallDetailHistoryAdapter;
Tyler Gunn8b0e8582014-07-10 12:28:43 -070056import com.android.dialer.calllog.CallLogQuery;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import com.android.dialer.calllog.CallTypeHelper;
58import com.android.dialer.calllog.ContactInfo;
59import com.android.dialer.calllog.ContactInfoHelper;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070060import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee24ec3192013-11-19 13:52:45 -080061import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070062import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070063import com.android.dialer.util.AsyncTaskExecutor;
64import com.android.dialer.util.AsyncTaskExecutors;
Yorke Lee39213592014-04-07 15:39:48 -070065import com.android.dialer.util.DialerUtils;
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
Yorke Lee03459442013-10-30 18:29:32 -070082 private static final int LOADER_ID = 0;
83 private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
84
Chiao Cheng35071c02012-10-15 18:36:24 -070085 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
86 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
87
Chiao Cheng94b10b52012-08-17 16:59:12 -070088 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
89 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
90 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
91 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
92
93 /** The enumeration of {@link AsyncTask} objects used in this class. */
94 public enum Tasks {
95 MARK_VOICEMAIL_READ,
96 DELETE_VOICEMAIL_AND_FINISH,
97 REMOVE_FROM_CALL_LOG_AND_FINISH,
98 UPDATE_PHONE_CALL_DETAILS,
99 }
100
101 /** A long array extra containing ids of call log entries to display. */
102 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
103 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
104 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
105 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
106 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
107 /** If the activity was triggered from a notification. */
108 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
109
110 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800111 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700112 private QuickContactBadge mQuickContactBadge;
113 private TextView mCallerName;
114 private TextView mCallerNumber;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 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
Chiao Cheng94b10b52012-08-17 16:59:12 -0700144 /** Listener to changes in the proximity sensor state. */
145 private class ProximitySensorListener implements ProximitySensorManager.Listener {
146 /** Used to show a blank view and hide the action bar. */
147 private final Runnable mBlankRunnable = new Runnable() {
148 @Override
149 public void run() {
150 View blankView = findViewById(R.id.blank);
151 blankView.setVisibility(View.VISIBLE);
152 getActionBar().hide();
153 }
154 };
155 /** Used to remove the blank view and show the action bar. */
156 private final Runnable mUnblankRunnable = new Runnable() {
157 @Override
158 public void run() {
159 View blankView = findViewById(R.id.blank);
160 blankView.setVisibility(View.GONE);
161 getActionBar().show();
162 }
163 };
164
165 @Override
166 public synchronized void onNear() {
167 clearPendingRequests();
168 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
169 }
170
171 @Override
172 public synchronized void onFar() {
173 clearPendingRequests();
174 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
175 }
176
177 /** Removed any delayed requests that may be pending. */
178 public synchronized void clearPendingRequests() {
179 View blankView = findViewById(R.id.blank);
180 blankView.removeCallbacks(mBlankRunnable);
181 blankView.removeCallbacks(mUnblankRunnable);
182 }
183
184 /** Post a {@link Runnable} with a delay on the main thread. */
185 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
186 // Post these instead of executing immediately so that:
187 // - They are guaranteed to be executed on the main thread.
188 // - If the sensor values changes rapidly for some time, the UI will not be
189 // updated immediately.
190 View blankView = findViewById(R.id.blank);
191 blankView.postDelayed(runnable, delayMillis);
192 }
193 }
194
195 static final String[] CALL_LOG_PROJECTION = new String[] {
196 CallLog.Calls.DATE,
197 CallLog.Calls.DURATION,
198 CallLog.Calls.NUMBER,
199 CallLog.Calls.TYPE,
200 CallLog.Calls.COUNTRY_ISO,
201 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700202 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700203 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
204 CallLog.Calls.PHONE_ACCOUNT_ID,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700205 CallLog.Calls.FEATURES,
206 CallLog.Calls.DATA_USAGE
Chiao Cheng94b10b52012-08-17 16:59:12 -0700207 };
208
209 static final int DATE_COLUMN_INDEX = 0;
210 static final int DURATION_COLUMN_INDEX = 1;
211 static final int NUMBER_COLUMN_INDEX = 2;
212 static final int CALL_TYPE_COLUMN_INDEX = 3;
213 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
214 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700215 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700216 static final int ACCOUNT_COMPONENT_NAME = 7;
217 static final int ACCOUNT_ID = 8;
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700218 static final int FEATURES = 9;
219 static final int DATA_USAGE = 10;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700220
Chiao Cheng94b10b52012-08-17 16:59:12 -0700221 @Override
222 protected void onCreate(Bundle icicle) {
223 super.onCreate(icicle);
224
225 setContentView(R.layout.call_detail);
226
227 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
228 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
229 mResources = getResources();
230
231 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee24ec3192013-11-19 13:52:45 -0800232 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700233 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
234 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700235 mStatusMessageView = findViewById(R.id.voicemail_status);
236 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
237 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Tyler Gunn18164c82014-06-09 10:20:57 -0700238 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
239 mQuickContactBadge.setOverlay(null);
240 mCallerName = (TextView) findViewById(R.id.caller_name);
241 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Chiao Cheng35071c02012-10-15 18:36:24 -0700242 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700243 mContactPhotoManager = ContactPhotoManager.getInstance(this);
244 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700245 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700246 getActionBar().setDisplayHomeAsUpEnabled(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700247 optionallyHandleVoicemail();
248 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
249 closeSystemDialogs();
250 }
251 }
252
253 @Override
254 public void onResume() {
255 super.onResume();
256 updateData(getCallLogEntryUris());
257 }
258
259 /**
260 * Handle voicemail playback or hide voicemail ui.
261 * <p>
262 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
263 * playback. If it doesn't, then hide the voicemail ui.
264 */
265 private void optionallyHandleVoicemail() {
266 View voicemailContainer = findViewById(R.id.voicemail_container);
267 if (hasVoicemail()) {
268 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
269 // to play and optionally start the playback.
270 // Do a query to fetch the voicemail status messages.
271 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
272 Bundle fragmentArguments = new Bundle();
273 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
274 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
275 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
276 }
277 playbackFragment.setArguments(fragmentArguments);
278 voicemailContainer.setVisibility(View.VISIBLE);
279 getFragmentManager().beginTransaction()
Yorke Leeaa536fd2013-07-29 11:31:04 -0700280 .add(R.id.voicemail_container, playbackFragment)
281 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700282 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
283 markVoicemailAsRead(getVoicemailUri());
284 } else {
285 // No voicemail uri: hide the status view.
286 mStatusMessageView.setVisibility(View.GONE);
287 voicemailContainer.setVisibility(View.GONE);
288 }
289 }
290
291 private boolean hasVoicemail() {
292 return getVoicemailUri() != null;
293 }
294
295 private Uri getVoicemailUri() {
296 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
297 }
298
299 private void markVoicemailAsRead(final Uri voicemailUri) {
300 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
301 @Override
302 public Void doInBackground(Void... params) {
303 ContentValues values = new ContentValues();
304 values.put(Voicemails.IS_READ, true);
305 getContentResolver().update(voicemailUri, values,
306 Voicemails.IS_READ + " = 0", null);
307 return null;
308 }
309 });
310 }
311
312 /**
313 * Returns the list of URIs to show.
314 * <p>
315 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
316 * a list of ids in the call log added as an extra on the URI.
317 * <p>
318 * If both are available, the data on the intent takes precedence.
319 */
320 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700321 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700322 if (uri != null) {
323 // If there is a data on the intent, it takes precedence over the extra.
324 return new Uri[]{ uri };
325 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700326 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
327 final int numIds = ids == null ? 0 : ids.length;
328 final Uri[] uris = new Uri[numIds];
329 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700330 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
331 }
332 return uris;
333 }
334
335 @Override
336 public boolean onKeyDown(int keyCode, KeyEvent event) {
337 switch (keyCode) {
338 case KeyEvent.KEYCODE_CALL: {
339 // Make sure phone isn't already busy before starting direct call
340 TelephonyManager tm = (TelephonyManager)
341 getSystemService(Context.TELEPHONY_SERVICE);
342 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700343 DialerUtils.startActivityWithErrorToast(this,
344 CallUtil.getCallIntent(Uri.fromParts(CallUtil.SCHEME_TEL, mNumber,
Nancy Chen87ba4892014-06-11 17:56:07 -0700345 null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700346 return true;
347 }
348 }
349 }
350
351 return super.onKeyDown(keyCode, event);
352 }
353
354 /**
355 * Update user interface with details of given call.
356 *
357 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
358 */
359 private void updateData(final Uri... callUris) {
360 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
361 @Override
362 public PhoneCallDetails[] doInBackground(Void... params) {
363 // TODO: All phone calls correspond to the same person, so we can make a single
364 // lookup.
365 final int numCalls = callUris.length;
366 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
367 try {
368 for (int index = 0; index < numCalls; ++index) {
369 details[index] = getPhoneCallDetailsForUri(callUris[index]);
370 }
371 return details;
372 } catch (IllegalArgumentException e) {
373 // Something went wrong reading in our primary data.
374 Log.w(TAG, "invalid URI starting call details", e);
375 return null;
376 }
377 }
378
379 @Override
380 public void onPostExecute(PhoneCallDetails[] details) {
381 if (details == null) {
382 // Somewhere went wrong: we're going to bail out and show error to users.
383 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
384 Toast.LENGTH_SHORT).show();
385 finish();
386 return;
387 }
388
389 // We know that all calls are from the same number and the same contact, so pick the
390 // first.
391 PhoneCallDetails firstDetails = details[0];
392 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700393 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700394 final Uri contactUri = firstDetails.contactUri;
395 final Uri photoUri = firstDetails.photoUri;
396
Chiao Cheng94b10b52012-08-17 16:59:12 -0700397 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700398 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700399 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
400 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
401 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
402 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700403
Tyler Gunn18164c82014-06-09 10:20:57 -0700404 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700405
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800406 final CharSequence displayNumber =
407 mPhoneNumberHelper.getDisplayNumber(
408 firstDetails.number,
409 firstDetails.numberPresentation,
410 firstDetails.formattedNumber);
411
Tyler Gunn18164c82014-06-09 10:20:57 -0700412 if (!TextUtils.isEmpty(firstDetails.name)) {
413 mCallerName.setText(firstDetails.name);
414 mCallerNumber.setText(callLocationOrType + " " + displayNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700415 } else {
Tyler Gunn18164c82014-06-09 10:20:57 -0700416 mCallerName.setText(displayNumber);
417 if (!TextUtils.isEmpty(callLocationOrType)) {
418 mCallerNumber.setText(callLocationOrType);
419 mCallerNumber.setVisibility(View.VISIBLE);
420 } else {
421 mCallerNumber.setVisibility(View.GONE);
422 }
423
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424 }
425
426 mHasEditNumberBeforeCallOption =
427 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
428 mHasTrashOption = hasVoicemail();
429 mHasRemoveFromCallLogOption = !hasVoicemail();
430 invalidateOptionsMenu();
431
432 ListView historyList = (ListView) findViewById(R.id.history);
433 historyList.setAdapter(
434 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
Tyler Gunn18164c82014-06-09 10:20:57 -0700435 mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700436
Tyler Gunn18164c82014-06-09 10:20:57 -0700437 String lookupKey = contactUri == null ? null
438 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800439
440 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
441
442 final int contactType =
443 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
444 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
445 ContactPhotoManager.TYPE_DEFAULT;
446
Tyler Gunn18164c82014-06-09 10:20:57 -0700447 String nameForDefaultImage;
448 if (TextUtils.isEmpty(firstDetails.name)) {
449 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(firstDetails.number,
450 firstDetails.numberPresentation,
451 firstDetails.formattedNumber).toString();
452 } else {
453 nameForDefaultImage = firstDetails.name.toString();
454 }
455
456 loadContactPhotos(
457 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700458 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
459 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700460
461 /**
462 * Determines the location geocode text for a call, or the phone number type
463 * (if available).
464 *
465 * @param details The call details.
466 * @return The phone number type or location.
467 */
468 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
469 if (!TextUtils.isEmpty(details.name)) {
470 return Phone.getTypeLabel(mResources, details.numberType,
471 details.numberLabel);
472 } else {
473 return details.geocode;
474 }
475 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700476 }
477 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
478 }
479
480 /** Return the phone call details for a given call log URI. */
481 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
482 ContentResolver resolver = getContentResolver();
483 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
484 try {
485 if (callCursor == null || !callCursor.moveToFirst()) {
486 throw new IllegalArgumentException("Cannot find content: " + callUri);
487 }
488
489 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700490 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
491 final int numberPresentation = callCursor.getInt(
492 NUMBER_PRESENTATION_COLUMN_INDEX);
493 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
494 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
495 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700496 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
497 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700498
499 final Drawable accountIcon = PhoneAccountUtils.getAccountIcon(this,
500 PhoneAccountUtils.getAccount(
501 callCursor.getString(ACCOUNT_COMPONENT_NAME),
502 callCursor.getString(ACCOUNT_ID)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700503
504 if (TextUtils.isEmpty(countryIso)) {
505 countryIso = mDefaultCountryIso;
506 }
507
508 // Formatted phone number.
509 final CharSequence formattedNumber;
510 // Read contact specifics.
511 final CharSequence nameText;
512 final int numberType;
513 final CharSequence numberLabel;
514 final Uri photoUri;
515 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800516 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700517 // If this is not a regular number, there is no point in looking it up in the contacts.
518 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700519 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700520 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700521 ? mContactInfoHelper.lookupNumber(number, countryIso)
522 : null;
523 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700524 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
525 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700526 nameText = "";
527 numberType = 0;
528 numberLabel = "";
529 photoUri = null;
530 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800531 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700532 } else {
533 formattedNumber = info.formattedNumber;
534 nameText = info.name;
535 numberType = info.type;
536 numberLabel = info.label;
537 photoUri = info.photoUri;
538 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800539 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700540 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700541 final int features = callCursor.getInt(FEATURES);
542 Long dataUsage = null;
543 if (!callCursor.isNull(DATA_USAGE)) {
544 dataUsage = callCursor.getLong(DATA_USAGE);
545 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700546 return new PhoneCallDetails(number, numberPresentation,
547 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700548 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700549 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700550 accountIcon, features, dataUsage);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700551 } finally {
552 if (callCursor != null) {
553 callCursor.close();
554 }
555 }
556 }
557
558 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700559 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
560 String lookupKey, int contactType) {
561
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800562 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700563 contactType, true /* isCircular */);
564
565 mQuickContactBadge.assignContactUri(contactUri);
566 mQuickContactBadge.setContentDescription(
567 mResources.getString(R.string.description_contact_details, displayName));
568
569 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
570 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700571 }
572
573 static final class ViewEntry {
574 public final String text;
575 public final Intent primaryIntent;
576 /** The description for accessibility of the primary action. */
577 public final String primaryDescription;
578
579 public CharSequence label = null;
580 /** Icon for the secondary action. */
581 public int secondaryIcon = 0;
582 /** Intent for the secondary action. If not null, an icon must be defined. */
583 public Intent secondaryIntent = null;
584 /** The description for accessibility of the secondary action. */
585 public String secondaryDescription = null;
586
587 public ViewEntry(String text, Intent intent, String description) {
588 this.text = text;
589 primaryIntent = intent;
590 primaryDescription = description;
591 }
592
593 public void setSecondaryAction(int icon, Intent intent, String description) {
594 secondaryIcon = icon;
595 secondaryIntent = intent;
596 secondaryDescription = description;
597 }
598 }
599
Chiao Cheng94b10b52012-08-17 16:59:12 -0700600 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
601 if (statusCursor == null) {
602 mStatusMessageView.setVisibility(View.GONE);
603 return;
604 }
605 final StatusMessage message = getStatusMessage(statusCursor);
606 if (message == null || !message.showInCallDetails()) {
607 mStatusMessageView.setVisibility(View.GONE);
608 return;
609 }
610
611 mStatusMessageView.setVisibility(View.VISIBLE);
612 mStatusMessageText.setText(message.callDetailsMessageId);
613 if (message.actionMessageId != -1) {
614 mStatusMessageAction.setText(message.actionMessageId);
615 }
616 if (message.actionUri != null) {
617 mStatusMessageAction.setClickable(true);
618 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
619 @Override
620 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700621 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
622 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700623 }
624 });
625 } else {
626 mStatusMessageAction.setClickable(false);
627 }
628 }
629
630 private StatusMessage getStatusMessage(Cursor statusCursor) {
631 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
632 if (messages.size() == 0) {
633 return null;
634 }
635 // There can only be a single status message per source package, so num of messages can
636 // at most be 1.
637 if (messages.size() > 1) {
638 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
639 " Will use the first one.", messages.size()));
640 }
641 return messages.get(0);
642 }
643
644 @Override
645 public boolean onCreateOptionsMenu(Menu menu) {
646 getMenuInflater().inflate(R.menu.call_details_options, menu);
647 return super.onCreateOptionsMenu(menu);
648 }
649
650 @Override
651 public boolean onPrepareOptionsMenu(Menu menu) {
652 // This action deletes all elements in the group from the call log.
653 // We don't have this action for voicemails, because you can just use the trash button.
654 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
655 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
656 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
657 return super.onPrepareOptionsMenu(menu);
658 }
659
Chiao Cheng94b10b52012-08-17 16:59:12 -0700660 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
661 final StringBuilder callIds = new StringBuilder();
662 for (Uri callUri : getCallLogEntryUris()) {
663 if (callIds.length() != 0) {
664 callIds.append(",");
665 }
666 callIds.append(ContentUris.parseId(callUri));
667 }
668 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
669 new AsyncTask<Void, Void, Void>() {
670 @Override
671 public Void doInBackground(Void... params) {
672 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
673 Calls._ID + " IN (" + callIds + ")", null);
674 return null;
675 }
676
677 @Override
678 public void onPostExecute(Void result) {
679 finish();
680 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700681 }
682 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700683 }
684
685 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700686 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700687 }
688
689 public void onMenuTrashVoicemail(MenuItem menuItem) {
690 final Uri voicemailUri = getVoicemailUri();
691 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
692 new AsyncTask<Void, Void, Void>() {
693 @Override
694 public Void doInBackground(Void... params) {
695 getContentResolver().delete(voicemailUri, null, null);
696 return null;
697 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700698
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 @Override
700 public void onPostExecute(Void result) {
701 finish();
702 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700703 }
704 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705 }
706
707 @Override
708 protected void onPause() {
709 // Immediately stop the proximity sensor.
710 disableProximitySensor(false);
711 mProximitySensorListener.clearPendingRequests();
712 super.onPause();
713 }
714
715 @Override
716 public void enableProximitySensor() {
717 mProximitySensorManager.enable();
718 }
719
720 @Override
721 public void disableProximitySensor(boolean waitForFarState) {
722 mProximitySensorManager.disable(waitForFarState);
723 }
724
Chiao Cheng94b10b52012-08-17 16:59:12 -0700725 private void closeSystemDialogs() {
726 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
727 }
728
Chiao Cheng35071c02012-10-15 18:36:24 -0700729 /** Returns the given text, forced to be left-to-right. */
730 private static CharSequence forceLeftToRight(CharSequence text) {
731 StringBuilder sb = new StringBuilder();
732 sb.append(LEFT_TO_RIGHT_EMBEDDING);
733 sb.append(text);
734 sb.append(POP_DIRECTIONAL_FORMATTING);
735 return sb.toString();
736 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700737}