blob: fa8d5ed28715064414c41ccdcead3b790443f7da [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;
53import com.android.dialer.calllog.CallTypeHelper;
54import com.android.dialer.calllog.ContactInfo;
55import com.android.dialer.calllog.ContactInfoHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -080056import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070057import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070058import com.android.dialer.util.AsyncTaskExecutor;
59import com.android.dialer.util.AsyncTaskExecutors;
Yorke Lee39213592014-04-07 15:39:48 -070060import com.android.dialer.util.DialerUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061import com.android.dialer.voicemail.VoicemailPlaybackFragment;
62import com.android.dialer.voicemail.VoicemailStatusHelper;
63import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
64import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
65
66import java.util.List;
67
68/**
69 * Displays the details of a specific call log entry.
70 * <p>
71 * This activity can be either started with the URI of a single call log entry, or with the
72 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
73 */
74public class CallDetailActivity extends Activity implements ProximitySensorAware {
75 private static final String TAG = "CallDetail";
76
Yorke Lee03459442013-10-30 18:29:32 -070077 private static final int LOADER_ID = 0;
78 private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
79
Chiao Cheng35071c02012-10-15 18:36:24 -070080 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
81 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
82
Chiao Cheng94b10b52012-08-17 16:59:12 -070083 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
84 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
85 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
86 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
87
88 /** The enumeration of {@link AsyncTask} objects used in this class. */
89 public enum Tasks {
90 MARK_VOICEMAIL_READ,
91 DELETE_VOICEMAIL_AND_FINISH,
92 REMOVE_FROM_CALL_LOG_AND_FINISH,
93 UPDATE_PHONE_CALL_DETAILS,
94 }
95
96 /** A long array extra containing ids of call log entries to display. */
97 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
98 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
99 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
100 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
101 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
102 /** If the activity was triggered from a notification. */
103 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
104
105 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800106 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700107 private QuickContactBadge mQuickContactBadge;
108 private TextView mCallerName;
109 private TextView mCallerNumber;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700110 private AsyncTaskExecutor mAsyncTaskExecutor;
111 private ContactInfoHelper mContactInfoHelper;
112
113 private String mNumber = null;
114 private String mDefaultCountryIso;
115
116 /* package */ LayoutInflater mInflater;
117 /* package */ Resources mResources;
118 /** Helper to load contact photos. */
119 private ContactPhotoManager mContactPhotoManager;
120 /** Helper to make async queries to content resolver. */
121 private CallDetailActivityQueryHandler mAsyncQueryHandler;
122 /** Helper to get voicemail status messages. */
123 private VoicemailStatusHelper mVoicemailStatusHelper;
124 // Views related to voicemail status message.
125 private View mStatusMessageView;
126 private TextView mStatusMessageText;
127 private TextView mStatusMessageAction;
128
129 /** Whether we should show "edit number before call" in the options menu. */
130 private boolean mHasEditNumberBeforeCallOption;
131 /** Whether we should show "trash" in the options menu. */
132 private boolean mHasTrashOption;
133 /** Whether we should show "remove from call log" in the options menu. */
134 private boolean mHasRemoveFromCallLogOption;
135
136 private ProximitySensorManager mProximitySensorManager;
137 private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
138
Chiao Cheng94b10b52012-08-17 16:59:12 -0700139 /** Listener to changes in the proximity sensor state. */
140 private class ProximitySensorListener implements ProximitySensorManager.Listener {
141 /** Used to show a blank view and hide the action bar. */
142 private final Runnable mBlankRunnable = new Runnable() {
143 @Override
144 public void run() {
145 View blankView = findViewById(R.id.blank);
146 blankView.setVisibility(View.VISIBLE);
147 getActionBar().hide();
148 }
149 };
150 /** Used to remove the blank view and show the action bar. */
151 private final Runnable mUnblankRunnable = new Runnable() {
152 @Override
153 public void run() {
154 View blankView = findViewById(R.id.blank);
155 blankView.setVisibility(View.GONE);
156 getActionBar().show();
157 }
158 };
159
160 @Override
161 public synchronized void onNear() {
162 clearPendingRequests();
163 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
164 }
165
166 @Override
167 public synchronized void onFar() {
168 clearPendingRequests();
169 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
170 }
171
172 /** Removed any delayed requests that may be pending. */
173 public synchronized void clearPendingRequests() {
174 View blankView = findViewById(R.id.blank);
175 blankView.removeCallbacks(mBlankRunnable);
176 blankView.removeCallbacks(mUnblankRunnable);
177 }
178
179 /** Post a {@link Runnable} with a delay on the main thread. */
180 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
181 // Post these instead of executing immediately so that:
182 // - They are guaranteed to be executed on the main thread.
183 // - If the sensor values changes rapidly for some time, the UI will not be
184 // updated immediately.
185 View blankView = findViewById(R.id.blank);
186 blankView.postDelayed(runnable, delayMillis);
187 }
188 }
189
190 static final String[] CALL_LOG_PROJECTION = new String[] {
191 CallLog.Calls.DATE,
192 CallLog.Calls.DURATION,
193 CallLog.Calls.NUMBER,
194 CallLog.Calls.TYPE,
195 CallLog.Calls.COUNTRY_ISO,
196 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700197 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700198 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
199 CallLog.Calls.PHONE_ACCOUNT_ID,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700200 };
201
202 static final int DATE_COLUMN_INDEX = 0;
203 static final int DURATION_COLUMN_INDEX = 1;
204 static final int NUMBER_COLUMN_INDEX = 2;
205 static final int CALL_TYPE_COLUMN_INDEX = 3;
206 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
207 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700208 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700209 static final int ACCOUNT_COMPONENT_NAME = 7;
210 static final int ACCOUNT_ID = 8;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700211
Chiao Cheng94b10b52012-08-17 16:59:12 -0700212 @Override
213 protected void onCreate(Bundle icicle) {
214 super.onCreate(icicle);
215
216 setContentView(R.layout.call_detail);
217
218 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
219 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
220 mResources = getResources();
221
222 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee24ec3192013-11-19 13:52:45 -0800223 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700224 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
225 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700226 mStatusMessageView = findViewById(R.id.voicemail_status);
227 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
228 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Tyler Gunn18164c82014-06-09 10:20:57 -0700229 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
230 mQuickContactBadge.setOverlay(null);
231 mCallerName = (TextView) findViewById(R.id.caller_name);
232 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Chiao Cheng35071c02012-10-15 18:36:24 -0700233 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700234 mContactPhotoManager = ContactPhotoManager.getInstance(this);
235 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700236 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700237 getActionBar().setDisplayHomeAsUpEnabled(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700238 optionallyHandleVoicemail();
239 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
240 closeSystemDialogs();
241 }
242 }
243
244 @Override
245 public void onResume() {
246 super.onResume();
247 updateData(getCallLogEntryUris());
248 }
249
250 /**
251 * Handle voicemail playback or hide voicemail ui.
252 * <p>
253 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
254 * playback. If it doesn't, then hide the voicemail ui.
255 */
256 private void optionallyHandleVoicemail() {
257 View voicemailContainer = findViewById(R.id.voicemail_container);
258 if (hasVoicemail()) {
259 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
260 // to play and optionally start the playback.
261 // Do a query to fetch the voicemail status messages.
262 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
263 Bundle fragmentArguments = new Bundle();
264 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
265 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
266 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
267 }
268 playbackFragment.setArguments(fragmentArguments);
269 voicemailContainer.setVisibility(View.VISIBLE);
270 getFragmentManager().beginTransaction()
Yorke Leeaa536fd2013-07-29 11:31:04 -0700271 .add(R.id.voicemail_container, playbackFragment)
272 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
274 markVoicemailAsRead(getVoicemailUri());
275 } else {
276 // No voicemail uri: hide the status view.
277 mStatusMessageView.setVisibility(View.GONE);
278 voicemailContainer.setVisibility(View.GONE);
279 }
280 }
281
282 private boolean hasVoicemail() {
283 return getVoicemailUri() != null;
284 }
285
286 private Uri getVoicemailUri() {
287 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
288 }
289
290 private void markVoicemailAsRead(final Uri voicemailUri) {
291 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
292 @Override
293 public Void doInBackground(Void... params) {
294 ContentValues values = new ContentValues();
295 values.put(Voicemails.IS_READ, true);
296 getContentResolver().update(voicemailUri, values,
297 Voicemails.IS_READ + " = 0", null);
298 return null;
299 }
300 });
301 }
302
303 /**
304 * Returns the list of URIs to show.
305 * <p>
306 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
307 * a list of ids in the call log added as an extra on the URI.
308 * <p>
309 * If both are available, the data on the intent takes precedence.
310 */
311 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700312 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700313 if (uri != null) {
314 // If there is a data on the intent, it takes precedence over the extra.
315 return new Uri[]{ uri };
316 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700317 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
318 final int numIds = ids == null ? 0 : ids.length;
319 final Uri[] uris = new Uri[numIds];
320 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
322 }
323 return uris;
324 }
325
326 @Override
327 public boolean onKeyDown(int keyCode, KeyEvent event) {
328 switch (keyCode) {
329 case KeyEvent.KEYCODE_CALL: {
330 // Make sure phone isn't already busy before starting direct call
331 TelephonyManager tm = (TelephonyManager)
332 getSystemService(Context.TELEPHONY_SERVICE);
333 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700334 DialerUtils.startActivityWithErrorToast(this,
335 CallUtil.getCallIntent(Uri.fromParts(CallUtil.SCHEME_TEL, mNumber,
Nancy Chen87ba4892014-06-11 17:56:07 -0700336 null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700337 return true;
338 }
339 }
340 }
341
342 return super.onKeyDown(keyCode, event);
343 }
344
345 /**
346 * Update user interface with details of given call.
347 *
348 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
349 */
350 private void updateData(final Uri... callUris) {
351 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
352 @Override
353 public PhoneCallDetails[] doInBackground(Void... params) {
354 // TODO: All phone calls correspond to the same person, so we can make a single
355 // lookup.
356 final int numCalls = callUris.length;
357 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
358 try {
359 for (int index = 0; index < numCalls; ++index) {
360 details[index] = getPhoneCallDetailsForUri(callUris[index]);
361 }
362 return details;
363 } catch (IllegalArgumentException e) {
364 // Something went wrong reading in our primary data.
365 Log.w(TAG, "invalid URI starting call details", e);
366 return null;
367 }
368 }
369
370 @Override
371 public void onPostExecute(PhoneCallDetails[] details) {
372 if (details == null) {
373 // Somewhere went wrong: we're going to bail out and show error to users.
374 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
375 Toast.LENGTH_SHORT).show();
376 finish();
377 return;
378 }
379
380 // We know that all calls are from the same number and the same contact, so pick the
381 // first.
382 PhoneCallDetails firstDetails = details[0];
383 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700384 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700385 final Uri contactUri = firstDetails.contactUri;
386 final Uri photoUri = firstDetails.photoUri;
387
Chiao Cheng94b10b52012-08-17 16:59:12 -0700388 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700389 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700390 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
391 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
392 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
393 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700394
Tyler Gunn18164c82014-06-09 10:20:57 -0700395 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700396
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800397 final CharSequence displayNumber =
398 mPhoneNumberHelper.getDisplayNumber(
399 firstDetails.number,
400 firstDetails.numberPresentation,
401 firstDetails.formattedNumber);
402
Tyler Gunn18164c82014-06-09 10:20:57 -0700403 if (!TextUtils.isEmpty(firstDetails.name)) {
404 mCallerName.setText(firstDetails.name);
405 mCallerNumber.setText(callLocationOrType + " " + displayNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700406 } else {
Tyler Gunn18164c82014-06-09 10:20:57 -0700407 mCallerName.setText(displayNumber);
408 if (!TextUtils.isEmpty(callLocationOrType)) {
409 mCallerNumber.setText(callLocationOrType);
410 mCallerNumber.setVisibility(View.VISIBLE);
411 } else {
412 mCallerNumber.setVisibility(View.GONE);
413 }
414
Chiao Cheng94b10b52012-08-17 16:59:12 -0700415 }
416
417 mHasEditNumberBeforeCallOption =
418 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
419 mHasTrashOption = hasVoicemail();
420 mHasRemoveFromCallLogOption = !hasVoicemail();
421 invalidateOptionsMenu();
422
423 ListView historyList = (ListView) findViewById(R.id.history);
424 historyList.setAdapter(
425 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
Tyler Gunn18164c82014-06-09 10:20:57 -0700426 mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700427
Tyler Gunn18164c82014-06-09 10:20:57 -0700428 String lookupKey = contactUri == null ? null
429 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800430
431 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
432
433 final int contactType =
434 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
435 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
436 ContactPhotoManager.TYPE_DEFAULT;
437
Tyler Gunn18164c82014-06-09 10:20:57 -0700438 String nameForDefaultImage;
439 if (TextUtils.isEmpty(firstDetails.name)) {
440 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(firstDetails.number,
441 firstDetails.numberPresentation,
442 firstDetails.formattedNumber).toString();
443 } else {
444 nameForDefaultImage = firstDetails.name.toString();
445 }
446
447 loadContactPhotos(
448 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700449 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
450 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700451
452 /**
453 * Determines the location geocode text for a call, or the phone number type
454 * (if available).
455 *
456 * @param details The call details.
457 * @return The phone number type or location.
458 */
459 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
460 if (!TextUtils.isEmpty(details.name)) {
461 return Phone.getTypeLabel(mResources, details.numberType,
462 details.numberLabel);
463 } else {
464 return details.geocode;
465 }
466 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700467 }
468 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
469 }
470
471 /** Return the phone call details for a given call log URI. */
472 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
473 ContentResolver resolver = getContentResolver();
474 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
475 try {
476 if (callCursor == null || !callCursor.moveToFirst()) {
477 throw new IllegalArgumentException("Cannot find content: " + callUri);
478 }
479
480 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700481 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
482 final int numberPresentation = callCursor.getInt(
483 NUMBER_PRESENTATION_COLUMN_INDEX);
484 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
485 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
486 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700487 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
488 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700489 final Drawable accountIcon = getAccountIcon(callCursor);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700490
491 if (TextUtils.isEmpty(countryIso)) {
492 countryIso = mDefaultCountryIso;
493 }
494
495 // Formatted phone number.
496 final CharSequence formattedNumber;
497 // Read contact specifics.
498 final CharSequence nameText;
499 final int numberType;
500 final CharSequence numberLabel;
501 final Uri photoUri;
502 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800503 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700504 // If this is not a regular number, there is no point in looking it up in the contacts.
505 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700506 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700507 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700508 ? mContactInfoHelper.lookupNumber(number, countryIso)
509 : null;
510 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700511 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
512 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700513 nameText = "";
514 numberType = 0;
515 numberLabel = "";
516 photoUri = null;
517 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800518 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700519 } else {
520 formattedNumber = info.formattedNumber;
521 nameText = info.name;
522 numberType = info.type;
523 numberLabel = info.label;
524 photoUri = info.photoUri;
525 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800526 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700527 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700528 return new PhoneCallDetails(number, numberPresentation,
529 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700530 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700531 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700532 accountIcon);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700533 } finally {
534 if (callCursor != null) {
535 callCursor.close();
536 }
537 }
538 }
539
Nancy Chen87ba4892014-06-11 17:56:07 -0700540 /**
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700541 * Generate account object from data in Telecomm database
Nancy Chen87ba4892014-06-11 17:56:07 -0700542 */
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700543 private Drawable getAccountIcon(Cursor c) {
544 final String component_name = c.getString(ACCOUNT_COMPONENT_NAME);
545 final String account_id = c.getString(ACCOUNT_ID);
Nancy Chen87ba4892014-06-11 17:56:07 -0700546
547 // TODO: actually pull data from the database
548 return null;
549 }
550
Chiao Cheng94b10b52012-08-17 16:59:12 -0700551 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700552 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
553 String lookupKey, int contactType) {
554
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800555 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700556 contactType, true /* isCircular */);
557
558 mQuickContactBadge.assignContactUri(contactUri);
559 mQuickContactBadge.setContentDescription(
560 mResources.getString(R.string.description_contact_details, displayName));
561
562 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
563 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700564 }
565
566 static final class ViewEntry {
567 public final String text;
568 public final Intent primaryIntent;
569 /** The description for accessibility of the primary action. */
570 public final String primaryDescription;
571
572 public CharSequence label = null;
573 /** Icon for the secondary action. */
574 public int secondaryIcon = 0;
575 /** Intent for the secondary action. If not null, an icon must be defined. */
576 public Intent secondaryIntent = null;
577 /** The description for accessibility of the secondary action. */
578 public String secondaryDescription = null;
579
580 public ViewEntry(String text, Intent intent, String description) {
581 this.text = text;
582 primaryIntent = intent;
583 primaryDescription = description;
584 }
585
586 public void setSecondaryAction(int icon, Intent intent, String description) {
587 secondaryIcon = icon;
588 secondaryIntent = intent;
589 secondaryDescription = description;
590 }
591 }
592
Chiao Cheng94b10b52012-08-17 16:59:12 -0700593 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
594 if (statusCursor == null) {
595 mStatusMessageView.setVisibility(View.GONE);
596 return;
597 }
598 final StatusMessage message = getStatusMessage(statusCursor);
599 if (message == null || !message.showInCallDetails()) {
600 mStatusMessageView.setVisibility(View.GONE);
601 return;
602 }
603
604 mStatusMessageView.setVisibility(View.VISIBLE);
605 mStatusMessageText.setText(message.callDetailsMessageId);
606 if (message.actionMessageId != -1) {
607 mStatusMessageAction.setText(message.actionMessageId);
608 }
609 if (message.actionUri != null) {
610 mStatusMessageAction.setClickable(true);
611 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
612 @Override
613 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700614 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
615 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700616 }
617 });
618 } else {
619 mStatusMessageAction.setClickable(false);
620 }
621 }
622
623 private StatusMessage getStatusMessage(Cursor statusCursor) {
624 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
625 if (messages.size() == 0) {
626 return null;
627 }
628 // There can only be a single status message per source package, so num of messages can
629 // at most be 1.
630 if (messages.size() > 1) {
631 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
632 " Will use the first one.", messages.size()));
633 }
634 return messages.get(0);
635 }
636
637 @Override
638 public boolean onCreateOptionsMenu(Menu menu) {
639 getMenuInflater().inflate(R.menu.call_details_options, menu);
640 return super.onCreateOptionsMenu(menu);
641 }
642
643 @Override
644 public boolean onPrepareOptionsMenu(Menu menu) {
645 // This action deletes all elements in the group from the call log.
646 // We don't have this action for voicemails, because you can just use the trash button.
647 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
648 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
649 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
650 return super.onPrepareOptionsMenu(menu);
651 }
652
Chiao Cheng94b10b52012-08-17 16:59:12 -0700653 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
654 final StringBuilder callIds = new StringBuilder();
655 for (Uri callUri : getCallLogEntryUris()) {
656 if (callIds.length() != 0) {
657 callIds.append(",");
658 }
659 callIds.append(ContentUris.parseId(callUri));
660 }
661 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
662 new AsyncTask<Void, Void, Void>() {
663 @Override
664 public Void doInBackground(Void... params) {
665 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
666 Calls._ID + " IN (" + callIds + ")", null);
667 return null;
668 }
669
670 @Override
671 public void onPostExecute(Void result) {
672 finish();
673 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700674 }
675 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700676 }
677
678 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700679 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700680 }
681
682 public void onMenuTrashVoicemail(MenuItem menuItem) {
683 final Uri voicemailUri = getVoicemailUri();
684 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
685 new AsyncTask<Void, Void, Void>() {
686 @Override
687 public Void doInBackground(Void... params) {
688 getContentResolver().delete(voicemailUri, null, null);
689 return null;
690 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700691
Chiao Cheng94b10b52012-08-17 16:59:12 -0700692 @Override
693 public void onPostExecute(Void result) {
694 finish();
695 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700696 }
697 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700698 }
699
700 @Override
701 protected void onPause() {
702 // Immediately stop the proximity sensor.
703 disableProximitySensor(false);
704 mProximitySensorListener.clearPendingRequests();
705 super.onPause();
706 }
707
708 @Override
709 public void enableProximitySensor() {
710 mProximitySensorManager.enable();
711 }
712
713 @Override
714 public void disableProximitySensor(boolean waitForFarState) {
715 mProximitySensorManager.disable(waitForFarState);
716 }
717
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 private void closeSystemDialogs() {
719 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
720 }
721
Chiao Cheng35071c02012-10-15 18:36:24 -0700722 /** Returns the given text, forced to be left-to-right. */
723 private static CharSequence forceLeftToRight(CharSequence text) {
724 StringBuilder sb = new StringBuilder();
725 sb.append(LEFT_TO_RIGHT_EMBEDDING);
726 sb.append(text);
727 sb.append(POP_DIRECTIONAL_FORMATTING);
728 return sb.toString();
729 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730}