QuickContactActivity.java revision c00a0b5370f0714f6af1ea9b3e29b10f25e91af0
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
19ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.accounts.Account;
208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.animation.ArgbEvaluator;
21b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport android.animation.ObjectAnimator;
22edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.app.Activity;
23d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.app.Fragment;
24cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.app.LoaderManager.LoaderCallbacks;
25eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.app.SearchManager;
26752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.content.ActivityNotFoundException;
27edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.ContentUris;
28ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.content.ContentValues;
296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwellimport android.content.Context;
30edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.Intent;
31cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.content.Loader;
32dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.PackageManager;
33dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.ResolveInfo;
3431b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.Bitmap;
3595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwellimport android.graphics.BitmapFactory;
368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.Color;
37eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuff;
38eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuffColorFilter;
3931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.BitmapDrawable;
408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.drawable.ColorDrawable;
4131b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.Drawable;
42eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.ParseException;
43edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.net.Uri;
44eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.WebAddress;
4531b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.os.AsyncTask;
46edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.os.Bundle;
478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.os.Trace;
48eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.CalendarContract;
49752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract;
50edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Email;
51eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Event;
52eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;
53eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Identity;
54eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Im;
55eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Nickname;
56eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Note;
57eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Organization;
58edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Phone;
59eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Relation;
60edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
61edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
62edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Website;
63752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract.Contacts;
6460e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulosimport android.provider.ContactsContract.Data;
65ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.provider.ContactsContract.Directory;
66eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.DisplayNameSources;
67ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.provider.ContactsContract.DataUsageFeedback;
68ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.provider.ContactsContract.Intents;
69edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.QuickContact;
70edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.RawContacts;
7131b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
7270e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chenimport android.telecomm.TelecommManager;
73edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
74edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
752a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu;
762a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu.ContextMenuInfo;
77d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
78d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
79eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
80f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chenimport android.view.MotionEvent;
81edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View;
82edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View.OnClickListener;
832a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.View.OnCreateContextMenuListener;
8410d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawaimport android.view.WindowManager;
85edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.ImageView;
86edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
87d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
88edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
8972b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
90d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
912d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
92e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
93eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
942a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.common.ClipboardUtils;
95eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
96eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
97d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.editor.SelectAccountDialogFragment;
9870e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chenimport com.android.contacts.common.interactions.TouchPointManager;
9931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport com.android.contacts.common.lettertiles.LetterTileDrawable;
100752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder;
101752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
1020d5588da244d0992c3ff8f25d0875fdf95a8c644Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
103cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.Contact;
104cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.ContactLoader;
105cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.RawContact;
106428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.AccountType;
107d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.model.account.AccountWithDataSet;
108cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.DataItem;
109428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.dataitem.DataKind;
110cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.EmailDataItem;
111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.EventDataItem;
112cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.ImDataItem;
113eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NicknameDataItem;
114eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NoteDataItem;
115eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.OrganizationDataItem;
116b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.common.model.dataitem.PhoneDataItem;
117eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.RelationDataItem;
118eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.SipAddressDataItem;
119eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredNameDataItem;
120eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredPostalDataItem;
121eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.WebsiteDataItem;
122eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.util.DateUtils;
123faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils;
124faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
125d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwellimport com.android.contacts.common.util.ViewUtil;
126333091ae754ddfc25714c14b9b89534be24379f9Paul Soulosimport com.android.contacts.detail.ContactDisplayUtils;
127899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
128ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
130b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
131b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1322d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
1332a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryContextMenuInfo;
1342ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryTag;
135e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
136e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
137eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
138b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
139eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
140b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
141b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
14202eaf11669d25fd885606b3b0700358323532cddBrian Attwellimport com.android.contacts.widget.QuickContactImageView;
14302eaf11669d25fd885606b3b0700358323532cddBrian Attwell
144e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.base.Preconditions;
145e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
146e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
1472d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
148899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
149eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
150b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
151b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
152eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
153edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
154edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
155b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
156edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
157edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
158edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
159edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
160edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
161edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
162d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellpublic class QuickContactActivity extends ContactsActivity {
1638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
1658a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
1668a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
1678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
1688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
1698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
1708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
171edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
172edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1739b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
1749b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1759b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
1768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
177bfa9442453c1d86e11e56702f9b8ef0e8e0da0b1Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xC8, 0, 0, 0);
178ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos    private static final int REQUEST_CODE_CONTACT_SELECTION_ACTIVITY = 2;
179ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
180dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
181752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
182752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
183752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
184edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
185edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
186edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
187edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
188e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
189e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
190e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE = "Add to circle";
191e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
192e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
193e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_HANGOUTS_VIDEO = "Start video call";
19448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos    private static final String CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY =
19548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos            "com.android.contacts.quickcontact.QuickContactActivity";
196e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
19763176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell    /**
19863176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * The URI used to load the the Contact. Once the contact is loaded, use Contact#getLookupUri()
19963176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * instead of referencing this URI.
20063176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     */
201edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
202edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
2038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
2048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
2058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
206edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
20702eaf11669d25fd885606b3b0700358323532cddBrian Attwell    private QuickContactImageView mPhotoView;
208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
2096095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
210b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
211eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
212b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
213d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
2146bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private AsyncTask<Void, Void, Cp2DataCardModel> mEntriesAndActionsTask;
2156bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
2166bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * The last copy of Cp2DataCardModel that was passed to {@link #populateContactAndAboutCard}.
2176bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
2186bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel mCachedCp2DataCardModel;
219f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    /**
220f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  This scrim's opacity is controlled in two different ways. 1) Before the initial entrance
221f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  animation finishes, the opacity is animated by a value animator. This is designed to
222f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  distract the user from the length of the initial loading time. 2) After the initial
223f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  entrance animation, the opacity is directly related to scroll position.
224f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     */
2259b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
226f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    private boolean mIsEntranceAnimationFinished;
227a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
2288477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
2298571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
230edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
231a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    /**
232a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * Used to stop the ExpandingEntry cards from adjusting between an entry click and the intent
233a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * being launched.
234a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     */
235a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    private boolean mHasIntentLaunched;
236a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
2376cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
2389815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
2399b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
2409815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
2419758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
2429758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
243edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
244cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
245edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
246edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
247edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
248edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
249edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
250cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
251405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
252edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
253a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private static final List<String> SORTED_ABOUT_CARD_MIMETYPES = Lists.newArrayList(
254a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Nickname.CONTENT_ITEM_TYPE,
255a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name is inserted after nickname if it is available.
256a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // No mimetype for phonetic name exists.
257a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Website.CONTENT_ITEM_TYPE,
258a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Organization.CONTENT_ITEM_TYPE,
259a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Event.CONTENT_ITEM_TYPE,
260a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Relation.CONTENT_ITEM_TYPE,
261a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Im.CONTENT_ITEM_TYPE,
262a153dba33c2152332c5706178a367f52b0550723Paul Soulos            GroupMembership.CONTENT_ITEM_TYPE,
263a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Identity.CONTENT_ITEM_TYPE,
264a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Note.CONTENT_ITEM_TYPE);
265eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
266b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
267b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
268b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
269ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
270ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
271ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
272b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
273b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
274b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
275ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
276ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
277899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
278ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
279ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
280899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
281899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
282899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
283899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            180L * 24L * 60L * 60L * 1000L /* 180 days */;
284899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
285899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            36L * 60L * 60L * 1000L /* 36 hours */;
286899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
287ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
288ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
289ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
2906095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
2916095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
2926095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
293ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
294ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
295ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
296ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
297ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
298ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
299a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults = new HashMap<>();
300b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
301d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
3026cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
3032d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
3042d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
3052d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
3062ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Object entryTagObject = v.getTag();
3072ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (entryTagObject == null || !(entryTagObject instanceof EntryTag)) {
3082ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                Log.w(TAG, "EntryTag was not used correctly");
3096095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
3106095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
3112ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final EntryTag entryTag = (EntryTag) entryTagObject;
3122ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Intent intent = entryTag.getIntent();
3132ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final int dataId = entryTag.getId();
3142ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos
3152ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
3162ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                editContact();
3172d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
3182d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
319ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
320ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
321ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
322ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
323ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
32420bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            final Uri intentUri = intent.getData();
32520bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            if ((intentUri != null && intentUri.getScheme() != null &&
32620bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                    intentUri.getScheme().equals(CallUtil.SCHEME_SMSTO)) ||
327ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
328ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
329ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
330ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
331ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
332ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
33320bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                final Uri dataUsageUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
334ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
335ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
336ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
337ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                final boolean successful = getContentResolver().update(
33820bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                        dataUsageUri, new ContentValues(), null, null) > 0;
339ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                if (!successful) {
340ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    Log.w(TAG, "DataUsageFeedback increment failed");
341ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
342ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
343ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
344ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
345ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
34670e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            // Pass the touch point through the intent for use in the InCallUI
34770e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            if (Intent.ACTION_CALL.equals(intent.getAction())) {
3481f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                if (TouchPointManager.getInstance().hasValidPoint()) {
34970e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                    Bundle extras = new Bundle();
3501f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                    extras.putParcelable(TouchPointManager.TOUCH_POINT,
3511f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                            TouchPointManager.getInstance().getPoint());
35270e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                    intent.putExtra(TelecommManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
35370e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                }
35470e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            }
35570e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen
356a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
357ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            startActivity(intent);
3582d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
3592d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
3602d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
361e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
362e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
363e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
364e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
365e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
366e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
3670cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
3680cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        @Override
3690cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        public void onExpand(int heightDelta) {
3700cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos            mScroller.prepareForExpandingScrollChild();
3710cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        }
372e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
373e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
3742a4207fb39330e840436215c896cde911489e111Paul Soulos    private final OnCreateContextMenuListener mEntryContextMenuListener =
3752a4207fb39330e840436215c896cde911489e111Paul Soulos            new OnCreateContextMenuListener() {
3762a4207fb39330e840436215c896cde911489e111Paul Soulos        @Override
3772a4207fb39330e840436215c896cde911489e111Paul Soulos        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3782a4207fb39330e840436215c896cde911489e111Paul Soulos            if (menuInfo == null) {
3792a4207fb39330e840436215c896cde911489e111Paul Soulos                return;
3802a4207fb39330e840436215c896cde911489e111Paul Soulos            }
3812a4207fb39330e840436215c896cde911489e111Paul Soulos            EntryContextMenuInfo info = (EntryContextMenuInfo) menuInfo;
3822a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.setHeaderTitle(info.getCopyText());
3832a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.add(R.string.copy_text);
3842a4207fb39330e840436215c896cde911489e111Paul Soulos        }
3852a4207fb39330e840436215c896cde911489e111Paul Soulos    };
3862a4207fb39330e840436215c896cde911489e111Paul Soulos
3872a4207fb39330e840436215c896cde911489e111Paul Soulos    @Override
3882a4207fb39330e840436215c896cde911489e111Paul Soulos    public boolean onContextItemSelected(MenuItem item) {
3892a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo menuInfo;
3902a4207fb39330e840436215c896cde911489e111Paul Soulos        try {
3912a4207fb39330e840436215c896cde911489e111Paul Soulos            menuInfo = (EntryContextMenuInfo) item.getMenuInfo();
3922a4207fb39330e840436215c896cde911489e111Paul Soulos        } catch (ClassCastException e) {
3932a4207fb39330e840436215c896cde911489e111Paul Soulos            Log.e(TAG, "bad menuInfo", e);
3942a4207fb39330e840436215c896cde911489e111Paul Soulos            return false;
3952a4207fb39330e840436215c896cde911489e111Paul Soulos        }
3962a4207fb39330e840436215c896cde911489e111Paul Soulos
3972a4207fb39330e840436215c896cde911489e111Paul Soulos        ClipboardUtils.copyText(this, menuInfo.getCopyLabel(), menuInfo.getCopyText(), true);
3982a4207fb39330e840436215c896cde911489e111Paul Soulos        return true;
3992a4207fb39330e840436215c896cde911489e111Paul Soulos    }
4002a4207fb39330e840436215c896cde911489e111Paul Soulos
401d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
402d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
403d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
404d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
405d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
406d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
407d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
408d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
409d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
410d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
411d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
412d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
413d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
414d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
415d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
416d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
417d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
418d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
419d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
420d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
421d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
422d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
423d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
424d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
425d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
426d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
427d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
428d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
429d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
430d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
431b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
432b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
433b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
434b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
435f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            finish();
436b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
4378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
4388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
4398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
4408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
4418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
4428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
4438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
4448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
4458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
4468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
4478477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
4488477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
4498477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
4508477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
4518477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
4528477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
4538477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
454f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onEntranceAnimationDone() {
455f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            mIsEntranceAnimationFinished = true;
456f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        }
457f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
458f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        @Override
459f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onTransparentViewHeightChange(float ratio) {
460f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            if (mIsEntranceAnimationFinished) {
461f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                mWindowScrim.setAlpha((int) (0xFF * ratio));
4628477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
4638477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
464b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
465b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
466eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
467eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
468eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
469eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
470eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
471eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
472eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
473eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
474eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
475eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
476eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
477eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
478eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
479eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
480eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
481eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
482eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
483eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
484eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
485eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
486eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
487eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
488eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
489eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
490eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
491eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
492eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
493eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
494eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
495eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
496eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
497eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
498eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
499eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
500eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
501eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
502cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
503cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
504cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 1. Times used
505cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 2. Last time used
506cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 3. Statically defined
507cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
508eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
509eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
510eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
511eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
512eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem lhs = lhsList.get(0);
513eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem rhs = rhsList.get(0);
514eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
515eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
516eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
517eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
518eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
519eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
520eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
521eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
522eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
523eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
524eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
525eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
526eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
527eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
528eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
529eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
530eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
531eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
532eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Times used and last time used are the same. Resort to statically defined.
533eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String lhsMimeType = lhs.getMimeType();
534eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String rhsMimeType = rhs.getMimeType();
535eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (String mimeType : LEADING_MIMETYPES) {
536eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (lhsMimeType.equals(mimeType)) {
537eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return -1;
538eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                } else if (rhsMimeType.equals(mimeType)) {
539eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return 1;
540eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
541eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
542eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
543eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
544eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
545eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
546edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
547f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    public boolean dispatchTouchEvent(MotionEvent ev) {
548f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
549f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen            TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
550f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        }
551f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        return super.dispatchTouchEvent(ev);
552f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    }
553f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen
554f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    @Override
5558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
5568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
5578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
558edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getWindow().setStatusBarColor(Color.TRANSPARENT);
5602426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
561d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
562dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
56310d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
56410d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
56510d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
56610d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
567edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
568edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
569a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
570a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
5710cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
5720cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
573eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
5746095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
575b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
576eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
577edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5786095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
579eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
580eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
58123889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
5822a4207fb39330e840436215c896cde911489e111Paul Soulos        mContactCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
583b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
584b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
585b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
586edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
587eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
5882a4207fb39330e840436215c896cde911489e111Paul Soulos        mAboutCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
589eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
59002eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView = (QuickContactImageView) findViewById(R.id.photo);
59163176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        final View transparentView = findViewById(R.id.transparent_view);
5920d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
59363176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell            transparentView.setOnClickListener(new OnClickListener() {
5940d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
5950d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
5960d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
5970d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
5980d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
5990d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
600edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
601d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        // Allow a shadow to be shown under the toolbar.
602d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
603d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell
604d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
605d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
6069b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
6079b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
6089b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
6099b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
6106219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
6118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
612f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = mHasAlreadyBeenOpened;
6139b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
614f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mWindowScrim.setAlpha(0);
6159b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
616edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
617c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
618ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
619ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
620ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
6218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
6229b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
6239b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
624d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
625d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
626d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
627d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
628d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
629d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
630d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
631d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
632d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
63323889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
634f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ true,
635f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                new Runnable() {
636f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    @Override
637f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    public void run() {
638f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        if (!mHasAlreadyBeenOpened) {
639f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // The initial scrim opacity must match the scrim opacity that would be
640f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // achieved by scrolling to the starting position.
641f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final float alphaRatio = mExtraMode == MODE_FULLY_EXPANDED ?
642f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    1 : mScroller.getStartingTransparentHeightRatio();
643f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int duration = getResources().getInteger(
644f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    android.R.integer.config_shortAnimTime);
645f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int desiredAlpha = (int) (0xFF * alphaRatio);
646f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            ObjectAnimator o = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0,
647f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    desiredAlpha).setDuration(duration);
648f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
649f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            o.start();
650f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        }
651f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    }
652f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                });
653f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
6549b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
6559b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
656ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
657ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
658ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
659ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
660ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
661ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
662ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
663ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
664ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
665ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
666ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
667ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
668ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
669ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
670a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
671a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
6729b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
673ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
674ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
6759b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
6769b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
6778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
6788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
6798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
680405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
681405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
6828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
6838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED) {
6848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // The contact that we were showing has been deleted.
6858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
686ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos        } else if (requestCode == REQUEST_CODE_CONTACT_SELECTION_ACTIVITY &&
687ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                resultCode != RESULT_CANCELED) {
688ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos            processIntent(data);
689b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
6908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
691dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
6928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
6938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
6948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
6958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
696f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = true;
6978571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
698d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
699d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
700d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
7019b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
7029b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
7039b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
7049b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
7059b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilter.getColor());
7069b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
7079b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
7089b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
709d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
710d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
711d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
712d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
713d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
714d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
715d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
716d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
717d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
718d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE,
719d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                QuickContact.MODE_LARGE);
720d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
721d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
722d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mLookupUri = Preconditions.checkNotNull(lookupUri, "missing lookupUri");
723d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
724d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
725d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
726d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
727d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
728d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
729d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // we need to restart the loader and reload the new contact.
730899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            for (int interactionLoaderId : mRecentLoaderIds) {
731899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                getLoaderManager().destroyLoader(interactionLoaderId);
732899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            }
733405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
734405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
735d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
7362d150da246632b1649999cfabed776133b097775Brian Attwell
7372d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
7382426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
739edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
740b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
7418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
7428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
7438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
7448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
745c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
746b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
747b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
74881281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
749d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
7509b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
751f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString());
7529b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
75381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
75481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
75581281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
756f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell    private void setHeaderNameText(String value) {
757d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
7589b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
759f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell                mScroller.setTitle(value);
7609b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
76181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
76281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
76381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
76481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
765edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
766edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
767edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
768edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
769edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
770edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
771edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
772edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
773edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
774edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
775edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
776edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
777edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
778edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
779cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
780edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
7818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
7828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
7836cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
784d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
785edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
7878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
788dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
78902eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView.setIsBusiness(mContactData.isDisplayNameFromOrganization());
7906219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
79131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
7924936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        analyzeWhitenessOfPhotoAsynchronously();
793333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos        setHeaderNameText(ContactDisplayUtils.getDisplayName(this, data).toString());
794edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
796dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
7976bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mEntriesAndActionsTask = new AsyncTask<Void, Void, Cp2DataCardModel>() {
7988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
8006bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected Cp2DataCardModel doInBackground(
801eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
802eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
8038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
8048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
8058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
8066bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected void onPostExecute(Cp2DataCardModel cardDataModel) {
8076bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                super.onPostExecute(cardDataModel);
8088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
8098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
8108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
8118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
8126bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    bindDataToCards(cardDataModel);
8138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
8148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
8158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
8168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
8178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
8188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
8206bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void bindDataToCards(Cp2DataCardModel cp2DataCardModel) {
8216bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        startInteractionLoaders(cp2DataCardModel);
8226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        populateContactAndAboutCard(cp2DataCardModel);
823eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
824eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
8256bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void startInteractionLoaders(Cp2DataCardModel cp2DataCardModel) {
8266bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Map<String, List<DataItem>> dataItemsMap = cp2DataCardModel.dataItemsMap;
8276bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> phoneDataItems = dataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
828eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
829eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
830eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
831eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
832eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
833eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
834eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
835ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
836eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
837eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
838eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
8398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
8408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
841ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
842ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
843ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
844ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
845ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
846ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
847ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
848ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
8498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
850899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
8518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
852eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
853899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
8546bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> emailDataItems = dataItemsMap.get(Email.CONTENT_ITEM_TYPE);
855eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
856eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
857eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
858eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
859eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
860eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
861eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
862ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
863eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
864899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
865899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
866ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
867899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
8688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
8698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
8718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
8728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
8738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
8748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
8758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
8768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
8778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
8788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
8798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
8808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
8818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
8828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
8846bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<List<Entry>> buildAboutCardEntries(Map<String, List<DataItem>> dataItemsMap) {
885a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
886a153dba33c2152332c5706178a367f52b0550723Paul Soulos        for (String mimetype : SORTED_ABOUT_CARD_MIMETYPES) {
8876bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> mimeTypeItems = dataItemsMap.get(mimetype);
888a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (mimeTypeItems == null) {
889a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
890a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
8916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // Set aboutCardTitleOut = null, since SORTED_ABOUT_CARD_MIMETYPES doesn't contain
8926bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // the name mimetype.
8936bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<Entry> aboutEntries = dataItemsToEntries(mimeTypeItems,
8946bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    /* aboutCardTitleOut = */ null);
895a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutEntries.size() > 0) {
896a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(aboutEntries);
897a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
898a153dba33c2152332c5706178a367f52b0550723Paul Soulos        }
899a153dba33c2152332c5706178a367f52b0550723Paul Soulos        return aboutCardEntries;
900a153dba33c2152332c5706178a367f52b0550723Paul Soulos    }
901a153dba33c2152332c5706178a367f52b0550723Paul Soulos
902a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    @Override
903a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    protected void onResume() {
904a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        super.onResume();
905a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        // If returning from a launched activity, repopulate the contact and about card
906a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        if (mHasIntentLaunched) {
907a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = false;
9086bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            populateContactAndAboutCard(mCachedCp2DataCardModel);
909a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
910a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    }
911a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
9126bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void populateContactAndAboutCard(Cp2DataCardModel cp2DataCardModel) {
9136bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mCachedCp2DataCardModel = cp2DataCardModel;
9146bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (mHasIntentLaunched || cp2DataCardModel == null) {
915a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            return;
916a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
917eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
918eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
9196bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = cp2DataCardModel.contactCardEntries;
9206bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = cp2DataCardModel.aboutCardEntries;
9216bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final String customAboutCardName = cp2DataCardModel.customAboutCardName;
922eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
923eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
924eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
925eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
926c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mContactCard.isExpanded(),
92789966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                    /* isAlwaysExpanded = */ false,
9280cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener,
9290cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mScroller);
930eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
931eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
932eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
933eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
934eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
9358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
936eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
937c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        // Phonetic name is not a data item, so the entry needs to be created separately
938c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        final String phoneticName = mContactData.getPhoneticName();
939c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        if (!TextUtils.isEmpty(phoneticName)) {
940c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            Entry phoneticEntry = new Entry(/* viewId = */ -1,
941c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* icon = */ null,
942c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    getResources().getString(R.string.name_phonetic),
943c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    phoneticName,
944c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* text = */ null,
945c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* intent = */ null,
946c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIcon = */ null,
947c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIntent = */ null,
948c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateContentDescription = */ null,
949c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* shouldApplyColor = */ false,
9502a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
9512a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ new EntryContextMenuInfo(phoneticName,
95248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                            getResources().getString(R.string.name_phonetic)),
95348fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
95448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
95548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdContentDescription = */ null);
956c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            List<Entry> phoneticList = new ArrayList<>();
957c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            phoneticList.add(phoneticEntry);
958a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name comes after nickname. Check to see if the first entry type is nickname
959a0fa4c18dc111ceea9ff47f35ee01fecc1003578Paul Soulos            if (aboutCardEntries.size() > 0 && aboutCardEntries.get(0).get(0).getHeader().equals(
960a153dba33c2152332c5706178a367f52b0550723Paul Soulos                    getResources().getString(R.string.header_nickname_entry))) {
961a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(1, phoneticList);
962a153dba33c2152332c5706178a367f52b0550723Paul Soulos            } else {
963a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(0, phoneticList);
964a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
965c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        }
966c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos
9676bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (!TextUtils.isEmpty(customAboutCardName)) {
9686bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            mAboutCard.setTitle(customAboutCardName);
9696bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
9706bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
971cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos        if (aboutCardEntries.size() > 0) {
972cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos            mAboutCard.initialize(aboutCardEntries,
973cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    /* numInitialVisibleEntries = */ 1,
974cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    /* isExpanded = */ true,
975cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    /* isAlwaysExpanded = */ true,
976cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    mExpandingEntryCardViewListener,
977cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    mScroller);
978cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos        }
9796095369885edcca566a812b551886e29c7ff8039Brian Attwell
9806095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
9816095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
9826095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
9836095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
9846095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
9856095369885edcca566a812b551886e29c7ff8039Brian Attwell
986a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
987a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
988a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
989a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
990a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
991eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
992eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
993eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
994eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
9956095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
9966095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
9976095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
9986095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
9996095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
10006095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
10016095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
1002dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* subHeader = */ null, /* text = */ null, getEditContactIntent(),
1003dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
1004714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
100548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* isEditable = */ false, /* EntryContextMenuInfo = */ null,
100648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* thirdIcon = */ null, /* thirdIntent = */ null,
100748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* thirdContentDescription = */ null);
10086095369885edcca566a812b551886e29c7ff8039Brian Attwell
10096095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
10106095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
10116095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
10126095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
1013dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* text = */ null, getEditContactIntent(), /* alternateIcon = */ null,
1014dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
10152a4207fb39330e840436215c896cde911489e111Paul Soulos                /* shouldApplyColor = */ true, /* isEditable = */ false,
101648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* EntryContextMenuInfo = */ null, /* thirdIcon = */ null,
101748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* thirdIntent = */ null, /* thirdContentDescription = */ null);
10186095369885edcca566a812b551886e29c7ff8039Brian Attwell
10196095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
10206095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
10216095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
10226095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
10236095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
10246095369885edcca566a812b551886e29c7ff8039Brian Attwell
10256095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
10266095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
10276095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
10286095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
1029c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
10300cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener, mScroller);
10316095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
10326095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
10336095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
10346095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
10356095369885edcca566a812b551886e29c7ff8039Brian Attwell
10366095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
1037eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
1038eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
1039eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
1040eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
1041eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
1042eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
10436bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel generateDataModelFromContact(
1044eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
1045eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
1046eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1047eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
10488bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
10498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
1050851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
1051851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
1052eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
1053eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1054851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
1055eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
1056eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
105747b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
105847b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
105947b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
1060eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
1061cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1062eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
1063b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1064eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
1065eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
1066899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
1067eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
1068edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1069eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
1070eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
1071eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
1072eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
1073edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
1074eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
1075edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1076edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
10778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1078edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1079eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
108016339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
108116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
1082eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
1083eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
1084eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
108516339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
1086eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
1087eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
1088eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
1089eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
1090eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
1091eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
1092eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
1093eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
1094edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1095eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1096edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1097eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
1098eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
1099eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
1100eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
110116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
11026bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.beginSection("cp2 data items to entries");
11036bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
11046bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = new ArrayList<>();
11056bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = buildAboutCardEntries(dataItemsMap);
11066bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final MutableString aboutCardName = new MutableString();
11076bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
11086bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        for (int i = 0; i < dataItemsList.size(); ++i) {
11096bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> dataItemsByMimeType = dataItemsList.get(i);
11106bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final DataItem topDataItem = dataItemsByMimeType.get(0);
11116bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
11126bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                // About card mimetypes are built in buildAboutCardEntries, skip here
11136bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                continue;
11146bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            } else {
11156bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                List<Entry> contactEntries = dataItemsToEntries(dataItemsList.get(i),
11166bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        aboutCardName);
11176bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (contactEntries.size() > 0) {
11186bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    contactCardEntries.add(contactEntries);
11196bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                }
11206bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            }
11216bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
11226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
11236bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.endSection();
11246bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
11256bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Cp2DataCardModel dataModel = new Cp2DataCardModel();
11266bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.customAboutCardName = aboutCardName.value;
11276bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.aboutCardEntries = aboutCardEntries;
11286bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.contactCardEntries = contactCardEntries;
11296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.dataItemsMap = dataItemsMap;
11306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return dataModel;
11316bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
11326bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
11336bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
11346bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * Class used to hold the About card and Contact cards' data model that gets generated
11356bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * on a background thread. All data is from CP2.
11366bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
11376bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class Cp2DataCardModel {
11386bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        /**
11396bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * A map between a mimetype string and the corresponding list of data items. The data items
11406bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * are in sorted order using mWithinMimeTypeDataItemComparator.
11416bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         */
11426bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public Map<String, List<DataItem>> dataItemsMap;
11436bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> aboutCardEntries;
11446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> contactCardEntries;
11456bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String customAboutCardName;
11466bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
11476bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
11486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class MutableString {
11496bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String value;
1150eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1151edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1152eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
1153eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
1154eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
11556bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
11566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * This runs on a background thread. This is set as static to avoid accidentally adding
11576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * additional dependencies on unsafe things (like the Activity).
11586bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
1159eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
1160eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
1161eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
11626bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static Entry dataItemToEntry(DataItem dataItem,
11636bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            Context context, Contact contactData,
11646bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final MutableString aboutCardName) {
1165eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
1166eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
1167eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
1168eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
1169eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
1170eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
1171eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
117248ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
1173dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
1174dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
1175dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        String alternateContentDescription = null;
1176eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
11772a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo entryContextMenuInfo = null;
117848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Drawable thirdIcon = null;
117948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Intent thirdIntent = null;
118048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        String thirdContentDescription = null;
1181eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
11826bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        context = context.getApplicationContext();
1183eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
1184eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1185eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
1186eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
11876bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            intent = ContactsUtils.buildImIntent(context, im).first;
1188eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
11897de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            final int protocol;
11907de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (!im.isProtocolValid()) {
11917de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = Im.PROTOCOL_CUSTOM;
11927de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
11937de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
11947de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
11957de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (protocol == Im.PROTOCOL_CUSTOM) {
11967de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // If the protocol is custom, display the "IM" entry header as well to distinguish
11977de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // this entry from other ones
11986bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                header = context.getResources().getString(R.string.header_im_entry);
11996bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                subHeader = Im.getProtocolLabel(context.getResources(), protocol,
12007de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
12017de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                text = im.getData();
12027de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
12036bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                header = Im.getProtocolLabel(context.getResources(), protocol,
12047de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
12057de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = im.getData();
12067de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
12072a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(im.getData(), header);
1208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
1209eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
12106bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = context.getResources().getString(R.string.header_organization_entry);
1211eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
12122a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1213eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
1214eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
1215eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
1216eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
1217eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
12186bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                (contactData.getNameRawContactId() == dataItem.getRawContactId());
1219eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1220eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
1221eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
12226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                && contactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
1223eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1224eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
12256bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                header = context.getResources().getString(R.string.header_nickname_entry);
1226eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
12272a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1228eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1229eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
1230eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
12316bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = context.getResources().getString(R.string.header_note_entry);
1232eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
12332a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1234eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
1235eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
12366bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = context.getResources().getString(R.string.header_website_entry);
1237eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
12382a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1239eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
12406bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                final WebAddress webAddress = new WebAddress(website.buildDataString(context,
12416bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        kind));
1242eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1243eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
12446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                Log.e(TAG, "Couldn't parse website: " + website.buildDataString(context, kind));
1245eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1246eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1247eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
12486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final String dataString = event.buildDataString(context, kind);
1249eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1250eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1251eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1252eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1253eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1254eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1255eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1256eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1257eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
12586bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = context.getResources().getString(R.string.header_event_entry);
1259f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
12606bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                subHeader = Event.getTypeLabel(context.getResources(), event.getKindTypeColumn(kind),
12617de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        event.getLabel()).toString();
1262f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
12636bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            text = DateUtils.formatDate(context, dataString);
1264f848c49cac796f99549c8418f8d4e2469ac9f963Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(text, header);
1265eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1266eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
12676bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final String dataString = relation.buildDataString(context, kind);
1268eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1269eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1270eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1271eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1272eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
12736bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = context.getResources().getString(R.string.header_relation_entry);
1274eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
12752a4207fb39330e840436215c896cde911489e111Paul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header);
1276f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
12776bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                text = Relation.getTypeLabel(context.getResources(),
12786bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        relation.getKindTypeColumn(kind),
1279f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1280f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1281eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1282eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
1283eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
12846bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                header = phone.buildDataString(context, kind);
12852a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
12866bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        context.getResources().getString(R.string.phoneLabelsGroup));
1287f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
12886bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    text = Phone.getTypeLabel(context.getResources(), phone.getKindTypeColumn(kind),
1289f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            phone.getLabel()).toString();
1290f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
12916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                icon = context.getResources().getDrawable(R.drawable.ic_phone_24dp);
12926bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (PhoneCapabilityTester.isPhone(context)) {
1293eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
129416339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1295dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
1296dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                        Uri.fromParts(CallUtil.SCHEME_SMSTO, phone.getNumber(), null));
129748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
12986bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                alternateIcon = context.getResources().getDrawable(R.drawable.ic_message_24dp);
12996bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                alternateContentDescription = context.getResources().getString(R.string.sms_other);
130048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
130148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                // Add video call button if supported
1302f764730a34da560db87206f9a48390d4c8085365Paul Soulos                if (CallUtil.isVideoEnabled(context)) {
130348fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdIcon = context.getResources().getDrawable(R.drawable.ic_videocam);
130448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdIntent = CallUtil.getVideoCallIntent(phone.getNumber(),
130548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                            CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY);
130648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdContentDescription =
130748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                            context.getResources().getString(R.string.description_video_call);
130848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                }
1309eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1310eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1311eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1312eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1313eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
1314eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri mailUri = Uri.fromParts(CallUtil.SCHEME_MAILTO, address, null);
1315eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1316eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
13172a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
13186bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        context.getResources().getString(R.string.emailLabelsGroup));
1319f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
13206bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    text = Email.getTypeLabel(context.getResources(), email.getKindTypeColumn(kind),
1321f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
1322f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
13236bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                icon = context.getResources().getDrawable(R.drawable.ic_email_24dp);
1324eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1325eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1326eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1327eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1328eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
1329eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1330eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
13312a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
13326bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        context.getResources().getString(R.string.postalLabelsGroup));
1333f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
13346bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    text = StructuredPostal.getTypeLabel(context.getResources(),
1335f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
1336f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
13376a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIntent =
13386a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                        StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
13396bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                alternateIcon = context.getResources().getDrawable(R.drawable.ic_directions_24dp);
13406bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                icon = context.getResources().getDrawable(R.drawable.ic_place_24dp);
1341eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1342eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
13436bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (PhoneCapabilityTester.isSipPhone(context)) {
1344eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1345eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final String address = sip.getSipAddress();
1346eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (!TextUtils.isEmpty(address)) {
1347eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    final Uri callUri = Uri.fromParts(CallUtil.SCHEME_SIP, address, null);
1348eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
13491cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    header = address;
13502a4207fb39330e840436215c896cde911489e111Paul Soulos                    entryContextMenuInfo = new EntryContextMenuInfo(header,
13516bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                            context.getResources().getString(R.string.phoneLabelsGroup));
13521cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    if (sip.hasKindTypeColumn(kind)) {
13536bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        text = SipAddress.getTypeLabel(context.getResources(),
13546bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                                sip.getKindTypeColumn(kind), sip.getLabel()).toString();
13551cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    }
13566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    icon = context.getResources().getDrawable(R.drawable.ic_dialer_sip_black_24dp);
135716339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1358edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1359eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
1360eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
1361eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(givenName)) {
13626bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                aboutCardName.value = context.getResources().getString(R.string.about_card_title) +
13636bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        " " + givenName;
1364eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
13656bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                aboutCardName.value = context.getResources().getString(R.string.about_card_title);
1366eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1367eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1368eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
13696bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = dataItem.buildDataStringForDisplay(context, kind);
1370eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1371eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
137260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
137360e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
1374e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1375e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
1376e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
1377e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1378e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                // Attempt to use known icons for known 3p types. Otherwise default to ResolveCache
1379e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
1380e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
1381e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE.equals(
1382e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                intent.getDataString())) {
13836bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                            icon = context.getResources().getDrawable(
1384e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                    R.drawable.ic_add_to_circles_black_24);
1385e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
13866bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                            icon = context.getResources().getDrawable(R.drawable.ic_google_plus_24dp);
1387e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1388e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1389e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
1390e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_HANGOUTS_VIDEO.equals(intent.getDataString())) {
13916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                            icon = context.getResources().getDrawable(R.drawable.ic_hangout_video_24dp);
1392e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
13936bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                            icon = context.getResources().getDrawable(R.drawable.ic_hangout_24dp);
1394e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1395e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1396e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
13972a4207fb39330e840436215c896cde911489e111Paul Soulos                        entryContextMenuInfo = new EntryContextMenuInfo(header, mimetype);
13986bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        icon = ResolveCache.getInstance(context).getIcon(
1399e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
1400e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
1401e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
1402e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
1403e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
140448ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
1405e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
1406e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
1407eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1408b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1409eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
1410eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
14116bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, intent)) {
1412eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
1413eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1414eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1415eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1416dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
1417dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
14186bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, alternateIntent)) {
1419dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
1420dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1421dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1422dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Attempt to use package manager to find a suitable content description if needed
1423dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (TextUtils.isEmpty(alternateContentDescription)) {
14246bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                alternateContentDescription = getIntentResolveLabel(alternateIntent, context);
1425dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1426dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1427dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1428eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
1429eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
1430eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
1431eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
1432eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1433eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1434fa238949855fba3b28a57fad3d585b13e80362bbBrian Attwell        // Ignore dataIds from the Me profile.
1435ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
1436ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
1437ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
1438dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon, intent,
1439dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon, alternateIntent, alternateContentDescription, shouldApplyColor,
144048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                isEditable, entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription);
1441eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1442eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
14436bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<Entry> dataItemsToEntries(List<DataItem> dataItems,
14446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            MutableString aboutCardTitleOut) {
1445eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1446eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
14476bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final Entry entry = dataItemToEntry(dataItem, this, mContactData, aboutCardTitleOut);
1448eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (entry != null) {
1449eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                entries.add(entry);
1450eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1451eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1452eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
1453edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1454edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
14556bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static String getIntentResolveLabel(Intent intent, Context context) {
14566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent,
1457dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
1458dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1459dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
1460dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
1461dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
1462dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
1463dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
1464dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
14656bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            bestResolve = ResolveCache.getInstance(context).getBestResolve(intent, matches);
1466dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1467dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1468dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
1469dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
1470dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1471dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
14726bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return String.valueOf(bestResolve.loadLabel(context.getPackageManager()));
1473dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
1474dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1475edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
147631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
147731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
147831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
147931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
148031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
148131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
148231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
148331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
148431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1485faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
148631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1487faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
1488faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
148995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                if (imageViewDrawable instanceof BitmapDrawable
149095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData() != null
149195c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData().length > 0) {
149295c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // Perform the color analysis on the thumbnail instead of the full sized
149395c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // image, so that our results will be as similar as possible to the Bugle
149495c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // app.
149595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    final Bitmap bitmap = BitmapFactory.decodeByteArray(
149695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData(), 0,
149795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData().length);
149895c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    try {
149995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        final int primaryColor = colorFromBitmap(bitmap);
150095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        if (primaryColor != 0) {
150195c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
150295c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                                    primaryColor);
150395c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        }
150495c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    } finally {
150595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        bitmap.recycle();
1506faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
15078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
15088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
1509faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
1510a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
151131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
1512a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
151331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
151431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
151531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1516faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
1517faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
15188571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
15198571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
15208571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
15218571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
15228571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
15238571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
15248571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
15258571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
15268571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
15278571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
15288571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
15298571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
1530faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
153131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
153231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
153331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
153431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
153531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
15364936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    /**
15374936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * Examine how many white pixels are in the bitmap in order to determine whether or not
15384936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * we need gradient overlays on top of the image.
15394936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     */
15404936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    private void analyzeWhitenessOfPhotoAsynchronously() {
15414936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
15424936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        new AsyncTask<Void, Void, Boolean>() {
15434936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
15444936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected Boolean doInBackground(Void... params) {
15454936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
15464936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
15474936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    return WhitenessUtils.isBitmapWhiteAtTopOrBottom(bitmap);
15484936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                }
15494936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                return !(imageViewDrawable instanceof LetterTileDrawable);
15504936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
15514936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
15524936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
15534936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected void onPostExecute(Boolean isWhite) {
15544936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                super.onPostExecute(isWhite);
15554936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                mScroller.setUseGradient(isWhite);
15564936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
15574936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        }.execute();
15584936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    }
15594936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
1560faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
15619b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
1562faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        final int primaryColor = palette.mPrimaryColor;
1563faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mScroller.setHeaderTintColor(primaryColor);
1564faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
15659b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
15668571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
15679b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
1568faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                new PorterDuffColorFilter(primaryColor, PorterDuff.Mode.SRC_ATOP);
1569faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mContactCard.setColorAndFilter(primaryColor, mColorFilter);
1570faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mRecentCard.setColorAndFilter(primaryColor, mColorFilter);
1571faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mAboutCard.setColorAndFilter(primaryColor, mColorFilter);
15729b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
15739b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
15748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
15758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller == null) {
15768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
15778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
15788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
15798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
15808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
15818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
15828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
15838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
15848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
15858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
1586847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
1587847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell                getWindow().getStatusBarColor(), desiredStatusBarColor);
1588847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
1589847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setEvaluator(new ArgbEvaluator());
1590847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.start();
15918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
15928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
15938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
15948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
15958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
15968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
1597a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
1598a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
15998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
16008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
16018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
16028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1603b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
1604eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1605b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
1606ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
1607ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
1608b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
1609b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
1610b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
1611b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
1612b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
1613b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
1614dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
1615dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
1616dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
161748ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
16182a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
161948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* EntryContextMenuInfo = */ null,
162048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
162148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
162248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdContentDescription = */ null));
1623b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1624b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
1625b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1626b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1627eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
1628851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
1629cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1630851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
1631405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
1632cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1633cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1634cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1635851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
16368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
16378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1638cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (isFinishing()) {
1639cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1640cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1641cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isError()) {
1642cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // This shouldn't ever happen, so throw an exception. The {@link ContactLoader}
1643cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // should log the actual exception.
1644cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                throw new IllegalStateException("Failed to load contact", data.getException());
1645cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1646cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isNotFound()) {
16478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (mHasAlreadyBeenOpened) {
16488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    finish();
16498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                } else {
16508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
16518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
16528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
16538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
1654cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1655cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1656cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1657b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            bindContactData(data);
1658cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
16598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.endSection();
1660cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1661cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1662cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1663851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
1664cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
1665cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
1666cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1667d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
1668d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
1669b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
1670d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
16718571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
1672cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1673cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
1674b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1675b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
1676b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
1677b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
16788477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
16798477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
16808477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
1681b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
1682b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
1683b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
1684b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
1685b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
16868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
16878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
16888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
16898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
16908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
16918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
16928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
16938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1694eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
1695b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
1696b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1697b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1698b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
1699b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Log.v(TAG, "onCreateLoader");
1700b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
1701b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
1702b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
1703b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    Log.v(TAG, "LOADER_SMS_ID");
1704b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
1705b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
1706ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1707b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
1708b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
1709899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
1710899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    Log.v(TAG, "LOADER_CALENDAR_ID");
1711ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
1712ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
1713ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
1714ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
1715ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
1716899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
1717899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
1718ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
1719899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
1720899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
1721899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
1722899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
1723899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
1724ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
1725ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    Log.v(TAG, "LOADER_CALL_LOG_ID");
1726ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
1727ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
1728ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1729ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
1730b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1731b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
1732b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1733b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1734b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1735b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
1736b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
1737b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
1738b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1739b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
1740b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
1741b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1742b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1743b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1744b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1745b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
1746b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
1747b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1748b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
1749b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1750b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
1751b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
1752b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1753b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1754b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
1755eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
1756b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
1757b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            allInteractions.addAll(loaderInteractions);
1758b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1759b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1760b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        // Sort the interactions by most recent
1761b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
1762b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
1763b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            public int compare(ContactInteraction a, ContactInteraction b) {
1764b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                return a.getInteractionDate() >= b.getInteractionDate() ? -1 : 1;
1765b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1766b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        });
1767b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
176897ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        // Wrap each interaction in its own list so that an icon is displayed for each entry
176960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        List<List<Entry>> interactionsWrapper = new ArrayList<>();
177097ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        for (Entry contactInteraction : contactInteractionsToEntries(allInteractions)) {
177197ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            List<Entry> entryListWrapper = new ArrayList<>(1);
177297ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            entryListWrapper.add(contactInteraction);
177397ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            interactionsWrapper.add(entryListWrapper);
177497ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        }
1775b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        if (allInteractions.size() > 0) {
177660e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            mRecentCard.initialize(interactionsWrapper,
1777b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
1778c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
17790cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener, mScroller);
1780b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentCard.setVisibility(View.VISIBLE);
1781b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1782eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1783eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // About card is initialized along with the contact card, but since it appears after
1784eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // the recent card in the UI, we hold off until making it visible until the recent card
1785eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // is also ready to avoid stuttering.
1786eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (mAboutCard.shouldShow()) {
1787eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1788eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1789eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.GONE);
1790eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1791b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
17928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
17938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
17948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
17958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
17968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
17978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
17988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
17998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
18008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
18018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
18028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
18038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
18048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
180523889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
180623889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
1807d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
1808d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
1809d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
1810d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1811d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1812d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1813a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    /**
1814a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     * Returns true if it is possible to share the current contact.
1815a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     */
1816a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    private boolean isContactShareable() {
1817a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1818a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    }
1819a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
18206095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
182163176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        final Intent intent = new Intent(Intent.ACTION_EDIT, mContactData.getLookupUri());
1822d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
18236095369885edcca566a812b551886e29c7ff8039Brian Attwell        return intent;
18246095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
18256095369885edcca566a812b551886e29c7ff8039Brian Attwell
18266095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
1827a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        mHasIntentLaunched = true;
1828c00a0b5370f0714f6af1ea9b3e29b10f25e91af0Zheng Fu        mContactLoader.cacheResult();
18296095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
1830d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1831d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1832d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
1833d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
183463176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        if (mContactData != null) {
1835d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
1836d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
1837d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
1838d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
1839d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1840d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
1841333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
1842d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1843d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
1844d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1845d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
1846eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
184763176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell                    QuickContactActivity.this, mContactData.getLookupUri(), !isStarred);
1848d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
184935ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell
185035ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            final CharSequence accessibilityText = !isStarred
185135ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    ? getResources().getText(R.string.description_action_menu_add_star)
185235ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    : getResources().getText(R.string.description_action_menu_remove_star);
185335ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // Accessibility actions need to have an associated view. We can't access the MenuItem's
185435ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // underlying view, so put this accessibility action on the root view.
185535ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            mScroller.announceForAccessibility(accessibilityText);
1856d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
1857d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1858d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1859752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1860752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Calls into the contacts provider to get a pre-authorized version of the given URI.
1861752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1862752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private Uri getPreAuthorizedUri(Uri uri) {
1863752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle uriBundle = new Bundle();
1864752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        uriBundle.putParcelable(ContactsContract.Authorization.KEY_URI_TO_AUTHORIZE, uri);
1865752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle authResponse = getContentResolver().call(
1866752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.AUTHORITY_URI,
1867752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.Authorization.AUTHORIZATION_METHOD,
1868752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                null,
1869752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                uriBundle);
1870752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (authResponse != null) {
1871752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return (Uri) authResponse.getParcelable(
1872752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    ContactsContract.Authorization.KEY_AUTHORIZED_URI);
1873752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        } else {
1874752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return uri;
1875752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1876752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
18772d150da246632b1649999cfabed776133b097775Brian Attwell
1878752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
1879752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
1880752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
1881752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (mContactData.isUserProfile()) {
1882752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // User is sharing the profile.  We don't want to force the receiver to have
1883752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // the highly-privileged READ_PROFILE permission, so we need to request a
1884752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // pre-authorized URI from the provider.
1885752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            shareUri = getPreAuthorizedUri(shareUri);
1886752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1887752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1888752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
1889752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.setType(Contacts.CONTENT_VCARD_TYPE);
1890752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
1891752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1892752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
1893752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
1894752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
1895752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1896752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
1897a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
1898752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            this.startActivity(chooseIntent);
1899eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
1900752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
1901752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1902752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1903752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1904752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1905752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
1906752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1907752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
1908752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
1909752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
1910752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1911752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
1912752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
1913752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
1914752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
1915752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
1916752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
1917752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1918752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
1919752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
1920752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        Toast.makeText(QuickContactActivity.this,
1921752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                R.string.createContactShortcutSuccessful,
1922752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
1923752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
1924752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1925752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
192663176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        builder.createContactShortcutIntent(mContactData.getLookupUri());
1927752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1928752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1929d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1930d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
1931eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
1932d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
1933d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
1934d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1935d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1936d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1937d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
1938d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
1939d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
1940333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
1941d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1942d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
1943a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
1944d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
1945d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
1946d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
1947d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
1948d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
194930cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
19502e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
1951d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
1952d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
19532e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
1954d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
1955d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
1956d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
1957a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
1958a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            final MenuItem shareMenuItem = menu.findItem(R.id.menu_share);
1959a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            shareMenuItem.setVisible(isContactShareable());
1960a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
19618a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
1962d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
19638a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
1964d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1965d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1966d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1967d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
1968d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
1969d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
1970d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
1971d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1972d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
1973d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
1974ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // This action is used to launch the contact selector, with the option of
1975ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // creating a new contact. Creating a new contact is an INSERT, while selecting
1976ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // an exisiting one is an edit. The fields in the edit screen will be
1977ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // prepopulated with data.
1978ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
1979ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1980ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.setType(Contacts.CONTENT_ITEM_TYPE);
1981ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
1982ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Only pre-fill the name field if the provided display name is an organization
1983ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // name or better (e.g. structured name, nickname)
1984ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    if (mContactData.getDisplayNameSource() >= DisplayNameSources.ORGANIZATION) {
1985ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.NAME, mContactData.getDisplayName());
1986ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
1987ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    ArrayList<ContentValues> values = mContactData.getContentValues();
1988ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Last time used and times used are aggregated values from the usage stat
1989ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // table. They need to be removed from data values so the SQL table can insert
1990ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // properly
1991ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    for (ContentValues value : values) {
1992ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.LAST_TIME_USED);
1993ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.TIMES_USED);
1994ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
1995ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.putExtra(Intents.Insert.DATA, values);
1996ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
1997ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // If the contact can only export to the same account, add it to the intent.
1998ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Otherwise the ContactEditorFragment will show a dialog for selecting an
1999ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // account.
2000ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    if (mContactData.getDirectoryExportSupport() ==
2001ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                            Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY) {
2002ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.ACCOUNT,
2003ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                new Account(mContactData.getDirectoryAccountName(),
2004ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                        mContactData.getDirectoryAccountType()));
2005ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.DATA_SET,
2006ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                mContactData.getRawContacts().get(0).getDataSet());
2007ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2008ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2009ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    startActivityForResult(intent, REQUEST_CODE_CONTACT_SELECTION_ACTIVITY);
2010d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
2011d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
2012d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
2013d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
2014d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
2015d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
2016752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
2017752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                shareContact();
2018752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
2019752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
2020752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                createLauncherShortcutWithContact();
2021752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
2022d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
2023d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
2024d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
202523889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
2026edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
2027