QuickContactActivity.java revision 1f8a3fda87bf61e579d61e3fdd246548943d070a
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
198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.animation.ArgbEvaluator;
20b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport android.animation.ObjectAnimator;
21edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.app.Activity;
22d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.app.Fragment;
23cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.app.LoaderManager.LoaderCallbacks;
24eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.app.SearchManager;
25752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.content.ActivityNotFoundException;
26edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.ContentUris;
27ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.content.ContentValues;
28edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.Intent;
29cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.content.Loader;
30dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.PackageManager;
31dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.ResolveInfo;
3231b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.Bitmap;
338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.Color;
3470e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chenimport android.graphics.Point;
35eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuff;
36eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuffColorFilter;
3731b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.BitmapDrawable;
388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.drawable.ColorDrawable;
3931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.Drawable;
40eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.ParseException;
41edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.net.Uri;
42eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.WebAddress;
4331b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.os.AsyncTask;
44edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.os.Bundle;
458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.os.Trace;
46eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.CalendarContract;
47752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract;
48edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Email;
49eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Event;
50eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;
51eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Identity;
52eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Im;
53eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Nickname;
54eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Note;
55eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Organization;
56edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Phone;
57eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Relation;
58edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
59edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
60edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Website;
61752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract.Contacts;
6260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulosimport android.provider.ContactsContract.Data;
63eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.DisplayNameSources;
64ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.provider.ContactsContract.DataUsageFeedback;
65edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.QuickContact;
66edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.RawContacts;
6731b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
6870e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chenimport android.telecomm.TelecommManager;
69edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
70edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
71eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.util.Pair;
722a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu;
732a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu.ContextMenuInfo;
74d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
75d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
76eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
77f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chenimport android.view.MotionEvent;
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;
83edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
84d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
85edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
8672b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
87d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
882d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
89e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
90eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
912a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.common.ClipboardUtils;
92eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
93eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
94d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.editor.SelectAccountDialogFragment;
9570e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chenimport com.android.contacts.common.interactions.TouchPointManager;
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;
122d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwellimport com.android.contacts.common.util.ViewUtil;
123333091ae754ddfc25714c14b9b89534be24379f9Paul Soulosimport com.android.contacts.detail.ContactDisplayUtils;
124899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
125ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
127b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
128b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1292d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
1302a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryContextMenuInfo;
1312ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryTag;
132e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
133e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
134eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
135b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
136eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
137b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
138b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
13970e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen
140e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.base.Preconditions;
141e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
142e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
1432d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
144899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
145eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
146b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
147b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
148eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
149edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
150edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
151b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
152edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
153edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
154edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
155edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
156edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
157edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
158d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellpublic class QuickContactActivity extends ContactsActivity {
1598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
1618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
1628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
1638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
1648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
1658a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
1668a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
167edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
168edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1699b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
1709b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1719b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
1728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
173bfa9442453c1d86e11e56702f9b8ef0e8e0da0b1Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xC8, 0, 0, 0);
174ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
175dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
176752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
177752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
178752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
179edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
180edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
181edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
182edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
183e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
184e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
185e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE = "Add to circle";
186e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
187e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
188e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_HANGOUTS_VIDEO = "Start video call";
189e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
190edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
191edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
1928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
1938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
1948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
195edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1966219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee    private ImageView mPhotoView;
1970d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell    private View mTransparentView;
198eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
1996095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
200b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
201eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
202eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
203eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * This list contains all the {@link DataItem}s. Each nested list contains all data items of a
204eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * specific mimetype in sorted order, using mWithinMimeTypeDataItemComparator. The mimetype
205eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * lists are sorted using mAmongstMimeTypeDataItemComparator.
206eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
207eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private List<List<DataItem>> mDataItemsList;
208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
209eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * A map between a mimetype string and the corresponding list of data items. The data items
210eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * are in sorted order using mWithinMimeTypeDataItemComparator.
211eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
212eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Map<String, List<DataItem>> mDataItemsMap;
213b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
214d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
215eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private AsyncTask<Void, Void, Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>
216eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mEntriesAndActionsTask;
217f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    /**
218f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  This scrim's opacity is controlled in two different ways. 1) Before the initial entrance
219f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  animation finishes, the opacity is animated by a value animator. This is designed to
220f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  distract the user from the length of the initial loading time. 2) After the initial
221f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  entrance animation, the opacity is directly related to scroll position.
222f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     */
2239b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
224f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    private boolean mIsEntranceAnimationFinished;
225a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
2268477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
2278571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
228edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
2296cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
2309815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
2319b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
2329815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
2339758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
2349758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
235edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
236cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
237edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
238edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
239edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
240edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
241edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
242cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
243405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
244edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
245a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private static final List<String> SORTED_ABOUT_CARD_MIMETYPES = Lists.newArrayList(
246a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Nickname.CONTENT_ITEM_TYPE,
247a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name is inserted after nickname if it is available.
248a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // No mimetype for phonetic name exists.
249a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Website.CONTENT_ITEM_TYPE,
250a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Organization.CONTENT_ITEM_TYPE,
251a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Event.CONTENT_ITEM_TYPE,
252a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Relation.CONTENT_ITEM_TYPE,
253a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Im.CONTENT_ITEM_TYPE,
254a153dba33c2152332c5706178a367f52b0550723Paul Soulos            GroupMembership.CONTENT_ITEM_TYPE,
255a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Identity.CONTENT_ITEM_TYPE,
256a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Note.CONTENT_ITEM_TYPE);
257eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
258b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
259b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
260b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
261ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
262ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
263ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
264b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
265b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
266b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
267ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
268ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
269899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
270ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
271ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
272899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
273899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
274899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
275899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            180L * 24L * 60L * 60L * 1000L /* 180 days */;
276899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
277899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            36L * 60L * 60L * 1000L /* 36 hours */;
278899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
279ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
280ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
281ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
2826095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
2836095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
2846095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
285ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
286ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
287ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
288ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
289ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
290ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
291a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults = new HashMap<>();
292b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
293d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
2946cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
2952d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
2962d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
2972d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
2982ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Object entryTagObject = v.getTag();
2992ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (entryTagObject == null || !(entryTagObject instanceof EntryTag)) {
3002ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                Log.w(TAG, "EntryTag was not used correctly");
3016095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
3026095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
3032ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final EntryTag entryTag = (EntryTag) entryTagObject;
3042ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Intent intent = entryTag.getIntent();
3052ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final int dataId = entryTag.getId();
3062ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos
3072ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
3082ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                editContact();
3092d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
3102d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
311ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
312ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
313ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
314ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
315ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
31620bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            final Uri intentUri = intent.getData();
31720bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            if ((intentUri != null && intentUri.getScheme() != null &&
31820bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                    intentUri.getScheme().equals(CallUtil.SCHEME_SMSTO)) ||
319ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
320ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
321ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
322ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
323ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
324ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
32520bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                final Uri dataUsageUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
326ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
327ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
328ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
329ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                final boolean successful = getContentResolver().update(
33020bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                        dataUsageUri, new ContentValues(), null, null) > 0;
331ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                if (!successful) {
332ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    Log.w(TAG, "DataUsageFeedback increment failed");
333ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
334ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
335ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
336ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
337ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
33870e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            // Pass the touch point through the intent for use in the InCallUI
33970e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            if (Intent.ACTION_CALL.equals(intent.getAction())) {
3401f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                if (TouchPointManager.getInstance().hasValidPoint()) {
34170e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                    Bundle extras = new Bundle();
3421f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                    extras.putParcelable(TouchPointManager.TOUCH_POINT,
3431f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                            TouchPointManager.getInstance().getPoint());
34470e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                    intent.putExtra(TelecommManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
34570e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                }
34670e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            }
34770e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen
348ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            startActivity(intent);
3492d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
3502d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
3512d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
352e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
353e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
354e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
355e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
356e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
357e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
3580cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
3590cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        @Override
3600cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        public void onExpand(int heightDelta) {
3610cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos            mScroller.prepareForExpandingScrollChild();
3620cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        }
363e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
364e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
3652a4207fb39330e840436215c896cde911489e111Paul Soulos    private final OnCreateContextMenuListener mEntryContextMenuListener =
3662a4207fb39330e840436215c896cde911489e111Paul Soulos            new OnCreateContextMenuListener() {
3672a4207fb39330e840436215c896cde911489e111Paul Soulos        @Override
3682a4207fb39330e840436215c896cde911489e111Paul Soulos        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3692a4207fb39330e840436215c896cde911489e111Paul Soulos            if (menuInfo == null) {
3702a4207fb39330e840436215c896cde911489e111Paul Soulos                return;
3712a4207fb39330e840436215c896cde911489e111Paul Soulos            }
3722a4207fb39330e840436215c896cde911489e111Paul Soulos            EntryContextMenuInfo info = (EntryContextMenuInfo) menuInfo;
3732a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.setHeaderTitle(info.getCopyText());
3742a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.add(R.string.copy_text);
3752a4207fb39330e840436215c896cde911489e111Paul Soulos        }
3762a4207fb39330e840436215c896cde911489e111Paul Soulos    };
3772a4207fb39330e840436215c896cde911489e111Paul Soulos
3782a4207fb39330e840436215c896cde911489e111Paul Soulos    @Override
3792a4207fb39330e840436215c896cde911489e111Paul Soulos    public boolean onContextItemSelected(MenuItem item) {
3802a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo menuInfo;
3812a4207fb39330e840436215c896cde911489e111Paul Soulos        try {
3822a4207fb39330e840436215c896cde911489e111Paul Soulos            menuInfo = (EntryContextMenuInfo) item.getMenuInfo();
3832a4207fb39330e840436215c896cde911489e111Paul Soulos        } catch (ClassCastException e) {
3842a4207fb39330e840436215c896cde911489e111Paul Soulos            Log.e(TAG, "bad menuInfo", e);
3852a4207fb39330e840436215c896cde911489e111Paul Soulos            return false;
3862a4207fb39330e840436215c896cde911489e111Paul Soulos        }
3872a4207fb39330e840436215c896cde911489e111Paul Soulos
3882a4207fb39330e840436215c896cde911489e111Paul Soulos        ClipboardUtils.copyText(this, menuInfo.getCopyLabel(), menuInfo.getCopyText(), true);
3892a4207fb39330e840436215c896cde911489e111Paul Soulos        return true;
3902a4207fb39330e840436215c896cde911489e111Paul Soulos    }
3912a4207fb39330e840436215c896cde911489e111Paul Soulos
392d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
393d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
394d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
395d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
396d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
397d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
398d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
399d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
400d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
401d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
402d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
403d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
404d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
405d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
406d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
407d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
408d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
409d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
410d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
411d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
412d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
413d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
414d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
415d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
416d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
417d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
418d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
419d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
420d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
421d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
422b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
423b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
424b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
425b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
426f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            finish();
427b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
4288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
4298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
4308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
4318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
4328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
4338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
4348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
4358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
4368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
4378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
4388477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
4398477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
4408477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
4418477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
4428477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
4438477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
4448477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
445f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onEntranceAnimationDone() {
446f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            mIsEntranceAnimationFinished = true;
447f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        }
448f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
449f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        @Override
450f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onTransparentViewHeightChange(float ratio) {
451f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            if (mIsEntranceAnimationFinished) {
452f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                mWindowScrim.setAlpha((int) (0xFF * ratio));
4538477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
4548477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
455b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
456b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
457eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
458eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
459eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
460eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
461eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
462eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
463eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
464eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
465eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
466eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
467eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
468eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
469eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
470eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
471eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
472eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
473eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
474eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
475eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
476eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
477eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
478eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
479eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
480eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
481eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
482eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
483eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
484eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
485eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
486eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
487eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
488eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
489eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
490eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
491eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
492eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
493cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
494cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
495cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 1. Times used
496cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 2. Last time used
497cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 3. Statically defined
498cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
499eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
500eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
501eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
502eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
503eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem lhs = lhsList.get(0);
504eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem rhs = rhsList.get(0);
505eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
506eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
507eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
508eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
509eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
510eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
511eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
512eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
513eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
514eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
515eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
516eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
517eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
518eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
519eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
520eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
521eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
522eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
523eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Times used and last time used are the same. Resort to statically defined.
524eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String lhsMimeType = lhs.getMimeType();
525eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String rhsMimeType = rhs.getMimeType();
526eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (String mimeType : LEADING_MIMETYPES) {
527eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (lhsMimeType.equals(mimeType)) {
528eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return -1;
529eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                } else if (rhsMimeType.equals(mimeType)) {
530eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return 1;
531eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
532eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
533eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
534eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
535eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
536eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
537edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
538f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    public boolean dispatchTouchEvent(MotionEvent ev) {
539f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
540f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen            TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
541f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        }
542f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        return super.dispatchTouchEvent(ev);
543f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    }
544f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen
545f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    @Override
5468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
5478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
5488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
549edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getWindow().setStatusBarColor(Color.TRANSPARENT);
5512426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
552d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
553dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
55410d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
55510d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
55610d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
55710d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
558edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
559edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
560a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
561a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
5620cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
5630cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
564eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
5656095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
566b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
567eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
568edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5696095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
570eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
571eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
57223889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
5732a4207fb39330e840436215c896cde911489e111Paul Soulos        mContactCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
574b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
575b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
576b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
577edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
578eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
5792a4207fb39330e840436215c896cde911489e111Paul Soulos        mAboutCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
580eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
581d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mPhotoView = (ImageView) findViewById(R.id.photo);
5820d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        mTransparentView = findViewById(R.id.transparent_view);
5830d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
5840d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            mTransparentView.setOnClickListener(new OnClickListener() {
5850d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
5860d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
5870d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
5880d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
5890d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
5900d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
591edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
592d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        // Allow a shadow to be shown under the toolbar.
593d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
594d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell
595d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
596d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
5979b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
5989b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
5999b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
6009b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
6016219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
6028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
603f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = mHasAlreadyBeenOpened;
6049b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
605f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mWindowScrim.setAlpha(0);
6069b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
607edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
608c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
609ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
610ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
611ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
6128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
6139b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
6149b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
615d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
616d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
617d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
618d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
619d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
620d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
621d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
622d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
623d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
62423889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
625f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ true,
626f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                new Runnable() {
627f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    @Override
628f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    public void run() {
629f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        if (!mHasAlreadyBeenOpened) {
630f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // The initial scrim opacity must match the scrim opacity that would be
631f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // achieved by scrolling to the starting position.
632f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final float alphaRatio = mExtraMode == MODE_FULLY_EXPANDED ?
633f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    1 : mScroller.getStartingTransparentHeightRatio();
634f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int duration = getResources().getInteger(
635f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    android.R.integer.config_shortAnimTime);
636f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int desiredAlpha = (int) (0xFF * alphaRatio);
637f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            ObjectAnimator o = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0,
638f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    desiredAlpha).setDuration(duration);
639f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
640f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            o.start();
641f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        }
642f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    }
643f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                });
644f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
6459b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
6469b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
647ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
648ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
649ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
650ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
651ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
652ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
653ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
654ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
655ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
656ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
657ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
658ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
659ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
660ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
661a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
662a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
6639b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
664ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
665ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
6669b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
6679b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
6688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
6698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
6708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
671405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
672405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
6738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
6748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED) {
6758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // The contact that we were showing has been deleted.
6768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
677b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
6788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
679dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
6808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
6818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
6828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
6838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
684f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = true;
6858571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
686d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
687d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
688d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
6899b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
6909b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
6919b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
6929b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
6939b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilter.getColor());
6949b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
6959b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
6969b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
697d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
698d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
699d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
700d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
701d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
702d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
703d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
704d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
705d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
706d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE,
707d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                QuickContact.MODE_LARGE);
708d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
709d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
710d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mLookupUri = Preconditions.checkNotNull(lookupUri, "missing lookupUri");
711d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
712d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
713d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
714d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
715d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
716d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
717d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // we need to restart the loader and reload the new contact.
718899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            for (int interactionLoaderId : mRecentLoaderIds) {
719899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                getLoaderManager().destroyLoader(interactionLoaderId);
720899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            }
721405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
722405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
723d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
7242d150da246632b1649999cfabed776133b097775Brian Attwell
7252d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
7262426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
727edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
728b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
7298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
7308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
7318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
7328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
733c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
734b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
735b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
73681281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
737d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
7389b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
739f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString());
7409b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
74181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
74281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
74381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
744f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell    private void setHeaderNameText(String value) {
745d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
7469b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
747f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell                mScroller.setTitle(value);
7489b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
74981281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
75081281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
75181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
75281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
753edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
754edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
755edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
756edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
757edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
758edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
759edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
760edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
761edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
762edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
763edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
764edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
765edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
766edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
767cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
768edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
7698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
7708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
7716cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
772d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
773edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
7758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
776dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
7776219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
77831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
7794936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        analyzeWhitenessOfPhotoAsynchronously();
780333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos        setHeaderNameText(ContactDisplayUtils.getDisplayName(this, data).toString());
781edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
783dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
784eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mEntriesAndActionsTask = new AsyncTask<Void, Void,
785eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>() {
7868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
788eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            protected Pair<List<List<DataItem>>, Map<String, List<DataItem>>> doInBackground(
789eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
790eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
7918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
7928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
794eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            protected void onPostExecute(Pair<List<List<DataItem>>,
795eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Map<String, List<DataItem>>> dataItemsPair) {
796eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                super.onPostExecute(dataItemsPair);
797eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mDataItemsList = dataItemsPair.first;
798eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mDataItemsMap = dataItemsPair.second;
7998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
8008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
8018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
8028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
803eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    bindDataToCards();
8048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
8058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
8068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
8078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
8088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
8098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
811eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void bindDataToCards() {
812eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        startInteractionLoaders();
813eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        populateContactAndAboutCard();
814eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
815eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
816eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void startInteractionLoaders() {
817eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<DataItem> phoneDataItems = mDataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
818eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
819eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
820eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
821eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
822eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
823eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
824eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
825ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
826eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
827eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
828eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
8298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
8308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
831ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
832ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
833ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
834ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
835ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
836ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
837ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
838ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
8398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
840899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
8418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
842eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
843899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
844eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<DataItem> emailDataItems = mDataItemsMap.get(Email.CONTENT_ITEM_TYPE);
845eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
846eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
847eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
848eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
849eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
850eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
851eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
852ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
853eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
854899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
855899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
856ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
857899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
8588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
8598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
8618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
8628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
8638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
8648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
8658a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
8668a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
8678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
8688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
8698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
8708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
8718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
8728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
874a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private List<List<Entry>> buildAboutCardEntries() {
875a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
876a153dba33c2152332c5706178a367f52b0550723Paul Soulos        for (String mimetype : SORTED_ABOUT_CARD_MIMETYPES) {
877a153dba33c2152332c5706178a367f52b0550723Paul Soulos            final List<DataItem> mimeTypeItems = mDataItemsMap.get(mimetype);
878a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (mimeTypeItems == null) {
879a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
880a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
881a153dba33c2152332c5706178a367f52b0550723Paul Soulos            final List<Entry> aboutEntries = dataItemsToEntries(mimeTypeItems);
882a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutEntries.size() > 0) {
883a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(aboutEntries);
884a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
885a153dba33c2152332c5706178a367f52b0550723Paul Soulos        }
886a153dba33c2152332c5706178a367f52b0550723Paul Soulos        return aboutCardEntries;
887a153dba33c2152332c5706178a367f52b0550723Paul Soulos    }
888a153dba33c2152332c5706178a367f52b0550723Paul Soulos
889eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void populateContactAndAboutCard() {
890eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
891eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
89260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        final List<List<Entry>> contactCardEntries = new ArrayList<>();
893a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = buildAboutCardEntries();
894eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
895eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (int i = 0; i < mDataItemsList.size(); ++i) {
896eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final List<DataItem> dataItemsByMimeType = mDataItemsList.get(i);
897eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final DataItem topDataItem = dataItemsByMimeType.get(0);
898a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
899a153dba33c2152332c5706178a367f52b0550723Paul Soulos                // About card mimetypes are built in buildAboutCardEntries, skip here
900a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
90160e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            } else {
90260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                List<Entry> contactEntries = dataItemsToEntries(mDataItemsList.get(i));
90360e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                if (contactEntries.size() > 0) {
90460e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                    contactCardEntries.add(contactEntries);
905eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
906eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
907eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
908eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
909eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
910eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
911eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
912c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mContactCard.isExpanded(),
91389966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                    /* isAlwaysExpanded = */ false,
9140cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener,
9150cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mScroller);
916eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
917eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
918eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
919eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
920eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
9218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
922eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
923c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        // Phonetic name is not a data item, so the entry needs to be created separately
924c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        final String phoneticName = mContactData.getPhoneticName();
925c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        if (!TextUtils.isEmpty(phoneticName)) {
926c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            Entry phoneticEntry = new Entry(/* viewId = */ -1,
927c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* icon = */ null,
928c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    getResources().getString(R.string.name_phonetic),
929c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    phoneticName,
930c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* text = */ null,
931c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* intent = */ null,
932c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIcon = */ null,
933c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIntent = */ null,
934c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateContentDescription = */ null,
935c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* shouldApplyColor = */ false,
9362a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
9372a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ new EntryContextMenuInfo(phoneticName,
9382a4207fb39330e840436215c896cde911489e111Paul Soulos                            getResources().getString(R.string.name_phonetic)));
939c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            List<Entry> phoneticList = new ArrayList<>();
940c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            phoneticList.add(phoneticEntry);
941a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name comes after nickname. Check to see if the first entry type is nickname
942a0fa4c18dc111ceea9ff47f35ee01fecc1003578Paul Soulos            if (aboutCardEntries.size() > 0 && aboutCardEntries.get(0).get(0).getHeader().equals(
943a153dba33c2152332c5706178a367f52b0550723Paul Soulos                    getResources().getString(R.string.header_nickname_entry))) {
944a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(1, phoneticList);
945a153dba33c2152332c5706178a367f52b0550723Paul Soulos            } else {
946a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(0, phoneticList);
947a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
948c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        }
949c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos
950eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.initialize(aboutCardEntries,
951eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                /* numInitialVisibleEntries = */ 1,
952eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                /* isExpanded = */ true,
95389966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                /* isAlwaysExpanded = */ true,
9540cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                mExpandingEntryCardViewListener,
9550cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                mScroller);
9566095369885edcca566a812b551886e29c7ff8039Brian Attwell
9576095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
9586095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
9596095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
9606095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
9616095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
9626095369885edcca566a812b551886e29c7ff8039Brian Attwell
963a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
964a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
965a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
966a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
967a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
968eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
969eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
970eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
971eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
9726095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
9736095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
9746095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
9756095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
9766095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
9776095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
9786095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
979dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* subHeader = */ null, /* text = */ null, getEditContactIntent(),
980dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
981714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
9822a4207fb39330e840436215c896cde911489e111Paul Soulos                /* isEditable = */ false, /* EntryContextMenuInfo = */ null);
9836095369885edcca566a812b551886e29c7ff8039Brian Attwell
9846095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
9856095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
9866095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
9876095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
988dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* text = */ null, getEditContactIntent(), /* alternateIcon = */ null,
989dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
9902a4207fb39330e840436215c896cde911489e111Paul Soulos                /* shouldApplyColor = */ true, /* isEditable = */ false,
9912a4207fb39330e840436215c896cde911489e111Paul Soulos                /* EntryContextMenuInfo = */ null);
9926095369885edcca566a812b551886e29c7ff8039Brian Attwell
9936095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
9946095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
9956095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
9966095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
9976095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
9986095369885edcca566a812b551886e29c7ff8039Brian Attwell
9996095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
10006095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
10016095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
10026095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
1003c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
10040cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener, mScroller);
10056095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
10066095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
10076095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
10086095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
10096095369885edcca566a812b551886e29c7ff8039Brian Attwell
10106095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
1011eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
1012eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
1013eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
1014eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
1015eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
1016eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
1017eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Pair<List<List<DataItem>>, Map<String, List<DataItem>>> generateDataModelFromContact(
1018eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
1019eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
1020eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1021eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
10228bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
10238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
1024851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
1025851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
1026eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
1027eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1028851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
1029eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
1030eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
103147b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
103247b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
103347b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
1034eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
1035cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1036eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
1037b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1038eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
1039eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
1040899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
1041eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
1042edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1043eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
1044eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
1045eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
1046eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
1047edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
1048eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
1049edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1050edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
10518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1052edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1053eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
105416339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
105516339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
1056eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
1057eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
1058eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
105916339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
1060eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
1061eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
1062eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
1063eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
1064eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
1065eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
1066eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
1067eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
1068edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1069eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1070edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1071eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
1072eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
1073eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
1074eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
107516339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
1076eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return new Pair<>(dataItemsList, dataItemsMap);
1077eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1078edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1079eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
1080eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
1081eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
1082eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
1083eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
1084eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
1085eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Entry dataItemToEntry(DataItem dataItem) {
1086eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
1087eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
1088eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
1089eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
1090eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
1091eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
1092eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
109348ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
1094dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
1095dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
1096dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        String alternateContentDescription = null;
1097eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
10982a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo entryContextMenuInfo = null;
1099eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1100eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
1101eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1102eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
1103eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
1104eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = ContactsUtils.buildImIntent(this, im).first;
1105eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
11067de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            final int protocol;
11077de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (!im.isProtocolValid()) {
11087de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = Im.PROTOCOL_CUSTOM;
11097de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
11107de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
11117de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
11127de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (protocol == Im.PROTOCOL_CUSTOM) {
11137de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // If the protocol is custom, display the "IM" entry header as well to distinguish
11147de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // this entry from other ones
11157de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                header = getResources().getString(R.string.header_im_entry);
11167de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = Im.getProtocolLabel(getResources(), protocol,
11177de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
11187de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                text = im.getData();
11197de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
11207de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                header = Im.getProtocolLabel(getResources(), protocol,
11217de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
11227de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = im.getData();
11237de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
11242a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(im.getData(), header);
1125eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
1126eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
1127eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_organization_entry);
1128eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
11292a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1130eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
1131eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
1132eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
1133eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
1134eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
1135eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                (mContactData.getNameRawContactId() == dataItem.getRawContactId());
1136eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1137eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
1138eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
1139eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                && mContactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
1140eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1141eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
1142eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = getResources().getString(R.string.header_nickname_entry);
1143eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
11442a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1145eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1146eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
1147eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
1148eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_note_entry);
1149eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
11502a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1151eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
1152eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
1153eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_website_entry);
1154eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
11552a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1156eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
1157eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final WebAddress webAddress = new WebAddress(website.buildDataString(this, kind));
1158eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1159eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
1160eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.e(TAG, "Couldn't parse website: " + website.buildDataString(this, kind));
1161eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1162eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1163eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
1164eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String dataString = event.buildDataString(this, kind);
1165eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1166eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1167eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1168eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1169eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1170eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1171eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1172eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1173eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1174eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_event_entry);
1175f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
11767de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = Event.getTypeLabel(getResources(), event.getKindTypeColumn(kind),
11777de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        event.getLabel()).toString();
1178f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1179eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = DateUtils.formatDate(this, dataString);
1180f848c49cac796f99549c8418f8d4e2469ac9f963Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(text, header);
1181eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1182eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
1183eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String dataString = relation.buildDataString(this, kind);
1184eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1185eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1186eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1187eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1188eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1189eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_relation_entry);
1190eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
11912a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1192f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
1193f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                text = Relation.getTypeLabel(getResources(), relation.getKindTypeColumn(kind),
1194f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1195f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1196eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1197eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
1198eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
1199eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = phone.buildDataString(this, kind);
12002a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
12012a4207fb39330e840436215c896cde911489e111Paul Soulos                        getResources().getString(R.string.phoneLabelsGroup));
1202f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
1203f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = Phone.getTypeLabel(getResources(), phone.getKindTypeColumn(kind),
1204f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            phone.getLabel()).toString();
1205f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1206eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_phone_24dp);
1207eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (PhoneCapabilityTester.isPhone(this)) {
1208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
120916339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1210dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
1211dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                        Uri.fromParts(CallUtil.SCHEME_SMSTO, phone.getNumber(), null));
1212dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon = getResources().getDrawable(R.drawable.ic_message_24dp);
1213dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateContentDescription = getResources().getString(R.string.sms_other);
1214eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1215eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1216eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1217eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1218eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
1219eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri mailUri = Uri.fromParts(CallUtil.SCHEME_MAILTO, address, null);
1220eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1221eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
12222a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
12232a4207fb39330e840436215c896cde911489e111Paul Soulos                        getResources().getString(R.string.emailLabelsGroup));
1224f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
1225f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = Email.getTypeLabel(getResources(), email.getKindTypeColumn(kind),
1226f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
1227f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1228eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_email_24dp);
1229eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1230eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1231eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1232eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1233eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
1234eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1235eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
12362a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
12372a4207fb39330e840436215c896cde911489e111Paul Soulos                        getResources().getString(R.string.postalLabelsGroup));
1238f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
1239f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = StructuredPostal.getTypeLabel(getResources(),
1240f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
1241f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
12426a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIntent =
12436a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                        StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
12446a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIcon = getResources().getDrawable(R.drawable.ic_directions_24dp);
1245eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_place_24dp);
1246eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1247eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
1248eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (PhoneCapabilityTester.isSipPhone(this)) {
1249eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1250eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final String address = sip.getSipAddress();
1251eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (!TextUtils.isEmpty(address)) {
1252eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    final Uri callUri = Uri.fromParts(CallUtil.SCHEME_SIP, address, null);
1253eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
12541cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    header = address;
12552a4207fb39330e840436215c896cde911489e111Paul Soulos                    entryContextMenuInfo = new EntryContextMenuInfo(header,
12562a4207fb39330e840436215c896cde911489e111Paul Soulos                            getResources().getString(R.string.phoneLabelsGroup));
12571cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    if (sip.hasKindTypeColumn(kind)) {
12581cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                        text = SipAddress.getTypeLabel(getResources(), sip.getKindTypeColumn(kind),
12591cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                            sip.getLabel()).toString();
12601cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    }
1261307b49420ce66b0aee5563bf2624dba7e20e28e2Paul Soulos                    icon = getResources().getDrawable(R.drawable.ic_dialer_sip_black_24dp);
126216339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1263edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1264eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
1265eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
1266eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(givenName)) {
1267eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mAboutCard.setTitle(getResources().getString(R.string.about_card_title) +
1268eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        " " + givenName);
1269eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
1270eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mAboutCard.setTitle(getResources().getString(R.string.about_card_title));
1271eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1272eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1273eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
1274eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = dataItem.buildDataStringForDisplay(this, kind);
1275eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1276eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
127760e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
127860e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
1279e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1280e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
1281e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
1282e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1283e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                // Attempt to use known icons for known 3p types. Otherwise default to ResolveCache
1284e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
1285e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
1286e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE.equals(
1287e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                intent.getDataString())) {
1288e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(
1289e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                    R.drawable.ic_add_to_circles_black_24);
1290e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
1291e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_google_plus_24dp);
1292e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1293e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1294e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
1295e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_HANGOUTS_VIDEO.equals(intent.getDataString())) {
1296e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_hangout_video_24dp);
1297e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
1298e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_hangout_24dp);
1299e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1300e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1301e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
13022a4207fb39330e840436215c896cde911489e111Paul Soulos                        entryContextMenuInfo = new EntryContextMenuInfo(header, mimetype);
1303e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        icon = ResolveCache.getInstance(this).getIcon(
1304e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
1305e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
1306e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
1307e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
1308e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
130948ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
1310e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
1311e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
1312eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1313b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1314eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
1315eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
1316eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!PhoneCapabilityTester.isIntentRegistered(this, intent)) {
1317eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
1318eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1319eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1320eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1321dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
1322dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
1323dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (!PhoneCapabilityTester.isIntentRegistered(this, alternateIntent)) {
1324dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
1325dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1326dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1327dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Attempt to use package manager to find a suitable content description if needed
1328dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (TextUtils.isEmpty(alternateContentDescription)) {
1329dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateContentDescription = getIntentResolveLabel(alternateIntent);
1330dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1331dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1332dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1333eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
1334eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
1335eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
1336eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
1337eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1338eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1339fa238949855fba3b28a57fad3d585b13e80362bbBrian Attwell        // Ignore dataIds from the Me profile.
1340ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
1341ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
1342ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
1343dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon, intent,
1344dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon, alternateIntent, alternateContentDescription, shouldApplyColor,
13452a4207fb39330e840436215c896cde911489e111Paul Soulos                isEditable, entryContextMenuInfo);
1346eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1347eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1348eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private List<Entry> dataItemsToEntries(List<DataItem> dataItems) {
1349eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1350eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
1351eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Entry entry = dataItemToEntry(dataItem);
1352eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (entry != null) {
1353eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                entries.add(entry);
1354eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1355eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1356eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
1357edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1358edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1359dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    private String getIntentResolveLabel(Intent intent) {
1360dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent,
1361dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
1362dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1363dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
1364dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
1365dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
1366dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
1367dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
1368dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
1369dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = ResolveCache.getInstance(this).getBestResolve(intent, matches);
1370dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1371dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1372dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
1373dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
1374dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1375dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1376dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return String.valueOf(bestResolve.loadLabel(getPackageManager()));
1377dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
1378dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1379edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
138031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
138131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
138231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
138331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
138431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
138531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
138631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
138731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
138831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1389faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
139031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1391faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
1392faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
139331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
13948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
1395faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = colorFromBitmap(bitmap);
1396faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    if (primaryColor != 0) {
1397a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                        return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
1398faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                                primaryColor);
1399faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
14008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
14018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
1402faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
1403a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
140431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
1405a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
140631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
140731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
140831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1409faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
1410faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
14118571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
14128571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
14138571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
14148571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
14158571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
14168571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
14178571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
14188571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
14198571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
14208571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
14218571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
14228571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
1423faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
142431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
142531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
142631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
142731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
142831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
14294936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    /**
14304936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * Examine how many white pixels are in the bitmap in order to determine whether or not
14314936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * we need gradient overlays on top of the image.
14324936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     */
14334936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    private void analyzeWhitenessOfPhotoAsynchronously() {
14344936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
14354936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        new AsyncTask<Void, Void, Boolean>() {
14364936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
14374936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected Boolean doInBackground(Void... params) {
14384936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
14394936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
14404936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    return WhitenessUtils.isBitmapWhiteAtTopOrBottom(bitmap);
14414936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                }
14424936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                return !(imageViewDrawable instanceof LetterTileDrawable);
14434936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
14444936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
14454936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
14464936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected void onPostExecute(Boolean isWhite) {
14474936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                super.onPostExecute(isWhite);
14484936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                mScroller.setUseGradient(isWhite);
14494936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
14504936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        }.execute();
14514936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    }
14524936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
1453faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
14549b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
1455faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        final int primaryColor = palette.mPrimaryColor;
1456faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mScroller.setHeaderTintColor(primaryColor);
1457faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
14589b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
14598571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
14609b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
1461faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                new PorterDuffColorFilter(primaryColor, PorterDuff.Mode.SRC_ATOP);
1462faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mContactCard.setColorAndFilter(primaryColor, mColorFilter);
1463faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mRecentCard.setColorAndFilter(primaryColor, mColorFilter);
1464faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mAboutCard.setColorAndFilter(primaryColor, mColorFilter);
14659b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
14669b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
14678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
14688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller == null) {
14698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
14708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
14718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
14728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
14738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
14748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
14758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
14768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
14778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
14788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
1479847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
1480847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell                getWindow().getStatusBarColor(), desiredStatusBarColor);
1481847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
1482847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setEvaluator(new ArgbEvaluator());
1483847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.start();
14848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
14858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
14868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
14878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
14888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
14898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
1490a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
1491a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
14928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
14938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
14948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
14958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1496b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
1497eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1498b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
1499ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
1500ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
1501b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
1502b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
1503b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
1504b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
1505b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
1506b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
1507dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
1508dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
1509dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
151048ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
15112a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
15122a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ null));
1513b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1514b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
1515b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1516b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1517eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
1518851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
1519cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1520851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
1521405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
1522cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1523cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1524cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1525851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
15268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
15278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1528cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (isFinishing()) {
1529cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1530cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1531cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isError()) {
1532cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // This shouldn't ever happen, so throw an exception. The {@link ContactLoader}
1533cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // should log the actual exception.
1534cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                throw new IllegalStateException("Failed to load contact", data.getException());
1535cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1536cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isNotFound()) {
15378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (mHasAlreadyBeenOpened) {
15388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    finish();
15398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                } else {
15408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
15418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
15428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
15438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
1544cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1545cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1546cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1547b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            bindContactData(data);
1548cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
15498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.endSection();
1550cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1551cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1552cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1553851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
1554cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
1555cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
1556cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1557d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
1558d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
1559b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
1560d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
15618571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
1562cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1563cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
1564b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1565b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
1566b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
1567b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
15688477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
15698477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
15708477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
1571b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
1572b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
1573b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
1574b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
1575b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
15768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
15778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
15788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
15798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
15808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
15818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
15828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
15838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1584eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
1585b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
1586b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1587b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1588b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
1589b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Log.v(TAG, "onCreateLoader");
1590b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
1591b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
1592b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
1593b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    Log.v(TAG, "LOADER_SMS_ID");
1594b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
1595b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
1596ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1597b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
1598b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
1599899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
1600899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    Log.v(TAG, "LOADER_CALENDAR_ID");
1601ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
1602ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
1603ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
1604ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
1605ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
1606899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
1607899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
1608ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
1609899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
1610899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
1611899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
1612899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
1613899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
1614ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
1615ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    Log.v(TAG, "LOADER_CALL_LOG_ID");
1616ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
1617ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
1618ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1619ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
1620b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1621b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
1622b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1623b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1624b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1625b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
1626b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
1627b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
1628b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1629b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
1630b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
1631b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1632b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1633b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1634b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1635b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
1636b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
1637b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1638b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
1639b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1640b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
1641b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
1642b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1643b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1644b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
1645eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
1646b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
1647b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            allInteractions.addAll(loaderInteractions);
1648b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1649b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1650b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        // Sort the interactions by most recent
1651b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
1652b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
1653b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            public int compare(ContactInteraction a, ContactInteraction b) {
1654b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                return a.getInteractionDate() >= b.getInteractionDate() ? -1 : 1;
1655b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1656b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        });
1657b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
165897ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        // Wrap each interaction in its own list so that an icon is displayed for each entry
165960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        List<List<Entry>> interactionsWrapper = new ArrayList<>();
166097ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        for (Entry contactInteraction : contactInteractionsToEntries(allInteractions)) {
166197ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            List<Entry> entryListWrapper = new ArrayList<>(1);
166297ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            entryListWrapper.add(contactInteraction);
166397ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            interactionsWrapper.add(entryListWrapper);
166497ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        }
1665b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        if (allInteractions.size() > 0) {
166660e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            mRecentCard.initialize(interactionsWrapper,
1667b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
1668c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
16690cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener, mScroller);
1670b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentCard.setVisibility(View.VISIBLE);
1671b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1672eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1673eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // About card is initialized along with the contact card, but since it appears after
1674eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // the recent card in the UI, we hold off until making it visible until the recent card
1675eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // is also ready to avoid stuttering.
1676eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (mAboutCard.shouldShow()) {
1677eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1678eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1679eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.GONE);
1680eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1681b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
16828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
16838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
16848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
16858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
16868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
16878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
16888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
16898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
16908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
16918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
16928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
16938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
16948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
169523889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
169623889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
1697d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
1698d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
1699d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
1700d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1701d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1702d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1703a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    /**
1704a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     * Returns true if it is possible to share the current contact.
1705a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     */
1706a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    private boolean isContactShareable() {
1707a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1708a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    }
1709a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
17106095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
1711d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Intent intent = new Intent(Intent.ACTION_EDIT, mLookupUri);
1712d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mContactLoader.cacheResult();
1713d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
17146095369885edcca566a812b551886e29c7ff8039Brian Attwell        return intent;
17156095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
17166095369885edcca566a812b551886e29c7ff8039Brian Attwell
17176095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
17186095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
1719d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1720d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1721d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
1722d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
1723d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mLookupUri != null) {
1724d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
1725d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
1726d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
1727d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
1728d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1729d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
1730333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
1731d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1732d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
1733d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1734d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
1735eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
1736d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    QuickContactActivity.this, mLookupUri, !isStarred);
1737d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
173835ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell
173935ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            final CharSequence accessibilityText = !isStarred
174035ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    ? getResources().getText(R.string.description_action_menu_add_star)
174135ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    : getResources().getText(R.string.description_action_menu_remove_star);
174235ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // Accessibility actions need to have an associated view. We can't access the MenuItem's
174335ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // underlying view, so put this accessibility action on the root view.
174435ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            mScroller.announceForAccessibility(accessibilityText);
1745d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
1746d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1747d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1748752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1749752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Calls into the contacts provider to get a pre-authorized version of the given URI.
1750752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1751752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private Uri getPreAuthorizedUri(Uri uri) {
1752752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle uriBundle = new Bundle();
1753752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        uriBundle.putParcelable(ContactsContract.Authorization.KEY_URI_TO_AUTHORIZE, uri);
1754752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle authResponse = getContentResolver().call(
1755752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.AUTHORITY_URI,
1756752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.Authorization.AUTHORIZATION_METHOD,
1757752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                null,
1758752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                uriBundle);
1759752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (authResponse != null) {
1760752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return (Uri) authResponse.getParcelable(
1761752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    ContactsContract.Authorization.KEY_AUTHORIZED_URI);
1762752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        } else {
1763752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return uri;
1764752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1765752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
17662d150da246632b1649999cfabed776133b097775Brian Attwell
1767752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
1768752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
1769752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
1770752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (mContactData.isUserProfile()) {
1771752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // User is sharing the profile.  We don't want to force the receiver to have
1772752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // the highly-privileged READ_PROFILE permission, so we need to request a
1773752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // pre-authorized URI from the provider.
1774752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            shareUri = getPreAuthorizedUri(shareUri);
1775752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1776752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1777752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
1778752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.setType(Contacts.CONTENT_VCARD_TYPE);
1779752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
1780752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1781752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
1782752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
1783752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
1784752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1785752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
1786752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            this.startActivity(chooseIntent);
1787eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
1788752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
1789752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1790752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1791752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1792752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1793752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
1794752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1795752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
1796752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
1797752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
1798752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1799752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
1800752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
1801752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
1802752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
1803752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
1804752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
1805752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1806752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
1807752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
1808752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        Toast.makeText(QuickContactActivity.this,
1809752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                R.string.createContactShortcutSuccessful,
1810752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
1811752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
1812752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1813752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
1814752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        builder.createContactShortcutIntent(mLookupUri);
1815752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1816752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1817d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1818d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
1819eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
1820d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
1821d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
1822d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1823d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1824d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1825d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
1826d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
1827d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
1828333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
1829d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1830d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
1831a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
1832d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
1833d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
1834d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
1835d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
1836d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
183730cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
18382e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
1839d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
1840d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
18412e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
1842d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
1843d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
1844d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
1845a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
1846a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            final MenuItem shareMenuItem = menu.findItem(R.id.menu_share);
1847a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            shareMenuItem.setVisible(isContactShareable());
1848a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
18498a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
1850d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
18518a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
1852d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1853d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1854d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1855d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
1856d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
1857d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
1858d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
1859d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1860d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
1861d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
1862d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    DirectoryContactUtil.addToMyContacts(mContactData, this, getFragmentManager(),
1863d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                            mSelectAccountFragmentListener);
1864d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
1865d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
1866d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
1867d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
1868d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
1869d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1870752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
1871752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                shareContact();
1872752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
1873752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
1874752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                createLauncherShortcutWithContact();
1875752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
1876d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
1877d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
1878d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
187923889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
1880edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
1881