blob: df8197a1055626e9367783f37b75d561f9ccbd21 [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
Brian Attwellbeab3bb2014-10-27 12:24:49 -070019import android.app.Activity;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import 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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.net.Uri;
28import android.os.AsyncTask;
29import android.os.Bundle;
30import android.provider.CallLog;
31import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.provider.VoicemailContract.Voicemails;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070034import android.telecom.PhoneAccount;
Nancy Chene80d6222014-10-08 20:17:55 -070035import android.telecom.PhoneAccountHandle;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import android.telephony.TelephonyManager;
Andrew Lee99e8adb2014-10-02 12:27:37 -070037import android.text.BidiFormatter;
38import android.text.TextDirectionHeuristics;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.text.TextUtils;
40import android.util.Log;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.view.KeyEvent;
42import android.view.LayoutInflater;
43import android.view.Menu;
44import android.view.MenuItem;
45import android.view.View;
Yorke Lee257b6f22014-07-31 14:16:44 -070046import android.widget.LinearLayout;
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;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080053import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070054import com.android.contacts.common.GeoUtil;
Brian Attwell82043242015-02-03 14:13:58 -080055import com.android.contacts.common.CallUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import com.android.dialer.calllog.CallDetailHistoryAdapter;
57import com.android.dialer.calllog.CallTypeHelper;
58import com.android.dialer.calllog.ContactInfo;
59import com.android.dialer.calllog.ContactInfoHelper;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070060import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee24ec3192013-11-19 13:52:45 -080061import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070062import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070063import com.android.dialer.util.AsyncTaskExecutor;
64import com.android.dialer.util.AsyncTaskExecutors;
Brian Attwell82043242015-02-03 14:13:58 -080065import com.android.dialer.util.PrivilegedCallUtil;
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 */
Brian Attwellbeab3bb2014-10-27 12:24:49 -070080public class CallDetailActivity extends Activity implements ProximitySensorAware {
Chiao Cheng94b10b52012-08-17 16:59:12 -070081 private static final String TAG = "CallDetail";
82
Chiao Cheng35071c02012-10-15 18:36:24 -070083 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
84 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
85
Chiao Cheng94b10b52012-08-17 16:59:12 -070086 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
87 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
88 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
89 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
90
91 /** The enumeration of {@link AsyncTask} objects used in this class. */
92 public enum Tasks {
93 MARK_VOICEMAIL_READ,
94 DELETE_VOICEMAIL_AND_FINISH,
95 REMOVE_FROM_CALL_LOG_AND_FINISH,
96 UPDATE_PHONE_CALL_DETAILS,
97 }
98
99 /** A long array extra containing ids of call log entries to display. */
100 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
101 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
102 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
103 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
104 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
105 /** If the activity was triggered from a notification. */
106 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
107
Yorke Lee8cd94232014-07-23 14:05:31 -0700108 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
109
Chiao Cheng94b10b52012-08-17 16:59:12 -0700110 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800111 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700112 private QuickContactBadge mQuickContactBadge;
113 private TextView mCallerName;
114 private TextView mCallerNumber;
Nancy Chen19702632014-07-25 13:23:16 -0700115 private TextView mAccountLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116 private AsyncTaskExecutor mAsyncTaskExecutor;
117 private ContactInfoHelper mContactInfoHelper;
118
119 private String mNumber = null;
120 private String mDefaultCountryIso;
121
122 /* package */ LayoutInflater mInflater;
123 /* package */ Resources mResources;
124 /** Helper to load contact photos. */
125 private ContactPhotoManager mContactPhotoManager;
126 /** Helper to make async queries to content resolver. */
127 private CallDetailActivityQueryHandler mAsyncQueryHandler;
128 /** Helper to get voicemail status messages. */
129 private VoicemailStatusHelper mVoicemailStatusHelper;
130 // Views related to voicemail status message.
131 private View mStatusMessageView;
132 private TextView mStatusMessageText;
133 private TextView mStatusMessageAction;
Yorke Lee8cd94232014-07-23 14:05:31 -0700134 private TextView mVoicemailTranscription;
Yorke Lee257b6f22014-07-31 14:16:44 -0700135 private LinearLayout mVoicemailHeader;
136
137 private Uri mVoicemailUri;
Andrew Lee99e8adb2014-10-02 12:27:37 -0700138 private BidiFormatter mBidiFormatter = BidiFormatter.getInstance();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700139
140 /** Whether we should show "edit number before call" in the options menu. */
141 private boolean mHasEditNumberBeforeCallOption;
142 /** Whether we should show "trash" in the options menu. */
143 private boolean mHasTrashOption;
144 /** Whether we should show "remove from call log" in the options menu. */
145 private boolean mHasRemoveFromCallLogOption;
146
147 private ProximitySensorManager mProximitySensorManager;
148 private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
149
Chiao Cheng94b10b52012-08-17 16:59:12 -0700150 /** Listener to changes in the proximity sensor state. */
151 private class ProximitySensorListener implements ProximitySensorManager.Listener {
152 /** Used to show a blank view and hide the action bar. */
153 private final Runnable mBlankRunnable = new Runnable() {
154 @Override
155 public void run() {
156 View blankView = findViewById(R.id.blank);
157 blankView.setVisibility(View.VISIBLE);
158 getActionBar().hide();
159 }
160 };
161 /** Used to remove the blank view and show the action bar. */
162 private final Runnable mUnblankRunnable = new Runnable() {
163 @Override
164 public void run() {
165 View blankView = findViewById(R.id.blank);
166 blankView.setVisibility(View.GONE);
167 getActionBar().show();
168 }
169 };
170
171 @Override
172 public synchronized void onNear() {
173 clearPendingRequests();
174 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
175 }
176
177 @Override
178 public synchronized void onFar() {
179 clearPendingRequests();
180 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
181 }
182
183 /** Removed any delayed requests that may be pending. */
184 public synchronized void clearPendingRequests() {
185 View blankView = findViewById(R.id.blank);
186 blankView.removeCallbacks(mBlankRunnable);
187 blankView.removeCallbacks(mUnblankRunnable);
188 }
189
190 /** Post a {@link Runnable} with a delay on the main thread. */
191 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
192 // Post these instead of executing immediately so that:
193 // - They are guaranteed to be executed on the main thread.
194 // - If the sensor values changes rapidly for some time, the UI will not be
195 // updated immediately.
196 View blankView = findViewById(R.id.blank);
197 blankView.postDelayed(runnable, delayMillis);
198 }
199 }
200
201 static final String[] CALL_LOG_PROJECTION = new String[] {
202 CallLog.Calls.DATE,
203 CallLog.Calls.DURATION,
204 CallLog.Calls.NUMBER,
205 CallLog.Calls.TYPE,
206 CallLog.Calls.COUNTRY_ISO,
207 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700208 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700209 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
210 CallLog.Calls.PHONE_ACCOUNT_ID,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700211 CallLog.Calls.FEATURES,
Yorke Lee8cd94232014-07-23 14:05:31 -0700212 CallLog.Calls.DATA_USAGE,
213 CallLog.Calls.TRANSCRIPTION
Chiao Cheng94b10b52012-08-17 16:59:12 -0700214 };
215
216 static final int DATE_COLUMN_INDEX = 0;
217 static final int DURATION_COLUMN_INDEX = 1;
218 static final int NUMBER_COLUMN_INDEX = 2;
219 static final int CALL_TYPE_COLUMN_INDEX = 3;
220 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
221 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700222 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700223 static final int ACCOUNT_COMPONENT_NAME = 7;
224 static final int ACCOUNT_ID = 8;
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700225 static final int FEATURES = 9;
226 static final int DATA_USAGE = 10;
Yorke Lee8cd94232014-07-23 14:05:31 -0700227 static final int TRANSCRIPTION_COLUMN_INDEX = 11;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700228
Chiao Cheng94b10b52012-08-17 16:59:12 -0700229 @Override
230 protected void onCreate(Bundle icicle) {
231 super.onCreate(icicle);
232
233 setContentView(R.layout.call_detail);
234
235 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
236 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
237 mResources = getResources();
238
239 mCallTypeHelper = new CallTypeHelper(getResources());
Nancy Chene80d6222014-10-08 20:17:55 -0700240 mPhoneNumberHelper = new PhoneNumberDisplayHelper(this, mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700241 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
242 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Yorke Lee257b6f22014-07-31 14:16:44 -0700243
244 mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
245
Tyler Gunn18164c82014-06-09 10:20:57 -0700246 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
247 mQuickContactBadge.setOverlay(null);
Brian Attwell74eb4ec2015-02-25 22:31:22 -0800248 mQuickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE);
Tyler Gunn18164c82014-06-09 10:20:57 -0700249 mCallerName = (TextView) findViewById(R.id.caller_name);
250 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700251 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700252 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700253 mContactPhotoManager = ContactPhotoManager.getInstance(this);
254 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700255 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700256 getActionBar().setDisplayHomeAsUpEnabled(true);
Yorke Lee257b6f22014-07-31 14:16:44 -0700257
Chiao Cheng94b10b52012-08-17 16:59:12 -0700258 optionallyHandleVoicemail();
259 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
260 closeSystemDialogs();
261 }
262 }
263
264 @Override
265 public void onResume() {
266 super.onResume();
267 updateData(getCallLogEntryUris());
268 }
269
270 /**
271 * Handle voicemail playback or hide voicemail ui.
272 * <p>
273 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
Yorke Lee257b6f22014-07-31 14:16:44 -0700274 * playback. If it doesn't, then don't inflate the voicemail ui.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275 */
276 private void optionallyHandleVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700277
Chiao Cheng94b10b52012-08-17 16:59:12 -0700278 if (hasVoicemail()) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700279 LayoutInflater inflater =
280 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
281 mVoicemailHeader =
282 (LinearLayout) inflater.inflate(R.layout.call_details_voicemail_header, null);
283 View voicemailContainer = mVoicemailHeader.findViewById(R.id.voicemail_container);
284 mStatusMessageView = mVoicemailHeader.findViewById(R.id.voicemail_status);
285 mStatusMessageText =
286 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_message);
287 mStatusMessageAction =
288 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_action);
289 mVoicemailTranscription = (
290 TextView) mVoicemailHeader.findViewById(R.id.voicemail_transcription);
291 ListView historyList = (ListView) findViewById(R.id.history);
292 historyList.addHeaderView(mVoicemailHeader);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
294 // to play and optionally start the playback.
295 // Do a query to fetch the voicemail status messages.
Yorke Lee8cd94232014-07-23 14:05:31 -0700296 VoicemailPlaybackFragment playbackFragment;
297
298 playbackFragment = (VoicemailPlaybackFragment) getFragmentManager().findFragmentByTag(
299 VOICEMAIL_FRAGMENT_TAG);
300
301 if (playbackFragment == null) {
302 playbackFragment = new VoicemailPlaybackFragment();
303 Bundle fragmentArguments = new Bundle();
Yorke Lee257b6f22014-07-31 14:16:44 -0700304 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, mVoicemailUri);
Yorke Lee8cd94232014-07-23 14:05:31 -0700305 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
306 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
307 }
308 playbackFragment.setArguments(fragmentArguments);
309 getFragmentManager().beginTransaction()
310 .add(R.id.voicemail_container, playbackFragment, VOICEMAIL_FRAGMENT_TAG)
311 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700312 }
Yorke Lee8cd94232014-07-23 14:05:31 -0700313
Chiao Cheng94b10b52012-08-17 16:59:12 -0700314 voicemailContainer.setVisibility(View.VISIBLE);
Yorke Lee257b6f22014-07-31 14:16:44 -0700315 mAsyncQueryHandler.startVoicemailStatusQuery(mVoicemailUri);
316 markVoicemailAsRead(mVoicemailUri);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700317 }
318 }
319
320 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700321 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700322 }
323
324 private void markVoicemailAsRead(final Uri voicemailUri) {
325 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
326 @Override
327 public Void doInBackground(Void... params) {
328 ContentValues values = new ContentValues();
329 values.put(Voicemails.IS_READ, true);
330 getContentResolver().update(voicemailUri, values,
331 Voicemails.IS_READ + " = 0", null);
332 return null;
333 }
334 });
335 }
336
337 /**
338 * Returns the list of URIs to show.
339 * <p>
340 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
341 * a list of ids in the call log added as an extra on the URI.
342 * <p>
343 * If both are available, the data on the intent takes precedence.
344 */
345 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700346 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700347 if (uri != null) {
348 // If there is a data on the intent, it takes precedence over the extra.
349 return new Uri[]{ uri };
350 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700351 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
352 final int numIds = ids == null ? 0 : ids.length;
353 final Uri[] uris = new Uri[numIds];
354 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700355 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
356 }
357 return uris;
358 }
359
360 @Override
361 public boolean onKeyDown(int keyCode, KeyEvent event) {
362 switch (keyCode) {
363 case KeyEvent.KEYCODE_CALL: {
364 // Make sure phone isn't already busy before starting direct call
365 TelephonyManager tm = (TelephonyManager)
366 getSystemService(Context.TELEPHONY_SERVICE);
367 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700368 DialerUtils.startActivityWithErrorToast(this,
Brian Attwell82043242015-02-03 14:13:58 -0800369 PrivilegedCallUtil.getCallIntent(Uri.fromParts(PhoneAccount.SCHEME_TEL,
370 mNumber, null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700371 return true;
372 }
373 }
374 }
375
376 return super.onKeyDown(keyCode, event);
377 }
378
379 /**
380 * Update user interface with details of given call.
381 *
Nancy Chene80d6222014-10-08 20:17:55 -0700382 * @param callUris URIs into {@link android.provider.CallLog.Calls} of the calls to be displayed
Chiao Cheng94b10b52012-08-17 16:59:12 -0700383 */
384 private void updateData(final Uri... callUris) {
385 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
386 @Override
387 public PhoneCallDetails[] doInBackground(Void... params) {
388 // TODO: All phone calls correspond to the same person, so we can make a single
389 // lookup.
390 final int numCalls = callUris.length;
391 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
392 try {
393 for (int index = 0; index < numCalls; ++index) {
394 details[index] = getPhoneCallDetailsForUri(callUris[index]);
395 }
396 return details;
397 } catch (IllegalArgumentException e) {
398 // Something went wrong reading in our primary data.
399 Log.w(TAG, "invalid URI starting call details", e);
400 return null;
401 }
402 }
403
404 @Override
405 public void onPostExecute(PhoneCallDetails[] details) {
Nancy Chene80d6222014-10-08 20:17:55 -0700406 Context context = CallDetailActivity.this;
407
Chiao Cheng94b10b52012-08-17 16:59:12 -0700408 if (details == null) {
409 // Somewhere went wrong: we're going to bail out and show error to users.
Nancy Chene80d6222014-10-08 20:17:55 -0700410 Toast.makeText(context, R.string.toast_call_detail_error,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700411 Toast.LENGTH_SHORT).show();
412 finish();
413 return;
414 }
415
416 // We know that all calls are from the same number and the same contact, so pick the
417 // first.
418 PhoneCallDetails firstDetails = details[0];
419 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700420 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700421 final Uri contactUri = firstDetails.contactUri;
422 final Uri photoUri = firstDetails.photoUri;
Nancy Chene80d6222014-10-08 20:17:55 -0700423 final PhoneAccountHandle accountHandle = firstDetails.accountHandle;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424
Chiao Cheng94b10b52012-08-17 16:59:12 -0700425 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700426 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700427 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
Nancy Chene80d6222014-10-08 20:17:55 -0700428 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper(context);
429 final boolean isVoicemailNumber =
430 phoneUtils.isVoicemailNumber(accountHandle, mNumber);
431 final boolean isSipNumber = PhoneNumberUtilsWrapper.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700432
Tyler Gunn18164c82014-06-09 10:20:57 -0700433 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700434
Nancy Chene80d6222014-10-08 20:17:55 -0700435 final CharSequence displayNumber =
436 mPhoneNumberHelper.getDisplayNumber(
437 firstDetails.accountHandle,
438 firstDetails.number,
439 firstDetails.numberPresentation,
440 firstDetails.formattedNumber);
Andrew Lee99e8adb2014-10-02 12:27:37 -0700441 final String displayNumberStr = mBidiFormatter.unicodeWrap(
442 displayNumber.toString(), TextDirectionHeuristics.LTR);
443
Tyler Gunn18164c82014-06-09 10:20:57 -0700444 if (!TextUtils.isEmpty(firstDetails.name)) {
445 mCallerName.setText(firstDetails.name);
Andrew Lee99e8adb2014-10-02 12:27:37 -0700446 mCallerNumber.setText(callLocationOrType + " " + displayNumberStr);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700447 } else {
Andrew Lee99e8adb2014-10-02 12:27:37 -0700448 mCallerName.setText(displayNumberStr);
Tyler Gunn18164c82014-06-09 10:20:57 -0700449 if (!TextUtils.isEmpty(callLocationOrType)) {
450 mCallerNumber.setText(callLocationOrType);
451 mCallerNumber.setVisibility(View.VISIBLE);
452 } else {
453 mCallerNumber.setVisibility(View.GONE);
454 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700455 }
456
Nancy Chene80d6222014-10-08 20:17:55 -0700457 String accountLabel = PhoneAccountUtils.getAccountLabel(context, accountHandle);
458 if (!TextUtils.isEmpty(accountLabel)) {
459 mAccountLabel.setText(accountLabel);
Nancy Chen19702632014-07-25 13:23:16 -0700460 mAccountLabel.setVisibility(View.VISIBLE);
461 } else {
462 mAccountLabel.setVisibility(View.GONE);
463 }
464
Chiao Cheng94b10b52012-08-17 16:59:12 -0700465 mHasEditNumberBeforeCallOption =
466 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
467 mHasTrashOption = hasVoicemail();
468 mHasRemoveFromCallLogOption = !hasVoicemail();
469 invalidateOptionsMenu();
470
471 ListView historyList = (ListView) findViewById(R.id.history);
472 historyList.setAdapter(
Nancy Chene80d6222014-10-08 20:17:55 -0700473 new CallDetailHistoryAdapter(context, mInflater, mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700474
Tyler Gunn18164c82014-06-09 10:20:57 -0700475 String lookupKey = contactUri == null ? null
476 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800477
478 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
479
480 final int contactType =
481 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
482 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
483 ContactPhotoManager.TYPE_DEFAULT;
484
Tyler Gunn18164c82014-06-09 10:20:57 -0700485 String nameForDefaultImage;
486 if (TextUtils.isEmpty(firstDetails.name)) {
Nancy Chene80d6222014-10-08 20:17:55 -0700487 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(
488 firstDetails.accountHandle,
489 firstDetails.number,
Tyler Gunn18164c82014-06-09 10:20:57 -0700490 firstDetails.numberPresentation,
491 firstDetails.formattedNumber).toString();
492 } else {
493 nameForDefaultImage = firstDetails.name.toString();
494 }
495
Yorke Lee8cd94232014-07-23 14:05:31 -0700496 if (hasVoicemail() && !TextUtils.isEmpty(firstDetails.transcription)) {
497 mVoicemailTranscription.setText(firstDetails.transcription);
498 mVoicemailTranscription.setVisibility(View.VISIBLE);
Yorke Lee8cd94232014-07-23 14:05:31 -0700499 }
500
Tyler Gunn18164c82014-06-09 10:20:57 -0700501 loadContactPhotos(
502 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700503 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
504 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700505
506 /**
507 * Determines the location geocode text for a call, or the phone number type
508 * (if available).
509 *
510 * @param details The call details.
511 * @return The phone number type or location.
512 */
513 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
514 if (!TextUtils.isEmpty(details.name)) {
515 return Phone.getTypeLabel(mResources, details.numberType,
516 details.numberLabel);
517 } else {
518 return details.geocode;
519 }
520 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700521 }
522 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
523 }
524
525 /** Return the phone call details for a given call log URI. */
526 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
527 ContentResolver resolver = getContentResolver();
528 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
529 try {
530 if (callCursor == null || !callCursor.moveToFirst()) {
531 throw new IllegalArgumentException("Cannot find content: " + callUri);
532 }
533
534 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700535 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
536 final int numberPresentation = callCursor.getInt(
537 NUMBER_PRESENTATION_COLUMN_INDEX);
538 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
539 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
540 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700541 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
542 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Yorke Lee8cd94232014-07-23 14:05:31 -0700543 final String transcription = callCursor.getString(TRANSCRIPTION_COLUMN_INDEX);
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700544
Nancy Chene80d6222014-10-08 20:17:55 -0700545 final PhoneAccountHandle accountHandle = PhoneAccountUtils.getAccount(
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700546 callCursor.getString(ACCOUNT_COMPONENT_NAME),
Nancy Chene80d6222014-10-08 20:17:55 -0700547 callCursor.getString(ACCOUNT_ID));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700548
549 if (TextUtils.isEmpty(countryIso)) {
550 countryIso = mDefaultCountryIso;
551 }
552
553 // Formatted phone number.
554 final CharSequence formattedNumber;
555 // Read contact specifics.
556 final CharSequence nameText;
557 final int numberType;
558 final CharSequence numberLabel;
559 final Uri photoUri;
560 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800561 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700562 // If this is not a regular number, there is no point in looking it up in the contacts.
563 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700564 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Nancy Chene80d6222014-10-08 20:17:55 -0700565 && !new PhoneNumberUtilsWrapper(this).isVoicemailNumber(accountHandle, number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700566 ? mContactInfoHelper.lookupNumber(number, countryIso)
567 : null;
568 if (info == null) {
Nancy Chene80d6222014-10-08 20:17:55 -0700569 formattedNumber = mPhoneNumberHelper.getDisplayNumber(accountHandle, number,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700570 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700571 nameText = "";
572 numberType = 0;
573 numberLabel = "";
574 photoUri = null;
575 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800576 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700577 } else {
578 formattedNumber = info.formattedNumber;
579 nameText = info.name;
580 numberType = info.type;
581 numberLabel = info.label;
582 photoUri = info.photoUri;
583 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800584 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700585 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700586 final int features = callCursor.getInt(FEATURES);
587 Long dataUsage = null;
588 if (!callCursor.isNull(DATA_USAGE)) {
589 dataUsage = callCursor.getLong(DATA_USAGE);
590 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700591 return new PhoneCallDetails(number, numberPresentation,
592 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700593 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700594 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Nancy Chene80d6222014-10-08 20:17:55 -0700595 accountHandle, features, dataUsage, transcription);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700596 } finally {
597 if (callCursor != null) {
598 callCursor.close();
599 }
600 }
601 }
602
603 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700604 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
605 String lookupKey, int contactType) {
606
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800607 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700608 contactType, true /* isCircular */);
609
610 mQuickContactBadge.assignContactUri(contactUri);
611 mQuickContactBadge.setContentDescription(
612 mResources.getString(R.string.description_contact_details, displayName));
613
614 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
615 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700616 }
617
618 static final class ViewEntry {
619 public final String text;
620 public final Intent primaryIntent;
621 /** The description for accessibility of the primary action. */
622 public final String primaryDescription;
623
624 public CharSequence label = null;
625 /** Icon for the secondary action. */
626 public int secondaryIcon = 0;
627 /** Intent for the secondary action. If not null, an icon must be defined. */
628 public Intent secondaryIntent = null;
629 /** The description for accessibility of the secondary action. */
630 public String secondaryDescription = null;
631
632 public ViewEntry(String text, Intent intent, String description) {
633 this.text = text;
634 primaryIntent = intent;
635 primaryDescription = description;
636 }
637
638 public void setSecondaryAction(int icon, Intent intent, String description) {
639 secondaryIcon = icon;
640 secondaryIntent = intent;
641 secondaryDescription = description;
642 }
643 }
644
Chiao Cheng94b10b52012-08-17 16:59:12 -0700645 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
646 if (statusCursor == null) {
647 mStatusMessageView.setVisibility(View.GONE);
648 return;
649 }
650 final StatusMessage message = getStatusMessage(statusCursor);
651 if (message == null || !message.showInCallDetails()) {
652 mStatusMessageView.setVisibility(View.GONE);
653 return;
654 }
655
656 mStatusMessageView.setVisibility(View.VISIBLE);
657 mStatusMessageText.setText(message.callDetailsMessageId);
658 if (message.actionMessageId != -1) {
659 mStatusMessageAction.setText(message.actionMessageId);
660 }
661 if (message.actionUri != null) {
662 mStatusMessageAction.setClickable(true);
663 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
664 @Override
665 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700666 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
667 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700668 }
669 });
670 } else {
671 mStatusMessageAction.setClickable(false);
672 }
673 }
674
675 private StatusMessage getStatusMessage(Cursor statusCursor) {
676 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
677 if (messages.size() == 0) {
678 return null;
679 }
680 // There can only be a single status message per source package, so num of messages can
681 // at most be 1.
682 if (messages.size() > 1) {
683 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
684 " Will use the first one.", messages.size()));
685 }
686 return messages.get(0);
687 }
688
689 @Override
690 public boolean onCreateOptionsMenu(Menu menu) {
691 getMenuInflater().inflate(R.menu.call_details_options, menu);
692 return super.onCreateOptionsMenu(menu);
693 }
694
695 @Override
696 public boolean onPrepareOptionsMenu(Menu menu) {
697 // This action deletes all elements in the group from the call log.
698 // We don't have this action for voicemails, because you can just use the trash button.
699 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
700 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
701 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
702 return super.onPrepareOptionsMenu(menu);
703 }
704
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
706 final StringBuilder callIds = new StringBuilder();
707 for (Uri callUri : getCallLogEntryUris()) {
708 if (callIds.length() != 0) {
709 callIds.append(",");
710 }
711 callIds.append(ContentUris.parseId(callUri));
712 }
713 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
714 new AsyncTask<Void, Void, Void>() {
715 @Override
716 public Void doInBackground(Void... params) {
717 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
718 Calls._ID + " IN (" + callIds + ")", null);
719 return null;
720 }
721
722 @Override
723 public void onPostExecute(Void result) {
724 finish();
725 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700726 }
727 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 }
729
730 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700731 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 }
733
734 public void onMenuTrashVoicemail(MenuItem menuItem) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700735 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
736 new AsyncTask<Void, Void, Void>() {
737 @Override
738 public Void doInBackground(Void... params) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700739 getContentResolver().delete(mVoicemailUri, null, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700740 return null;
741 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700742
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 @Override
744 public void onPostExecute(Void result) {
745 finish();
746 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700747 }
748 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700749 }
750
751 @Override
752 protected void onPause() {
753 // Immediately stop the proximity sensor.
754 disableProximitySensor(false);
755 mProximitySensorListener.clearPendingRequests();
756 super.onPause();
757 }
758
759 @Override
760 public void enableProximitySensor() {
761 mProximitySensorManager.enable();
762 }
763
764 @Override
765 public void disableProximitySensor(boolean waitForFarState) {
766 mProximitySensorManager.disable(waitForFarState);
767 }
768
Chiao Cheng94b10b52012-08-17 16:59:12 -0700769 private void closeSystemDialogs() {
770 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
771 }
772
Chiao Cheng35071c02012-10-15 18:36:24 -0700773 /** Returns the given text, forced to be left-to-right. */
774 private static CharSequence forceLeftToRight(CharSequence text) {
775 StringBuilder sb = new StringBuilder();
776 sb.append(LEFT_TO_RIGHT_EMBEDDING);
777 sb.append(text);
778 sb.append(POP_DIRECTIONAL_FORMATTING);
779 return sb.toString();
780 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700781}