blob: 6da7c798cd71c12b93bfc3d75ee4465ce8d23329 [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 -070064
65import java.util.List;
66
67/**
68 * Displays the details of a specific call log entry.
69 * <p>
70 * This activity can be either started with the URI of a single call log entry, or with the
71 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
72 */
Andrew Lee93b816f2015-05-13 12:23:35 -070073public class CallDetailActivity extends Activity {
Chiao Cheng94b10b52012-08-17 16:59:12 -070074 private static final String TAG = "CallDetail";
75
Andrew Lee7c461dc2015-05-15 10:18:45 -070076 /** A long array extra containing ids of call log entries to display. */
Chiao Cheng94b10b52012-08-17 16:59:12 -070077 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
78 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
79 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
80 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
81 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
82 /** If the activity was triggered from a notification. */
83 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
84
Yorke Lee8cd94232014-07-23 14:05:31 -070085 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
86
Andrew Lee7c461dc2015-05-15 10:18:45 -070087 private CallLogAsyncTaskListener mCallLogAsyncTaskListener = new CallLogAsyncTaskListener() {
88 @Override
89 public void onDeleteCall() {
90 finish();
91 }
92
93 @Override
94 public void onDeleteVoicemail() {
95 finish();
96 }
97
98 @Override
99 public void onGetCallDetails(PhoneCallDetails[] details) {
100 Context context = CallDetailActivity.this;
101
102 if (details == null) {
103 // Somewhere went wrong: we're going to bail out and show error to users.
104 Toast.makeText(context, R.string.toast_call_detail_error,
105 Toast.LENGTH_SHORT).show();
106 finish();
107 return;
108 }
109
110 // We know that all calls are from the same number and the same contact, so pick the
111 // first.
112 PhoneCallDetails firstDetails = details[0];
113 mNumber = TextUtils.isEmpty(firstDetails.number) ?
114 null : firstDetails.number.toString();
115 final int numberPresentation = firstDetails.numberPresentation;
116 final Uri contactUri = firstDetails.contactUri;
117 final Uri photoUri = firstDetails.photoUri;
118 final PhoneAccountHandle accountHandle = firstDetails.accountHandle;
119
120 // Cache the details about the phone number.
121 final boolean canPlaceCallsTo =
122 PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
123 final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper(context);
124 final boolean isVoicemailNumber =
125 phoneUtils.isVoicemailNumber(accountHandle, mNumber);
126 final boolean isSipNumber = PhoneNumberUtilsWrapper.isSipNumber(mNumber);
127
128 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
129
130 final CharSequence displayNumber = firstDetails.displayNumber;
131 final String displayNumberStr = mBidiFormatter.unicodeWrap(
132 displayNumber.toString(), TextDirectionHeuristics.LTR);
133
134 if (!TextUtils.isEmpty(firstDetails.name)) {
135 mCallerName.setText(firstDetails.name);
136 mCallerNumber.setText(callLocationOrType + " " + displayNumberStr);
137 } else {
138 mCallerName.setText(displayNumberStr);
139 if (!TextUtils.isEmpty(callLocationOrType)) {
140 mCallerNumber.setText(callLocationOrType);
141 mCallerNumber.setVisibility(View.VISIBLE);
142 } else {
143 mCallerNumber.setVisibility(View.GONE);
144 }
145 }
146
147 String accountLabel = PhoneAccountUtils.getAccountLabel(context, accountHandle);
148 if (!TextUtils.isEmpty(accountLabel)) {
149 mAccountLabel.setText(accountLabel);
150 mAccountLabel.setVisibility(View.VISIBLE);
151 } else {
152 mAccountLabel.setVisibility(View.GONE);
153 }
154
155 mHasEditNumberBeforeCallOption =
156 canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
157 mHasTrashOption = hasVoicemail();
158 mHasRemoveFromCallLogOption = !hasVoicemail();
159 invalidateOptionsMenu();
160
161 ListView historyList = (ListView) findViewById(R.id.history);
162 historyList.setAdapter(
163 new CallDetailHistoryAdapter(context, mInflater, mCallTypeHelper, details));
164
165 String lookupKey = contactUri == null ? null
166 : ContactInfoHelper.getLookupKeyFromUri(contactUri);
167
168 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
169
170 final int contactType =
171 isVoicemailNumber ? ContactPhotoManager.TYPE_VOICEMAIL :
172 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
173 ContactPhotoManager.TYPE_DEFAULT;
174
175 String nameForDefaultImage;
176 if (TextUtils.isEmpty(firstDetails.name)) {
177 nameForDefaultImage = firstDetails.displayNumber;
178 } else {
179 nameForDefaultImage = firstDetails.name.toString();
180 }
181
182 loadContactPhotos(
183 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
184 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
185 }
186
187 /**
188 * Determines the location geocode text for a call, or the phone number type
189 * (if available).
190 *
191 * @param details The call details.
192 * @return The phone number type or location.
193 */
194 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
195 if (!TextUtils.isEmpty(details.name)) {
196 return Phone.getTypeLabel(mResources, details.numberType,
197 details.numberLabel);
198 } else {
199 return details.geocode;
200 }
201 }
202 };
203
Chiao Cheng94b10b52012-08-17 16:59:12 -0700204 private CallTypeHelper mCallTypeHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700205 private QuickContactBadge mQuickContactBadge;
206 private TextView mCallerName;
207 private TextView mCallerNumber;
Nancy Chen19702632014-07-25 13:23:16 -0700208 private TextView mAccountLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700209 private ContactInfoHelper mContactInfoHelper;
210
211 private String mNumber = null;
212 private String mDefaultCountryIso;
213
214 /* package */ LayoutInflater mInflater;
215 /* package */ Resources mResources;
216 /** Helper to load contact photos. */
217 private ContactPhotoManager mContactPhotoManager;
Andrew Leec1167e92015-05-18 11:27:54 -0700218
Yorke Lee257b6f22014-07-31 14:16:44 -0700219 private Uri mVoicemailUri;
Andrew Lee99e8adb2014-10-02 12:27:37 -0700220 private BidiFormatter mBidiFormatter = BidiFormatter.getInstance();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700221
222 /** Whether we should show "edit number before call" in the options menu. */
223 private boolean mHasEditNumberBeforeCallOption;
224 /** Whether we should show "trash" in the options menu. */
225 private boolean mHasTrashOption;
226 /** Whether we should show "remove from call log" in the options menu. */
227 private boolean mHasRemoveFromCallLogOption;
228
Chiao Cheng94b10b52012-08-17 16:59:12 -0700229 @Override
230 protected void onCreate(Bundle icicle) {
231 super.onCreate(icicle);
232
233 setContentView(R.layout.call_detail);
234
Chiao Cheng94b10b52012-08-17 16:59:12 -0700235 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
236 mResources = getResources();
237
238 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee257b6f22014-07-31 14:16:44 -0700239
240 mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
241
Tyler Gunn18164c82014-06-09 10:20:57 -0700242 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
243 mQuickContactBadge.setOverlay(null);
Brian Attwell74eb4ec2015-02-25 22:31:22 -0800244 mQuickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE);
Tyler Gunn18164c82014-06-09 10:20:57 -0700245 mCallerName = (TextView) findViewById(R.id.caller_name);
246 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700247 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700248 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700249 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Yorke Lee71513982015-02-25 20:23:53 -0800250
Chiao Cheng35071c02012-10-15 18:36:24 -0700251 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Chiao Chengadb742c2013-10-07 17:46:25 -0700252 getActionBar().setDisplayHomeAsUpEnabled(true);
Yorke Lee257b6f22014-07-31 14:16:44 -0700253
Chiao Cheng94b10b52012-08-17 16:59:12 -0700254 if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
255 closeSystemDialogs();
256 }
257 }
258
259 @Override
260 public void onResume() {
261 super.onResume();
Andrew Lee7c461dc2015-05-15 10:18:45 -0700262
263 CallLogAsyncTaskUtil.getCallDetails(this, getCallLogEntryUris(), mCallLogAsyncTaskListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700264 }
265
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700267 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700268 }
269
Chiao Cheng94b10b52012-08-17 16:59:12 -0700270 /**
271 * Returns the list of URIs to show.
272 * <p>
273 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
274 * a list of ids in the call log added as an extra on the URI.
275 * <p>
276 * If both are available, the data on the intent takes precedence.
277 */
278 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700279 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 if (uri != null) {
281 // If there is a data on the intent, it takes precedence over the extra.
282 return new Uri[]{ uri };
283 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700284 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
285 final int numIds = ids == null ? 0 : ids.length;
286 final Uri[] uris = new Uri[numIds];
287 for (int index = 0; index < numIds; ++index) {
Yorke Lee4aece952015-05-02 22:22:54 -0700288 uris[index] = ContentUris.withAppendedId(
289 TelecomUtil.getCallLogUri(CallDetailActivity.this), ids[index]);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290 }
291 return uris;
292 }
293
Chiao Cheng94b10b52012-08-17 16:59:12 -0700294 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700295 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
296 String lookupKey, int contactType) {
297
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800298 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700299 contactType, true /* isCircular */);
300
301 mQuickContactBadge.assignContactUri(contactUri);
302 mQuickContactBadge.setContentDescription(
303 mResources.getString(R.string.description_contact_details, displayName));
304
305 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
306 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700307 }
308
Chiao Cheng94b10b52012-08-17 16:59:12 -0700309 @Override
310 public boolean onCreateOptionsMenu(Menu menu) {
311 getMenuInflater().inflate(R.menu.call_details_options, menu);
312 return super.onCreateOptionsMenu(menu);
313 }
314
315 @Override
316 public boolean onPrepareOptionsMenu(Menu menu) {
317 // This action deletes all elements in the group from the call log.
318 // We don't have this action for voicemails, because you can just use the trash button.
319 menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
320 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
321 menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
322 return super.onPrepareOptionsMenu(menu);
323 }
324
Chiao Cheng94b10b52012-08-17 16:59:12 -0700325 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
326 final StringBuilder callIds = new StringBuilder();
327 for (Uri callUri : getCallLogEntryUris()) {
328 if (callIds.length() != 0) {
329 callIds.append(",");
330 }
331 callIds.append(ContentUris.parseId(callUri));
332 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700333
Andrew Lee7c461dc2015-05-15 10:18:45 -0700334 CallLogAsyncTaskUtil.deleteCalls(this, callIds.toString(), mCallLogAsyncTaskListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700335 }
336
337 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700338 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700339 }
340
341 public void onMenuTrashVoicemail(MenuItem menuItem) {
Andrew Lee7c461dc2015-05-15 10:18:45 -0700342 CallLogAsyncTaskUtil.deleteVoicemail(this, mVoicemailUri, mCallLogAsyncTaskListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700343 }
344
Chiao Cheng94b10b52012-08-17 16:59:12 -0700345 private void closeSystemDialogs() {
346 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
347 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700348}