blob: cd5fb3bc7177a4522924a9b3e92f3f457ed38e33 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Chiao Cheng94b10b52012-08-17 16:59:12 -070019import android.app.Activity;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070020import android.content.ComponentName;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.content.ContentResolver;
22import android.content.ContentUris;
23import android.content.ContentValues;
24import android.content.Context;
25import android.content.Intent;
26import android.content.res.Resources;
27import android.database.Cursor;
Nancy Chen87ba4892014-06-11 17:56:07 -070028import android.graphics.drawable.Drawable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.net.Uri;
30import android.os.AsyncTask;
31import android.os.Bundle;
32import android.provider.CallLog;
33import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.provider.ContactsContract.CommonDataKinds.Phone;
Yorke Lee8cd94232014-07-23 14:05:31 -070035import android.provider.VoicemailContract;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import android.provider.VoicemailContract.Voicemails;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070037import android.telecomm.PhoneAccount;
38import android.telecomm.TelecommManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.telephony.TelephonyManager;
40import android.text.TextUtils;
41import android.util.Log;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.view.KeyEvent;
43import android.view.LayoutInflater;
44import android.view.Menu;
45import android.view.MenuItem;
46import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.widget.ListView;
Tyler Gunn18164c82014-06-09 10:20:57 -070048import android.widget.QuickContactBadge;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.widget.TextView;
50import android.widget.Toast;
51
Chiao Cheng35071c02012-10-15 18:36:24 -070052import com.android.contacts.common.ContactPhotoManager;
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070053import com.android.contacts.common.CallUtil;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080054import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070055import com.android.contacts.common.GeoUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import com.android.dialer.calllog.CallDetailHistoryAdapter;
Tyler Gunn8b0e8582014-07-10 12:28:43 -070057import com.android.dialer.calllog.CallLogQuery;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058import com.android.dialer.calllog.CallTypeHelper;
59import com.android.dialer.calllog.ContactInfo;
60import com.android.dialer.calllog.ContactInfoHelper;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070061import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee24ec3192013-11-19 13:52:45 -080062import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070063import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070064import com.android.dialer.util.AsyncTaskExecutor;
65import com.android.dialer.util.AsyncTaskExecutors;
Yorke Lee39213592014-04-07 15:39:48 -070066import com.android.dialer.util.DialerUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070067import com.android.dialer.voicemail.VoicemailPlaybackFragment;
68import com.android.dialer.voicemail.VoicemailStatusHelper;
69import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
70import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
71
72import java.util.List;
73
74/**
75 * Displays the details of a specific call log entry.
76 * <p>
77 * This activity can be either started with the URI of a single call log entry, or with the
78 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
79 */
80public class CallDetailActivity extends Activity implements ProximitySensorAware {
81 private static final String TAG = "CallDetail";
82
Yorke Lee03459442013-10-30 18:29:32 -070083 private static final int LOADER_ID = 0;
84 private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
85
Chiao Cheng35071c02012-10-15 18:36:24 -070086 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
87 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
88
Chiao Cheng94b10b52012-08-17 16:59:12 -070089 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
90 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
91 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
92 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
93
94 /** The enumeration of {@link AsyncTask} objects used in this class. */
95 public enum Tasks {
96 MARK_VOICEMAIL_READ,
97 DELETE_VOICEMAIL_AND_FINISH,
98 REMOVE_FROM_CALL_LOG_AND_FINISH,
99 UPDATE_PHONE_CALL_DETAILS,
100 }
101
102 /** A long array extra containing ids of call log entries to display. */
103 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
104 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
105 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
106 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
107 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
108 /** If the activity was triggered from a notification. */
109 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
110
Yorke Lee8cd94232014-07-23 14:05:31 -0700111 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
112
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800114 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700115 private QuickContactBadge mQuickContactBadge;
116 private TextView mCallerName;
117 private TextView mCallerNumber;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 private AsyncTaskExecutor mAsyncTaskExecutor;
119 private ContactInfoHelper mContactInfoHelper;
120
121 private String mNumber = null;
122 private String mDefaultCountryIso;
123
124 /* package */ LayoutInflater mInflater;
125 /* package */ Resources mResources;
126 /** Helper to load contact photos. */
127 private ContactPhotoManager mContactPhotoManager;
128 /** Helper to make async queries to content resolver. */
129 private CallDetailActivityQueryHandler mAsyncQueryHandler;
130 /** Helper to get voicemail status messages. */
131 private VoicemailStatusHelper mVoicemailStatusHelper;
132 // Views related to voicemail status message.
133 private View mStatusMessageView;
134 private TextView mStatusMessageText;
135 private TextView mStatusMessageAction;
Yorke Lee8cd94232014-07-23 14:05:31 -0700136 private TextView mVoicemailTranscription;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137
138 /** Whether we should show "edit number before call" in the options menu. */
139 private boolean mHasEditNumberBeforeCallOption;
140 /** Whether we should show "trash" in the options menu. */
141 private boolean mHasTrashOption;
142 /** Whether we should show "remove from call log" in the options menu. */
143 private boolean mHasRemoveFromCallLogOption;
144
145 private ProximitySensorManager mProximitySensorManager;
146 private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
147
Chiao Cheng94b10b52012-08-17 16:59:12 -0700148 /** Listener to changes in the proximity sensor state. */
149 private class ProximitySensorListener implements ProximitySensorManager.Listener {
150 /** Used to show a blank view and hide the action bar. */
151 private final Runnable mBlankRunnable = new Runnable() {
152 @Override
153 public void run() {
154 View blankView = findViewById(R.id.blank);
155 blankView.setVisibility(View.VISIBLE);
156 getActionBar().hide();
157 }
158 };
159 /** Used to remove the blank view and show the action bar. */
160 private final Runnable mUnblankRunnable = new Runnable() {
161 @Override
162 public void run() {
163 View blankView = findViewById(R.id.blank);
164 blankView.setVisibility(View.GONE);
165 getActionBar().show();
166 }
167 };
168
169 @Override
170 public synchronized void onNear() {
171 clearPendingRequests();
172 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
173 }
174
175 @Override
176 public synchronized void onFar() {
177 clearPendingRequests();
178 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
179 }
180
181 /** Removed any delayed requests that may be pending. */
182 public synchronized void clearPendingRequests() {
183 View blankView = findViewById(R.id.blank);
184 blankView.removeCallbacks(mBlankRunnable);
185 blankView.removeCallbacks(mUnblankRunnable);
186 }
187
188 /** Post a {@link Runnable} with a delay on the main thread. */
189 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
190 // Post these instead of executing immediately so that:
191 // - They are guaranteed to be executed on the main thread.
192 // - If the sensor values changes rapidly for some time, the UI will not be
193 // updated immediately.
194 View blankView = findViewById(R.id.blank);
195 blankView.postDelayed(runnable, delayMillis);
196 }
197 }
198
199 static final String[] CALL_LOG_PROJECTION = new String[] {
200 CallLog.Calls.DATE,
201 CallLog.Calls.DURATION,
202 CallLog.Calls.NUMBER,
203 CallLog.Calls.TYPE,
204 CallLog.Calls.COUNTRY_ISO,
205 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700206 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700207 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
208 CallLog.Calls.PHONE_ACCOUNT_ID,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700209 CallLog.Calls.FEATURES,
Yorke Lee8cd94232014-07-23 14:05:31 -0700210 CallLog.Calls.DATA_USAGE,
211 CallLog.Calls.TRANSCRIPTION
Chiao Cheng94b10b52012-08-17 16:59:12 -0700212 };
213
214 static final int DATE_COLUMN_INDEX = 0;
215 static final int DURATION_COLUMN_INDEX = 1;
216 static final int NUMBER_COLUMN_INDEX = 2;
217 static final int CALL_TYPE_COLUMN_INDEX = 3;
218 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
219 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700220 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700221 static final int ACCOUNT_COMPONENT_NAME = 7;
222 static final int ACCOUNT_ID = 8;
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700223 static final int FEATURES = 9;
224 static final int DATA_USAGE = 10;
Yorke Lee8cd94232014-07-23 14:05:31 -0700225 static final int TRANSCRIPTION_COLUMN_INDEX = 11;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700226
Chiao Cheng94b10b52012-08-17 16:59:12 -0700227 @Override
228 protected void onCreate(Bundle icicle) {
229 super.onCreate(icicle);
230
231 setContentView(R.layout.call_detail);
232
233 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
234 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
235 mResources = getResources();
236
237 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee24ec3192013-11-19 13:52:45 -0800238 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
240 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700241 mStatusMessageView = findViewById(R.id.voicemail_status);
242 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
243 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Yorke Lee8cd94232014-07-23 14:05:31 -0700244 mVoicemailTranscription = (TextView) findViewById(R.id.voicemail_transcription);
Tyler Gunn18164c82014-06-09 10:20:57 -0700245 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
246 mQuickContactBadge.setOverlay(null);
247 mCallerName = (TextView) findViewById(R.id.caller_name);
248 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Chiao Cheng35071c02012-10-15 18:36:24 -0700249 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700250 mContactPhotoManager = ContactPhotoManager.getInstance(this);
251 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700252 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700253 getActionBar().setDisplayHomeAsUpEnabled(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700254 optionallyHandleVoicemail();
255 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
256 closeSystemDialogs();
257 }
258 }
259
260 @Override
261 public void onResume() {
262 super.onResume();
263 updateData(getCallLogEntryUris());
264 }
265
266 /**
267 * Handle voicemail playback or hide voicemail ui.
268 * <p>
269 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
270 * playback. If it doesn't, then hide the voicemail ui.
271 */
272 private void optionallyHandleVoicemail() {
273 View voicemailContainer = findViewById(R.id.voicemail_container);
274 if (hasVoicemail()) {
275 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
276 // to play and optionally start the playback.
277 // Do a query to fetch the voicemail status messages.
Yorke Lee8cd94232014-07-23 14:05:31 -0700278 VoicemailPlaybackFragment playbackFragment;
279
280 playbackFragment = (VoicemailPlaybackFragment) getFragmentManager().findFragmentByTag(
281 VOICEMAIL_FRAGMENT_TAG);
282
283 if (playbackFragment == null) {
284 playbackFragment = new VoicemailPlaybackFragment();
285 Bundle fragmentArguments = new Bundle();
286 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
287 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
288 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
289 }
290 playbackFragment.setArguments(fragmentArguments);
291 getFragmentManager().beginTransaction()
292 .add(R.id.voicemail_container, playbackFragment, VOICEMAIL_FRAGMENT_TAG)
293 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700294 }
Yorke Lee8cd94232014-07-23 14:05:31 -0700295
Chiao Cheng94b10b52012-08-17 16:59:12 -0700296 voicemailContainer.setVisibility(View.VISIBLE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700297 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
298 markVoicemailAsRead(getVoicemailUri());
299 } else {
300 // No voicemail uri: hide the status view.
301 mStatusMessageView.setVisibility(View.GONE);
302 voicemailContainer.setVisibility(View.GONE);
303 }
304 }
305
306 private boolean hasVoicemail() {
307 return getVoicemailUri() != null;
308 }
309
310 private Uri getVoicemailUri() {
311 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
312 }
313
314 private void markVoicemailAsRead(final Uri voicemailUri) {
315 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
316 @Override
317 public Void doInBackground(Void... params) {
318 ContentValues values = new ContentValues();
319 values.put(Voicemails.IS_READ, true);
320 getContentResolver().update(voicemailUri, values,
321 Voicemails.IS_READ + " = 0", null);
322 return null;
323 }
324 });
325 }
326
327 /**
328 * Returns the list of URIs to show.
329 * <p>
330 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
331 * a list of ids in the call log added as an extra on the URI.
332 * <p>
333 * If both are available, the data on the intent takes precedence.
334 */
335 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700336 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700337 if (uri != null) {
338 // If there is a data on the intent, it takes precedence over the extra.
339 return new Uri[]{ uri };
340 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700341 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
342 final int numIds = ids == null ? 0 : ids.length;
343 final Uri[] uris = new Uri[numIds];
344 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700345 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
346 }
347 return uris;
348 }
349
350 @Override
351 public boolean onKeyDown(int keyCode, KeyEvent event) {
352 switch (keyCode) {
353 case KeyEvent.KEYCODE_CALL: {
354 // Make sure phone isn't already busy before starting direct call
355 TelephonyManager tm = (TelephonyManager)
356 getSystemService(Context.TELEPHONY_SERVICE);
357 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700358 DialerUtils.startActivityWithErrorToast(this,
359 CallUtil.getCallIntent(Uri.fromParts(CallUtil.SCHEME_TEL, mNumber,
Nancy Chen87ba4892014-06-11 17:56:07 -0700360 null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700361 return true;
362 }
363 }
364 }
365
366 return super.onKeyDown(keyCode, event);
367 }
368
369 /**
370 * Update user interface with details of given call.
371 *
372 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
373 */
374 private void updateData(final Uri... callUris) {
375 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
376 @Override
377 public PhoneCallDetails[] doInBackground(Void... params) {
378 // TODO: All phone calls correspond to the same person, so we can make a single
379 // lookup.
380 final int numCalls = callUris.length;
381 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
382 try {
383 for (int index = 0; index < numCalls; ++index) {
384 details[index] = getPhoneCallDetailsForUri(callUris[index]);
385 }
386 return details;
387 } catch (IllegalArgumentException e) {
388 // Something went wrong reading in our primary data.
389 Log.w(TAG, "invalid URI starting call details", e);
390 return null;
391 }
392 }
393
394 @Override
395 public void onPostExecute(PhoneCallDetails[] details) {
396 if (details == null) {
397 // Somewhere went wrong: we're going to bail out and show error to users.
398 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
399 Toast.LENGTH_SHORT).show();
400 finish();
401 return;
402 }
403
404 // We know that all calls are from the same number and the same contact, so pick the
405 // first.
406 PhoneCallDetails firstDetails = details[0];
407 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700408 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700409 final Uri contactUri = firstDetails.contactUri;
410 final Uri photoUri = firstDetails.photoUri;
411
Chiao Cheng94b10b52012-08-17 16:59:12 -0700412 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700413 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700414 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
415 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
416 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
417 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700418
Tyler Gunn18164c82014-06-09 10:20:57 -0700419 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700420
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800421 final CharSequence displayNumber =
422 mPhoneNumberHelper.getDisplayNumber(
423 firstDetails.number,
424 firstDetails.numberPresentation,
425 firstDetails.formattedNumber);
426
Tyler Gunn18164c82014-06-09 10:20:57 -0700427 if (!TextUtils.isEmpty(firstDetails.name)) {
428 mCallerName.setText(firstDetails.name);
429 mCallerNumber.setText(callLocationOrType + " " + displayNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700430 } else {
Tyler Gunn18164c82014-06-09 10:20:57 -0700431 mCallerName.setText(displayNumber);
432 if (!TextUtils.isEmpty(callLocationOrType)) {
433 mCallerNumber.setText(callLocationOrType);
434 mCallerNumber.setVisibility(View.VISIBLE);
435 } else {
436 mCallerNumber.setVisibility(View.GONE);
437 }
438
Chiao Cheng94b10b52012-08-17 16:59:12 -0700439 }
440
441 mHasEditNumberBeforeCallOption =
442 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
443 mHasTrashOption = hasVoicemail();
444 mHasRemoveFromCallLogOption = !hasVoicemail();
445 invalidateOptionsMenu();
446
447 ListView historyList = (ListView) findViewById(R.id.history);
448 historyList.setAdapter(
449 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
Tyler Gunn18164c82014-06-09 10:20:57 -0700450 mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700451
Tyler Gunn18164c82014-06-09 10:20:57 -0700452 String lookupKey = contactUri == null ? null
453 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800454
455 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
456
457 final int contactType =
458 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
459 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
460 ContactPhotoManager.TYPE_DEFAULT;
461
Tyler Gunn18164c82014-06-09 10:20:57 -0700462 String nameForDefaultImage;
463 if (TextUtils.isEmpty(firstDetails.name)) {
464 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(firstDetails.number,
465 firstDetails.numberPresentation,
466 firstDetails.formattedNumber).toString();
467 } else {
468 nameForDefaultImage = firstDetails.name.toString();
469 }
470
Yorke Lee8cd94232014-07-23 14:05:31 -0700471 if (hasVoicemail() && !TextUtils.isEmpty(firstDetails.transcription)) {
472 mVoicemailTranscription.setText(firstDetails.transcription);
473 mVoicemailTranscription.setVisibility(View.VISIBLE);
474 } else {
475 mVoicemailTranscription.setText(null);
476 mVoicemailTranscription.setVisibility(View.GONE);
477 }
478
Tyler Gunn18164c82014-06-09 10:20:57 -0700479 loadContactPhotos(
480 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700481 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
482 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700483
484 /**
485 * Determines the location geocode text for a call, or the phone number type
486 * (if available).
487 *
488 * @param details The call details.
489 * @return The phone number type or location.
490 */
491 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
492 if (!TextUtils.isEmpty(details.name)) {
493 return Phone.getTypeLabel(mResources, details.numberType,
494 details.numberLabel);
495 } else {
496 return details.geocode;
497 }
498 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700499 }
500 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
501 }
502
503 /** Return the phone call details for a given call log URI. */
504 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
505 ContentResolver resolver = getContentResolver();
506 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
507 try {
508 if (callCursor == null || !callCursor.moveToFirst()) {
509 throw new IllegalArgumentException("Cannot find content: " + callUri);
510 }
511
512 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700513 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
514 final int numberPresentation = callCursor.getInt(
515 NUMBER_PRESENTATION_COLUMN_INDEX);
516 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
517 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
518 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700519 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
520 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Yorke Lee8cd94232014-07-23 14:05:31 -0700521 final String transcription = callCursor.getString(TRANSCRIPTION_COLUMN_INDEX);
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700522
523 final Drawable accountIcon = PhoneAccountUtils.getAccountIcon(this,
524 PhoneAccountUtils.getAccount(
525 callCursor.getString(ACCOUNT_COMPONENT_NAME),
526 callCursor.getString(ACCOUNT_ID)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700527
528 if (TextUtils.isEmpty(countryIso)) {
529 countryIso = mDefaultCountryIso;
530 }
531
532 // Formatted phone number.
533 final CharSequence formattedNumber;
534 // Read contact specifics.
535 final CharSequence nameText;
536 final int numberType;
537 final CharSequence numberLabel;
538 final Uri photoUri;
539 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800540 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700541 // If this is not a regular number, there is no point in looking it up in the contacts.
542 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700543 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700544 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700545 ? mContactInfoHelper.lookupNumber(number, countryIso)
546 : null;
547 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700548 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
549 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700550 nameText = "";
551 numberType = 0;
552 numberLabel = "";
553 photoUri = null;
554 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800555 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700556 } else {
557 formattedNumber = info.formattedNumber;
558 nameText = info.name;
559 numberType = info.type;
560 numberLabel = info.label;
561 photoUri = info.photoUri;
562 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800563 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700564 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700565 final int features = callCursor.getInt(FEATURES);
566 Long dataUsage = null;
567 if (!callCursor.isNull(DATA_USAGE)) {
568 dataUsage = callCursor.getLong(DATA_USAGE);
569 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700570 return new PhoneCallDetails(number, numberPresentation,
571 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700572 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700573 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Yorke Lee8cd94232014-07-23 14:05:31 -0700574 accountIcon, features, dataUsage, transcription);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700575 } finally {
576 if (callCursor != null) {
577 callCursor.close();
578 }
579 }
580 }
581
582 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700583 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
584 String lookupKey, int contactType) {
585
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800586 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700587 contactType, true /* isCircular */);
588
589 mQuickContactBadge.assignContactUri(contactUri);
590 mQuickContactBadge.setContentDescription(
591 mResources.getString(R.string.description_contact_details, displayName));
592
593 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
594 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700595 }
596
597 static final class ViewEntry {
598 public final String text;
599 public final Intent primaryIntent;
600 /** The description for accessibility of the primary action. */
601 public final String primaryDescription;
602
603 public CharSequence label = null;
604 /** Icon for the secondary action. */
605 public int secondaryIcon = 0;
606 /** Intent for the secondary action. If not null, an icon must be defined. */
607 public Intent secondaryIntent = null;
608 /** The description for accessibility of the secondary action. */
609 public String secondaryDescription = null;
610
611 public ViewEntry(String text, Intent intent, String description) {
612 this.text = text;
613 primaryIntent = intent;
614 primaryDescription = description;
615 }
616
617 public void setSecondaryAction(int icon, Intent intent, String description) {
618 secondaryIcon = icon;
619 secondaryIntent = intent;
620 secondaryDescription = description;
621 }
622 }
623
Chiao Cheng94b10b52012-08-17 16:59:12 -0700624 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
625 if (statusCursor == null) {
626 mStatusMessageView.setVisibility(View.GONE);
627 return;
628 }
629 final StatusMessage message = getStatusMessage(statusCursor);
630 if (message == null || !message.showInCallDetails()) {
631 mStatusMessageView.setVisibility(View.GONE);
632 return;
633 }
634
635 mStatusMessageView.setVisibility(View.VISIBLE);
636 mStatusMessageText.setText(message.callDetailsMessageId);
637 if (message.actionMessageId != -1) {
638 mStatusMessageAction.setText(message.actionMessageId);
639 }
640 if (message.actionUri != null) {
641 mStatusMessageAction.setClickable(true);
642 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
643 @Override
644 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700645 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
646 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700647 }
648 });
649 } else {
650 mStatusMessageAction.setClickable(false);
651 }
652 }
653
654 private StatusMessage getStatusMessage(Cursor statusCursor) {
655 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
656 if (messages.size() == 0) {
657 return null;
658 }
659 // There can only be a single status message per source package, so num of messages can
660 // at most be 1.
661 if (messages.size() > 1) {
662 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
663 " Will use the first one.", messages.size()));
664 }
665 return messages.get(0);
666 }
667
668 @Override
669 public boolean onCreateOptionsMenu(Menu menu) {
670 getMenuInflater().inflate(R.menu.call_details_options, menu);
671 return super.onCreateOptionsMenu(menu);
672 }
673
674 @Override
675 public boolean onPrepareOptionsMenu(Menu menu) {
676 // This action deletes all elements in the group from the call log.
677 // We don't have this action for voicemails, because you can just use the trash button.
678 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
679 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
680 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
681 return super.onPrepareOptionsMenu(menu);
682 }
683
Chiao Cheng94b10b52012-08-17 16:59:12 -0700684 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
685 final StringBuilder callIds = new StringBuilder();
686 for (Uri callUri : getCallLogEntryUris()) {
687 if (callIds.length() != 0) {
688 callIds.append(",");
689 }
690 callIds.append(ContentUris.parseId(callUri));
691 }
692 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
693 new AsyncTask<Void, Void, Void>() {
694 @Override
695 public Void doInBackground(Void... params) {
696 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
697 Calls._ID + " IN (" + callIds + ")", null);
698 return null;
699 }
700
701 @Override
702 public void onPostExecute(Void result) {
703 finish();
704 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700705 }
706 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707 }
708
709 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700710 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711 }
712
713 public void onMenuTrashVoicemail(MenuItem menuItem) {
714 final Uri voicemailUri = getVoicemailUri();
715 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
716 new AsyncTask<Void, Void, Void>() {
717 @Override
718 public Void doInBackground(Void... params) {
719 getContentResolver().delete(voicemailUri, null, null);
720 return null;
721 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700722
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 @Override
724 public void onPostExecute(Void result) {
725 finish();
726 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700727 }
728 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 }
730
731 @Override
732 protected void onPause() {
733 // Immediately stop the proximity sensor.
734 disableProximitySensor(false);
735 mProximitySensorListener.clearPendingRequests();
736 super.onPause();
737 }
738
739 @Override
740 public void enableProximitySensor() {
741 mProximitySensorManager.enable();
742 }
743
744 @Override
745 public void disableProximitySensor(boolean waitForFarState) {
746 mProximitySensorManager.disable(waitForFarState);
747 }
748
Chiao Cheng94b10b52012-08-17 16:59:12 -0700749 private void closeSystemDialogs() {
750 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
751 }
752
Chiao Cheng35071c02012-10-15 18:36:24 -0700753 /** Returns the given text, forced to be left-to-right. */
754 private static CharSequence forceLeftToRight(CharSequence text) {
755 StringBuilder sb = new StringBuilder();
756 sb.append(LEFT_TO_RIGHT_EMBEDDING);
757 sb.append(text);
758 sb.append(POP_DIRECTIONAL_FORMATTING);
759 return sb.toString();
760 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700761}