blob: 735d6eb565378b16123fcb9edc266942e9613b73 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Brian Attwellbeab3bb2014-10-27 12:24:49 -070019import android.app.Activity;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.content.ContentResolver;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.Intent;
25import android.content.res.Resources;
26import android.database.Cursor;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.net.Uri;
28import android.os.AsyncTask;
29import android.os.Bundle;
Yorke Lee71513982015-02-25 20:23:53 -080030import android.os.PowerManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.provider.CallLog;
32import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.provider.VoicemailContract.Voicemails;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070035import android.telecom.PhoneAccount;
Nancy Chene80d6222014-10-08 20:17:55 -070036import android.telecom.PhoneAccountHandle;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.telephony.TelephonyManager;
Andrew Lee99e8adb2014-10-02 12:27:37 -070038import android.text.BidiFormatter;
39import android.text.TextDirectionHeuristics;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import 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;
Yorke Lee257b6f22014-07-31 14:16:44 -070047import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.widget.ListView;
Tyler Gunn18164c82014-06-09 10:20:57 -070049import android.widget.QuickContactBadge;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.widget.TextView;
51import android.widget.Toast;
52
Chiao Cheng35071c02012-10-15 18:36:24 -070053import com.android.contacts.common.ContactPhotoManager;
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;
Brian Attwell82043242015-02-03 14:13:58 -080056import com.android.contacts.common.CallUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import com.android.dialer.calllog.CallDetailHistoryAdapter;
Nancy Chenf153bf42015-05-06 19:29:56 -070058import com.android.dialer.calllog.CallLogNotificationsService;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059import com.android.dialer.calllog.CallTypeHelper;
60import com.android.dialer.calllog.ContactInfo;
61import com.android.dialer.calllog.ContactInfoHelper;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070062import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee24ec3192013-11-19 13:52:45 -080063import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070064import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng91197042012-08-24 14:19:37 -070065import com.android.dialer.util.AsyncTaskExecutor;
66import com.android.dialer.util.AsyncTaskExecutors;
Andrew Lee247df6e2015-05-12 19:09:00 -070067import com.android.dialer.util.IntentUtil;
Yorke Lee39213592014-04-07 15:39:48 -070068import com.android.dialer.util.DialerUtils;
Yorke Lee4aece952015-05-02 22:22:54 -070069import com.android.dialer.util.TelecomUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070070import com.android.dialer.voicemail.VoicemailPlaybackFragment;
71import com.android.dialer.voicemail.VoicemailStatusHelper;
72import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
73import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
74
75import java.util.List;
76
77/**
78 * Displays the details of a specific call log entry.
79 * <p>
80 * This activity can be either started with the URI of a single call log entry, or with the
81 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
82 */
Brian Attwellbeab3bb2014-10-27 12:24:49 -070083public class CallDetailActivity extends Activity implements ProximitySensorAware {
Chiao Cheng94b10b52012-08-17 16:59:12 -070084 private static final String TAG = "CallDetail";
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 enumeration of {@link AsyncTask} objects used in this class. */
90 public enum Tasks {
91 MARK_VOICEMAIL_READ,
92 DELETE_VOICEMAIL_AND_FINISH,
93 REMOVE_FROM_CALL_LOG_AND_FINISH,
94 UPDATE_PHONE_CALL_DETAILS,
95 }
96
97 /** A long array extra containing ids of call log entries to display. */
98 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
99 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
100 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
101 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
102 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
103 /** If the activity was triggered from a notification. */
104 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
105
Yorke Lee8cd94232014-07-23 14:05:31 -0700106 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
107
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800109 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700110 private QuickContactBadge mQuickContactBadge;
111 private TextView mCallerName;
112 private TextView mCallerNumber;
Nancy Chen19702632014-07-25 13:23:16 -0700113 private TextView mAccountLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114 private AsyncTaskExecutor mAsyncTaskExecutor;
115 private ContactInfoHelper mContactInfoHelper;
116
117 private String mNumber = null;
118 private String mDefaultCountryIso;
119
120 /* package */ LayoutInflater mInflater;
121 /* package */ Resources mResources;
122 /** Helper to load contact photos. */
123 private ContactPhotoManager mContactPhotoManager;
124 /** Helper to make async queries to content resolver. */
125 private CallDetailActivityQueryHandler mAsyncQueryHandler;
126 /** Helper to get voicemail status messages. */
127 private VoicemailStatusHelper mVoicemailStatusHelper;
128 // Views related to voicemail status message.
129 private View mStatusMessageView;
130 private TextView mStatusMessageText;
131 private TextView mStatusMessageAction;
Yorke Lee8cd94232014-07-23 14:05:31 -0700132 private TextView mVoicemailTranscription;
Yorke Lee257b6f22014-07-31 14:16:44 -0700133 private LinearLayout mVoicemailHeader;
134
135 private Uri mVoicemailUri;
Andrew Lee99e8adb2014-10-02 12:27:37 -0700136 private BidiFormatter mBidiFormatter = BidiFormatter.getInstance();
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
Yorke Lee71513982015-02-25 20:23:53 -0800145 private PowerManager.WakeLock mProximityWakeLock;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700146
147 static final String[] CALL_LOG_PROJECTION = new String[] {
148 CallLog.Calls.DATE,
149 CallLog.Calls.DURATION,
150 CallLog.Calls.NUMBER,
151 CallLog.Calls.TYPE,
152 CallLog.Calls.COUNTRY_ISO,
153 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700154 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700155 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
156 CallLog.Calls.PHONE_ACCOUNT_ID,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700157 CallLog.Calls.FEATURES,
Yorke Lee8cd94232014-07-23 14:05:31 -0700158 CallLog.Calls.DATA_USAGE,
159 CallLog.Calls.TRANSCRIPTION
Chiao Cheng94b10b52012-08-17 16:59:12 -0700160 };
161
162 static final int DATE_COLUMN_INDEX = 0;
163 static final int DURATION_COLUMN_INDEX = 1;
164 static final int NUMBER_COLUMN_INDEX = 2;
165 static final int CALL_TYPE_COLUMN_INDEX = 3;
166 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
167 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700168 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700169 static final int ACCOUNT_COMPONENT_NAME = 7;
170 static final int ACCOUNT_ID = 8;
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700171 static final int FEATURES = 9;
172 static final int DATA_USAGE = 10;
Yorke Lee8cd94232014-07-23 14:05:31 -0700173 static final int TRANSCRIPTION_COLUMN_INDEX = 11;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700174
Chiao Cheng94b10b52012-08-17 16:59:12 -0700175 @Override
176 protected void onCreate(Bundle icicle) {
177 super.onCreate(icicle);
178
179 setContentView(R.layout.call_detail);
180
181 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
182 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
183 mResources = getResources();
184
185 mCallTypeHelper = new CallTypeHelper(getResources());
Nancy Chene80d6222014-10-08 20:17:55 -0700186 mPhoneNumberHelper = new PhoneNumberDisplayHelper(this, mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700187 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
188 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Yorke Lee257b6f22014-07-31 14:16:44 -0700189
190 mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
191
Tyler Gunn18164c82014-06-09 10:20:57 -0700192 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
193 mQuickContactBadge.setOverlay(null);
Brian Attwell74eb4ec2015-02-25 22:31:22 -0800194 mQuickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE);
Tyler Gunn18164c82014-06-09 10:20:57 -0700195 mCallerName = (TextView) findViewById(R.id.caller_name);
196 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700197 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700198 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700199 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Yorke Lee71513982015-02-25 20:23:53 -0800200 final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
201 if (powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
202 mProximityWakeLock = powerManager.newWakeLock(
203 PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
204 } else {
205 mProximityWakeLock = null;
206 }
207
Chiao Cheng35071c02012-10-15 18:36:24 -0700208 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700209 getActionBar().setDisplayHomeAsUpEnabled(true);
Yorke Lee257b6f22014-07-31 14:16:44 -0700210
Chiao Cheng94b10b52012-08-17 16:59:12 -0700211 optionallyHandleVoicemail();
212 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
213 closeSystemDialogs();
214 }
215 }
216
217 @Override
218 public void onResume() {
219 super.onResume();
220 updateData(getCallLogEntryUris());
221 }
222
223 /**
224 * Handle voicemail playback or hide voicemail ui.
225 * <p>
226 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
Yorke Lee257b6f22014-07-31 14:16:44 -0700227 * playback. If it doesn't, then don't inflate the voicemail ui.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700228 */
229 private void optionallyHandleVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700230
Chiao Cheng94b10b52012-08-17 16:59:12 -0700231 if (hasVoicemail()) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700232 LayoutInflater inflater =
233 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
234 mVoicemailHeader =
235 (LinearLayout) inflater.inflate(R.layout.call_details_voicemail_header, null);
236 View voicemailContainer = mVoicemailHeader.findViewById(R.id.voicemail_container);
237 mStatusMessageView = mVoicemailHeader.findViewById(R.id.voicemail_status);
238 mStatusMessageText =
239 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_message);
240 mStatusMessageAction =
241 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_action);
242 mVoicemailTranscription = (
243 TextView) mVoicemailHeader.findViewById(R.id.voicemail_transcription);
244 ListView historyList = (ListView) findViewById(R.id.history);
245 historyList.addHeaderView(mVoicemailHeader);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700246 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
247 // to play and optionally start the playback.
248 // Do a query to fetch the voicemail status messages.
Yorke Lee8cd94232014-07-23 14:05:31 -0700249 VoicemailPlaybackFragment playbackFragment;
250
251 playbackFragment = (VoicemailPlaybackFragment) getFragmentManager().findFragmentByTag(
252 VOICEMAIL_FRAGMENT_TAG);
253
254 if (playbackFragment == null) {
255 playbackFragment = new VoicemailPlaybackFragment();
256 Bundle fragmentArguments = new Bundle();
Yorke Lee257b6f22014-07-31 14:16:44 -0700257 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, mVoicemailUri);
Yorke Lee8cd94232014-07-23 14:05:31 -0700258 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
259 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
260 }
261 playbackFragment.setArguments(fragmentArguments);
262 getFragmentManager().beginTransaction()
263 .add(R.id.voicemail_container, playbackFragment, VOICEMAIL_FRAGMENT_TAG)
264 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700265 }
Yorke Lee8cd94232014-07-23 14:05:31 -0700266
Chiao Cheng94b10b52012-08-17 16:59:12 -0700267 voicemailContainer.setVisibility(View.VISIBLE);
Yorke Lee257b6f22014-07-31 14:16:44 -0700268 mAsyncQueryHandler.startVoicemailStatusQuery(mVoicemailUri);
269 markVoicemailAsRead(mVoicemailUri);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700270 }
271 }
272
273 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700274 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275 }
276
277 private void markVoicemailAsRead(final Uri voicemailUri) {
278 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
279 @Override
280 public Void doInBackground(Void... params) {
281 ContentValues values = new ContentValues();
282 values.put(Voicemails.IS_READ, true);
283 getContentResolver().update(voicemailUri, values,
284 Voicemails.IS_READ + " = 0", null);
Nancy Chenf153bf42015-05-06 19:29:56 -0700285 Intent intent = new Intent(getBaseContext(), CallLogNotificationsService.class);
286 intent.setAction(CallLogNotificationsService.ACTION_MARK_NEW_VOICEMAILS_AS_OLD);
287 getBaseContext().startService(intent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700288 return null;
289 }
290 });
291 }
292
293 /**
294 * Returns the list of URIs to show.
295 * <p>
296 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
297 * a list of ids in the call log added as an extra on the URI.
298 * <p>
299 * If both are available, the data on the intent takes precedence.
300 */
301 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700302 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700303 if (uri != null) {
304 // If there is a data on the intent, it takes precedence over the extra.
305 return new Uri[]{ uri };
306 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700307 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
308 final int numIds = ids == null ? 0 : ids.length;
309 final Uri[] uris = new Uri[numIds];
310 for (int index = 0; index < numIds; ++index) {
Yorke Lee4aece952015-05-02 22:22:54 -0700311 uris[index] = ContentUris.withAppendedId(
312 TelecomUtil.getCallLogUri(CallDetailActivity.this), ids[index]);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700313 }
314 return uris;
315 }
316
317 @Override
318 public boolean onKeyDown(int keyCode, KeyEvent event) {
319 switch (keyCode) {
320 case KeyEvent.KEYCODE_CALL: {
321 // Make sure phone isn't already busy before starting direct call
322 TelephonyManager tm = (TelephonyManager)
323 getSystemService(Context.TELEPHONY_SERVICE);
324 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700325 DialerUtils.startActivityWithErrorToast(this,
Andrew Lee247df6e2015-05-12 19:09:00 -0700326 IntentUtil.getCallIntent(
327 Uri.fromParts(PhoneAccount.SCHEME_TEL, mNumber, null)),
328 R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700329 return true;
330 }
331 }
332 }
333
334 return super.onKeyDown(keyCode, event);
335 }
336
337 /**
338 * Update user interface with details of given call.
339 *
Nancy Chene80d6222014-10-08 20:17:55 -0700340 * @param callUris URIs into {@link android.provider.CallLog.Calls} of the calls to be displayed
Chiao Cheng94b10b52012-08-17 16:59:12 -0700341 */
342 private void updateData(final Uri... callUris) {
343 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
344 @Override
345 public PhoneCallDetails[] doInBackground(Void... params) {
346 // TODO: All phone calls correspond to the same person, so we can make a single
347 // lookup.
348 final int numCalls = callUris.length;
349 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
350 try {
351 for (int index = 0; index < numCalls; ++index) {
352 details[index] = getPhoneCallDetailsForUri(callUris[index]);
353 }
354 return details;
355 } catch (IllegalArgumentException e) {
356 // Something went wrong reading in our primary data.
357 Log.w(TAG, "invalid URI starting call details", e);
358 return null;
359 }
360 }
361
362 @Override
363 public void onPostExecute(PhoneCallDetails[] details) {
Nancy Chene80d6222014-10-08 20:17:55 -0700364 Context context = CallDetailActivity.this;
365
Chiao Cheng94b10b52012-08-17 16:59:12 -0700366 if (details == null) {
367 // Somewhere went wrong: we're going to bail out and show error to users.
Nancy Chene80d6222014-10-08 20:17:55 -0700368 Toast.makeText(context, R.string.toast_call_detail_error,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700369 Toast.LENGTH_SHORT).show();
370 finish();
371 return;
372 }
373
374 // We know that all calls are from the same number and the same contact, so pick the
375 // first.
376 PhoneCallDetails firstDetails = details[0];
Nancy Chen164d9d52015-03-09 16:24:08 -0700377 mNumber = TextUtils.isEmpty(firstDetails.number) ?
378 null : firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700379 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700380 final Uri contactUri = firstDetails.contactUri;
381 final Uri photoUri = firstDetails.photoUri;
Nancy Chene80d6222014-10-08 20:17:55 -0700382 final PhoneAccountHandle accountHandle = firstDetails.accountHandle;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700383
Chiao Cheng94b10b52012-08-17 16:59:12 -0700384 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700385 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700386 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
Nancy Chene80d6222014-10-08 20:17:55 -0700387 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper(context);
388 final boolean isVoicemailNumber =
389 phoneUtils.isVoicemailNumber(accountHandle, mNumber);
390 final boolean isSipNumber = PhoneNumberUtilsWrapper.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700391
Tyler Gunn18164c82014-06-09 10:20:57 -0700392 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700393
Nancy Chene80d6222014-10-08 20:17:55 -0700394 final CharSequence displayNumber =
395 mPhoneNumberHelper.getDisplayNumber(
396 firstDetails.accountHandle,
397 firstDetails.number,
398 firstDetails.numberPresentation,
399 firstDetails.formattedNumber);
Andrew Lee99e8adb2014-10-02 12:27:37 -0700400 final String displayNumberStr = mBidiFormatter.unicodeWrap(
401 displayNumber.toString(), TextDirectionHeuristics.LTR);
402
Tyler Gunn18164c82014-06-09 10:20:57 -0700403 if (!TextUtils.isEmpty(firstDetails.name)) {
404 mCallerName.setText(firstDetails.name);
Andrew Lee99e8adb2014-10-02 12:27:37 -0700405 mCallerNumber.setText(callLocationOrType + " " + displayNumberStr);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700406 } else {
Andrew Lee99e8adb2014-10-02 12:27:37 -0700407 mCallerName.setText(displayNumberStr);
Tyler Gunn18164c82014-06-09 10:20:57 -0700408 if (!TextUtils.isEmpty(callLocationOrType)) {
409 mCallerNumber.setText(callLocationOrType);
410 mCallerNumber.setVisibility(View.VISIBLE);
411 } else {
412 mCallerNumber.setVisibility(View.GONE);
413 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700414 }
415
Nancy Chene80d6222014-10-08 20:17:55 -0700416 String accountLabel = PhoneAccountUtils.getAccountLabel(context, accountHandle);
417 if (!TextUtils.isEmpty(accountLabel)) {
418 mAccountLabel.setText(accountLabel);
Nancy Chen19702632014-07-25 13:23:16 -0700419 mAccountLabel.setVisibility(View.VISIBLE);
420 } else {
421 mAccountLabel.setVisibility(View.GONE);
422 }
423
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424 mHasEditNumberBeforeCallOption =
425 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
426 mHasTrashOption = hasVoicemail();
427 mHasRemoveFromCallLogOption = !hasVoicemail();
428 invalidateOptionsMenu();
429
430 ListView historyList = (ListView) findViewById(R.id.history);
431 historyList.setAdapter(
Nancy Chene80d6222014-10-08 20:17:55 -0700432 new CallDetailHistoryAdapter(context, mInflater, mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700433
Tyler Gunn18164c82014-06-09 10:20:57 -0700434 String lookupKey = contactUri == null ? null
435 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800436
437 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
438
439 final int contactType =
440 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
441 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
442 ContactPhotoManager.TYPE_DEFAULT;
443
Tyler Gunn18164c82014-06-09 10:20:57 -0700444 String nameForDefaultImage;
445 if (TextUtils.isEmpty(firstDetails.name)) {
Nancy Chene80d6222014-10-08 20:17:55 -0700446 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(
447 firstDetails.accountHandle,
448 firstDetails.number,
Tyler Gunn18164c82014-06-09 10:20:57 -0700449 firstDetails.numberPresentation,
450 firstDetails.formattedNumber).toString();
451 } else {
452 nameForDefaultImage = firstDetails.name.toString();
453 }
454
Yorke Lee8cd94232014-07-23 14:05:31 -0700455 if (hasVoicemail() && !TextUtils.isEmpty(firstDetails.transcription)) {
456 mVoicemailTranscription.setText(firstDetails.transcription);
457 mVoicemailTranscription.setVisibility(View.VISIBLE);
Yorke Lee8cd94232014-07-23 14:05:31 -0700458 }
459
Tyler Gunn18164c82014-06-09 10:20:57 -0700460 loadContactPhotos(
461 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700462 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
463 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700464
465 /**
466 * Determines the location geocode text for a call, or the phone number type
467 * (if available).
468 *
469 * @param details The call details.
470 * @return The phone number type or location.
471 */
472 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
473 if (!TextUtils.isEmpty(details.name)) {
474 return Phone.getTypeLabel(mResources, details.numberType,
475 details.numberLabel);
476 } else {
477 return details.geocode;
478 }
479 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700480 }
481 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
482 }
483
484 /** Return the phone call details for a given call log URI. */
485 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
486 ContentResolver resolver = getContentResolver();
487 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
488 try {
489 if (callCursor == null || !callCursor.moveToFirst()) {
490 throw new IllegalArgumentException("Cannot find content: " + callUri);
491 }
492
493 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700494 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
495 final int numberPresentation = callCursor.getInt(
496 NUMBER_PRESENTATION_COLUMN_INDEX);
497 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
498 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
499 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700500 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
501 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Yorke Lee8cd94232014-07-23 14:05:31 -0700502 final String transcription = callCursor.getString(TRANSCRIPTION_COLUMN_INDEX);
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700503
Nancy Chene80d6222014-10-08 20:17:55 -0700504 final PhoneAccountHandle accountHandle = PhoneAccountUtils.getAccount(
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700505 callCursor.getString(ACCOUNT_COMPONENT_NAME),
Nancy Chene80d6222014-10-08 20:17:55 -0700506 callCursor.getString(ACCOUNT_ID));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700507
508 if (TextUtils.isEmpty(countryIso)) {
509 countryIso = mDefaultCountryIso;
510 }
511
512 // Formatted phone number.
513 final CharSequence formattedNumber;
514 // Read contact specifics.
515 final CharSequence nameText;
516 final int numberType;
517 final CharSequence numberLabel;
518 final Uri photoUri;
519 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800520 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700521 // If this is not a regular number, there is no point in looking it up in the contacts.
522 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700523 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Nancy Chene80d6222014-10-08 20:17:55 -0700524 && !new PhoneNumberUtilsWrapper(this).isVoicemailNumber(accountHandle, number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700525 ? mContactInfoHelper.lookupNumber(number, countryIso)
526 : null;
527 if (info == null) {
Nancy Chene80d6222014-10-08 20:17:55 -0700528 formattedNumber = mPhoneNumberHelper.getDisplayNumber(accountHandle, number,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700529 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700530 nameText = "";
531 numberType = 0;
532 numberLabel = "";
533 photoUri = null;
534 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800535 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700536 } else {
537 formattedNumber = info.formattedNumber;
538 nameText = info.name;
539 numberType = info.type;
540 numberLabel = info.label;
541 photoUri = info.photoUri;
542 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800543 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700544 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700545 final int features = callCursor.getInt(FEATURES);
546 Long dataUsage = null;
547 if (!callCursor.isNull(DATA_USAGE)) {
548 dataUsage = callCursor.getLong(DATA_USAGE);
549 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700550 return new PhoneCallDetails(number, numberPresentation,
551 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700552 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700553 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Nancy Chene80d6222014-10-08 20:17:55 -0700554 accountHandle, features, dataUsage, transcription);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700555 } finally {
556 if (callCursor != null) {
557 callCursor.close();
558 }
559 }
560 }
561
562 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700563 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
564 String lookupKey, int contactType) {
565
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800566 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700567 contactType, true /* isCircular */);
568
569 mQuickContactBadge.assignContactUri(contactUri);
570 mQuickContactBadge.setContentDescription(
571 mResources.getString(R.string.description_contact_details, displayName));
572
573 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
574 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700575 }
576
577 static final class ViewEntry {
578 public final String text;
579 public final Intent primaryIntent;
580 /** The description for accessibility of the primary action. */
581 public final String primaryDescription;
582
583 public CharSequence label = null;
584 /** Icon for the secondary action. */
585 public int secondaryIcon = 0;
586 /** Intent for the secondary action. If not null, an icon must be defined. */
587 public Intent secondaryIntent = null;
588 /** The description for accessibility of the secondary action. */
589 public String secondaryDescription = null;
590
591 public ViewEntry(String text, Intent intent, String description) {
592 this.text = text;
593 primaryIntent = intent;
594 primaryDescription = description;
595 }
596
597 public void setSecondaryAction(int icon, Intent intent, String description) {
598 secondaryIcon = icon;
599 secondaryIntent = intent;
600 secondaryDescription = description;
601 }
602 }
603
Chiao Cheng94b10b52012-08-17 16:59:12 -0700604 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
605 if (statusCursor == null) {
606 mStatusMessageView.setVisibility(View.GONE);
607 return;
608 }
609 final StatusMessage message = getStatusMessage(statusCursor);
610 if (message == null || !message.showInCallDetails()) {
611 mStatusMessageView.setVisibility(View.GONE);
612 return;
613 }
614
615 mStatusMessageView.setVisibility(View.VISIBLE);
616 mStatusMessageText.setText(message.callDetailsMessageId);
617 if (message.actionMessageId != -1) {
618 mStatusMessageAction.setText(message.actionMessageId);
619 }
620 if (message.actionUri != null) {
621 mStatusMessageAction.setClickable(true);
622 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
623 @Override
624 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700625 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
626 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700627 }
628 });
629 } else {
630 mStatusMessageAction.setClickable(false);
631 }
632 }
633
634 private StatusMessage getStatusMessage(Cursor statusCursor) {
635 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
636 if (messages.size() == 0) {
637 return null;
638 }
639 // There can only be a single status message per source package, so num of messages can
640 // at most be 1.
641 if (messages.size() > 1) {
642 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
643 " Will use the first one.", messages.size()));
644 }
645 return messages.get(0);
646 }
647
648 @Override
649 public boolean onCreateOptionsMenu(Menu menu) {
650 getMenuInflater().inflate(R.menu.call_details_options, menu);
651 return super.onCreateOptionsMenu(menu);
652 }
653
654 @Override
655 public boolean onPrepareOptionsMenu(Menu menu) {
656 // This action deletes all elements in the group from the call log.
657 // We don't have this action for voicemails, because you can just use the trash button.
658 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
659 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
660 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
661 return super.onPrepareOptionsMenu(menu);
662 }
663
Chiao Cheng94b10b52012-08-17 16:59:12 -0700664 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
665 final StringBuilder callIds = new StringBuilder();
666 for (Uri callUri : getCallLogEntryUris()) {
667 if (callIds.length() != 0) {
668 callIds.append(",");
669 }
670 callIds.append(ContentUris.parseId(callUri));
671 }
672 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
673 new AsyncTask<Void, Void, Void>() {
674 @Override
675 public Void doInBackground(Void... params) {
Yorke Lee4aece952015-05-02 22:22:54 -0700676 getContentResolver().delete(
677 TelecomUtil.getCallLogUri(CallDetailActivity.this),
Chiao Cheng94b10b52012-08-17 16:59:12 -0700678 Calls._ID + " IN (" + callIds + ")", null);
679 return null;
680 }
681
682 @Override
683 public void onPostExecute(Void result) {
684 finish();
685 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700686 }
687 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688 }
689
690 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700691 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700692 }
693
694 public void onMenuTrashVoicemail(MenuItem menuItem) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700695 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
696 new AsyncTask<Void, Void, Void>() {
697 @Override
698 public Void doInBackground(Void... params) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700699 getContentResolver().delete(mVoicemailUri, null, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700700 return null;
701 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700702
Chiao Cheng94b10b52012-08-17 16:59:12 -0700703 @Override
704 public void onPostExecute(Void result) {
705 finish();
706 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700707 }
708 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700709 }
710
711 @Override
712 protected void onPause() {
713 // Immediately stop the proximity sensor.
714 disableProximitySensor(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 super.onPause();
716 }
717
718 @Override
719 public void enableProximitySensor() {
Yorke Lee71513982015-02-25 20:23:53 -0800720 if (mProximityWakeLock == null) {
721 return;
722 }
723 if (!mProximityWakeLock.isHeld()) {
724 Log.i(TAG, "Acquiring proximity wake lock");
725 mProximityWakeLock.acquire();
726 } else {
727 Log.i(TAG, "Proximity wake lock already acquired");
728 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 }
730
731 @Override
732 public void disableProximitySensor(boolean waitForFarState) {
Yorke Lee71513982015-02-25 20:23:53 -0800733 if (mProximityWakeLock == null) {
734 return;
735 }
736 if (mProximityWakeLock.isHeld()) {
737 Log.i(TAG, "Releasing proximity wake lock");
738 int flags = (waitForFarState ? PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY : 0);
739 mProximityWakeLock.release(flags);
740 } else {
741 Log.i(TAG, "Proximity wake lock already released");
742 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 }
744
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 private void closeSystemDialogs() {
746 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
747 }
748
Chiao Cheng35071c02012-10-15 18:36:24 -0700749 /** Returns the given text, forced to be left-to-right. */
750 private static CharSequence forceLeftToRight(CharSequence text) {
751 StringBuilder sb = new StringBuilder();
752 sb.append(LEFT_TO_RIGHT_EMBEDDING);
753 sb.append(text);
754 sb.append(POP_DIRECTIONAL_FORMATTING);
755 return sb.toString();
756 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700757}