blob: d07060111c4fafc88497f3beb461dafa63a0188b [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;
Nancy Chen87ba4892014-06-11 17:56:07 -070035import android.telecomm.Subscription;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import android.telephony.TelephonyManager;
37import android.text.TextUtils;
38import android.util.Log;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.view.KeyEvent;
40import android.view.LayoutInflater;
41import android.view.Menu;
42import android.view.MenuItem;
43import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.widget.ListView;
Tyler Gunn18164c82014-06-09 10:20:57 -070045import android.widget.QuickContactBadge;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.widget.TextView;
47import android.widget.Toast;
48
Chiao Cheng35071c02012-10-15 18:36:24 -070049import com.android.contacts.common.ContactPhotoManager;
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070050import com.android.contacts.common.CallUtil;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080051import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070052import com.android.contacts.common.GeoUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import com.android.dialer.calllog.CallDetailHistoryAdapter;
54import 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,
Nancy Chen87ba4892014-06-11 17:56:07 -0700199 CallLog.Calls.SUBSCRIPTION_COMPONENT_NAME,
200 CallLog.Calls.SUBSCRIPTION_ID,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700201 };
202
203 static final int DATE_COLUMN_INDEX = 0;
204 static final int DURATION_COLUMN_INDEX = 1;
205 static final int NUMBER_COLUMN_INDEX = 2;
206 static final int CALL_TYPE_COLUMN_INDEX = 3;
207 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
208 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700209 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Nancy Chen87ba4892014-06-11 17:56:07 -0700210 static final int SUBSCRIPTION_COMPONENT_NAME = 7;
211 static final int SUBSCRIPTION_ID = 8;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700212
Chiao Cheng94b10b52012-08-17 16:59:12 -0700213 @Override
214 protected void onCreate(Bundle icicle) {
215 super.onCreate(icicle);
216
217 setContentView(R.layout.call_detail);
218
219 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
220 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
221 mResources = getResources();
222
223 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee24ec3192013-11-19 13:52:45 -0800224 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700225 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
226 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700227 mStatusMessageView = findViewById(R.id.voicemail_status);
228 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
229 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Tyler Gunn18164c82014-06-09 10:20:57 -0700230 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
231 mQuickContactBadge.setOverlay(null);
232 mCallerName = (TextView) findViewById(R.id.caller_name);
233 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Chiao Cheng35071c02012-10-15 18:36:24 -0700234 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700235 mContactPhotoManager = ContactPhotoManager.getInstance(this);
236 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700237 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700238 getActionBar().setDisplayHomeAsUpEnabled(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239 optionallyHandleVoicemail();
240 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
241 closeSystemDialogs();
242 }
243 }
244
245 @Override
246 public void onResume() {
247 super.onResume();
248 updateData(getCallLogEntryUris());
249 }
250
251 /**
252 * Handle voicemail playback or hide voicemail ui.
253 * <p>
254 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
255 * playback. If it doesn't, then hide the voicemail ui.
256 */
257 private void optionallyHandleVoicemail() {
258 View voicemailContainer = findViewById(R.id.voicemail_container);
259 if (hasVoicemail()) {
260 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
261 // to play and optionally start the playback.
262 // Do a query to fetch the voicemail status messages.
263 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
264 Bundle fragmentArguments = new Bundle();
265 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
266 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
267 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
268 }
269 playbackFragment.setArguments(fragmentArguments);
270 voicemailContainer.setVisibility(View.VISIBLE);
271 getFragmentManager().beginTransaction()
Yorke Leeaa536fd2013-07-29 11:31:04 -0700272 .add(R.id.voicemail_container, playbackFragment)
273 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700274 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
275 markVoicemailAsRead(getVoicemailUri());
276 } else {
277 // No voicemail uri: hide the status view.
278 mStatusMessageView.setVisibility(View.GONE);
279 voicemailContainer.setVisibility(View.GONE);
280 }
281 }
282
283 private boolean hasVoicemail() {
284 return getVoicemailUri() != null;
285 }
286
287 private Uri getVoicemailUri() {
288 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
289 }
290
291 private void markVoicemailAsRead(final Uri voicemailUri) {
292 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
293 @Override
294 public Void doInBackground(Void... params) {
295 ContentValues values = new ContentValues();
296 values.put(Voicemails.IS_READ, true);
297 getContentResolver().update(voicemailUri, values,
298 Voicemails.IS_READ + " = 0", null);
299 return null;
300 }
301 });
302 }
303
304 /**
305 * Returns the list of URIs to show.
306 * <p>
307 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
308 * a list of ids in the call log added as an extra on the URI.
309 * <p>
310 * If both are available, the data on the intent takes precedence.
311 */
312 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700313 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700314 if (uri != null) {
315 // If there is a data on the intent, it takes precedence over the extra.
316 return new Uri[]{ uri };
317 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700318 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
319 final int numIds = ids == null ? 0 : ids.length;
320 final Uri[] uris = new Uri[numIds];
321 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700322 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
323 }
324 return uris;
325 }
326
327 @Override
328 public boolean onKeyDown(int keyCode, KeyEvent event) {
329 switch (keyCode) {
330 case KeyEvent.KEYCODE_CALL: {
331 // Make sure phone isn't already busy before starting direct call
332 TelephonyManager tm = (TelephonyManager)
333 getSystemService(Context.TELEPHONY_SERVICE);
334 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700335 DialerUtils.startActivityWithErrorToast(this,
336 CallUtil.getCallIntent(Uri.fromParts(CallUtil.SCHEME_TEL, mNumber,
Nancy Chen87ba4892014-06-11 17:56:07 -0700337 null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700338 return true;
339 }
340 }
341 }
342
343 return super.onKeyDown(keyCode, event);
344 }
345
346 /**
347 * Update user interface with details of given call.
348 *
349 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
350 */
351 private void updateData(final Uri... callUris) {
352 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
353 @Override
354 public PhoneCallDetails[] doInBackground(Void... params) {
355 // TODO: All phone calls correspond to the same person, so we can make a single
356 // lookup.
357 final int numCalls = callUris.length;
358 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
359 try {
360 for (int index = 0; index < numCalls; ++index) {
361 details[index] = getPhoneCallDetailsForUri(callUris[index]);
362 }
363 return details;
364 } catch (IllegalArgumentException e) {
365 // Something went wrong reading in our primary data.
366 Log.w(TAG, "invalid URI starting call details", e);
367 return null;
368 }
369 }
370
371 @Override
372 public void onPostExecute(PhoneCallDetails[] details) {
373 if (details == null) {
374 // Somewhere went wrong: we're going to bail out and show error to users.
375 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
376 Toast.LENGTH_SHORT).show();
377 finish();
378 return;
379 }
380
381 // We know that all calls are from the same number and the same contact, so pick the
382 // first.
383 PhoneCallDetails firstDetails = details[0];
384 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700385 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700386 final Uri contactUri = firstDetails.contactUri;
387 final Uri photoUri = firstDetails.photoUri;
388
Chiao Cheng94b10b52012-08-17 16:59:12 -0700389 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700390 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700391 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
392 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
393 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
394 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700395
Tyler Gunn18164c82014-06-09 10:20:57 -0700396 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700397
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800398 final CharSequence displayNumber =
399 mPhoneNumberHelper.getDisplayNumber(
400 firstDetails.number,
401 firstDetails.numberPresentation,
402 firstDetails.formattedNumber);
403
Tyler Gunn18164c82014-06-09 10:20:57 -0700404 if (!TextUtils.isEmpty(firstDetails.name)) {
405 mCallerName.setText(firstDetails.name);
406 mCallerNumber.setText(callLocationOrType + " " + displayNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700407 } else {
Tyler Gunn18164c82014-06-09 10:20:57 -0700408 mCallerName.setText(displayNumber);
409 if (!TextUtils.isEmpty(callLocationOrType)) {
410 mCallerNumber.setText(callLocationOrType);
411 mCallerNumber.setVisibility(View.VISIBLE);
412 } else {
413 mCallerNumber.setVisibility(View.GONE);
414 }
415
Chiao Cheng94b10b52012-08-17 16:59:12 -0700416 }
417
418 mHasEditNumberBeforeCallOption =
419 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
420 mHasTrashOption = hasVoicemail();
421 mHasRemoveFromCallLogOption = !hasVoicemail();
422 invalidateOptionsMenu();
423
424 ListView historyList = (ListView) findViewById(R.id.history);
425 historyList.setAdapter(
426 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
Tyler Gunn18164c82014-06-09 10:20:57 -0700427 mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700428
Tyler Gunn18164c82014-06-09 10:20:57 -0700429 String lookupKey = contactUri == null ? null
430 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800431
432 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
433
434 final int contactType =
435 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
436 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
437 ContactPhotoManager.TYPE_DEFAULT;
438
Tyler Gunn18164c82014-06-09 10:20:57 -0700439 String nameForDefaultImage;
440 if (TextUtils.isEmpty(firstDetails.name)) {
441 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(firstDetails.number,
442 firstDetails.numberPresentation,
443 firstDetails.formattedNumber).toString();
444 } else {
445 nameForDefaultImage = firstDetails.name.toString();
446 }
447
448 loadContactPhotos(
449 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700450 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
451 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700452
453 /**
454 * Determines the location geocode text for a call, or the phone number type
455 * (if available).
456 *
457 * @param details The call details.
458 * @return The phone number type or location.
459 */
460 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
461 if (!TextUtils.isEmpty(details.name)) {
462 return Phone.getTypeLabel(mResources, details.numberType,
463 details.numberLabel);
464 } else {
465 return details.geocode;
466 }
467 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700468 }
469 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
470 }
471
472 /** Return the phone call details for a given call log URI. */
473 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
474 ContentResolver resolver = getContentResolver();
475 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
476 try {
477 if (callCursor == null || !callCursor.moveToFirst()) {
478 throw new IllegalArgumentException("Cannot find content: " + callUri);
479 }
480
481 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700482 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
483 final int numberPresentation = callCursor.getInt(
484 NUMBER_PRESENTATION_COLUMN_INDEX);
485 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
486 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
487 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700488 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
489 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Nancy Chen87ba4892014-06-11 17:56:07 -0700490 final Drawable subscriptionIcon = getSubscriptionIcon(callCursor);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700491
492 if (TextUtils.isEmpty(countryIso)) {
493 countryIso = mDefaultCountryIso;
494 }
495
496 // Formatted phone number.
497 final CharSequence formattedNumber;
498 // Read contact specifics.
499 final CharSequence nameText;
500 final int numberType;
501 final CharSequence numberLabel;
502 final Uri photoUri;
503 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800504 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700505 // If this is not a regular number, there is no point in looking it up in the contacts.
506 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700507 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700508 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700509 ? mContactInfoHelper.lookupNumber(number, countryIso)
510 : null;
511 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700512 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
513 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700514 nameText = "";
515 numberType = 0;
516 numberLabel = "";
517 photoUri = null;
518 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800519 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700520 } else {
521 formattedNumber = info.formattedNumber;
522 nameText = info.name;
523 numberType = info.type;
524 numberLabel = info.label;
525 photoUri = info.photoUri;
526 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800527 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700528 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700529 return new PhoneCallDetails(number, numberPresentation,
530 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700531 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700532 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
533 subscriptionIcon);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700534 } finally {
535 if (callCursor != null) {
536 callCursor.close();
537 }
538 }
539 }
540
Nancy Chen87ba4892014-06-11 17:56:07 -0700541 /**
542 * Generate subscription object from data in Telecomm database
543 */
544 private Drawable getSubscriptionIcon(Cursor c) {
545 final String component_name = c.getString(SUBSCRIPTION_COMPONENT_NAME);
546 final String subscription_id = c.getString(SUBSCRIPTION_ID);
547
548 // TODO: actually pull data from the database
549 return null;
550 }
551
Chiao Cheng94b10b52012-08-17 16:59:12 -0700552 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700553 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
554 String lookupKey, int contactType) {
555
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800556 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700557 contactType, true /* isCircular */);
558
559 mQuickContactBadge.assignContactUri(contactUri);
560 mQuickContactBadge.setContentDescription(
561 mResources.getString(R.string.description_contact_details, displayName));
562
563 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
564 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700565 }
566
567 static final class ViewEntry {
568 public final String text;
569 public final Intent primaryIntent;
570 /** The description for accessibility of the primary action. */
571 public final String primaryDescription;
572
573 public CharSequence label = null;
574 /** Icon for the secondary action. */
575 public int secondaryIcon = 0;
576 /** Intent for the secondary action. If not null, an icon must be defined. */
577 public Intent secondaryIntent = null;
578 /** The description for accessibility of the secondary action. */
579 public String secondaryDescription = null;
580
581 public ViewEntry(String text, Intent intent, String description) {
582 this.text = text;
583 primaryIntent = intent;
584 primaryDescription = description;
585 }
586
587 public void setSecondaryAction(int icon, Intent intent, String description) {
588 secondaryIcon = icon;
589 secondaryIntent = intent;
590 secondaryDescription = description;
591 }
592 }
593
Chiao Cheng94b10b52012-08-17 16:59:12 -0700594 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
595 if (statusCursor == null) {
596 mStatusMessageView.setVisibility(View.GONE);
597 return;
598 }
599 final StatusMessage message = getStatusMessage(statusCursor);
600 if (message == null || !message.showInCallDetails()) {
601 mStatusMessageView.setVisibility(View.GONE);
602 return;
603 }
604
605 mStatusMessageView.setVisibility(View.VISIBLE);
606 mStatusMessageText.setText(message.callDetailsMessageId);
607 if (message.actionMessageId != -1) {
608 mStatusMessageAction.setText(message.actionMessageId);
609 }
610 if (message.actionUri != null) {
611 mStatusMessageAction.setClickable(true);
612 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
613 @Override
614 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700615 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
616 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700617 }
618 });
619 } else {
620 mStatusMessageAction.setClickable(false);
621 }
622 }
623
624 private StatusMessage getStatusMessage(Cursor statusCursor) {
625 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
626 if (messages.size() == 0) {
627 return null;
628 }
629 // There can only be a single status message per source package, so num of messages can
630 // at most be 1.
631 if (messages.size() > 1) {
632 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
633 " Will use the first one.", messages.size()));
634 }
635 return messages.get(0);
636 }
637
638 @Override
639 public boolean onCreateOptionsMenu(Menu menu) {
640 getMenuInflater().inflate(R.menu.call_details_options, menu);
641 return super.onCreateOptionsMenu(menu);
642 }
643
644 @Override
645 public boolean onPrepareOptionsMenu(Menu menu) {
646 // This action deletes all elements in the group from the call log.
647 // We don't have this action for voicemails, because you can just use the trash button.
648 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
649 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
650 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
651 return super.onPrepareOptionsMenu(menu);
652 }
653
Chiao Cheng94b10b52012-08-17 16:59:12 -0700654 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
655 final StringBuilder callIds = new StringBuilder();
656 for (Uri callUri : getCallLogEntryUris()) {
657 if (callIds.length() != 0) {
658 callIds.append(",");
659 }
660 callIds.append(ContentUris.parseId(callUri));
661 }
662 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
663 new AsyncTask<Void, Void, Void>() {
664 @Override
665 public Void doInBackground(Void... params) {
666 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
667 Calls._ID + " IN (" + callIds + ")", null);
668 return null;
669 }
670
671 @Override
672 public void onPostExecute(Void result) {
673 finish();
674 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700675 }
676 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700677 }
678
679 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700680 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700681 }
682
683 public void onMenuTrashVoicemail(MenuItem menuItem) {
684 final Uri voicemailUri = getVoicemailUri();
685 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
686 new AsyncTask<Void, Void, Void>() {
687 @Override
688 public Void doInBackground(Void... params) {
689 getContentResolver().delete(voicemailUri, null, null);
690 return null;
691 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700692
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693 @Override
694 public void onPostExecute(Void result) {
695 finish();
696 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700697 }
698 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 }
700
701 @Override
702 protected void onPause() {
703 // Immediately stop the proximity sensor.
704 disableProximitySensor(false);
705 mProximitySensorListener.clearPendingRequests();
706 super.onPause();
707 }
708
709 @Override
710 public void enableProximitySensor() {
711 mProximitySensorManager.enable();
712 }
713
714 @Override
715 public void disableProximitySensor(boolean waitForFarState) {
716 mProximitySensorManager.disable(waitForFarState);
717 }
718
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 private void closeSystemDialogs() {
720 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
721 }
722
Chiao Cheng35071c02012-10-15 18:36:24 -0700723 /** Returns the given text, forced to be left-to-right. */
724 private static CharSequence forceLeftToRight(CharSequence text) {
725 StringBuilder sb = new StringBuilder();
726 sb.append(LEFT_TO_RIGHT_EMBEDDING);
727 sb.append(text);
728 sb.append(POP_DIRECTIONAL_FORMATTING);
729 return sb.toString();
730 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700731}