blob: db247ba11c4e99507ee49a52f5b02298332d6a56 [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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.content.Context;
23import android.content.Intent;
24import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070025import android.net.Uri;
Chiao Cheng94b10b52012-08-17 16:59:12 -070026import android.os.Bundle;
Yorke Lee71513982015-02-25 20:23:53 -080027import android.os.PowerManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.provider.VoicemailContract.Voicemails;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070030import android.telecom.PhoneAccount;
Nancy Chene80d6222014-10-08 20:17:55 -070031import android.telecom.PhoneAccountHandle;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.telephony.TelephonyManager;
Andrew Lee99e8adb2014-10-02 12:27:37 -070033import android.text.BidiFormatter;
34import android.text.TextDirectionHeuristics;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import 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;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080049import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070050import com.android.contacts.common.GeoUtil;
Brian Attwell82043242015-02-03 14:13:58 -080051import com.android.contacts.common.CallUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070052import com.android.dialer.calllog.CallDetailHistoryAdapter;
Andrew Lee7c461dc2015-05-15 10:18:45 -070053import com.android.dialer.calllog.CallLogAsyncTaskUtil.CallLogAsyncTaskListener;
54import com.android.dialer.calllog.CallLogAsyncTaskUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055import com.android.dialer.calllog.CallTypeHelper;
56import com.android.dialer.calllog.ContactInfo;
57import com.android.dialer.calllog.ContactInfoHelper;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070058import com.android.dialer.calllog.PhoneAccountUtils;
Andrew Lee49efd912015-05-19 12:17:26 -070059import com.android.dialer.calllog.PhoneNumberDisplayUtil;
Chiao Chengfb0a9342013-09-13 17:27:42 -070060import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Andrew Lee247df6e2015-05-12 19:09:00 -070061import com.android.dialer.util.IntentUtil;
Yorke Lee39213592014-04-07 15:39:48 -070062import com.android.dialer.util.DialerUtils;
Yorke Lee4aece952015-05-02 22:22:54 -070063import com.android.dialer.util.TelecomUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070064import com.android.dialer.voicemail.VoicemailPlaybackFragment;
65import com.android.dialer.voicemail.VoicemailStatusHelper;
66import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
67import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
68
69import java.util.List;
70
71/**
72 * Displays the details of a specific call log entry.
73 * <p>
74 * This activity can be either started with the URI of a single call log entry, or with the
75 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
76 */
Andrew Lee93b816f2015-05-13 12:23:35 -070077public class CallDetailActivity extends Activity {
Chiao Cheng94b10b52012-08-17 16:59:12 -070078 private static final String TAG = "CallDetail";
79
Andrew Lee7c461dc2015-05-15 10:18:45 -070080 /** A long array extra containing ids of call log entries to display. */
Chiao Cheng94b10b52012-08-17 16:59:12 -070081 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
82 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
83 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
84 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
85 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
86 /** If the activity was triggered from a notification. */
87 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
88
Yorke Lee8cd94232014-07-23 14:05:31 -070089 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
90
Andrew Lee7c461dc2015-05-15 10:18:45 -070091 private CallLogAsyncTaskListener mCallLogAsyncTaskListener = new CallLogAsyncTaskListener() {
92 @Override
93 public void onDeleteCall() {
94 finish();
95 }
96
97 @Override
98 public void onDeleteVoicemail() {
99 finish();
100 }
101
102 @Override
103 public void onGetCallDetails(PhoneCallDetails[] details) {
104 Context context = CallDetailActivity.this;
105
106 if (details == null) {
107 // Somewhere went wrong: we're going to bail out and show error to users.
108 Toast.makeText(context, R.string.toast_call_detail_error,
109 Toast.LENGTH_SHORT).show();
110 finish();
111 return;
112 }
113
114 // We know that all calls are from the same number and the same contact, so pick the
115 // first.
116 PhoneCallDetails firstDetails = details[0];
117 mNumber = TextUtils.isEmpty(firstDetails.number) ?
118 null : firstDetails.number.toString();
119 final int numberPresentation = firstDetails.numberPresentation;
120 final Uri contactUri = firstDetails.contactUri;
121 final Uri photoUri = firstDetails.photoUri;
122 final PhoneAccountHandle accountHandle = firstDetails.accountHandle;
123
124 // Cache the details about the phone number.
125 final boolean canPlaceCallsTo =
126 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
127 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper(context);
128 final boolean isVoicemailNumber =
129 phoneUtils.isVoicemailNumber(accountHandle, mNumber);
130 final boolean isSipNumber = PhoneNumberUtilsWrapper.isSipNumber(mNumber);
131
132 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
133
134 final CharSequence displayNumber = firstDetails.displayNumber;
135 final String displayNumberStr = mBidiFormatter.unicodeWrap(
136 displayNumber.toString(), TextDirectionHeuristics.LTR);
137
138 if (!TextUtils.isEmpty(firstDetails.name)) {
139 mCallerName.setText(firstDetails.name);
140 mCallerNumber.setText(callLocationOrType + " " + displayNumberStr);
141 } else {
142 mCallerName.setText(displayNumberStr);
143 if (!TextUtils.isEmpty(callLocationOrType)) {
144 mCallerNumber.setText(callLocationOrType);
145 mCallerNumber.setVisibility(View.VISIBLE);
146 } else {
147 mCallerNumber.setVisibility(View.GONE);
148 }
149 }
150
151 String accountLabel = PhoneAccountUtils.getAccountLabel(context, accountHandle);
152 if (!TextUtils.isEmpty(accountLabel)) {
153 mAccountLabel.setText(accountLabel);
154 mAccountLabel.setVisibility(View.VISIBLE);
155 } else {
156 mAccountLabel.setVisibility(View.GONE);
157 }
158
159 mHasEditNumberBeforeCallOption =
160 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
161 mHasTrashOption = hasVoicemail();
162 mHasRemoveFromCallLogOption = !hasVoicemail();
163 invalidateOptionsMenu();
164
165 ListView historyList = (ListView) findViewById(R.id.history);
166 historyList.setAdapter(
167 new CallDetailHistoryAdapter(context, mInflater, mCallTypeHelper, details));
168
169 String lookupKey = contactUri == null ? null
170 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
171
172 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
173
174 final int contactType =
175 isVoicemailNumber ? ContactPhotoManager.TYPE_VOICEMAIL :
176 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
177 ContactPhotoManager.TYPE_DEFAULT;
178
179 String nameForDefaultImage;
180 if (TextUtils.isEmpty(firstDetails.name)) {
181 nameForDefaultImage = firstDetails.displayNumber;
182 } else {
183 nameForDefaultImage = firstDetails.name.toString();
184 }
185
186 loadContactPhotos(
187 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
188 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
189 }
190
191 /**
192 * Determines the location geocode text for a call, or the phone number type
193 * (if available).
194 *
195 * @param details The call details.
196 * @return The phone number type or location.
197 */
198 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
199 if (!TextUtils.isEmpty(details.name)) {
200 return Phone.getTypeLabel(mResources, details.numberType,
201 details.numberLabel);
202 } else {
203 return details.geocode;
204 }
205 }
206 };
207
Chiao Cheng94b10b52012-08-17 16:59:12 -0700208 private CallTypeHelper mCallTypeHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700209 private QuickContactBadge mQuickContactBadge;
210 private TextView mCallerName;
211 private TextView mCallerNumber;
Nancy Chen19702632014-07-25 13:23:16 -0700212 private TextView mAccountLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700213 private ContactInfoHelper mContactInfoHelper;
214
215 private String mNumber = null;
216 private String mDefaultCountryIso;
217
218 /* package */ LayoutInflater mInflater;
219 /* package */ Resources mResources;
220 /** Helper to load contact photos. */
221 private ContactPhotoManager mContactPhotoManager;
Andrew Leec1167e92015-05-18 11:27:54 -0700222
Yorke Lee257b6f22014-07-31 14:16:44 -0700223 private LinearLayout mVoicemailHeader;
224
225 private Uri mVoicemailUri;
Andrew Lee99e8adb2014-10-02 12:27:37 -0700226 private BidiFormatter mBidiFormatter = BidiFormatter.getInstance();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700227
228 /** Whether we should show "edit number before call" in the options menu. */
229 private boolean mHasEditNumberBeforeCallOption;
230 /** Whether we should show "trash" in the options menu. */
231 private boolean mHasTrashOption;
232 /** Whether we should show "remove from call log" in the options menu. */
233 private boolean mHasRemoveFromCallLogOption;
234
Chiao Cheng94b10b52012-08-17 16:59:12 -0700235 @Override
236 protected void onCreate(Bundle icicle) {
237 super.onCreate(icicle);
238
239 setContentView(R.layout.call_detail);
240
Chiao Cheng94b10b52012-08-17 16:59:12 -0700241 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
242 mResources = getResources();
243
244 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee257b6f22014-07-31 14:16:44 -0700245
246 mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
247
Tyler Gunn18164c82014-06-09 10:20:57 -0700248 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
249 mQuickContactBadge.setOverlay(null);
Brian Attwell74eb4ec2015-02-25 22:31:22 -0800250 mQuickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE);
Tyler Gunn18164c82014-06-09 10:20:57 -0700251 mCallerName = (TextView) findViewById(R.id.caller_name);
252 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700253 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700254 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700255 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Yorke Lee71513982015-02-25 20:23:53 -0800256
Chiao Cheng35071c02012-10-15 18:36:24 -0700257 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700258 getActionBar().setDisplayHomeAsUpEnabled(true);
Yorke Lee257b6f22014-07-31 14:16:44 -0700259
Chiao Cheng94b10b52012-08-17 16:59:12 -0700260 optionallyHandleVoicemail();
261 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
262 closeSystemDialogs();
263 }
264 }
265
266 @Override
267 public void onResume() {
268 super.onResume();
Andrew Lee7c461dc2015-05-15 10:18:45 -0700269
270 CallLogAsyncTaskUtil.getCallDetails(this, getCallLogEntryUris(), mCallLogAsyncTaskListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700271 }
272
273 /**
274 * Handle voicemail playback or hide voicemail ui.
275 * <p>
276 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
Yorke Lee257b6f22014-07-31 14:16:44 -0700277 * playback. If it doesn't, then don't inflate the voicemail ui.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700278 */
279 private void optionallyHandleVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700280
Chiao Cheng94b10b52012-08-17 16:59:12 -0700281 if (hasVoicemail()) {
Yorke Lee257b6f22014-07-31 14:16:44 -0700282 LayoutInflater inflater =
283 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
284 mVoicemailHeader =
285 (LinearLayout) inflater.inflate(R.layout.call_details_voicemail_header, null);
286 View voicemailContainer = mVoicemailHeader.findViewById(R.id.voicemail_container);
Yorke Lee257b6f22014-07-31 14:16:44 -0700287 ListView historyList = (ListView) findViewById(R.id.history);
288 historyList.addHeaderView(mVoicemailHeader);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
290 // to play and optionally start the playback.
291 // Do a query to fetch the voicemail status messages.
Yorke Lee8cd94232014-07-23 14:05:31 -0700292 VoicemailPlaybackFragment playbackFragment;
293
294 playbackFragment = (VoicemailPlaybackFragment) getFragmentManager().findFragmentByTag(
295 VOICEMAIL_FRAGMENT_TAG);
296
297 if (playbackFragment == null) {
298 playbackFragment = new VoicemailPlaybackFragment();
299 Bundle fragmentArguments = new Bundle();
Yorke Lee257b6f22014-07-31 14:16:44 -0700300 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, mVoicemailUri);
Yorke Lee8cd94232014-07-23 14:05:31 -0700301 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
302 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
303 }
304 playbackFragment.setArguments(fragmentArguments);
305 getFragmentManager().beginTransaction()
306 .add(R.id.voicemail_container, playbackFragment, VOICEMAIL_FRAGMENT_TAG)
307 .commitAllowingStateLoss();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700308 }
Yorke Lee8cd94232014-07-23 14:05:31 -0700309
Chiao Cheng94b10b52012-08-17 16:59:12 -0700310 voicemailContainer.setVisibility(View.VISIBLE);
Andrew Lee7c461dc2015-05-15 10:18:45 -0700311 CallLogAsyncTaskUtil.markVoicemailAsRead(this, mVoicemailUri);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700312 }
313 }
314
315 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700316 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700317 }
318
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 /**
320 * Returns the list of URIs to show.
321 * <p>
322 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
323 * a list of ids in the call log added as an extra on the URI.
324 * <p>
325 * If both are available, the data on the intent takes precedence.
326 */
327 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700328 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700329 if (uri != null) {
330 // If there is a data on the intent, it takes precedence over the extra.
331 return new Uri[]{ uri };
332 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700333 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
334 final int numIds = ids == null ? 0 : ids.length;
335 final Uri[] uris = new Uri[numIds];
336 for (int index = 0; index < numIds; ++index) {
Yorke Lee4aece952015-05-02 22:22:54 -0700337 uris[index] = ContentUris.withAppendedId(
338 TelecomUtil.getCallLogUri(CallDetailActivity.this), ids[index]);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700339 }
340 return uris;
341 }
342
Chiao Cheng94b10b52012-08-17 16:59:12 -0700343 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700344 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
345 String lookupKey, int contactType) {
346
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800347 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700348 contactType, true /* isCircular */);
349
350 mQuickContactBadge.assignContactUri(contactUri);
351 mQuickContactBadge.setContentDescription(
352 mResources.getString(R.string.description_contact_details, displayName));
353
354 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
355 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700356 }
357
Chiao Cheng94b10b52012-08-17 16:59:12 -0700358 @Override
359 public boolean onCreateOptionsMenu(Menu menu) {
360 getMenuInflater().inflate(R.menu.call_details_options, menu);
361 return super.onCreateOptionsMenu(menu);
362 }
363
364 @Override
365 public boolean onPrepareOptionsMenu(Menu menu) {
366 // This action deletes all elements in the group from the call log.
367 // We don't have this action for voicemails, because you can just use the trash button.
368 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
369 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
370 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
371 return super.onPrepareOptionsMenu(menu);
372 }
373
Chiao Cheng94b10b52012-08-17 16:59:12 -0700374 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
375 final StringBuilder callIds = new StringBuilder();
376 for (Uri callUri : getCallLogEntryUris()) {
377 if (callIds.length() != 0) {
378 callIds.append(",");
379 }
380 callIds.append(ContentUris.parseId(callUri));
381 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700382
Andrew Lee7c461dc2015-05-15 10:18:45 -0700383 CallLogAsyncTaskUtil.deleteCalls(this, callIds.toString(), mCallLogAsyncTaskListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700384 }
385
386 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700387 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700388 }
389
390 public void onMenuTrashVoicemail(MenuItem menuItem) {
Andrew Lee7c461dc2015-05-15 10:18:45 -0700391 CallLogAsyncTaskUtil.deleteVoicemail(this, mVoicemailUri, mCallLogAsyncTaskListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700392 }
393
Chiao Cheng94b10b52012-08-17 16:59:12 -0700394 private void closeSystemDialogs() {
395 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
396 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700397}