CallDetailActivity.java revision 3921359f3f01938768f0b0e731941542f0385787
1/*
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
19import android.app.Activity;
20import android.app.LoaderManager.LoaderCallbacks;
21import android.content.ActivityNotFoundException;
22import android.content.ContentResolver;
23import android.content.ContentUris;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
27import android.content.Loader;
28import android.content.res.Resources;
29import android.database.Cursor;
30import android.graphics.drawable.Drawable;
31import android.net.Uri;
32import android.os.AsyncTask;
33import android.os.Bundle;
34import android.provider.CallLog;
35import android.provider.ContactsContract;
36import android.provider.CallLog.Calls;
37import android.provider.ContactsContract.CommonDataKinds.Phone;
38import android.provider.ContactsContract.Contacts;
39import android.provider.ContactsContract.DisplayNameSources;
40import android.provider.ContactsContract.Intents.Insert;
41import android.provider.VoicemailContract.Voicemails;
42import android.telephony.TelephonyManager;
43import android.text.TextUtils;
44import android.util.Log;
45import android.view.ActionMode;
46import android.view.KeyEvent;
47import android.view.LayoutInflater;
48import android.view.Menu;
49import android.view.MenuItem;
50import android.view.View;
51import android.widget.ImageButton;
52import android.widget.ImageView;
53import android.widget.ListView;
54import android.widget.TextView;
55import android.widget.Toast;
56
57import com.android.contacts.common.ContactPhotoManager;
58import com.android.contacts.common.CallUtil;
59import com.android.contacts.common.ClipboardUtils;
60import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
61import com.android.contacts.common.GeoUtil;
62import com.android.contacts.common.model.Contact;
63import com.android.contacts.common.model.ContactLoader;
64import com.android.contacts.common.util.PhoneNumberHelper;
65import com.android.contacts.common.util.UriUtils;
66import com.android.dialer.BackScrollManager.ScrollableHeader;
67import com.android.dialer.calllog.CallDetailHistoryAdapter;
68import com.android.dialer.calllog.CallTypeHelper;
69import com.android.dialer.calllog.ContactInfo;
70import com.android.dialer.calllog.ContactInfoHelper;
71import com.android.dialer.calllog.PhoneNumberDisplayHelper;
72import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
73import com.android.dialer.util.AsyncTaskExecutor;
74import com.android.dialer.util.AsyncTaskExecutors;
75import com.android.dialer.util.DialerUtils;
76import com.android.dialer.voicemail.VoicemailPlaybackFragment;
77import com.android.dialer.voicemail.VoicemailStatusHelper;
78import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage;
79import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
80
81import java.util.List;
82
83/**
84 * Displays the details of a specific call log entry.
85 * <p>
86 * This activity can be either started with the URI of a single call log entry, or with the
87 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
88 */
89public class CallDetailActivity extends Activity implements ProximitySensorAware {
90    private static final String TAG = "CallDetail";
91
92    private static final int LOADER_ID = 0;
93    private static final String BUNDLE_CONTACT_URI_EXTRA = "contact_uri_extra";
94
95    private static final char LEFT_TO_RIGHT_EMBEDDING = '\u202A';
96    private static final char POP_DIRECTIONAL_FORMATTING = '\u202C';
97
98    /** The time to wait before enabling the blank the screen due to the proximity sensor. */
99    private static final long PROXIMITY_BLANK_DELAY_MILLIS = 100;
100    /** The time to wait before disabling the blank the screen due to the proximity sensor. */
101    private static final long PROXIMITY_UNBLANK_DELAY_MILLIS = 500;
102
103    /** The enumeration of {@link AsyncTask} objects used in this class. */
104    public enum Tasks {
105        MARK_VOICEMAIL_READ,
106        DELETE_VOICEMAIL_AND_FINISH,
107        REMOVE_FROM_CALL_LOG_AND_FINISH,
108        UPDATE_PHONE_CALL_DETAILS,
109    }
110
111    /** A long array extra containing ids of call log entries to display. */
112    public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
113    /** If we are started with a voicemail, we'll find the uri to play with this extra. */
114    public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
115    /** If we should immediately start playback of the voicemail, this extra will be set to true. */
116    public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
117    /** If the activity was triggered from a notification. */
118    public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
119
120    private CallTypeHelper mCallTypeHelper;
121    private PhoneNumberDisplayHelper mPhoneNumberHelper;
122    private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
123    private TextView mHeaderTextView;
124    private View mHeaderOverlayView;
125    private ImageView mMainActionView;
126    private ImageButton mMainActionPushLayerView;
127    private ImageView mContactBackgroundView;
128    private AsyncTaskExecutor mAsyncTaskExecutor;
129    private ContactInfoHelper mContactInfoHelper;
130
131    private String mNumber = null;
132    private String mDefaultCountryIso;
133
134    /* package */ LayoutInflater mInflater;
135    /* package */ Resources mResources;
136    /** Helper to load contact photos. */
137    private ContactPhotoManager mContactPhotoManager;
138    /** Helper to make async queries to content resolver. */
139    private CallDetailActivityQueryHandler mAsyncQueryHandler;
140    /** Helper to get voicemail status messages. */
141    private VoicemailStatusHelper mVoicemailStatusHelper;
142    // Views related to voicemail status message.
143    private View mStatusMessageView;
144    private TextView mStatusMessageText;
145    private TextView mStatusMessageAction;
146
147    /** Whether we should show "edit number before call" in the options menu. */
148    private boolean mHasEditNumberBeforeCallOption;
149    /** Whether we should show "trash" in the options menu. */
150    private boolean mHasTrashOption;
151    /** Whether we should show "remove from call log" in the options menu. */
152    private boolean mHasRemoveFromCallLogOption;
153
154    private ProximitySensorManager mProximitySensorManager;
155    private final ProximitySensorListener mProximitySensorListener = new ProximitySensorListener();
156
157    /**
158     * The action mode used when the phone number is selected.  This will be non-null only when the
159     * phone number is selected.
160     */
161    private ActionMode mPhoneNumberActionMode;
162
163    private CharSequence mPhoneNumberLabelToCopy;
164    private CharSequence mPhoneNumberToCopy;
165
166    /** Listener to changes in the proximity sensor state. */
167    private class ProximitySensorListener implements ProximitySensorManager.Listener {
168        /** Used to show a blank view and hide the action bar. */
169        private final Runnable mBlankRunnable = new Runnable() {
170            @Override
171            public void run() {
172                View blankView = findViewById(R.id.blank);
173                blankView.setVisibility(View.VISIBLE);
174                getActionBar().hide();
175            }
176        };
177        /** Used to remove the blank view and show the action bar. */
178        private final Runnable mUnblankRunnable = new Runnable() {
179            @Override
180            public void run() {
181                View blankView = findViewById(R.id.blank);
182                blankView.setVisibility(View.GONE);
183                getActionBar().show();
184            }
185        };
186
187        @Override
188        public synchronized void onNear() {
189            clearPendingRequests();
190            postDelayed(mBlankRunnable, PROXIMITY_BLANK_DELAY_MILLIS);
191        }
192
193        @Override
194        public synchronized void onFar() {
195            clearPendingRequests();
196            postDelayed(mUnblankRunnable, PROXIMITY_UNBLANK_DELAY_MILLIS);
197        }
198
199        /** Removed any delayed requests that may be pending. */
200        public synchronized void clearPendingRequests() {
201            View blankView = findViewById(R.id.blank);
202            blankView.removeCallbacks(mBlankRunnable);
203            blankView.removeCallbacks(mUnblankRunnable);
204        }
205
206        /** Post a {@link Runnable} with a delay on the main thread. */
207        private synchronized void postDelayed(Runnable runnable, long delayMillis) {
208            // Post these instead of executing immediately so that:
209            // - They are guaranteed to be executed on the main thread.
210            // - If the sensor values changes rapidly for some time, the UI will not be
211            //   updated immediately.
212            View blankView = findViewById(R.id.blank);
213            blankView.postDelayed(runnable, delayMillis);
214        }
215    }
216
217    static final String[] CALL_LOG_PROJECTION = new String[] {
218        CallLog.Calls.DATE,
219        CallLog.Calls.DURATION,
220        CallLog.Calls.NUMBER,
221        CallLog.Calls.TYPE,
222        CallLog.Calls.COUNTRY_ISO,
223        CallLog.Calls.GEOCODED_LOCATION,
224        CallLog.Calls.NUMBER_PRESENTATION,
225    };
226
227    static final int DATE_COLUMN_INDEX = 0;
228    static final int DURATION_COLUMN_INDEX = 1;
229    static final int NUMBER_COLUMN_INDEX = 2;
230    static final int CALL_TYPE_COLUMN_INDEX = 3;
231    static final int COUNTRY_ISO_COLUMN_INDEX = 4;
232    static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
233    static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6;
234
235    private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
236        @Override
237        public void onClick(View view) {
238            if (finishPhoneNumerSelectedActionModeIfShown()) {
239                return;
240            }
241            DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
242                    ((ViewEntry) view.getTag()).primaryIntent);
243        }
244    };
245
246    private final View.OnClickListener mSecondaryActionListener = new View.OnClickListener() {
247        @Override
248        public void onClick(View view) {
249            if (finishPhoneNumerSelectedActionModeIfShown()) {
250                return;
251            }
252            DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
253                    ((ViewEntry) view.getTag()).secondaryIntent);
254        }
255    };
256
257    private final View.OnLongClickListener mPrimaryLongClickListener =
258            new View.OnLongClickListener() {
259        @Override
260        public boolean onLongClick(View v) {
261            if (finishPhoneNumerSelectedActionModeIfShown()) {
262                return true;
263            }
264            startPhoneNumberSelectedActionMode(v);
265            return true;
266        }
267    };
268
269    private final LoaderCallbacks<Contact> mLoaderCallbacks = new LoaderCallbacks<Contact>() {
270        @Override
271        public void onLoaderReset(Loader<Contact> loader) {
272        }
273
274        @Override
275        public void onLoadFinished(Loader<Contact> loader, Contact data) {
276            final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
277            intent.setType(Contacts.CONTENT_ITEM_TYPE);
278            if (data.getDisplayNameSource() >= DisplayNameSources.ORGANIZATION) {
279                intent.putExtra(Insert.NAME, data.getDisplayName());
280            }
281            intent.putExtra(Insert.DATA, data.getContentValues());
282            bindContactPhotoAction(intent, R.drawable.ic_add_contact_holo_dark,
283                    getString(R.string.description_add_contact));
284        }
285
286        @Override
287        public Loader<Contact> onCreateLoader(int id, Bundle args) {
288            final Uri contactUri = args.getParcelable(BUNDLE_CONTACT_URI_EXTRA);
289            if (contactUri == null) {
290                Log.wtf(TAG, "No contact lookup uri provided.");
291            }
292            return new ContactLoader(CallDetailActivity.this, contactUri,
293                    false /* loadGroupMetaData */, false /* loadInvitableAccountTypes */,
294                    false /* postViewNotification */, true /* computeFormattedPhoneNumber */);
295        }
296    };
297
298    @Override
299    protected void onCreate(Bundle icicle) {
300        super.onCreate(icicle);
301
302        setContentView(R.layout.call_detail);
303
304        mAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
305        mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
306        mResources = getResources();
307
308        mCallTypeHelper = new CallTypeHelper(getResources());
309        mPhoneNumberHelper = new PhoneNumberDisplayHelper(mResources);
310        mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
311                new PhoneNumberUtilsWrapper());
312        mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
313        mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
314        mHeaderTextView = (TextView) findViewById(R.id.header_text);
315        mHeaderOverlayView = findViewById(R.id.photo_text_bar);
316        mStatusMessageView = findViewById(R.id.voicemail_status);
317        mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
318        mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
319        mMainActionView = (ImageView) findViewById(R.id.main_action);
320        mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
321        mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
322        mDefaultCountryIso = GeoUtil.getCurrentCountryIso(this);
323        mContactPhotoManager = ContactPhotoManager.getInstance(this);
324        mProximitySensorManager = new ProximitySensorManager(this, mProximitySensorListener);
325        mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this));
326        getActionBar().setDisplayHomeAsUpEnabled(true);
327        optionallyHandleVoicemail();
328        if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) {
329            closeSystemDialogs();
330        }
331    }
332
333    @Override
334    public void onResume() {
335        super.onResume();
336        updateData(getCallLogEntryUris());
337    }
338
339    /**
340     * Handle voicemail playback or hide voicemail ui.
341     * <p>
342     * If the Intent used to start this Activity contains the suitable extras, then start voicemail
343     * playback.  If it doesn't, then hide the voicemail ui.
344     */
345    private void optionallyHandleVoicemail() {
346        View voicemailContainer = findViewById(R.id.voicemail_container);
347        if (hasVoicemail()) {
348            // Has voicemail: add the voicemail fragment.  Add suitable arguments to set the uri
349            // to play and optionally start the playback.
350            // Do a query to fetch the voicemail status messages.
351            VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
352            Bundle fragmentArguments = new Bundle();
353            fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
354            if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
355                fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
356            }
357            playbackFragment.setArguments(fragmentArguments);
358            voicemailContainer.setVisibility(View.VISIBLE);
359            getFragmentManager().beginTransaction()
360                    .add(R.id.voicemail_container, playbackFragment)
361                    .commitAllowingStateLoss();
362            mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
363            markVoicemailAsRead(getVoicemailUri());
364        } else {
365            // No voicemail uri: hide the status view.
366            mStatusMessageView.setVisibility(View.GONE);
367            voicemailContainer.setVisibility(View.GONE);
368        }
369    }
370
371    private boolean hasVoicemail() {
372        return getVoicemailUri() != null;
373    }
374
375    private Uri getVoicemailUri() {
376        return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
377    }
378
379    private void markVoicemailAsRead(final Uri voicemailUri) {
380        mAsyncTaskExecutor.submit(Tasks.MARK_VOICEMAIL_READ, new AsyncTask<Void, Void, Void>() {
381            @Override
382            public Void doInBackground(Void... params) {
383                ContentValues values = new ContentValues();
384                values.put(Voicemails.IS_READ, true);
385                getContentResolver().update(voicemailUri, values,
386                        Voicemails.IS_READ + " = 0", null);
387                return null;
388            }
389        });
390    }
391
392    /**
393     * Returns the list of URIs to show.
394     * <p>
395     * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
396     * a list of ids in the call log added as an extra on the URI.
397     * <p>
398     * If both are available, the data on the intent takes precedence.
399     */
400    private Uri[] getCallLogEntryUris() {
401        Uri uri = getIntent().getData();
402        if (uri != null) {
403            // If there is a data on the intent, it takes precedence over the extra.
404            return new Uri[]{ uri };
405        }
406        long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
407        Uri[] uris = new Uri[ids.length];
408        for (int index = 0; index < ids.length; ++index) {
409            uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
410        }
411        return uris;
412    }
413
414    @Override
415    public boolean onKeyDown(int keyCode, KeyEvent event) {
416        switch (keyCode) {
417            case KeyEvent.KEYCODE_CALL: {
418                // Make sure phone isn't already busy before starting direct call
419                TelephonyManager tm = (TelephonyManager)
420                        getSystemService(Context.TELEPHONY_SERVICE);
421                if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
422                    DialerUtils.startActivityWithErrorToast(this,
423                            CallUtil.getCallIntent(Uri.fromParts(CallUtil.SCHEME_TEL, mNumber,
424                                    null)),
425                            R.string.call_not_available);
426                    return true;
427                }
428            }
429        }
430
431        return super.onKeyDown(keyCode, event);
432    }
433
434    /**
435     * Update user interface with details of given call.
436     *
437     * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
438     */
439    private void updateData(final Uri... callUris) {
440        class UpdateContactDetailsTask extends AsyncTask<Void, Void, PhoneCallDetails[]> {
441            @Override
442            public PhoneCallDetails[] doInBackground(Void... params) {
443                // TODO: All phone calls correspond to the same person, so we can make a single
444                // lookup.
445                final int numCalls = callUris.length;
446                PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
447                try {
448                    for (int index = 0; index < numCalls; ++index) {
449                        details[index] = getPhoneCallDetailsForUri(callUris[index]);
450                    }
451                    return details;
452                } catch (IllegalArgumentException e) {
453                    // Something went wrong reading in our primary data.
454                    Log.w(TAG, "invalid URI starting call details", e);
455                    return null;
456                }
457            }
458
459            @Override
460            public void onPostExecute(PhoneCallDetails[] details) {
461                if (details == null) {
462                    // Somewhere went wrong: we're going to bail out and show error to users.
463                    Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
464                            Toast.LENGTH_SHORT).show();
465                    finish();
466                    return;
467                }
468
469                // We know that all calls are from the same number and the same contact, so pick the
470                // first.
471                PhoneCallDetails firstDetails = details[0];
472                mNumber = firstDetails.number.toString();
473                final int numberPresentation = firstDetails.numberPresentation;
474                final Uri contactUri = firstDetails.contactUri;
475                final Uri photoUri = firstDetails.photoUri;
476
477                // Set the details header, based on the first phone call.
478                mPhoneCallDetailsHelper.setCallDetailsHeader(mHeaderTextView, firstDetails);
479
480                // Cache the details about the phone number.
481                final boolean canPlaceCallsTo =
482                    PhoneNumberUtilsWrapper.canPlaceCallsTo(mNumber, numberPresentation);
483                final PhoneNumberUtilsWrapper phoneUtils = new PhoneNumberUtilsWrapper();
484                final boolean isVoicemailNumber = phoneUtils.isVoicemailNumber(mNumber);
485                final boolean isSipNumber = phoneUtils.isSipNumber(mNumber);
486
487                // Let user view contact details if they exist, otherwise add option to create new
488                // contact from this number.
489                final Intent mainActionIntent;
490                final int mainActionIcon;
491                final String mainActionDescription;
492
493                final CharSequence nameOrNumber;
494                if (!TextUtils.isEmpty(firstDetails.name)) {
495                    nameOrNumber = firstDetails.name;
496                } else {
497                    nameOrNumber = firstDetails.number;
498                }
499
500                boolean skipBind = false;
501
502                if (contactUri != null && !UriUtils.isEncodedContactUri(contactUri)) {
503                    mainActionIntent = new Intent(Intent.ACTION_VIEW, contactUri);
504                    // This will launch People's detail contact screen, so we probably want to
505                    // treat it as a separate People task.
506                    mainActionIntent.setFlags(
507                            Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
508                    mainActionIcon = R.drawable.ic_contacts_holo_dark;
509                    mainActionDescription =
510                            getString(R.string.description_view_contact, nameOrNumber);
511                } else if (UriUtils.isEncodedContactUri(contactUri)) {
512                    final Bundle bundle = new Bundle(1);
513                    bundle.putParcelable(BUNDLE_CONTACT_URI_EXTRA, contactUri);
514                    getLoaderManager().initLoader(LOADER_ID, bundle, mLoaderCallbacks);
515                    mainActionIntent = null;
516                    mainActionIcon = R.drawable.ic_add_contact_holo_dark;
517                    mainActionDescription = getString(R.string.description_add_contact);
518                    skipBind = true;
519                } else if (isVoicemailNumber) {
520                    mainActionIntent = null;
521                    mainActionIcon = 0;
522                    mainActionDescription = null;
523                } else if (isSipNumber) {
524                    // TODO: This item is currently disabled for SIP addresses, because
525                    // the Insert.PHONE extra only works correctly for PSTN numbers.
526                    //
527                    // To fix this for SIP addresses, we need to:
528                    // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
529                    //   the current number is a SIP address
530                    // - update the contacts UI code to handle Insert.SIP_ADDRESS by
531                    //   updating the SipAddress field
532                    // and then we can remove the "!isSipNumber" check above.
533                    mainActionIntent = null;
534                    mainActionIcon = 0;
535                    mainActionDescription = null;
536                } else if (canPlaceCallsTo) {
537                    mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
538                    mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
539                    mainActionIntent.putExtra(Insert.PHONE, mNumber);
540                    mainActionIcon = R.drawable.ic_add_contact_holo_dark;
541                    mainActionDescription = getString(R.string.description_add_contact);
542                } else {
543                    // If we cannot call the number, when we probably cannot add it as a contact
544                    // either. This is usually the case of private, unknown, or payphone numbers.
545                    mainActionIntent = null;
546                    mainActionIcon = 0;
547                    mainActionDescription = null;
548                }
549
550                if (!skipBind) {
551                    bindContactPhotoAction(mainActionIntent, mainActionIcon,
552                            mainActionDescription);
553                }
554
555                final CharSequence displayNumber =
556                        mPhoneNumberHelper.getDisplayNumber(
557                                firstDetails.number,
558                                firstDetails.numberPresentation,
559                                firstDetails.formattedNumber);
560
561                // This action allows to call the number that places the call.
562                if (canPlaceCallsTo) {
563                    ViewEntry entry = new ViewEntry(
564                            getString(R.string.menu_callNumber,
565                                    forceLeftToRight(displayNumber)),
566                                    CallUtil.getCallIntent(mNumber),
567                                    getString(R.string.description_call, nameOrNumber));
568
569                    // Only show a label if the number is shown and it is not a SIP address.
570                    if (!TextUtils.isEmpty(firstDetails.name)
571                            && !TextUtils.isEmpty(firstDetails.number)
572                            && !PhoneNumberHelper.isUriNumber(firstDetails.number.toString())) {
573                        entry.label = Phone.getTypeLabel(mResources, firstDetails.numberType,
574                                firstDetails.numberLabel);
575                    }
576
577                    // The secondary action allows to send an SMS to the number that placed the
578                    // call.
579                    if (phoneUtils.canSendSmsTo(mNumber, numberPresentation)) {
580                        entry.setSecondaryAction(
581                                R.drawable.ic_text_holo_light,
582                                new Intent(Intent.ACTION_SENDTO,
583                                           Uri.fromParts("sms", mNumber, null)),
584                                getString(R.string.description_send_text_message, nameOrNumber));
585                    }
586
587                    configureCallButton(entry);
588                    mPhoneNumberToCopy = displayNumber;
589                    mPhoneNumberLabelToCopy = entry.label;
590                } else {
591                    disableCallButton();
592                    mPhoneNumberToCopy = null;
593                    mPhoneNumberLabelToCopy = null;
594                }
595
596                mHasEditNumberBeforeCallOption =
597                        canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
598                mHasTrashOption = hasVoicemail();
599                mHasRemoveFromCallLogOption = !hasVoicemail();
600                invalidateOptionsMenu();
601
602                ListView historyList = (ListView) findViewById(R.id.history);
603                historyList.setAdapter(
604                        new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
605                                mCallTypeHelper, details, hasVoicemail(), canPlaceCallsTo,
606                                findViewById(R.id.controls)));
607                BackScrollManager.bind(
608                        new ScrollableHeader() {
609                            private View mControls = findViewById(R.id.controls);
610                            private View mPhoto = findViewById(R.id.contact_background_sizer);
611                            private View mHeader = findViewById(R.id.photo_text_bar);
612                        private View mSeparator = findViewById(R.id.separator);
613
614                            @Override
615                            public void setOffset(int offset) {
616                                mControls.setY(-offset);
617                            }
618
619                            @Override
620                            public int getMaximumScrollableHeaderOffset() {
621                                // We can scroll the photo out, but we should keep the header if
622                                // present.
623                                if (mHeader.getVisibility() == View.VISIBLE) {
624                                    return mPhoto.getHeight() - mHeader.getHeight();
625                                } else {
626                                    // If the header is not present, we should also scroll out the
627                                    // separator line.
628                                    return mPhoto.getHeight() + mSeparator.getHeight();
629                                }
630                            }
631                        },
632                        historyList);
633
634                final String displayNameForDefaultImage = TextUtils.isEmpty(firstDetails.name) ?
635                        displayNumber.toString() : firstDetails.name.toString();
636
637                final String lookupKey = ContactInfoHelper.getLookupKeyFromUri(contactUri);
638
639                final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
640
641                final int contactType =
642                        isVoicemailNumber? ContactPhotoManager.TYPE_VOICEMAIL :
643                        isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
644                        ContactPhotoManager.TYPE_DEFAULT;
645
646                loadContactPhotos(photoUri, displayNameForDefaultImage, lookupKey, contactType);
647                findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
648            }
649        }
650        mAsyncTaskExecutor.submit(Tasks.UPDATE_PHONE_CALL_DETAILS, new UpdateContactDetailsTask());
651    }
652
653    private void bindContactPhotoAction(final Intent actionIntent, int actionIcon,
654            String actionDescription) {
655        if (actionIntent == null) {
656            mMainActionView.setVisibility(View.INVISIBLE);
657            mMainActionPushLayerView.setVisibility(View.GONE);
658            mHeaderTextView.setVisibility(View.INVISIBLE);
659            mHeaderOverlayView.setVisibility(View.INVISIBLE);
660        } else {
661            mMainActionView.setVisibility(View.VISIBLE);
662            mMainActionView.setImageResource(actionIcon);
663            mMainActionPushLayerView.setVisibility(View.VISIBLE);
664            mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
665                @Override
666                public void onClick(View v) {
667                    DialerUtils.startActivityWithErrorToast(CallDetailActivity.this, actionIntent,
668                            R.string.add_contact_not_available);
669                }
670            });
671            mMainActionPushLayerView.setContentDescription(actionDescription);
672            mHeaderTextView.setVisibility(View.VISIBLE);
673            mHeaderOverlayView.setVisibility(View.VISIBLE);
674        }
675    }
676
677    /** Return the phone call details for a given call log URI. */
678    private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
679        ContentResolver resolver = getContentResolver();
680        Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
681        try {
682            if (callCursor == null || !callCursor.moveToFirst()) {
683                throw new IllegalArgumentException("Cannot find content: " + callUri);
684            }
685
686            // Read call log specifics.
687            final String number = callCursor.getString(NUMBER_COLUMN_INDEX);
688            final int numberPresentation = callCursor.getInt(
689                    NUMBER_PRESENTATION_COLUMN_INDEX);
690            final long date = callCursor.getLong(DATE_COLUMN_INDEX);
691            final long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
692            final int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
693            String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
694            final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
695
696            if (TextUtils.isEmpty(countryIso)) {
697                countryIso = mDefaultCountryIso;
698            }
699
700            // Formatted phone number.
701            final CharSequence formattedNumber;
702            // Read contact specifics.
703            final CharSequence nameText;
704            final int numberType;
705            final CharSequence numberLabel;
706            final Uri photoUri;
707            final Uri lookupUri;
708            int sourceType;
709            // If this is not a regular number, there is no point in looking it up in the contacts.
710            ContactInfo info =
711                    PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation)
712                    && !new PhoneNumberUtilsWrapper().isVoicemailNumber(number)
713                            ? mContactInfoHelper.lookupNumber(number, countryIso)
714                            : null;
715            if (info == null) {
716                formattedNumber = mPhoneNumberHelper.getDisplayNumber(number,
717                        numberPresentation, null);
718                nameText = "";
719                numberType = 0;
720                numberLabel = "";
721                photoUri = null;
722                lookupUri = null;
723                sourceType = 0;
724            } else {
725                formattedNumber = info.formattedNumber;
726                nameText = info.name;
727                numberType = info.type;
728                numberLabel = info.label;
729                photoUri = info.photoUri;
730                lookupUri = info.lookupUri;
731                sourceType = info.sourceType;
732            }
733            return new PhoneCallDetails(number, numberPresentation,
734                    formattedNumber, countryIso, geocode,
735                    new int[]{ callType }, date, duration,
736                    nameText, numberType, numberLabel, lookupUri, photoUri, sourceType);
737        } finally {
738            if (callCursor != null) {
739                callCursor.close();
740            }
741        }
742    }
743
744    /** Load the contact photos and places them in the corresponding views. */
745    private void loadContactPhotos(Uri photoUri, String displayName, String lookupKey,
746            int contactType) {
747        final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
748                contactType);
749        mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri,
750                mContactBackgroundView.getWidth(), true, request);
751    }
752
753    static final class ViewEntry {
754        public final String text;
755        public final Intent primaryIntent;
756        /** The description for accessibility of the primary action. */
757        public final String primaryDescription;
758
759        public CharSequence label = null;
760        /** Icon for the secondary action. */
761        public int secondaryIcon = 0;
762        /** Intent for the secondary action. If not null, an icon must be defined. */
763        public Intent secondaryIntent = null;
764        /** The description for accessibility of the secondary action. */
765        public String secondaryDescription = null;
766
767        public ViewEntry(String text, Intent intent, String description) {
768            this.text = text;
769            primaryIntent = intent;
770            primaryDescription = description;
771        }
772
773        public void setSecondaryAction(int icon, Intent intent, String description) {
774            secondaryIcon = icon;
775            secondaryIntent = intent;
776            secondaryDescription = description;
777        }
778    }
779
780    /** Disables the call button area, e.g., for private numbers. */
781    private void disableCallButton() {
782        findViewById(R.id.call_and_sms).setVisibility(View.GONE);
783    }
784
785    /** Configures the call button area using the given entry. */
786    private void configureCallButton(ViewEntry entry) {
787        View convertView = findViewById(R.id.call_and_sms);
788        convertView.setVisibility(View.VISIBLE);
789
790        ImageView icon = (ImageView) convertView.findViewById(R.id.call_and_sms_icon);
791        View divider = convertView.findViewById(R.id.call_and_sms_divider);
792        TextView text = (TextView) convertView.findViewById(R.id.call_and_sms_text);
793
794        View mainAction = convertView.findViewById(R.id.call_and_sms_main_action);
795        mainAction.setOnClickListener(mPrimaryActionListener);
796        mainAction.setTag(entry);
797        mainAction.setContentDescription(entry.primaryDescription);
798        mainAction.setOnLongClickListener(mPrimaryLongClickListener);
799
800        if (entry.secondaryIntent != null) {
801            icon.setOnClickListener(mSecondaryActionListener);
802            icon.setImageResource(entry.secondaryIcon);
803            icon.setVisibility(View.VISIBLE);
804            icon.setTag(entry);
805            icon.setContentDescription(entry.secondaryDescription);
806            divider.setVisibility(View.VISIBLE);
807        } else {
808            icon.setVisibility(View.GONE);
809            divider.setVisibility(View.GONE);
810        }
811        text.setText(entry.text);
812
813        TextView label = (TextView) convertView.findViewById(R.id.call_and_sms_label);
814        if (TextUtils.isEmpty(entry.label)) {
815            label.setVisibility(View.GONE);
816        } else {
817            label.setText(entry.label);
818            label.setVisibility(View.VISIBLE);
819        }
820    }
821
822    protected void updateVoicemailStatusMessage(Cursor statusCursor) {
823        if (statusCursor == null) {
824            mStatusMessageView.setVisibility(View.GONE);
825            return;
826        }
827        final StatusMessage message = getStatusMessage(statusCursor);
828        if (message == null || !message.showInCallDetails()) {
829            mStatusMessageView.setVisibility(View.GONE);
830            return;
831        }
832
833        mStatusMessageView.setVisibility(View.VISIBLE);
834        mStatusMessageText.setText(message.callDetailsMessageId);
835        if (message.actionMessageId != -1) {
836            mStatusMessageAction.setText(message.actionMessageId);
837        }
838        if (message.actionUri != null) {
839            mStatusMessageAction.setClickable(true);
840            mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
841                @Override
842                public void onClick(View v) {
843                    DialerUtils.startActivityWithErrorToast(CallDetailActivity.this,
844                            new Intent(Intent.ACTION_VIEW, message.actionUri));
845                }
846            });
847        } else {
848            mStatusMessageAction.setClickable(false);
849        }
850    }
851
852    private StatusMessage getStatusMessage(Cursor statusCursor) {
853        List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
854        if (messages.size() == 0) {
855            return null;
856        }
857        // There can only be a single status message per source package, so num of messages can
858        // at most be 1.
859        if (messages.size() > 1) {
860            Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
861                    " Will use the first one.", messages.size()));
862        }
863        return messages.get(0);
864    }
865
866    @Override
867    public boolean onCreateOptionsMenu(Menu menu) {
868        getMenuInflater().inflate(R.menu.call_details_options, menu);
869        return super.onCreateOptionsMenu(menu);
870    }
871
872    @Override
873    public boolean onPrepareOptionsMenu(Menu menu) {
874        // This action deletes all elements in the group from the call log.
875        // We don't have this action for voicemails, because you can just use the trash button.
876        menu.findItem(R.id.menu_remove_from_call_log).setVisible(mHasRemoveFromCallLogOption);
877        menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCallOption);
878        menu.findItem(R.id.menu_trash).setVisible(mHasTrashOption);
879        return super.onPrepareOptionsMenu(menu);
880    }
881
882    public void onMenuRemoveFromCallLog(MenuItem menuItem) {
883        final StringBuilder callIds = new StringBuilder();
884        for (Uri callUri : getCallLogEntryUris()) {
885            if (callIds.length() != 0) {
886                callIds.append(",");
887            }
888            callIds.append(ContentUris.parseId(callUri));
889        }
890        mAsyncTaskExecutor.submit(Tasks.REMOVE_FROM_CALL_LOG_AND_FINISH,
891                new AsyncTask<Void, Void, Void>() {
892                    @Override
893                    public Void doInBackground(Void... params) {
894                        getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
895                                Calls._ID + " IN (" + callIds + ")", null);
896                        return null;
897                    }
898
899                    @Override
900                    public void onPostExecute(Void result) {
901                        finish();
902                    }
903                });
904    }
905
906    public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
907        startActivity(new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(mNumber)));
908    }
909
910    public void onMenuTrashVoicemail(MenuItem menuItem) {
911        final Uri voicemailUri = getVoicemailUri();
912        mAsyncTaskExecutor.submit(Tasks.DELETE_VOICEMAIL_AND_FINISH,
913                new AsyncTask<Void, Void, Void>() {
914                    @Override
915                    public Void doInBackground(Void... params) {
916                        getContentResolver().delete(voicemailUri, null, null);
917                        return null;
918                    }
919                    @Override
920                    public void onPostExecute(Void result) {
921                        finish();
922                    }
923                });
924    }
925
926    /** Invoked when the user presses the home button in the action bar. */
927    private void onHomeSelected() {
928        Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
929        // This will open the call log even if the detail view has been opened directly.
930        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
931        startActivity(intent);
932        finish();
933    }
934
935    @Override
936    protected void onPause() {
937        // Immediately stop the proximity sensor.
938        disableProximitySensor(false);
939        mProximitySensorListener.clearPendingRequests();
940        super.onPause();
941    }
942
943    @Override
944    public void enableProximitySensor() {
945        mProximitySensorManager.enable();
946    }
947
948    @Override
949    public void disableProximitySensor(boolean waitForFarState) {
950        mProximitySensorManager.disable(waitForFarState);
951    }
952
953    /**
954     * If the phone number is selected, unselect it and return {@code true}.
955     * Otherwise, just {@code false}.
956     */
957    private boolean finishPhoneNumerSelectedActionModeIfShown() {
958        if (mPhoneNumberActionMode == null) return false;
959        mPhoneNumberActionMode.finish();
960        return true;
961    }
962
963    private void startPhoneNumberSelectedActionMode(View targetView) {
964        mPhoneNumberActionMode = startActionMode(new PhoneNumberActionModeCallback(targetView));
965    }
966
967    private void closeSystemDialogs() {
968        sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
969    }
970
971    private class PhoneNumberActionModeCallback implements ActionMode.Callback {
972        private final View mTargetView;
973        private final Drawable mOriginalViewBackground;
974
975        public PhoneNumberActionModeCallback(View targetView) {
976            mTargetView = targetView;
977
978            // Highlight the phone number view.  Remember the old background, and put a new one.
979            mOriginalViewBackground = mTargetView.getBackground();
980            mTargetView.setBackgroundColor(getResources().getColor(R.color.item_selected));
981        }
982
983        @Override
984        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
985            if (TextUtils.isEmpty(mPhoneNumberToCopy)) return false;
986
987            getMenuInflater().inflate(R.menu.call_details_cab, menu);
988            return true;
989        }
990
991        @Override
992        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
993            return true;
994        }
995
996        @Override
997        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
998            switch (item.getItemId()) {
999                case R.id.copy_phone_number:
1000                    ClipboardUtils.copyText(CallDetailActivity.this, mPhoneNumberLabelToCopy,
1001                            mPhoneNumberToCopy, true);
1002                    mode.finish(); // Close the CAB
1003                    return true;
1004            }
1005            return false;
1006        }
1007
1008        @Override
1009        public void onDestroyActionMode(ActionMode mode) {
1010            mPhoneNumberActionMode = null;
1011
1012            // Restore the view background.
1013            mTargetView.setBackground(mOriginalViewBackground);
1014        }
1015    }
1016
1017    /** Returns the given text, forced to be left-to-right. */
1018    private static CharSequence forceLeftToRight(CharSequence text) {
1019        StringBuilder sb = new StringBuilder();
1020        sb.append(LEFT_TO_RIGHT_EMBEDDING);
1021        sb.append(text);
1022        sb.append(POP_DIRECTIONAL_FORMATTING);
1023        return sb.toString();
1024    }
1025}
1026