blob: 72a50127ec2292865582856be57be1d65b078a01 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Chiao Cheng94b10b52012-08-17 16:59:12 -070019import android.content.ContentUris;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.content.Context;
21import android.content.Intent;
22import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.net.Uri;
Chiao Cheng94b10b52012-08-17 16:59:12 -070024import android.os.Bundle;
Chiao Cheng94b10b52012-08-17 16:59:12 -070025import android.provider.ContactsContract.CommonDataKinds.Phone;
Nancy Chen44898ad2015-08-13 12:03:47 -070026import android.support.v7.app.AppCompatActivity;
Nancy Chene80d6222014-10-08 20:17:55 -070027import android.telecom.PhoneAccountHandle;
Andrew Lee99e8adb2014-10-02 12:27:37 -070028import android.text.BidiFormatter;
29import android.text.TextDirectionHeuristics;
Chiao Cheng94b10b52012-08-17 16:59:12 -070030import android.text.TextUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.view.LayoutInflater;
32import android.view.Menu;
33import android.view.MenuItem;
34import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.widget.ListView;
Tyler Gunn18164c82014-06-09 10:20:57 -070036import android.widget.QuickContactBadge;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.widget.TextView;
38import android.widget.Toast;
39
Chiao Cheng35071c02012-10-15 18:36:24 -070040import com.android.contacts.common.ContactPhotoManager;
Yorke Lee56cb0ef2014-02-21 10:02:18 -080041import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Cheng35071c02012-10-15 18:36:24 -070042import com.android.contacts.common.GeoUtil;
Brian Attwell82043242015-02-03 14:13:58 -080043import com.android.contacts.common.CallUtil;
Tyler Gunn18b4a2e2015-08-05 08:18:37 -070044import com.android.contacts.common.util.UriUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import com.android.dialer.calllog.CallDetailHistoryAdapter;
Andrew Lee7c461dc2015-05-15 10:18:45 -070046import com.android.dialer.calllog.CallLogAsyncTaskUtil.CallLogAsyncTaskListener;
47import com.android.dialer.calllog.CallLogAsyncTaskUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import com.android.dialer.calllog.CallTypeHelper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import com.android.dialer.calllog.ContactInfoHelper;
Nancy Chenb2eebaf2014-07-21 13:41:36 -070050import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Leee3a2d132015-09-14 16:52:08 -070051import com.android.dialer.util.IntentUtil.CallIntentBuilder;
Andrew Leed3f6a6c2015-06-25 16:53:56 -070052import com.android.dialer.util.PhoneNumberUtil;
Yorke Lee4aece952015-05-02 22:22:54 -070053import com.android.dialer.util.TelecomUtil;
Yorke Leec8d6e162015-09-14 18:43:27 -070054import com.android.incallui.Call.LogState;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055
Chiao Cheng94b10b52012-08-17 16:59:12 -070056/**
57 * Displays the details of a specific call log entry.
58 * <p>
59 * This activity can be either started with the URI of a single call log entry, or with the
60 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
61 */
Nancy Chen44898ad2015-08-13 12:03:47 -070062public class CallDetailActivity extends AppCompatActivity
Andrew Lee2f05af32015-06-09 15:59:47 -070063 implements MenuItem.OnMenuItemClickListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070064 private static final String TAG = "CallDetail";
65
Andrew Lee7c461dc2015-05-15 10:18:45 -070066 /** A long array extra containing ids of call log entries to display. */
Chiao Cheng94b10b52012-08-17 16:59:12 -070067 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
68 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
69 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
Chiao Cheng94b10b52012-08-17 16:59:12 -070070 /** If the activity was triggered from a notification. */
71 public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
72
Yorke Lee8cd94232014-07-23 14:05:31 -070073 public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
74
Andrew Lee7c461dc2015-05-15 10:18:45 -070075 private CallLogAsyncTaskListener mCallLogAsyncTaskListener = new CallLogAsyncTaskListener() {
76 @Override
77 public void onDeleteCall() {
78 finish();
79 }
80
81 @Override
82 public void onDeleteVoicemail() {
83 finish();
84 }
85
86 @Override
87 public void onGetCallDetails(PhoneCallDetails[] details) {
Andrew Lee7c461dc2015-05-15 10:18:45 -070088 if (details == null) {
89 // Somewhere went wrong: we're going to bail out and show error to users.
Andrew Leee177fe62015-06-05 13:01:09 -070090 Toast.makeText(mContext, R.string.toast_call_detail_error,
Andrew Lee7c461dc2015-05-15 10:18:45 -070091 Toast.LENGTH_SHORT).show();
92 finish();
93 return;
94 }
95
96 // We know that all calls are from the same number and the same contact, so pick the
97 // first.
98 PhoneCallDetails firstDetails = details[0];
99 mNumber = TextUtils.isEmpty(firstDetails.number) ?
100 null : firstDetails.number.toString();
101 final int numberPresentation = firstDetails.numberPresentation;
102 final Uri contactUri = firstDetails.contactUri;
103 final Uri photoUri = firstDetails.photoUri;
104 final PhoneAccountHandle accountHandle = firstDetails.accountHandle;
105
106 // Cache the details about the phone number.
107 final boolean canPlaceCallsTo =
Andrew Leed3f6a6c2015-06-25 16:53:56 -0700108 PhoneNumberUtil.canPlaceCallsTo(mNumber, numberPresentation);
Andrew Leee177fe62015-06-05 13:01:09 -0700109 mIsVoicemailNumber =
Andrew Leed3f6a6c2015-06-25 16:53:56 -0700110 PhoneNumberUtil.isVoicemailNumber(mContext, accountHandle, mNumber);
111 final boolean isSipNumber = PhoneNumberUtil.isSipNumber(mNumber);
Andrew Lee7c461dc2015-05-15 10:18:45 -0700112
113 final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
114
115 final CharSequence displayNumber = firstDetails.displayNumber;
116 final String displayNumberStr = mBidiFormatter.unicodeWrap(
117 displayNumber.toString(), TextDirectionHeuristics.LTR);
118
119 if (!TextUtils.isEmpty(firstDetails.name)) {
120 mCallerName.setText(firstDetails.name);
121 mCallerNumber.setText(callLocationOrType + " " + displayNumberStr);
122 } else {
123 mCallerName.setText(displayNumberStr);
124 if (!TextUtils.isEmpty(callLocationOrType)) {
125 mCallerNumber.setText(callLocationOrType);
126 mCallerNumber.setVisibility(View.VISIBLE);
127 } else {
128 mCallerNumber.setVisibility(View.GONE);
129 }
130 }
131
Andrew Leee177fe62015-06-05 13:01:09 -0700132 mCallButton.setVisibility(canPlaceCallsTo ? View.VISIBLE : View.GONE);
133
134 String accountLabel = PhoneAccountUtils.getAccountLabel(mContext, accountHandle);
Andrew Lee7c461dc2015-05-15 10:18:45 -0700135 if (!TextUtils.isEmpty(accountLabel)) {
136 mAccountLabel.setText(accountLabel);
137 mAccountLabel.setVisibility(View.VISIBLE);
138 } else {
139 mAccountLabel.setVisibility(View.GONE);
140 }
141
142 mHasEditNumberBeforeCallOption =
Andrew Leee177fe62015-06-05 13:01:09 -0700143 canPlaceCallsTo && !isSipNumber && !mIsVoicemailNumber;
Andrew Lee2f05af32015-06-09 15:59:47 -0700144 mHasReportMenuOption = mContactInfoHelper.canReportAsInvalid(
145 firstDetails.sourceType, firstDetails.objectId);
Andrew Lee7c461dc2015-05-15 10:18:45 -0700146 invalidateOptionsMenu();
147
148 ListView historyList = (ListView) findViewById(R.id.history);
149 historyList.setAdapter(
Andrew Leee177fe62015-06-05 13:01:09 -0700150 new CallDetailHistoryAdapter(mContext, mInflater, mCallTypeHelper, details));
Andrew Lee7c461dc2015-05-15 10:18:45 -0700151
152 String lookupKey = contactUri == null ? null
Tyler Gunn18b4a2e2015-08-05 08:18:37 -0700153 : UriUtils.getLookupKeyFromUri(contactUri);
Andrew Lee7c461dc2015-05-15 10:18:45 -0700154
155 final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
156
157 final int contactType =
Andrew Leee177fe62015-06-05 13:01:09 -0700158 mIsVoicemailNumber ? ContactPhotoManager.TYPE_VOICEMAIL :
Andrew Lee7c461dc2015-05-15 10:18:45 -0700159 isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
160 ContactPhotoManager.TYPE_DEFAULT;
161
162 String nameForDefaultImage;
163 if (TextUtils.isEmpty(firstDetails.name)) {
164 nameForDefaultImage = firstDetails.displayNumber;
165 } else {
166 nameForDefaultImage = firstDetails.name.toString();
167 }
168
169 loadContactPhotos(
170 contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
171 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
172 }
173
174 /**
175 * Determines the location geocode text for a call, or the phone number type
176 * (if available).
177 *
178 * @param details The call details.
179 * @return The phone number type or location.
180 */
181 private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
182 if (!TextUtils.isEmpty(details.name)) {
183 return Phone.getTypeLabel(mResources, details.numberType,
184 details.numberLabel);
185 } else {
186 return details.geocode;
187 }
188 }
189 };
190
Andrew Leee177fe62015-06-05 13:01:09 -0700191 private Context mContext;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700192 private CallTypeHelper mCallTypeHelper;
Tyler Gunn18164c82014-06-09 10:20:57 -0700193 private QuickContactBadge mQuickContactBadge;
194 private TextView mCallerName;
195 private TextView mCallerNumber;
Nancy Chen19702632014-07-25 13:23:16 -0700196 private TextView mAccountLabel;
Andrew Leee177fe62015-06-05 13:01:09 -0700197 private View mCallButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700198 private ContactInfoHelper mContactInfoHelper;
199
Andrew Lee2f05af32015-06-09 15:59:47 -0700200 protected String mNumber;
Andrew Leee177fe62015-06-05 13:01:09 -0700201 private boolean mIsVoicemailNumber;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700202 private String mDefaultCountryIso;
203
204 /* package */ LayoutInflater mInflater;
205 /* package */ Resources mResources;
206 /** Helper to load contact photos. */
207 private ContactPhotoManager mContactPhotoManager;
Andrew Leec1167e92015-05-18 11:27:54 -0700208
Yorke Lee257b6f22014-07-31 14:16:44 -0700209 private Uri mVoicemailUri;
Andrew Lee99e8adb2014-10-02 12:27:37 -0700210 private BidiFormatter mBidiFormatter = BidiFormatter.getInstance();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700211
212 /** Whether we should show "edit number before call" in the options menu. */
213 private boolean mHasEditNumberBeforeCallOption;
Andrew Lee2f05af32015-06-09 15:59:47 -0700214 private boolean mHasReportMenuOption;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700215
Chiao Cheng94b10b52012-08-17 16:59:12 -0700216 @Override
217 protected void onCreate(Bundle icicle) {
218 super.onCreate(icicle);
219
Andrew Leee177fe62015-06-05 13:01:09 -0700220 mContext = this;
221
Chiao Cheng94b10b52012-08-17 16:59:12 -0700222 setContentView(R.layout.call_detail);
223
Chiao Cheng94b10b52012-08-17 16:59:12 -0700224 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
225 mResources = getResources();
226
227 mCallTypeHelper = new CallTypeHelper(getResources());
Yorke Lee257b6f22014-07-31 14:16:44 -0700228
229 mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
230
Tyler Gunn18164c82014-06-09 10:20:57 -0700231 mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
232 mQuickContactBadge.setOverlay(null);
Brian Attwell74eb4ec2015-02-25 22:31:22 -0800233 mQuickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE);
Tyler Gunn18164c82014-06-09 10:20:57 -0700234 mCallerName = (TextView) findViewById(R.id.caller_name);
235 mCallerNumber = (TextView) findViewById(R.id.caller_number);
Nancy Chen19702632014-07-25 13:23:16 -0700236 mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
Chiao Cheng35071c02012-10-15 18:36:24 -0700237 mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700238 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Yorke Lee71513982015-02-25 20:23:53 -0800239
Yorke Leee3a2d132015-09-14 16:52:08 -0700240 mCallButton = findViewById(R.id.call_back_button);
Andrew Leee177fe62015-06-05 13:01:09 -0700241 mCallButton.setOnClickListener(new View.OnClickListener() {
242 @Override
243 public void onClick(View view) {
Yorke Leec8d6e162015-09-14 18:43:27 -0700244 mContext.startActivity(
245 new CallIntentBuilder(mNumber)
246 .setCallInitiationType(LogState.INITIATION_CALL_DETAILS)
247 .build());
Andrew Leee177fe62015-06-05 13:01:09 -0700248 }
249 });
250
Chiao Cheng35071c02012-10-15 18:36:24 -0700251 mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
Nancy Chen44898ad2015-08-13 12:03:47 -0700252 getSupportActionBar().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 Lee2f05af32015-06-09 15:59:47 -0700262 getCallDetails();
263 }
Andrew Lee7c461dc2015-05-15 10:18:45 -0700264
Andrew Lee2f05af32015-06-09 15:59:47 -0700265 public void getCallDetails() {
Andrew Lee7c461dc2015-05-15 10:18:45 -0700266 CallLogAsyncTaskUtil.getCallDetails(this, getCallLogEntryUris(), mCallLogAsyncTaskListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700267 }
268
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269 private boolean hasVoicemail() {
Yorke Lee257b6f22014-07-31 14:16:44 -0700270 return mVoicemailUri != null;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700271 }
272
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273 /**
274 * Returns the list of URIs to show.
275 * <p>
276 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
277 * a list of ids in the call log added as an extra on the URI.
278 * <p>
279 * If both are available, the data on the intent takes precedence.
280 */
281 private Uri[] getCallLogEntryUris() {
Jay Shrauner757bdd32014-05-28 10:13:40 -0700282 final Uri uri = getIntent().getData();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283 if (uri != null) {
284 // If there is a data on the intent, it takes precedence over the extra.
285 return new Uri[]{ uri };
286 }
Jay Shrauner757bdd32014-05-28 10:13:40 -0700287 final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
288 final int numIds = ids == null ? 0 : ids.length;
289 final Uri[] uris = new Uri[numIds];
290 for (int index = 0; index < numIds; ++index) {
Yorke Lee4aece952015-05-02 22:22:54 -0700291 uris[index] = ContentUris.withAppendedId(
292 TelecomUtil.getCallLogUri(CallDetailActivity.this), ids[index]);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 }
294 return uris;
295 }
296
Chiao Cheng94b10b52012-08-17 16:59:12 -0700297 /** Load the contact photos and places them in the corresponding views. */
Tyler Gunn18164c82014-06-09 10:20:57 -0700298 private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
299 String lookupKey, int contactType) {
300
Yorke Lee56cb0ef2014-02-21 10:02:18 -0800301 final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
Tyler Gunn18164c82014-06-09 10:20:57 -0700302 contactType, true /* isCircular */);
303
304 mQuickContactBadge.assignContactUri(contactUri);
305 mQuickContactBadge.setContentDescription(
306 mResources.getString(R.string.description_contact_details, displayName));
307
308 mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
309 false /* darkTheme */, true /* isCircular */, request);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700310 }
311
Chiao Cheng94b10b52012-08-17 16:59:12 -0700312 @Override
313 public boolean onCreateOptionsMenu(Menu menu) {
314 getMenuInflater().inflate(R.menu.call_details_options, menu);
315 return super.onCreateOptionsMenu(menu);
316 }
317
318 @Override
319 public boolean onPrepareOptionsMenu(Menu menu) {
320 // This action deletes all elements in the group from the call log.
321 // We don't have this action for voicemails, because you can just use the trash button.
Andrew Lee2f05af32015-06-09 15:59:47 -0700322 menu.findItem(R.id.menu_remove_from_call_log)
323 .setVisible(!hasVoicemail())
324 .setOnMenuItemClickListener(this);
325 menu.findItem(R.id.menu_edit_number_before_call)
326 .setVisible(mHasEditNumberBeforeCallOption)
327 .setOnMenuItemClickListener(this);
328 menu.findItem(R.id.menu_trash)
329 .setVisible(hasVoicemail())
330 .setOnMenuItemClickListener(this);
331 menu.findItem(R.id.menu_report)
332 .setVisible(mHasReportMenuOption)
333 .setOnMenuItemClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700334 return super.onPrepareOptionsMenu(menu);
335 }
336
Andrew Lee2f05af32015-06-09 15:59:47 -0700337 @Override
338 public boolean onMenuItemClick(MenuItem item) {
339 switch (item.getItemId()) {
340 case R.id.menu_remove_from_call_log:
341 final StringBuilder callIds = new StringBuilder();
342 for (Uri callUri : getCallLogEntryUris()) {
343 if (callIds.length() != 0) {
344 callIds.append(",");
345 }
346 callIds.append(ContentUris.parseId(callUri));
347 }
348 CallLogAsyncTaskUtil.deleteCalls(
349 this, callIds.toString(), mCallLogAsyncTaskListener);
350 break;
351 case R.id.menu_edit_number_before_call:
352 startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
353 break;
354 case R.id.menu_trash:
355 CallLogAsyncTaskUtil.deleteVoicemail(
356 this, mVoicemailUri, mCallLogAsyncTaskListener);
357 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700358 }
Andrew Lee2f05af32015-06-09 15:59:47 -0700359 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700360 }
361
Chiao Cheng94b10b52012-08-17 16:59:12 -0700362 private void closeSystemDialogs() {
363 sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
364 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700365}