blob: fb663d729f8951db2471058d6b77362c77b719b2 [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);
248 mCallerName = (TextView) findViewById(R.id.caller_name);
249 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700250 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700251 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700252 mContactPhotoManager = ContactPhotoManager.getInstance(this);
253 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700254 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700255 getActionBar().setDisplayHomeAsUpEnabled(true);
Yorke Lee257b6f22014-07-31 14:16:44 -0700256
Chiao Cheng94b10b52012-08-17 16:59:12 -0700257 optionallyHandleVoicemail();
258 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
259 closeSystemDialogs();
260 }
261 }
262
263 @Override
264 public void onResume() {
265 super.onResume();
266 updateData(getCallLogEntryUris());
267 }
268
269 /**
270 * Handle voicemail playback or hide voicemail ui.
271 * <p>
272 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
Yorke Lee257b6f22014-07-31 14:16:44 -0700273 * playback. If it doesn't, then don't inflate the voicemail ui.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700274 */
275 private void optionallyHandleVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700276
Chiao Cheng94b10b52012-08-17 16:59:12 -0700277 if (hasVoicemail()) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700278 LayoutInflater inflater =
279 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
280 mVoicemailHeader =
281 (LinearLayout) inflater.inflate(R.layout.call_details_voicemail_header, null);
282 View voicemailContainer = mVoicemailHeader.findViewById(R.id.voicemail_container);
283 mStatusMessageView = mVoicemailHeader.findViewById(R.id.voicemail_status);
284 mStatusMessageText =
285 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_message);
286 mStatusMessageAction =
287 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_action);
288 mVoicemailTranscription = (
289 TextView) mVoicemailHeader.findViewById(R.id.voicemail_transcription);
290 ListView historyList = (ListView) findViewById(R.id.history);
291 historyList.addHeaderView(mVoicemailHeader);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700292 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
293 // to play and optionally start the playback.
294 // Do a query to fetch the voicemail status messages.
Yorke Lee8cd94232014-07-23 14:05:31 -0700295 VoicemailPlaybackFragment playbackFragment;
296
297 playbackFragment = (VoicemailPlaybackFragment) getFragmentManager().findFragmentByTag(
298 VOICEMAIL_FRAGMENT_TAG);
299
300 if (playbackFragment == null) {
301 playbackFragment = new VoicemailPlaybackFragment();
302 Bundle fragmentArguments = new Bundle();
Yorke Lee257b6f22014-07-31 14:16:44 -0700303 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, mVoicemailUri);
Yorke Lee8cd94232014-07-23 14:05:31 -0700304 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
305 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
306 }
307 playbackFragment.setArguments(fragmentArguments);
308 getFragmentManager().beginTransaction()
309 .add(R.id.voicemail_container, playbackFragment, VOICEMAIL_FRAGMENT_TAG)
310 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700311 }
Yorke Lee8cd94232014-07-23 14:05:31 -0700312
Chiao Cheng94b10b52012-08-17 16:59:12 -0700313 voicemailContainer.setVisibility(View.VISIBLE);
Yorke Lee257b6f22014-07-31 14:16:44 -0700314 mAsyncQueryHandler.startVoicemailStatusQuery(mVoicemailUri);
315 markVoicemailAsRead(mVoicemailUri);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 }
317 }
318
319 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700320 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321 }
322
323 private void markVoicemailAsRead(final Uri voicemailUri) {
324 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
325 @Override
326 public Void doInBackground(Void... params) {
327 ContentValues values = new ContentValues();
328 values.put(Voicemails.IS_READ, true);
329 getContentResolver().update(voicemailUri, values,
330 Voicemails.IS_READ + " = 0", null);
331 return null;
332 }
333 });
334 }
335
336 /**
337 * Returns the list of URIs to show.
338 * <p>
339 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
340 * a list of ids in the call log added as an extra on the URI.
341 * <p>
342 * If both are available, the data on the intent takes precedence.
343 */
344 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700345 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700346 if (uri != null) {
347 // If there is a data on the intent, it takes precedence over the extra.
348 return new Uri[]{ uri };
349 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700350 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
351 final int numIds = ids == null ? 0 : ids.length;
352 final Uri[] uris = new Uri[numIds];
353 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700354 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
355 }
356 return uris;
357 }
358
359 @Override
360 public boolean onKeyDown(int keyCode, KeyEvent event) {
361 switch (keyCode) {
362 case KeyEvent.KEYCODE_CALL: {
363 // Make sure phone isn't already busy before starting direct call
364 TelephonyManager tm = (TelephonyManager)
365 getSystemService(Context.TELEPHONY_SERVICE);
366 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700367 DialerUtils.startActivityWithErrorToast(this,
Brian Attwell82043242015-02-03 14:13:58 -0800368 PrivilegedCallUtil.getCallIntent(Uri.fromParts(PhoneAccount.SCHEME_TEL,
369 mNumber, null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700370 return true;
371 }
372 }
373 }
374
375 return super.onKeyDown(keyCode, event);
376 }
377
378 /**
379 * Update user interface with details of given call.
380 *
Nancy Chene80d6222014-10-08 20:17:55 -0700381 * @param callUris URIs into {@link android.provider.CallLog.Calls} of the calls to be displayed
Chiao Cheng94b10b52012-08-17 16:59:12 -0700382 */
383 private void updateData(final Uri... callUris) {
384 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
385 @Override
386 public PhoneCallDetails[] doInBackground(Void... params) {
387 // TODO: All phone calls correspond to the same person, so we can make a single
388 // lookup.
389 final int numCalls = callUris.length;
390 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
391 try {
392 for (int index = 0; index < numCalls; ++index) {
393 details[index] = getPhoneCallDetailsForUri(callUris[index]);
394 }
395 return details;
396 } catch (IllegalArgumentException e) {
397 // Something went wrong reading in our primary data.
398 Log.w(TAG, "invalid URI starting call details", e);
399 return null;
400 }
401 }
402
403 @Override
404 public void onPostExecute(PhoneCallDetails[] details) {
Nancy Chene80d6222014-10-08 20:17:55 -0700405 Context context = CallDetailActivity.this;
406
Chiao Cheng94b10b52012-08-17 16:59:12 -0700407 if (details == null) {
408 // Somewhere went wrong: we're going to bail out and show error to users.
Nancy Chene80d6222014-10-08 20:17:55 -0700409 Toast.makeText(context, R.string.toast_call_detail_error,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700410 Toast.LENGTH_SHORT).show();
411 finish();
412 return;
413 }
414
415 // We know that all calls are from the same number and the same contact, so pick the
416 // first.
417 PhoneCallDetails firstDetails = details[0];
418 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700419 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700420 final Uri contactUri = firstDetails.contactUri;
421 final Uri photoUri = firstDetails.photoUri;
Nancy Chene80d6222014-10-08 20:17:55 -0700422 final PhoneAccountHandle accountHandle = firstDetails.accountHandle;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700425 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700426 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
Nancy Chene80d6222014-10-08 20:17:55 -0700427 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper(context);
428 final boolean isVoicemailNumber =
429 phoneUtils.isVoicemailNumber(accountHandle, mNumber);
430 final boolean isSipNumber = PhoneNumberUtilsWrapper.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700431
Tyler Gunn18164c82014-06-09 10:20:57 -0700432 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700433
Nancy Chene80d6222014-10-08 20:17:55 -0700434 final CharSequence displayNumber =
435 mPhoneNumberHelper.getDisplayNumber(
436 firstDetails.accountHandle,
437 firstDetails.number,
438 firstDetails.numberPresentation,
439 firstDetails.formattedNumber);
Andrew Lee99e8adb2014-10-02 12:27:37 -0700440 final String displayNumberStr = mBidiFormatter.unicodeWrap(
441 displayNumber.toString(), TextDirectionHeuristics.LTR);
442
Tyler Gunn18164c82014-06-09 10:20:57 -0700443 if (!TextUtils.isEmpty(firstDetails.name)) {
444 mCallerName.setText(firstDetails.name);
Andrew Lee99e8adb2014-10-02 12:27:37 -0700445 mCallerNumber.setText(callLocationOrType + " " + displayNumberStr);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700446 } else {
Andrew Lee99e8adb2014-10-02 12:27:37 -0700447 mCallerName.setText(displayNumberStr);
Tyler Gunn18164c82014-06-09 10:20:57 -0700448 if (!TextUtils.isEmpty(callLocationOrType)) {
449 mCallerNumber.setText(callLocationOrType);
450 mCallerNumber.setVisibility(View.VISIBLE);
451 } else {
452 mCallerNumber.setVisibility(View.GONE);
453 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700454 }
455
Nancy Chene80d6222014-10-08 20:17:55 -0700456 String accountLabel = PhoneAccountUtils.getAccountLabel(context, accountHandle);
457 if (!TextUtils.isEmpty(accountLabel)) {
458 mAccountLabel.setText(accountLabel);
Nancy Chen19702632014-07-25 13:23:16 -0700459 mAccountLabel.setVisibility(View.VISIBLE);
460 } else {
461 mAccountLabel.setVisibility(View.GONE);
462 }
463
Chiao Cheng94b10b52012-08-17 16:59:12 -0700464 mHasEditNumberBeforeCallOption =
465 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
466 mHasTrashOption = hasVoicemail();
467 mHasRemoveFromCallLogOption = !hasVoicemail();
468 invalidateOptionsMenu();
469
470 ListView historyList = (ListView) findViewById(R.id.history);
471 historyList.setAdapter(
Nancy Chene80d6222014-10-08 20:17:55 -0700472 new CallDetailHistoryAdapter(context, mInflater, mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700473
Tyler Gunn18164c82014-06-09 10:20:57 -0700474 String lookupKey = contactUri == null ? null
475 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800476
477 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
478
479 final int contactType =
480 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
481 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
482 ContactPhotoManager.TYPE_DEFAULT;
483
Tyler Gunn18164c82014-06-09 10:20:57 -0700484 String nameForDefaultImage;
485 if (TextUtils.isEmpty(firstDetails.name)) {
Nancy Chene80d6222014-10-08 20:17:55 -0700486 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(
487 firstDetails.accountHandle,
488 firstDetails.number,
Tyler Gunn18164c82014-06-09 10:20:57 -0700489 firstDetails.numberPresentation,
490 firstDetails.formattedNumber).toString();
491 } else {
492 nameForDefaultImage = firstDetails.name.toString();
493 }
494
Yorke Lee8cd94232014-07-23 14:05:31 -0700495 if (hasVoicemail() && !TextUtils.isEmpty(firstDetails.transcription)) {
496 mVoicemailTranscription.setText(firstDetails.transcription);
497 mVoicemailTranscription.setVisibility(View.VISIBLE);
Yorke Lee8cd94232014-07-23 14:05:31 -0700498 }
499
Tyler Gunn18164c82014-06-09 10:20:57 -0700500 loadContactPhotos(
501 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700502 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
503 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700504
505 /**
506 * Determines the location geocode text for a call, or the phone number type
507 * (if available).
508 *
509 * @param details The call details.
510 * @return The phone number type or location.
511 */
512 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
513 if (!TextUtils.isEmpty(details.name)) {
514 return Phone.getTypeLabel(mResources, details.numberType,
515 details.numberLabel);
516 } else {
517 return details.geocode;
518 }
519 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700520 }
521 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
522 }
523
524 /** Return the phone call details for a given call log URI. */
525 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
526 ContentResolver resolver = getContentResolver();
527 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
528 try {
529 if (callCursor == null || !callCursor.moveToFirst()) {
530 throw new IllegalArgumentException("Cannot find content: " + callUri);
531 }
532
533 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700534 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
535 final int numberPresentation = callCursor.getInt(
536 NUMBER_PRESENTATION_COLUMN_INDEX);
537 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
538 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
539 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700540 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
541 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Yorke Lee8cd94232014-07-23 14:05:31 -0700542 final String transcription = callCursor.getString(TRANSCRIPTION_COLUMN_INDEX);
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700543
Nancy Chene80d6222014-10-08 20:17:55 -0700544 final PhoneAccountHandle accountHandle = PhoneAccountUtils.getAccount(
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700545 callCursor.getString(ACCOUNT_COMPONENT_NAME),
Nancy Chene80d6222014-10-08 20:17:55 -0700546 callCursor.getString(ACCOUNT_ID));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700547
548 if (TextUtils.isEmpty(countryIso)) {
549 countryIso = mDefaultCountryIso;
550 }
551
552 // Formatted phone number.
553 final CharSequence formattedNumber;
554 // Read contact specifics.
555 final CharSequence nameText;
556 final int numberType;
557 final CharSequence numberLabel;
558 final Uri photoUri;
559 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800560 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 // If this is not a regular number, there is no point in looking it up in the contacts.
562 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700563 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Nancy Chene80d6222014-10-08 20:17:55 -0700564 && !new PhoneNumberUtilsWrapper(this).isVoicemailNumber(accountHandle, number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700565 ? mContactInfoHelper.lookupNumber(number, countryIso)
566 : null;
567 if (info == null) {
Nancy Chene80d6222014-10-08 20:17:55 -0700568 formattedNumber = mPhoneNumberHelper.getDisplayNumber(accountHandle, number,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700569 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700570 nameText = "";
571 numberType = 0;
572 numberLabel = "";
573 photoUri = null;
574 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800575 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700576 } else {
577 formattedNumber = info.formattedNumber;
578 nameText = info.name;
579 numberType = info.type;
580 numberLabel = info.label;
581 photoUri = info.photoUri;
582 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800583 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700584 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700585 final int features = callCursor.getInt(FEATURES);
586 Long dataUsage = null;
587 if (!callCursor.isNull(DATA_USAGE)) {
588 dataUsage = callCursor.getLong(DATA_USAGE);
589 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700590 return new PhoneCallDetails(number, numberPresentation,
591 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700592 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700593 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Nancy Chene80d6222014-10-08 20:17:55 -0700594 accountHandle, features, dataUsage, transcription);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700595 } finally {
596 if (callCursor != null) {
597 callCursor.close();
598 }
599 }
600 }
601
602 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700603 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
604 String lookupKey, int contactType) {
605
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800606 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700607 contactType, true /* isCircular */);
608
609 mQuickContactBadge.assignContactUri(contactUri);
610 mQuickContactBadge.setContentDescription(
611 mResources.getString(R.string.description_contact_details, displayName));
612
613 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
614 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700615 }
616
617 static final class ViewEntry {
618 public final String text;
619 public final Intent primaryIntent;
620 /** The description for accessibility of the primary action. */
621 public final String primaryDescription;
622
623 public CharSequence label = null;
624 /** Icon for the secondary action. */
625 public int secondaryIcon = 0;
626 /** Intent for the secondary action. If not null, an icon must be defined. */
627 public Intent secondaryIntent = null;
628 /** The description for accessibility of the secondary action. */
629 public String secondaryDescription = null;
630
631 public ViewEntry(String text, Intent intent, String description) {
632 this.text = text;
633 primaryIntent = intent;
634 primaryDescription = description;
635 }
636
637 public void setSecondaryAction(int icon, Intent intent, String description) {
638 secondaryIcon = icon;
639 secondaryIntent = intent;
640 secondaryDescription = description;
641 }
642 }
643
Chiao Cheng94b10b52012-08-17 16:59:12 -0700644 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
645 if (statusCursor == null) {
646 mStatusMessageView.setVisibility(View.GONE);
647 return;
648 }
649 final StatusMessage message = getStatusMessage(statusCursor);
650 if (message == null || !message.showInCallDetails()) {
651 mStatusMessageView.setVisibility(View.GONE);
652 return;
653 }
654
655 mStatusMessageView.setVisibility(View.VISIBLE);
656 mStatusMessageText.setText(message.callDetailsMessageId);
657 if (message.actionMessageId != -1) {
658 mStatusMessageAction.setText(message.actionMessageId);
659 }
660 if (message.actionUri != null) {
661 mStatusMessageAction.setClickable(true);
662 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
663 @Override
664 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700665 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
666 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700667 }
668 });
669 } else {
670 mStatusMessageAction.setClickable(false);
671 }
672 }
673
674 private StatusMessage getStatusMessage(Cursor statusCursor) {
675 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
676 if (messages.size() == 0) {
677 return null;
678 }
679 // There can only be a single status message per source package, so num of messages can
680 // at most be 1.
681 if (messages.size() > 1) {
682 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
683 " Will use the first one.", messages.size()));
684 }
685 return messages.get(0);
686 }
687
688 @Override
689 public boolean onCreateOptionsMenu(Menu menu) {
690 getMenuInflater().inflate(R.menu.call_details_options, menu);
691 return super.onCreateOptionsMenu(menu);
692 }
693
694 @Override
695 public boolean onPrepareOptionsMenu(Menu menu) {
696 // This action deletes all elements in the group from the call log.
697 // We don't have this action for voicemails, because you can just use the trash button.
698 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
699 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
700 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
701 return super.onPrepareOptionsMenu(menu);
702 }
703
Chiao Cheng94b10b52012-08-17 16:59:12 -0700704 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
705 final StringBuilder callIds = new StringBuilder();
706 for (Uri callUri : getCallLogEntryUris()) {
707 if (callIds.length() != 0) {
708 callIds.append(",");
709 }
710 callIds.append(ContentUris.parseId(callUri));
711 }
712 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
713 new AsyncTask<Void, Void, Void>() {
714 @Override
715 public Void doInBackground(Void... params) {
716 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
717 Calls._ID + " IN (" + callIds + ")", null);
718 return null;
719 }
720
721 @Override
722 public void onPostExecute(Void result) {
723 finish();
724 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700725 }
726 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 }
728
729 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700730 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700731 }
732
733 public void onMenuTrashVoicemail(MenuItem menuItem) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700734 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
735 new AsyncTask<Void, Void, Void>() {
736 @Override
737 public Void doInBackground(Void... params) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700738 getContentResolver().delete(mVoicemailUri, null, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739 return null;
740 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700741
Chiao Cheng94b10b52012-08-17 16:59:12 -0700742 @Override
743 public void onPostExecute(Void result) {
744 finish();
745 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700746 }
747 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700748 }
749
750 @Override
751 protected void onPause() {
752 // Immediately stop the proximity sensor.
753 disableProximitySensor(false);
754 mProximitySensorListener.clearPendingRequests();
755 super.onPause();
756 }
757
758 @Override
759 public void enableProximitySensor() {
760 mProximitySensorManager.enable();
761 }
762
763 @Override
764 public void disableProximitySensor(boolean waitForFarState) {
765 mProximitySensorManager.disable(waitForFarState);
766 }
767
Chiao Cheng94b10b52012-08-17 16:59:12 -0700768 private void closeSystemDialogs() {
769 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
770 }
771
Chiao Cheng35071c02012-10-15 18:36:24 -0700772 /** Returns the given text, forced to be left-to-right. */
773 private static CharSequence forceLeftToRight(CharSequence text) {
774 StringBuilder sb = new StringBuilder();
775 sb.append(LEFT_TO_RIGHT_EMBEDDING);
776 sb.append(text);
777 sb.append(POP_DIRECTIONAL_FORMATTING);
778 return sb.toString();
779 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780}