QuickContactActivity.java revision bcae18d136522e190d5074909e5b7148c00f0db8
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;
348d02b165232cb219f082e634db45374aa43e3f6aWenyi Wangimport android.content.res.ColorStateList;
3523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulosimport android.content.res.Resources;
3631b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.Bitmap;
3795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwellimport android.graphics.BitmapFactory;
388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.Color;
39eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuff;
40eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuffColorFilter;
4131b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.BitmapDrawable;
428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.drawable.ColorDrawable;
4331b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.Drawable;
44edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.net.Uri;
4531b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.os.AsyncTask;
46edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.os.Bundle;
478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.os.Trace;
48eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.CalendarContract;
49e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport 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;
7123d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wangimport android.support.v4.content.ContextCompat;
7231b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
73e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.support.v7.widget.CardView;
740319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.PhoneAccount;
750319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.TelecomManager;
76c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwellimport android.text.BidiFormatter;
77b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wangimport android.text.Spannable;
787ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jangimport android.text.SpannableString;
79c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwellimport android.text.TextDirectionHeuristics;
80edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
81edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
822a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu;
832a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu.ContextMenuInfo;
84e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.view.LayoutInflater;
85d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
86d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
87eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
88f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chenimport android.view.MotionEvent;
89edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View;
90edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View.OnClickListener;
912a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.View.OnCreateContextMenuListener;
9210d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawaimport android.view.WindowManager;
93bcae18d136522e190d5074909e5b7148c00f0db8guanxiongliuimport android.view.accessibility.AccessibilityEvent;
94e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.Button;
95e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.CheckBox;
96e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.ImageView;
97e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.LinearLayout;
98e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.TextView;
99edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
100d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
101edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
10272b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
103d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
1042d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
105e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
106d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wangimport com.android.contacts.activities.ContactEditorBaseActivity;
107eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
1082a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.common.ClipboardUtils;
109eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
110e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.common.ContactPhotoManager;
111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
112f62363701d76b1283edeb7305fc19249fc34899cWalter Jangimport com.android.contacts.common.activity.RequestDesiredPermissionsActivity;
113bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwellimport com.android.contacts.common.activity.RequestPermissionsActivity;
1144c3d3e24b0de8b4170106e312f6a4ac8139e1242Wenyi Wangimport com.android.contacts.common.compat.CompatUtils;
115c85af2872f231f48b586c30f6306083a039dd6c6Wenyi Wangimport com.android.contacts.common.compat.EventCompat;
1165f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunnimport com.android.contacts.common.dialog.CallSubjectDialog;
117d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.editor.SelectAccountDialogFragment;
11870e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chenimport com.android.contacts.common.interactions.TouchPointManager;
11931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport com.android.contacts.common.lettertiles.LetterTileDrawable;
120752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder;
121752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
12262a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jangimport com.android.contacts.common.logging.Logger;
12362a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jangimport com.android.contacts.common.logging.ScreenEvent.ScreenType;
1240d5588da244d0992c3ff8f25d0875fdf95a8c644Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
125cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.Contact;
126cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.ContactLoader;
127cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.RawContact;
128428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.AccountType;
129d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.model.account.AccountWithDataSet;
130cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.DataItem;
131428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.dataitem.DataKind;
132cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.EmailDataItem;
133eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.EventDataItem;
134cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.ImDataItem;
135eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NicknameDataItem;
136eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NoteDataItem;
137eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.OrganizationDataItem;
138b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.common.model.dataitem.PhoneDataItem;
139eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.RelationDataItem;
140eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.SipAddressDataItem;
141eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredNameDataItem;
142eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredPostalDataItem;
143eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.WebsiteDataItem;
144e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.common.model.ValuesDelta;
145c6100ffd22ae176a3e84a1062d8cb92d955faef2Brian Attwellimport com.android.contacts.common.util.ImplicitIntentsUtil;
146eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.util.DateUtils;
147faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils;
148faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
1495f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunnimport com.android.contacts.common.util.UriUtils;
150d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwellimport com.android.contacts.common.util.ViewUtil;
151333091ae754ddfc25714c14b9b89534be24379f9Paul Soulosimport com.android.contacts.detail.ContactDisplayUtils;
152e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.editor.AggregationSuggestionEngine;
153e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.editor.AggregationSuggestionEngine.Suggestion;
154f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulosimport com.android.contacts.editor.ContactEditorFragment;
1555a7a23bdb698b8e741a425c9617c5e33e6314cddWalter Jangimport com.android.contacts.editor.EditorIntents;
156899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
157ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
159b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
160e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.interactions.JoinContactsDialogFragment;
1615368489acaadac82706ca0e678ec04d662db702eWenyi Wangimport com.android.contacts.interactions.JoinContactsDialogFragment.JoinContactsListener;
162b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1632d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
1642a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryContextMenuInfo;
1652ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryTag;
166e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
167493f8d14b1d974a2939a110ce89fa68e0c915c36Brian Attwellimport com.android.contacts.quickcontact.WebAddress.ParseException;
168e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
169eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
170b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
171eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
172b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
173b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
17402eaf11669d25fd885606b3b0700358323532cddBrian Attwellimport com.android.contacts.widget.QuickContactImageView;
17556bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwellimport com.android.contactsbind.HelpUtils;
17656bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell
177e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
178e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
17982a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shraunerimport java.lang.SecurityException;
1802d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
181899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
182eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
183b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
184b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
185eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
186edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
187e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport java.util.HashSet;
188edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
189b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
190e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport java.util.Set;
191e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport java.util.TreeSet;
19212ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shraunerimport java.util.concurrent.ConcurrentHashMap;
193edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
194edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
195edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
196edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
197edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
198edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
199e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangpublic class QuickContactActivity extends ContactsActivity
2005368489acaadac82706ca0e678ec04d662db702eWenyi Wang        implements AggregationSuggestionEngine.Listener, JoinContactsListener {
2018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
2028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
2038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
2048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
2058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
2068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
2078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
2088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
20962a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jang    /** Used to pass the screen where the user came before launching this Activity. */
21062a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jang    public static final String EXTRA_PREVIOUS_SCREEN_TYPE = "previous_screen_type";
21162a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jang
212edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
213edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
2149b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
215e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private static final String KEY_IS_SUGGESTION_LIST_COLLAPSED = "is_suggestion_list_collapsed";
216e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private static final String KEY_SELECTED_SUGGESTION_CONTACTS = "selected_suggestion_contacts";
21718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private static final String KEY_PREVIOUS_CONTACT_ID = "previous_contact_id";
21818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private static final String KEY_SUGGESTIONS_AUTO_SELECTED = "suggestions_auto_seleted";
2199b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
2209b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
2218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
2220b267fec5cb5b2d836057b16d0085be349be5243Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xC8, 0, 0, 0);
223ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos    private static final int REQUEST_CODE_CONTACT_SELECTION_ACTIVITY = 2;
224ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
225dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
226752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
227752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
228752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
229edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
230edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
231edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
232edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
233e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
234e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
2356a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE = "addtocircle";
2366a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String GPLUS_PROFILE_DATA_5_VIEW_PROFILE = "view";
237e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
238e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
2396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String HANGOUTS_DATA_5_VIDEO = "hangout";
2406a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String HANGOUTS_DATA_5_MESSAGE = "conversation";
24148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos    private static final String CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY =
24248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos            "com.android.contacts.quickcontact.QuickContactActivity";
243e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
24463176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell    /**
24563176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * The URI used to load the the Contact. Once the contact is loaded, use Contact#getLookupUri()
24663176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * instead of referencing this URI.
24763176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     */
248edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
249edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
2508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
25105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell    private String mExtraPrioritizedMimeType;
2528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
2538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
25497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private boolean mOnlyOnePhoneNumber;
25597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private boolean mOnlyOneEmail;
256edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
25702eaf11669d25fd885606b3b0700358323532cddBrian Attwell    private QuickContactImageView mPhotoView;
258eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
2596095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
260b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
261eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
262e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
263e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    // Suggestion card.
26418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private CardView mCollapsedSuggestionCardView;
26518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private CardView mExpandSuggestionCardView;
26618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private View mCollapasedSuggestionHeader;
26718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private TextView mCollapsedSuggestionCardTitle;
26818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private TextView mExpandSuggestionCardTitle;
269e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private ImageView mSuggestionSummaryPhoto;
270e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private TextView mSuggestionForName;
2715368489acaadac82706ca0e678ec04d662db702eWenyi Wang    private TextView mSuggestionContactsNumber;
272e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private LinearLayout mSuggestionList;
27318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private Button mSuggestionsCancelButton;
2745585c6ef5e8f075bdc3d514887322c6fa683f4cbTingting Wang    private Button mSuggestionsLinkButton;
275e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private boolean mIsSuggestionListCollapsed;
27618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private boolean mSuggestionsShouldAutoSelected = true;
27718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private long mPreviousContactId = 0;
278e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
279b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
280d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
2816bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private AsyncTask<Void, Void, Cp2DataCardModel> mEntriesAndActionsTask;
28230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell    private AsyncTask<Void, Void, Void> mRecentDataTask;
283e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
284e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private AggregationSuggestionEngine mAggregationSuggestionEngine;
285e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private List<Suggestion> mSuggestions;
286e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
287e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private TreeSet<Long> mSelectedAggregationIds = new TreeSet<>();
2886bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
2896bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * The last copy of Cp2DataCardModel that was passed to {@link #populateContactAndAboutCard}.
2906bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
2916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel mCachedCp2DataCardModel;
292f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    /**
293f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  This scrim's opacity is controlled in two different ways. 1) Before the initial entrance
294f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  animation finishes, the opacity is animated by a value animator. This is designed to
295f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  distract the user from the length of the initial loading time. 2) After the initial
296f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  entrance animation, the opacity is directly related to scroll position.
297f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     */
2989b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
299f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    private boolean mIsEntranceAnimationFinished;
300a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
3018477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
3028571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
303edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
304a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    /**
305a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * Used to stop the ExpandingEntry cards from adjusting between an entry click and the intent
306a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * being launched.
307a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     */
308a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    private boolean mHasIntentLaunched;
309a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
3106cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
3119815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
3129b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
3138e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell    private int mColorFilterColor;
3149815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
3159758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
3169758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
317edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
318cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
319edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
320edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
321edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
322edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
323edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
324cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
325405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
326edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
327a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private static final List<String> SORTED_ABOUT_CARD_MIMETYPES = Lists.newArrayList(
328a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Nickname.CONTENT_ITEM_TYPE,
329a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name is inserted after nickname if it is available.
330a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // No mimetype for phonetic name exists.
331a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Website.CONTENT_ITEM_TYPE,
332a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Organization.CONTENT_ITEM_TYPE,
333a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Event.CONTENT_ITEM_TYPE,
334a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Relation.CONTENT_ITEM_TYPE,
335a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Im.CONTENT_ITEM_TYPE,
336a153dba33c2152332c5706178a367f52b0550723Paul Soulos            GroupMembership.CONTENT_ITEM_TYPE,
337a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Identity.CONTENT_ITEM_TYPE,
338a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Note.CONTENT_ITEM_TYPE);
339eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
340c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell    private static final BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
341c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell
342b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
343b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
344b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
345ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
346ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
347ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
348b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
349b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
350b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
351ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
352ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
353899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
354ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
355ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
356899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
357899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
358899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
35940d7a65c6e68967f3f486715b194c144d93c296fPaul Soulos            1L * 24L * 60L * 60L * 1000L /* 1 day */;
360899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
36140d7a65c6e68967f3f486715b194c144d93c296fPaul Soulos            7L * 24L * 60L * 60L * 1000L /* 7 days */;
362899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
363ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
364ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
365ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
3666095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
3676095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
3686095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
369ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
370ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
371ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
372ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
373ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
374ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
37512ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner    /**
37612ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * ConcurrentHashMap constructor params: 4 is initial table size, 0.9f is
37712ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * load factor before resizing, 1 means we only expect a single thread to
37812ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * write to the map so make only a single shard
37912ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     */
38012ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults =
38112ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner        new ConcurrentHashMap<>(4, 0.9f, 1);
382b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
383d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
3846cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
3852d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
3862d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
3872d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
3882ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Object entryTagObject = v.getTag();
3892ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (entryTagObject == null || !(entryTagObject instanceof EntryTag)) {
3902ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                Log.w(TAG, "EntryTag was not used correctly");
3916095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
3926095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
3932ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final EntryTag entryTag = (EntryTag) entryTagObject;
3942ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Intent intent = entryTag.getIntent();
3952ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final int dataId = entryTag.getId();
3962ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos
3972ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
3982ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                editContact();
3992d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
4002d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
401ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
40219c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            // Pass the touch point through the intent for use in the InCallUI
40319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            if (Intent.ACTION_CALL.equals(intent.getAction())) {
40419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                if (TouchPointManager.getInstance().hasValidPoint()) {
40519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    Bundle extras = new Bundle();
40619c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    extras.putParcelable(TouchPointManager.TOUCH_POINT,
40719c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                            TouchPointManager.getInstance().getPoint());
40819c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
40919c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                }
41019c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            }
41119c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
41219c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
41319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
41419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            mHasIntentLaunched = true;
41519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            try {
4168c434e5bcc92963f34200634ae5e0ccbe0e7c473Tingting Wang                ImplicitIntentsUtil.startActivityInAppIfPossible(QuickContactActivity.this, intent);
41719c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            } catch (SecurityException ex) {
41819c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
41919c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        Toast.LENGTH_SHORT).show();
42019c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Log.e(TAG, "QuickContacts does not have permission to launch "
42119c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        + intent);
42219c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            } catch (ActivityNotFoundException ex) {
42319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
42419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        Toast.LENGTH_SHORT).show();
42519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            }
42619c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
427ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
428ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
429ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
430ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
43120bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            final Uri intentUri = intent.getData();
43220bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            if ((intentUri != null && intentUri.getScheme() != null &&
4331cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                    intentUri.getScheme().equals(ContactsUtils.SCHEME_SMSTO)) ||
434ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
435ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
436ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
437ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
438ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
439ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
44020bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                final Uri dataUsageUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
441ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
442ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
443ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
4449881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                try {
4459881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    final boolean successful = getContentResolver().update(
4469881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                            dataUsageUri, new ContentValues(), null, null) > 0;
4479881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    if (!successful) {
4489881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                        Log.w(TAG, "DataUsageFeedback increment failed");
4499881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    }
4509881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                } catch (SecurityException ex) {
4519881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    Log.w(TAG, "DataUsageFeedback increment failed", ex);
452ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
453ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
454ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
455ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
4562d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
4572d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
4582d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
459e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
460e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
461e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
462e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
463e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
464e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
4650cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
4660cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        @Override
467245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        public void onExpand() {
468245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell            mScroller.setDisableTouchesForSuppressLayout(/* areTouchesDisabled = */ true);
469245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        }
470245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell
471245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        @Override
472245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        public void onExpandDone() {
473245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell            mScroller.setDisableTouchesForSuppressLayout(/* areTouchesDisabled = */ false);
4740cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        }
475e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
476e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
477e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    @Override
478e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    public void onAggregationSuggestionChange() {
4791d86a67a46e5904bb682496f109c6e236b314381Wenyi Wang        if (mAggregationSuggestionEngine == null) {
4801d86a67a46e5904bb682496f109c6e236b314381Wenyi Wang            return;
4811d86a67a46e5904bb682496f109c6e236b314381Wenyi Wang        }
482e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestions = mAggregationSuggestionEngine.getSuggestions();
48318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardView.setVisibility(View.GONE);
48418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardView.setVisibility(View.GONE);
485e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestionList.removeAllViews();
486e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
48796efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        if (mContactData == null) {
48896efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang            return;
48996efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        }
49096efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang
491e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final String suggestionForName = mContactData.getDisplayName();
492e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final int suggestionNumber = mSuggestions.size();
493e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
494e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        if (suggestionNumber <= 0) {
495e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            mSelectedAggregationIds.clear();
496e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            return;
497e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        }
498e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
4995368489acaadac82706ca0e678ec04d662db702eWenyi Wang        ContactPhotoManager.DefaultImageRequest
5005368489acaadac82706ca0e678ec04d662db702eWenyi Wang                request = new ContactPhotoManager.DefaultImageRequest(
5015368489acaadac82706ca0e678ec04d662db702eWenyi Wang                suggestionForName, mContactData.getLookupKey(), ContactPhotoManager.TYPE_DEFAULT,
5025368489acaadac82706ca0e678ec04d662db702eWenyi Wang                /* isCircular */ true );
5035368489acaadac82706ca0e678ec04d662db702eWenyi Wang        final long photoId = mContactData.getPhotoId();
50418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        final byte[] photoBytes = mContactData.getThumbnailPhotoBinaryData();
50518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (photoBytes != null) {
5065368489acaadac82706ca0e678ec04d662db702eWenyi Wang            ContactPhotoManager.getInstance(this).loadThumbnail(mSuggestionSummaryPhoto, photoId,
5075cf746f679b7866505f8b057565a648d65d1cad8Wenyi Wang                /* darkTheme */ false , /* isCircular */ true , request);
508e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        } else {
5095368489acaadac82706ca0e678ec04d662db702eWenyi Wang            ContactPhotoManager.DEFAULT_AVATAR.applyDefaultImage(mSuggestionSummaryPhoto,
5105368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    -1, false, request);
511e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        }
512e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
51318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        final String suggestionTitle = getResources().getQuantityString(
51418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                R.plurals.quickcontact_suggestion_card_title, suggestionNumber, suggestionNumber);
51518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardTitle.setText(suggestionTitle);
51618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardTitle.setText(suggestionTitle);
51718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
518e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        mSuggestionForName.setText(suggestionForName);
5195368489acaadac82706ca0e678ec04d662db702eWenyi Wang        final int linkedContactsNumber = mContactData.getRawContacts().size();
5205368489acaadac82706ca0e678ec04d662db702eWenyi Wang        final String contactsInfo;
52196efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        final String accountName = mContactData.getRawContacts().get(0).getAccountName();
52296efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        if (linkedContactsNumber == 1 && accountName == null) {
52396efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang            mSuggestionContactsNumber.setVisibility(View.INVISIBLE);
52496efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        }
52596efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        if (linkedContactsNumber == 1 && accountName != null) {
5265368489acaadac82706ca0e678ec04d662db702eWenyi Wang            contactsInfo = getResources().getString(R.string.contact_from_account_name,
52796efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang                    accountName);
5285368489acaadac82706ca0e678ec04d662db702eWenyi Wang        } else {
5295368489acaadac82706ca0e678ec04d662db702eWenyi Wang            contactsInfo = getResources().getString(
5305368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    R.string.quickcontact_contacts_number, linkedContactsNumber);
5315368489acaadac82706ca0e678ec04d662db702eWenyi Wang        }
5325368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionContactsNumber.setText(contactsInfo);
533e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
534e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        final Set<Long> suggestionContactIds = new HashSet<>();
535e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        for (Suggestion suggestion : mSuggestions) {
536e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            mSuggestionList.addView(inflateSuggestionListView(suggestion));
537e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            suggestionContactIds.add(suggestion.contactId);
538e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
539e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang
54018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (mIsSuggestionListCollapsed) {
54118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            collapseSuggestionList();
54218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        } else {
54318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            expandSuggestionList();
54418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        }
54518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
546e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        // Remove contact Ids that are not suggestions.
547e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        final Set<Long> selectedSuggestionIds = com.google.common.collect.Sets.intersection(
548e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang                mSelectedAggregationIds, suggestionContactIds);
549e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        mSelectedAggregationIds = new TreeSet<>(selectedSuggestionIds);
55096efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        if (!mSelectedAggregationIds.isEmpty()) {
55196efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang            enableLinkButton();
55296efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        }
553e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
554e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
555e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private void collapseSuggestionList() {
55618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardView.setVisibility(View.VISIBLE);
55718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardView.setVisibility(View.GONE);
558e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mIsSuggestionListCollapsed = true;
559e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
560e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
561e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private void expandSuggestionList() {
56218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardView.setVisibility(View.GONE);
56318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardView.setVisibility(View.VISIBLE);
564e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mIsSuggestionListCollapsed = false;
565e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
566e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
56718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private View inflateSuggestionListView(final Suggestion suggestion) {
568e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final LayoutInflater layoutInflater = LayoutInflater.from(this);
569e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final View suggestionView = layoutInflater.inflate(
570e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                R.layout.quickcontact_suggestion_contact_item, null);
571e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
5725368489acaadac82706ca0e678ec04d662db702eWenyi Wang        ContactPhotoManager.DefaultImageRequest
5735368489acaadac82706ca0e678ec04d662db702eWenyi Wang                request = new ContactPhotoManager.DefaultImageRequest(
5745368489acaadac82706ca0e678ec04d662db702eWenyi Wang                suggestion.name, suggestion.lookupKey, ContactPhotoManager.TYPE_DEFAULT, /*
5755368489acaadac82706ca0e678ec04d662db702eWenyi Wang                isCircular */ true);
576e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final ImageView photo = (ImageView) suggestionView.findViewById(
577e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                R.id.aggregation_suggestion_photo);
578e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        if (suggestion.photo != null) {
5795368489acaadac82706ca0e678ec04d662db702eWenyi Wang            ContactPhotoManager.getInstance(this).loadThumbnail(photo, suggestion.photoId,
5805cf746f679b7866505f8b057565a648d65d1cad8Wenyi Wang                   /* darkTheme */ false, /* isCircular */ true, request);
581e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        } else {
5825368489acaadac82706ca0e678ec04d662db702eWenyi Wang            ContactPhotoManager.DEFAULT_AVATAR.applyDefaultImage(photo, -1, false, request);
583e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
584e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
585e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final TextView name = (TextView) suggestionView.findViewById(R.id.aggregation_suggestion_name);
586e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        name.setText(suggestion.name);
587e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
58818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        final TextView accountNameView = (TextView) suggestionView.findViewById(
58918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                R.id.aggregation_suggestion_account_name);
59018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        final String accountName = suggestion.rawContacts.get(0).accountName;
59118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (!TextUtils.isEmpty(accountName)) {
5925368489acaadac82706ca0e678ec04d662db702eWenyi Wang            accountNameView.setText(
5935368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    getResources().getString(R.string.contact_from_account_name, accountName));
59418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        } else {
59596efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang            accountNameView.setVisibility(View.INVISIBLE);
596e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
597e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
598e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final CheckBox checkbox = (CheckBox) suggestionView.findViewById(R.id.suggestion_checkbox);
5998d02b165232cb219f082e634db45374aa43e3f6aWenyi Wang        final int[][] stateSet = new int[][] {
6008d02b165232cb219f082e634db45374aa43e3f6aWenyi Wang                new int[] { android.R.attr.state_checked },
6018d02b165232cb219f082e634db45374aa43e3f6aWenyi Wang                new int[] { -android.R.attr.state_checked }
6028d02b165232cb219f082e634db45374aa43e3f6aWenyi Wang        };
6038d02b165232cb219f082e634db45374aa43e3f6aWenyi Wang        final int[] colors = new int[] { mColorFilterColor, mColorFilterColor };
604051315fadbb8e0cc57ee34701a590d07575003bbguanxiongliu        if (suggestion != null && suggestion.name != null) {
605051315fadbb8e0cc57ee34701a590d07575003bbguanxiongliu            checkbox.setContentDescription(suggestion.name + " " +
606051315fadbb8e0cc57ee34701a590d07575003bbguanxiongliu                    getResources().getString(R.string.contact_from_account_name, accountName));
607051315fadbb8e0cc57ee34701a590d07575003bbguanxiongliu        }
6088d02b165232cb219f082e634db45374aa43e3f6aWenyi Wang        checkbox.setButtonTintList(new ColorStateList(stateSet, colors));
60918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        checkbox.setChecked(mSuggestionsShouldAutoSelected ||
61018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mSelectedAggregationIds.contains(suggestion.contactId));
61118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (checkbox.isChecked()) {
61218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mSelectedAggregationIds.add(suggestion.contactId);
61318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        }
614e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        checkbox.setTag(suggestion.contactId);
615e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        checkbox.setOnClickListener(new OnClickListener() {
616e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            @Override
617e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            public void onClick(View v) {
618e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                final CheckBox checkBox = (CheckBox) v;
619e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                final Long contactId = (Long) checkBox.getTag();
6205368489acaadac82706ca0e678ec04d662db702eWenyi Wang                if (mSelectedAggregationIds.contains(mContactData.getId())) {
6215368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    mSelectedAggregationIds.remove(mContactData.getId());
6225368489acaadac82706ca0e678ec04d662db702eWenyi Wang                }
623e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                if (checkBox.isChecked()) {
624e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                    mSelectedAggregationIds.add(contactId);
6255368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    if (mSelectedAggregationIds.size() >= 1) {
6265368489acaadac82706ca0e678ec04d662db702eWenyi Wang                        enableLinkButton();
6275368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    }
628e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                } else {
629e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                    mSelectedAggregationIds.remove(contactId);
63018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                    mSuggestionsShouldAutoSelected = false;
6315368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    if (mSelectedAggregationIds.isEmpty()) {
6325368489acaadac82706ca0e678ec04d662db702eWenyi Wang                        disableLinkButton();
6335368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    }
634e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                }
635e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            }
636e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        });
63718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
638e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        return suggestionView;
639e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
640e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
6415368489acaadac82706ca0e678ec04d662db702eWenyi Wang    private void enableLinkButton() {
6425368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.setClickable(true);
6438d02b165232cb219f082e634db45374aa43e3f6aWenyi Wang        mSuggestionsLinkButton.getBackground().setColorFilter(mColorFilter);
64456dfa580b5497f25169a4c875800ffff95cd31cdWenyi Wang        mSuggestionsLinkButton.setTextColor(
64523d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wang                ContextCompat.getColor(this, android.R.color.white));
6465368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.setOnClickListener(new OnClickListener() {
6475368489acaadac82706ca0e678ec04d662db702eWenyi Wang            @Override
6485368489acaadac82706ca0e678ec04d662db702eWenyi Wang            public void onClick(View view) {
6495368489acaadac82706ca0e678ec04d662db702eWenyi Wang                // Join selected contacts.
6505368489acaadac82706ca0e678ec04d662db702eWenyi Wang                if (!mSelectedAggregationIds.contains(mContactData.getId())) {
6515368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    mSelectedAggregationIds.add(mContactData.getId());
6525368489acaadac82706ca0e678ec04d662db702eWenyi Wang                }
6535368489acaadac82706ca0e678ec04d662db702eWenyi Wang                JoinContactsDialogFragment.start(
6545368489acaadac82706ca0e678ec04d662db702eWenyi Wang                        QuickContactActivity.this, mSelectedAggregationIds);
6555368489acaadac82706ca0e678ec04d662db702eWenyi Wang            }
6565368489acaadac82706ca0e678ec04d662db702eWenyi Wang        });
6575368489acaadac82706ca0e678ec04d662db702eWenyi Wang    }
6585368489acaadac82706ca0e678ec04d662db702eWenyi Wang
6595368489acaadac82706ca0e678ec04d662db702eWenyi Wang    @Override
6605368489acaadac82706ca0e678ec04d662db702eWenyi Wang    public void onContactsJoined() {
6615368489acaadac82706ca0e678ec04d662db702eWenyi Wang        disableLinkButton();
6625368489acaadac82706ca0e678ec04d662db702eWenyi Wang    }
6635368489acaadac82706ca0e678ec04d662db702eWenyi Wang
6645368489acaadac82706ca0e678ec04d662db702eWenyi Wang    private void disableLinkButton() {
6655368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.setClickable(false);
6665368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.getBackground().setColorFilter(
66723d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wang                ContextCompat.getColor(this, R.color.disabled_button_background),
66833f60d35a16407868de09ca898028e5eca6c6de3Wenyi Wang                PorterDuff.Mode.SRC_ATOP);
6695368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.setTextColor(
67023d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wang                ContextCompat.getColor(this, R.color.disabled_button_text));
6715368489acaadac82706ca0e678ec04d662db702eWenyi Wang    }
6725368489acaadac82706ca0e678ec04d662db702eWenyi Wang
67397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private interface ContextMenuIds {
67497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int COPY_TEXT = 0;
67597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int CLEAR_DEFAULT = 1;
67697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int SET_DEFAULT = 2;
67797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    }
67897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
6792a4207fb39330e840436215c896cde911489e111Paul Soulos    private final OnCreateContextMenuListener mEntryContextMenuListener =
6802a4207fb39330e840436215c896cde911489e111Paul Soulos            new OnCreateContextMenuListener() {
6812a4207fb39330e840436215c896cde911489e111Paul Soulos        @Override
6822a4207fb39330e840436215c896cde911489e111Paul Soulos        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
6832a4207fb39330e840436215c896cde911489e111Paul Soulos            if (menuInfo == null) {
6842a4207fb39330e840436215c896cde911489e111Paul Soulos                return;
6852a4207fb39330e840436215c896cde911489e111Paul Soulos            }
68697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            final EntryContextMenuInfo info = (EntryContextMenuInfo) menuInfo;
6872a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.setHeaderTitle(info.getCopyText());
68897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            menu.add(ContextMenu.NONE, ContextMenuIds.COPY_TEXT,
68997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    ContextMenu.NONE, getString(R.string.copy_text));
69097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
69197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Don't allow setting or clearing of defaults for non-editable contacts
69297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (!isContactEditable()) {
69397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return;
69497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
69597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
69697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            final String selectedMimeType = info.getMimeType();
69797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
69897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Defaults to true will only enable the detail to be copied to the clipboard.
69997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            boolean onlyOneOfMimeType = true;
70097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
70197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Only allow primary support for Phone and Email content types
70297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (Phone.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
70397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                onlyOneOfMimeType = mOnlyOnePhoneNumber;
70497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            } else if (Email.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
70597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                onlyOneOfMimeType = mOnlyOneEmail;
70697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
70797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
70897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Checking for previously set default
70997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (info.isSuperPrimary()) {
71097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                menu.add(ContextMenu.NONE, ContextMenuIds.CLEAR_DEFAULT,
71197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        ContextMenu.NONE, getString(R.string.clear_default));
71297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            } else if (!onlyOneOfMimeType) {
71397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                menu.add(ContextMenu.NONE, ContextMenuIds.SET_DEFAULT,
71497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        ContextMenu.NONE, getString(R.string.set_default));
71597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
7162a4207fb39330e840436215c896cde911489e111Paul Soulos        }
7172a4207fb39330e840436215c896cde911489e111Paul Soulos    };
7182a4207fb39330e840436215c896cde911489e111Paul Soulos
7192a4207fb39330e840436215c896cde911489e111Paul Soulos    @Override
7202a4207fb39330e840436215c896cde911489e111Paul Soulos    public boolean onContextItemSelected(MenuItem item) {
7212a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo menuInfo;
7222a4207fb39330e840436215c896cde911489e111Paul Soulos        try {
7232a4207fb39330e840436215c896cde911489e111Paul Soulos            menuInfo = (EntryContextMenuInfo) item.getMenuInfo();
7242a4207fb39330e840436215c896cde911489e111Paul Soulos        } catch (ClassCastException e) {
7252a4207fb39330e840436215c896cde911489e111Paul Soulos            Log.e(TAG, "bad menuInfo", e);
7262a4207fb39330e840436215c896cde911489e111Paul Soulos            return false;
7272a4207fb39330e840436215c896cde911489e111Paul Soulos        }
7282a4207fb39330e840436215c896cde911489e111Paul Soulos
72997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        switch (item.getItemId()) {
73097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.COPY_TEXT:
73197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                ClipboardUtils.copyText(this, menuInfo.getCopyLabel(), menuInfo.getCopyText(),
73297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        true);
73397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
73497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.SET_DEFAULT:
73597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                final Intent setIntent = ContactSaveService.createSetSuperPrimaryIntent(this,
73697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        menuInfo.getId());
73797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                this.startService(setIntent);
73897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
73997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.CLEAR_DEFAULT:
74097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                final Intent clearIntent = ContactSaveService.createClearPrimaryIntent(this,
74197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        menuInfo.getId());
74297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                this.startService(clearIntent);
74397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
74497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            default:
74597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                throw new IllegalArgumentException("Unknown menu option " + item.getItemId());
74697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
7472a4207fb39330e840436215c896cde911489e111Paul Soulos    }
7482a4207fb39330e840436215c896cde911489e111Paul Soulos
749d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
750d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
751d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
752d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
753d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
754d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
755d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
756d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
757d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
758d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
759d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
760d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
761d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
762d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
763d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
764d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
765d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
766d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
767d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
768d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
769d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
770d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
771d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
772d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
773d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
774d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
775d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
776d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
777d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
778d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
779b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
780b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
781b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
782b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
783f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            finish();
784b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
7858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
7878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
7888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
7898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
7908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
7928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
7938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
7948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
7958477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
7968477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
7978477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
7988477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
7998477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
8008477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
8018477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
802f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onEntranceAnimationDone() {
803f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            mIsEntranceAnimationFinished = true;
804f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        }
805f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
806f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        @Override
807f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onTransparentViewHeightChange(float ratio) {
808f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            if (mIsEntranceAnimationFinished) {
809f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                mWindowScrim.setAlpha((int) (0xFF * ratio));
8108477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
8118477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
812b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
813b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
814eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
815eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
816eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
817eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
818eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
819eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
820eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
821eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
822eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
823eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
824eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
825eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
826eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
827eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
828eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
829eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
830eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
831eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
832eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
833eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
834eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
835eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
836eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
837eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
838eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
839eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
840eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
841eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
842eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
843eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
844eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
845eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
846eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
847eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
848eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
849eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
850cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
851cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
85205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 1. Whether one of the mimetypes is the prioritized mimetype
85305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 2. Number of times used
85405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 3. Last time used
85505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 4. Statically defined
856cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
857eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
858eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
859eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
860eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
86105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final DataItem lhs = lhsList.get(0);
86205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final DataItem rhs = rhsList.get(0);
86305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final String lhsMimeType = lhs.getMimeType();
86405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final String rhsMimeType = rhs.getMimeType();
86505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell
86605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 1. Whether one of the mimetypes is the prioritized mimetype
86705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            if (!TextUtils.isEmpty(mExtraPrioritizedMimeType) && !lhsMimeType.equals(rhsMimeType)) {
86805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                if (rhsMimeType.equals(mExtraPrioritizedMimeType)) {
86905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    return 1;
87005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                }
87105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                if (lhsMimeType.equals(mExtraPrioritizedMimeType)) {
87205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    return -1;
87305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                }
87405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            }
87505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell
87605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 2. Number of times used
877eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
878eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
879eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
880eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
881eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
882eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
883eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
88405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 3. Last time used
885eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
886eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
887eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
888eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
889eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
890eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
891eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
892eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
893eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
894eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
895eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
89605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 4. Resort to a statically defined mimetype order.
89705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            if (!lhsMimeType.equals(rhsMimeType)) {
89805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                for (String mimeType : LEADING_MIMETYPES) {
89905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    if (lhsMimeType.equals(mimeType)) {
90005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                        return -1;
90105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    } else if (rhsMimeType.equals(mimeType)) {
90205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                        return 1;
90305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    }
904eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
905eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
906eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
907eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
908eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
909eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
910edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
911f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    public boolean dispatchTouchEvent(MotionEvent ev) {
912f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
913f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen            TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
914f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        }
915f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        return super.dispatchTouchEvent(ev);
916f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    }
917f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen
918f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    @Override
9198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
9208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
9218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
922edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
923f62363701d76b1283edeb7305fc19249fc34899cWalter Jang        if (RequestPermissionsActivity.startPermissionActivity(this) ||
924f62363701d76b1283edeb7305fc19249fc34899cWalter Jang                RequestDesiredPermissionsActivity.startPermissionActivity(this)) {
925bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell            return;
926bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell        }
927bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell
92862a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jang        final int previousScreenType = getIntent().getExtras()
92962a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jang                .getInt(EXTRA_PREVIOUS_SCREEN_TYPE, ScreenType.UNKNOWN);
93062a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jang        Logger.logScreenView(this, ScreenType.QUICK_CONTACT, previousScreenType);
93162a2074bb3b8dc66996e9fc6a8748b999ab2b51cWalter Jang
9324c3d3e24b0de8b4170106e312f6a4ac8139e1242Wenyi Wang        if (CompatUtils.isLollipopCompatible()) {
9334c3d3e24b0de8b4170106e312f6a4ac8139e1242Wenyi Wang            getWindow().setStatusBarColor(Color.TRANSPARENT);
9344c3d3e24b0de8b4170106e312f6a4ac8139e1242Wenyi Wang        }
9352426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
936d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
937dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
93810d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
93910d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
94010d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
94110d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
942edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
943edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
944a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
945a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
9460cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
9470cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
948eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
9496095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
950b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
951eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
952edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
95318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardView = (CardView) findViewById(R.id.collapsed_suggestion_card);
95418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardView = (CardView) findViewById(R.id.expand_suggestion_card);
95518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapasedSuggestionHeader = findViewById(R.id.collapsed_suggestion_header);
95618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardTitle = (TextView) findViewById(
95718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                R.id.collapsed_suggestion_card_title);
95818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardTitle = (TextView) findViewById(R.id.expand_suggestion_card_title);
959e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestionSummaryPhoto = (ImageView) findViewById(R.id.suggestion_icon);
960e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestionForName = (TextView) findViewById(R.id.suggestion_for_name);
9615368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionContactsNumber = (TextView) findViewById(R.id.suggestion_for_contacts_number);
962e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestionList = (LinearLayout) findViewById(R.id.suggestion_list);
96318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mSuggestionsCancelButton= (Button) findViewById(R.id.cancel_button);
9645585c6ef5e8f075bdc3d514887322c6fa683f4cbTingting Wang        mSuggestionsLinkButton = (Button) findViewById(R.id.link_button);
965e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        if (savedInstanceState != null) {
966e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mIsSuggestionListCollapsed = savedInstanceState.getBoolean(
967e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                    KEY_IS_SUGGESTION_LIST_COLLAPSED, true);
96818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mPreviousContactId = savedInstanceState.getLong(KEY_PREVIOUS_CONTACT_ID);
96918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mSuggestionsShouldAutoSelected = savedInstanceState.getBoolean(
97018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                    KEY_SUGGESTIONS_AUTO_SELECTED, true);
971e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mSelectedAggregationIds = (TreeSet<Long>)
972e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                    savedInstanceState.getSerializable(KEY_SELECTED_SUGGESTION_CONTACTS);
973e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        } else {
974e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mIsSuggestionListCollapsed = true;
975e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mSelectedAggregationIds.clear();
9765368489acaadac82706ca0e678ec04d662db702eWenyi Wang        }
9772876977d8df4e61004b103a8c12f7443ab297711Walter Jang        if (mSelectedAggregationIds.isEmpty()) {
9785368489acaadac82706ca0e678ec04d662db702eWenyi Wang            disableLinkButton();
9792876977d8df4e61004b103a8c12f7443ab297711Walter Jang        } else {
9802876977d8df4e61004b103a8c12f7443ab297711Walter Jang            enableLinkButton();
981e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
98218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapasedSuggestionHeader.setOnClickListener(new OnClickListener() {
98318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            @Override
98418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            public void onClick(View view) {
98518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mCollapsedSuggestionCardView.setVisibility(View.GONE);
98618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mExpandSuggestionCardView.setVisibility(View.VISIBLE);
98718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mIsSuggestionListCollapsed = false;
988bcae18d136522e190d5074909e5b7148c00f0db8guanxiongliu                mExpandSuggestionCardTitle.requestFocus();
989bcae18d136522e190d5074909e5b7148c00f0db8guanxiongliu                mExpandSuggestionCardTitle.sendAccessibilityEvent(
990bcae18d136522e190d5074909e5b7148c00f0db8guanxiongliu                        AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
99118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            }
99218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        });
99318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
99418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mSuggestionsCancelButton.setOnClickListener(new OnClickListener() {
99518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            @Override
99618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            public void onClick(View view) {
99718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mCollapsedSuggestionCardView.setVisibility(View.VISIBLE);
99818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mExpandSuggestionCardView.setVisibility(View.GONE);
99918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mIsSuggestionListCollapsed = true;
100018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            }
100118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        });
100218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
10036095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
1004eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
1005eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
100623889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
10072a4207fb39330e840436215c896cde911489e111Paul Soulos        mContactCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
1008b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1009b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
1010b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
1011edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1012eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
10132a4207fb39330e840436215c896cde911489e111Paul Soulos        mAboutCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
1014eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
101502eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView = (QuickContactImageView) findViewById(R.id.photo);
101663176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        final View transparentView = findViewById(R.id.transparent_view);
10170d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
101863176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell            transparentView.setOnClickListener(new OnClickListener() {
10190d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
10200d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
10210d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
10220d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
10230d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
10240d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
1025edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1026d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        // Allow a shadow to be shown under the toolbar.
1027d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
1028d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell
1029d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
1030d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
10319b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
10329b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
10339b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
10349b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
10356219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
10368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
1037f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = mHasAlreadyBeenOpened;
10389b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
1039f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mWindowScrim.setAlpha(0);
10409b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
1041edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1042c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
1043ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
1044ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
1045ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
10468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
10479b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
10489b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1049d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
1050d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
1051d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
1052d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
1053d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
1054d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
1055d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
1056d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
1057d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
105823889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
1059f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ true,
1060f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                new Runnable() {
1061f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    @Override
1062f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    public void run() {
1063f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        if (!mHasAlreadyBeenOpened) {
1064f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // The initial scrim opacity must match the scrim opacity that would be
1065f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // achieved by scrolling to the starting position.
1066f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final float alphaRatio = mExtraMode == MODE_FULLY_EXPANDED ?
1067f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    1 : mScroller.getStartingTransparentHeightRatio();
1068f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int duration = getResources().getInteger(
1069f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    android.R.integer.config_shortAnimTime);
1070f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int desiredAlpha = (int) (0xFF * alphaRatio);
1071f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            ObjectAnimator o = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0,
1072f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    desiredAlpha).setDuration(duration);
1073f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
1074f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            o.start();
1075f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        }
1076f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    }
1077f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                });
1078f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
10799b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
10809b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
1081ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
1082ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
1083ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
1084ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
1085ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
1086ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
1087ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
1088ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
1089ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
1090ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
1091ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
1092ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
1093ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
1094ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
1095a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
1096a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
10979b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
1098ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
1099ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
11009b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
11019b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
11028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
11038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
11048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1105405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
1106405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1107d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wang        final boolean deletedOrSplit = requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
1108d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wang                (resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED ||
1109d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wang                resultCode == ContactEditorBaseActivity.RESULT_CODE_SPLIT);
1110d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wang        if (deletedOrSplit) {
11118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
1112ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos        } else if (requestCode == REQUEST_CODE_CONTACT_SELECTION_ACTIVITY &&
1113ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                resultCode != RESULT_CANCELED) {
1114ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos            processIntent(data);
1115b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
11168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
1117dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
11188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
11198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
11208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
11218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
1122f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = true;
11238571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
1124d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
1125d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1126d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
11279b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
11289b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
11299b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
11309b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
11318e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilterColor);
11329b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
1133e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        savedInstanceState.putBoolean(KEY_IS_SUGGESTION_LIST_COLLAPSED, mIsSuggestionListCollapsed);
113418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        savedInstanceState.putLong(KEY_PREVIOUS_CONTACT_ID, mPreviousContactId);
113518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        savedInstanceState.putBoolean(
113618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                KEY_SUGGESTIONS_AUTO_SELECTED, mSuggestionsShouldAutoSelected);
1137e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        savedInstanceState.putSerializable(
1138e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                KEY_SELECTED_SUGGESTION_CONTACTS, mSelectedAggregationIds);
11399b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
11409b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1141d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
1142f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos        if (intent == null) {
1143f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos            finish();
1144f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos            return;
1145f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos        }
1146d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
1147d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1148d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
1149d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
1150d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
1151d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
1152d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
1153d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
115405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE, QuickContact.MODE_LARGE);
115505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell        mExtraPrioritizedMimeType = getIntent().getStringExtra(QuickContact.EXTRA_PRIORITIZED_MIMETYPE);
1156d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
1157d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1158fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        if (lookupUri == null) {
1159fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner            finish();
1160fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner            return;
1161fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        }
1162fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        mLookupUri = lookupUri;
1163d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
1164d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
1165d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
1166d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
1167d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
1168d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
1169144a4d16daf88dded258063bc33810b20c57f81dWenyi Wang            // we need to reload the new contact.
117059a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            destroyInteractionLoaders();
1171144a4d16daf88dded258063bc33810b20c57f81dWenyi Wang            mContactLoader = (ContactLoader) (Loader<?>) getLoaderManager().getLoader(
1172144a4d16daf88dded258063bc33810b20c57f81dWenyi Wang                    LOADER_CONTACT_ID);
117359a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            mCachedCp2DataCardModel = null;
1174d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
1175144a4d16daf88dded258063bc33810b20c57f81dWenyi Wang        mContactLoader.forceLoad();
11762d150da246632b1649999cfabed776133b097775Brian Attwell
11772d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
11782426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
1179edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
118059a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell    private void destroyInteractionLoaders() {
118159a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        for (int interactionLoaderId : mRecentLoaderIds) {
118259a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            getLoaderManager().destroyLoader(interactionLoaderId);
118359a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        }
118459a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell    }
118559a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell
1186b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
11878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
11888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
11898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
11908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
1191c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
1192b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
1193b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
119481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
1195d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
11969b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
11979f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString(),
11989f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang                    /* isPhoneNumber= */ false);
11999b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
120081281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
120181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
120281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
12039f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang    private void setHeaderNameText(String value, boolean isPhoneNumber) {
1204d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
12059b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
12069f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang                mScroller.setTitle(value, isPhoneNumber);
12079b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
120881281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
120981281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
121081281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
121181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
1212edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
1213edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
1214edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
1215edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
1216edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
1217edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
1218edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
1219edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
1220edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1221edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1222edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
1223edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1224edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1225edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
1226cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
1227edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
12288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
12298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
12306cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
1231d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
1232edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
12338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
12348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
1235dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
123602eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView.setIsBusiness(mContactData.isDisplayNameFromOrganization());
12376219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
123831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
1239df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang        final String displayName = ContactDisplayUtils.getDisplayName(this, data).toString();
12409f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang        setHeaderNameText(
12419f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang                displayName, mContactData.getDisplayNameSource() == DisplayNameSources.PHONE);
1242df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang        final String phoneticName = ContactDisplayUtils.getPhoneticName(this, data);
1243df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang        if (mScroller != null) {
1244df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang            if (mContactData.getDisplayNameSource() != DisplayNameSources.STRUCTURED_PHONETIC_NAME
1245df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang                    && !TextUtils.isEmpty(phoneticName)) {
1246df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang                mScroller.setPhoneticName(phoneticName);
1247df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang            } else {
1248df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang                mScroller.setPhoneticNameGone();
1249df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang            }
1250d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang        }
1251edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
12528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1253dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
12546bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mEntriesAndActionsTask = new AsyncTask<Void, Void, Cp2DataCardModel>() {
12558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
12568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
12576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected Cp2DataCardModel doInBackground(
1258eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
1259eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
12608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
12618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
12628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
12636bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected void onPostExecute(Cp2DataCardModel cardDataModel) {
12646bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                super.onPostExecute(cardDataModel);
12658a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
12668a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
12678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
12688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
12696bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    bindDataToCards(cardDataModel);
12708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
12718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
12728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
12738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
12748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
12758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
12768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
12776bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void bindDataToCards(Cp2DataCardModel cp2DataCardModel) {
12786bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        startInteractionLoaders(cp2DataCardModel);
12796bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        populateContactAndAboutCard(cp2DataCardModel);
1280e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        populateSuggestionCard();
1281eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1282eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
12836bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void startInteractionLoaders(Cp2DataCardModel cp2DataCardModel) {
12846bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Map<String, List<DataItem>> dataItemsMap = cp2DataCardModel.dataItemsMap;
12856bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> phoneDataItems = dataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
128697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        if (phoneDataItems != null && phoneDataItems.size() == 1) {
128797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            mOnlyOnePhoneNumber = true;
128897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
1289eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
1290eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
1291eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
1292eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
1293eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
1294eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1295eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1296ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
1297eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
1298eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1299eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
13008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
13018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
1302ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
1303ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
1304ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
1305ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
1306ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
1307ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
1308ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
1309ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
13108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
1311899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
13128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1313eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1314899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
13156bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> emailDataItems = dataItemsMap.get(Email.CONTENT_ITEM_TYPE);
131697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        if (emailDataItems != null && emailDataItems.size() == 1) {
131797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            mOnlyOneEmail = true;
131897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
1319eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
1320eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
1321eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
1322eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
1323eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
1324eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1325eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1326ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
1327eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
1328899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
1329899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
1330ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
1331899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
13328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
13338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
13348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
13358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
13368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
13378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
13388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
13398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
13408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
13418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
13428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
13438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
13448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
13458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
13468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
13478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
13486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<List<Entry>> buildAboutCardEntries(Map<String, List<DataItem>> dataItemsMap) {
1349a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
1350a153dba33c2152332c5706178a367f52b0550723Paul Soulos        for (String mimetype : SORTED_ABOUT_CARD_MIMETYPES) {
13516bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> mimeTypeItems = dataItemsMap.get(mimetype);
1352a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (mimeTypeItems == null) {
1353a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
1354a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
13556bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // Set aboutCardTitleOut = null, since SORTED_ABOUT_CARD_MIMETYPES doesn't contain
13566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // the name mimetype.
13576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<Entry> aboutEntries = dataItemsToEntries(mimeTypeItems,
13586bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    /* aboutCardTitleOut = */ null);
1359a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutEntries.size() > 0) {
1360a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(aboutEntries);
1361a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
1362a153dba33c2152332c5706178a367f52b0550723Paul Soulos        }
1363a153dba33c2152332c5706178a367f52b0550723Paul Soulos        return aboutCardEntries;
1364a153dba33c2152332c5706178a367f52b0550723Paul Soulos    }
1365a153dba33c2152332c5706178a367f52b0550723Paul Soulos
1366a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    @Override
1367a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    protected void onResume() {
1368a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        super.onResume();
1369a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        // If returning from a launched activity, repopulate the contact and about card
1370a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        if (mHasIntentLaunched) {
1371a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = false;
13726bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            populateContactAndAboutCard(mCachedCp2DataCardModel);
1373a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
137459a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell
137559a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // When exiting the activity and resuming, we want to force a full reload of all the
137659a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // interaction data in case something changed in the background. On screen rotation,
137759a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // we don't need to do this. And, mCachedCp2DataCardModel will be null, so we won't.
137859a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        if (mCachedCp2DataCardModel != null) {
137959a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            destroyInteractionLoaders();
138059a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            startInteractionLoaders(mCachedCp2DataCardModel);
138159a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        }
1382a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    }
1383a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
1384e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private void populateSuggestionCard() {
1385e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        // Initialize suggestion related view and data.
138618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (mPreviousContactId != mContactData.getId()) {
138718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mCollapsedSuggestionCardView.setVisibility(View.GONE);
138818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mExpandSuggestionCardView.setVisibility(View.GONE);
138918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mIsSuggestionListCollapsed = true;
139042a08d901e84ab4a7f3a8c4035f70dda1f4dd20aTingting Wang            mSuggestionsShouldAutoSelected = true;
13910fafb0e2813a28b31a71e908d30e18a6edaa3d95Tingting Wang            mSuggestionList.removeAllViews();
13920fafb0e2813a28b31a71e908d30e18a6edaa3d95Tingting Wang        }
1393e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
13943d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang        // Do not show the card when it's directory contact or invisible.
13953d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang        if (DirectoryContactUtil.isDirectoryContact(mContactData)
13963d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang                || InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
13973d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang            return;
13983d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang        }
13993d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang
1400e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        if (mAggregationSuggestionEngine == null) {
1401e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mAggregationSuggestionEngine = new AggregationSuggestionEngine(this);
1402e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mAggregationSuggestionEngine.setListener(this);
1403e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            mAggregationSuggestionEngine.setSuggestionsLimit(getResources().getInteger(
1404e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang                    R.integer.quickcontact_suggestions_limit));
1405e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mAggregationSuggestionEngine.start();
1406e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
1407e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
1408e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mAggregationSuggestionEngine.setContactId(mContactData.getId());
140918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (mPreviousContactId != 0
141018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                && mPreviousContactId != mContactData.getId()) {
1411e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            // Clear selected Ids when listing suggestions for new contact Id.
1412e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            mSelectedAggregationIds.clear();
1413e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        }
141418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mPreviousContactId = mContactData.getId();
1415e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
1416e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        // Trigger suggestion engine to compute suggestions.
1417a1af0c833e7b4113126131e9949c3d4c9579e5dfTingting Wang        if (mContactData.getId() <= 0) {
1418a1af0c833e7b4113126131e9949c3d4c9579e5dfTingting Wang            return;
1419a1af0c833e7b4113126131e9949c3d4c9579e5dfTingting Wang        }
1420e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final ContentValues values = new ContentValues();
1421e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
1422e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                mContactData.getDisplayName());
1423e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME,
1424e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                mContactData.getPhoneticName());
1425e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mAggregationSuggestionEngine.onNameChange(ValuesDelta.fromBefore(values));
1426e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
1427e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
14286bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void populateContactAndAboutCard(Cp2DataCardModel cp2DataCardModel) {
14296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mCachedCp2DataCardModel = cp2DataCardModel;
14306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (mHasIntentLaunched || cp2DataCardModel == null) {
1431a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            return;
1432a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
1433eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
1434eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
14356bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = cp2DataCardModel.contactCardEntries;
14366bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = cp2DataCardModel.aboutCardEntries;
14376bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final String customAboutCardName = cp2DataCardModel.customAboutCardName;
1438eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1439eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
144005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final boolean firstEntriesArePrioritizedMimeType =
144105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    !TextUtils.isEmpty(mExtraPrioritizedMimeType) &&
144205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mCachedCp2DataCardModel.dataItemsMap.containsKey(mExtraPrioritizedMimeType) &&
144305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mCachedCp2DataCardModel.dataItemsMap.get(mExtraPrioritizedMimeType).size() != 0;
1444eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
1445eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
1446c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mContactCard.isExpanded(),
144789966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                    /* isAlwaysExpanded = */ false,
14480cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener,
144905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mScroller,
145005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    firstEntriesArePrioritizedMimeType);
1451eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
1452eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1453eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
1454eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1455eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
14568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1457eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
1458c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        // Phonetic name is not a data item, so the entry needs to be created separately
1459c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        final String phoneticName = mContactData.getPhoneticName();
1460c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        if (!TextUtils.isEmpty(phoneticName)) {
1461c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            Entry phoneticEntry = new Entry(/* viewId = */ -1,
1462c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* icon = */ null,
1463c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    getResources().getString(R.string.name_phonetic),
1464c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    phoneticName,
14658a3fe52802135be2402a5b216325615fb796a509Walter Jang                    /* subHeaderIcon = */ null,
1466c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* text = */ null,
14678a3fe52802135be2402a5b216325615fb796a509Walter Jang                    /* textIcon = */ null,
146823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    /* primaryContentDescription = */ null,
1469c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* intent = */ null,
1470c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIcon = */ null,
1471c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIntent = */ null,
1472c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateContentDescription = */ null,
1473c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* shouldApplyColor = */ false,
14742a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
14752a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ new EntryContextMenuInfo(phoneticName,
147697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                            getResources().getString(R.string.name_phonetic),
147797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                            /* mimeType = */ null, /* id = */ -1, /* isPrimary = */ false),
147848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
147948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
148048290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* thirdContentDescription = */ null,
14815f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* thirdAction = */ Entry.ACTION_NONE,
14825f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* thirdExtras = */ null,
14835f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* iconResourceId = */  0);
1484c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            List<Entry> phoneticList = new ArrayList<>();
1485c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            phoneticList.add(phoneticEntry);
1486a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name comes after nickname. Check to see if the first entry type is nickname
1487a0fa4c18dc111ceea9ff47f35ee01fecc1003578Paul Soulos            if (aboutCardEntries.size() > 0 && aboutCardEntries.get(0).get(0).getHeader().equals(
1488a153dba33c2152332c5706178a367f52b0550723Paul Soulos                    getResources().getString(R.string.header_nickname_entry))) {
1489a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(1, phoneticList);
1490a153dba33c2152332c5706178a367f52b0550723Paul Soulos            } else {
1491a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(0, phoneticList);
1492a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
1493c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        }
1494c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos
14956bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (!TextUtils.isEmpty(customAboutCardName)) {
14966bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            mAboutCard.setTitle(customAboutCardName);
14976bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
14986bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
14993b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox        mAboutCard.initialize(aboutCardEntries,
15003b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* numInitialVisibleEntries = */ 1,
15013b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* isExpanded = */ true,
15023b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* isAlwaysExpanded = */ true,
15033b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                mExpandingEntryCardViewListener,
15043b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                mScroller);
15056095369885edcca566a812b551886e29c7ff8039Brian Attwell
15066095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
15076095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
15086095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
15096095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
15106095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
15116095369885edcca566a812b551886e29c7ff8039Brian Attwell
1512a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
1513a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
1514a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
1515a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1516a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
1517eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1518eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1519eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1520eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
15216095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
15226095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
15236095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
15246095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
15256095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
15266095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
15276095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
15288a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* subHeader = */ null, /* subHeaderIcon = */ null, /* text = */ null,
15298a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* textIcon = */ null, /* primaryContentDescription = */ null,
153023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                getEditContactIntent(),
1531dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
1532714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
153348fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* isEditable = */ false, /* EntryContextMenuInfo = */ null,
153448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* thirdIcon = */ null, /* thirdIntent = */ null,
15355f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                /* thirdContentDescription = */ null,
15365f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                /* thirdAction = */ Entry.ACTION_NONE,
15375f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                /* thirdExtras = */ null,
15385f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                R.drawable.ic_phone_24dp);
15396095369885edcca566a812b551886e29c7ff8039Brian Attwell
15406095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
15416095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
15426095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
15436095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
15448a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* subHeaderIcon = */ null,
15458a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* text = */ null, /* textIcon = */ null, /* primaryContentDescription = */ null,
154623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                getEditContactIntent(), /* alternateIcon = */ null,
1547dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
15482a4207fb39330e840436215c896cde911489e111Paul Soulos                /* shouldApplyColor = */ true, /* isEditable = */ false,
154948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* EntryContextMenuInfo = */ null, /* thirdIcon = */ null,
155048290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                /* thirdIntent = */ null, /* thirdContentDescription = */ null,
15515f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                /* thirdAction = */ Entry.ACTION_NONE, /* thirdExtras = */ null,
155248290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                R.drawable.ic_email_24dp);
15536095369885edcca566a812b551886e29c7ff8039Brian Attwell
15546095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
15556095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
15566095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
15576095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
15586095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
15596095369885edcca566a812b551886e29c7ff8039Brian Attwell
15606095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
15616095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
15626095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
15636095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
1564c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
15650cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener, mScroller);
15666095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
15676095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
15686095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
15696095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
15706095369885edcca566a812b551886e29c7ff8039Brian Attwell
15716095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
1572eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
1573eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
1574eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
1575eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
1576eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
1577eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
15786bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel generateDataModelFromContact(
1579eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
1580eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
1581eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1582eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
15838bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
15848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
1585851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
1586851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
1587eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
1588eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1589851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
1590eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
1591eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
159247b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
159347b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
159447b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
1595eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
1596cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1597eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
1598b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1599eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
1600eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
1601899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
1602eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
1603edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1604eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
1605eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
1606eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
1607eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
1608edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
1609eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
1610edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1611edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
16128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1613edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1614eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
161516339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
161616339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
1617eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
1618eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
1619eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
162016339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
1621eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
1622eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
1623eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
1624eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
1625eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
1626eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
1627eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
1628eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
1629edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1630eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1631edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1632eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
1633eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
1634eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
1635eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
163616339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
16376bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.beginSection("cp2 data items to entries");
16386bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16396bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = new ArrayList<>();
16406bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = buildAboutCardEntries(dataItemsMap);
16416bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final MutableString aboutCardName = new MutableString();
16426bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16436bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        for (int i = 0; i < dataItemsList.size(); ++i) {
16446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> dataItemsByMimeType = dataItemsList.get(i);
16456bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final DataItem topDataItem = dataItemsByMimeType.get(0);
16466bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
16476bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                // About card mimetypes are built in buildAboutCardEntries, skip here
16486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                continue;
16496bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            } else {
16506bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                List<Entry> contactEntries = dataItemsToEntries(dataItemsList.get(i),
16516bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        aboutCardName);
16526bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (contactEntries.size() > 0) {
16536bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    contactCardEntries.add(contactEntries);
16546bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                }
16556bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            }
16566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
16576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16586bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.endSection();
16596bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16606bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Cp2DataCardModel dataModel = new Cp2DataCardModel();
16616bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.customAboutCardName = aboutCardName.value;
16626bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.aboutCardEntries = aboutCardEntries;
16636bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.contactCardEntries = contactCardEntries;
16646bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.dataItemsMap = dataItemsMap;
16656bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return dataModel;
16666bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
16676bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16686bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
16696bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * Class used to hold the About card and Contact cards' data model that gets generated
16706bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * on a background thread. All data is from CP2.
16716bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
16726bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class Cp2DataCardModel {
16736bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        /**
16746bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * A map between a mimetype string and the corresponding list of data items. The data items
16756bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * are in sorted order using mWithinMimeTypeDataItemComparator.
16766bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         */
16776bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public Map<String, List<DataItem>> dataItemsMap;
16786bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> aboutCardEntries;
16796bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> contactCardEntries;
16806bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String customAboutCardName;
16816bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
16826bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16836bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class MutableString {
16846bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String value;
1685eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1686edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1687eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
1688eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
1689eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
16906bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
16916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * This runs on a background thread. This is set as static to avoid accidentally adding
16926bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * additional dependencies on unsafe things (like the Activity).
16936bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
1694eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
16956a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * @param secondDataItem A second {@link DataItem} to help build a full entry for some
16966a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     *  mimetypes
1697eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
1698eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
16996a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static Entry dataItemToEntry(DataItem dataItem, DataItem secondDataItem,
17006bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            Context context, Contact contactData,
17016bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final MutableString aboutCardName) {
1702eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
1703eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
1704eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
1705eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
1706eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
1707eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
170823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        StringBuilder primaryContentDescription = new StringBuilder();
1709b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang        Spannable phoneContentDescription = null;
17105da55ff837f57a63b409cd495cc25f61ed6fd709Wenyi Wang        Spannable smsContentDescription = null;
1711eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
171248ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
1713dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
1714dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
171523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        StringBuilder alternateContentDescription = new StringBuilder();
1716eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
17172a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo entryContextMenuInfo = null;
171848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Drawable thirdIcon = null;
171948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Intent thirdIntent = null;
17205f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn        int thirdAction = Entry.ACTION_NONE;
172148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        String thirdContentDescription = null;
17225f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn        Bundle thirdExtras = null;
172348290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos        int iconResourceId = 0;
1724eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
17256bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        context = context.getApplicationContext();
172623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        final Resources res = context.getResources();
1727eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
1728eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1729eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
1730eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
17316bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            intent = ContactsUtils.buildImIntent(context, im).first;
1732eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
17337de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            final int protocol;
17347de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (!im.isProtocolValid()) {
17357de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = Im.PROTOCOL_CUSTOM;
17367de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
17377de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
17387de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
17397de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (protocol == Im.PROTOCOL_CUSTOM) {
17407de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // If the protocol is custom, display the "IM" entry header as well to distinguish
17417de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // this entry from other ones
174223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = res.getString(R.string.header_im_entry);
174323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                subHeader = Im.getProtocolLabel(res, protocol,
17447de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
17457de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                text = im.getData();
17467de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
174723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = Im.getProtocolLabel(res, protocol,
17487de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
17497de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = im.getData();
17507de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
175197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(im.getData(), header,
175297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1753eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
1754eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
175523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_organization_entry);
1756eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
175797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
175897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1759eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
1760eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
1761eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
1762eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
1763eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
17646bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                (contactData.getNameRawContactId() == dataItem.getRawContactId());
1765eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1766eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
1767eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
17686bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                && contactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
1769eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1770eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
177123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = res.getString(R.string.header_nickname_entry);
1772eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
177397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
177497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1775eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1776eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
1777eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
177823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_note_entry);
1779eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
178097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
178197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1782eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
1783eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
178423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_website_entry);
1785eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
178697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
178797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1788eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
17893bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                final WebAddress webAddress = new WebAddress(website.buildDataStringForDisplay
17903bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                        (context, kind));
1791eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1792eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
17933bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                Log.e(TAG, "Couldn't parse website: " + website.buildDataStringForDisplay(
17943bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                        context, kind));
1795eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1796eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1797eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
17983bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell            final String dataString = event.buildDataStringForDisplay(context, kind);
1799eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1800eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1801eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1802eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1803eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1804eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1805eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1806eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1807eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
180823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_event_entry);
1809f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
1810c85af2872f231f48b586c30f6306083a039dd6c6Wenyi Wang                subHeader = EventCompat.getTypeLabel(res, event.getKindTypeColumn(kind),
18117de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        event.getLabel()).toString();
1812f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
18136bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            text = DateUtils.formatDate(context, dataString);
181497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(text, header,
181597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1816eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1817eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
18183bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell            final String dataString = relation.buildDataStringForDisplay(context, kind);
1819eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1820eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1821eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1822eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1823eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
182423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_relation_entry);
1825eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
182697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
182797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1828f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
182923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                text = Relation.getTypeLabel(res,
18306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        relation.getKindTypeColumn(kind),
1831f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1832f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1833eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1834eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
18355f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn            String phoneLabel = null;
1836eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
183723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.call_other)).append(" ");
18383bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                header = sBidiFormatter.unicodeWrap(phone.buildDataStringForDisplay(context, kind),
1839c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell                        TextDirectionHeuristics.LTR);
18402a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
184197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.phoneLabelsGroup), dataItem.getMimeType(),
184297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1843f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
184401b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                    final int kindTypeColumn = phone.getKindTypeColumn(kind);
184501b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                    final String label = phone.getLabel();
18465f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    phoneLabel = label;
184701b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                    if (kindTypeColumn == Phone.TYPE_CUSTOM && TextUtils.isEmpty(label)) {
184801b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                        text = "";
184901b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                    } else {
185001b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                        text = Phone.getTypeLabel(res, kindTypeColumn, label).toString();
18515f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                        phoneLabel= text;
18523df785b0ad0c0292adfc03a4f804d1b38053de63Tingting Wang                        primaryContentDescription.append(text).append(" ");
18533df785b0ad0c0292adfc03a4f804d1b38053de63Tingting Wang                    }
1854f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
185523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
1856b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                phoneContentDescription = com.android.contacts.common.util.ContactDisplayUtils
1857b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                        .getTelephoneTtsSpannable(primaryContentDescription.toString(), header);
185823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_phone_24dp);
185948290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_phone_24dp;
18606bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (PhoneCapabilityTester.isPhone(context)) {
1861eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
186216339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1863dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
18641cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                        Uri.fromParts(ContactsUtils.SCHEME_SMSTO, phone.getNumber(), null));
186548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
186623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateIcon = res.getDrawable(R.drawable.ic_message_24dp);
186723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(res.getString(R.string.sms_custom, header));
18685da55ff837f57a63b409cd495cc25f61ed6fd709Wenyi Wang                smsContentDescription = com.android.contacts.common.util.ContactDisplayUtils
18695da55ff837f57a63b409cd495cc25f61ed6fd709Wenyi Wang                        .getTelephoneTtsSpannable(alternateContentDescription.toString(), header);
187048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
18710a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                int videoCapability = CallUtil.getVideoCallingAvailability(context);
18720a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                boolean isPresenceEnabled =
18730a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                        (videoCapability & CallUtil.VIDEO_CALLING_PRESENCE) != 0;
18740a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                boolean isVideoEnabled = (videoCapability & CallUtil.VIDEO_CALLING_ENABLED) != 0;
18750a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn
18765f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                if (CallUtil.isCallWithSubjectSupported(context)) {
18775f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdIcon = res.getDrawable(R.drawable.ic_call_note_white_24dp);
18785f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdAction = Entry.ACTION_CALL_WITH_SUBJECT;
18795f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdContentDescription =
18805f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            res.getString(R.string.call_with_a_note);
18815f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    // Create a bundle containing the data the call subject dialog requires.
18825f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras = new Bundle();
18835f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putLong(CallSubjectDialog.ARG_PHOTO_ID,
18845f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            contactData.getPhotoId());
18855f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putParcelable(CallSubjectDialog.ARG_PHOTO_URI,
18865f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            UriUtils.parseUriOrNull(contactData.getPhotoUri()));
18875f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putParcelable(CallSubjectDialog.ARG_CONTACT_URI,
18885f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            contactData.getLookupUri());
18895f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putString(CallSubjectDialog.ARG_NAME_OR_NUMBER,
18905f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            contactData.getDisplayName());
18915f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putBoolean(CallSubjectDialog.ARG_IS_BUSINESS, false);
18925f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putString(CallSubjectDialog.ARG_NUMBER,
18935f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            phone.getNumber());
18945f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putString(CallSubjectDialog.ARG_DISPLAY_NUMBER,
18955f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            phone.getFormattedPhoneNumber());
18965f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putString(CallSubjectDialog.ARG_NUMBER_LABEL,
18975f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            phoneLabel);
18980a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                } else if (isVideoEnabled) {
18990a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                    // Check to ensure carrier presence indicates the number supports video calling.
19000a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                    int carrierPresence = dataItem.getCarrierPresence();
19010a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                    boolean isPresent = (carrierPresence & Phone.CARRIER_PRESENCE_VT_CAPABLE) != 0;
19020a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn
19030a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                    if ((isPresenceEnabled && isPresent) || !isPresenceEnabled) {
19040a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                        thirdIcon = res.getDrawable(R.drawable.ic_videocam);
19050a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                        thirdAction = Entry.ACTION_INTENT;
19060a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                        thirdIntent = CallUtil.getVideoCallIntent(phone.getNumber(),
19070a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                                CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY);
19080a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                        thirdContentDescription =
19090a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                                res.getString(R.string.description_video_call);
19100a8f9736d1af8903674c412cf88c1233b60a156eTyler Gunn                    }
191148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                }
1912eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1913eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1914eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1915eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1916eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
191723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.email_other)).append(" ");
19181cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                final Uri mailUri = Uri.fromParts(ContactsUtils.SCHEME_MAILTO, address, null);
1919eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1920eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
19212a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
192297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.emailLabelsGroup), dataItem.getMimeType(),
192397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1924f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
192523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = Email.getTypeLabel(res, email.getKindTypeColumn(kind),
1926f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
192723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1928f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
192923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
193023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_email_24dp);
193148290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_email_24dp;
1932eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1933eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1934eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1935eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1936eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
193723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.map_other)).append(" ");
1938eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1939eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
19402a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
194197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.postalLabelsGroup), dataItem.getMimeType(),
194297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1943f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
194423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = StructuredPostal.getTypeLabel(res,
1945f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
194623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1947f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
194823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
19496a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIntent =
19506a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                        StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
195123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateIcon = res.getDrawable(R.drawable.ic_directions_24dp);
195223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(res.getString(
195323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                        R.string.content_description_directions)).append(" ").append(header);
195423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_place_24dp);
195548290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_place_24dp;
1956eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1957eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
1958593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1959593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            final String address = sip.getSipAddress();
1960593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            if (!TextUtils.isEmpty(address)) {
1961593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                primaryContentDescription.append(res.getString(R.string.call_other)).append(
1962593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        " ");
1963593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                if (PhoneCapabilityTester.isSipPhone(context)) {
19641cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                    final Uri callUri = Uri.fromParts(PhoneAccount.SCHEME_SIP, address, null);
1965eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
196616339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1967593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                header = address;
1968593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                entryContextMenuInfo = new EntryContextMenuInfo(header,
1969593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        res.getString(R.string.phoneLabelsGroup), dataItem.getMimeType(),
1970593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        dataItem.getId(), dataItem.isSuperPrimary());
1971593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                if (sip.hasKindTypeColumn(kind)) {
1972593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                    text = SipAddress.getTypeLabel(res,
1973593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                            sip.getKindTypeColumn(kind), sip.getLabel()).toString();
1974593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                    primaryContentDescription.append(text).append(" ");
1975593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                }
1976593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                primaryContentDescription.append(header);
1977593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                icon = res.getDrawable(R.drawable.ic_dialer_sip_black_24dp);
1978593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                iconResourceId = R.drawable.ic_dialer_sip_black_24dp;
1979edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1980eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
19818025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            // If the name is already set and this is not the super primary value then leave the
19828025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            // current value. This way we show the super primary value when we are able to.
19838025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            if (dataItem.isSuperPrimary() || aboutCardName.value == null
19848025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    || aboutCardName.value.isEmpty()) {
19858025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
19868025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                if (!TextUtils.isEmpty(givenName)) {
19878025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    aboutCardName.value = res.getString(R.string.about_card_title) +
19888025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                            " " + givenName;
19898025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                } else {
19908025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    aboutCardName.value = res.getString(R.string.about_card_title);
19918025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                }
1992eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1993eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1994eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
19956bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = dataItem.buildDataStringForDisplay(context, kind);
1996eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1997eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
199860e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
199960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
2000e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
2001e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
2002e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
2003e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
20046a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                // Build advanced entry for known 3p types. Otherwise default to ResolveCache icon.
2005e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
2006e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
20076a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // If a secondDataItem is available, use it to build an entry with
20086a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // alternate actions
20096a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        if (secondDataItem != null) {
201023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
20116a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            alternateIcon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
20126a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            final GPlusOrHangoutsDataItemModel itemModel =
20136a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    new GPlusOrHangoutsDataItemModel(intent, alternateIntent,
20146a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            dataItem, secondDataItem, alternateContentDescription,
20156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            header, text, context);
20166a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
201797b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            populateGPlusOrHangoutsDataItemModel(itemModel);
201897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            intent = itemModel.intent;
201997b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateIntent = itemModel.alternateIntent;
202097b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateContentDescription = itemModel.alternateContentDescription;
202197b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            header = itemModel.header;
202297b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            text = itemModel.text;
20236a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        } else {
20246a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            if (GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(
20256a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    intent.getDataString())) {
20266a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
20276a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            } else {
20286a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
20296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            }
2030e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
2031e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
2032e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
20336a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // If a secondDataItem is available, use it to build an entry with
20346a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // alternate actions
20356a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        if (secondDataItem != null) {
203623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            icon = res.getDrawable(R.drawable.ic_hangout_24dp);
20376a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            alternateIcon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
203897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            final GPlusOrHangoutsDataItemModel itemModel =
20396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    new GPlusOrHangoutsDataItemModel(intent, alternateIntent,
20406a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            dataItem, secondDataItem, alternateContentDescription,
20416a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            header, text, context);
20426a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
204397b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            populateGPlusOrHangoutsDataItemModel(itemModel);
204497b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            intent = itemModel.intent;
204597b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateIntent = itemModel.alternateIntent;
204697b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateContentDescription = itemModel.alternateContentDescription;
204797b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            header = itemModel.header;
204897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            text = itemModel.text;
20496a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        } else {
20506a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            if (HANGOUTS_DATA_5_VIDEO.equals(intent.getDataString())) {
20516a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
20526a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            } else {
20536a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_hangout_24dp);
20546a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            }
2055e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
2056e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
2057e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
205897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        entryContextMenuInfo = new EntryContextMenuInfo(header, mimetype,
205997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                                dataItem.getMimeType(), dataItem.getId(),
206097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                                dataItem.isSuperPrimary());
20616bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        icon = ResolveCache.getInstance(context).getIcon(
2062e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
2063e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
2064e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
2065e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
2066e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
206748ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
2068e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
2069e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
2070eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
2071b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2072eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
2073eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
20746bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, intent)) {
2075eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
2076eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
2077eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
2078eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
2079dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
2080dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
20816bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, alternateIntent)) {
2082dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
2083f0d04c11b649e953177e79722d05fa048e53273dJay Shrauner            } else if (TextUtils.isEmpty(alternateContentDescription)) {
2084f0d04c11b649e953177e79722d05fa048e53273dJay Shrauner                // Attempt to use package manager to find a suitable content description if needed
208523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(getIntentResolveLabel(alternateIntent, context));
2086dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
2087dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
2088dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
2089eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
2090eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
2091eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
2092eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
2093eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
2094eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
2095fa238949855fba3b28a57fad3d585b13e80362bbBrian Attwell        // Ignore dataIds from the Me profile.
2096ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
2097ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
2098ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
209923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon,
2100b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                phoneContentDescription == null
2101b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                        ? new SpannableString(primaryContentDescription.toString())
2102b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                        : phoneContentDescription,
21037ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jang                intent, alternateIcon, alternateIntent,
21045da55ff837f57a63b409cd495cc25f61ed6fd709Wenyi Wang                smsContentDescription == null
21055da55ff837f57a63b409cd495cc25f61ed6fd709Wenyi Wang                        ? new SpannableString(alternateContentDescription.toString())
21065da55ff837f57a63b409cd495cc25f61ed6fd709Wenyi Wang                        : smsContentDescription,
21075da55ff837f57a63b409cd495cc25f61ed6fd709Wenyi Wang                shouldApplyColor, isEditable,
21085f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription, thirdAction,
21095f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                thirdExtras, iconResourceId);
2110eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
2111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
21126bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<Entry> dataItemsToEntries(List<DataItem> dataItems,
21136bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            MutableString aboutCardTitleOut) {
21146a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Hangouts and G+ use two data items to create one entry.
21156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        if (dataItems.get(0).getMimeType().equals(MIMETYPE_GPLUS_PROFILE) ||
21166a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataItems.get(0).getMimeType().equals(MIMETYPE_HANGOUTS)) {
21176a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            return gPlusOrHangoutsDataItemsToEntries(dataItems);
21186a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        } else {
21196a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            final List<Entry> entries = new ArrayList<>();
21206a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            for (DataItem dataItem : dataItems) {
21216a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                final Entry entry = dataItemToEntry(dataItem, /* secondDataItem = */ null,
21226a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        this, mContactData, aboutCardTitleOut);
21236a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                if (entry != null) {
21246a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    entries.add(entry);
21256a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
21266a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            }
21276a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            return entries;
21286a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
21296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
21306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
21316a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    /**
21326a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * G+ and Hangout entries are unique in that a single ExpandingEntryCardView.Entry consists
21336a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * of two data items. This method attempts to build each entry using the two data items if
21346a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * they are available. If there are more or less than two data items, a fall back is used
21356a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * and each data item gets its own entry.
21366a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     */
21376a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private List<Entry> gPlusOrHangoutsDataItemsToEntries(List<DataItem> dataItems) {
2138eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
21396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        final Map<Long, List<DataItem>> buckets = new HashMap<>();
21406a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Put the data items into buckets based on the raw contact id
2141eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
21426a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            List<DataItem> bucket = buckets.get(dataItem.getRawContactId());
21436a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            if (bucket == null) {
21446a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                bucket = new ArrayList<>();
21456a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                buckets.put(dataItem.getRawContactId(), bucket);
21466a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            }
21476a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            bucket.add(dataItem);
21486a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
21496a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
21506a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Use the buckets to build entries. If a bucket contains two data items, build the special
21516a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // entry, otherwise fall back to the normal entry.
21526a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        for (List<DataItem> bucket : buckets.values()) {
21536a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            if (bucket.size() == 2) {
21546a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                // Use the pair to build an entry
21556a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                final Entry entry = dataItemToEntry(bucket.get(0),
21566a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        /* secondDataItem = */ bucket.get(1), this, mContactData,
21576a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        /* aboutCardName = */ null);
21586a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                if (entry != null) {
21596a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    entries.add(entry);
21606a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
21616a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            } else {
21626a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                for (DataItem dataItem : bucket) {
21636a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    final Entry entry = dataItemToEntry(dataItem, /* secondDataItem = */ null,
21646a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            this, mContactData, /* aboutCardName = */ null);
21656a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    if (entry != null) {
21666a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        entries.add(entry);
21676a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    }
21686a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
2169eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
2170eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
2171eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
2172edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
2173edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
21746a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    /**
21756a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * Used for statically passing around G+ or Hangouts data items and entry fields to
21766a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * populateGPlusOrHangoutsDataItemModel.
21776a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     */
21786a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final class GPlusOrHangoutsDataItemModel {
21796a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Intent intent;
21806a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Intent alternateIntent;
21816a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public DataItem dataItem;
21826a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public DataItem secondDataItem;
21836a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public StringBuilder alternateContentDescription;
21846a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public String header;
21856a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public String text;
21866a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Context context;
21876a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
21886a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public GPlusOrHangoutsDataItemModel(Intent intent, Intent alternateIntent, DataItem dataItem,
21896a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                DataItem secondDataItem, StringBuilder alternateContentDescription, String header,
21906a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                String text, Context context) {
21916a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.intent = intent;
21926a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.alternateIntent = alternateIntent;
21936a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.dataItem = dataItem;
21946a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.secondDataItem = secondDataItem;
21956a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.alternateContentDescription = alternateContentDescription;
21966a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.header = header;
21976a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.text = text;
21986a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.context = context;
21996a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
22006a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
22016a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
22026a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static void populateGPlusOrHangoutsDataItemModel(
22036a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            GPlusOrHangoutsDataItemModel dataModel) {
22046a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        final Intent secondIntent = new Intent(Intent.ACTION_VIEW);
22056a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        secondIntent.setDataAndType(ContentUris.withAppendedId(Data.CONTENT_URI,
22066a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.secondDataItem.getId()), dataModel.secondDataItem.getMimeType());
22076a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // There is no guarantee the order the data items come in. Second
22086a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // data item does not necessarily mean it's the alternate.
22096a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Hangouts video and Add to circles should be alternate. Swap if needed
22106a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        if (HANGOUTS_DATA_5_VIDEO.equals(
22116a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.dataItem.getContentValues().getAsString(Data.DATA5)) ||
22126a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(
22136a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        dataModel.dataItem.getContentValues().getAsString(Data.DATA5))) {
22146a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateIntent = dataModel.intent;
22156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateContentDescription = new StringBuilder(dataModel.header);
22166a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
22176a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.intent = secondIntent;
22186a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.header = dataModel.secondDataItem.buildDataStringForDisplay(dataModel.context,
22196a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    dataModel.secondDataItem.getDataKind());
22206a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.text = dataModel.secondDataItem.getDataKind().typeColumn;
22216a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        } else if (HANGOUTS_DATA_5_MESSAGE.equals(
22226a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.dataItem.getContentValues().getAsString(Data.DATA5)) ||
22236a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                GPLUS_PROFILE_DATA_5_VIEW_PROFILE.equals(
22246a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        dataModel.dataItem.getContentValues().getAsString(Data.DATA5))) {
22256a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateIntent = secondIntent;
22266a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateContentDescription = new StringBuilder(
22276a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    dataModel.secondDataItem.buildDataStringForDisplay(dataModel.context,
22286a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            dataModel.secondDataItem.getDataKind()));
22296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
22306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
22316a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
22326bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static String getIntentResolveLabel(Intent intent, Context context) {
22336bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent,
2234dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
2235dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
2236dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
2237dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
2238dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
2239dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
2240dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
2241dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
22426bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            bestResolve = ResolveCache.getInstance(context).getBestResolve(intent, matches);
2243dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
2244dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
2245dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
2246dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
2247dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
2248dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
22496bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return String.valueOf(bestResolve.loadLabel(context.getPackageManager()));
2250dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
2251dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
2252edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
225331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
225431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
225531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
225631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
225731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
225831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
225931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
226031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
226131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
2262faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
226331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
2264faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
2265faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
2266929a62f5c1bcdc3773270f7d189d88c0464b36adJay Shrauner                if (imageViewDrawable instanceof BitmapDrawable && mContactData != null
226795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData() != null
226895c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData().length > 0) {
226995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // Perform the color analysis on the thumbnail instead of the full sized
227095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // image, so that our results will be as similar as possible to the Bugle
227195c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // app.
227295c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    final Bitmap bitmap = BitmapFactory.decodeByteArray(
227395c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData(), 0,
227495c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData().length);
227595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    try {
227695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        final int primaryColor = colorFromBitmap(bitmap);
227795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        if (primaryColor != 0) {
227895c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
227995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                                    primaryColor);
228095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        }
228195c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    } finally {
228295c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        bitmap.recycle();
2283faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
22848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
22858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
2286faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
2287a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
228831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
2289a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
229031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
229131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
229231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
2293faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
2294faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
22958571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
22968571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
22978571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
22988571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
22998571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
23008571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
23018571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
23028571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
23038571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
23048571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
23058571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
23068571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
2307faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
23081d86a67a46e5904bb682496f109c6e236b314381Wenyi Wang                    // update color and photo in suggestion card
23091d86a67a46e5904bb682496f109c6e236b314381Wenyi Wang                    onAggregationSuggestionChange();
231031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
231131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
231231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
231331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
231431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
2315faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
23169b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
23178e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mColorFilterColor = palette.mPrimaryColor;
23188e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mScroller.setHeaderTintColor(mColorFilterColor);
2319faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
23209b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
23218571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
23229b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
23238e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell                new PorterDuffColorFilter(mColorFilterColor, PorterDuff.Mode.SRC_ATOP);
23248e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mContactCard.setColorAndFilter(mColorFilterColor, mColorFilter);
23258e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mRecentCard.setColorAndFilter(mColorFilterColor, mColorFilter);
23268e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mAboutCard.setColorAndFilter(mColorFilterColor, mColorFilter);
23278d02b165232cb219f082e634db45374aa43e3f6aWenyi Wang        mSuggestionsCancelButton.setTextColor(mColorFilterColor);
23289b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
23299b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
23308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
23314c3d3e24b0de8b4170106e312f6a4ac8139e1242Wenyi Wang        if (mScroller == null || !CompatUtils.isLollipopCompatible()) {
23328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
23338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
23348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
23358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
23368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
23378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
23388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
23398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
23408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
23418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
2342847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
2343847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell                getWindow().getStatusBarColor(), desiredStatusBarColor);
2344847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
2345847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setEvaluator(new ArgbEvaluator());
2346847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.start();
23478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
23488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
23498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
23508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
23518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
23528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
2353a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
2354a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
23558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
23568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
23578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
23588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
2359b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
2360eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
2361b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
2362ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            if (interaction == null) {
2363ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                continue;
2364ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            }
2365ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
2366ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
2367b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
2368b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
2369b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
2370b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
2371b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
237223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    interaction.getContentDescription(this),
2373b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
2374dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
2375dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
2376dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
237748ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
23782a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
237948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* EntryContextMenuInfo = */ null,
238048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
238148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
238248290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* thirdContentDescription = */ null,
23835f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* thirdAction = */ Entry.ACTION_NONE,
23845f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* thirdActionExtras = */ null,
238548290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    interaction.getIconResourceId()));
2386b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2387b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
2388b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
2389b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2390eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
2391851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
2392cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
2393851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
2394405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
2395cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
2396cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2397cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
2398851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
23998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
2400930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            try {
24018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
2402930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (isFinishing()) {
2403930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    return;
2404930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                }
2405930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (data.isError()) {
240602ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    // This means either the contact is invalid or we had an
240702ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    // internal error such as an acore crash.
240802ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    Log.i(TAG, "Failed to load contact: " + ((ContactLoader)loader).getLookupUri());
240902ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
241002ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                            Toast.LENGTH_LONG).show();
241102ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    finish();
241202ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    return;
2413930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                }
2414930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (data.isNotFound()) {
24158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
24168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
24178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
2418930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    finish();
2419930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    return;
24208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
2421cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2422930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                bindContactData(data);
2423cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2424930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            } finally {
2425930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                Trace.endSection();
2426930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            }
2427cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
2428cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2429cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
2430851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
2431cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
2432cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
2433cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
2434d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
2435d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
2436b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
2437d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
24388571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
2439cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
2440cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
2441b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2442b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
2443b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
2444b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
24458477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
24468477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
24478477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
2448b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
2449b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
2450b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
2451b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
2452b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
24538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
24548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
24558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
24568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
24578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
24588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
24598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
24608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
2461eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
2462b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
2463b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2464b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2465b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
2466b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
2467b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
2468b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
2469b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
2470b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
2471ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
2472b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
2473b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
2474899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
2475ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
2476ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
2477ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
2478ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
2479ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
2480899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
2481899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
2482ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
2483899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
2484899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
2485899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
2486899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
2487899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
2488ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
2489ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
2490ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
2491ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
2492ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
2493b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
2494b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
2495b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2496b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2497b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2498b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
2499b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
2500b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
2501b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2502b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
2503b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
2504b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
2505b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2506b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2507b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2508b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
2509b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
2510b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2511b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
2512b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2513b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
2514b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
2515b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
2516b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2517b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
2518eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
251930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        final List<List<Entry>> interactionsWrapper = new ArrayList<>();
2520b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2521ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        // Serialize mRecentLoaderResults into a single list. This should be done on the main
2522ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        // thread to avoid races against mRecentLoaderResults edits.
2523ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
2524ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            allInteractions.addAll(loaderInteractions);
2525ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        }
2526ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell
252730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        mRecentDataTask = new AsyncTask<Void, Void, Void>() {
2528b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
252930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            protected Void doInBackground(Void... params) {
253030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("sort recent loader results");
253130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
253230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // Sort the interactions by most recent
253330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
253430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    @Override
253530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    public int compare(ContactInteraction a, ContactInteraction b) {
2536ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a == null && b == null) {
2537ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 0;
2538ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2539ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a == null) {
2540ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 1;
2541ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2542ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (b == null) {
2543ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return -1;
2544ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2545ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a.getInteractionDate() > b.getInteractionDate()) {
2546ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return -1;
2547ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2548ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a.getInteractionDate() == b.getInteractionDate()) {
2549ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 0;
2550ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2551ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        return 1;
255230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    }
255330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                });
255430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
255530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
255630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("contactInteractionsToEntries");
255730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
255830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // Wrap each interaction in its own list so that an icon is displayed for each entry
255930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                for (Entry contactInteraction : contactInteractionsToEntries(allInteractions)) {
256030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    List<Entry> entryListWrapper = new ArrayList<>(1);
256130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    entryListWrapper.add(contactInteraction);
256230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    interactionsWrapper.add(entryListWrapper);
256330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
256430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
256530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
256630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                return null;
2567b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
256830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
256930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            @Override
257030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            protected void onPostExecute(Void aVoid) {
257130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                super.onPostExecute(aVoid);
257230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("initialize recents card");
257330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
257430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                if (allInteractions.size() > 0) {
257530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mRecentCard.initialize(interactionsWrapper,
2576b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
2577c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
257830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                            mExpandingEntryCardViewListener, mScroller);
257930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mRecentCard.setVisibility(View.VISIBLE);
258030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
2581eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
258230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
258330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
258430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // About card is initialized along with the contact card, but since it appears after
258530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // the recent card in the UI, we hold off until making it visible until the recent
258630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // card is also ready to avoid stuttering.
258730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                if (mAboutCard.shouldShow()) {
258830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mAboutCard.setVisibility(View.VISIBLE);
258930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                } else {
259030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mAboutCard.setVisibility(View.GONE);
259130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
259230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                mRecentDataTask = null;
259330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            }
259430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        };
259530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        mRecentDataTask.execute();
2596b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
25978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
25988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
25998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
26008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
26018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
26028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
26038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
26048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
26058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
26068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
26078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
26088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
260930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        if (mRecentDataTask != null) {
261030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            mRecentDataTask.cancel(/* mayInterruptIfRunning = */ false);
261130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        }
26128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
261323889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
2614e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    @Override
2615e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    public void onDestroy() {
2616e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        super.onDestroy();
2617e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        if (mAggregationSuggestionEngine != null) {
2618e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mAggregationSuggestionEngine.quit();
2619e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
2620e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
2621e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
262223889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
2623d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
2624d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
2625d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
2626d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
2627d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2628d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2629a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    /**
2630a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     * Returns true if it is possible to share the current contact.
2631a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     */
2632a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    private boolean isContactShareable() {
2633a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
2634a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    }
2635a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
26366095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
26375a7a23bdb698b8e741a425c9617c5e33e6314cddWalter Jang        return EditorIntents.createCompactEditContactIntent(
26381e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                mContactData.getLookupUri(),
26391e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                mHasComputedThemeColor
26401e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                        ? new MaterialPalette(mColorFilterColor, mStatusBarColor) : null,
2641c41a1e5f5f4872c3a0da5dec7671b66d9d80eb4eWenyi Wang                mContactData.getPhotoId());
26426095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
26436095369885edcca566a812b551886e29c7ff8039Brian Attwell
26446095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
2645a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        mHasIntentLaunched = true;
2646c00a0b5370f0714f6af1ea9b3e29b10f25e91af0Zheng Fu        mContactLoader.cacheResult();
26476095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
2648d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2649d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
26505d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang    private void deleteContact() {
26515d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang        final Uri contactUri = mContactData.getLookupUri();
26525d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang        ContactDeletionInteraction.start(this, contactUri, /* finishActivityWhenDone =*/ true);
26535d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang    }
26545d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang
2655d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
2656d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
265763176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        if (mContactData != null) {
2658d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
2659d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
2660d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
2661d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
2662d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2663d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
2664333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
2665d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
2666d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
2667d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2668d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
2669eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
267063176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell                    QuickContactActivity.this, mContactData.getLookupUri(), !isStarred);
2671d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
267235ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell
267335ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            final CharSequence accessibilityText = !isStarred
267435ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    ? getResources().getText(R.string.description_action_menu_add_star)
267535ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    : getResources().getText(R.string.description_action_menu_remove_star);
267635ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // Accessibility actions need to have an associated view. We can't access the MenuItem's
267735ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // underlying view, so put this accessibility action on the root view.
267835ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            mScroller.announceForAccessibility(accessibilityText);
2679d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
2680d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2681d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2682752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
2683752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
2684652936f204b15097d030e870dda5054d8115cbf3Brian Attwell        final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
2685752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
2686552ee569d46ae5c63207a162157171b249243e56Yorke Lee        intent.setType(Contacts.CONTENT_VCARD_TYPE);
26878dcb4ed13da40fc609e6913708c3d730ba4f0574Brian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
2688752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2689752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
2690752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
2691752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
2692752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2693752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
2694a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
2695652936f204b15097d030e870dda5054d8115cbf3Brian Attwell            ImplicitIntentsUtil.startActivityOutsideApp(this, chooseIntent);
2696eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
2697752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
2698752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
2699752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
2700752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2701752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
2702752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
2703752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
2704752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
2705752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
2706752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
2707752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2708752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
2709752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
2710752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
2711752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
2712752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
2713752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
2714752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2715752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
2716752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
2717168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                        final String displayName = mContactData.getDisplayName();
2718168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                        final String toastMessage = TextUtils.isEmpty(displayName)
2719168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                                ? getString(R.string.createContactShortcutSuccessful_NoName)
2720168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                                : getString(R.string.createContactShortcutSuccessful, displayName);
2721168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                        Toast.makeText(QuickContactActivity.this, toastMessage,
2722752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
2723752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
2724752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2725752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
272663176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        builder.createContactShortcutIntent(mContactData.getLookupUri());
2727752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
2728752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
272966965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell    private boolean isShortcutCreatable() {
27301c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner        if (mContactData == null || mContactData.isUserProfile() ||
27311c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                mContactData.isDirectoryEntry()) {
27328d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell            return false;
27338d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell        }
273466965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        final Intent createShortcutIntent = new Intent();
273566965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        createShortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
273666965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        final List<ResolveInfo> receivers = getPackageManager()
273766965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell                .queryBroadcastReceivers(createShortcutIntent, 0);
273866965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        return receivers != null && receivers.size() > 0;
273966965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell    }
274066965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell
2741d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2742d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
2743eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
2744d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
2745d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
2746d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2747d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2748d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2749d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
2750d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
2751d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
2752333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
2753d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
2754d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
2755a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
2756d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
2757d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
2758d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
2759d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
2760d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
276130cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
27622e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
2763d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
2764d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
27652e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
2766d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
2767d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
2768d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
2769a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
27705d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            final MenuItem deleteMenuItem = menu.findItem(R.id.menu_delete);
277182ed2b5380e134705f96e7e1f1586a25f8ac403fWalter Jang            deleteMenuItem.setVisible(isContactEditable() && !mContactData.isUserProfile());
27725d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang
2773a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            final MenuItem shareMenuItem = menu.findItem(R.id.menu_share);
2774a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            shareMenuItem.setVisible(isContactShareable());
2775a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
277666965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell            final MenuItem shortcutMenuItem = menu.findItem(R.id.menu_create_contact_shortcut);
277766965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell            shortcutMenuItem.setVisible(isShortcutCreatable());
277866965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell
277956bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            final MenuItem helpMenu = menu.findItem(R.id.menu_help);
278056bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            helpMenu.setVisible(HelpUtils.isHelpAndFeedbackAvailable());
278156bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell
27828a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
2783d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
27848a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
2785d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2786d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2787d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2788d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
2789d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
2790d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
2791d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
2792d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
2793d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
2794d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
2795ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // This action is used to launch the contact selector, with the option of
2796ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // creating a new contact. Creating a new contact is an INSERT, while selecting
2797ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // an exisiting one is an edit. The fields in the edit screen will be
2798ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // prepopulated with data.
2799ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2800ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
2801ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.setType(Contacts.CONTENT_ITEM_TYPE);
2802ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2803fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    ArrayList<ContentValues> values = mContactData.getContentValues();
2804fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell
2805fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    // Only pre-fill the name field if the provided display name is an nickname
2806fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    // or better (e.g. structured name, nickname)
2807fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    if (mContactData.getDisplayNameSource() >= DisplayNameSources.NICKNAME) {
2808ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.NAME, mContactData.getDisplayName());
2809fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    } else if (mContactData.getDisplayNameSource()
2810fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                            == DisplayNameSources.ORGANIZATION) {
2811fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // This is probably an organization. Instead of copying the organization
2812fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // name into a name entry, copy it into the organization entry. This
2813fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // way we will still consider the contact an organization.
2814fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        final ContentValues organization = new ContentValues();
2815fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        organization.put(Organization.COMPANY, mContactData.getDisplayName());
2816fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        organization.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
2817fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        values.add(organization);
2818ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2819fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell
2820ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Last time used and times used are aggregated values from the usage stat
2821ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // table. They need to be removed from data values so the SQL table can insert
2822ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // properly
2823ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    for (ContentValues value : values) {
2824ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.LAST_TIME_USED);
2825ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.TIMES_USED);
2826ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2827ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.putExtra(Intents.Insert.DATA, values);
2828ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2829ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // If the contact can only export to the same account, add it to the intent.
2830ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Otherwise the ContactEditorFragment will show a dialog for selecting an
2831ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // account.
2832ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    if (mContactData.getDirectoryExportSupport() ==
2833ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                            Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY) {
28344a1c574cd62eb7ca1e0fcc3a61e5378e5e0787feBrian Attwell                        intent.putExtra(Intents.Insert.EXTRA_ACCOUNT,
2835ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                new Account(mContactData.getDirectoryAccountName(),
2836ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                        mContactData.getDirectoryAccountType()));
28374a1c574cd62eb7ca1e0fcc3a61e5378e5e0787feBrian Attwell                        intent.putExtra(Intents.Insert.EXTRA_DATA_SET,
2838ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                mContactData.getRawContacts().get(0).getDataSet());
2839ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2840ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2841f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    // Add this flag to disable the delete menu option on directory contact joins
2842f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    // with local contacts. The delete option is ambiguous when joining contacts.
2843f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    intent.putExtra(ContactEditorFragment.INTENT_EXTRA_DISABLE_DELETE_MENU_OPTION,
2844f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                            true);
2845f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos
2846ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    startActivityForResult(intent, REQUEST_CODE_CONTACT_SELECTION_ACTIVITY);
2847d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
2848d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
2849d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
2850d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
2851d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
2852d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
28535d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            case R.id.menu_delete:
28541c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                if (isContactEditable()) {
28551c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                    deleteContact();
28561c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                }
28575d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang                return true;
2858752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
285949de62f06d24653484adebd69123eeff2850a757Jay Shrauner                if (isContactShareable()) {
286049de62f06d24653484adebd69123eeff2850a757Jay Shrauner                    shareContact();
286149de62f06d24653484adebd69123eeff2850a757Jay Shrauner                }
2862752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
2863752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
28641c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                if (isShortcutCreatable()) {
28651c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                    createLauncherShortcutWithContact();
28661c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                }
2867752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
286856bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            case R.id.menu_help:
286956bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell                HelpUtils.launchHelpAndFeedbackForContactScreen(this);
287056bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell                return true;
2871d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
2872d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
2873d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
287423889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
2875edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
2876