194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/*
294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Copyright (C) 2009 The Android Open Source Project
394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * you may not use this file except in compliance with the License.
694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * You may obtain a copy of the License at
794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
1094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Unless required by applicable law or agreed to in writing, software
1194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
1294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * See the License for the specific language governing permissions and
1494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * limitations under the License.
1594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
1694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
1794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengpackage com.android.dialer;
1894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
19beab3bbc2c977a48e3affc3e61cc2fdbb22c4ae2Brian Attwellimport android.app.Activity;
2094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.ContentResolver;
2194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.ContentUris;
2294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Context;
2394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Intent;
2494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.res.Resources;
2594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.net.Uri;
2694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.Bundle;
277151398929e487f7f74b285f3e4c478f95daa7c6Yorke Leeimport android.os.PowerManager;
2894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.CommonDataKinds.Phone;
2994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.VoicemailContract.Voicemails;
309dc924c8bcc0bc8d996452e9ce3215b5f064962eTyler Gunnimport android.telecom.PhoneAccount;
31e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chenimport android.telecom.PhoneAccountHandle;
3294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.telephony.TelephonyManager;
3399e8adb92ce3dce67a6e3456de37dded27873c5eAndrew Leeimport android.text.BidiFormatter;
3499e8adb92ce3dce67a6e3456de37dded27873c5eAndrew Leeimport android.text.TextDirectionHeuristics;
3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.text.TextUtils;
3694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.util.Log;
3794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.KeyEvent;
3894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.LayoutInflater;
3994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.Menu;
4094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.MenuItem;
4194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View;
42257b6f27744dc528da719beb1a6d29cd9f72ea31Yorke Leeimport android.widget.LinearLayout;
4394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.ListView;
4418164c85000ad4049b787051b3ab472d2901c586Tyler Gunnimport android.widget.QuickContactBadge;
4594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.TextView;
4694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.Toast;
4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
4835071c06d1587942f5a66c8f12e6247e8f904d26Chiao Chengimport com.android.contacts.common.ContactPhotoManager;
4956cb0efa5eda1670077e66fc0e8c79478d0c1c67Yorke Leeimport com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
502a613022dbb278f0f7dd94cf49885cde9d36100cYorke Leeimport com.android.contacts.common.util.PermissionsUtil;
5135071c06d1587942f5a66c8f12e6247e8f904d26Chiao Chengimport com.android.contacts.common.GeoUtil;
528204324a69c4d6b1b95351ab2863b5ddac6fb0a5Brian Attwellimport com.android.contacts.common.CallUtil;
5318b4a2e3b8d60df39b3489c76bf4edffb81e15a5Tyler Gunnimport com.android.contacts.common.util.UriUtils;
5494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.dialer.calllog.CallDetailHistoryAdapter;
557c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Leeimport com.android.dialer.calllog.CallLogAsyncTaskUtil.CallLogAsyncTaskListener;
567c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Leeimport com.android.dialer.calllog.CallLogAsyncTaskUtil;
5794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.dialer.calllog.CallTypeHelper;
5894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.dialer.calllog.ContactInfo;
5994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.dialer.calllog.ContactInfoHelper;
60b2eebafbc1f1e26b5178b2858ca335467b4bc341Nancy Chenimport com.android.dialer.calllog.PhoneAccountUtils;
6149efd91e50a11dc7bdef8382a0ceac01bc060f77Andrew Leeimport com.android.dialer.calllog.PhoneNumberDisplayUtil;
623921359f3f01938768f0b0e731941542f0385787Yorke Leeimport com.android.dialer.util.DialerUtils;
63d3f6a6c56dec7d47bae1121ed1030f67c26736feAndrew Leeimport com.android.dialer.util.IntentUtil;
64d3f6a6c56dec7d47bae1121ed1030f67c26736feAndrew Leeimport com.android.dialer.util.PhoneNumberUtil;
654aece9536ed64da965fd42da07a393f935abd6b1Yorke Leeimport com.android.dialer.util.TelecomUtil;
6694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
6794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport java.util.List;
6894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
6994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/**
7094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Displays the details of a specific call log entry.
7194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * <p>
7294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * This activity can be either started with the URI of a single call log entry, or with the
7394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
7494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
752f05af35af03de67c1c474cd148719b24fac3552Andrew Leepublic class CallDetailActivity extends Activity
762f05af35af03de67c1c474cd148719b24fac3552Andrew Lee        implements MenuItem.OnMenuItemClickListener {
7794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String TAG = "CallDetail";
7894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
797c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee     /** A long array extra containing ids of call log entries to display. */
8094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
8194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** If we are started with a voicemail, we'll find the uri to play with this extra. */
8294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
8394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** If the activity was triggered from a notification. */
8494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
8594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
868cd9423bd04584acbcbf178bf6a1c1953debb8daYorke Lee    public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment";
878cd9423bd04584acbcbf178bf6a1c1953debb8daYorke Lee
887c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee    private CallLogAsyncTaskListener mCallLogAsyncTaskListener = new CallLogAsyncTaskListener() {
897c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        @Override
907c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        public void onDeleteCall() {
917c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            finish();
927c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        }
937c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
947c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        @Override
957c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        public void onDeleteVoicemail() {
967c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            finish();
977c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        }
987c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
997c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        @Override
1007c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        public void onGetCallDetails(PhoneCallDetails[] details) {
1017c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            if (details == null) {
1027c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                // Somewhere went wrong: we're going to bail out and show error to users.
103e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee                Toast.makeText(mContext, R.string.toast_call_detail_error,
1047c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                        Toast.LENGTH_SHORT).show();
1057c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                finish();
1067c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                return;
1077c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            }
1087c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1097c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            // We know that all calls are from the same number and the same contact, so pick the
1107c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            // first.
1117c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            PhoneCallDetails firstDetails = details[0];
1127c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            mNumber = TextUtils.isEmpty(firstDetails.number) ?
1137c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                    null : firstDetails.number.toString();
1147c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final int numberPresentation = firstDetails.numberPresentation;
1157c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final Uri contactUri = firstDetails.contactUri;
1167c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final Uri photoUri = firstDetails.photoUri;
1177c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final PhoneAccountHandle accountHandle = firstDetails.accountHandle;
1187c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1197c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            // Cache the details about the phone number.
1207c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final boolean canPlaceCallsTo =
121d3f6a6c56dec7d47bae1121ed1030f67c26736feAndrew Lee                    PhoneNumberUtil.canPlaceCallsTo(mNumber, numberPresentation);
122e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee            mIsVoicemailNumber =
123d3f6a6c56dec7d47bae1121ed1030f67c26736feAndrew Lee                    PhoneNumberUtil.isVoicemailNumber(mContext, accountHandle, mNumber);
124d3f6a6c56dec7d47bae1121ed1030f67c26736feAndrew Lee            final boolean isSipNumber = PhoneNumberUtil.isSipNumber(mNumber);
1257c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1267c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails);
1277c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1287c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final CharSequence displayNumber = firstDetails.displayNumber;
1297c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final String displayNumberStr = mBidiFormatter.unicodeWrap(
1307c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                    displayNumber.toString(), TextDirectionHeuristics.LTR);
1317c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1327c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            if (!TextUtils.isEmpty(firstDetails.name)) {
1337c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                mCallerName.setText(firstDetails.name);
1347c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                mCallerNumber.setText(callLocationOrType + " " + displayNumberStr);
1357c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            } else {
1367c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                mCallerName.setText(displayNumberStr);
1377c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                if (!TextUtils.isEmpty(callLocationOrType)) {
1387c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                    mCallerNumber.setText(callLocationOrType);
1397c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                    mCallerNumber.setVisibility(View.VISIBLE);
1407c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                } else {
1417c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                    mCallerNumber.setVisibility(View.GONE);
1427c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                }
1437c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            }
1447c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
145e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee            mCallButton.setVisibility(canPlaceCallsTo ? View.VISIBLE : View.GONE);
146e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee
147e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee            String accountLabel = PhoneAccountUtils.getAccountLabel(mContext, accountHandle);
1487c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            if (!TextUtils.isEmpty(accountLabel)) {
1497c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                mAccountLabel.setText(accountLabel);
1507c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                mAccountLabel.setVisibility(View.VISIBLE);
1517c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            } else {
1527c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                mAccountLabel.setVisibility(View.GONE);
1537c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            }
1547c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1557c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            mHasEditNumberBeforeCallOption =
156e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee                    canPlaceCallsTo && !isSipNumber && !mIsVoicemailNumber;
1572f05af35af03de67c1c474cd148719b24fac3552Andrew Lee            mHasReportMenuOption = mContactInfoHelper.canReportAsInvalid(
1582f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                    firstDetails.sourceType, firstDetails.objectId);
1597c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            invalidateOptionsMenu();
1607c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1617c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            ListView historyList = (ListView) findViewById(R.id.history);
1627c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            historyList.setAdapter(
163e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee                    new CallDetailHistoryAdapter(mContext, mInflater, mCallTypeHelper, details));
1647c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1657c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            String lookupKey = contactUri == null ? null
16618b4a2e3b8d60df39b3489c76bf4edffb81e15a5Tyler Gunn                    : UriUtils.getLookupKeyFromUri(contactUri);
1677c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1687c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
1697c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1707c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            final int contactType =
171e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee                    mIsVoicemailNumber ? ContactPhotoManager.TYPE_VOICEMAIL :
1727c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                    isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
1737c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                    ContactPhotoManager.TYPE_DEFAULT;
1747c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1757c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            String nameForDefaultImage;
1767c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            if (TextUtils.isEmpty(firstDetails.name)) {
1777c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                nameForDefaultImage = firstDetails.displayNumber;
1787c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            } else {
1797c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                nameForDefaultImage = firstDetails.name.toString();
1807c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            }
1817c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1827c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            loadContactPhotos(
1837c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                    contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
1847c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
1857c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        }
1867c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
1877c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        /**
1887c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee         * Determines the location geocode text for a call, or the phone number type
1897c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee         * (if available).
1907c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee         *
1917c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee         * @param details The call details.
1927c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee         * @return The phone number type or location.
1937c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee         */
1947c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) {
1957c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            if (!TextUtils.isEmpty(details.name)) {
1967c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                return Phone.getTypeLabel(mResources, details.numberType,
1977c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                        details.numberLabel);
1987c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            } else {
1997c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee                return details.geocode;
2007c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee            }
2017c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        }
2027c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee    };
2037c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
204e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee    private Context mContext;
20594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private CallTypeHelper mCallTypeHelper;
20618164c85000ad4049b787051b3ab472d2901c586Tyler Gunn    private QuickContactBadge mQuickContactBadge;
20718164c85000ad4049b787051b3ab472d2901c586Tyler Gunn    private TextView mCallerName;
20818164c85000ad4049b787051b3ab472d2901c586Tyler Gunn    private TextView mCallerNumber;
2091970263aad3e26c0e36dbe3783bef5d9f0ff29f0Nancy Chen    private TextView mAccountLabel;
210e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee    private View mCallButton;
21194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private ContactInfoHelper mContactInfoHelper;
21294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
2132f05af35af03de67c1c474cd148719b24fac3552Andrew Lee    protected String mNumber;
214e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee    private boolean mIsVoicemailNumber;
21594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private String mDefaultCountryIso;
21694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
21794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /* package */ LayoutInflater mInflater;
21894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /* package */ Resources mResources;
21994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Helper to load contact photos. */
22094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private ContactPhotoManager mContactPhotoManager;
221c1167e9a05c0578d483d7cd87302a8746e714df0Andrew Lee
222257b6f27744dc528da719beb1a6d29cd9f72ea31Yorke Lee    private Uri mVoicemailUri;
22399e8adb92ce3dce67a6e3456de37dded27873c5eAndrew Lee    private BidiFormatter mBidiFormatter = BidiFormatter.getInstance();
22494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
22594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Whether we should show "edit number before call" in the options menu. */
22694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean mHasEditNumberBeforeCallOption;
2272f05af35af03de67c1c474cd148719b24fac3552Andrew Lee    private boolean mHasReportMenuOption;
22894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
22994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
23094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    protected void onCreate(Bundle icicle) {
23194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        super.onCreate(icicle);
23294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
233e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee        mContext = this;
234e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee
23594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setContentView(R.layout.call_detail);
23694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
23794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
23894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mResources = getResources();
23994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
24094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mCallTypeHelper = new CallTypeHelper(getResources());
241257b6f27744dc528da719beb1a6d29cd9f72ea31Yorke Lee
242257b6f27744dc528da719beb1a6d29cd9f72ea31Yorke Lee        mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
243257b6f27744dc528da719beb1a6d29cd9f72ea31Yorke Lee
24418164c85000ad4049b787051b3ab472d2901c586Tyler Gunn        mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo);
24518164c85000ad4049b787051b3ab472d2901c586Tyler Gunn        mQuickContactBadge.setOverlay(null);
24674eb4ecc5df272f681b427760d46deafce8ed0b2Brian Attwell        mQuickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE);
24718164c85000ad4049b787051b3ab472d2901c586Tyler Gunn        mCallerName = (TextView) findViewById(R.id.caller_name);
24818164c85000ad4049b787051b3ab472d2901c586Tyler Gunn        mCallerNumber = (TextView) findViewById(R.id.caller_number);
2491970263aad3e26c0e36dbe3783bef5d9f0ff29f0Nancy Chen        mAccountLabel = (TextView) findViewById(R.id.phone_account_label);
25035071c06d1587942f5a66c8f12e6247e8f904d26Chiao Cheng        mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
25194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mContactPhotoManager = ContactPhotoManager.getInstance(this);
2527151398929e487f7f74b285f3e4c478f95daa7c6Yorke Lee
253e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee        mCallButton = (View) findViewById(R.id.call_back_button);
254e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee        mCallButton.setOnClickListener(new View.OnClickListener() {
255e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee            @Override
256e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee            public void onClick(View view) {
257e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee                mContext.startActivity(IntentUtil.getCallIntent(mNumber));
258e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee            }
259e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee        });
260e177fe610d3f9725d6b7c66bfe25823efcd15fe6Andrew Lee
26135071c06d1587942f5a66c8f12e6247e8f904d26Chiao Cheng        mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
262adb742ca30da01a3f09be5551f0844f170eaa5d8Chiao Cheng        getActionBar().setDisplayHomeAsUpEnabled(true);
263257b6f27744dc528da719beb1a6d29cd9f72ea31Yorke Lee
26494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
26594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            closeSystemDialogs();
26694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
26794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
26894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
26994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
27094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onResume() {
27194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        super.onResume();
2722f05af35af03de67c1c474cd148719b24fac3552Andrew Lee        getCallDetails();
2732f05af35af03de67c1c474cd148719b24fac3552Andrew Lee    }
2747c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee
2752f05af35af03de67c1c474cd148719b24fac3552Andrew Lee    public void getCallDetails() {
2767c461dc3809ce0d4d969a7c9b6406a1c6e4a1495Andrew Lee        CallLogAsyncTaskUtil.getCallDetails(this, getCallLogEntryUris(), mCallLogAsyncTaskListener);
27794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
27894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
27994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean hasVoicemail() {
280257b6f27744dc528da719beb1a6d29cd9f72ea31Yorke Lee        return mVoicemailUri != null;
28194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
28294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
28394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
28494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns the list of URIs to show.
28594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * <p>
28694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
28794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * a list of ids in the call log added as an extra on the URI.
28894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * <p>
28994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * If both are available, the data on the intent takes precedence.
29094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
29194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private Uri[] getCallLogEntryUris() {
292757bdd39b79c38f1f7713aa16db95d8a8f5dc6daJay Shrauner        final Uri uri = getIntent().getData();
29394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (uri != null) {
29494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            // If there is a data on the intent, it takes precedence over the extra.
29594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return new Uri[]{ uri };
29694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
297757bdd39b79c38f1f7713aa16db95d8a8f5dc6daJay Shrauner        final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
298757bdd39b79c38f1f7713aa16db95d8a8f5dc6daJay Shrauner        final int numIds = ids == null ? 0 : ids.length;
299757bdd39b79c38f1f7713aa16db95d8a8f5dc6daJay Shrauner        final Uri[] uris = new Uri[numIds];
300757bdd39b79c38f1f7713aa16db95d8a8f5dc6daJay Shrauner        for (int index = 0; index < numIds; ++index) {
3014aece9536ed64da965fd42da07a393f935abd6b1Yorke Lee            uris[index] = ContentUris.withAppendedId(
3024aece9536ed64da965fd42da07a393f935abd6b1Yorke Lee                    TelecomUtil.getCallLogUri(CallDetailActivity.this), ids[index]);
30394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
30494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return uris;
30594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
30694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
30794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Load the contact photos and places them in the corresponding views. */
30818164c85000ad4049b787051b3ab472d2901c586Tyler Gunn    private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
30918164c85000ad4049b787051b3ab472d2901c586Tyler Gunn            String lookupKey, int contactType) {
31018164c85000ad4049b787051b3ab472d2901c586Tyler Gunn
31156cb0efa5eda1670077e66fc0e8c79478d0c1c67Yorke Lee        final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
31218164c85000ad4049b787051b3ab472d2901c586Tyler Gunn                contactType, true /* isCircular */);
31318164c85000ad4049b787051b3ab472d2901c586Tyler Gunn
31418164c85000ad4049b787051b3ab472d2901c586Tyler Gunn        mQuickContactBadge.assignContactUri(contactUri);
31518164c85000ad4049b787051b3ab472d2901c586Tyler Gunn        mQuickContactBadge.setContentDescription(
31618164c85000ad4049b787051b3ab472d2901c586Tyler Gunn                mResources.getString(R.string.description_contact_details, displayName));
31718164c85000ad4049b787051b3ab472d2901c586Tyler Gunn
31818164c85000ad4049b787051b3ab472d2901c586Tyler Gunn        mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
31918164c85000ad4049b787051b3ab472d2901c586Tyler Gunn                false /* darkTheme */, true /* isCircular */, request);
32094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
32194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
32294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
32394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public boolean onCreateOptionsMenu(Menu menu) {
32494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        getMenuInflater().inflate(R.menu.call_details_options, menu);
32594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return super.onCreateOptionsMenu(menu);
32694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
32794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
32894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
32994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public boolean onPrepareOptionsMenu(Menu menu) {
33094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // This action deletes all elements in the group from the call log.
33194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // We don't have this action for voicemails, because you can just use the trash button.
3322f05af35af03de67c1c474cd148719b24fac3552Andrew Lee        menu.findItem(R.id.menu_remove_from_call_log)
3332f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                .setVisible(!hasVoicemail())
3342f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                .setOnMenuItemClickListener(this);
3352f05af35af03de67c1c474cd148719b24fac3552Andrew Lee        menu.findItem(R.id.menu_edit_number_before_call)
3362f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                .setVisible(mHasEditNumberBeforeCallOption)
3372f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                .setOnMenuItemClickListener(this);
3382f05af35af03de67c1c474cd148719b24fac3552Andrew Lee        menu.findItem(R.id.menu_trash)
3392f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                .setVisible(hasVoicemail())
3402f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                .setOnMenuItemClickListener(this);
3412f05af35af03de67c1c474cd148719b24fac3552Andrew Lee        menu.findItem(R.id.menu_report)
3422f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                .setVisible(mHasReportMenuOption)
3432f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                .setOnMenuItemClickListener(this);
34494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return super.onPrepareOptionsMenu(menu);
34594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
34694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
3472f05af35af03de67c1c474cd148719b24fac3552Andrew Lee    @Override
3482f05af35af03de67c1c474cd148719b24fac3552Andrew Lee    public boolean onMenuItemClick(MenuItem item) {
3492f05af35af03de67c1c474cd148719b24fac3552Andrew Lee        switch (item.getItemId()) {
3502f05af35af03de67c1c474cd148719b24fac3552Andrew Lee            case R.id.menu_remove_from_call_log:
3512f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                final StringBuilder callIds = new StringBuilder();
3522f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                for (Uri callUri : getCallLogEntryUris()) {
3532f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                    if (callIds.length() != 0) {
3542f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                        callIds.append(",");
3552f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                    }
3562f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                    callIds.append(ContentUris.parseId(callUri));
3572f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                }
3582f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                CallLogAsyncTaskUtil.deleteCalls(
3592f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                        this, callIds.toString(), mCallLogAsyncTaskListener);
3602f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                break;
3612f05af35af03de67c1c474cd148719b24fac3552Andrew Lee            case R.id.menu_edit_number_before_call:
3622f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
3632f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                break;
3642f05af35af03de67c1c474cd148719b24fac3552Andrew Lee            case R.id.menu_trash:
3652f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                CallLogAsyncTaskUtil.deleteVoicemail(
3662f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                        this, mVoicemailUri, mCallLogAsyncTaskListener);
3672f05af35af03de67c1c474cd148719b24fac3552Andrew Lee                break;
36894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
3692f05af35af03de67c1c474cd148719b24fac3552Andrew Lee        return true;
37094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
37194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
37294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void closeSystemDialogs() {
37394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
37494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
37594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng}
376