blob: 5ef75dbeaf23733eca3d983c32568c1e9ecedf3d [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
Nancy Chenb2eebaf2014-07-21 13:41:36 -070019import android.content.ComponentName;
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;
Nancy Chen87ba4892014-06-11 17:56:07 -070027import android.graphics.drawable.Drawable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.net.Uri;
29import android.os.AsyncTask;
30import android.os.Bundle;
31import android.provider.CallLog;
32import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.provider.ContactsContract.CommonDataKinds.Phone;
Yorke Lee8cd94232014-07-23 14:05:31 -070034import android.provider.VoicemailContract;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.VoicemailContract.Voicemails;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070036import android.telecomm.PhoneAccount;
37import android.telecomm.TelecommManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.telephony.TelephonyManager;
39import 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;
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;
Sai Cheemalapati7fa029f2014-07-29 15:03:55 -070071import com.android.dialerbind.analytics.AnalyticsActivity;
Chiao Cheng94b10b52012-08-17 16:59:12 -070072
73import java.util.List;
74
75/**
76 * Displays the details of a specific call log entry.
77 * <p>
78 * This activity can be either started with the URI of a single call log entry, or with the
79 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
80 */
Sai Cheemalapati7fa029f2014-07-29 15:03:55 -070081public class CallDetailActivity extends AnalyticsActivity implements ProximitySensorAware {
Chiao Cheng94b10b52012-08-17 16:59:12 -070082 private static final String TAG = "CallDetail";
83
Yorke Lee03459442013-10-30 18:29:32 -070084 private static final int LOADER_ID = 0;
85 private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
86
Chiao Cheng35071c02012-10-15 18:36:24 -070087 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
88 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
89
Chiao Cheng94b10b52012-08-17 16:59:12 -070090 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
91 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
92 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
93 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
94
95 /** The enumeration of {@link AsyncTask} objects used in this class. */
96 public enum Tasks {
97 MARK_VOICEMAIL_READ,
98 DELETE_VOICEMAIL_AND_FINISH,
99 REMOVE_FROM_CALL_LOG_AND_FINISH,
100 UPDATE_PHONE_CALL_DETAILS,
101 }
102
103 /** A long array extra containing ids of call log entries to display. */
104 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
105 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
106 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
107 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
108 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
109 /** If the activity was triggered from a notification. */
110 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
111
Yorke Lee8cd94232014-07-23 14:05:31 -0700112 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
113
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800115 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700116 private QuickContactBadge mQuickContactBadge;
117 private TextView mCallerName;
118 private TextView mCallerNumber;
Nancy Chen19702632014-07-25 13:23:16 -0700119 private TextView mAccountLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700120 private AsyncTaskExecutor mAsyncTaskExecutor;
121 private ContactInfoHelper mContactInfoHelper;
122
123 private String mNumber = null;
124 private String mDefaultCountryIso;
125
126 /* package */ LayoutInflater mInflater;
127 /* package */ Resources mResources;
128 /** Helper to load contact photos. */
129 private ContactPhotoManager mContactPhotoManager;
130 /** Helper to make async queries to content resolver. */
131 private CallDetailActivityQueryHandler mAsyncQueryHandler;
132 /** Helper to get voicemail status messages. */
133 private VoicemailStatusHelper mVoicemailStatusHelper;
134 // Views related to voicemail status message.
135 private View mStatusMessageView;
136 private TextView mStatusMessageText;
137 private TextView mStatusMessageAction;
Yorke Lee8cd94232014-07-23 14:05:31 -0700138 private TextView mVoicemailTranscription;
Yorke Lee257b6f22014-07-31 14:16:44 -0700139 private LinearLayout mVoicemailHeader;
140
141 private Uri mVoicemailUri;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142
143 /** Whether we should show "edit number before call" in the options menu. */
144 private boolean mHasEditNumberBeforeCallOption;
145 /** Whether we should show "trash" in the options menu. */
146 private boolean mHasTrashOption;
147 /** Whether we should show "remove from call log" in the options menu. */
148 private boolean mHasRemoveFromCallLogOption;
149
150 private ProximitySensorManager mProximitySensorManager;
151 private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
152
Chiao Cheng94b10b52012-08-17 16:59:12 -0700153 /** Listener to changes in the proximity sensor state. */
154 private class ProximitySensorListener implements ProximitySensorManager.Listener {
155 /** Used to show a blank view and hide the action bar. */
156 private final Runnable mBlankRunnable = new Runnable() {
157 @Override
158 public void run() {
159 View blankView = findViewById(R.id.blank);
160 blankView.setVisibility(View.VISIBLE);
161 getActionBar().hide();
162 }
163 };
164 /** Used to remove the blank view and show the action bar. */
165 private final Runnable mUnblankRunnable = new Runnable() {
166 @Override
167 public void run() {
168 View blankView = findViewById(R.id.blank);
169 blankView.setVisibility(View.GONE);
170 getActionBar().show();
171 }
172 };
173
174 @Override
175 public synchronized void onNear() {
176 clearPendingRequests();
177 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
178 }
179
180 @Override
181 public synchronized void onFar() {
182 clearPendingRequests();
183 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
184 }
185
186 /** Removed any delayed requests that may be pending. */
187 public synchronized void clearPendingRequests() {
188 View blankView = findViewById(R.id.blank);
189 blankView.removeCallbacks(mBlankRunnable);
190 blankView.removeCallbacks(mUnblankRunnable);
191 }
192
193 /** Post a {@link Runnable} with a delay on the main thread. */
194 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
195 // Post these instead of executing immediately so that:
196 // - They are guaranteed to be executed on the main thread.
197 // - If the sensor values changes rapidly for some time, the UI will not be
198 // updated immediately.
199 View blankView = findViewById(R.id.blank);
200 blankView.postDelayed(runnable, delayMillis);
201 }
202 }
203
204 static final String[] CALL_LOG_PROJECTION = new String[] {
205 CallLog.Calls.DATE,
206 CallLog.Calls.DURATION,
207 CallLog.Calls.NUMBER,
208 CallLog.Calls.TYPE,
209 CallLog.Calls.COUNTRY_ISO,
210 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700211 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700212 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
213 CallLog.Calls.PHONE_ACCOUNT_ID,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700214 CallLog.Calls.FEATURES,
Yorke Lee8cd94232014-07-23 14:05:31 -0700215 CallLog.Calls.DATA_USAGE,
216 CallLog.Calls.TRANSCRIPTION
Chiao Cheng94b10b52012-08-17 16:59:12 -0700217 };
218
219 static final int DATE_COLUMN_INDEX = 0;
220 static final int DURATION_COLUMN_INDEX = 1;
221 static final int NUMBER_COLUMN_INDEX = 2;
222 static final int CALL_TYPE_COLUMN_INDEX = 3;
223 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
224 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700225 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700226 static final int ACCOUNT_COMPONENT_NAME = 7;
227 static final int ACCOUNT_ID = 8;
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700228 static final int FEATURES = 9;
229 static final int DATA_USAGE = 10;
Yorke Lee8cd94232014-07-23 14:05:31 -0700230 static final int TRANSCRIPTION_COLUMN_INDEX = 11;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700231
Chiao Cheng94b10b52012-08-17 16:59:12 -0700232 @Override
233 protected void onCreate(Bundle icicle) {
234 super.onCreate(icicle);
235
236 setContentView(R.layout.call_detail);
237
238 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
239 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
240 mResources = getResources();
241
242 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee24ec3192013-11-19 13:52:45 -0800243 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700244 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
245 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Yorke Lee257b6f22014-07-31 14:16:44 -0700246
247 mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
248
Tyler Gunn18164c82014-06-09 10:20:57 -0700249 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
250 mQuickContactBadge.setOverlay(null);
251 mCallerName = (TextView) findViewById(R.id.caller_name);
252 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700253 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700254 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700255 mContactPhotoManager = ContactPhotoManager.getInstance(this);
256 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700257 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700258 getActionBar().setDisplayHomeAsUpEnabled(true);
Yorke Lee257b6f22014-07-31 14:16:44 -0700259
Chiao Cheng94b10b52012-08-17 16:59:12 -0700260 optionallyHandleVoicemail();
261 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
262 closeSystemDialogs();
263 }
264 }
265
266 @Override
267 public void onResume() {
268 super.onResume();
269 updateData(getCallLogEntryUris());
270 }
271
272 /**
273 * Handle voicemail playback or hide voicemail ui.
274 * <p>
275 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
Yorke Lee257b6f22014-07-31 14:16:44 -0700276 * playback. If it doesn't, then don't inflate the voicemail ui.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700277 */
278 private void optionallyHandleVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700279
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 if (hasVoicemail()) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700281 LayoutInflater inflater =
282 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
283 mVoicemailHeader =
284 (LinearLayout) inflater.inflate(R.layout.call_details_voicemail_header, null);
285 View voicemailContainer = mVoicemailHeader.findViewById(R.id.voicemail_container);
286 mStatusMessageView = mVoicemailHeader.findViewById(R.id.voicemail_status);
287 mStatusMessageText =
288 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_message);
289 mStatusMessageAction =
290 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_action);
291 mVoicemailTranscription = (
292 TextView) mVoicemailHeader.findViewById(R.id.voicemail_transcription);
293 ListView historyList = (ListView) findViewById(R.id.history);
294 historyList.addHeaderView(mVoicemailHeader);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
296 // to play and optionally start the playback.
297 // Do a query to fetch the voicemail status messages.
Yorke Lee8cd94232014-07-23 14:05:31 -0700298 VoicemailPlaybackFragment playbackFragment;
299
300 playbackFragment = (VoicemailPlaybackFragment) getFragmentManager().findFragmentByTag(
301 VOICEMAIL_FRAGMENT_TAG);
302
303 if (playbackFragment == null) {
304 playbackFragment = new VoicemailPlaybackFragment();
305 Bundle fragmentArguments = new Bundle();
Yorke Lee257b6f22014-07-31 14:16:44 -0700306 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, mVoicemailUri);
Yorke Lee8cd94232014-07-23 14:05:31 -0700307 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
308 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
309 }
310 playbackFragment.setArguments(fragmentArguments);
311 getFragmentManager().beginTransaction()
312 .add(R.id.voicemail_container, playbackFragment, VOICEMAIL_FRAGMENT_TAG)
313 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700314 }
Yorke Lee8cd94232014-07-23 14:05:31 -0700315
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 voicemailContainer.setVisibility(View.VISIBLE);
Yorke Lee257b6f22014-07-31 14:16:44 -0700317 mAsyncQueryHandler.startVoicemailStatusQuery(mVoicemailUri);
318 markVoicemailAsRead(mVoicemailUri);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 }
320 }
321
322 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700323 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700324 }
325
326 private void markVoicemailAsRead(final Uri voicemailUri) {
327 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
328 @Override
329 public Void doInBackground(Void... params) {
330 ContentValues values = new ContentValues();
331 values.put(Voicemails.IS_READ, true);
332 getContentResolver().update(voicemailUri, values,
333 Voicemails.IS_READ + " = 0", null);
334 return null;
335 }
336 });
337 }
338
339 /**
340 * Returns the list of URIs to show.
341 * <p>
342 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
343 * a list of ids in the call log added as an extra on the URI.
344 * <p>
345 * If both are available, the data on the intent takes precedence.
346 */
347 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700348 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700349 if (uri != null) {
350 // If there is a data on the intent, it takes precedence over the extra.
351 return new Uri[]{ uri };
352 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700353 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
354 final int numIds = ids == null ? 0 : ids.length;
355 final Uri[] uris = new Uri[numIds];
356 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700357 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
358 }
359 return uris;
360 }
361
362 @Override
363 public boolean onKeyDown(int keyCode, KeyEvent event) {
364 switch (keyCode) {
365 case KeyEvent.KEYCODE_CALL: {
366 // Make sure phone isn't already busy before starting direct call
367 TelephonyManager tm = (TelephonyManager)
368 getSystemService(Context.TELEPHONY_SERVICE);
369 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700370 DialerUtils.startActivityWithErrorToast(this,
371 CallUtil.getCallIntent(Uri.fromParts(CallUtil.SCHEME_TEL, mNumber,
Nancy Chen87ba4892014-06-11 17:56:07 -0700372 null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700373 return true;
374 }
375 }
376 }
377
378 return super.onKeyDown(keyCode, event);
379 }
380
381 /**
382 * Update user interface with details of given call.
383 *
384 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
385 */
386 private void updateData(final Uri... callUris) {
387 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
388 @Override
389 public PhoneCallDetails[] doInBackground(Void... params) {
390 // TODO: All phone calls correspond to the same person, so we can make a single
391 // lookup.
392 final int numCalls = callUris.length;
393 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
394 try {
395 for (int index = 0; index < numCalls; ++index) {
396 details[index] = getPhoneCallDetailsForUri(callUris[index]);
397 }
398 return details;
399 } catch (IllegalArgumentException e) {
400 // Something went wrong reading in our primary data.
401 Log.w(TAG, "invalid URI starting call details", e);
402 return null;
403 }
404 }
405
406 @Override
407 public void onPostExecute(PhoneCallDetails[] details) {
408 if (details == null) {
409 // Somewhere went wrong: we're going to bail out and show error to users.
410 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
411 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;
423
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);
427 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
428 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
429 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700430
Tyler Gunn18164c82014-06-09 10:20:57 -0700431 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700432
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800433 final CharSequence displayNumber =
434 mPhoneNumberHelper.getDisplayNumber(
435 firstDetails.number,
436 firstDetails.numberPresentation,
437 firstDetails.formattedNumber);
438
Tyler Gunn18164c82014-06-09 10:20:57 -0700439 if (!TextUtils.isEmpty(firstDetails.name)) {
440 mCallerName.setText(firstDetails.name);
441 mCallerNumber.setText(callLocationOrType + " " + displayNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700442 } else {
Tyler Gunn18164c82014-06-09 10:20:57 -0700443 mCallerName.setText(displayNumber);
444 if (!TextUtils.isEmpty(callLocationOrType)) {
445 mCallerNumber.setText(callLocationOrType);
446 mCallerNumber.setVisibility(View.VISIBLE);
447 } else {
448 mCallerNumber.setVisibility(View.GONE);
449 }
450
Chiao Cheng94b10b52012-08-17 16:59:12 -0700451 }
452
Nancy Chen19702632014-07-25 13:23:16 -0700453 if (!TextUtils.isEmpty(firstDetails.accountLabel)) {
454 mAccountLabel.setText(firstDetails.accountLabel);
455 mAccountLabel.setVisibility(View.VISIBLE);
456 } else {
457 mAccountLabel.setVisibility(View.GONE);
458 }
459
Chiao Cheng94b10b52012-08-17 16:59:12 -0700460 mHasEditNumberBeforeCallOption =
461 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
462 mHasTrashOption = hasVoicemail();
463 mHasRemoveFromCallLogOption = !hasVoicemail();
464 invalidateOptionsMenu();
465
466 ListView historyList = (ListView) findViewById(R.id.history);
467 historyList.setAdapter(
468 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
Tyler Gunn18164c82014-06-09 10:20:57 -0700469 mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700470
Tyler Gunn18164c82014-06-09 10:20:57 -0700471 String lookupKey = contactUri == null ? null
472 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800473
474 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
475
476 final int contactType =
477 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
478 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
479 ContactPhotoManager.TYPE_DEFAULT;
480
Tyler Gunn18164c82014-06-09 10:20:57 -0700481 String nameForDefaultImage;
482 if (TextUtils.isEmpty(firstDetails.name)) {
483 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(firstDetails.number,
484 firstDetails.numberPresentation,
485 firstDetails.formattedNumber).toString();
486 } else {
487 nameForDefaultImage = firstDetails.name.toString();
488 }
489
Yorke Lee8cd94232014-07-23 14:05:31 -0700490 if (hasVoicemail() && !TextUtils.isEmpty(firstDetails.transcription)) {
491 mVoicemailTranscription.setText(firstDetails.transcription);
492 mVoicemailTranscription.setVisibility(View.VISIBLE);
Yorke Lee8cd94232014-07-23 14:05:31 -0700493 }
494
Tyler Gunn18164c82014-06-09 10:20:57 -0700495 loadContactPhotos(
496 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700497 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
498 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700499
500 /**
501 * Determines the location geocode text for a call, or the phone number type
502 * (if available).
503 *
504 * @param details The call details.
505 * @return The phone number type or location.
506 */
507 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
508 if (!TextUtils.isEmpty(details.name)) {
509 return Phone.getTypeLabel(mResources, details.numberType,
510 details.numberLabel);
511 } else {
512 return details.geocode;
513 }
514 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700515 }
516 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
517 }
518
519 /** Return the phone call details for a given call log URI. */
520 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
521 ContentResolver resolver = getContentResolver();
522 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
523 try {
524 if (callCursor == null || !callCursor.moveToFirst()) {
525 throw new IllegalArgumentException("Cannot find content: " + callUri);
526 }
527
528 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700529 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
530 final int numberPresentation = callCursor.getInt(
531 NUMBER_PRESENTATION_COLUMN_INDEX);
532 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
533 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
534 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700535 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
536 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Yorke Lee8cd94232014-07-23 14:05:31 -0700537 final String transcription = callCursor.getString(TRANSCRIPTION_COLUMN_INDEX);
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700538
Nancy Chen19702632014-07-25 13:23:16 -0700539 final String accountLabel = PhoneAccountUtils.getAccountLabel(this,
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700540 PhoneAccountUtils.getAccount(
541 callCursor.getString(ACCOUNT_COMPONENT_NAME),
542 callCursor.getString(ACCOUNT_ID)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700543
544 if (TextUtils.isEmpty(countryIso)) {
545 countryIso = mDefaultCountryIso;
546 }
547
548 // Formatted phone number.
549 final CharSequence formattedNumber;
550 // Read contact specifics.
551 final CharSequence nameText;
552 final int numberType;
553 final CharSequence numberLabel;
554 final Uri photoUri;
555 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800556 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700557 // If this is not a regular number, there is no point in looking it up in the contacts.
558 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700559 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700560 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 ? mContactInfoHelper.lookupNumber(number, countryIso)
562 : null;
563 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700564 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
565 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700566 nameText = "";
567 numberType = 0;
568 numberLabel = "";
569 photoUri = null;
570 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800571 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700572 } else {
573 formattedNumber = info.formattedNumber;
574 nameText = info.name;
575 numberType = info.type;
576 numberLabel = info.label;
577 photoUri = info.photoUri;
578 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800579 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700580 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700581 final int features = callCursor.getInt(FEATURES);
582 Long dataUsage = null;
583 if (!callCursor.isNull(DATA_USAGE)) {
584 dataUsage = callCursor.getLong(DATA_USAGE);
585 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700586 return new PhoneCallDetails(number, numberPresentation,
587 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700588 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700589 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Nancy Chen19702632014-07-25 13:23:16 -0700590 accountLabel, null, features, dataUsage, transcription);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700591 } finally {
592 if (callCursor != null) {
593 callCursor.close();
594 }
595 }
596 }
597
598 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700599 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
600 String lookupKey, int contactType) {
601
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800602 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700603 contactType, true /* isCircular */);
604
605 mQuickContactBadge.assignContactUri(contactUri);
606 mQuickContactBadge.setContentDescription(
607 mResources.getString(R.string.description_contact_details, displayName));
608
609 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
610 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700611 }
612
613 static final class ViewEntry {
614 public final String text;
615 public final Intent primaryIntent;
616 /** The description for accessibility of the primary action. */
617 public final String primaryDescription;
618
619 public CharSequence label = null;
620 /** Icon for the secondary action. */
621 public int secondaryIcon = 0;
622 /** Intent for the secondary action. If not null, an icon must be defined. */
623 public Intent secondaryIntent = null;
624 /** The description for accessibility of the secondary action. */
625 public String secondaryDescription = null;
626
627 public ViewEntry(String text, Intent intent, String description) {
628 this.text = text;
629 primaryIntent = intent;
630 primaryDescription = description;
631 }
632
633 public void setSecondaryAction(int icon, Intent intent, String description) {
634 secondaryIcon = icon;
635 secondaryIntent = intent;
636 secondaryDescription = description;
637 }
638 }
639
Chiao Cheng94b10b52012-08-17 16:59:12 -0700640 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
641 if (statusCursor == null) {
642 mStatusMessageView.setVisibility(View.GONE);
643 return;
644 }
645 final StatusMessage message = getStatusMessage(statusCursor);
646 if (message == null || !message.showInCallDetails()) {
647 mStatusMessageView.setVisibility(View.GONE);
648 return;
649 }
650
651 mStatusMessageView.setVisibility(View.VISIBLE);
652 mStatusMessageText.setText(message.callDetailsMessageId);
653 if (message.actionMessageId != -1) {
654 mStatusMessageAction.setText(message.actionMessageId);
655 }
656 if (message.actionUri != null) {
657 mStatusMessageAction.setClickable(true);
658 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
659 @Override
660 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700661 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
662 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700663 }
664 });
665 } else {
666 mStatusMessageAction.setClickable(false);
667 }
668 }
669
670 private StatusMessage getStatusMessage(Cursor statusCursor) {
671 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
672 if (messages.size() == 0) {
673 return null;
674 }
675 // There can only be a single status message per source package, so num of messages can
676 // at most be 1.
677 if (messages.size() > 1) {
678 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
679 " Will use the first one.", messages.size()));
680 }
681 return messages.get(0);
682 }
683
684 @Override
685 public boolean onCreateOptionsMenu(Menu menu) {
686 getMenuInflater().inflate(R.menu.call_details_options, menu);
687 return super.onCreateOptionsMenu(menu);
688 }
689
690 @Override
691 public boolean onPrepareOptionsMenu(Menu menu) {
692 // This action deletes all elements in the group from the call log.
693 // We don't have this action for voicemails, because you can just use the trash button.
694 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
695 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
696 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
697 return super.onPrepareOptionsMenu(menu);
698 }
699
Chiao Cheng94b10b52012-08-17 16:59:12 -0700700 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
701 final StringBuilder callIds = new StringBuilder();
702 for (Uri callUri : getCallLogEntryUris()) {
703 if (callIds.length() != 0) {
704 callIds.append(",");
705 }
706 callIds.append(ContentUris.parseId(callUri));
707 }
708 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
709 new AsyncTask<Void, Void, Void>() {
710 @Override
711 public Void doInBackground(Void... params) {
712 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
713 Calls._ID + " IN (" + callIds + ")", null);
714 return null;
715 }
716
717 @Override
718 public void onPostExecute(Void result) {
719 finish();
720 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700721 }
722 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 }
724
725 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700726 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 }
728
729 public void onMenuTrashVoicemail(MenuItem menuItem) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
731 new AsyncTask<Void, Void, Void>() {
732 @Override
733 public Void doInBackground(Void... params) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700734 getContentResolver().delete(mVoicemailUri, null, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700735 return null;
736 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700737
Chiao Cheng94b10b52012-08-17 16:59:12 -0700738 @Override
739 public void onPostExecute(Void result) {
740 finish();
741 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700742 }
743 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 }
745
746 @Override
747 protected void onPause() {
748 // Immediately stop the proximity sensor.
749 disableProximitySensor(false);
750 mProximitySensorListener.clearPendingRequests();
751 super.onPause();
752 }
753
754 @Override
755 public void enableProximitySensor() {
756 mProximitySensorManager.enable();
757 }
758
759 @Override
760 public void disableProximitySensor(boolean waitForFarState) {
761 mProximitySensorManager.disable(waitForFarState);
762 }
763
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 private void closeSystemDialogs() {
765 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
766 }
767
Chiao Cheng35071c02012-10-15 18:36:24 -0700768 /** Returns the given text, forced to be left-to-right. */
769 private static CharSequence forceLeftToRight(CharSequence text) {
770 StringBuilder sb = new StringBuilder();
771 sb.append(LEFT_TO_RIGHT_EMBEDDING);
772 sb.append(text);
773 sb.append(POP_DIRECTIONAL_FORMATTING);
774 return sb.toString();
775 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700776}