blob: 3ef78af5cf12badb198c1695dc7a93f018a34b85 [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;
Nancy Chen87ba4892014-06-11 17:56:07 -070027import android.graphics.drawable.Drawable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.net.Uri;
29import android.os.AsyncTask;
30import android.os.Bundle;
31import android.provider.CallLog;
32import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.provider.VoicemailContract.Voicemails;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.telephony.TelephonyManager;
36import android.text.TextUtils;
37import android.util.Log;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.view.KeyEvent;
39import android.view.LayoutInflater;
40import android.view.Menu;
41import android.view.MenuItem;
42import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.widget.ListView;
Tyler Gunn18164c82014-06-09 10:20:57 -070044import android.widget.QuickContactBadge;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.widget.TextView;
46import android.widget.Toast;
47
Chiao Cheng35071c02012-10-15 18:36:24 -070048import com.android.contacts.common.ContactPhotoManager;
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070049import com.android.contacts.common.CallUtil;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080050import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070051import com.android.contacts.common.GeoUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070052import com.android.dialer.calllog.CallDetailHistoryAdapter;
Tyler Gunn8b0e8582014-07-10 12:28:43 -070053import com.android.dialer.calllog.CallLogQuery;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054import com.android.dialer.calllog.CallTypeHelper;
55import com.android.dialer.calllog.ContactInfo;
56import com.android.dialer.calllog.ContactInfoHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -080057import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070058import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070059import com.android.dialer.util.AsyncTaskExecutor;
60import com.android.dialer.util.AsyncTaskExecutors;
Yorke Lee39213592014-04-07 15:39:48 -070061import com.android.dialer.util.DialerUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070062import com.android.dialer.voicemail.VoicemailPlaybackFragment;
63import com.android.dialer.voicemail.VoicemailStatusHelper;
64import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
65import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
66
67import java.util.List;
68
69/**
70 * Displays the details of a specific call log entry.
71 * <p>
72 * This activity can be either started with the URI of a single call log entry, or with the
73 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
74 */
75public class CallDetailActivity extends Activity implements ProximitySensorAware {
76 private static final String TAG = "CallDetail";
77
Yorke Lee03459442013-10-30 18:29:32 -070078 private static final int LOADER_ID = 0;
79 private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
80
Chiao Cheng35071c02012-10-15 18:36:24 -070081 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
82 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
83
Chiao Cheng94b10b52012-08-17 16:59:12 -070084 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
85 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
86 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
87 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
88
89 /** The enumeration of {@link AsyncTask} objects used in this class. */
90 public enum Tasks {
91 MARK_VOICEMAIL_READ,
92 DELETE_VOICEMAIL_AND_FINISH,
93 REMOVE_FROM_CALL_LOG_AND_FINISH,
94 UPDATE_PHONE_CALL_DETAILS,
95 }
96
97 /** A long array extra containing ids of call log entries to display. */
98 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
99 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
100 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
101 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
102 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
103 /** If the activity was triggered from a notification. */
104 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
105
106 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800107 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700108 private QuickContactBadge mQuickContactBadge;
109 private TextView mCallerName;
110 private TextView mCallerNumber;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 private AsyncTaskExecutor mAsyncTaskExecutor;
112 private ContactInfoHelper mContactInfoHelper;
113
114 private String mNumber = null;
115 private String mDefaultCountryIso;
116
117 /* package */ LayoutInflater mInflater;
118 /* package */ Resources mResources;
119 /** Helper to load contact photos. */
120 private ContactPhotoManager mContactPhotoManager;
121 /** Helper to make async queries to content resolver. */
122 private CallDetailActivityQueryHandler mAsyncQueryHandler;
123 /** Helper to get voicemail status messages. */
124 private VoicemailStatusHelper mVoicemailStatusHelper;
125 // Views related to voicemail status message.
126 private View mStatusMessageView;
127 private TextView mStatusMessageText;
128 private TextView mStatusMessageAction;
129
130 /** Whether we should show "edit number before call" in the options menu. */
131 private boolean mHasEditNumberBeforeCallOption;
132 /** Whether we should show "trash" in the options menu. */
133 private boolean mHasTrashOption;
134 /** Whether we should show "remove from call log" in the options menu. */
135 private boolean mHasRemoveFromCallLogOption;
136
137 private ProximitySensorManager mProximitySensorManager;
138 private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
139
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140 /** Listener to changes in the proximity sensor state. */
141 private class ProximitySensorListener implements ProximitySensorManager.Listener {
142 /** Used to show a blank view and hide the action bar. */
143 private final Runnable mBlankRunnable = new Runnable() {
144 @Override
145 public void run() {
146 View blankView = findViewById(R.id.blank);
147 blankView.setVisibility(View.VISIBLE);
148 getActionBar().hide();
149 }
150 };
151 /** Used to remove the blank view and show the action bar. */
152 private final Runnable mUnblankRunnable = new Runnable() {
153 @Override
154 public void run() {
155 View blankView = findViewById(R.id.blank);
156 blankView.setVisibility(View.GONE);
157 getActionBar().show();
158 }
159 };
160
161 @Override
162 public synchronized void onNear() {
163 clearPendingRequests();
164 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
165 }
166
167 @Override
168 public synchronized void onFar() {
169 clearPendingRequests();
170 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
171 }
172
173 /** Removed any delayed requests that may be pending. */
174 public synchronized void clearPendingRequests() {
175 View blankView = findViewById(R.id.blank);
176 blankView.removeCallbacks(mBlankRunnable);
177 blankView.removeCallbacks(mUnblankRunnable);
178 }
179
180 /** Post a {@link Runnable} with a delay on the main thread. */
181 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
182 // Post these instead of executing immediately so that:
183 // - They are guaranteed to be executed on the main thread.
184 // - If the sensor values changes rapidly for some time, the UI will not be
185 // updated immediately.
186 View blankView = findViewById(R.id.blank);
187 blankView.postDelayed(runnable, delayMillis);
188 }
189 }
190
191 static final String[] CALL_LOG_PROJECTION = new String[] {
192 CallLog.Calls.DATE,
193 CallLog.Calls.DURATION,
194 CallLog.Calls.NUMBER,
195 CallLog.Calls.TYPE,
196 CallLog.Calls.COUNTRY_ISO,
197 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700198 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700199 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
200 CallLog.Calls.PHONE_ACCOUNT_ID,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700201 CallLog.Calls.FEATURES,
202 CallLog.Calls.DATA_USAGE
Chiao Cheng94b10b52012-08-17 16:59:12 -0700203 };
204
205 static final int DATE_COLUMN_INDEX = 0;
206 static final int DURATION_COLUMN_INDEX = 1;
207 static final int NUMBER_COLUMN_INDEX = 2;
208 static final int CALL_TYPE_COLUMN_INDEX = 3;
209 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
210 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700211 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700212 static final int ACCOUNT_COMPONENT_NAME = 7;
213 static final int ACCOUNT_ID = 8;
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700214 static final int FEATURES = 9;
215 static final int DATA_USAGE = 10;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700216
Chiao Cheng94b10b52012-08-17 16:59:12 -0700217 @Override
218 protected void onCreate(Bundle icicle) {
219 super.onCreate(icicle);
220
221 setContentView(R.layout.call_detail);
222
223 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
224 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
225 mResources = getResources();
226
227 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee24ec3192013-11-19 13:52:45 -0800228 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700229 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
230 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700231 mStatusMessageView = findViewById(R.id.voicemail_status);
232 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
233 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Tyler Gunn18164c82014-06-09 10:20:57 -0700234 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
235 mQuickContactBadge.setOverlay(null);
236 mCallerName = (TextView) findViewById(R.id.caller_name);
237 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Chiao Cheng35071c02012-10-15 18:36:24 -0700238 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239 mContactPhotoManager = ContactPhotoManager.getInstance(this);
240 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700241 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700242 getActionBar().setDisplayHomeAsUpEnabled(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700243 optionallyHandleVoicemail();
244 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
245 closeSystemDialogs();
246 }
247 }
248
249 @Override
250 public void onResume() {
251 super.onResume();
252 updateData(getCallLogEntryUris());
253 }
254
255 /**
256 * Handle voicemail playback or hide voicemail ui.
257 * <p>
258 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
259 * playback. If it doesn't, then hide the voicemail ui.
260 */
261 private void optionallyHandleVoicemail() {
262 View voicemailContainer = findViewById(R.id.voicemail_container);
263 if (hasVoicemail()) {
264 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
265 // to play and optionally start the playback.
266 // Do a query to fetch the voicemail status messages.
267 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
268 Bundle fragmentArguments = new Bundle();
269 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
270 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
271 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
272 }
273 playbackFragment.setArguments(fragmentArguments);
274 voicemailContainer.setVisibility(View.VISIBLE);
275 getFragmentManager().beginTransaction()
Yorke Leeaa536fd2013-07-29 11:31:04 -0700276 .add(R.id.voicemail_container, playbackFragment)
277 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700278 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
279 markVoicemailAsRead(getVoicemailUri());
280 } else {
281 // No voicemail uri: hide the status view.
282 mStatusMessageView.setVisibility(View.GONE);
283 voicemailContainer.setVisibility(View.GONE);
284 }
285 }
286
287 private boolean hasVoicemail() {
288 return getVoicemailUri() != null;
289 }
290
291 private Uri getVoicemailUri() {
292 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
293 }
294
295 private void markVoicemailAsRead(final Uri voicemailUri) {
296 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
297 @Override
298 public Void doInBackground(Void... params) {
299 ContentValues values = new ContentValues();
300 values.put(Voicemails.IS_READ, true);
301 getContentResolver().update(voicemailUri, values,
302 Voicemails.IS_READ + " = 0", null);
303 return null;
304 }
305 });
306 }
307
308 /**
309 * Returns the list of URIs to show.
310 * <p>
311 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
312 * a list of ids in the call log added as an extra on the URI.
313 * <p>
314 * If both are available, the data on the intent takes precedence.
315 */
316 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700317 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700318 if (uri != null) {
319 // If there is a data on the intent, it takes precedence over the extra.
320 return new Uri[]{ uri };
321 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700322 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
323 final int numIds = ids == null ? 0 : ids.length;
324 final Uri[] uris = new Uri[numIds];
325 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700326 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
327 }
328 return uris;
329 }
330
331 @Override
332 public boolean onKeyDown(int keyCode, KeyEvent event) {
333 switch (keyCode) {
334 case KeyEvent.KEYCODE_CALL: {
335 // Make sure phone isn't already busy before starting direct call
336 TelephonyManager tm = (TelephonyManager)
337 getSystemService(Context.TELEPHONY_SERVICE);
338 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700339 DialerUtils.startActivityWithErrorToast(this,
340 CallUtil.getCallIntent(Uri.fromParts(CallUtil.SCHEME_TEL, mNumber,
Nancy Chen87ba4892014-06-11 17:56:07 -0700341 null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700342 return true;
343 }
344 }
345 }
346
347 return super.onKeyDown(keyCode, event);
348 }
349
350 /**
351 * Update user interface with details of given call.
352 *
353 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
354 */
355 private void updateData(final Uri... callUris) {
356 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
357 @Override
358 public PhoneCallDetails[] doInBackground(Void... params) {
359 // TODO: All phone calls correspond to the same person, so we can make a single
360 // lookup.
361 final int numCalls = callUris.length;
362 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
363 try {
364 for (int index = 0; index < numCalls; ++index) {
365 details[index] = getPhoneCallDetailsForUri(callUris[index]);
366 }
367 return details;
368 } catch (IllegalArgumentException e) {
369 // Something went wrong reading in our primary data.
370 Log.w(TAG, "invalid URI starting call details", e);
371 return null;
372 }
373 }
374
375 @Override
376 public void onPostExecute(PhoneCallDetails[] details) {
377 if (details == null) {
378 // Somewhere went wrong: we're going to bail out and show error to users.
379 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
380 Toast.LENGTH_SHORT).show();
381 finish();
382 return;
383 }
384
385 // We know that all calls are from the same number and the same contact, so pick the
386 // first.
387 PhoneCallDetails firstDetails = details[0];
388 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700389 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390 final Uri contactUri = firstDetails.contactUri;
391 final Uri photoUri = firstDetails.photoUri;
392
Chiao Cheng94b10b52012-08-17 16:59:12 -0700393 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700394 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700395 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
396 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
397 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
398 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700399
Tyler Gunn18164c82014-06-09 10:20:57 -0700400 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700401
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800402 final CharSequence displayNumber =
403 mPhoneNumberHelper.getDisplayNumber(
404 firstDetails.number,
405 firstDetails.numberPresentation,
406 firstDetails.formattedNumber);
407
Tyler Gunn18164c82014-06-09 10:20:57 -0700408 if (!TextUtils.isEmpty(firstDetails.name)) {
409 mCallerName.setText(firstDetails.name);
410 mCallerNumber.setText(callLocationOrType + " " + displayNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700411 } else {
Tyler Gunn18164c82014-06-09 10:20:57 -0700412 mCallerName.setText(displayNumber);
413 if (!TextUtils.isEmpty(callLocationOrType)) {
414 mCallerNumber.setText(callLocationOrType);
415 mCallerNumber.setVisibility(View.VISIBLE);
416 } else {
417 mCallerNumber.setVisibility(View.GONE);
418 }
419
Chiao Cheng94b10b52012-08-17 16:59:12 -0700420 }
421
422 mHasEditNumberBeforeCallOption =
423 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
424 mHasTrashOption = hasVoicemail();
425 mHasRemoveFromCallLogOption = !hasVoicemail();
426 invalidateOptionsMenu();
427
428 ListView historyList = (ListView) findViewById(R.id.history);
429 historyList.setAdapter(
430 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
Tyler Gunn18164c82014-06-09 10:20:57 -0700431 mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700432
Tyler Gunn18164c82014-06-09 10:20:57 -0700433 String lookupKey = contactUri == null ? null
434 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800435
436 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
437
438 final int contactType =
439 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
440 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
441 ContactPhotoManager.TYPE_DEFAULT;
442
Tyler Gunn18164c82014-06-09 10:20:57 -0700443 String nameForDefaultImage;
444 if (TextUtils.isEmpty(firstDetails.name)) {
445 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(firstDetails.number,
446 firstDetails.numberPresentation,
447 firstDetails.formattedNumber).toString();
448 } else {
449 nameForDefaultImage = firstDetails.name.toString();
450 }
451
452 loadContactPhotos(
453 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700454 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
455 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700456
457 /**
458 * Determines the location geocode text for a call, or the phone number type
459 * (if available).
460 *
461 * @param details The call details.
462 * @return The phone number type or location.
463 */
464 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
465 if (!TextUtils.isEmpty(details.name)) {
466 return Phone.getTypeLabel(mResources, details.numberType,
467 details.numberLabel);
468 } else {
469 return details.geocode;
470 }
471 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700472 }
473 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
474 }
475
476 /** Return the phone call details for a given call log URI. */
477 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
478 ContentResolver resolver = getContentResolver();
479 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
480 try {
481 if (callCursor == null || !callCursor.moveToFirst()) {
482 throw new IllegalArgumentException("Cannot find content: " + callUri);
483 }
484
485 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700486 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
487 final int numberPresentation = callCursor.getInt(
488 NUMBER_PRESENTATION_COLUMN_INDEX);
489 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
490 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
491 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700492 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
493 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700494 final Drawable accountIcon = getAccountIcon(callCursor);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700495
496 if (TextUtils.isEmpty(countryIso)) {
497 countryIso = mDefaultCountryIso;
498 }
499
500 // Formatted phone number.
501 final CharSequence formattedNumber;
502 // Read contact specifics.
503 final CharSequence nameText;
504 final int numberType;
505 final CharSequence numberLabel;
506 final Uri photoUri;
507 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800508 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700509 // If this is not a regular number, there is no point in looking it up in the contacts.
510 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700511 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700512 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700513 ? mContactInfoHelper.lookupNumber(number, countryIso)
514 : null;
515 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700516 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
517 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700518 nameText = "";
519 numberType = 0;
520 numberLabel = "";
521 photoUri = null;
522 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800523 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700524 } else {
525 formattedNumber = info.formattedNumber;
526 nameText = info.name;
527 numberType = info.type;
528 numberLabel = info.label;
529 photoUri = info.photoUri;
530 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800531 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700532 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700533 final int features = callCursor.getInt(FEATURES);
534 Long dataUsage = null;
535 if (!callCursor.isNull(DATA_USAGE)) {
536 dataUsage = callCursor.getLong(DATA_USAGE);
537 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700538 return new PhoneCallDetails(number, numberPresentation,
539 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700540 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700541 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700542 accountIcon, features, dataUsage);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700543 } finally {
544 if (callCursor != null) {
545 callCursor.close();
546 }
547 }
548 }
549
Nancy Chen87ba4892014-06-11 17:56:07 -0700550 /**
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700551 * Generate account object from data in Telecomm database
Nancy Chen87ba4892014-06-11 17:56:07 -0700552 */
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700553 private Drawable getAccountIcon(Cursor c) {
554 final String component_name = c.getString(ACCOUNT_COMPONENT_NAME);
555 final String account_id = c.getString(ACCOUNT_ID);
Nancy Chen87ba4892014-06-11 17:56:07 -0700556
557 // TODO: actually pull data from the database
558 return null;
559 }
560
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700562 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
563 String lookupKey, int contactType) {
564
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800565 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700566 contactType, true /* isCircular */);
567
568 mQuickContactBadge.assignContactUri(contactUri);
569 mQuickContactBadge.setContentDescription(
570 mResources.getString(R.string.description_contact_details, displayName));
571
572 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
573 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700574 }
575
576 static final class ViewEntry {
577 public final String text;
578 public final Intent primaryIntent;
579 /** The description for accessibility of the primary action. */
580 public final String primaryDescription;
581
582 public CharSequence label = null;
583 /** Icon for the secondary action. */
584 public int secondaryIcon = 0;
585 /** Intent for the secondary action. If not null, an icon must be defined. */
586 public Intent secondaryIntent = null;
587 /** The description for accessibility of the secondary action. */
588 public String secondaryDescription = null;
589
590 public ViewEntry(String text, Intent intent, String description) {
591 this.text = text;
592 primaryIntent = intent;
593 primaryDescription = description;
594 }
595
596 public void setSecondaryAction(int icon, Intent intent, String description) {
597 secondaryIcon = icon;
598 secondaryIntent = intent;
599 secondaryDescription = description;
600 }
601 }
602
Chiao Cheng94b10b52012-08-17 16:59:12 -0700603 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
604 if (statusCursor == null) {
605 mStatusMessageView.setVisibility(View.GONE);
606 return;
607 }
608 final StatusMessage message = getStatusMessage(statusCursor);
609 if (message == null || !message.showInCallDetails()) {
610 mStatusMessageView.setVisibility(View.GONE);
611 return;
612 }
613
614 mStatusMessageView.setVisibility(View.VISIBLE);
615 mStatusMessageText.setText(message.callDetailsMessageId);
616 if (message.actionMessageId != -1) {
617 mStatusMessageAction.setText(message.actionMessageId);
618 }
619 if (message.actionUri != null) {
620 mStatusMessageAction.setClickable(true);
621 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
622 @Override
623 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700624 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
625 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700626 }
627 });
628 } else {
629 mStatusMessageAction.setClickable(false);
630 }
631 }
632
633 private StatusMessage getStatusMessage(Cursor statusCursor) {
634 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
635 if (messages.size() == 0) {
636 return null;
637 }
638 // There can only be a single status message per source package, so num of messages can
639 // at most be 1.
640 if (messages.size() > 1) {
641 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
642 " Will use the first one.", messages.size()));
643 }
644 return messages.get(0);
645 }
646
647 @Override
648 public boolean onCreateOptionsMenu(Menu menu) {
649 getMenuInflater().inflate(R.menu.call_details_options, menu);
650 return super.onCreateOptionsMenu(menu);
651 }
652
653 @Override
654 public boolean onPrepareOptionsMenu(Menu menu) {
655 // This action deletes all elements in the group from the call log.
656 // We don't have this action for voicemails, because you can just use the trash button.
657 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
658 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
659 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
660 return super.onPrepareOptionsMenu(menu);
661 }
662
Chiao Cheng94b10b52012-08-17 16:59:12 -0700663 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
664 final StringBuilder callIds = new StringBuilder();
665 for (Uri callUri : getCallLogEntryUris()) {
666 if (callIds.length() != 0) {
667 callIds.append(",");
668 }
669 callIds.append(ContentUris.parseId(callUri));
670 }
671 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
672 new AsyncTask<Void, Void, Void>() {
673 @Override
674 public Void doInBackground(Void... params) {
675 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
676 Calls._ID + " IN (" + callIds + ")", null);
677 return null;
678 }
679
680 @Override
681 public void onPostExecute(Void result) {
682 finish();
683 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700684 }
685 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700686 }
687
688 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700689 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700690 }
691
692 public void onMenuTrashVoicemail(MenuItem menuItem) {
693 final Uri voicemailUri = getVoicemailUri();
694 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
695 new AsyncTask<Void, Void, Void>() {
696 @Override
697 public Void doInBackground(Void... params) {
698 getContentResolver().delete(voicemailUri, null, null);
699 return null;
700 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700701
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702 @Override
703 public void onPostExecute(Void result) {
704 finish();
705 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700706 }
707 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700708 }
709
710 @Override
711 protected void onPause() {
712 // Immediately stop the proximity sensor.
713 disableProximitySensor(false);
714 mProximitySensorListener.clearPendingRequests();
715 super.onPause();
716 }
717
718 @Override
719 public void enableProximitySensor() {
720 mProximitySensorManager.enable();
721 }
722
723 @Override
724 public void disableProximitySensor(boolean waitForFarState) {
725 mProximitySensorManager.disable(waitForFarState);
726 }
727
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 private void closeSystemDialogs() {
729 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
730 }
731
Chiao Cheng35071c02012-10-15 18:36:24 -0700732 /** Returns the given text, forced to be left-to-right. */
733 private static CharSequence forceLeftToRight(CharSequence text) {
734 StringBuilder sb = new StringBuilder();
735 sb.append(LEFT_TO_RIGHT_EMBEDDING);
736 sb.append(text);
737 sb.append(POP_DIRECTIONAL_FORMATTING);
738 return sb.toString();
739 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700740}