blob: 32d61a27b1a265d17b116ee4c12d553e8863ba6c [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.content.ContentResolver;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.content.Context;
23import android.content.Intent;
24import android.content.res.Resources;
25import android.database.Cursor;
Chiao Cheng94b10b52012-08-17 16:59:12 -070026import android.net.Uri;
27import android.os.AsyncTask;
28import android.os.Bundle;
29import android.provider.CallLog;
30import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.provider.VoicemailContract.Voicemails;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070033import android.telecom.PhoneAccount;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.telephony.TelephonyManager;
35import android.text.TextUtils;
36import android.util.Log;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.view.KeyEvent;
38import android.view.LayoutInflater;
39import android.view.Menu;
40import android.view.MenuItem;
41import android.view.View;
Yorke Lee257b6f22014-07-31 14:16:44 -070042import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.widget.ListView;
Tyler Gunn18164c82014-06-09 10:20:57 -070044import android.widget.QuickContactBadge;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.widget.TextView;
46import android.widget.Toast;
47
Chiao Cheng35071c02012-10-15 18:36:24 -070048import com.android.contacts.common.ContactPhotoManager;
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070049import com.android.contacts.common.CallUtil;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080050import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070051import com.android.contacts.common.GeoUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070052import com.android.dialer.calllog.CallDetailHistoryAdapter;
53import com.android.dialer.calllog.CallTypeHelper;
54import com.android.dialer.calllog.ContactInfo;
55import com.android.dialer.calllog.ContactInfoHelper;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070056import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee24ec3192013-11-19 13:52:45 -080057import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070058import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070059import com.android.dialer.util.AsyncTaskExecutor;
60import com.android.dialer.util.AsyncTaskExecutors;
Yorke Lee39213592014-04-07 15:39:48 -070061import com.android.dialer.util.DialerUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070062import com.android.dialer.voicemail.VoicemailPlaybackFragment;
63import com.android.dialer.voicemail.VoicemailStatusHelper;
64import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
65import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
Sai Cheemalapati7fa029f2014-07-29 15:03:55 -070066import com.android.dialerbind.analytics.AnalyticsActivity;
Chiao Cheng94b10b52012-08-17 16:59:12 -070067
68import java.util.List;
69
70/**
71 * Displays the details of a specific call log entry.
72 * <p>
73 * This activity can be either started with the URI of a single call log entry, or with the
74 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
75 */
Sai Cheemalapati7fa029f2014-07-29 15:03:55 -070076public class CallDetailActivity extends AnalyticsActivity implements ProximitySensorAware {
Chiao Cheng94b10b52012-08-17 16:59:12 -070077 private static final String TAG = "CallDetail";
78
Yorke Lee03459442013-10-30 18:29:32 -070079 private static final int LOADER_ID = 0;
80 private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
81
Chiao Cheng35071c02012-10-15 18:36:24 -070082 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
83 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
84
Chiao Cheng94b10b52012-08-17 16:59:12 -070085 /** The time to wait before enabling the blank the screen due to the proximity sensor. */
86 private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
87 /** The time to wait before disabling the blank the screen due to the proximity sensor. */
88 private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
89
90 /** The enumeration of {@link AsyncTask} objects used in this class. */
91 public enum Tasks {
92 MARK_VOICEMAIL_READ,
93 DELETE_VOICEMAIL_AND_FINISH,
94 REMOVE_FROM_CALL_LOG_AND_FINISH,
95 UPDATE_PHONE_CALL_DETAILS,
96 }
97
98 /** A long array extra containing ids of call log entries to display. */
99 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
100 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
101 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
102 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
103 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
104 /** If the activity was triggered from a notification. */
105 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
106
Yorke Lee8cd94232014-07-23 14:05:31 -0700107 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
108
Chiao Cheng94b10b52012-08-17 16:59:12 -0700109 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800110 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700111 private QuickContactBadge mQuickContactBadge;
112 private TextView mCallerName;
113 private TextView mCallerNumber;
Nancy Chen19702632014-07-25 13:23:16 -0700114 private TextView mAccountLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 private AsyncTaskExecutor mAsyncTaskExecutor;
116 private ContactInfoHelper mContactInfoHelper;
117
118 private String mNumber = null;
119 private String mDefaultCountryIso;
120
121 /* package */ LayoutInflater mInflater;
122 /* package */ Resources mResources;
123 /** Helper to load contact photos. */
124 private ContactPhotoManager mContactPhotoManager;
125 /** Helper to make async queries to content resolver. */
126 private CallDetailActivityQueryHandler mAsyncQueryHandler;
127 /** Helper to get voicemail status messages. */
128 private VoicemailStatusHelper mVoicemailStatusHelper;
129 // Views related to voicemail status message.
130 private View mStatusMessageView;
131 private TextView mStatusMessageText;
132 private TextView mStatusMessageAction;
Yorke Lee8cd94232014-07-23 14:05:31 -0700133 private TextView mVoicemailTranscription;
Yorke Lee257b6f22014-07-31 14:16:44 -0700134 private LinearLayout mVoicemailHeader;
135
136 private Uri mVoicemailUri;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137
138 /** Whether we should show "edit number before call" in the options menu. */
139 private boolean mHasEditNumberBeforeCallOption;
140 /** Whether we should show "trash" in the options menu. */
141 private boolean mHasTrashOption;
142 /** Whether we should show "remove from call log" in the options menu. */
143 private boolean mHasRemoveFromCallLogOption;
144
145 private ProximitySensorManager mProximitySensorManager;
146 private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
147
Chiao Cheng94b10b52012-08-17 16:59:12 -0700148 /** Listener to changes in the proximity sensor state. */
149 private class ProximitySensorListener implements ProximitySensorManager.Listener {
150 /** Used to show a blank view and hide the action bar. */
151 private final Runnable mBlankRunnable = new Runnable() {
152 @Override
153 public void run() {
154 View blankView = findViewById(R.id.blank);
155 blankView.setVisibility(View.VISIBLE);
156 getActionBar().hide();
157 }
158 };
159 /** Used to remove the blank view and show the action bar. */
160 private final Runnable mUnblankRunnable = new Runnable() {
161 @Override
162 public void run() {
163 View blankView = findViewById(R.id.blank);
164 blankView.setVisibility(View.GONE);
165 getActionBar().show();
166 }
167 };
168
169 @Override
170 public synchronized void onNear() {
171 clearPendingRequests();
172 postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
173 }
174
175 @Override
176 public synchronized void onFar() {
177 clearPendingRequests();
178 postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
179 }
180
181 /** Removed any delayed requests that may be pending. */
182 public synchronized void clearPendingRequests() {
183 View blankView = findViewById(R.id.blank);
184 blankView.removeCallbacks(mBlankRunnable);
185 blankView.removeCallbacks(mUnblankRunnable);
186 }
187
188 /** Post a {@link Runnable} with a delay on the main thread. */
189 private synchronized void postDelayed(Runnable runnable, long delayMillis) {
190 // Post these instead of executing immediately so that:
191 // - They are guaranteed to be executed on the main thread.
192 // - If the sensor values changes rapidly for some time, the UI will not be
193 // updated immediately.
194 View blankView = findViewById(R.id.blank);
195 blankView.postDelayed(runnable, delayMillis);
196 }
197 }
198
199 static final String[] CALL_LOG_PROJECTION = new String[] {
200 CallLog.Calls.DATE,
201 CallLog.Calls.DURATION,
202 CallLog.Calls.NUMBER,
203 CallLog.Calls.TYPE,
204 CallLog.Calls.COUNTRY_ISO,
205 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700206 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700207 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
208 CallLog.Calls.PHONE_ACCOUNT_ID,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700209 CallLog.Calls.FEATURES,
Yorke Lee8cd94232014-07-23 14:05:31 -0700210 CallLog.Calls.DATA_USAGE,
211 CallLog.Calls.TRANSCRIPTION
Chiao Cheng94b10b52012-08-17 16:59:12 -0700212 };
213
214 static final int DATE_COLUMN_INDEX = 0;
215 static final int DURATION_COLUMN_INDEX = 1;
216 static final int NUMBER_COLUMN_INDEX = 2;
217 static final int CALL_TYPE_COLUMN_INDEX = 3;
218 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
219 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700220 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700221 static final int ACCOUNT_COMPONENT_NAME = 7;
222 static final int ACCOUNT_ID = 8;
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700223 static final int FEATURES = 9;
224 static final int DATA_USAGE = 10;
Yorke Lee8cd94232014-07-23 14:05:31 -0700225 static final int TRANSCRIPTION_COLUMN_INDEX = 11;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700226
Chiao Cheng94b10b52012-08-17 16:59:12 -0700227 @Override
228 protected void onCreate(Bundle icicle) {
229 super.onCreate(icicle);
230
231 setContentView(R.layout.call_detail);
232
233 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
234 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
235 mResources = getResources();
236
237 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee24ec3192013-11-19 13:52:45 -0800238 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
240 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Yorke Lee257b6f22014-07-31 14:16:44 -0700241
242 mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
243
Tyler Gunn18164c82014-06-09 10:20:57 -0700244 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
245 mQuickContactBadge.setOverlay(null);
246 mCallerName = (TextView) findViewById(R.id.caller_name);
247 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700248 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700249 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700250 mContactPhotoManager = ContactPhotoManager.getInstance(this);
251 mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
Chiao Cheng35071c02012-10-15 18:36:24 -0700252 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700253 getActionBar().setDisplayHomeAsUpEnabled(true);
Yorke Lee257b6f22014-07-31 14:16:44 -0700254
Chiao Cheng94b10b52012-08-17 16:59:12 -0700255 optionallyHandleVoicemail();
256 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
257 closeSystemDialogs();
258 }
259 }
260
261 @Override
262 public void onResume() {
263 super.onResume();
264 updateData(getCallLogEntryUris());
265 }
266
267 /**
268 * Handle voicemail playback or hide voicemail ui.
269 * <p>
270 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
Yorke Lee257b6f22014-07-31 14:16:44 -0700271 * playback. If it doesn't, then don't inflate the voicemail ui.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 */
273 private void optionallyHandleVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700274
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275 if (hasVoicemail()) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700276 LayoutInflater inflater =
277 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
278 mVoicemailHeader =
279 (LinearLayout) inflater.inflate(R.layout.call_details_voicemail_header, null);
280 View voicemailContainer = mVoicemailHeader.findViewById(R.id.voicemail_container);
281 mStatusMessageView = mVoicemailHeader.findViewById(R.id.voicemail_status);
282 mStatusMessageText =
283 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_message);
284 mStatusMessageAction =
285 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_action);
286 mVoicemailTranscription = (
287 TextView) mVoicemailHeader.findViewById(R.id.voicemail_transcription);
288 ListView historyList = (ListView) findViewById(R.id.history);
289 historyList.addHeaderView(mVoicemailHeader);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
291 // to play and optionally start the playback.
292 // Do a query to fetch the voicemail status messages.
Yorke Lee8cd94232014-07-23 14:05:31 -0700293 VoicemailPlaybackFragment playbackFragment;
294
295 playbackFragment = (VoicemailPlaybackFragment) getFragmentManager().findFragmentByTag(
296 VOICEMAIL_FRAGMENT_TAG);
297
298 if (playbackFragment == null) {
299 playbackFragment = new VoicemailPlaybackFragment();
300 Bundle fragmentArguments = new Bundle();
Yorke Lee257b6f22014-07-31 14:16:44 -0700301 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, mVoicemailUri);
Yorke Lee8cd94232014-07-23 14:05:31 -0700302 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
303 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
304 }
305 playbackFragment.setArguments(fragmentArguments);
306 getFragmentManager().beginTransaction()
307 .add(R.id.voicemail_container, playbackFragment, VOICEMAIL_FRAGMENT_TAG)
308 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700309 }
Yorke Lee8cd94232014-07-23 14:05:31 -0700310
Chiao Cheng94b10b52012-08-17 16:59:12 -0700311 voicemailContainer.setVisibility(View.VISIBLE);
Yorke Lee257b6f22014-07-31 14:16:44 -0700312 mAsyncQueryHandler.startVoicemailStatusQuery(mVoicemailUri);
313 markVoicemailAsRead(mVoicemailUri);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700314 }
315 }
316
317 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700318 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 }
320
321 private void markVoicemailAsRead(final Uri voicemailUri) {
322 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
323 @Override
324 public Void doInBackground(Void... params) {
325 ContentValues values = new ContentValues();
326 values.put(Voicemails.IS_READ, true);
327 getContentResolver().update(voicemailUri, values,
328 Voicemails.IS_READ + " = 0", null);
329 return null;
330 }
331 });
332 }
333
334 /**
335 * Returns the list of URIs to show.
336 * <p>
337 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
338 * a list of ids in the call log added as an extra on the URI.
339 * <p>
340 * If both are available, the data on the intent takes precedence.
341 */
342 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700343 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700344 if (uri != null) {
345 // If there is a data on the intent, it takes precedence over the extra.
346 return new Uri[]{ uri };
347 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700348 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
349 final int numIds = ids == null ? 0 : ids.length;
350 final Uri[] uris = new Uri[numIds];
351 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700352 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
353 }
354 return uris;
355 }
356
357 @Override
358 public boolean onKeyDown(int keyCode, KeyEvent event) {
359 switch (keyCode) {
360 case KeyEvent.KEYCODE_CALL: {
361 // Make sure phone isn't already busy before starting direct call
362 TelephonyManager tm = (TelephonyManager)
363 getSystemService(Context.TELEPHONY_SERVICE);
364 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700365 DialerUtils.startActivityWithErrorToast(this,
Jay Shraunerae274c02014-09-06 10:09:24 -0700366 CallUtil.getCallIntent(Uri.fromParts(PhoneAccount.SCHEME_TEL, mNumber,
Nancy Chen87ba4892014-06-11 17:56:07 -0700367 null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700368 return true;
369 }
370 }
371 }
372
373 return super.onKeyDown(keyCode, event);
374 }
375
376 /**
377 * Update user interface with details of given call.
378 *
379 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
380 */
381 private void updateData(final Uri... callUris) {
382 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
383 @Override
384 public PhoneCallDetails[] doInBackground(Void... params) {
385 // TODO: All phone calls correspond to the same person, so we can make a single
386 // lookup.
387 final int numCalls = callUris.length;
388 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
389 try {
390 for (int index = 0; index < numCalls; ++index) {
391 details[index] = getPhoneCallDetailsForUri(callUris[index]);
392 }
393 return details;
394 } catch (IllegalArgumentException e) {
395 // Something went wrong reading in our primary data.
396 Log.w(TAG, "invalid URI starting call details", e);
397 return null;
398 }
399 }
400
401 @Override
402 public void onPostExecute(PhoneCallDetails[] details) {
403 if (details == null) {
404 // Somewhere went wrong: we're going to bail out and show error to users.
405 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
406 Toast.LENGTH_SHORT).show();
407 finish();
408 return;
409 }
410
411 // We know that all calls are from the same number and the same contact, so pick the
412 // first.
413 PhoneCallDetails firstDetails = details[0];
414 mNumber = firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700415 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700416 final Uri contactUri = firstDetails.contactUri;
417 final Uri photoUri = firstDetails.photoUri;
418
Chiao Cheng94b10b52012-08-17 16:59:12 -0700419 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700420 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700421 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
422 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
423 final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
424 final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700425
Tyler Gunn18164c82014-06-09 10:20:57 -0700426 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700427
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800428 final CharSequence displayNumber =
429 mPhoneNumberHelper.getDisplayNumber(
430 firstDetails.number,
431 firstDetails.numberPresentation,
432 firstDetails.formattedNumber);
433
Tyler Gunn18164c82014-06-09 10:20:57 -0700434 if (!TextUtils.isEmpty(firstDetails.name)) {
435 mCallerName.setText(firstDetails.name);
436 mCallerNumber.setText(callLocationOrType + " " + displayNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700437 } else {
Tyler Gunn18164c82014-06-09 10:20:57 -0700438 mCallerName.setText(displayNumber);
439 if (!TextUtils.isEmpty(callLocationOrType)) {
440 mCallerNumber.setText(callLocationOrType);
441 mCallerNumber.setVisibility(View.VISIBLE);
442 } else {
443 mCallerNumber.setVisibility(View.GONE);
444 }
445
Chiao Cheng94b10b52012-08-17 16:59:12 -0700446 }
447
Nancy Chen19702632014-07-25 13:23:16 -0700448 if (!TextUtils.isEmpty(firstDetails.accountLabel)) {
449 mAccountLabel.setText(firstDetails.accountLabel);
450 mAccountLabel.setVisibility(View.VISIBLE);
451 } else {
452 mAccountLabel.setVisibility(View.GONE);
453 }
454
Chiao Cheng94b10b52012-08-17 16:59:12 -0700455 mHasEditNumberBeforeCallOption =
456 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
457 mHasTrashOption = hasVoicemail();
458 mHasRemoveFromCallLogOption = !hasVoicemail();
459 invalidateOptionsMenu();
460
461 ListView historyList = (ListView) findViewById(R.id.history);
462 historyList.setAdapter(
463 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
Tyler Gunn18164c82014-06-09 10:20:57 -0700464 mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700465
Tyler Gunn18164c82014-06-09 10:20:57 -0700466 String lookupKey = contactUri == null ? null
467 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800468
469 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
470
471 final int contactType =
472 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
473 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
474 ContactPhotoManager.TYPE_DEFAULT;
475
Tyler Gunn18164c82014-06-09 10:20:57 -0700476 String nameForDefaultImage;
477 if (TextUtils.isEmpty(firstDetails.name)) {
478 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(firstDetails.number,
479 firstDetails.numberPresentation,
480 firstDetails.formattedNumber).toString();
481 } else {
482 nameForDefaultImage = firstDetails.name.toString();
483 }
484
Yorke Lee8cd94232014-07-23 14:05:31 -0700485 if (hasVoicemail() && !TextUtils.isEmpty(firstDetails.transcription)) {
486 mVoicemailTranscription.setText(firstDetails.transcription);
487 mVoicemailTranscription.setVisibility(View.VISIBLE);
Yorke Lee8cd94232014-07-23 14:05:31 -0700488 }
489
Tyler Gunn18164c82014-06-09 10:20:57 -0700490 loadContactPhotos(
491 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700492 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
493 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700494
495 /**
496 * Determines the location geocode text for a call, or the phone number type
497 * (if available).
498 *
499 * @param details The call details.
500 * @return The phone number type or location.
501 */
502 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
503 if (!TextUtils.isEmpty(details.name)) {
504 return Phone.getTypeLabel(mResources, details.numberType,
505 details.numberLabel);
506 } else {
507 return details.geocode;
508 }
509 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700510 }
511 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
512 }
513
514 /** Return the phone call details for a given call log URI. */
515 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
516 ContentResolver resolver = getContentResolver();
517 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
518 try {
519 if (callCursor == null || !callCursor.moveToFirst()) {
520 throw new IllegalArgumentException("Cannot find content: " + callUri);
521 }
522
523 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700524 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
525 final int numberPresentation = callCursor.getInt(
526 NUMBER_PRESENTATION_COLUMN_INDEX);
527 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
528 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
529 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700530 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
531 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Yorke Lee8cd94232014-07-23 14:05:31 -0700532 final String transcription = callCursor.getString(TRANSCRIPTION_COLUMN_INDEX);
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700533
Nancy Chen19702632014-07-25 13:23:16 -0700534 final String accountLabel = PhoneAccountUtils.getAccountLabel(this,
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700535 PhoneAccountUtils.getAccount(
536 callCursor.getString(ACCOUNT_COMPONENT_NAME),
537 callCursor.getString(ACCOUNT_ID)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700538
539 if (TextUtils.isEmpty(countryIso)) {
540 countryIso = mDefaultCountryIso;
541 }
542
543 // Formatted phone number.
544 final CharSequence formattedNumber;
545 // Read contact specifics.
546 final CharSequence nameText;
547 final int numberType;
548 final CharSequence numberLabel;
549 final Uri photoUri;
550 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800551 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700552 // If this is not a regular number, there is no point in looking it up in the contacts.
553 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700554 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Yorke Lee1a7c1962013-09-20 16:04:34 -0700555 && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700556 ? mContactInfoHelper.lookupNumber(number, countryIso)
557 : null;
558 if (info == null) {
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700559 formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
560 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 nameText = "";
562 numberType = 0;
563 numberLabel = "";
564 photoUri = null;
565 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800566 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700567 } else {
568 formattedNumber = info.formattedNumber;
569 nameText = info.name;
570 numberType = info.type;
571 numberLabel = info.label;
572 photoUri = info.photoUri;
573 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800574 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700575 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700576 final int features = callCursor.getInt(FEATURES);
577 Long dataUsage = null;
578 if (!callCursor.isNull(DATA_USAGE)) {
579 dataUsage = callCursor.getLong(DATA_USAGE);
580 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700581 return new PhoneCallDetails(number, numberPresentation,
582 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700583 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700584 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Nancy Chen19702632014-07-25 13:23:16 -0700585 accountLabel, null, features, dataUsage, transcription);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700586 } finally {
587 if (callCursor != null) {
588 callCursor.close();
589 }
590 }
591 }
592
593 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700594 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
595 String lookupKey, int contactType) {
596
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800597 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700598 contactType, true /* isCircular */);
599
600 mQuickContactBadge.assignContactUri(contactUri);
601 mQuickContactBadge.setContentDescription(
602 mResources.getString(R.string.description_contact_details, displayName));
603
604 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
605 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700606 }
607
608 static final class ViewEntry {
609 public final String text;
610 public final Intent primaryIntent;
611 /** The description for accessibility of the primary action. */
612 public final String primaryDescription;
613
614 public CharSequence label = null;
615 /** Icon for the secondary action. */
616 public int secondaryIcon = 0;
617 /** Intent for the secondary action. If not null, an icon must be defined. */
618 public Intent secondaryIntent = null;
619 /** The description for accessibility of the secondary action. */
620 public String secondaryDescription = null;
621
622 public ViewEntry(String text, Intent intent, String description) {
623 this.text = text;
624 primaryIntent = intent;
625 primaryDescription = description;
626 }
627
628 public void setSecondaryAction(int icon, Intent intent, String description) {
629 secondaryIcon = icon;
630 secondaryIntent = intent;
631 secondaryDescription = description;
632 }
633 }
634
Chiao Cheng94b10b52012-08-17 16:59:12 -0700635 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
636 if (statusCursor == null) {
637 mStatusMessageView.setVisibility(View.GONE);
638 return;
639 }
640 final StatusMessage message = getStatusMessage(statusCursor);
641 if (message == null || !message.showInCallDetails()) {
642 mStatusMessageView.setVisibility(View.GONE);
643 return;
644 }
645
646 mStatusMessageView.setVisibility(View.VISIBLE);
647 mStatusMessageText.setText(message.callDetailsMessageId);
648 if (message.actionMessageId != -1) {
649 mStatusMessageAction.setText(message.actionMessageId);
650 }
651 if (message.actionUri != null) {
652 mStatusMessageAction.setClickable(true);
653 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
654 @Override
655 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700656 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
657 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700658 }
659 });
660 } else {
661 mStatusMessageAction.setClickable(false);
662 }
663 }
664
665 private StatusMessage getStatusMessage(Cursor statusCursor) {
666 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
667 if (messages.size() == 0) {
668 return null;
669 }
670 // There can only be a single status message per source package, so num of messages can
671 // at most be 1.
672 if (messages.size() > 1) {
673 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
674 " Will use the first one.", messages.size()));
675 }
676 return messages.get(0);
677 }
678
679 @Override
680 public boolean onCreateOptionsMenu(Menu menu) {
681 getMenuInflater().inflate(R.menu.call_details_options, menu);
682 return super.onCreateOptionsMenu(menu);
683 }
684
685 @Override
686 public boolean onPrepareOptionsMenu(Menu menu) {
687 // This action deletes all elements in the group from the call log.
688 // We don't have this action for voicemails, because you can just use the trash button.
689 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
690 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
691 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
692 return super.onPrepareOptionsMenu(menu);
693 }
694
Chiao Cheng94b10b52012-08-17 16:59:12 -0700695 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
696 final StringBuilder callIds = new StringBuilder();
697 for (Uri callUri : getCallLogEntryUris()) {
698 if (callIds.length() != 0) {
699 callIds.append(",");
700 }
701 callIds.append(ContentUris.parseId(callUri));
702 }
703 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
704 new AsyncTask<Void, Void, Void>() {
705 @Override
706 public Void doInBackground(Void... params) {
707 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
708 Calls._ID + " IN (" + callIds + ")", null);
709 return null;
710 }
711
712 @Override
713 public void onPostExecute(Void result) {
714 finish();
715 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700716 }
717 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 }
719
720 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700721 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700722 }
723
724 public void onMenuTrashVoicemail(MenuItem menuItem) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700725 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
726 new AsyncTask<Void, Void, Void>() {
727 @Override
728 public Void doInBackground(Void... params) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700729 getContentResolver().delete(mVoicemailUri, null, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 return null;
731 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700732
Chiao Cheng94b10b52012-08-17 16:59:12 -0700733 @Override
734 public void onPostExecute(Void result) {
735 finish();
736 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700737 }
738 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739 }
740
741 @Override
742 protected void onPause() {
743 // Immediately stop the proximity sensor.
744 disableProximitySensor(false);
745 mProximitySensorListener.clearPendingRequests();
746 super.onPause();
747 }
748
749 @Override
750 public void enableProximitySensor() {
751 mProximitySensorManager.enable();
752 }
753
754 @Override
755 public void disableProximitySensor(boolean waitForFarState) {
756 mProximitySensorManager.disable(waitForFarState);
757 }
758
Chiao Cheng94b10b52012-08-17 16:59:12 -0700759 private void closeSystemDialogs() {
760 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
761 }
762
Chiao Cheng35071c02012-10-15 18:36:24 -0700763 /** Returns the given text, forced to be left-to-right. */
764 private static CharSequence forceLeftToRight(CharSequence text) {
765 StringBuilder sb = new StringBuilder();
766 sb.append(LEFT_TO_RIGHT_EMBEDDING);
767 sb.append(text);
768 sb.append(POP_DIRECTIONAL_FORMATTING);
769 return sb.toString();
770 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771}