QuickContactActivity.java revision 2a4207fb39330e840436215c896cde911489e111
1edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/*
2edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Copyright (C) 2009 The Android Open Source Project
3edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann *
4edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Licensed under the Apache License, Version 2.0 (the "License");
5edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * you may not use this file except in compliance with the License.
6edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * You may obtain a copy of the License at
7edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann *
8edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann *      http://www.apache.org/licenses/LICENSE-2.0
9edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann *
10edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Unless required by applicable law or agreed to in writing, software
11edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * distributed under the License is distributed on an "AS IS" BASIS,
12edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * See the License for the specific language governing permissions and
14edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * limitations under the License.
15edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
16edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
17edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannpackage com.android.contacts.quickcontact;
18edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
198477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.Animator;
208477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.Animator.AnimatorListener;
218477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.AnimatorListenerAdapter;
228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.animation.ArgbEvaluator;
23b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport android.animation.ObjectAnimator;
24edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.app.Activity;
25d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.app.Fragment;
26cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.app.LoaderManager.LoaderCallbacks;
27eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.app.SearchManager;
28752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.content.ActivityNotFoundException;
29edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.ContentUris;
30ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.content.ContentValues;
31edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.Intent;
32cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.content.Loader;
33dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.PackageManager;
34dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.ResolveInfo;
3531b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.Bitmap;
368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.Color;
37eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuff;
38eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuffColorFilter;
3931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.BitmapDrawable;
408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.drawable.ColorDrawable;
4131b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.Drawable;
42eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.ParseException;
43edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.net.Uri;
44eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.WebAddress;
4531b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.os.AsyncTask;
46edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.os.Bundle;
478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.os.Trace;
48eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.CalendarContract;
49752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract;
50edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Email;
51eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Event;
52eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;
53eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Identity;
54eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Im;
55eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Nickname;
56eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Note;
57eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Organization;
58edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Phone;
59eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Relation;
60edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
61edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
62edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Website;
63752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract.Contacts;
6460e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulosimport android.provider.ContactsContract.Data;
65eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.DisplayNameSources;
66ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.provider.ContactsContract.DataUsageFeedback;
67edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.QuickContact;
68edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.RawContacts;
6931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
70edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
71edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
72eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.util.Pair;
732a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu;
742a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu.ContextMenuInfo;
75d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
76d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
77eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
78edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View;
79edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View.OnClickListener;
802a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.View.OnCreateContextMenuListener;
8110d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawaimport android.view.WindowManager;
82edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.ImageView;
830cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulosimport android.widget.LinearLayout;
84edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
85d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
86edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
8772b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
88d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
892d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
90e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
91eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
922a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.common.ClipboardUtils;
93eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
94eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
95d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.editor.SelectAccountDialogFragment;
9631b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport com.android.contacts.common.lettertiles.LetterTileDrawable;
97752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder;
98752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
990d5588da244d0992c3ff8f25d0875fdf95a8c644Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
100cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.Contact;
101cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.ContactLoader;
102cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.RawContact;
103428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.AccountType;
104d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.model.account.AccountWithDataSet;
105cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.DataItem;
106428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.dataitem.DataKind;
107cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.EmailDataItem;
108eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.EventDataItem;
109cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.ImDataItem;
110eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NicknameDataItem;
111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NoteDataItem;
112eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.OrganizationDataItem;
113b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.common.model.dataitem.PhoneDataItem;
114eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.RelationDataItem;
115eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.SipAddressDataItem;
116eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredNameDataItem;
117eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredPostalDataItem;
118eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.WebsiteDataItem;
119eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.util.DateUtils;
120faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils;
121faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
122333091ae754ddfc25714c14b9b89534be24379f9Paul Soulosimport com.android.contacts.detail.ContactDisplayUtils;
123899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
124ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1258a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
126b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
127b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1282d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
1292a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryContextMenuInfo;
130e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
131e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
132eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
133b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
134eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
135b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
136b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
137e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.base.Preconditions;
138e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
139e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
1402d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
141899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
142eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
143b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
144b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
145eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
146edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
147edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
148b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
149edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
150edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
151edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
152edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
153edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
154edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
155d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellpublic class QuickContactActivity extends ContactsActivity {
1568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
1588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
1598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
1608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
1618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
1628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
1638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
164edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
165edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1669b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
1679b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1689b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
1698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
1709b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xB2, 0, 0, 0);
171ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
172dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
173752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
174752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
175752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
176edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
177edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
178edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
179edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
180e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
181e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
182e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE = "Add to circle";
183e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
184e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
185e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_HANGOUTS_VIDEO = "Start video call";
186e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
187edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
188edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
1898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
1908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
1918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
192edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1936219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee    private ImageView mPhotoView;
1940d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell    private View mTransparentView;
195eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
1966095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
197b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
198eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
199eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
200eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * This list contains all the {@link DataItem}s. Each nested list contains all data items of a
201eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * specific mimetype in sorted order, using mWithinMimeTypeDataItemComparator. The mimetype
202eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * lists are sorted using mAmongstMimeTypeDataItemComparator.
203eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
204eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private List<List<DataItem>> mDataItemsList;
205eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
206eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * A map between a mimetype string and the corresponding list of data items. The data items
207eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * are in sorted order using mWithinMimeTypeDataItemComparator.
208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
209eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Map<String, List<DataItem>> mDataItemsMap;
210b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
211d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
212eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private AsyncTask<Void, Void, Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>
213eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mEntriesAndActionsTask;
2149b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
215a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
2168477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsWaitingForOtherPieceOfExitAnimation;
2178477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
2188571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
219edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
2206cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
2219815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
2229b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
2239815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
2249758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
2259758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
226edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
227cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
228edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
229edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
230edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
231edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
232edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
233cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
234405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
235edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
236a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private static final List<String> SORTED_ABOUT_CARD_MIMETYPES = Lists.newArrayList(
237a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Nickname.CONTENT_ITEM_TYPE,
238a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name is inserted after nickname if it is available.
239a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // No mimetype for phonetic name exists.
240a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Website.CONTENT_ITEM_TYPE,
241a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Organization.CONTENT_ITEM_TYPE,
242a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Event.CONTENT_ITEM_TYPE,
243a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Relation.CONTENT_ITEM_TYPE,
244a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Im.CONTENT_ITEM_TYPE,
245a153dba33c2152332c5706178a367f52b0550723Paul Soulos            GroupMembership.CONTENT_ITEM_TYPE,
246a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Identity.CONTENT_ITEM_TYPE,
247a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Note.CONTENT_ITEM_TYPE);
248eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
249b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
250b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
251b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
252ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
253ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
254ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
255b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
256b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
257b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
258ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
259ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
260899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
261ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
262ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
263899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
264899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
265899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
266899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            180L * 24L * 60L * 60L * 1000L /* 180 days */;
267899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
268899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            36L * 60L * 60L * 1000L /* 36 hours */;
269899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
270ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
271ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
272ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
2736095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
2746095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
2756095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
276ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
277ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
278ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
279ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
280ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
281ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
282a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults = new HashMap<>();
283b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
284d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
2856cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
2862d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
2872d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
2882d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
289ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data Id is stored as the entry view id
290ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            final int dataId = v.getId();
2916095369885edcca566a812b551886e29c7ff8039Brian Attwell            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
2926095369885edcca566a812b551886e29c7ff8039Brian Attwell                editContact();
2936095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
2946095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
2956095369885edcca566a812b551886e29c7ff8039Brian Attwell            final Object intentObject = v.getTag();
296ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (intentObject == null || !(intentObject instanceof Intent)) {
297ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Intent tag was not used correctly");
2982d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
2992d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
300ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            final Intent intent = (Intent) intentObject;
301ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
302ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
303ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
304ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
305ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
30620bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            final Uri intentUri = intent.getData();
30720bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            if ((intentUri != null && intentUri.getScheme() != null &&
30820bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                    intentUri.getScheme().equals(CallUtil.SCHEME_SMSTO)) ||
309ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
310ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
311ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
312ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
313ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
314ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
31520bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                final Uri dataUsageUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
316ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
317ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
318ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
319ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                final boolean successful = getContentResolver().update(
32020bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                        dataUsageUri, new ContentValues(), null, null) > 0;
321ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                if (!successful) {
322ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    Log.w(TAG, "DataUsageFeedback increment failed");
323ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
324ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
325ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
326ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
327ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
328ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            startActivity(intent);
3292d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
3302d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
3312d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
332e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
333e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
334e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
335e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
336e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
337e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
3380cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
3390cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        @Override
3400cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        public void onExpand(int heightDelta) {
3410cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos            mScroller.prepareForExpandingScrollChild();
3420cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        }
343e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
344e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
3452a4207fb39330e840436215c896cde911489e111Paul Soulos    private final OnCreateContextMenuListener mEntryContextMenuListener =
3462a4207fb39330e840436215c896cde911489e111Paul Soulos            new OnCreateContextMenuListener() {
3472a4207fb39330e840436215c896cde911489e111Paul Soulos        @Override
3482a4207fb39330e840436215c896cde911489e111Paul Soulos        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3492a4207fb39330e840436215c896cde911489e111Paul Soulos            if (menuInfo == null) {
3502a4207fb39330e840436215c896cde911489e111Paul Soulos                return;
3512a4207fb39330e840436215c896cde911489e111Paul Soulos            }
3522a4207fb39330e840436215c896cde911489e111Paul Soulos            EntryContextMenuInfo info = (EntryContextMenuInfo) menuInfo;
3532a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.setHeaderTitle(info.getCopyText());
3542a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.add(R.string.copy_text);
3552a4207fb39330e840436215c896cde911489e111Paul Soulos        }
3562a4207fb39330e840436215c896cde911489e111Paul Soulos    };
3572a4207fb39330e840436215c896cde911489e111Paul Soulos
3582a4207fb39330e840436215c896cde911489e111Paul Soulos    @Override
3592a4207fb39330e840436215c896cde911489e111Paul Soulos    public boolean onContextItemSelected(MenuItem item) {
3602a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo menuInfo;
3612a4207fb39330e840436215c896cde911489e111Paul Soulos        try {
3622a4207fb39330e840436215c896cde911489e111Paul Soulos            menuInfo = (EntryContextMenuInfo) item.getMenuInfo();
3632a4207fb39330e840436215c896cde911489e111Paul Soulos        } catch (ClassCastException e) {
3642a4207fb39330e840436215c896cde911489e111Paul Soulos            Log.e(TAG, "bad menuInfo", e);
3652a4207fb39330e840436215c896cde911489e111Paul Soulos            return false;
3662a4207fb39330e840436215c896cde911489e111Paul Soulos        }
3672a4207fb39330e840436215c896cde911489e111Paul Soulos
3682a4207fb39330e840436215c896cde911489e111Paul Soulos        ClipboardUtils.copyText(this, menuInfo.getCopyLabel(), menuInfo.getCopyText(), true);
3692a4207fb39330e840436215c896cde911489e111Paul Soulos        return true;
3702a4207fb39330e840436215c896cde911489e111Paul Soulos    }
3712a4207fb39330e840436215c896cde911489e111Paul Soulos
372d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
373d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
374d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
375d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
376d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
377d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
378d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
379d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
380d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
381d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
382d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
383d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
384d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
385d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
386d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
387d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
388d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
389d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
390d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
391d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
392d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
393d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
394d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
395d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
396d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
397d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
398d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
399d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
400d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
401d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
402b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
403b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
404b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
405b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
4068477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsWaitingForOtherPieceOfExitAnimation) {
4078477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                finish();
4088477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                return;
4098477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
4108477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = false;
411b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
4128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
4138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
4148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
4158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
4168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
4178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
4188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
4198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
4208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
4218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
4228477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
4238477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
4248477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
4258477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            // Remove the window shim now that we are starting an Activity exit animation.
4268477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            final int duration = getResources().getInteger(android.R.integer.config_shortAnimTime);
4279b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final ObjectAnimator animator = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0xFF, 0);
4288477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            animator.addListener(mExitWindowShimAnimationListener);
4298477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            animator.setDuration(duration).start();
4308477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = true;
4318477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
4328477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
4338477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    };
4348477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
4358477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    final AnimatorListener mExitWindowShimAnimationListener = new AnimatorListenerAdapter() {
4368477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
4378477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onAnimationEnd(Animator animation) {
4388477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsWaitingForOtherPieceOfExitAnimation) {
4398477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                finish();
4408477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                return;
4418477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
4428477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = false;
4438477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
444b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
445b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
446eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
447eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
448eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
449eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
450eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
451eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
452eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
453eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
454eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
455eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
456eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
457eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
458eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
459eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
460eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
461eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
462eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
463eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
464eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
465eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
466eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
467eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
468eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
469eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
470eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
471eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
472eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
473eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
474eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
475eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
476eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
477eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
478eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
479eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
480eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
481eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
482cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
483cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
484cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 1. Times used
485cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 2. Last time used
486cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 3. Statically defined
487cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
488eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
489eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
490eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
491eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
492eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem lhs = lhsList.get(0);
493eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem rhs = rhsList.get(0);
494eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
495eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
496eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
497eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
498eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
499eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
500eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
501eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
502eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
503eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
504eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
505eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
506eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
507eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
508eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
509eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
510eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
511eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
512eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Times used and last time used are the same. Resort to statically defined.
513eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String lhsMimeType = lhs.getMimeType();
514eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String rhsMimeType = rhs.getMimeType();
515eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (String mimeType : LEADING_MIMETYPES) {
516eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (lhsMimeType.equals(mimeType)) {
517eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return -1;
518eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                } else if (rhsMimeType.equals(mimeType)) {
519eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return 1;
520eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
521eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
522eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
523eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
524eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
525eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
526edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
5278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
5288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
5298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
530edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getWindow().setStatusBarColor(Color.TRANSPARENT);
5322426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
533d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
534dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
53510d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
53610d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
53710d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
53810d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
539edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
540edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
541a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
542a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
5430cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
5440cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
545eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
5466095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
547b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
548eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
549edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5506095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
551eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
552eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
55323889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
5542a4207fb39330e840436215c896cde911489e111Paul Soulos        mContactCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
555b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
556b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
557b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
558edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
559eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
5602a4207fb39330e840436215c896cde911489e111Paul Soulos        mAboutCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
561eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
562d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mPhotoView = (ImageView) findViewById(R.id.photo);
5630d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        mTransparentView = findViewById(R.id.transparent_view);
5640d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
5650d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            mTransparentView.setOnClickListener(new OnClickListener() {
5660d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
5670d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
5680d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
5690d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
5700d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
5710d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
572edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
573d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
574d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
5759b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
5769b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
5779b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
5789b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
5796219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
5808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
5818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5829b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
5839b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
5848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (!mHasAlreadyBeenOpened) {
5858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            final int duration = getResources().getInteger(android.R.integer.config_shortAnimTime);
5869b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            ObjectAnimator.ofInt(mWindowScrim, "alpha", 0, 0xFF).setDuration(duration).start();
5878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
588edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
589c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
590ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
591ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
592ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
5938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5949b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
5959b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
596d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
597d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
598d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
599d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
600d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
601d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
602d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
603d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
604d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
60523889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
6069b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
6079b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
608ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
609ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
610ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
611ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
612ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
613ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
614ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
615ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
616ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
617ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
618ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
619ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
620ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
621ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
622a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
623a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
6249b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
625ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
626ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
6279b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
6289b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
6298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
6308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
6318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
632405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
633405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
6348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
6358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED) {
6368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // The contact that we were showing has been deleted.
6378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
638b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
6398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
640dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
6418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
6428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
6438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
6448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
6458571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
646d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
647d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
648d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
6499b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
6509b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
6519b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
6529b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
6539b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilter.getColor());
6549b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
6559b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
6569b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
657d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
658d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
659d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
660d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
661d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
662d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
663d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
664d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
665d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
666d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE,
667d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                QuickContact.MODE_LARGE);
668d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
669d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
670d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mLookupUri = Preconditions.checkNotNull(lookupUri, "missing lookupUri");
671d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
672d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
673d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
674d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
675d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
676d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
677d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // we need to restart the loader and reload the new contact.
678899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            for (int interactionLoaderId : mRecentLoaderIds) {
679899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                getLoaderManager().destroyLoader(interactionLoaderId);
680899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            }
681405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
682405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
683d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
6842d150da246632b1649999cfabed776133b097775Brian Attwell
6852d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
6862426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
687edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
688b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
6898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
6908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
6918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
6928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
693c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
694b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
695b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
69681281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
697d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
6989b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
699f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString());
7009b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
70181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
70281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
70381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
704f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell    private void setHeaderNameText(String value) {
705d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
7069b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
707f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell                mScroller.setTitle(value);
7089b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
70981281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
71081281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
71181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
71281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
713edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
714edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
715edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
716edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
717edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
718edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
719edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
720edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
721edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
722edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
723edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
724edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
725edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
726edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
727cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
728edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
7298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
7308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
7316cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
732d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
733edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
7358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
736dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
7376219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
73831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
7394936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        analyzeWhitenessOfPhotoAsynchronously();
740333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos        setHeaderNameText(ContactDisplayUtils.getDisplayName(this, data).toString());
741edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
743dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
744eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mEntriesAndActionsTask = new AsyncTask<Void, Void,
745eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>() {
7468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
748eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            protected Pair<List<List<DataItem>>, Map<String, List<DataItem>>> doInBackground(
749eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
750eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
7518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
7528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
754eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            protected void onPostExecute(Pair<List<List<DataItem>>,
755eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Map<String, List<DataItem>>> dataItemsPair) {
756eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                super.onPostExecute(dataItemsPair);
757eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mDataItemsList = dataItemsPair.first;
758eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mDataItemsMap = dataItemsPair.second;
7598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
7608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
7618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
7628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
763eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    bindDataToCards();
7648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
7658a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
7668a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
7678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
7688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
7698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
7708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
771eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void bindDataToCards() {
772eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        startInteractionLoaders();
773eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        populateContactAndAboutCard();
774eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
775eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
776eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void startInteractionLoaders() {
777eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<DataItem> phoneDataItems = mDataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
778eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
779eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
780eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
781eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
782eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
783eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
784eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
785ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
786eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
787eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
788eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
7898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
7908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
791ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
792ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
793ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
794ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
795ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
796ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
797ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
798ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
7998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
800899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
8018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
802eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
803899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
804eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<DataItem> emailDataItems = mDataItemsMap.get(Email.CONTENT_ITEM_TYPE);
805eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
806eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
807eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
808eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
809eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
810eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
811eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
812ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
813eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
814899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
815899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
816ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
817899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
8188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
8198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
8218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
8228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
8238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
8248a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
8258a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
8268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
8278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
8288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
8298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
8308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
8318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
8328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
834a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private List<List<Entry>> buildAboutCardEntries() {
835a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
836a153dba33c2152332c5706178a367f52b0550723Paul Soulos        for (String mimetype : SORTED_ABOUT_CARD_MIMETYPES) {
837a153dba33c2152332c5706178a367f52b0550723Paul Soulos            final List<DataItem> mimeTypeItems = mDataItemsMap.get(mimetype);
838a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (mimeTypeItems == null) {
839a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
840a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
841a153dba33c2152332c5706178a367f52b0550723Paul Soulos            final List<Entry> aboutEntries = dataItemsToEntries(mimeTypeItems);
842a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutEntries.size() > 0) {
843a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(aboutEntries);
844a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
845a153dba33c2152332c5706178a367f52b0550723Paul Soulos        }
846a153dba33c2152332c5706178a367f52b0550723Paul Soulos        return aboutCardEntries;
847a153dba33c2152332c5706178a367f52b0550723Paul Soulos    }
848a153dba33c2152332c5706178a367f52b0550723Paul Soulos
849eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void populateContactAndAboutCard() {
850eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
851eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
85260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        final List<List<Entry>> contactCardEntries = new ArrayList<>();
853a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = buildAboutCardEntries();
854eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
855eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (int i = 0; i < mDataItemsList.size(); ++i) {
856eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final List<DataItem> dataItemsByMimeType = mDataItemsList.get(i);
857eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final DataItem topDataItem = dataItemsByMimeType.get(0);
858a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
859a153dba33c2152332c5706178a367f52b0550723Paul Soulos                // About card mimetypes are built in buildAboutCardEntries, skip here
860a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
86160e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            } else {
86260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                List<Entry> contactEntries = dataItemsToEntries(mDataItemsList.get(i));
86360e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                if (contactEntries.size() > 0) {
86460e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                    contactCardEntries.add(contactEntries);
865eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
866eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
867eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
868eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
869eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
870eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
871eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
872c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mContactCard.isExpanded(),
87389966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                    /* isAlwaysExpanded = */ false,
8740cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener,
8750cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mScroller);
876eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
877eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
878eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
879eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
880eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
8818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
882eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
883c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        // Phonetic name is not a data item, so the entry needs to be created separately
884c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        final String phoneticName = mContactData.getPhoneticName();
885c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        if (!TextUtils.isEmpty(phoneticName)) {
886c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            Entry phoneticEntry = new Entry(/* viewId = */ -1,
887c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* icon = */ null,
888c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    getResources().getString(R.string.name_phonetic),
889c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    phoneticName,
890c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* text = */ null,
891c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* intent = */ null,
892c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIcon = */ null,
893c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIntent = */ null,
894c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateContentDescription = */ null,
895c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* shouldApplyColor = */ false,
8962a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
8972a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ new EntryContextMenuInfo(phoneticName,
8982a4207fb39330e840436215c896cde911489e111Paul Soulos                            getResources().getString(R.string.name_phonetic)));
899c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            List<Entry> phoneticList = new ArrayList<>();
900c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            phoneticList.add(phoneticEntry);
901a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name comes after nickname. Check to see if the first entry type is nickname
902a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutCardEntries.get(0).get(0).getHeader().equals(
903a153dba33c2152332c5706178a367f52b0550723Paul Soulos                    getResources().getString(R.string.header_nickname_entry))) {
904a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(1, phoneticList);
905a153dba33c2152332c5706178a367f52b0550723Paul Soulos            } else {
906a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(0, phoneticList);
907a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
908c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        }
909c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos
910eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.initialize(aboutCardEntries,
911eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                /* numInitialVisibleEntries = */ 1,
912eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                /* isExpanded = */ true,
91389966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                /* isAlwaysExpanded = */ true,
9140cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                mExpandingEntryCardViewListener,
9150cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                mScroller);
9166095369885edcca566a812b551886e29c7ff8039Brian Attwell
9176095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
9186095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
9196095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
9206095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
9216095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
9226095369885edcca566a812b551886e29c7ff8039Brian Attwell
923a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
924a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
925a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
926a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
927a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
928eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
929eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
930eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
931eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
9326095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
9336095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
9346095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
9356095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
9366095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
9376095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
9386095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
939dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* subHeader = */ null, /* text = */ null, getEditContactIntent(),
940dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
941714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
9422a4207fb39330e840436215c896cde911489e111Paul Soulos                /* isEditable = */ false, /* EntryContextMenuInfo = */ null);
9436095369885edcca566a812b551886e29c7ff8039Brian Attwell
9446095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
9456095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
9466095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
9476095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
948dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* text = */ null, getEditContactIntent(), /* alternateIcon = */ null,
949dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
9502a4207fb39330e840436215c896cde911489e111Paul Soulos                /* shouldApplyColor = */ true, /* isEditable = */ false,
9512a4207fb39330e840436215c896cde911489e111Paul Soulos                /* EntryContextMenuInfo = */ null);
9526095369885edcca566a812b551886e29c7ff8039Brian Attwell
9536095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
9546095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
9556095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
9566095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
9576095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
9586095369885edcca566a812b551886e29c7ff8039Brian Attwell
9596095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
9606095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
9616095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
9626095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
963c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
9640cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener, mScroller);
9656095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
9666095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
9676095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
9686095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
9696095369885edcca566a812b551886e29c7ff8039Brian Attwell
9706095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
971eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
972eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
973eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
974eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
975eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
976eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
977eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Pair<List<List<DataItem>>, Map<String, List<DataItem>>> generateDataModelFromContact(
978eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
979eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
980eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
981eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
9828bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
9838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
984851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
985851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
986eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
987eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
988851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
989eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
990eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
99147b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
99247b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
99347b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
994eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
995cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
996eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
997b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
998eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
999eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
1000899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
1001eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
1002edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1003eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
1004eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
1005eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
1006eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
1007edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
1008eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
1009edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1010edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
10118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1012edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1013eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
101416339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
101516339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
1016eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
1017eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
1018eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
101916339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
1020eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
1021eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
1022eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
1023eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
1024eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
1025eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
1026eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
1027eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
1028edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1029eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1030edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1031eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
1032eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
1033eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
1034eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
103516339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
1036eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return new Pair<>(dataItemsList, dataItemsMap);
1037eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1038edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1039eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
1040eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
1041eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
1042eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
1043eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
1044eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
1045eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Entry dataItemToEntry(DataItem dataItem) {
1046eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
1047eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
1048eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
1049eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
1050eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
1051eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
1052eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
105348ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
1054dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
1055dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
1056dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        String alternateContentDescription = null;
1057eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
10582a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo entryContextMenuInfo = null;
1059eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1060eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
1061eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1062eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
1063eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
1064eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = ContactsUtils.buildImIntent(this, im).first;
1065eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
10667de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            final int protocol;
10677de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (!im.isProtocolValid()) {
10687de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = Im.PROTOCOL_CUSTOM;
10697de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
10707de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
10717de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
10727de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (protocol == Im.PROTOCOL_CUSTOM) {
10737de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // If the protocol is custom, display the "IM" entry header as well to distinguish
10747de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // this entry from other ones
10757de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                header = getResources().getString(R.string.header_im_entry);
10767de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = Im.getProtocolLabel(getResources(), protocol,
10777de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
10787de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                text = im.getData();
10797de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
10807de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                header = Im.getProtocolLabel(getResources(), protocol,
10817de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
10827de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = im.getData();
10837de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
10842a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(im.getData(), header);
1085eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
1086eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
1087eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_organization_entry);
1088eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
10892a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1090eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
1091eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
1092eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
1093eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
1094eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
1095eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                (mContactData.getNameRawContactId() == dataItem.getRawContactId());
1096eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1097eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
1098eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
1099eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                && mContactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
1100eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1101eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
1102eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = getResources().getString(R.string.header_nickname_entry);
1103eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
11042a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1105eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1106eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
1107eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
1108eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_note_entry);
1109eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
11102a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
1112eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
1113eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_website_entry);
1114eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
11152a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1116eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
1117eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final WebAddress webAddress = new WebAddress(website.buildDataString(this, kind));
1118eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1119eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
1120eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.e(TAG, "Couldn't parse website: " + website.buildDataString(this, kind));
1121eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1122eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1123eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
1124eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String dataString = event.buildDataString(this, kind);
1125eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1126eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1127eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1128eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1129eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1130eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1131eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1132eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1133eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1134eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_event_entry);
1135f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
11367de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = Event.getTypeLabel(getResources(), event.getKindTypeColumn(kind),
11377de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        event.getLabel()).toString();
1138f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1139eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = DateUtils.formatDate(this, dataString);
1140eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1141eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
1142eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String dataString = relation.buildDataString(this, kind);
1143eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1144eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1145eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1146eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1147eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1148eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_relation_entry);
1149eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
11502a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1151f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
1152f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                text = Relation.getTypeLabel(getResources(), relation.getKindTypeColumn(kind),
1153f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1154f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1155eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1156eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
1157eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
1158eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = phone.buildDataString(this, kind);
11592a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
11602a4207fb39330e840436215c896cde911489e111Paul Soulos                        getResources().getString(R.string.phoneLabelsGroup));
1161f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
1162f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = Phone.getTypeLabel(getResources(), phone.getKindTypeColumn(kind),
1163f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            phone.getLabel()).toString();
1164f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1165eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_phone_24dp);
1166eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (PhoneCapabilityTester.isPhone(this)) {
1167eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
116816339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1169dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
1170dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                        Uri.fromParts(CallUtil.SCHEME_SMSTO, phone.getNumber(), null));
1171dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon = getResources().getDrawable(R.drawable.ic_message_24dp);
1172dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateContentDescription = getResources().getString(R.string.sms_other);
1173eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1174eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1175eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1176eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1177eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
1178eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri mailUri = Uri.fromParts(CallUtil.SCHEME_MAILTO, address, null);
1179eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1180eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
11812a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
11822a4207fb39330e840436215c896cde911489e111Paul Soulos                        getResources().getString(R.string.emailLabelsGroup));
1183f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
1184f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = Email.getTypeLabel(getResources(), email.getKindTypeColumn(kind),
1185f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
1186f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1187eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_email_24dp);
1188eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1189eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1190eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1191eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1192eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
1193eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1194eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
11952a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
11962a4207fb39330e840436215c896cde911489e111Paul Soulos                        getResources().getString(R.string.postalLabelsGroup));
1197f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
1198f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = StructuredPostal.getTypeLabel(getResources(),
1199f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
1200f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
12016a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIntent =
12026a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                        StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
12036a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIcon = getResources().getDrawable(R.drawable.ic_directions_24dp);
1204eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_place_24dp);
1205eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1206eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
1207eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (PhoneCapabilityTester.isSipPhone(this)) {
1208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1209eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final String address = sip.getSipAddress();
1210eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (!TextUtils.isEmpty(address)) {
1211eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    final Uri callUri = Uri.fromParts(CallUtil.SCHEME_SIP, address, null);
1212eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
12131cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    header = address;
12142a4207fb39330e840436215c896cde911489e111Paul Soulos                    entryContextMenuInfo = new EntryContextMenuInfo(header,
12152a4207fb39330e840436215c896cde911489e111Paul Soulos                            getResources().getString(R.string.phoneLabelsGroup));
12161cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    if (sip.hasKindTypeColumn(kind)) {
12171cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                        text = SipAddress.getTypeLabel(getResources(), sip.getKindTypeColumn(kind),
12181cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                            sip.getLabel()).toString();
12191cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    }
1220eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // Note that this item will get a SIP-specific variant
1221eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // of the "call phone" icon, rather than the standard
1222eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // app icon for the Phone app (which we show for
1223eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // regular phone numbers.)  That's because the phone
1224eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // app explicitly specifies an android:icon attribute
1225eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // for the SIP-related intent-filters in its manifest.
12261cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    icon = ResolveCache.getInstance(this).getIcon(sip.getMimeType(), intent);
12271cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    // Call mutate to create a new Drawable.ConstantState for color filtering
12281cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    if (icon != null) {
12291cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                        icon.mutate();
12301cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    }
123116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1232edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1233eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
1234eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
1235eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(givenName)) {
1236eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mAboutCard.setTitle(getResources().getString(R.string.about_card_title) +
1237eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        " " + givenName);
1238eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
1239eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mAboutCard.setTitle(getResources().getString(R.string.about_card_title));
1240eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1241eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1242eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
1243eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = dataItem.buildDataStringForDisplay(this, kind);
1244eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1245eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
124660e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
124760e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
1248e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1249e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
1250e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
1251e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1252e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                // Attempt to use known icons for known 3p types. Otherwise default to ResolveCache
1253e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
1254e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
1255e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE.equals(
1256e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                intent.getDataString())) {
1257e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(
1258e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                    R.drawable.ic_add_to_circles_black_24);
1259e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
1260e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_google_plus_24dp);
1261e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1262e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1263e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
1264e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_HANGOUTS_VIDEO.equals(intent.getDataString())) {
1265e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_hangout_video_24dp);
1266e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
1267e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_hangout_24dp);
1268e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1269e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1270e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
12712a4207fb39330e840436215c896cde911489e111Paul Soulos                        entryContextMenuInfo = new EntryContextMenuInfo(header, mimetype);
1272e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        icon = ResolveCache.getInstance(this).getIcon(
1273e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
1274e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
1275e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
1276e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
1277e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
127848ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
1279e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
1280e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
1281eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1282b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1283eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
1284eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
1285eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!PhoneCapabilityTester.isIntentRegistered(this, intent)) {
1286eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
1287eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1288eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1289eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1290dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
1291dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
1292dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (!PhoneCapabilityTester.isIntentRegistered(this, alternateIntent)) {
1293dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
1294dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1295dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1296dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Attempt to use package manager to find a suitable content description if needed
1297dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (TextUtils.isEmpty(alternateContentDescription)) {
1298dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateContentDescription = getIntentResolveLabel(alternateIntent);
1299dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1300dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1301dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1302eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
1303eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
1304eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
1305eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
1306eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1307eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1308ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
1309ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
1310ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
1311dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon, intent,
1312dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon, alternateIntent, alternateContentDescription, shouldApplyColor,
13132a4207fb39330e840436215c896cde911489e111Paul Soulos                isEditable, entryContextMenuInfo);
1314eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1315eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1316eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private List<Entry> dataItemsToEntries(List<DataItem> dataItems) {
1317eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1318eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
1319eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Entry entry = dataItemToEntry(dataItem);
1320eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (entry != null) {
1321eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                entries.add(entry);
1322eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1323eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1324eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
1325edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1326edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1327dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    private String getIntentResolveLabel(Intent intent) {
1328dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent,
1329dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
1330dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1331dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
1332dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
1333dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
1334dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
1335dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
1336dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
1337dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = ResolveCache.getInstance(this).getBestResolve(intent, matches);
1338dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1339dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1340dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
1341dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
1342dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1343dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1344dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return String.valueOf(bestResolve.loadLabel(getPackageManager()));
1345dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
1346dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1347edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
134831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
134931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
135031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
135131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
135231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
135331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
135431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
135531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
135631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1357faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
135831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1359faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
1360faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
136131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
13628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
1363faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = colorFromBitmap(bitmap);
1364faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    if (primaryColor != 0) {
1365a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                        return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
1366faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                                primaryColor);
1367faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
13688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
13698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
1370faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
1371a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
137231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
1373a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
137431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
137531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
137631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1377faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
1378faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
13798571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
13808571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
13818571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
13828571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
13838571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
13848571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
13858571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
13868571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
13878571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
13888571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
13898571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
13908571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
1391faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
139231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
139331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
139431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
139531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
139631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
13974936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    /**
13984936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * Examine how many white pixels are in the bitmap in order to determine whether or not
13994936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * we need gradient overlays on top of the image.
14004936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     */
14014936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    private void analyzeWhitenessOfPhotoAsynchronously() {
14024936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
14034936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        new AsyncTask<Void, Void, Boolean>() {
14044936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
14054936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected Boolean doInBackground(Void... params) {
14064936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
14074936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
14084936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    return WhitenessUtils.isBitmapWhiteAtTopOrBottom(bitmap);
14094936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                }
14104936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                return !(imageViewDrawable instanceof LetterTileDrawable);
14114936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
14124936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
14134936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
14144936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected void onPostExecute(Boolean isWhite) {
14154936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                super.onPostExecute(isWhite);
14164936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                mScroller.setUseGradient(isWhite);
14174936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
14184936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        }.execute();
14194936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    }
14204936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
1421faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
14229b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
1423faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        final int primaryColor = palette.mPrimaryColor;
1424faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mScroller.setHeaderTintColor(primaryColor);
1425faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
14269b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
14278571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
14289b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
1429faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                new PorterDuffColorFilter(primaryColor, PorterDuff.Mode.SRC_ATOP);
1430faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mContactCard.setColorAndFilter(primaryColor, mColorFilter);
1431faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mRecentCard.setColorAndFilter(primaryColor, mColorFilter);
1432faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mAboutCard.setColorAndFilter(primaryColor, mColorFilter);
14339b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
14349b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
14358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
14368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller == null) {
14378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
14388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
14398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
14408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
14418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
14428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
14438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
14448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
14458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
14468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
1447847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
1448847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell                getWindow().getStatusBarColor(), desiredStatusBarColor);
1449847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
1450847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setEvaluator(new ArgbEvaluator());
1451847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.start();
14528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
14538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
14548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
14558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
14568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
14578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
1458a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
1459a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
14608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
14618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
14628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
14638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1464b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
1465eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1466b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
1467ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
1468ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
1469b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
1470b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
1471b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
1472b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
1473b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
1474b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
1475dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
1476dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
1477dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
147848ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
14792a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
14802a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ null));
1481b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1482b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
1483b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1484b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1485eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
1486851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
1487cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1488851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
1489405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
1490cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1491cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1492cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1493851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
14948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
14958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1496cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (isFinishing()) {
1497cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1498cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1499cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isError()) {
1500cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // This shouldn't ever happen, so throw an exception. The {@link ContactLoader}
1501cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // should log the actual exception.
1502cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                throw new IllegalStateException("Failed to load contact", data.getException());
1503cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1504cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isNotFound()) {
15058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (mHasAlreadyBeenOpened) {
15068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    finish();
15078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                } else {
15088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
15098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
15108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
15118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
1512cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1513cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1514cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1515b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            bindContactData(data);
1516cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
15178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.endSection();
1518cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1519cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1520cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1521851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
1522cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
1523cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
1524cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1525d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
1526d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
1527b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
1528d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
15298571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
1530cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1531cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
1532b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1533b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
1534b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
1535b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
15368477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
15378477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
15388477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
1539b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
1540b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
1541b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
1542b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
1543b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
15448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
15458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
15468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
15478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
15488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
15498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
15508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
15518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1552eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
1553b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
1554b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1555b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1556b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
1557b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Log.v(TAG, "onCreateLoader");
1558b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
1559b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
1560b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
1561b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    Log.v(TAG, "LOADER_SMS_ID");
1562b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
1563b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
1564ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1565b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
1566b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
1567899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
1568899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    Log.v(TAG, "LOADER_CALENDAR_ID");
1569ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
1570ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
1571ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
1572ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
1573ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
1574899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
1575899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
1576ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
1577899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
1578899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
1579899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
1580899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
1581899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
1582ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
1583ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    Log.v(TAG, "LOADER_CALL_LOG_ID");
1584ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
1585ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
1586ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1587ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
1588b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1589b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
1590b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1591b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1592b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1593b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
1594b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
1595b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
1596b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1597b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
1598b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
1599b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1600b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1601b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1602b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1603b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
1604b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
1605b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1606b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
1607b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1608b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
1609b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
1610b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1611b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1612b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
1613eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
1614b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
1615b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            allInteractions.addAll(loaderInteractions);
1616b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1617b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1618b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        // Sort the interactions by most recent
1619b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
1620b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
1621b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            public int compare(ContactInteraction a, ContactInteraction b) {
1622b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                return a.getInteractionDate() >= b.getInteractionDate() ? -1 : 1;
1623b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1624b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        });
1625b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
162697ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        // Wrap each interaction in its own list so that an icon is displayed for each entry
162760e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        List<List<Entry>> interactionsWrapper = new ArrayList<>();
162897ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        for (Entry contactInteraction : contactInteractionsToEntries(allInteractions)) {
162997ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            List<Entry> entryListWrapper = new ArrayList<>(1);
163097ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            entryListWrapper.add(contactInteraction);
163197ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            interactionsWrapper.add(entryListWrapper);
163297ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        }
1633b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        if (allInteractions.size() > 0) {
163460e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            mRecentCard.initialize(interactionsWrapper,
1635b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
1636c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
16370cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener, mScroller);
1638b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentCard.setVisibility(View.VISIBLE);
1639b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1640eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1641eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // About card is initialized along with the contact card, but since it appears after
1642eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // the recent card in the UI, we hold off until making it visible until the recent card
1643eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // is also ready to avoid stuttering.
1644eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (mAboutCard.shouldShow()) {
1645eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1646eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1647eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.GONE);
1648eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1649b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
16508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
16518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
16528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
16538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
16548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
16558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
16568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
16578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
16588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
16598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
16608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
16618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
16628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
166323889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
166423889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
1665d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
1666d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
1667d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
1668d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1669d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1670d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1671a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    /**
1672a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     * Returns true if it is possible to share the current contact.
1673a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     */
1674a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    private boolean isContactShareable() {
1675a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1676a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    }
1677a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
16786095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
1679d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Intent intent = new Intent(Intent.ACTION_EDIT, mLookupUri);
1680d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mContactLoader.cacheResult();
1681d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
16826095369885edcca566a812b551886e29c7ff8039Brian Attwell        return intent;
16836095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
16846095369885edcca566a812b551886e29c7ff8039Brian Attwell
16856095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
16866095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
1687d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1688d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1689d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
1690d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
1691d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mLookupUri != null) {
1692d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
1693d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
1694d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
1695d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
1696d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1697d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
1698333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
1699d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1700d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
1701d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1702d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
1703eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
1704d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    QuickContactActivity.this, mLookupUri, !isStarred);
1705d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
170635ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell
170735ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            final CharSequence accessibilityText = !isStarred
170835ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    ? getResources().getText(R.string.description_action_menu_add_star)
170935ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    : getResources().getText(R.string.description_action_menu_remove_star);
171035ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // Accessibility actions need to have an associated view. We can't access the MenuItem's
171135ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // underlying view, so put this accessibility action on the root view.
171235ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            mScroller.announceForAccessibility(accessibilityText);
1713d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
1714d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1715d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1716752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1717752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Calls into the contacts provider to get a pre-authorized version of the given URI.
1718752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1719752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private Uri getPreAuthorizedUri(Uri uri) {
1720752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle uriBundle = new Bundle();
1721752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        uriBundle.putParcelable(ContactsContract.Authorization.KEY_URI_TO_AUTHORIZE, uri);
1722752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle authResponse = getContentResolver().call(
1723752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.AUTHORITY_URI,
1724752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.Authorization.AUTHORIZATION_METHOD,
1725752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                null,
1726752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                uriBundle);
1727752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (authResponse != null) {
1728752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return (Uri) authResponse.getParcelable(
1729752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    ContactsContract.Authorization.KEY_AUTHORIZED_URI);
1730752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        } else {
1731752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return uri;
1732752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1733752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
17342d150da246632b1649999cfabed776133b097775Brian Attwell
1735752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
1736752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
1737752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
1738752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (mContactData.isUserProfile()) {
1739752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // User is sharing the profile.  We don't want to force the receiver to have
1740752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // the highly-privileged READ_PROFILE permission, so we need to request a
1741752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // pre-authorized URI from the provider.
1742752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            shareUri = getPreAuthorizedUri(shareUri);
1743752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1744752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1745752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
1746752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.setType(Contacts.CONTENT_VCARD_TYPE);
1747752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
1748752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1749752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
1750752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
1751752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
1752752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1753752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
1754752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            this.startActivity(chooseIntent);
1755eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
1756752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
1757752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1758752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1759752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1760752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1761752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
1762752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1763752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
1764752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
1765752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
1766752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1767752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
1768752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
1769752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
1770752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
1771752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
1772752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
1773752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1774752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
1775752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
1776752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        Toast.makeText(QuickContactActivity.this,
1777752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                R.string.createContactShortcutSuccessful,
1778752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
1779752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
1780752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1781752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
1782752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        builder.createContactShortcutIntent(mLookupUri);
1783752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1784752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1785d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1786d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
1787eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
1788d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
1789d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
1790d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1791d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1792d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1793d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
1794d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
1795d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
1796333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
1797d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1798d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
1799a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
1800d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
1801d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
1802d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
1803d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
1804d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
180530cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
18062e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
1807d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
1808d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
18092e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
1810d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
1811d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
1812d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
1813a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
1814a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            final MenuItem shareMenuItem = menu.findItem(R.id.menu_share);
1815a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            shareMenuItem.setVisible(isContactShareable());
1816a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
18178a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
1818d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
18198a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
1820d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1821d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1822d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1823d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
1824d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
1825d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
1826d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
1827d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1828d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
1829d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
1830d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    DirectoryContactUtil.addToMyContacts(mContactData, this, getFragmentManager(),
1831d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                            mSelectAccountFragmentListener);
1832d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
1833d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
1834d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
1835d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
1836d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
1837d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1838752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
1839752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                shareContact();
1840752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
1841752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
1842752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                createLauncherShortcutWithContact();
1843752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
1844d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
1845d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
1846d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
184723889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
1848edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
1849