blob: 7a23944a06a29214237f111c4478ab21c6eacb9a [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;
Yorke Leeb82e8da2015-04-18 14:37:50 -070067import com.android.dialer.util.CallIntentUtil;
Yorke Lee39213592014-04-07 15:39:48 -070068import com.android.dialer.util.DialerUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070069import com.android.dialer.voicemail.VoicemailPlaybackFragment;
70import com.android.dialer.voicemail.VoicemailStatusHelper;
71import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
72import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
73
74import java.util.List;
75
76/**
77 * Displays the details of a specific call log entry.
78 * <p>
79 * This activity can be either started with the URI of a single call log entry, or with the
80 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
81 */
Brian Attwellbeab3bb2014-10-27 12:24:49 -070082public class CallDetailActivity extends Activity implements ProximitySensorAware {
Chiao Cheng94b10b52012-08-17 16:59:12 -070083 private static final String TAG = "CallDetail";
84
Chiao Cheng35071c02012-10-15 18:36:24 -070085 private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
86 private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
87
Chiao Cheng94b10b52012-08-17 16:59:12 -070088 /** The enumeration of {@link AsyncTask} objects used in this class. */
89 public enum Tasks {
90 MARK_VOICEMAIL_READ,
91 DELETE_VOICEMAIL_AND_FINISH,
92 REMOVE_FROM_CALL_LOG_AND_FINISH,
93 UPDATE_PHONE_CALL_DETAILS,
94 }
95
96 /** A long array extra containing ids of call log entries to display. */
97 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
98 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
99 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
100 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
101 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
102 /** If the activity was triggered from a notification. */
103 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
104
Yorke Lee8cd94232014-07-23 14:05:31 -0700105 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
106
Chiao Cheng94b10b52012-08-17 16:59:12 -0700107 private CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -0800108 private PhoneNumberDisplayHelper mPhoneNumberHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700109 private QuickContactBadge mQuickContactBadge;
110 private TextView mCallerName;
111 private TextView mCallerNumber;
Nancy Chen19702632014-07-25 13:23:16 -0700112 private TextView mAccountLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 private AsyncTaskExecutor mAsyncTaskExecutor;
114 private ContactInfoHelper mContactInfoHelper;
115
116 private String mNumber = null;
117 private String mDefaultCountryIso;
118
119 /* package */ LayoutInflater mInflater;
120 /* package */ Resources mResources;
121 /** Helper to load contact photos. */
122 private ContactPhotoManager mContactPhotoManager;
123 /** Helper to make async queries to content resolver. */
124 private CallDetailActivityQueryHandler mAsyncQueryHandler;
125 /** Helper to get voicemail status messages. */
126 private VoicemailStatusHelper mVoicemailStatusHelper;
127 // Views related to voicemail status message.
128 private View mStatusMessageView;
129 private TextView mStatusMessageText;
130 private TextView mStatusMessageAction;
Yorke Lee8cd94232014-07-23 14:05:31 -0700131 private TextView mVoicemailTranscription;
Yorke Lee257b6f22014-07-31 14:16:44 -0700132 private LinearLayout mVoicemailHeader;
133
134 private Uri mVoicemailUri;
Andrew Lee99e8adb2014-10-02 12:27:37 -0700135 private BidiFormatter mBidiFormatter = BidiFormatter.getInstance();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700136
137 /** Whether we should show "edit number before call" in the options menu. */
138 private boolean mHasEditNumberBeforeCallOption;
139 /** Whether we should show "trash" in the options menu. */
140 private boolean mHasTrashOption;
141 /** Whether we should show "remove from call log" in the options menu. */
142 private boolean mHasRemoveFromCallLogOption;
143
Yorke Lee71513982015-02-25 20:23:53 -0800144 private PowerManager.WakeLock mProximityWakeLock;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700145
146 static final String[] CALL_LOG_PROJECTION = new String[] {
147 CallLog.Calls.DATE,
148 CallLog.Calls.DURATION,
149 CallLog.Calls.NUMBER,
150 CallLog.Calls.TYPE,
151 CallLog.Calls.COUNTRY_ISO,
152 CallLog.Calls.GEOCODED_LOCATION,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700153 CallLog.Calls.NUMBER_PRESENTATION,
Ihab Awadc8daf202014-07-02 14:03:28 -0700154 CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME,
155 CallLog.Calls.PHONE_ACCOUNT_ID,
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700156 CallLog.Calls.FEATURES,
Yorke Lee8cd94232014-07-23 14:05:31 -0700157 CallLog.Calls.DATA_USAGE,
158 CallLog.Calls.TRANSCRIPTION
Chiao Cheng94b10b52012-08-17 16:59:12 -0700159 };
160
161 static final int DATE_COLUMN_INDEX = 0;
162 static final int DURATION_COLUMN_INDEX = 1;
163 static final int NUMBER_COLUMN_INDEX = 2;
164 static final int CALL_TYPE_COLUMN_INDEX = 3;
165 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
166 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700167 static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700168 static final int ACCOUNT_COMPONENT_NAME = 7;
169 static final int ACCOUNT_ID = 8;
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700170 static final int FEATURES = 9;
171 static final int DATA_USAGE = 10;
Yorke Lee8cd94232014-07-23 14:05:31 -0700172 static final int TRANSCRIPTION_COLUMN_INDEX = 11;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700173
Chiao Cheng94b10b52012-08-17 16:59:12 -0700174 @Override
175 protected void onCreate(Bundle icicle) {
176 super.onCreate(icicle);
177
178 setContentView(R.layout.call_detail);
179
180 mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
181 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
182 mResources = getResources();
183
184 mCallTypeHelper = new CallTypeHelper(getResources());
Nancy Chene80d6222014-10-08 20:17:55 -0700185 mPhoneNumberHelper = new PhoneNumberDisplayHelper(this, mResources);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700186 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
187 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Yorke Lee257b6f22014-07-31 14:16:44 -0700188
189 mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
190
Tyler Gunn18164c82014-06-09 10:20:57 -0700191 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
192 mQuickContactBadge.setOverlay(null);
Brian Attwell74eb4ec2015-02-25 22:31:22 -0800193 mQuickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE);
Tyler Gunn18164c82014-06-09 10:20:57 -0700194 mCallerName = (TextView) findViewById(R.id.caller_name);
195 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700196 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700197 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700198 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Yorke Lee71513982015-02-25 20:23:53 -0800199 final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
200 if (powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
201 mProximityWakeLock = powerManager.newWakeLock(
202 PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
203 } else {
204 mProximityWakeLock = null;
205 }
206
Chiao Cheng35071c02012-10-15 18:36:24 -0700207 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700208 getActionBar().setDisplayHomeAsUpEnabled(true);
Yorke Lee257b6f22014-07-31 14:16:44 -0700209
Chiao Cheng94b10b52012-08-17 16:59:12 -0700210 optionallyHandleVoicemail();
211 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
212 closeSystemDialogs();
213 }
214 }
215
216 @Override
217 public void onResume() {
218 super.onResume();
219 updateData(getCallLogEntryUris());
220 }
221
222 /**
223 * Handle voicemail playback or hide voicemail ui.
224 * <p>
225 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
Yorke Lee257b6f22014-07-31 14:16:44 -0700226 * playback. If it doesn't, then don't inflate the voicemail ui.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700227 */
228 private void optionallyHandleVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700229
Chiao Cheng94b10b52012-08-17 16:59:12 -0700230 if (hasVoicemail()) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700231 LayoutInflater inflater =
232 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
233 mVoicemailHeader =
234 (LinearLayout) inflater.inflate(R.layout.call_details_voicemail_header, null);
235 View voicemailContainer = mVoicemailHeader.findViewById(R.id.voicemail_container);
236 mStatusMessageView = mVoicemailHeader.findViewById(R.id.voicemail_status);
237 mStatusMessageText =
238 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_message);
239 mStatusMessageAction =
240 (TextView) mVoicemailHeader.findViewById(R.id.voicemail_status_action);
241 mVoicemailTranscription = (
242 TextView) mVoicemailHeader.findViewById(R.id.voicemail_transcription);
243 ListView historyList = (ListView) findViewById(R.id.history);
244 historyList.addHeaderView(mVoicemailHeader);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700245 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
246 // to play and optionally start the playback.
247 // Do a query to fetch the voicemail status messages.
Yorke Lee8cd94232014-07-23 14:05:31 -0700248 VoicemailPlaybackFragment playbackFragment;
249
250 playbackFragment = (VoicemailPlaybackFragment) getFragmentManager().findFragmentByTag(
251 VOICEMAIL_FRAGMENT_TAG);
252
253 if (playbackFragment == null) {
254 playbackFragment = new VoicemailPlaybackFragment();
255 Bundle fragmentArguments = new Bundle();
Yorke Lee257b6f22014-07-31 14:16:44 -0700256 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, mVoicemailUri);
Yorke Lee8cd94232014-07-23 14:05:31 -0700257 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
258 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
259 }
260 playbackFragment.setArguments(fragmentArguments);
261 getFragmentManager().beginTransaction()
262 .add(R.id.voicemail_container, playbackFragment, VOICEMAIL_FRAGMENT_TAG)
263 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700264 }
Yorke Lee8cd94232014-07-23 14:05:31 -0700265
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266 voicemailContainer.setVisibility(View.VISIBLE);
Yorke Lee257b6f22014-07-31 14:16:44 -0700267 mAsyncQueryHandler.startVoicemailStatusQuery(mVoicemailUri);
268 markVoicemailAsRead(mVoicemailUri);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269 }
270 }
271
272 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700273 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700274 }
275
276 private void markVoicemailAsRead(final Uri voicemailUri) {
277 mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
278 @Override
279 public Void doInBackground(Void... params) {
280 ContentValues values = new ContentValues();
281 values.put(Voicemails.IS_READ, true);
282 getContentResolver().update(voicemailUri, values,
283 Voicemails.IS_READ + " = 0", null);
Nancy Chenf153bf42015-05-06 19:29:56 -0700284 Intent intent = new Intent(getBaseContext(), CallLogNotificationsService.class);
285 intent.setAction(CallLogNotificationsService.ACTION_MARK_NEW_VOICEMAILS_AS_OLD);
286 getBaseContext().startService(intent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287 return null;
288 }
289 });
290 }
291
292 /**
293 * Returns the list of URIs to show.
294 * <p>
295 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
296 * a list of ids in the call log added as an extra on the URI.
297 * <p>
298 * If both are available, the data on the intent takes precedence.
299 */
300 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700301 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700302 if (uri != null) {
303 // If there is a data on the intent, it takes precedence over the extra.
304 return new Uri[]{ uri };
305 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700306 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
307 final int numIds = ids == null ? 0 : ids.length;
308 final Uri[] uris = new Uri[numIds];
309 for (int index = 0; index < numIds; ++index) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700310 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
311 }
312 return uris;
313 }
314
315 @Override
316 public boolean onKeyDown(int keyCode, KeyEvent event) {
317 switch (keyCode) {
318 case KeyEvent.KEYCODE_CALL: {
319 // Make sure phone isn't already busy before starting direct call
320 TelephonyManager tm = (TelephonyManager)
321 getSystemService(Context.TELEPHONY_SERVICE);
322 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Yorke Lee39213592014-04-07 15:39:48 -0700323 DialerUtils.startActivityWithErrorToast(this,
Yorke Leeb82e8da2015-04-18 14:37:50 -0700324 CallIntentUtil.getCallIntent(Uri.fromParts(PhoneAccount.SCHEME_TEL,
Brian Attwell82043242015-02-03 14:13:58 -0800325 mNumber, null)), R.string.call_not_available);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700326 return true;
327 }
328 }
329 }
330
331 return super.onKeyDown(keyCode, event);
332 }
333
334 /**
335 * Update user interface with details of given call.
336 *
Nancy Chene80d6222014-10-08 20:17:55 -0700337 * @param callUris URIs into {@link android.provider.CallLog.Calls} of the calls to be displayed
Chiao Cheng94b10b52012-08-17 16:59:12 -0700338 */
339 private void updateData(final Uri... callUris) {
340 class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
341 @Override
342 public PhoneCallDetails[] doInBackground(Void... params) {
343 // TODO: All phone calls correspond to the same person, so we can make a single
344 // lookup.
345 final int numCalls = callUris.length;
346 PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
347 try {
348 for (int index = 0; index < numCalls; ++index) {
349 details[index] = getPhoneCallDetailsForUri(callUris[index]);
350 }
351 return details;
352 } catch (IllegalArgumentException e) {
353 // Something went wrong reading in our primary data.
354 Log.w(TAG, "invalid URI starting call details", e);
355 return null;
356 }
357 }
358
359 @Override
360 public void onPostExecute(PhoneCallDetails[] details) {
Nancy Chene80d6222014-10-08 20:17:55 -0700361 Context context = CallDetailActivity.this;
362
Chiao Cheng94b10b52012-08-17 16:59:12 -0700363 if (details == null) {
364 // Somewhere went wrong: we're going to bail out and show error to users.
Nancy Chene80d6222014-10-08 20:17:55 -0700365 Toast.makeText(context, R.string.toast_call_detail_error,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700366 Toast.LENGTH_SHORT).show();
367 finish();
368 return;
369 }
370
371 // We know that all calls are from the same number and the same contact, so pick the
372 // first.
373 PhoneCallDetails firstDetails = details[0];
Nancy Chen164d9d52015-03-09 16:24:08 -0700374 mNumber = TextUtils.isEmpty(firstDetails.number) ?
375 null : firstDetails.number.toString();
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700376 final int numberPresentation = firstDetails.numberPresentation;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700377 final Uri contactUri = firstDetails.contactUri;
378 final Uri photoUri = firstDetails.photoUri;
Nancy Chene80d6222014-10-08 20:17:55 -0700379 final PhoneAccountHandle accountHandle = firstDetails.accountHandle;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700380
Chiao Cheng94b10b52012-08-17 16:59:12 -0700381 // Cache the details about the phone number.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700382 final boolean canPlaceCallsTo =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700383 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
Nancy Chene80d6222014-10-08 20:17:55 -0700384 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper(context);
385 final boolean isVoicemailNumber =
386 phoneUtils.isVoicemailNumber(accountHandle, mNumber);
387 final boolean isSipNumber = PhoneNumberUtilsWrapper.isSipNumber(mNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700388
Tyler Gunn18164c82014-06-09 10:20:57 -0700389 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390
Nancy Chene80d6222014-10-08 20:17:55 -0700391 final CharSequence displayNumber =
392 mPhoneNumberHelper.getDisplayNumber(
393 firstDetails.accountHandle,
394 firstDetails.number,
395 firstDetails.numberPresentation,
396 firstDetails.formattedNumber);
Andrew Lee99e8adb2014-10-02 12:27:37 -0700397 final String displayNumberStr = mBidiFormatter.unicodeWrap(
398 displayNumber.toString(), TextDirectionHeuristics.LTR);
399
Tyler Gunn18164c82014-06-09 10:20:57 -0700400 if (!TextUtils.isEmpty(firstDetails.name)) {
401 mCallerName.setText(firstDetails.name);
Andrew Lee99e8adb2014-10-02 12:27:37 -0700402 mCallerNumber.setText(callLocationOrType + " " + displayNumberStr);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700403 } else {
Andrew Lee99e8adb2014-10-02 12:27:37 -0700404 mCallerName.setText(displayNumberStr);
Tyler Gunn18164c82014-06-09 10:20:57 -0700405 if (!TextUtils.isEmpty(callLocationOrType)) {
406 mCallerNumber.setText(callLocationOrType);
407 mCallerNumber.setVisibility(View.VISIBLE);
408 } else {
409 mCallerNumber.setVisibility(View.GONE);
410 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700411 }
412
Nancy Chene80d6222014-10-08 20:17:55 -0700413 String accountLabel = PhoneAccountUtils.getAccountLabel(context, accountHandle);
414 if (!TextUtils.isEmpty(accountLabel)) {
415 mAccountLabel.setText(accountLabel);
Nancy Chen19702632014-07-25 13:23:16 -0700416 mAccountLabel.setVisibility(View.VISIBLE);
417 } else {
418 mAccountLabel.setVisibility(View.GONE);
419 }
420
Chiao Cheng94b10b52012-08-17 16:59:12 -0700421 mHasEditNumberBeforeCallOption =
422 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
423 mHasTrashOption = hasVoicemail();
424 mHasRemoveFromCallLogOption = !hasVoicemail();
425 invalidateOptionsMenu();
426
427 ListView historyList = (ListView) findViewById(R.id.history);
428 historyList.setAdapter(
Nancy Chene80d6222014-10-08 20:17:55 -0700429 new CallDetailHistoryAdapter(context, mInflater, mCallTypeHelper, details));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700430
Tyler Gunn18164c82014-06-09 10:20:57 -0700431 String lookupKey = contactUri == null ? null
432 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800433
434 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
435
436 final int contactType =
437 isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
438 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
439 ContactPhotoManager.TYPE_DEFAULT;
440
Tyler Gunn18164c82014-06-09 10:20:57 -0700441 String nameForDefaultImage;
442 if (TextUtils.isEmpty(firstDetails.name)) {
Nancy Chene80d6222014-10-08 20:17:55 -0700443 nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(
444 firstDetails.accountHandle,
445 firstDetails.number,
Tyler Gunn18164c82014-06-09 10:20:57 -0700446 firstDetails.numberPresentation,
447 firstDetails.formattedNumber).toString();
448 } else {
449 nameForDefaultImage = firstDetails.name.toString();
450 }
451
Yorke Lee8cd94232014-07-23 14:05:31 -0700452 if (hasVoicemail() && !TextUtils.isEmpty(firstDetails.transcription)) {
453 mVoicemailTranscription.setText(firstDetails.transcription);
454 mVoicemailTranscription.setVisibility(View.VISIBLE);
Yorke Lee8cd94232014-07-23 14:05:31 -0700455 }
456
Tyler Gunn18164c82014-06-09 10:20:57 -0700457 loadContactPhotos(
458 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700459 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
460 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700461
462 /**
463 * Determines the location geocode text for a call, or the phone number type
464 * (if available).
465 *
466 * @param details The call details.
467 * @return The phone number type or location.
468 */
469 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
470 if (!TextUtils.isEmpty(details.name)) {
471 return Phone.getTypeLabel(mResources, details.numberType,
472 details.numberLabel);
473 } else {
474 return details.geocode;
475 }
476 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700477 }
478 mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
479 }
480
481 /** Return the phone call details for a given call log URI. */
482 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
483 ContentResolver resolver = getContentResolver();
484 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
485 try {
486 if (callCursor == null || !callCursor.moveToFirst()) {
487 throw new IllegalArgumentException("Cannot find content: " + callUri);
488 }
489
490 // Read call log specifics.
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700491 final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
492 final int numberPresentation = callCursor.getInt(
493 NUMBER_PRESENTATION_COLUMN_INDEX);
494 final long date = callCursor.getLong(DATE_COLUMN_INDEX);
495 final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
496 final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700497 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
498 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
Yorke Lee8cd94232014-07-23 14:05:31 -0700499 final String transcription = callCursor.getString(TRANSCRIPTION_COLUMN_INDEX);
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700500
Nancy Chene80d6222014-10-08 20:17:55 -0700501 final PhoneAccountHandle accountHandle = PhoneAccountUtils.getAccount(
Nancy Chenb2eebaf2014-07-21 13:41:36 -0700502 callCursor.getString(ACCOUNT_COMPONENT_NAME),
Nancy Chene80d6222014-10-08 20:17:55 -0700503 callCursor.getString(ACCOUNT_ID));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700504
505 if (TextUtils.isEmpty(countryIso)) {
506 countryIso = mDefaultCountryIso;
507 }
508
509 // Formatted phone number.
510 final CharSequence formattedNumber;
511 // Read contact specifics.
512 final CharSequence nameText;
513 final int numberType;
514 final CharSequence numberLabel;
515 final Uri photoUri;
516 final Uri lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800517 int sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700518 // If this is not a regular number, there is no point in looking it up in the contacts.
519 ContactInfo info =
Chiao Chengfb0a9342013-09-13 17:27:42 -0700520 PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
Nancy Chene80d6222014-10-08 20:17:55 -0700521 && !new PhoneNumberUtilsWrapper(this).isVoicemailNumber(accountHandle, number)
Chiao Cheng94b10b52012-08-17 16:59:12 -0700522 ? mContactInfoHelper.lookupNumber(number, countryIso)
523 : null;
524 if (info == null) {
Nancy Chene80d6222014-10-08 20:17:55 -0700525 formattedNumber = mPhoneNumberHelper.getDisplayNumber(accountHandle, number,
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700526 numberPresentation, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700527 nameText = "";
528 numberType = 0;
529 numberLabel = "";
530 photoUri = null;
531 lookupUri = null;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800532 sourceType = 0;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700533 } else {
534 formattedNumber = info.formattedNumber;
535 nameText = info.name;
536 numberType = info.type;
537 numberLabel = info.label;
538 photoUri = info.photoUri;
539 lookupUri = info.lookupUri;
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800540 sourceType = info.sourceType;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700541 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700542 final int features = callCursor.getInt(FEATURES);
543 Long dataUsage = null;
544 if (!callCursor.isNull(DATA_USAGE)) {
545 dataUsage = callCursor.getLong(DATA_USAGE);
546 }
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700547 return new PhoneCallDetails(number, numberPresentation,
548 formattedNumber, countryIso, geocode,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700549 new int[]{ callType }, date, duration,
Nancy Chen87ba4892014-06-11 17:56:07 -0700550 nameText, numberType, numberLabel, lookupUri, photoUri, sourceType,
Nancy Chene80d6222014-10-08 20:17:55 -0700551 accountHandle, features, dataUsage, transcription);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700552 } finally {
553 if (callCursor != null) {
554 callCursor.close();
555 }
556 }
557 }
558
559 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700560 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
561 String lookupKey, int contactType) {
562
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800563 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700564 contactType, true /* isCircular */);
565
566 mQuickContactBadge.assignContactUri(contactUri);
567 mQuickContactBadge.setContentDescription(
568 mResources.getString(R.string.description_contact_details, displayName));
569
570 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
571 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700572 }
573
574 static final class ViewEntry {
575 public final String text;
576 public final Intent primaryIntent;
577 /** The description for accessibility of the primary action. */
578 public final String primaryDescription;
579
580 public CharSequence label = null;
581 /** Icon for the secondary action. */
582 public int secondaryIcon = 0;
583 /** Intent for the secondary action. If not null, an icon must be defined. */
584 public Intent secondaryIntent = null;
585 /** The description for accessibility of the secondary action. */
586 public String secondaryDescription = null;
587
588 public ViewEntry(String text, Intent intent, String description) {
589 this.text = text;
590 primaryIntent = intent;
591 primaryDescription = description;
592 }
593
594 public void setSecondaryAction(int icon, Intent intent, String description) {
595 secondaryIcon = icon;
596 secondaryIntent = intent;
597 secondaryDescription = description;
598 }
599 }
600
Chiao Cheng94b10b52012-08-17 16:59:12 -0700601 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
602 if (statusCursor == null) {
603 mStatusMessageView.setVisibility(View.GONE);
604 return;
605 }
606 final StatusMessage message = getStatusMessage(statusCursor);
607 if (message == null || !message.showInCallDetails()) {
608 mStatusMessageView.setVisibility(View.GONE);
609 return;
610 }
611
612 mStatusMessageView.setVisibility(View.VISIBLE);
613 mStatusMessageText.setText(message.callDetailsMessageId);
614 if (message.actionMessageId != -1) {
615 mStatusMessageAction.setText(message.actionMessageId);
616 }
617 if (message.actionUri != null) {
618 mStatusMessageAction.setClickable(true);
619 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
620 @Override
621 public void onClick(View v) {
Yorke Lee39213592014-04-07 15:39:48 -0700622 DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
623 new Intent(Intent.ACTION_VIEW, message.actionUri));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700624 }
625 });
626 } else {
627 mStatusMessageAction.setClickable(false);
628 }
629 }
630
631 private StatusMessage getStatusMessage(Cursor statusCursor) {
632 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
633 if (messages.size() == 0) {
634 return null;
635 }
636 // There can only be a single status message per source package, so num of messages can
637 // at most be 1.
638 if (messages.size() > 1) {
639 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
640 " Will use the first one.", messages.size()));
641 }
642 return messages.get(0);
643 }
644
645 @Override
646 public boolean onCreateOptionsMenu(Menu menu) {
647 getMenuInflater().inflate(R.menu.call_details_options, menu);
648 return super.onCreateOptionsMenu(menu);
649 }
650
651 @Override
652 public boolean onPrepareOptionsMenu(Menu menu) {
653 // This action deletes all elements in the group from the call log.
654 // We don't have this action for voicemails, because you can just use the trash button.
655 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
656 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
657 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
658 return super.onPrepareOptionsMenu(menu);
659 }
660
Chiao Cheng94b10b52012-08-17 16:59:12 -0700661 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
662 final StringBuilder callIds = new StringBuilder();
663 for (Uri callUri : getCallLogEntryUris()) {
664 if (callIds.length() != 0) {
665 callIds.append(",");
666 }
667 callIds.append(ContentUris.parseId(callUri));
668 }
669 mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
670 new AsyncTask<Void, Void, Void>() {
671 @Override
672 public Void doInBackground(Void... params) {
673 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
674 Calls._ID + " IN (" + callIds + ")", null);
675 return null;
676 }
677
678 @Override
679 public void onPostExecute(Void result) {
680 finish();
681 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700682 }
683 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700684 }
685
686 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700687 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688 }
689
690 public void onMenuTrashVoicemail(MenuItem menuItem) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700691 mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
692 new AsyncTask<Void, Void, Void>() {
693 @Override
694 public Void doInBackground(Void... params) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700695 getContentResolver().delete(mVoicemailUri, null, null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700696 return null;
697 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700698
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 @Override
700 public void onPostExecute(Void result) {
701 finish();
702 }
Tyler Gunn18164c82014-06-09 10:20:57 -0700703 }
704 );
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705 }
706
707 @Override
708 protected void onPause() {
709 // Immediately stop the proximity sensor.
710 disableProximitySensor(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711 super.onPause();
712 }
713
714 @Override
715 public void enableProximitySensor() {
Yorke Lee71513982015-02-25 20:23:53 -0800716 if (mProximityWakeLock == null) {
717 return;
718 }
719 if (!mProximityWakeLock.isHeld()) {
720 Log.i(TAG, "Acquiring proximity wake lock");
721 mProximityWakeLock.acquire();
722 } else {
723 Log.i(TAG, "Proximity wake lock already acquired");
724 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700725 }
726
727 @Override
728 public void disableProximitySensor(boolean waitForFarState) {
Yorke Lee71513982015-02-25 20:23:53 -0800729 if (mProximityWakeLock == null) {
730 return;
731 }
732 if (mProximityWakeLock.isHeld()) {
733 Log.i(TAG, "Releasing proximity wake lock");
734 int flags = (waitForFarState ? PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY : 0);
735 mProximityWakeLock.release(flags);
736 } else {
737 Log.i(TAG, "Proximity wake lock already released");
738 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739 }
740
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741 private void closeSystemDialogs() {
742 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
743 }
744
Chiao Cheng35071c02012-10-15 18:36:24 -0700745 /** Returns the given text, forced to be left-to-right. */
746 private static CharSequence forceLeftToRight(CharSequence text) {
747 StringBuilder sb = new StringBuilder();
748 sb.append(LEFT_TO_RIGHT_EMBEDDING);
749 sb.append(text);
750 sb.append(POP_DIRECTIONAL_FORMATTING);
751 return sb.toString();
752 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753}