QuickContactActivity.java revision c8e2a91807b581194566eb45019fa16e2268b462
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.contacts.quickcontact;
18
19import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.ArgbEvaluator;
23import android.animation.ObjectAnimator;
24import android.app.Activity;
25import android.app.Fragment;
26import android.app.LoaderManager.LoaderCallbacks;
27import android.app.SearchManager;
28import android.content.ActivityNotFoundException;
29import android.content.ContentUris;
30import android.content.ContentValues;
31import android.content.Intent;
32import android.content.Loader;
33import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
35import android.graphics.Bitmap;
36import android.graphics.Color;
37import android.graphics.PorterDuff;
38import android.graphics.PorterDuffColorFilter;
39import android.graphics.drawable.BitmapDrawable;
40import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
42import android.net.ParseException;
43import android.net.Uri;
44import android.net.WebAddress;
45import android.os.AsyncTask;
46import android.os.Bundle;
47import android.os.Trace;
48import android.provider.CalendarContract;
49import android.provider.ContactsContract;
50import android.provider.ContactsContract.CommonDataKinds.Email;
51import android.provider.ContactsContract.CommonDataKinds.Event;
52import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
53import android.provider.ContactsContract.CommonDataKinds.Identity;
54import android.provider.ContactsContract.CommonDataKinds.Im;
55import android.provider.ContactsContract.CommonDataKinds.Nickname;
56import android.provider.ContactsContract.CommonDataKinds.Note;
57import android.provider.ContactsContract.CommonDataKinds.Organization;
58import android.provider.ContactsContract.CommonDataKinds.Phone;
59import android.provider.ContactsContract.CommonDataKinds.Relation;
60import android.provider.ContactsContract.CommonDataKinds.SipAddress;
61import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
62import android.provider.ContactsContract.CommonDataKinds.Website;
63import android.provider.ContactsContract.Contacts;
64import android.provider.ContactsContract.Data;
65import android.provider.ContactsContract.DisplayNameSources;
66import android.provider.ContactsContract.DataUsageFeedback;
67import android.provider.ContactsContract.QuickContact;
68import android.provider.ContactsContract.RawContacts;
69import android.support.v7.graphics.Palette;
70import android.text.TextUtils;
71import android.util.Log;
72import android.util.Pair;
73import android.view.Menu;
74import android.view.MenuInflater;
75import android.view.MenuItem;
76import android.view.View;
77import android.view.View.OnClickListener;
78import android.view.WindowManager;
79import android.widget.ImageView;
80import android.widget.Toast;
81import android.widget.Toolbar;
82
83import com.android.contacts.ContactSaveService;
84import com.android.contacts.ContactsActivity;
85import com.android.contacts.NfcHandler;
86import com.android.contacts.R;
87import com.android.contacts.common.CallUtil;
88import com.android.contacts.common.Collapser;
89import com.android.contacts.common.ContactsUtils;
90import com.android.contacts.common.editor.SelectAccountDialogFragment;
91import com.android.contacts.common.lettertiles.LetterTileDrawable;
92import com.android.contacts.common.list.ShortcutIntentBuilder;
93import com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
94import com.android.contacts.common.model.AccountTypeManager;
95import com.android.contacts.common.model.Contact;
96import com.android.contacts.common.model.ContactLoader;
97import com.android.contacts.common.model.RawContact;
98import com.android.contacts.common.model.account.AccountType;
99import com.android.contacts.common.model.account.AccountWithDataSet;
100import com.android.contacts.common.model.dataitem.DataItem;
101import com.android.contacts.common.model.dataitem.DataKind;
102import com.android.contacts.common.model.dataitem.EmailDataItem;
103import com.android.contacts.common.model.dataitem.EventDataItem;
104import com.android.contacts.common.model.dataitem.ImDataItem;
105import com.android.contacts.common.model.dataitem.NicknameDataItem;
106import com.android.contacts.common.model.dataitem.NoteDataItem;
107import com.android.contacts.common.model.dataitem.OrganizationDataItem;
108import com.android.contacts.common.model.dataitem.PhoneDataItem;
109import com.android.contacts.common.model.dataitem.RelationDataItem;
110import com.android.contacts.common.model.dataitem.SipAddressDataItem;
111import com.android.contacts.common.model.dataitem.StructuredNameDataItem;
112import com.android.contacts.common.model.dataitem.StructuredPostalDataItem;
113import com.android.contacts.common.model.dataitem.WebsiteDataItem;
114import com.android.contacts.common.util.DateUtils;
115import com.android.contacts.common.util.MaterialColorMapUtils;
116import com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
117import com.android.contacts.detail.ContactDetailDisplayUtils;
118import com.android.contacts.interactions.CalendarInteractionsLoader;
119import com.android.contacts.interactions.CallLogInteractionsLoader;
120import com.android.contacts.interactions.ContactDeletionInteraction;
121import com.android.contacts.interactions.ContactInteraction;
122import com.android.contacts.interactions.SmsInteractionsLoader;
123import com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
124import com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
125import com.android.contacts.util.ImageViewDrawableSetter;
126import com.android.contacts.util.PhoneCapabilityTester;
127import com.android.contacts.util.SchedulingUtils;
128import com.android.contacts.util.StructuredPostalUtils;
129import com.android.contacts.widget.MultiShrinkScroller;
130import com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
131import com.google.common.base.Preconditions;
132import com.google.common.collect.Lists;
133
134import java.util.ArrayList;
135import java.util.Arrays;
136import java.util.Calendar;
137import java.util.Collections;
138import java.util.Comparator;
139import java.util.Date;
140import java.util.HashMap;
141import java.util.List;
142import java.util.Map;
143
144/**
145 * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
146 * data asynchronously, and then shows a popup with details centered around
147 * {@link Intent#getSourceBounds()}.
148 */
149public class QuickContactActivity extends ContactsActivity {
150
151    /**
152     * QuickContacts immediately takes up the full screen. All possible information is shown.
153     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
154     * should only be used by the Contacts app.
155     */
156    public static final int MODE_FULLY_EXPANDED = 4;
157
158    private static final String TAG = "QuickContact";
159
160    private static final String KEY_THEME_COLOR = "theme_color";
161
162    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
163    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
164    private static final int SCRIM_COLOR = Color.argb(0xB2, 0, 0, 0);
165    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
166
167    /** This is the Intent action to install a shortcut in the launcher. */
168    private static final String ACTION_INSTALL_SHORTCUT =
169            "com.android.launcher.action.INSTALL_SHORTCUT";
170
171    @SuppressWarnings("deprecation")
172    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
173
174    private static final String MIMETYPE_GPLUS_PROFILE =
175            "vnd.android.cursor.item/vnd.googleplus.profile";
176    private static final String INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE = "Add to circle";
177    private static final String MIMETYPE_HANGOUTS =
178            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
179    private static final String INTENT_DATA_HANGOUTS_VIDEO = "Start video call";
180
181    private Uri mLookupUri;
182    private String[] mExcludeMimes;
183    private int mExtraMode;
184    private int mStatusBarColor;
185    private boolean mHasAlreadyBeenOpened;
186
187    private ImageView mPhotoView;
188    private View mTransparentView;
189    private ExpandingEntryCardView mContactCard;
190    private ExpandingEntryCardView mNoContactDetailsCard;
191    private ExpandingEntryCardView mRecentCard;
192    private ExpandingEntryCardView mAboutCard;
193    /**
194     * This list contains all the {@link DataItem}s. Each nested list contains all data items of a
195     * specific mimetype in sorted order, using mWithinMimeTypeDataItemComparator. The mimetype
196     * lists are sorted using mAmongstMimeTypeDataItemComparator.
197     */
198    private List<List<DataItem>> mDataItemsList;
199    /**
200     * A map between a mimetype string and the corresponding list of data items. The data items
201     * are in sorted order using mWithinMimeTypeDataItemComparator.
202     */
203    private Map<String, List<DataItem>> mDataItemsMap;
204    private MultiShrinkScroller mScroller;
205    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
206    private AsyncTask<Void, Void, Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>
207            mEntriesAndActionsTask;
208    private ColorDrawable mWindowScrim;
209    private MaterialColorMapUtils mMaterialColorMapUtils;
210    private boolean mIsWaitingForOtherPieceOfExitAnimation;
211    private boolean mIsExitAnimationInProgress;
212    private boolean mHasComputedThemeColor;
213
214    private Contact mContactData;
215    private ContactLoader mContactLoader;
216    private PorterDuffColorFilter mColorFilter;
217
218    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
219
220    /**
221     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
222     *
223     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
224     * in the order specified here.</p>
225     */
226    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
227            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
228            StructuredPostal.CONTENT_ITEM_TYPE);
229
230    private static final List<String> ABOUT_CARD_MIMETYPES = Lists.newArrayList(
231            Event.CONTENT_ITEM_TYPE, GroupMembership.CONTENT_ITEM_TYPE, Identity.CONTENT_ITEM_TYPE,
232            Im.CONTENT_ITEM_TYPE, Nickname.CONTENT_ITEM_TYPE, Note.CONTENT_ITEM_TYPE,
233            Organization.CONTENT_ITEM_TYPE, Relation.CONTENT_ITEM_TYPE, Website.CONTENT_ITEM_TYPE);
234
235    /** Id for the background contact loader */
236    private static final int LOADER_CONTACT_ID = 0;
237
238    private static final String KEY_LOADER_EXTRA_PHONES =
239            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
240
241    /** Id for the background Sms Loader */
242    private static final int LOADER_SMS_ID = 1;
243    private static final int MAX_SMS_RETRIEVE = 3;
244
245    /** Id for the back Calendar Loader */
246    private static final int LOADER_CALENDAR_ID = 2;
247    private static final String KEY_LOADER_EXTRA_EMAILS =
248            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
249    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
250    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
251    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
252            180L * 24L * 60L * 60L * 1000L /* 180 days */;
253    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
254            36L * 60L * 60L * 1000L /* 36 hours */;
255
256    /** Id for the background Call Log Loader */
257    private static final int LOADER_CALL_LOG_ID = 3;
258    private static final int MAX_CALL_LOG_RETRIEVE = 3;
259    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
260    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
261    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
262
263
264    private static final int[] mRecentLoaderIds = new int[]{
265        LOADER_SMS_ID,
266        LOADER_CALENDAR_ID,
267        LOADER_CALL_LOG_ID};
268    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults = new HashMap<>();
269
270    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
271
272    final OnClickListener mEntryClickHandler = new OnClickListener() {
273        @Override
274        public void onClick(View v) {
275            // Data Id is stored as the entry view id
276            final int dataId = v.getId();
277            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
278                editContact();
279                return;
280            }
281            final Object intentObject = v.getTag();
282            if (intentObject == null || !(intentObject instanceof Intent)) {
283                Log.w(TAG, "Intent tag was not used correctly");
284                return;
285            }
286            final Intent intent = (Intent) intentObject;
287
288            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
289            // so the exact usage type is not necessary in all cases
290            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
291
292            final String scheme = intent.getData().getScheme();
293            if ((scheme != null && scheme.equals(CallUtil.SCHEME_SMSTO)) ||
294                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
295                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
296            }
297
298            // Data IDs start at 1 so anything less is invalid
299            if (dataId > 0) {
300                final Uri uri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
301                        .appendPath(String.valueOf(dataId))
302                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
303                        .build();
304                final boolean successful = getContentResolver().update(
305                        uri, new ContentValues(), null, null) > 0;
306                if (!successful) {
307                    Log.w(TAG, "DataUsageFeedback increment failed");
308                }
309            } else {
310                Log.w(TAG, "Invalid Data ID");
311            }
312
313            startActivity(intent);
314        }
315    };
316
317    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
318            = new ExpandingEntryCardViewListener() {
319        @Override
320        public void onCollapse(int heightDelta) {
321            mScroller.prepareForShrinkingScrollChild(heightDelta);
322        }
323    };
324
325    /**
326     * Headless fragment used to handle account selection callbacks invoked from
327     * {@link DirectoryContactUtil}.
328     */
329    public static class SelectAccountDialogFragmentListener extends Fragment
330            implements SelectAccountDialogFragment.Listener {
331
332        private QuickContactActivity mQuickContactActivity;
333
334        public SelectAccountDialogFragmentListener() {}
335
336        @Override
337        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
338            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
339                    account, mQuickContactActivity);
340        }
341
342        @Override
343        public void onAccountSelectorCancelled() {}
344
345        /**
346         * Set the parent activity. Since rotation can cause this fragment to be used across
347         * more than one activity instance, we need to explicitly set this value instead
348         * of making this class non-static.
349         */
350        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
351            mQuickContactActivity = quickContactActivity;
352        }
353    }
354
355    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
356            = new MultiShrinkScrollerListener() {
357        @Override
358        public void onScrolledOffBottom() {
359            if (!mIsWaitingForOtherPieceOfExitAnimation) {
360                finish();
361                return;
362            }
363            mIsWaitingForOtherPieceOfExitAnimation = false;
364        }
365
366        @Override
367        public void onEnterFullscreen() {
368            updateStatusBarColor();
369        }
370
371        @Override
372        public void onExitFullscreen() {
373            updateStatusBarColor();
374        }
375
376        @Override
377        public void onStartScrollOffBottom() {
378            // Remove the window shim now that we are starting an Activity exit animation.
379            final int duration = getResources().getInteger(android.R.integer.config_shortAnimTime);
380            final ObjectAnimator animator = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0xFF, 0);
381            animator.addListener(mExitWindowShimAnimationListener);
382            animator.setDuration(duration).start();
383            mIsWaitingForOtherPieceOfExitAnimation = true;
384            mIsExitAnimationInProgress = true;
385        }
386    };
387
388    final AnimatorListener mExitWindowShimAnimationListener = new AnimatorListenerAdapter() {
389        @Override
390        public void onAnimationEnd(Animator animation) {
391            if (!mIsWaitingForOtherPieceOfExitAnimation) {
392                finish();
393                return;
394            }
395            mIsWaitingForOtherPieceOfExitAnimation = false;
396        }
397    };
398
399
400    /**
401     * Data items are compared to the same mimetype based off of three qualities:
402     * 1. Super primary
403     * 2. Primary
404     * 3. Times used
405     */
406    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
407            new Comparator<DataItem>() {
408        @Override
409        public int compare(DataItem lhs, DataItem rhs) {
410            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
411                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
412                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
413                return 0;
414            }
415
416            if (lhs.isSuperPrimary()) {
417                return -1;
418            } else if (rhs.isSuperPrimary()) {
419                return 1;
420            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
421                return -1;
422            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
423                return 1;
424            } else {
425                final int lhsTimesUsed =
426                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
427                final int rhsTimesUsed =
428                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
429
430                return rhsTimesUsed - lhsTimesUsed;
431            }
432        }
433    };
434
435    /**
436     * Sorts among different mimetypes based off:
437     * 1. Times used
438     * 2. Last time used
439     * 3. Statically defined
440     */
441    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
442            new Comparator<List<DataItem>> () {
443        @Override
444        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
445            DataItem lhs = lhsList.get(0);
446            DataItem rhs = rhsList.get(0);
447            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
448            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
449            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
450            if (timesUsedDifference != 0) {
451                return timesUsedDifference;
452            }
453
454            final long lhsLastTimeUsed =
455                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
456            final long rhsLastTimeUsed =
457                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
458            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
459            if (lastTimeUsedDifference > 0) {
460                return 1;
461            } else if (lastTimeUsedDifference < 0) {
462                return -1;
463            }
464
465            // Times used and last time used are the same. Resort to statically defined.
466            final String lhsMimeType = lhs.getMimeType();
467            final String rhsMimeType = rhs.getMimeType();
468            for (String mimeType : LEADING_MIMETYPES) {
469                if (lhsMimeType.equals(mimeType)) {
470                    return -1;
471                } else if (rhsMimeType.equals(mimeType)) {
472                    return 1;
473                }
474            }
475            return 0;
476        }
477    };
478
479    @Override
480    protected void onCreate(Bundle savedInstanceState) {
481        Trace.beginSection("onCreate()");
482        super.onCreate(savedInstanceState);
483
484        getWindow().setStatusBarColor(Color.TRANSPARENT);
485
486        processIntent(getIntent());
487
488        // Show QuickContact in front of soft input
489        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
490                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
491
492        setContentView(R.layout.quickcontact_activity);
493
494        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
495
496        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
497        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
498        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
499        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
500        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
501
502        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
503        mContactCard.setOnClickListener(mEntryClickHandler);
504        mContactCard.setExpandButtonText(
505        getResources().getString(R.string.expanding_entry_card_view_see_all));
506
507        mRecentCard.setOnClickListener(mEntryClickHandler);
508        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
509
510        mAboutCard.setOnClickListener(mEntryClickHandler);
511
512        mPhotoView = (ImageView) findViewById(R.id.photo);
513        mTransparentView = findViewById(R.id.transparent_view);
514        if (mScroller != null) {
515            mTransparentView.setOnClickListener(new OnClickListener() {
516                @Override
517                public void onClick(View v) {
518                    mScroller.scrollOffBottom();
519                }
520            });
521        }
522
523        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
524        setActionBar(toolbar);
525        getActionBar().setTitle(null);
526        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
527        // find the correct TextView location & size later.
528        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
529
530        mHasAlreadyBeenOpened = savedInstanceState != null;
531
532        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
533        getWindow().setBackgroundDrawable(mWindowScrim);
534        if (!mHasAlreadyBeenOpened) {
535            final int duration = getResources().getInteger(android.R.integer.config_shortAnimTime);
536            ObjectAnimator.ofInt(mWindowScrim, "alpha", 0, 0xFF).setDuration(duration).start();
537        }
538
539        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
540        // mScroller needs to perform asynchronous measurements after initalize(), therefore
541        // we can't mark this as GONE.
542        mScroller.setVisibility(View.INVISIBLE);
543
544        setHeaderNameText(R.string.missing_name);
545
546        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
547                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
548        if (mSelectAccountFragmentListener == null) {
549            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
550            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
551                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
552            mSelectAccountFragmentListener.setRetainInstance(true);
553        }
554        mSelectAccountFragmentListener.setQuickContactActivity(this);
555
556        if (savedInstanceState != null) {
557            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
558            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
559                    new Runnable() {
560                        @Override
561                        public void run() {
562                            // Need to wait for the pre draw before setting the initial scroll
563                            // value. Prior to pre draw all scroll values are invalid.
564                            if (mHasAlreadyBeenOpened) {
565                                mScroller.setVisibility(View.VISIBLE);
566                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
567                            }
568                            // Need to wait for pre draw for setting the theme color. Setting the
569                            // header tint before the MultiShrinkScroller has been measured will
570                            // cause incorrect tinting calculations.
571                            if (color != 0) {
572                                setThemeColor(mMaterialColorMapUtils
573                                        .calculatePrimaryAndSecondaryColor(color));
574                            }
575                        }
576                    });
577        }
578
579        Trace.endSection();
580    }
581
582    @Override
583    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
584        if (requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
585                resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED) {
586            // The contact that we were showing has been deleted.
587            finish();
588        }
589    }
590
591    @Override
592    protected void onNewIntent(Intent intent) {
593        super.onNewIntent(intent);
594        mHasAlreadyBeenOpened = true;
595        mHasComputedThemeColor = false;
596        processIntent(intent);
597    }
598
599    @Override
600    public void onSaveInstanceState(Bundle savedInstanceState) {
601        super.onSaveInstanceState(savedInstanceState);
602        if (mColorFilter != null) {
603            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilter.getColor());
604        }
605    }
606
607    private void processIntent(Intent intent) {
608        Uri lookupUri = intent.getData();
609
610        // Check to see whether it comes from the old version.
611        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
612            final long rawContactId = ContentUris.parseId(lookupUri);
613            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
614                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
615        }
616        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE,
617                QuickContact.MODE_LARGE);
618        final Uri oldLookupUri = mLookupUri;
619
620        mLookupUri = Preconditions.checkNotNull(lookupUri, "missing lookupUri");
621        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
622        if (oldLookupUri == null) {
623            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
624                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
625        } else if (oldLookupUri != mLookupUri) {
626            // After copying a directory contact, the contact URI changes. Therefore,
627            // we need to restart the loader and reload the new contact.
628            for (int interactionLoaderId : mRecentLoaderIds) {
629                getLoaderManager().destroyLoader(interactionLoaderId);
630            }
631            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
632                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
633        }
634
635        NfcHandler.register(this, mLookupUri);
636    }
637
638    private void runEntranceAnimation() {
639        if (mHasAlreadyBeenOpened) {
640            return;
641        }
642        mHasAlreadyBeenOpened = true;
643        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
644    }
645
646    /** Assign this string to the view if it is not empty. */
647    private void setHeaderNameText(int resId) {
648        if (mScroller != null) {
649            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString());
650        }
651    }
652
653    /** Assign this string to the view if it is not empty. */
654    private void setHeaderNameText(String value) {
655        if (!TextUtils.isEmpty(value)) {
656            if (mScroller != null) {
657                mScroller.setTitle(value);
658            }
659        }
660    }
661
662    /**
663     * Check if the given MIME-type appears in the list of excluded MIME-types
664     * that the most-recent caller requested.
665     */
666    private boolean isMimeExcluded(String mimeType) {
667        if (mExcludeMimes == null) return false;
668        for (String excludedMime : mExcludeMimes) {
669            if (TextUtils.equals(excludedMime, mimeType)) {
670                return true;
671            }
672        }
673        return false;
674    }
675
676    /**
677     * Handle the result from the ContactLoader
678     */
679    private void bindContactData(final Contact data) {
680        Trace.beginSection("bindContactData");
681        mContactData = data;
682        invalidateOptionsMenu();
683
684        Trace.endSection();
685        Trace.beginSection("Set display photo & name");
686
687        mPhotoSetter.setupContactPhoto(data, mPhotoView);
688        extractAndApplyTintFromPhotoViewAsynchronously();
689        analyzeWhitenessOfPhotoAsynchronously();
690        setHeaderNameText(data.getDisplayName());
691
692        Trace.endSection();
693
694        mEntriesAndActionsTask = new AsyncTask<Void, Void,
695                Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>() {
696
697            @Override
698            protected Pair<List<List<DataItem>>, Map<String, List<DataItem>>> doInBackground(
699                    Void... params) {
700                return generateDataModelFromContact(data);
701            }
702
703            @Override
704            protected void onPostExecute(Pair<List<List<DataItem>>,
705                    Map<String, List<DataItem>>> dataItemsPair) {
706                super.onPostExecute(dataItemsPair);
707                mDataItemsList = dataItemsPair.first;
708                mDataItemsMap = dataItemsPair.second;
709                // Check that original AsyncTask parameters are still valid and the activity
710                // is still running before binding to UI. A new intent could invalidate
711                // the results, for example.
712                if (data == mContactData && !isCancelled()) {
713                    bindDataToCards();
714                    showActivity();
715                }
716            }
717        };
718        mEntriesAndActionsTask.execute();
719    }
720
721    private void bindDataToCards() {
722        startInteractionLoaders();
723        populateContactAndAboutCard();
724    }
725
726    private void startInteractionLoaders() {
727        final List<DataItem> phoneDataItems = mDataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
728        String[] phoneNumbers = null;
729        if (phoneDataItems != null) {
730            phoneNumbers = new String[phoneDataItems.size()];
731            for (int i = 0; i < phoneDataItems.size(); ++i) {
732                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
733            }
734        }
735        final Bundle phonesExtraBundle = new Bundle();
736        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
737
738        Trace.beginSection("start sms loader");
739        getLoaderManager().initLoader(
740                LOADER_SMS_ID,
741                phonesExtraBundle,
742                mLoaderInteractionsCallbacks);
743        Trace.endSection();
744
745        Trace.beginSection("start call log loader");
746        getLoaderManager().initLoader(
747                LOADER_CALL_LOG_ID,
748                phonesExtraBundle,
749                mLoaderInteractionsCallbacks);
750        Trace.endSection();
751
752
753        Trace.beginSection("start calendar loader");
754        final List<DataItem> emailDataItems = mDataItemsMap.get(Email.CONTENT_ITEM_TYPE);
755        String[] emailAddresses = null;
756        if (emailDataItems != null) {
757            emailAddresses = new String[emailDataItems.size()];
758            for (int i = 0; i < emailDataItems.size(); ++i) {
759                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
760            }
761        }
762        final Bundle emailsExtraBundle = new Bundle();
763        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
764        getLoaderManager().initLoader(
765                LOADER_CALENDAR_ID,
766                emailsExtraBundle,
767                mLoaderInteractionsCallbacks);
768        Trace.endSection();
769    }
770
771    private void showActivity() {
772        if (mScroller != null) {
773            mScroller.setVisibility(View.VISIBLE);
774            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
775                    new Runnable() {
776                        @Override
777                        public void run() {
778                            runEntranceAnimation();
779                        }
780                    });
781        }
782    }
783
784    private void populateContactAndAboutCard() {
785        Trace.beginSection("bind contact card");
786
787        final List<List<Entry>> contactCardEntries = new ArrayList<>();
788        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
789
790        for (int i = 0; i < mDataItemsList.size(); ++i) {
791            final List<DataItem> dataItemsByMimeType = mDataItemsList.get(i);
792            final DataItem topDataItem = dataItemsByMimeType.get(0);
793            if (ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
794                List<Entry> aboutEntries = dataItemsToEntries(mDataItemsList.get(i));
795                if (aboutEntries.size() > 0) {
796                    aboutCardEntries.add(aboutEntries);
797                }
798            } else {
799                List<Entry> contactEntries = dataItemsToEntries(mDataItemsList.get(i));
800                if (contactEntries.size() > 0) {
801                    contactCardEntries.add(contactEntries);
802                }
803            }
804        }
805
806        if (contactCardEntries.size() > 0) {
807            mContactCard.initialize(contactCardEntries,
808                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
809                    /* isExpanded = */ mContactCard.isExpanded(),
810                    /* isAlwaysExpanded = */ false,
811                    mExpandingEntryCardViewListener);
812            mContactCard.setVisibility(View.VISIBLE);
813        } else {
814            mContactCard.setVisibility(View.GONE);
815        }
816        Trace.endSection();
817
818        Trace.beginSection("bind about card");
819        mAboutCard.initialize(aboutCardEntries,
820                /* numInitialVisibleEntries = */ 1,
821                /* isExpanded = */ true,
822                /* isAlwaysExpanded = */ true,
823                mExpandingEntryCardViewListener);
824
825        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
826            initializeNoContactDetailCard();
827        } else {
828            mNoContactDetailsCard.setVisibility(View.GONE);
829        }
830
831        // If the Recent card is already initialized (all recent data is loaded), show the About
832        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
833        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
834            mAboutCard.setVisibility(View.VISIBLE);
835        }
836        Trace.endSection();
837    }
838
839    /**
840     * Create a card that shows "Add email" and "Add phone number" entries in grey.
841     */
842    private void initializeNoContactDetailCard() {
843        final Drawable phoneIcon = getResources().getDrawable(
844                R.drawable.ic_phone_24dp).mutate();
845        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
846                phoneIcon, getString(R.string.quickcontact_add_phone_number),
847                /* subHeader = */ null, /* text = */ null, getEditContactIntent(),
848                /* alternateIcon = */ null, /* alternateIntent = */ null,
849                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
850                /* isEditable = */ false);
851
852        final Drawable emailIcon = getResources().getDrawable(
853                R.drawable.ic_email_24dp).mutate();
854        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
855                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
856                /* text = */ null, getEditContactIntent(), /* alternateIcon = */ null,
857                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
858                /* shouldApplyColor = */ true, /* isEditable = */ false);
859
860        final List<List<Entry>> promptEntries = new ArrayList<>();
861        promptEntries.add(new ArrayList<Entry>(1));
862        promptEntries.add(new ArrayList<Entry>(1));
863        promptEntries.get(0).add(phonePromptEntry);
864        promptEntries.get(1).add(emailPromptEntry);
865
866        final int subHeaderTextColor = getResources().getColor(
867                R.color.quickcontact_entry_sub_header_text_color);
868        final PorterDuffColorFilter greyColorFilter =
869                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
870        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
871                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener);
872        mNoContactDetailsCard.setVisibility(View.VISIBLE);
873        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
874        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
875    }
876
877    /**
878     * Builds the {@link DataItem}s Map out of the Contact.
879     * @param data The contact to build the data from.
880     * @return A pair containing a list of data items sorted within mimetype and sorted
881     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
882     *  mimetype
883     */
884    private Pair<List<List<DataItem>>, Map<String, List<DataItem>>> generateDataModelFromContact(
885            Contact data) {
886        Trace.beginSection("Build data items map");
887
888        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
889
890        final ResolveCache cache = ResolveCache.getInstance(this);
891        for (RawContact rawContact : data.getRawContacts()) {
892            for (DataItem dataItem : rawContact.getDataItems()) {
893                dataItem.setRawContactId(rawContact.getId());
894
895                final String mimeType = dataItem.getMimeType();
896                if (mimeType == null) continue;
897
898                final AccountType accountType = rawContact.getAccountType(this);
899                final DataKind dataKind = AccountTypeManager.getInstance(this)
900                        .getKindOrFallback(accountType, mimeType);
901                if (dataKind == null) continue;
902
903                dataItem.setDataKind(dataKind);
904
905                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
906                        dataKind));
907
908                if (isMimeExcluded(mimeType) || !hasData) continue;
909
910                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
911                if (dataItemListByType == null) {
912                    dataItemListByType = new ArrayList<>();
913                    dataItemsMap.put(mimeType, dataItemListByType);
914                }
915                dataItemListByType.add(dataItem);
916            }
917        }
918        Trace.endSection();
919
920        Trace.beginSection("sort within mimetypes");
921        /*
922         * Sorting is a multi part step. The end result is to a have a sorted list of the most
923         * used data items, one per mimetype. Then, within each mimetype, the list of data items
924         * for that type is also sorted, based off of {super primary, primary, times used} in that
925         * order.
926         */
927        final List<List<DataItem>> dataItemsList = new ArrayList<>();
928        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
929            // Remove duplicate data items
930            Collapser.collapseList(mimeTypeDataItems, this);
931            // Sort within mimetype
932            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
933            // Add to the list of data item lists
934            dataItemsList.add(mimeTypeDataItems);
935        }
936        Trace.endSection();
937
938        Trace.beginSection("sort amongst mimetypes");
939        // Sort amongst mimetypes to bubble up the top data items for the contact card
940        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
941        Trace.endSection();
942
943        return new Pair<>(dataItemsList, dataItemsMap);
944    }
945
946    /**
947     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
948     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
949     * @param dataItem The {@link DataItem} to convert.
950     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
951     */
952    private Entry dataItemToEntry(DataItem dataItem) {
953        Drawable icon = null;
954        String header = null;
955        String subHeader = null;
956        Drawable subHeaderIcon = null;
957        String text = null;
958        Drawable textIcon = null;
959        Intent intent = null;
960        boolean shouldApplyColor = true;
961        Drawable alternateIcon = null;
962        Intent alternateIntent = null;
963        String alternateContentDescription = null;
964        final boolean isEditable = false;
965
966        DataKind kind = dataItem.getDataKind();
967
968        if (dataItem instanceof ImDataItem) {
969            final ImDataItem im = (ImDataItem) dataItem;
970            intent = ContactsUtils.buildImIntent(this, im).first;
971            header = getResources().getString(R.string.header_im_entry);
972            final boolean isEmail = im.isCreatedFromEmail();
973            final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
974            subHeader = Im.getProtocolLabel(getResources(), protocol,
975                    im.getCustomProtocol()).toString();
976        } else if (dataItem instanceof OrganizationDataItem) {
977            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
978            header = getResources().getString(R.string.header_organization_entry);
979            subHeader = organization.getCompany();
980            text = organization.getTitle();
981        } else if (dataItem instanceof NicknameDataItem) {
982            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
983            // Build nickname entries
984            final boolean isNameRawContact =
985                (mContactData.getNameRawContactId() == dataItem.getRawContactId());
986
987            final boolean duplicatesTitle =
988                isNameRawContact
989                && mContactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
990
991            if (!duplicatesTitle) {
992                header = getResources().getString(R.string.header_nickname_entry);
993                subHeader = nickname.getName();
994            }
995        } else if (dataItem instanceof NoteDataItem) {
996            final NoteDataItem note = (NoteDataItem) dataItem;
997            header = getResources().getString(R.string.header_note_entry);
998            subHeader = note.getNote();
999        } else if (dataItem instanceof WebsiteDataItem) {
1000            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
1001            header = getResources().getString(R.string.header_website_entry);
1002            subHeader = website.getUrl();
1003            try {
1004                final WebAddress webAddress = new WebAddress(website.buildDataString(this, kind));
1005                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1006            } catch (final ParseException e) {
1007                Log.e(TAG, "Couldn't parse website: " + website.buildDataString(this, kind));
1008            }
1009        } else if (dataItem instanceof EventDataItem) {
1010            final EventDataItem event = (EventDataItem) dataItem;
1011            final String dataString = event.buildDataString(this, kind);
1012            final Calendar cal = DateUtils.parseDate(dataString, false);
1013            if (cal != null) {
1014                final Date nextAnniversary =
1015                        DateUtils.getNextAnnualDate(cal);
1016                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1017                builder.appendPath("time");
1018                ContentUris.appendId(builder, nextAnniversary.getTime());
1019                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1020            }
1021            header = getResources().getString(R.string.header_event_entry);
1022            if (event.hasKindTypeColumn(kind)) {
1023                subHeader = getResources().getString(Event.getTypeResource(
1024                        event.getKindTypeColumn(kind)));
1025            }
1026            text = DateUtils.formatDate(this, dataString);
1027        } else if (dataItem instanceof RelationDataItem) {
1028            final RelationDataItem relation = (RelationDataItem) dataItem;
1029            final String dataString = relation.buildDataString(this, kind);
1030            if (!TextUtils.isEmpty(dataString)) {
1031                intent = new Intent(Intent.ACTION_SEARCH);
1032                intent.putExtra(SearchManager.QUERY, dataString);
1033                intent.setType(Contacts.CONTENT_TYPE);
1034            }
1035            header = getResources().getString(R.string.header_relation_entry);
1036            subHeader = relation.getName();
1037            if (relation.hasKindTypeColumn(kind)) {
1038                text = Relation.getTypeLabel(getResources(), relation.getKindTypeColumn(kind),
1039                        relation.getLabel()).toString();
1040            }
1041        } else if (dataItem instanceof PhoneDataItem) {
1042            final PhoneDataItem phone = (PhoneDataItem) dataItem;
1043            if (!TextUtils.isEmpty(phone.getNumber())) {
1044                header = phone.buildDataString(this, kind);
1045                if (phone.hasKindTypeColumn(kind)) {
1046                    text = Phone.getTypeLabel(getResources(), phone.getKindTypeColumn(kind),
1047                            phone.getLabel()).toString();
1048                }
1049                icon = getResources().getDrawable(R.drawable.ic_phone_24dp);
1050                if (PhoneCapabilityTester.isPhone(this)) {
1051                    intent = CallUtil.getCallIntent(phone.getNumber());
1052                }
1053                alternateIntent = new Intent(Intent.ACTION_SENDTO,
1054                        Uri.fromParts(CallUtil.SCHEME_SMSTO, phone.getNumber(), null));
1055                alternateIcon = getResources().getDrawable(R.drawable.ic_message_24dp);
1056                alternateContentDescription = getResources().getString(R.string.sms_other);
1057            }
1058        } else if (dataItem instanceof EmailDataItem) {
1059            final EmailDataItem email = (EmailDataItem) dataItem;
1060            final String address = email.getData();
1061            if (!TextUtils.isEmpty(address)) {
1062                final Uri mailUri = Uri.fromParts(CallUtil.SCHEME_MAILTO, address, null);
1063                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1064                header = email.getAddress();
1065                if (email.hasKindTypeColumn(kind)) {
1066                    text = Email.getTypeLabel(getResources(), email.getKindTypeColumn(kind),
1067                            email.getLabel()).toString();
1068                }
1069                icon = getResources().getDrawable(R.drawable.ic_email_24dp);
1070            }
1071        } else if (dataItem instanceof StructuredPostalDataItem) {
1072            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1073            final String postalAddress = postal.getFormattedAddress();
1074            if (!TextUtils.isEmpty(postalAddress)) {
1075                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1076                header = postal.getFormattedAddress();
1077                if (postal.hasKindTypeColumn(kind)) {
1078                    text = StructuredPostal.getTypeLabel(getResources(),
1079                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
1080                }
1081                icon = getResources().getDrawable(R.drawable.ic_place_24dp);
1082            }
1083        } else if (dataItem instanceof SipAddressDataItem) {
1084            if (PhoneCapabilityTester.isSipPhone(this)) {
1085                final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1086                final String address = sip.getSipAddress();
1087                if (!TextUtils.isEmpty(address)) {
1088                    final Uri callUri = Uri.fromParts(CallUtil.SCHEME_SIP, address, null);
1089                    intent = CallUtil.getCallIntent(callUri);
1090                    header = address;
1091                    if (sip.hasKindTypeColumn(kind)) {
1092                        text = SipAddress.getTypeLabel(getResources(), sip.getKindTypeColumn(kind),
1093                            sip.getLabel()).toString();
1094                    }
1095                    // Note that this item will get a SIP-specific variant
1096                    // of the "call phone" icon, rather than the standard
1097                    // app icon for the Phone app (which we show for
1098                    // regular phone numbers.)  That's because the phone
1099                    // app explicitly specifies an android:icon attribute
1100                    // for the SIP-related intent-filters in its manifest.
1101                    icon = ResolveCache.getInstance(this).getIcon(sip.getMimeType(), intent);
1102                    // Call mutate to create a new Drawable.ConstantState for color filtering
1103                    if (icon != null) {
1104                        icon.mutate();
1105                    }
1106                }
1107            }
1108        } else if (dataItem instanceof StructuredNameDataItem) {
1109            final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
1110            if (!TextUtils.isEmpty(givenName)) {
1111                mAboutCard.setTitle(getResources().getString(R.string.about_card_title) +
1112                        " " + givenName);
1113            } else {
1114                mAboutCard.setTitle(getResources().getString(R.string.about_card_title));
1115            }
1116        } else {
1117            // Custom DataItem
1118            header = dataItem.buildDataStringForDisplay(this, kind);
1119            text = kind.typeColumn;
1120            intent = new Intent(Intent.ACTION_VIEW);
1121            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
1122            intent.setDataAndType(uri, dataItem.getMimeType());
1123
1124            if (intent != null) {
1125                final String mimetype = intent.getType();
1126
1127                // Attempt to use known icons for known 3p types. Otherwise default to ResolveCache
1128                switch (mimetype) {
1129                    case MIMETYPE_GPLUS_PROFILE:
1130                        if (INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE.equals(
1131                                intent.getDataString())) {
1132                            icon = getResources().getDrawable(
1133                                    R.drawable.ic_add_to_circles_black_24);
1134                        } else {
1135                            icon = getResources().getDrawable(R.drawable.ic_google_plus_24dp);
1136                        }
1137                        break;
1138                    case MIMETYPE_HANGOUTS:
1139                        if (INTENT_DATA_HANGOUTS_VIDEO.equals(intent.getDataString())) {
1140                            icon = getResources().getDrawable(R.drawable.ic_hangout_video_24dp);
1141                        } else {
1142                            icon = getResources().getDrawable(R.drawable.ic_hangout_24dp);
1143                        }
1144                        break;
1145                    default:
1146                        icon = ResolveCache.getInstance(this).getIcon(
1147                                dataItem.getMimeType(), intent);
1148                        // Call mutate to create a new Drawable.ConstantState for color filtering
1149                        if (icon != null) {
1150                            icon.mutate();
1151                        }
1152                        shouldApplyColor = false;
1153                }
1154            }
1155        }
1156
1157        if (intent != null) {
1158            // Do not set the intent is there are no resolves
1159            if (!PhoneCapabilityTester.isIntentRegistered(this, intent)) {
1160                intent = null;
1161            }
1162        }
1163
1164        if (alternateIntent != null) {
1165            // Do not set the alternate intent is there are no resolves
1166            if (!PhoneCapabilityTester.isIntentRegistered(this, alternateIntent)) {
1167                alternateIntent = null;
1168            }
1169
1170            // Attempt to use package manager to find a suitable content description if needed
1171            if (TextUtils.isEmpty(alternateContentDescription)) {
1172                alternateContentDescription = getIntentResolveLabel(alternateIntent);
1173            }
1174        }
1175
1176        // If the Entry has no visual elements, return null
1177        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
1178                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
1179            return null;
1180        }
1181
1182        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
1183                -1 : (int) dataItem.getId();
1184
1185        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon, intent,
1186                alternateIcon, alternateIntent, alternateContentDescription, shouldApplyColor,
1187                isEditable);
1188    }
1189
1190    private List<Entry> dataItemsToEntries(List<DataItem> dataItems) {
1191        final List<Entry> entries = new ArrayList<>();
1192        for (DataItem dataItem : dataItems) {
1193            final Entry entry = dataItemToEntry(dataItem);
1194            if (entry != null) {
1195                entries.add(entry);
1196            }
1197        }
1198        return entries;
1199    }
1200
1201    private String getIntentResolveLabel(Intent intent) {
1202        final List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent,
1203                PackageManager.MATCH_DEFAULT_ONLY);
1204
1205        // Pick first match, otherwise best found
1206        ResolveInfo bestResolve = null;
1207        final int size = matches.size();
1208        if (size == 1) {
1209            bestResolve = matches.get(0);
1210        } else if (size > 1) {
1211            bestResolve = ResolveCache.getInstance(this).getBestResolve(intent, matches);
1212        }
1213
1214        if (bestResolve == null) {
1215            return null;
1216        }
1217
1218        return String.valueOf(bestResolve.loadLabel(getPackageManager()));
1219    }
1220
1221    /**
1222     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
1223     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
1224     * on a Nexus 5.
1225     */
1226    private void extractAndApplyTintFromPhotoViewAsynchronously() {
1227        if (mScroller == null) {
1228            return;
1229        }
1230        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1231        new AsyncTask<Void, Void, MaterialPalette>() {
1232            @Override
1233            protected MaterialPalette doInBackground(Void... params) {
1234
1235                if (imageViewDrawable instanceof BitmapDrawable) {
1236                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
1237                    final int primaryColor = colorFromBitmap(bitmap);
1238                    if (primaryColor != 0) {
1239                        return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
1240                                primaryColor);
1241                    }
1242                }
1243                if (imageViewDrawable instanceof LetterTileDrawable) {
1244                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
1245                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
1246                }
1247                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
1248            }
1249
1250            @Override
1251            protected void onPostExecute(MaterialPalette palette) {
1252                super.onPostExecute(palette);
1253                if (mHasComputedThemeColor) {
1254                    // If we had previously computed a theme color from the contact photo,
1255                    // then do not update the theme color. Changing the theme color several
1256                    // seconds after QC has started, as a result of an updated/upgraded photo,
1257                    // is a jarring experience. On the other hand, changing the theme color after
1258                    // a rotation or onNewIntent() is perfectly fine.
1259                    return;
1260                }
1261                // Check that the Photo has not changed. If it has changed, the new tint
1262                // color needs to be extracted
1263                if (imageViewDrawable == mPhotoView.getDrawable()) {
1264                    mHasComputedThemeColor = true;
1265                    setThemeColor(palette);
1266                }
1267            }
1268        }.execute();
1269    }
1270
1271    /**
1272     * Examine how many white pixels are in the bitmap in order to determine whether or not
1273     * we need gradient overlays on top of the image.
1274     */
1275    private void analyzeWhitenessOfPhotoAsynchronously() {
1276        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1277        new AsyncTask<Void, Void, Boolean>() {
1278            @Override
1279            protected Boolean doInBackground(Void... params) {
1280                if (imageViewDrawable instanceof BitmapDrawable) {
1281                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
1282                    return WhitenessUtils.isBitmapWhiteAtTopOrBottom(bitmap);
1283                }
1284                return !(imageViewDrawable instanceof LetterTileDrawable);
1285            }
1286
1287            @Override
1288            protected void onPostExecute(Boolean isWhite) {
1289                super.onPostExecute(isWhite);
1290                mScroller.setUseGradient(isWhite);
1291            }
1292        }.execute();
1293    }
1294
1295    private void setThemeColor(MaterialPalette palette) {
1296        // If the color is invalid, use the predefined default
1297        final int primaryColor = palette.mPrimaryColor;
1298        mScroller.setHeaderTintColor(primaryColor);
1299        mStatusBarColor = palette.mSecondaryColor;
1300        updateStatusBarColor();
1301
1302        mColorFilter =
1303                new PorterDuffColorFilter(primaryColor, PorterDuff.Mode.SRC_ATOP);
1304        mContactCard.setColorAndFilter(primaryColor, mColorFilter);
1305        mRecentCard.setColorAndFilter(primaryColor, mColorFilter);
1306        mAboutCard.setColorAndFilter(primaryColor, mColorFilter);
1307    }
1308
1309    private void updateStatusBarColor() {
1310        if (mScroller == null) {
1311            return;
1312        }
1313        final int desiredStatusBarColor;
1314        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
1315        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
1316            desiredStatusBarColor = mStatusBarColor;
1317        } else {
1318            desiredStatusBarColor = Color.TRANSPARENT;
1319        }
1320        // Animate to the new color.
1321        if (desiredStatusBarColor != getWindow().getStatusBarColor()) {
1322            final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
1323                    getWindow().getStatusBarColor(), desiredStatusBarColor);
1324            animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
1325            animation.setEvaluator(new ArgbEvaluator());
1326            animation.start();
1327        }
1328    }
1329
1330    private int colorFromBitmap(Bitmap bitmap) {
1331        // Author of Palette recommends using 24 colors when analyzing profile photos.
1332        final int NUMBER_OF_PALETTE_COLORS = 24;
1333        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
1334        if (palette != null && palette.getVibrantSwatch() != null) {
1335            return palette.getVibrantSwatch().getRgb();
1336        }
1337        return 0;
1338    }
1339
1340    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
1341        final List<Entry> entries = new ArrayList<>();
1342        for (ContactInteraction interaction : interactions) {
1343            entries.add(new Entry(/* id = */ -1,
1344                    interaction.getIcon(this),
1345                    interaction.getViewHeader(this),
1346                    interaction.getViewBody(this),
1347                    interaction.getBodyIcon(this),
1348                    interaction.getViewFooter(this),
1349                    interaction.getFooterIcon(this),
1350                    interaction.getIntent(),
1351                    /* alternateIcon = */ null,
1352                    /* alternateIntent = */ null,
1353                    /* alternateContentDescription = */ null,
1354                    /* shouldApplyColor = */ true,
1355                    /* isEditable = */ false));
1356        }
1357        return entries;
1358    }
1359
1360    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
1361            new LoaderCallbacks<Contact>() {
1362        @Override
1363        public void onLoaderReset(Loader<Contact> loader) {
1364            mContactData = null;
1365        }
1366
1367        @Override
1368        public void onLoadFinished(Loader<Contact> loader, Contact data) {
1369            Trace.beginSection("onLoadFinished()");
1370
1371            if (isFinishing()) {
1372                return;
1373            }
1374            if (data.isError()) {
1375                // This shouldn't ever happen, so throw an exception. The {@link ContactLoader}
1376                // should log the actual exception.
1377                throw new IllegalStateException("Failed to load contact", data.getException());
1378            }
1379            if (data.isNotFound()) {
1380                if (mHasAlreadyBeenOpened) {
1381                    finish();
1382                } else {
1383                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
1384                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
1385                            Toast.LENGTH_LONG).show();
1386                }
1387                return;
1388            }
1389
1390            bindContactData(data);
1391
1392            Trace.endSection();
1393        }
1394
1395        @Override
1396        public Loader<Contact> onCreateLoader(int id, Bundle args) {
1397            if (mLookupUri == null) {
1398                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
1399            }
1400            // Load all contact data. We need loadGroupMetaData=true to determine whether the
1401            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
1402            return new ContactLoader(getApplicationContext(), mLookupUri,
1403                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
1404                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
1405        }
1406    };
1407
1408    @Override
1409    public void onBackPressed() {
1410        if (mScroller != null) {
1411            if (!mIsExitAnimationInProgress) {
1412                mScroller.scrollOffBottom();
1413            }
1414        } else {
1415            super.onBackPressed();
1416        }
1417    }
1418
1419    @Override
1420    public void finish() {
1421        super.finish();
1422
1423        // override transitions to skip the standard window animations
1424        overridePendingTransition(0, 0);
1425    }
1426
1427    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
1428            new LoaderCallbacks<List<ContactInteraction>>() {
1429
1430        @Override
1431        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
1432            Log.v(TAG, "onCreateLoader");
1433            Loader<List<ContactInteraction>> loader = null;
1434            switch (id) {
1435                case LOADER_SMS_ID:
1436                    Log.v(TAG, "LOADER_SMS_ID");
1437                    loader = new SmsInteractionsLoader(
1438                            QuickContactActivity.this,
1439                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1440                            MAX_SMS_RETRIEVE);
1441                    break;
1442                case LOADER_CALENDAR_ID:
1443                    Log.v(TAG, "LOADER_CALENDAR_ID");
1444                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
1445                    List<String> emailsList = null;
1446                    if (emailsArray != null) {
1447                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
1448                    }
1449                    loader = new CalendarInteractionsLoader(
1450                            QuickContactActivity.this,
1451                            emailsList,
1452                            MAX_FUTURE_CALENDAR_RETRIEVE,
1453                            MAX_PAST_CALENDAR_RETRIEVE,
1454                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
1455                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
1456                    break;
1457                case LOADER_CALL_LOG_ID:
1458                    Log.v(TAG, "LOADER_CALL_LOG_ID");
1459                    loader = new CallLogInteractionsLoader(
1460                            QuickContactActivity.this,
1461                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1462                            MAX_CALL_LOG_RETRIEVE);
1463            }
1464            return loader;
1465        }
1466
1467        @Override
1468        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
1469                List<ContactInteraction> data) {
1470            mRecentLoaderResults.put(loader.getId(), data);
1471
1472            if (isAllRecentDataLoaded()) {
1473                bindRecentData();
1474            }
1475        }
1476
1477        @Override
1478        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
1479            mRecentLoaderResults.remove(loader.getId());
1480        }
1481    };
1482
1483    private boolean isAllRecentDataLoaded() {
1484        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
1485    }
1486
1487    private void bindRecentData() {
1488        final List<ContactInteraction> allInteractions = new ArrayList<>();
1489        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
1490            allInteractions.addAll(loaderInteractions);
1491        }
1492
1493        // Sort the interactions by most recent
1494        Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
1495            @Override
1496            public int compare(ContactInteraction a, ContactInteraction b) {
1497                return a.getInteractionDate() >= b.getInteractionDate() ? -1 : 1;
1498            }
1499        });
1500
1501
1502        List<List<Entry>> interactionsWrapper = new ArrayList<>();
1503        interactionsWrapper.add(contactInteractionsToEntries(allInteractions));
1504        if (allInteractions.size() > 0) {
1505            mRecentCard.initialize(interactionsWrapper,
1506                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
1507                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
1508                    mExpandingEntryCardViewListener);
1509            mRecentCard.setVisibility(View.VISIBLE);
1510        }
1511
1512        // About card is initialized along with the contact card, but since it appears after
1513        // the recent card in the UI, we hold off until making it visible until the recent card
1514        // is also ready to avoid stuttering.
1515        if (mAboutCard.shouldShow()) {
1516            mAboutCard.setVisibility(View.VISIBLE);
1517        } else {
1518            mAboutCard.setVisibility(View.GONE);
1519        }
1520    }
1521
1522    @Override
1523    protected void onStop() {
1524        super.onStop();
1525
1526        if (mEntriesAndActionsTask != null) {
1527            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
1528            // results on the UI thread. In some circumstances Activities are killed without
1529            // onStop() being called. This is not a problem, because in these circumstances
1530            // the entire process will be killed.
1531            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
1532        }
1533    }
1534
1535    /**
1536     * Returns true if it is possible to edit the current contact.
1537     */
1538    private boolean isContactEditable() {
1539        return mContactData != null && !mContactData.isDirectoryEntry();
1540    }
1541
1542    private Intent getEditContactIntent() {
1543        final Intent intent = new Intent(Intent.ACTION_EDIT, mLookupUri);
1544        mContactLoader.cacheResult();
1545        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
1546        return intent;
1547    }
1548
1549    private void editContact() {
1550        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
1551    }
1552
1553    private void toggleStar(MenuItem starredMenuItem) {
1554        // Make sure there is a contact
1555        if (mLookupUri != null) {
1556            // Read the current starred value from the UI instead of using the last
1557            // loaded state. This allows rapid tapping without writing the same
1558            // value several times
1559            final boolean isStarred = starredMenuItem.isChecked();
1560
1561            // To improve responsiveness, swap out the picture (and tag) in the UI already
1562            ContactDetailDisplayUtils.configureStarredMenuItem(starredMenuItem,
1563                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1564                    !isStarred);
1565
1566            // Now perform the real save
1567            final Intent intent = ContactSaveService.createSetStarredIntent(
1568                    QuickContactActivity.this, mLookupUri, !isStarred);
1569            startService(intent);
1570
1571            final CharSequence accessibilityText = !isStarred
1572                    ? getResources().getText(R.string.description_action_menu_add_star)
1573                    : getResources().getText(R.string.description_action_menu_remove_star);
1574            // Accessibility actions need to have an associated view. We can't access the MenuItem's
1575            // underlying view, so put this accessibility action on the root view.
1576            mScroller.announceForAccessibility(accessibilityText);
1577        }
1578    }
1579
1580    /**
1581     * Calls into the contacts provider to get a pre-authorized version of the given URI.
1582     */
1583    private Uri getPreAuthorizedUri(Uri uri) {
1584        final Bundle uriBundle = new Bundle();
1585        uriBundle.putParcelable(ContactsContract.Authorization.KEY_URI_TO_AUTHORIZE, uri);
1586        final Bundle authResponse = getContentResolver().call(
1587                ContactsContract.AUTHORITY_URI,
1588                ContactsContract.Authorization.AUTHORIZATION_METHOD,
1589                null,
1590                uriBundle);
1591        if (authResponse != null) {
1592            return (Uri) authResponse.getParcelable(
1593                    ContactsContract.Authorization.KEY_AUTHORIZED_URI);
1594        } else {
1595            return uri;
1596        }
1597    }
1598
1599    private void shareContact() {
1600        final String lookupKey = mContactData.getLookupKey();
1601        Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
1602        if (mContactData.isUserProfile()) {
1603            // User is sharing the profile.  We don't want to force the receiver to have
1604            // the highly-privileged READ_PROFILE permission, so we need to request a
1605            // pre-authorized URI from the provider.
1606            shareUri = getPreAuthorizedUri(shareUri);
1607        }
1608
1609        final Intent intent = new Intent(Intent.ACTION_SEND);
1610        intent.setType(Contacts.CONTENT_VCARD_TYPE);
1611        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
1612
1613        // Launch chooser to share contact via
1614        final CharSequence chooseTitle = getText(R.string.share_via);
1615        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
1616
1617        try {
1618            this.startActivity(chooseIntent);
1619        } catch (final ActivityNotFoundException ex) {
1620            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
1621        }
1622    }
1623
1624    /**
1625     * Creates a launcher shortcut with the current contact.
1626     */
1627    private void createLauncherShortcutWithContact() {
1628        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
1629                new OnShortcutIntentCreatedListener() {
1630
1631                    @Override
1632                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
1633                        // Broadcast the shortcutIntent to the launcher to create a
1634                        // shortcut to this contact
1635                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
1636                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
1637
1638                        // Send a toast to give feedback to the user that a shortcut to this
1639                        // contact was added to the launcher.
1640                        Toast.makeText(QuickContactActivity.this,
1641                                R.string.createContactShortcutSuccessful,
1642                                Toast.LENGTH_SHORT).show();
1643                    }
1644
1645                });
1646        builder.createContactShortcutIntent(mLookupUri);
1647    }
1648
1649    @Override
1650    public boolean onCreateOptionsMenu(Menu menu) {
1651        final MenuInflater inflater = getMenuInflater();
1652        inflater.inflate(R.menu.quickcontact, menu);
1653        return true;
1654    }
1655
1656    @Override
1657    public boolean onPrepareOptionsMenu(Menu menu) {
1658        if (mContactData != null) {
1659            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
1660            ContactDetailDisplayUtils.configureStarredMenuItem(starredMenuItem,
1661                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1662                    mContactData.getStarred());
1663            // Configure edit MenuItem
1664            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
1665            editMenuItem.setVisible(true);
1666            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
1667                    .isInvisibleAndAddable(mContactData, this)) {
1668                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
1669                editMenuItem.setTitle(R.string.menu_add_contact);
1670            } else if (isContactEditable()) {
1671                editMenuItem.setIcon(R.drawable.ic_create_24dp);
1672                editMenuItem.setTitle(R.string.menu_editContact);
1673            } else {
1674                editMenuItem.setVisible(false);
1675            }
1676            return true;
1677        }
1678        return false;
1679    }
1680
1681    @Override
1682    public boolean onOptionsItemSelected(MenuItem item) {
1683        switch (item.getItemId()) {
1684            case R.id.menu_star:
1685                toggleStar(item);
1686                return true;
1687            case R.id.menu_edit:
1688                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
1689                    DirectoryContactUtil.addToMyContacts(mContactData, this, getFragmentManager(),
1690                            mSelectAccountFragmentListener);
1691                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
1692                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
1693                } else if (isContactEditable()) {
1694                    editContact();
1695                }
1696                return true;
1697            case R.id.menu_share:
1698                shareContact();
1699                return true;
1700            case R.id.menu_create_contact_shortcut:
1701                createLauncherShortcutWithContact();
1702                return true;
1703            default:
1704                return super.onOptionsItemSelected(item);
1705        }
1706    }
1707}
1708