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