QuickContactActivity.java revision 42a08d901e84ab4a7f3a8c4035f70dda1f4dd20a
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;
3423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulosimport android.content.res.Resources;
3531b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.Bitmap;
3695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwellimport android.graphics.BitmapFactory;
378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.Color;
38eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuff;
39eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuffColorFilter;
4031b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.BitmapDrawable;
418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.drawable.ColorDrawable;
4231b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.Drawable;
43edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.net.Uri;
4431b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.os.AsyncTask;
45edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.os.Bundle;
468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.os.Trace;
47eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.CalendarContract;
48e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.provider.ContactsContract;
49edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Email;
50eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Event;
51eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;
52eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Identity;
53eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Im;
54eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Nickname;
55eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Note;
56eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Organization;
57edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Phone;
58eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Relation;
59edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
60edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
61edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Website;
62752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract.Contacts;
6360e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulosimport android.provider.ContactsContract.Data;
64ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.provider.ContactsContract.Directory;
65eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.DisplayNameSources;
66ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.provider.ContactsContract.DataUsageFeedback;
67ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.provider.ContactsContract.Intents;
68edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.QuickContact;
69edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.RawContacts;
7023d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wangimport android.support.v4.content.ContextCompat;
7131b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
72e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.support.v7.widget.CardView;
730319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.PhoneAccount;
740319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.TelecomManager;
75c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwellimport android.text.BidiFormatter;
76b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wangimport android.text.Spannable;
777ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jangimport android.text.SpannableString;
78c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwellimport android.text.TextDirectionHeuristics;
79edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
80edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
812a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu;
822a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu.ContextMenuInfo;
83e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.view.LayoutInflater;
84d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
85d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
86eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
87f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chenimport android.view.MotionEvent;
88edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View;
89edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View.OnClickListener;
902a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.View.OnCreateContextMenuListener;
9110d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawaimport android.view.WindowManager;
92e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.Button;
93e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.CheckBox;
94e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.ImageView;
95e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.LinearLayout;
96e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport android.widget.TextView;
97edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
98d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
99edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
10072b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
101d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
1022d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
103e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
104d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wangimport com.android.contacts.activities.ContactEditorBaseActivity;
105eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
1062a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.common.ClipboardUtils;
107eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
108e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.common.ContactPhotoManager;
109eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
110bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwellimport com.android.contacts.common.activity.RequestPermissionsActivity;
1115f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunnimport com.android.contacts.common.dialog.CallSubjectDialog;
112d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.editor.SelectAccountDialogFragment;
11370e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chenimport com.android.contacts.common.interactions.TouchPointManager;
11431b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport com.android.contacts.common.lettertiles.LetterTileDrawable;
115752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder;
116752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
1170d5588da244d0992c3ff8f25d0875fdf95a8c644Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
118cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.Contact;
119cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.ContactLoader;
120cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.RawContact;
121428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.AccountType;
122d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.model.account.AccountWithDataSet;
123cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.DataItem;
124428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.dataitem.DataKind;
125cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.EmailDataItem;
126eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.EventDataItem;
127cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.ImDataItem;
128eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NicknameDataItem;
129eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NoteDataItem;
130eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.OrganizationDataItem;
131b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.common.model.dataitem.PhoneDataItem;
132eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.RelationDataItem;
133eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.SipAddressDataItem;
134eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredNameDataItem;
135eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredPostalDataItem;
136eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.WebsiteDataItem;
137e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.common.model.ValuesDelta;
138c6100ffd22ae176a3e84a1062d8cb92d955faef2Brian Attwellimport com.android.contacts.common.util.ImplicitIntentsUtil;
139eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.util.DateUtils;
140faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils;
141faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
1425f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunnimport com.android.contacts.common.util.UriUtils;
143d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwellimport com.android.contacts.common.util.ViewUtil;
144333091ae754ddfc25714c14b9b89534be24379f9Paul Soulosimport com.android.contacts.detail.ContactDisplayUtils;
145e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.editor.AggregationSuggestionEngine;
146e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.editor.AggregationSuggestionEngine.Suggestion;
147f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulosimport com.android.contacts.editor.ContactEditorFragment;
1485a7a23bdb698b8e741a425c9617c5e33e6314cddWalter Jangimport com.android.contacts.editor.EditorIntents;
149899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
150ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
152b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
153e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport com.android.contacts.interactions.JoinContactsDialogFragment;
1545368489acaadac82706ca0e678ec04d662db702eWenyi Wangimport com.android.contacts.interactions.JoinContactsDialogFragment.JoinContactsListener;
155b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1562d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
1572a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryContextMenuInfo;
1582ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryTag;
159e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
160493f8d14b1d974a2939a110ce89fa68e0c915c36Brian Attwellimport com.android.contacts.quickcontact.WebAddress.ParseException;
161e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
162eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
163b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
164eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
165b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
166b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
16702eaf11669d25fd885606b3b0700358323532cddBrian Attwellimport com.android.contacts.widget.QuickContactImageView;
16856bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwellimport com.android.contactsbind.HelpUtils;
16956bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell
170e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
171e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
17282a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shraunerimport java.lang.SecurityException;
1732d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
174899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
175eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
176b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
177b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
178eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
179edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
180e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport java.util.HashSet;
181edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
182b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
183e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport java.util.Set;
184e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangimport java.util.TreeSet;
18512ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shraunerimport java.util.concurrent.ConcurrentHashMap;
186edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
187edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
188edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
189edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
190edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
191edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
192e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wangpublic class QuickContactActivity extends ContactsActivity
1935368489acaadac82706ca0e678ec04d662db702eWenyi Wang        implements AggregationSuggestionEngine.Listener, JoinContactsListener {
1948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
1968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
1978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
1988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
1998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
2008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
2018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
202edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
203edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
2049b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
205e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private static final String KEY_IS_SUGGESTION_LIST_COLLAPSED = "is_suggestion_list_collapsed";
206e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private static final String KEY_SELECTED_SUGGESTION_CONTACTS = "selected_suggestion_contacts";
20718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private static final String KEY_PREVIOUS_CONTACT_ID = "previous_contact_id";
20818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private static final String KEY_SUGGESTIONS_AUTO_SELECTED = "suggestions_auto_seleted";
2099b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
2109b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
2118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
2120b267fec5cb5b2d836057b16d0085be349be5243Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xC8, 0, 0, 0);
213ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos    private static final int REQUEST_CODE_CONTACT_SELECTION_ACTIVITY = 2;
214ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
215dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
216752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
217752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
218752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
219edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
220edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
221edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
222edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
223e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
224e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
2256a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE = "addtocircle";
2266a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String GPLUS_PROFILE_DATA_5_VIEW_PROFILE = "view";
227e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
228e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
2296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String HANGOUTS_DATA_5_VIDEO = "hangout";
2306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String HANGOUTS_DATA_5_MESSAGE = "conversation";
23148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos    private static final String CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY =
23248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos            "com.android.contacts.quickcontact.QuickContactActivity";
233e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
23463176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell    /**
23563176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * The URI used to load the the Contact. Once the contact is loaded, use Contact#getLookupUri()
23663176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * instead of referencing this URI.
23763176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     */
238edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
239edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
2408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
24105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell    private String mExtraPrioritizedMimeType;
2428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
2438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
24497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private boolean mOnlyOnePhoneNumber;
24597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private boolean mOnlyOneEmail;
246edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
24702eaf11669d25fd885606b3b0700358323532cddBrian Attwell    private QuickContactImageView mPhotoView;
248eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
2496095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
250b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
251eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
252e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
253e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    // Suggestion card.
25418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private CardView mCollapsedSuggestionCardView;
25518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private CardView mExpandSuggestionCardView;
25618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private View mCollapasedSuggestionHeader;
25718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private TextView mCollapsedSuggestionCardTitle;
25818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private TextView mExpandSuggestionCardTitle;
259e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private ImageView mSuggestionSummaryPhoto;
260e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private TextView mSuggestionForName;
2615368489acaadac82706ca0e678ec04d662db702eWenyi Wang    private TextView mSuggestionContactsNumber;
262e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private LinearLayout mSuggestionList;
26318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private Button mSuggestionsCancelButton;
2645585c6ef5e8f075bdc3d514887322c6fa683f4cbTingting Wang    private Button mSuggestionsLinkButton;
265e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private boolean mIsSuggestionListCollapsed;
26618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private boolean mSuggestionsShouldAutoSelected = true;
26718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private long mPreviousContactId = 0;
268e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
269b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
270d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
2716bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private AsyncTask<Void, Void, Cp2DataCardModel> mEntriesAndActionsTask;
27230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell    private AsyncTask<Void, Void, Void> mRecentDataTask;
273e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
274e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private AggregationSuggestionEngine mAggregationSuggestionEngine;
275e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private List<Suggestion> mSuggestions;
276e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
277e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private TreeSet<Long> mSelectedAggregationIds = new TreeSet<>();
2786bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
2796bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * The last copy of Cp2DataCardModel that was passed to {@link #populateContactAndAboutCard}.
2806bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
2816bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel mCachedCp2DataCardModel;
282f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    /**
283f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  This scrim's opacity is controlled in two different ways. 1) Before the initial entrance
284f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  animation finishes, the opacity is animated by a value animator. This is designed to
285f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  distract the user from the length of the initial loading time. 2) After the initial
286f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  entrance animation, the opacity is directly related to scroll position.
287f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     */
2889b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
289f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    private boolean mIsEntranceAnimationFinished;
290a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
2918477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
2928571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
293edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
294a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    /**
295a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * Used to stop the ExpandingEntry cards from adjusting between an entry click and the intent
296a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * being launched.
297a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     */
298a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    private boolean mHasIntentLaunched;
299a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
3006cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
3019815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
3029b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
3038e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell    private int mColorFilterColor;
3049815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
3059758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
3069758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
307edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
308cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
309edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
310edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
311edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
312edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
313edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
314cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
315405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
316edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
317a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private static final List<String> SORTED_ABOUT_CARD_MIMETYPES = Lists.newArrayList(
318a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Nickname.CONTENT_ITEM_TYPE,
319a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name is inserted after nickname if it is available.
320a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // No mimetype for phonetic name exists.
321a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Website.CONTENT_ITEM_TYPE,
322a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Organization.CONTENT_ITEM_TYPE,
323a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Event.CONTENT_ITEM_TYPE,
324a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Relation.CONTENT_ITEM_TYPE,
325a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Im.CONTENT_ITEM_TYPE,
326a153dba33c2152332c5706178a367f52b0550723Paul Soulos            GroupMembership.CONTENT_ITEM_TYPE,
327a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Identity.CONTENT_ITEM_TYPE,
328a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Note.CONTENT_ITEM_TYPE);
329eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
330c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell    private static final BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
331c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell
332b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
333b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
334b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
335ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
336ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
337ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
338b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
339b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
340b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
341ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
342ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
343899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
344ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
345ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
346899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
347899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
348899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
34940d7a65c6e68967f3f486715b194c144d93c296fPaul Soulos            1L * 24L * 60L * 60L * 1000L /* 1 day */;
350899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
35140d7a65c6e68967f3f486715b194c144d93c296fPaul Soulos            7L * 24L * 60L * 60L * 1000L /* 7 days */;
352899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
353ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
354ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
355ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
3566095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
3576095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
3586095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
359ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
360ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
361ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
362ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
363ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
364ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
36512ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner    /**
36612ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * ConcurrentHashMap constructor params: 4 is initial table size, 0.9f is
36712ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * load factor before resizing, 1 means we only expect a single thread to
36812ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * write to the map so make only a single shard
36912ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     */
37012ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults =
37112ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner        new ConcurrentHashMap<>(4, 0.9f, 1);
372b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
373d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
3746cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
3752d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
3762d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
3772d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
3782ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Object entryTagObject = v.getTag();
3792ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (entryTagObject == null || !(entryTagObject instanceof EntryTag)) {
3802ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                Log.w(TAG, "EntryTag was not used correctly");
3816095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
3826095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
3832ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final EntryTag entryTag = (EntryTag) entryTagObject;
3842ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Intent intent = entryTag.getIntent();
3852ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final int dataId = entryTag.getId();
3862ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos
3872ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
3882ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                editContact();
3892d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
3902d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
391ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
39219c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            // Pass the touch point through the intent for use in the InCallUI
39319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            if (Intent.ACTION_CALL.equals(intent.getAction())) {
39419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                if (TouchPointManager.getInstance().hasValidPoint()) {
39519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    Bundle extras = new Bundle();
39619c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    extras.putParcelable(TouchPointManager.TOUCH_POINT,
39719c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                            TouchPointManager.getInstance().getPoint());
39819c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
39919c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                }
40019c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            }
40119c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
40219c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
40319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
40419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            mHasIntentLaunched = true;
40519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            try {
40619c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                startActivity(intent);
40719c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            } catch (SecurityException ex) {
40819c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
40919c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        Toast.LENGTH_SHORT).show();
41019c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Log.e(TAG, "QuickContacts does not have permission to launch "
41119c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        + intent);
41219c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            } catch (ActivityNotFoundException ex) {
41319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
41419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        Toast.LENGTH_SHORT).show();
41519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            }
41619c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
417ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
418ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
419ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
420ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
42120bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            final Uri intentUri = intent.getData();
42220bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            if ((intentUri != null && intentUri.getScheme() != null &&
4231cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                    intentUri.getScheme().equals(ContactsUtils.SCHEME_SMSTO)) ||
424ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
425ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
426ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
427ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
428ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
429ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
43020bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                final Uri dataUsageUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
431ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
432ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
433ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
4349881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                try {
4359881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    final boolean successful = getContentResolver().update(
4369881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                            dataUsageUri, new ContentValues(), null, null) > 0;
4379881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    if (!successful) {
4389881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                        Log.w(TAG, "DataUsageFeedback increment failed");
4399881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    }
4409881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                } catch (SecurityException ex) {
4419881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    Log.w(TAG, "DataUsageFeedback increment failed", ex);
442ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
443ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
444ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
445ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
446ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
44770e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            // Pass the touch point through the intent for use in the InCallUI
44870e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            if (Intent.ACTION_CALL.equals(intent.getAction())) {
4491f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                if (TouchPointManager.getInstance().hasValidPoint()) {
45070e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                    Bundle extras = new Bundle();
4511f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                    extras.putParcelable(TouchPointManager.TOUCH_POINT,
4521f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                            TouchPointManager.getInstance().getPoint());
4530319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunn                    intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
45470e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                }
45570e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            }
45670e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen
457793e73c859ed839c28cc37d80d62bde0a361c78aPaul Soulos            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
458793e73c859ed839c28cc37d80d62bde0a361c78aPaul Soulos
459a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
460417309a770568d7b278abc91c46a739020e48157Jay Shrauner            try {
461c6100ffd22ae176a3e84a1062d8cb92d955faef2Brian Attwell                ImplicitIntentsUtil.startActivityInAppIfPossible(QuickContactActivity.this, intent);
46282a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner            } catch (SecurityException ex) {
46382a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
46482a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                        Toast.LENGTH_SHORT).show();
46582a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                Log.e(TAG, "QuickContacts does not have permission to launch "
46682a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                        + intent);
467417309a770568d7b278abc91c46a739020e48157Jay Shrauner            } catch (ActivityNotFoundException ex) {
468417309a770568d7b278abc91c46a739020e48157Jay Shrauner                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
469417309a770568d7b278abc91c46a739020e48157Jay Shrauner                        Toast.LENGTH_SHORT).show();
470417309a770568d7b278abc91c46a739020e48157Jay Shrauner            }
4712d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
4722d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
4732d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
474e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
475e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
476e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
477e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
478e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
479e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
4800cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
4810cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        @Override
482245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        public void onExpand() {
483245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell            mScroller.setDisableTouchesForSuppressLayout(/* areTouchesDisabled = */ true);
484245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        }
485245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell
486245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        @Override
487245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        public void onExpandDone() {
488245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell            mScroller.setDisableTouchesForSuppressLayout(/* areTouchesDisabled = */ false);
4890cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        }
490e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
491e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
492e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    @Override
493e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    public void onAggregationSuggestionChange() {
494e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestions = mAggregationSuggestionEngine.getSuggestions();
49518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardView.setVisibility(View.GONE);
49618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardView.setVisibility(View.GONE);
497e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestionList.removeAllViews();
498e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
49996efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        if (mContactData == null) {
50096efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang            return;
50196efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        }
50296efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang
503e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final String suggestionForName = mContactData.getDisplayName();
504e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final int suggestionNumber = mSuggestions.size();
505e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
506e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        if (suggestionNumber <= 0) {
507e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            mSelectedAggregationIds.clear();
508e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            return;
509e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        }
510e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
5115368489acaadac82706ca0e678ec04d662db702eWenyi Wang        ContactPhotoManager.DefaultImageRequest
5125368489acaadac82706ca0e678ec04d662db702eWenyi Wang                request = new ContactPhotoManager.DefaultImageRequest(
5135368489acaadac82706ca0e678ec04d662db702eWenyi Wang                suggestionForName, mContactData.getLookupKey(), ContactPhotoManager.TYPE_DEFAULT,
5145368489acaadac82706ca0e678ec04d662db702eWenyi Wang                /* isCircular */ true );
5155368489acaadac82706ca0e678ec04d662db702eWenyi Wang        final long photoId = mContactData.getPhotoId();
51618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        final byte[] photoBytes = mContactData.getThumbnailPhotoBinaryData();
51718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (photoBytes != null) {
5185368489acaadac82706ca0e678ec04d662db702eWenyi Wang            ContactPhotoManager.getInstance(this).loadThumbnail(mSuggestionSummaryPhoto, photoId,
5195cf746f679b7866505f8b057565a648d65d1cad8Wenyi Wang                /* darkTheme */ false , /* isCircular */ true , request);
520e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        } else {
5215368489acaadac82706ca0e678ec04d662db702eWenyi Wang            ContactPhotoManager.DEFAULT_AVATAR.applyDefaultImage(mSuggestionSummaryPhoto,
5225368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    -1, false, request);
523e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        }
524e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
52518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        final String suggestionTitle = getResources().getQuantityString(
52618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                R.plurals.quickcontact_suggestion_card_title, suggestionNumber, suggestionNumber);
52718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardTitle.setText(suggestionTitle);
52818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardTitle.setText(suggestionTitle);
52918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
530e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        mSuggestionForName.setText(suggestionForName);
5315368489acaadac82706ca0e678ec04d662db702eWenyi Wang        final int linkedContactsNumber = mContactData.getRawContacts().size();
5325368489acaadac82706ca0e678ec04d662db702eWenyi Wang        final String contactsInfo;
53396efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        final String accountName = mContactData.getRawContacts().get(0).getAccountName();
53496efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        if (linkedContactsNumber == 1 && accountName == null) {
53596efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang            mSuggestionContactsNumber.setVisibility(View.INVISIBLE);
53696efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        }
53796efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        if (linkedContactsNumber == 1 && accountName != null) {
5385368489acaadac82706ca0e678ec04d662db702eWenyi Wang            contactsInfo = getResources().getString(R.string.contact_from_account_name,
53996efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang                    accountName);
5405368489acaadac82706ca0e678ec04d662db702eWenyi Wang        } else {
5415368489acaadac82706ca0e678ec04d662db702eWenyi Wang            contactsInfo = getResources().getString(
5425368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    R.string.quickcontact_contacts_number, linkedContactsNumber);
5435368489acaadac82706ca0e678ec04d662db702eWenyi Wang        }
5445368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionContactsNumber.setText(contactsInfo);
545e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
546e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        final Set<Long> suggestionContactIds = new HashSet<>();
547e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        for (Suggestion suggestion : mSuggestions) {
548e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            mSuggestionList.addView(inflateSuggestionListView(suggestion));
549e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            suggestionContactIds.add(suggestion.contactId);
550e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
551e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang
55218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (mIsSuggestionListCollapsed) {
55318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            collapseSuggestionList();
55418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        } else {
55518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            expandSuggestionList();
55618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        }
55718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
558e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        // Remove contact Ids that are not suggestions.
559e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        final Set<Long> selectedSuggestionIds = com.google.common.collect.Sets.intersection(
560e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang                mSelectedAggregationIds, suggestionContactIds);
561e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        mSelectedAggregationIds = new TreeSet<>(selectedSuggestionIds);
56296efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        if (!mSelectedAggregationIds.isEmpty()) {
56396efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang            enableLinkButton();
56496efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang        }
565e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
566e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
567e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private void collapseSuggestionList() {
56818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardView.setVisibility(View.VISIBLE);
56918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardView.setVisibility(View.GONE);
570e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mIsSuggestionListCollapsed = true;
571e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
572e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
573e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private void expandSuggestionList() {
57418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardView.setVisibility(View.GONE);
57518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardView.setVisibility(View.VISIBLE);
576e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mIsSuggestionListCollapsed = false;
577e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
578e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
57918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang    private View inflateSuggestionListView(final Suggestion suggestion) {
580e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final LayoutInflater layoutInflater = LayoutInflater.from(this);
581e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final View suggestionView = layoutInflater.inflate(
582e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                R.layout.quickcontact_suggestion_contact_item, null);
583e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
5845368489acaadac82706ca0e678ec04d662db702eWenyi Wang        ContactPhotoManager.DefaultImageRequest
5855368489acaadac82706ca0e678ec04d662db702eWenyi Wang                request = new ContactPhotoManager.DefaultImageRequest(
5865368489acaadac82706ca0e678ec04d662db702eWenyi Wang                suggestion.name, suggestion.lookupKey, ContactPhotoManager.TYPE_DEFAULT, /*
5875368489acaadac82706ca0e678ec04d662db702eWenyi Wang                isCircular */ true);
588e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final ImageView photo = (ImageView) suggestionView.findViewById(
589e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                R.id.aggregation_suggestion_photo);
590e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        if (suggestion.photo != null) {
5915368489acaadac82706ca0e678ec04d662db702eWenyi Wang            ContactPhotoManager.getInstance(this).loadThumbnail(photo, suggestion.photoId,
5925cf746f679b7866505f8b057565a648d65d1cad8Wenyi Wang                   /* darkTheme */ false, /* isCircular */ true, request);
593e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        } else {
5945368489acaadac82706ca0e678ec04d662db702eWenyi Wang            ContactPhotoManager.DEFAULT_AVATAR.applyDefaultImage(photo, -1, false, request);
595e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
596e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
597e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final TextView name = (TextView) suggestionView.findViewById(R.id.aggregation_suggestion_name);
598e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        name.setText(suggestion.name);
599e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
60018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        final TextView accountNameView = (TextView) suggestionView.findViewById(
60118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                R.id.aggregation_suggestion_account_name);
60218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        final String accountName = suggestion.rawContacts.get(0).accountName;
60318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (!TextUtils.isEmpty(accountName)) {
6045368489acaadac82706ca0e678ec04d662db702eWenyi Wang            accountNameView.setText(
6055368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    getResources().getString(R.string.contact_from_account_name, accountName));
60618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        } else {
60796efbb85af828e3dac9c4e09048c9ef9a7cfdde4Wenyi Wang            accountNameView.setVisibility(View.INVISIBLE);
608e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
609e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
610e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final CheckBox checkbox = (CheckBox) suggestionView.findViewById(R.id.suggestion_checkbox);
61118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        checkbox.setChecked(mSuggestionsShouldAutoSelected ||
61218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mSelectedAggregationIds.contains(suggestion.contactId));
61318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (checkbox.isChecked()) {
61418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mSelectedAggregationIds.add(suggestion.contactId);
61518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        }
616e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        checkbox.setTag(suggestion.contactId);
617e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        checkbox.setOnClickListener(new OnClickListener() {
618e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            @Override
619e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            public void onClick(View v) {
620e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                final CheckBox checkBox = (CheckBox) v;
621e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                final Long contactId = (Long) checkBox.getTag();
6225368489acaadac82706ca0e678ec04d662db702eWenyi Wang                if (mSelectedAggregationIds.contains(mContactData.getId())) {
6235368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    mSelectedAggregationIds.remove(mContactData.getId());
6245368489acaadac82706ca0e678ec04d662db702eWenyi Wang                }
625e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                if (checkBox.isChecked()) {
626e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                    mSelectedAggregationIds.add(contactId);
6275368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    if (mSelectedAggregationIds.size() >= 1) {
6285368489acaadac82706ca0e678ec04d662db702eWenyi Wang                        enableLinkButton();
6295368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    }
630e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                } else {
631e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                    mSelectedAggregationIds.remove(contactId);
63218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                    mSuggestionsShouldAutoSelected = false;
6335368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    if (mSelectedAggregationIds.isEmpty()) {
6345368489acaadac82706ca0e678ec04d662db702eWenyi Wang                        disableLinkButton();
6355368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    }
636e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                }
637e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            }
638e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        });
63918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
640e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        return suggestionView;
641e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
642e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
6435368489acaadac82706ca0e678ec04d662db702eWenyi Wang    private void enableLinkButton() {
6445368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.setClickable(true);
64556dfa580b5497f25169a4c875800ffff95cd31cdWenyi Wang        mSuggestionsLinkButton.getBackground().setColorFilter(
64623d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wang                ContextCompat.getColor(this, R.color.primary_color),
64756dfa580b5497f25169a4c875800ffff95cd31cdWenyi Wang                PorterDuff.Mode.SRC_ATOP);
64856dfa580b5497f25169a4c875800ffff95cd31cdWenyi Wang        mSuggestionsLinkButton.setTextColor(
64923d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wang                ContextCompat.getColor(this, android.R.color.white));
6505368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.setOnClickListener(new OnClickListener() {
6515368489acaadac82706ca0e678ec04d662db702eWenyi Wang            @Override
6525368489acaadac82706ca0e678ec04d662db702eWenyi Wang            public void onClick(View view) {
6535368489acaadac82706ca0e678ec04d662db702eWenyi Wang                // Join selected contacts.
6545368489acaadac82706ca0e678ec04d662db702eWenyi Wang                if (!mSelectedAggregationIds.contains(mContactData.getId())) {
6555368489acaadac82706ca0e678ec04d662db702eWenyi Wang                    mSelectedAggregationIds.add(mContactData.getId());
6565368489acaadac82706ca0e678ec04d662db702eWenyi Wang                }
6575368489acaadac82706ca0e678ec04d662db702eWenyi Wang                JoinContactsDialogFragment.start(
6585368489acaadac82706ca0e678ec04d662db702eWenyi Wang                        QuickContactActivity.this, mSelectedAggregationIds);
6595368489acaadac82706ca0e678ec04d662db702eWenyi Wang            }
6605368489acaadac82706ca0e678ec04d662db702eWenyi Wang        });
6615368489acaadac82706ca0e678ec04d662db702eWenyi Wang    }
6625368489acaadac82706ca0e678ec04d662db702eWenyi Wang
6635368489acaadac82706ca0e678ec04d662db702eWenyi Wang    @Override
6645368489acaadac82706ca0e678ec04d662db702eWenyi Wang    public void onContactsJoined() {
6655368489acaadac82706ca0e678ec04d662db702eWenyi Wang        disableLinkButton();
6665368489acaadac82706ca0e678ec04d662db702eWenyi Wang    }
6675368489acaadac82706ca0e678ec04d662db702eWenyi Wang
6685368489acaadac82706ca0e678ec04d662db702eWenyi Wang    private void disableLinkButton() {
6695368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.setClickable(false);
6705368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.getBackground().setColorFilter(
67123d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wang                ContextCompat.getColor(this, R.color.disabled_button_background),
67233f60d35a16407868de09ca898028e5eca6c6de3Wenyi Wang                PorterDuff.Mode.SRC_ATOP);
6735368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionsLinkButton.setTextColor(
67423d9b6e86d2cd15347ff7be0e911ee4992704d44Wenyi Wang                ContextCompat.getColor(this, R.color.disabled_button_text));
6755368489acaadac82706ca0e678ec04d662db702eWenyi Wang    }
6765368489acaadac82706ca0e678ec04d662db702eWenyi Wang
67797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private interface ContextMenuIds {
67897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int COPY_TEXT = 0;
67997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int CLEAR_DEFAULT = 1;
68097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int SET_DEFAULT = 2;
68197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    }
68297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
6832a4207fb39330e840436215c896cde911489e111Paul Soulos    private final OnCreateContextMenuListener mEntryContextMenuListener =
6842a4207fb39330e840436215c896cde911489e111Paul Soulos            new OnCreateContextMenuListener() {
6852a4207fb39330e840436215c896cde911489e111Paul Soulos        @Override
6862a4207fb39330e840436215c896cde911489e111Paul Soulos        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
6872a4207fb39330e840436215c896cde911489e111Paul Soulos            if (menuInfo == null) {
6882a4207fb39330e840436215c896cde911489e111Paul Soulos                return;
6892a4207fb39330e840436215c896cde911489e111Paul Soulos            }
69097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            final EntryContextMenuInfo info = (EntryContextMenuInfo) menuInfo;
6912a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.setHeaderTitle(info.getCopyText());
69297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            menu.add(ContextMenu.NONE, ContextMenuIds.COPY_TEXT,
69397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    ContextMenu.NONE, getString(R.string.copy_text));
69497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
69597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Don't allow setting or clearing of defaults for non-editable contacts
69697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (!isContactEditable()) {
69797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return;
69897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
69997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
70097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            final String selectedMimeType = info.getMimeType();
70197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
70297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Defaults to true will only enable the detail to be copied to the clipboard.
70397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            boolean onlyOneOfMimeType = true;
70497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
70597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Only allow primary support for Phone and Email content types
70697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (Phone.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
70797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                onlyOneOfMimeType = mOnlyOnePhoneNumber;
70897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            } else if (Email.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
70997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                onlyOneOfMimeType = mOnlyOneEmail;
71097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
71197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
71297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Checking for previously set default
71397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (info.isSuperPrimary()) {
71497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                menu.add(ContextMenu.NONE, ContextMenuIds.CLEAR_DEFAULT,
71597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        ContextMenu.NONE, getString(R.string.clear_default));
71697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            } else if (!onlyOneOfMimeType) {
71797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                menu.add(ContextMenu.NONE, ContextMenuIds.SET_DEFAULT,
71897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        ContextMenu.NONE, getString(R.string.set_default));
71997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
7202a4207fb39330e840436215c896cde911489e111Paul Soulos        }
7212a4207fb39330e840436215c896cde911489e111Paul Soulos    };
7222a4207fb39330e840436215c896cde911489e111Paul Soulos
7232a4207fb39330e840436215c896cde911489e111Paul Soulos    @Override
7242a4207fb39330e840436215c896cde911489e111Paul Soulos    public boolean onContextItemSelected(MenuItem item) {
7252a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo menuInfo;
7262a4207fb39330e840436215c896cde911489e111Paul Soulos        try {
7272a4207fb39330e840436215c896cde911489e111Paul Soulos            menuInfo = (EntryContextMenuInfo) item.getMenuInfo();
7282a4207fb39330e840436215c896cde911489e111Paul Soulos        } catch (ClassCastException e) {
7292a4207fb39330e840436215c896cde911489e111Paul Soulos            Log.e(TAG, "bad menuInfo", e);
7302a4207fb39330e840436215c896cde911489e111Paul Soulos            return false;
7312a4207fb39330e840436215c896cde911489e111Paul Soulos        }
7322a4207fb39330e840436215c896cde911489e111Paul Soulos
73397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        switch (item.getItemId()) {
73497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.COPY_TEXT:
73597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                ClipboardUtils.copyText(this, menuInfo.getCopyLabel(), menuInfo.getCopyText(),
73697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        true);
73797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
73897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.SET_DEFAULT:
73997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                final Intent setIntent = ContactSaveService.createSetSuperPrimaryIntent(this,
74097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        menuInfo.getId());
74197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                this.startService(setIntent);
74297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
74397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.CLEAR_DEFAULT:
74497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                final Intent clearIntent = ContactSaveService.createClearPrimaryIntent(this,
74597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        menuInfo.getId());
74697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                this.startService(clearIntent);
74797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
74897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            default:
74997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                throw new IllegalArgumentException("Unknown menu option " + item.getItemId());
75097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
7512a4207fb39330e840436215c896cde911489e111Paul Soulos    }
7522a4207fb39330e840436215c896cde911489e111Paul Soulos
753d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
754d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
755d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
756d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
757d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
758d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
759d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
760d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
761d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
762d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
763d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
764d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
765d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
766d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
767d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
768d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
769d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
770d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
771d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
772d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
773d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
774d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
775d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
776d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
777d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
778d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
779d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
780d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
781d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
782d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
783b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
784b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
785b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
786b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
787f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            finish();
788b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
7898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
7918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
7928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
7938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
7948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
7968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
7978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
7988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
7998477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
8008477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
8018477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
8028477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
8038477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
8048477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
8058477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
806f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onEntranceAnimationDone() {
807f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            mIsEntranceAnimationFinished = true;
808f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        }
809f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
810f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        @Override
811f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onTransparentViewHeightChange(float ratio) {
812f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            if (mIsEntranceAnimationFinished) {
813f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                mWindowScrim.setAlpha((int) (0xFF * ratio));
8148477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
8158477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
816b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
817b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
818eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
819eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
820eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
821eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
822eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
823eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
824eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
825eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
826eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
827eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
828eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
829eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
830eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
831eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
832eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
833eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
834eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
835eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
836eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
837eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
838eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
839eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
840eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
841eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
842eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
843eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
844eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
845eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
846eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
847eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
848eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
849eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
850eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
851eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
852eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
853eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
854cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
855cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
85605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 1. Whether one of the mimetypes is the prioritized mimetype
85705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 2. Number of times used
85805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 3. Last time used
85905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 4. Statically defined
860cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
861eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
862eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
863eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
864eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
86505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final DataItem lhs = lhsList.get(0);
86605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final DataItem rhs = rhsList.get(0);
86705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final String lhsMimeType = lhs.getMimeType();
86805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final String rhsMimeType = rhs.getMimeType();
86905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell
87005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 1. Whether one of the mimetypes is the prioritized mimetype
87105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            if (!TextUtils.isEmpty(mExtraPrioritizedMimeType) && !lhsMimeType.equals(rhsMimeType)) {
87205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                if (rhsMimeType.equals(mExtraPrioritizedMimeType)) {
87305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    return 1;
87405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                }
87505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                if (lhsMimeType.equals(mExtraPrioritizedMimeType)) {
87605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    return -1;
87705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                }
87805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            }
87905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell
88005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 2. Number of times used
881eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
882eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
883eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
884eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
885eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
886eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
887eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
88805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 3. Last time used
889eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
890eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
891eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
892eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
893eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
894eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
895eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
896eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
897eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
898eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
899eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
90005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 4. Resort to a statically defined mimetype order.
90105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            if (!lhsMimeType.equals(rhsMimeType)) {
90205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                for (String mimeType : LEADING_MIMETYPES) {
90305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    if (lhsMimeType.equals(mimeType)) {
90405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                        return -1;
90505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    } else if (rhsMimeType.equals(mimeType)) {
90605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                        return 1;
90705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    }
908eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
909eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
910eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
911eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
912eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
913eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
914edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
915f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    public boolean dispatchTouchEvent(MotionEvent ev) {
916f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
917f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen            TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
918f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        }
919f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        return super.dispatchTouchEvent(ev);
920f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    }
921f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen
922f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    @Override
9238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
9248a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
9258a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
926edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
927bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell        if (RequestPermissionsActivity.startPermissionActivity(this)) {
928bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell            return;
929bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell        }
930bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell
9318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getWindow().setStatusBarColor(Color.TRANSPARENT);
9322426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
933d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
934dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
93510d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
93610d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
93710d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
93810d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
939edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
940edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
941a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
942a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
9430cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
9440cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
945eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
9466095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
947b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
948eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
949edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
95018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardView = (CardView) findViewById(R.id.collapsed_suggestion_card);
95118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardView = (CardView) findViewById(R.id.expand_suggestion_card);
95218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapasedSuggestionHeader = findViewById(R.id.collapsed_suggestion_header);
95318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapsedSuggestionCardTitle = (TextView) findViewById(
95418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                R.id.collapsed_suggestion_card_title);
95518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mExpandSuggestionCardTitle = (TextView) findViewById(R.id.expand_suggestion_card_title);
956e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestionSummaryPhoto = (ImageView) findViewById(R.id.suggestion_icon);
957e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestionForName = (TextView) findViewById(R.id.suggestion_for_name);
9585368489acaadac82706ca0e678ec04d662db702eWenyi Wang        mSuggestionContactsNumber = (TextView) findViewById(R.id.suggestion_for_contacts_number);
959e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mSuggestionList = (LinearLayout) findViewById(R.id.suggestion_list);
96018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mSuggestionsCancelButton= (Button) findViewById(R.id.cancel_button);
9615585c6ef5e8f075bdc3d514887322c6fa683f4cbTingting Wang        mSuggestionsLinkButton = (Button) findViewById(R.id.link_button);
962e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        if (savedInstanceState != null) {
963e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mIsSuggestionListCollapsed = savedInstanceState.getBoolean(
964e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                    KEY_IS_SUGGESTION_LIST_COLLAPSED, true);
96518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mPreviousContactId = savedInstanceState.getLong(KEY_PREVIOUS_CONTACT_ID);
96618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mSuggestionsShouldAutoSelected = savedInstanceState.getBoolean(
96718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                    KEY_SUGGESTIONS_AUTO_SELECTED, true);
968e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mSelectedAggregationIds = (TreeSet<Long>)
969e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                    savedInstanceState.getSerializable(KEY_SELECTED_SUGGESTION_CONTACTS);
970e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        } else {
971e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mIsSuggestionListCollapsed = true;
972e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mSelectedAggregationIds.clear();
9735368489acaadac82706ca0e678ec04d662db702eWenyi Wang        }
9742876977d8df4e61004b103a8c12f7443ab297711Walter Jang        if (mSelectedAggregationIds.isEmpty()) {
9755368489acaadac82706ca0e678ec04d662db702eWenyi Wang            disableLinkButton();
9762876977d8df4e61004b103a8c12f7443ab297711Walter Jang        } else {
9772876977d8df4e61004b103a8c12f7443ab297711Walter Jang            enableLinkButton();
978e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
97918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mCollapasedSuggestionHeader.setOnClickListener(new OnClickListener() {
98018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            @Override
98118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            public void onClick(View view) {
98218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mCollapsedSuggestionCardView.setVisibility(View.GONE);
98318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mExpandSuggestionCardView.setVisibility(View.VISIBLE);
98418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mIsSuggestionListCollapsed = false;
98518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            }
98618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        });
98718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
98818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mSuggestionsCancelButton.setOnClickListener(new OnClickListener() {
98918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            @Override
99018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            public void onClick(View view) {
99118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mCollapsedSuggestionCardView.setVisibility(View.VISIBLE);
99218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mExpandSuggestionCardView.setVisibility(View.GONE);
99318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                mIsSuggestionListCollapsed = true;
99418906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            }
99518906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        });
99618906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang
9976095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
998eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
999eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
100023889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
10012a4207fb39330e840436215c896cde911489e111Paul Soulos        mContactCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
1002b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1003b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
1004b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
1005edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1006eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
10072a4207fb39330e840436215c896cde911489e111Paul Soulos        mAboutCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
1008eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
100902eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView = (QuickContactImageView) findViewById(R.id.photo);
101063176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        final View transparentView = findViewById(R.id.transparent_view);
10110d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
101263176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell            transparentView.setOnClickListener(new OnClickListener() {
10130d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
10140d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
10150d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
10160d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
10170d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
10180d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
1019edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1020d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        // Allow a shadow to be shown under the toolbar.
1021d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
1022d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell
1023d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
1024d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
10259b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
10269b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
10279b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
10289b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
10296219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
10308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
1031f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = mHasAlreadyBeenOpened;
10329b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
1033f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mWindowScrim.setAlpha(0);
10349b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
1035edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1036c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
1037ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
1038ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
1039ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
10408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
10419b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
10429b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1043d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
1044d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
1045d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
1046d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
1047d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
1048d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
1049d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
1050d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
1051d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
105223889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
1053f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ true,
1054f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                new Runnable() {
1055f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    @Override
1056f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    public void run() {
1057f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        if (!mHasAlreadyBeenOpened) {
1058f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // The initial scrim opacity must match the scrim opacity that would be
1059f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // achieved by scrolling to the starting position.
1060f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final float alphaRatio = mExtraMode == MODE_FULLY_EXPANDED ?
1061f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    1 : mScroller.getStartingTransparentHeightRatio();
1062f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int duration = getResources().getInteger(
1063f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    android.R.integer.config_shortAnimTime);
1064f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int desiredAlpha = (int) (0xFF * alphaRatio);
1065f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            ObjectAnimator o = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0,
1066f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    desiredAlpha).setDuration(duration);
1067f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
1068f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            o.start();
1069f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        }
1070f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    }
1071f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                });
1072f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
10739b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
10749b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
1075ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
1076ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
1077ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
1078ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
1079ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
1080ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
1081ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
1082ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
1083ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
1084ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
1085ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
1086ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
1087ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
1088ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
1089a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
1090a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
10919b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
1092ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
1093ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
10949b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
10959b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
10968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
10978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
10988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1099405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
1100405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1101d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wang        final boolean deletedOrSplit = requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
1102d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wang                (resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED ||
1103d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wang                resultCode == ContactEditorBaseActivity.RESULT_CODE_SPLIT);
1104d1cb9045bffc3843f5a294d41ce4608c701bccccWenyi Wang        if (deletedOrSplit) {
11058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
1106ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos        } else if (requestCode == REQUEST_CODE_CONTACT_SELECTION_ACTIVITY &&
1107ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                resultCode != RESULT_CANCELED) {
1108ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos            processIntent(data);
1109b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
11108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
1111dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
11128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
11138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
11148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
11158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
1116f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = true;
11178571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
1118d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
1119d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1120d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
11219b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
11229b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
11239b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
11249b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
11258e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilterColor);
11269b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
1127e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        savedInstanceState.putBoolean(KEY_IS_SUGGESTION_LIST_COLLAPSED, mIsSuggestionListCollapsed);
112818906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        savedInstanceState.putLong(KEY_PREVIOUS_CONTACT_ID, mPreviousContactId);
112918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        savedInstanceState.putBoolean(
113018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                KEY_SUGGESTIONS_AUTO_SELECTED, mSuggestionsShouldAutoSelected);
1131e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        savedInstanceState.putSerializable(
1132e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                KEY_SELECTED_SUGGESTION_CONTACTS, mSelectedAggregationIds);
11339b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
11349b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1135d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
1136f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos        if (intent == null) {
1137f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos            finish();
1138f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos            return;
1139f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos        }
1140d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
1141d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1142d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
1143d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
1144d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
1145d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
1146d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
1147d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
114805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE, QuickContact.MODE_LARGE);
114905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell        mExtraPrioritizedMimeType = getIntent().getStringExtra(QuickContact.EXTRA_PRIORITIZED_MIMETYPE);
1150d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
1151d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1152fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        if (lookupUri == null) {
1153fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner            finish();
1154fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner            return;
1155fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        }
1156fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        mLookupUri = lookupUri;
1157d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
1158d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
1159d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
1160d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
1161d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
1162d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
1163d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // we need to restart the loader and reload the new contact.
116459a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            destroyInteractionLoaders();
1165405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
1166405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
116759a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            mCachedCp2DataCardModel = null;
1168d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
11692d150da246632b1649999cfabed776133b097775Brian Attwell
11702d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
11712426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
1172edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
117359a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell    private void destroyInteractionLoaders() {
117459a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        for (int interactionLoaderId : mRecentLoaderIds) {
117559a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            getLoaderManager().destroyLoader(interactionLoaderId);
117659a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        }
117759a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell    }
117859a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell
1179b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
11808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
11818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
11828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
11838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
1184c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
1185b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
1186b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
118781281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
1188d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
11899b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
11909f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString(),
11919f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang                    /* isPhoneNumber= */ false);
11929b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
119381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
119481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
119581281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
11969f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang    private void setHeaderNameText(String value, boolean isPhoneNumber) {
1197d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
11989b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
11999f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang                mScroller.setTitle(value, isPhoneNumber);
12009b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
120181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
120281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
120381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
120481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
1205edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
1206edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
1207edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
1208edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
1209edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
1210edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
1211edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
1212edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
1213edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1214edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1215edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
1216edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1217edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1218edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
1219cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
1220edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
12218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
12228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
12236cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
1224d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
1225edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
12268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
12278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
1228dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
122902eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView.setIsBusiness(mContactData.isDisplayNameFromOrganization());
12306219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
123131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
1232df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang        final String displayName = ContactDisplayUtils.getDisplayName(this, data).toString();
12339f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang        setHeaderNameText(
12349f153a02a56b9d97dc51371243c52e79fbce5813Tingting Wang                displayName, mContactData.getDisplayNameSource() == DisplayNameSources.PHONE);
1235df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang        final String phoneticName = ContactDisplayUtils.getPhoneticName(this, data);
1236df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang        if (mScroller != null) {
1237df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang            if (mContactData.getDisplayNameSource() != DisplayNameSources.STRUCTURED_PHONETIC_NAME
1238df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang                    && !TextUtils.isEmpty(phoneticName)) {
1239df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang                mScroller.setPhoneticName(phoneticName);
1240df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang            } else {
1241df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang                mScroller.setPhoneticNameGone();
1242df65d168660ce7ebd4961335e3363eb7c267e0c6Tingting Wang            }
1243d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang        }
1244edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
12458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1246dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
12476bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mEntriesAndActionsTask = new AsyncTask<Void, Void, Cp2DataCardModel>() {
12488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
12498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
12506bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected Cp2DataCardModel doInBackground(
1251eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
1252eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
12538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
12548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
12558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
12566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected void onPostExecute(Cp2DataCardModel cardDataModel) {
12576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                super.onPostExecute(cardDataModel);
12588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
12598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
12608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
12618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
12626bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    bindDataToCards(cardDataModel);
12638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
12648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
12658a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
12668a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
12678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
12688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
12698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
12706bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void bindDataToCards(Cp2DataCardModel cp2DataCardModel) {
12716bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        startInteractionLoaders(cp2DataCardModel);
12726bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        populateContactAndAboutCard(cp2DataCardModel);
1273e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        populateSuggestionCard();
1274eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1275eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
12766bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void startInteractionLoaders(Cp2DataCardModel cp2DataCardModel) {
12776bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Map<String, List<DataItem>> dataItemsMap = cp2DataCardModel.dataItemsMap;
12786bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> phoneDataItems = dataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
127997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        if (phoneDataItems != null && phoneDataItems.size() == 1) {
128097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            mOnlyOnePhoneNumber = true;
128197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
1282eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
1283eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
1284eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
1285eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
1286eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
1287eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1288eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1289ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
1290eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
1291eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1292eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
12938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
12948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
1295ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
1296ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
1297ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
1298ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
1299ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
1300ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
1301ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
1302ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
13038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
1304899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
13058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1306eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1307899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
13086bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> emailDataItems = dataItemsMap.get(Email.CONTENT_ITEM_TYPE);
130997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        if (emailDataItems != null && emailDataItems.size() == 1) {
131097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            mOnlyOneEmail = true;
131197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
1312eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
1313eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
1314eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
1315eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
1316eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
1317eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1318eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1319ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
1320eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
1321899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
1322899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
1323ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
1324899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
13258a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
13268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
13278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
13288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
13298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
13308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
13318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
13328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
13338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
13348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
13358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
13368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
13378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
13388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
13398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
13408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
13416bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<List<Entry>> buildAboutCardEntries(Map<String, List<DataItem>> dataItemsMap) {
1342a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
1343a153dba33c2152332c5706178a367f52b0550723Paul Soulos        for (String mimetype : SORTED_ABOUT_CARD_MIMETYPES) {
13446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> mimeTypeItems = dataItemsMap.get(mimetype);
1345a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (mimeTypeItems == null) {
1346a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
1347a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
13486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // Set aboutCardTitleOut = null, since SORTED_ABOUT_CARD_MIMETYPES doesn't contain
13496bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // the name mimetype.
13506bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<Entry> aboutEntries = dataItemsToEntries(mimeTypeItems,
13516bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    /* aboutCardTitleOut = */ null);
1352a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutEntries.size() > 0) {
1353a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(aboutEntries);
1354a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
1355a153dba33c2152332c5706178a367f52b0550723Paul Soulos        }
1356a153dba33c2152332c5706178a367f52b0550723Paul Soulos        return aboutCardEntries;
1357a153dba33c2152332c5706178a367f52b0550723Paul Soulos    }
1358a153dba33c2152332c5706178a367f52b0550723Paul Soulos
1359a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    @Override
1360a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    protected void onResume() {
1361a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        super.onResume();
1362a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        // If returning from a launched activity, repopulate the contact and about card
1363a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        if (mHasIntentLaunched) {
1364a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = false;
13656bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            populateContactAndAboutCard(mCachedCp2DataCardModel);
1366a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
136759a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell
136859a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // When exiting the activity and resuming, we want to force a full reload of all the
136959a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // interaction data in case something changed in the background. On screen rotation,
137059a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // we don't need to do this. And, mCachedCp2DataCardModel will be null, so we won't.
137159a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        if (mCachedCp2DataCardModel != null) {
137259a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            destroyInteractionLoaders();
137359a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            startInteractionLoaders(mCachedCp2DataCardModel);
137459a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        }
1375a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    }
1376a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
1377e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    private void populateSuggestionCard() {
1378e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        // Initialize suggestion related view and data.
137918906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (mPreviousContactId != mContactData.getId()) {
138018906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mCollapsedSuggestionCardView.setVisibility(View.GONE);
138118906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mExpandSuggestionCardView.setVisibility(View.GONE);
138218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang            mIsSuggestionListCollapsed = true;
138342a08d901e84ab4a7f3a8c4035f70dda1f4dd20aTingting Wang            mSuggestionsShouldAutoSelected = true;
13840fafb0e2813a28b31a71e908d30e18a6edaa3d95Tingting Wang            mSuggestionList.removeAllViews();
13850fafb0e2813a28b31a71e908d30e18a6edaa3d95Tingting Wang        }
1386e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
13873d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang        // Do not show the card when it's directory contact or invisible.
13883d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang        if (DirectoryContactUtil.isDirectoryContact(mContactData)
13893d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang                || InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
13903d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang            return;
13913d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang        }
13923d65ea5f6113c497f09abf18d3f11abdcd1c894fTingting Wang
1393e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        if (mAggregationSuggestionEngine == null) {
1394e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mAggregationSuggestionEngine = new AggregationSuggestionEngine(this);
1395e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mAggregationSuggestionEngine.setListener(this);
1396e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            mAggregationSuggestionEngine.setSuggestionsLimit(getResources().getInteger(
1397e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang                    R.integer.quickcontact_suggestions_limit));
1398e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mAggregationSuggestionEngine.start();
1399e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
1400e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
1401e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mAggregationSuggestionEngine.setContactId(mContactData.getId());
140218906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        if (mPreviousContactId != 0
140318906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang                && mPreviousContactId != mContactData.getId()) {
1404e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            // Clear selected Ids when listing suggestions for new contact Id.
1405e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang            mSelectedAggregationIds.clear();
1406e29b6943ade6f5e32b7caf85dd127673d710f7e3Tingting Wang        }
140718906c03230be7de5b8e8bf663312cbc9a7c3097Tingting Wang        mPreviousContactId = mContactData.getId();
1408e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
1409e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        // Trigger suggestion engine to compute suggestions.
1410a1af0c833e7b4113126131e9949c3d4c9579e5dfTingting Wang        if (mContactData.getId() <= 0) {
1411a1af0c833e7b4113126131e9949c3d4c9579e5dfTingting Wang            return;
1412a1af0c833e7b4113126131e9949c3d4c9579e5dfTingting Wang        }
1413e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        final ContentValues values = new ContentValues();
1414e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
1415e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                mContactData.getDisplayName());
1416e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME,
1417e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang                mContactData.getPhoneticName());
1418e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        mAggregationSuggestionEngine.onNameChange(ValuesDelta.fromBefore(values));
1419e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
1420e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
14216bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void populateContactAndAboutCard(Cp2DataCardModel cp2DataCardModel) {
14226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mCachedCp2DataCardModel = cp2DataCardModel;
14236bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (mHasIntentLaunched || cp2DataCardModel == null) {
1424a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            return;
1425a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
1426eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
1427eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
14286bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = cp2DataCardModel.contactCardEntries;
14296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = cp2DataCardModel.aboutCardEntries;
14306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final String customAboutCardName = cp2DataCardModel.customAboutCardName;
1431eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1432eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
143305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final boolean firstEntriesArePrioritizedMimeType =
143405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    !TextUtils.isEmpty(mExtraPrioritizedMimeType) &&
143505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mCachedCp2DataCardModel.dataItemsMap.containsKey(mExtraPrioritizedMimeType) &&
143605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mCachedCp2DataCardModel.dataItemsMap.get(mExtraPrioritizedMimeType).size() != 0;
1437eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
1438eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
1439c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mContactCard.isExpanded(),
144089966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                    /* isAlwaysExpanded = */ false,
14410cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener,
144205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mScroller,
144305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    firstEntriesArePrioritizedMimeType);
1444eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
1445eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1446eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
1447eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1448eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
14498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1450eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
1451c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        // Phonetic name is not a data item, so the entry needs to be created separately
1452c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        final String phoneticName = mContactData.getPhoneticName();
1453c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        if (!TextUtils.isEmpty(phoneticName)) {
1454c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            Entry phoneticEntry = new Entry(/* viewId = */ -1,
1455c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* icon = */ null,
1456c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    getResources().getString(R.string.name_phonetic),
1457c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    phoneticName,
14588a3fe52802135be2402a5b216325615fb796a509Walter Jang                    /* subHeaderIcon = */ null,
1459c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* text = */ null,
14608a3fe52802135be2402a5b216325615fb796a509Walter Jang                    /* textIcon = */ null,
146123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    /* primaryContentDescription = */ null,
1462c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* intent = */ null,
1463c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIcon = */ null,
1464c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIntent = */ null,
1465c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateContentDescription = */ null,
1466c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* shouldApplyColor = */ false,
14672a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
14682a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ new EntryContextMenuInfo(phoneticName,
146997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                            getResources().getString(R.string.name_phonetic),
147097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                            /* mimeType = */ null, /* id = */ -1, /* isPrimary = */ false),
147148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
147248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
147348290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* thirdContentDescription = */ null,
14745f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* thirdAction = */ Entry.ACTION_NONE,
14755f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* thirdExtras = */ null,
14765f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* iconResourceId = */  0);
1477c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            List<Entry> phoneticList = new ArrayList<>();
1478c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            phoneticList.add(phoneticEntry);
1479a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name comes after nickname. Check to see if the first entry type is nickname
1480a0fa4c18dc111ceea9ff47f35ee01fecc1003578Paul Soulos            if (aboutCardEntries.size() > 0 && aboutCardEntries.get(0).get(0).getHeader().equals(
1481a153dba33c2152332c5706178a367f52b0550723Paul Soulos                    getResources().getString(R.string.header_nickname_entry))) {
1482a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(1, phoneticList);
1483a153dba33c2152332c5706178a367f52b0550723Paul Soulos            } else {
1484a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(0, phoneticList);
1485a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
1486c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        }
1487c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos
14886bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (!TextUtils.isEmpty(customAboutCardName)) {
14896bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            mAboutCard.setTitle(customAboutCardName);
14906bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
14916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
14923b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox        mAboutCard.initialize(aboutCardEntries,
14933b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* numInitialVisibleEntries = */ 1,
14943b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* isExpanded = */ true,
14953b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* isAlwaysExpanded = */ true,
14963b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                mExpandingEntryCardViewListener,
14973b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                mScroller);
14986095369885edcca566a812b551886e29c7ff8039Brian Attwell
14996095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
15006095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
15016095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
15026095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
15036095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
15046095369885edcca566a812b551886e29c7ff8039Brian Attwell
1505a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
1506a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
1507a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
1508a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1509a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
1510eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1511eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1512eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1513eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
15146095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
15156095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
15166095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
15176095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
15186095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
15196095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
15206095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
15218a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* subHeader = */ null, /* subHeaderIcon = */ null, /* text = */ null,
15228a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* textIcon = */ null, /* primaryContentDescription = */ null,
152323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                getEditContactIntent(),
1524dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
1525714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
152648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* isEditable = */ false, /* EntryContextMenuInfo = */ null,
152748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* thirdIcon = */ null, /* thirdIntent = */ null,
15285f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                /* thirdContentDescription = */ null,
15295f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                /* thirdAction = */ Entry.ACTION_NONE,
15305f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                /* thirdExtras = */ null,
15315f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                R.drawable.ic_phone_24dp);
15326095369885edcca566a812b551886e29c7ff8039Brian Attwell
15336095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
15346095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
15356095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
15366095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
15378a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* subHeaderIcon = */ null,
15388a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* text = */ null, /* textIcon = */ null, /* primaryContentDescription = */ null,
153923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                getEditContactIntent(), /* alternateIcon = */ null,
1540dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
15412a4207fb39330e840436215c896cde911489e111Paul Soulos                /* shouldApplyColor = */ true, /* isEditable = */ false,
154248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* EntryContextMenuInfo = */ null, /* thirdIcon = */ null,
154348290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                /* thirdIntent = */ null, /* thirdContentDescription = */ null,
15445f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                /* thirdAction = */ Entry.ACTION_NONE, /* thirdExtras = */ null,
154548290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                R.drawable.ic_email_24dp);
15466095369885edcca566a812b551886e29c7ff8039Brian Attwell
15476095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
15486095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
15496095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
15506095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
15516095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
15526095369885edcca566a812b551886e29c7ff8039Brian Attwell
15536095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
15546095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
15556095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
15566095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
1557c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
15580cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener, mScroller);
15596095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
15606095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
15616095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
15626095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
15636095369885edcca566a812b551886e29c7ff8039Brian Attwell
15646095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
1565eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
1566eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
1567eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
1568eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
1569eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
1570eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
15716bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel generateDataModelFromContact(
1572eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
1573eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
1574eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1575eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
15768bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
15778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
1578851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
1579851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
1580eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
1581eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1582851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
1583eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
1584eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
158547b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
158647b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
158747b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
1588eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
1589cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1590eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
1591b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1592eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
1593eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
1594899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
1595eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
1596edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1597eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
1598eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
1599eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
1600eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
1601edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
1602eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
1603edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1604edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
16058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1606edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1607eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
160816339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
160916339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
1610eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
1611eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
1612eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
161316339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
1614eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
1615eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
1616eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
1617eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
1618eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
1619eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
1620eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
1621eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
1622edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1623eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1624edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1625eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
1626eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
1627eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
1628eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
162916339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
16306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.beginSection("cp2 data items to entries");
16316bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16326bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = new ArrayList<>();
16336bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = buildAboutCardEntries(dataItemsMap);
16346bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final MutableString aboutCardName = new MutableString();
16356bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16366bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        for (int i = 0; i < dataItemsList.size(); ++i) {
16376bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> dataItemsByMimeType = dataItemsList.get(i);
16386bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final DataItem topDataItem = dataItemsByMimeType.get(0);
16396bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
16406bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                // About card mimetypes are built in buildAboutCardEntries, skip here
16416bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                continue;
16426bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            } else {
16436bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                List<Entry> contactEntries = dataItemsToEntries(dataItemsList.get(i),
16446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        aboutCardName);
16456bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (contactEntries.size() > 0) {
16466bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    contactCardEntries.add(contactEntries);
16476bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                }
16486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            }
16496bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
16506bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16516bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.endSection();
16526bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16536bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Cp2DataCardModel dataModel = new Cp2DataCardModel();
16546bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.customAboutCardName = aboutCardName.value;
16556bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.aboutCardEntries = aboutCardEntries;
16566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.contactCardEntries = contactCardEntries;
16576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.dataItemsMap = dataItemsMap;
16586bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return dataModel;
16596bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
16606bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16616bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
16626bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * Class used to hold the About card and Contact cards' data model that gets generated
16636bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * on a background thread. All data is from CP2.
16646bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
16656bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class Cp2DataCardModel {
16666bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        /**
16676bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * A map between a mimetype string and the corresponding list of data items. The data items
16686bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * are in sorted order using mWithinMimeTypeDataItemComparator.
16696bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         */
16706bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public Map<String, List<DataItem>> dataItemsMap;
16716bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> aboutCardEntries;
16726bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> contactCardEntries;
16736bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String customAboutCardName;
16746bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
16756bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
16766bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class MutableString {
16776bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String value;
1678eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1679edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1680eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
1681eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
1682eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
16836bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
16846bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * This runs on a background thread. This is set as static to avoid accidentally adding
16856bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * additional dependencies on unsafe things (like the Activity).
16866bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
1687eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
16886a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * @param secondDataItem A second {@link DataItem} to help build a full entry for some
16896a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     *  mimetypes
1690eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
1691eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
16926a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static Entry dataItemToEntry(DataItem dataItem, DataItem secondDataItem,
16936bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            Context context, Contact contactData,
16946bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final MutableString aboutCardName) {
1695eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
1696eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
1697eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
1698eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
1699eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
1700eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
170123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        StringBuilder primaryContentDescription = new StringBuilder();
1702b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang        Spannable phoneContentDescription = null;
1703eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
170448ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
1705dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
1706dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
170723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        StringBuilder alternateContentDescription = new StringBuilder();
1708eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
17092a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo entryContextMenuInfo = null;
171048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Drawable thirdIcon = null;
171148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Intent thirdIntent = null;
17125f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn        int thirdAction = Entry.ACTION_NONE;
171348fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        String thirdContentDescription = null;
17145f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn        Bundle thirdExtras = null;
171548290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos        int iconResourceId = 0;
1716eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
17176bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        context = context.getApplicationContext();
171823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        final Resources res = context.getResources();
1719eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
1720eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1721eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
1722eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
17236bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            intent = ContactsUtils.buildImIntent(context, im).first;
1724eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
17257de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            final int protocol;
17267de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (!im.isProtocolValid()) {
17277de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = Im.PROTOCOL_CUSTOM;
17287de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
17297de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
17307de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
17317de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (protocol == Im.PROTOCOL_CUSTOM) {
17327de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // If the protocol is custom, display the "IM" entry header as well to distinguish
17337de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // this entry from other ones
173423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = res.getString(R.string.header_im_entry);
173523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                subHeader = Im.getProtocolLabel(res, protocol,
17367de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
17377de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                text = im.getData();
17387de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
173923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = Im.getProtocolLabel(res, protocol,
17407de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
17417de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = im.getData();
17427de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
174397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(im.getData(), header,
174497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1745eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
1746eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
174723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_organization_entry);
1748eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
174997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
175097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1751eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
1752eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
1753eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
1754eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
1755eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
17566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                (contactData.getNameRawContactId() == dataItem.getRawContactId());
1757eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1758eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
1759eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
17606bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                && contactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
1761eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1762eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
176323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = res.getString(R.string.header_nickname_entry);
1764eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
176597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
176697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1767eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1768eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
1769eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
177023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_note_entry);
1771eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
177297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
177397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1774eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
1775eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
177623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_website_entry);
1777eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
177897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
177997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1780eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
17813bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                final WebAddress webAddress = new WebAddress(website.buildDataStringForDisplay
17823bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                        (context, kind));
1783eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1784eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
17853bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                Log.e(TAG, "Couldn't parse website: " + website.buildDataStringForDisplay(
17863bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                        context, kind));
1787eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1788eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1789eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
17903bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell            final String dataString = event.buildDataStringForDisplay(context, kind);
1791eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1792eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1793eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1794eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1795eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1796eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1797eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1798eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1799eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
180023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_event_entry);
1801f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
180223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                subHeader = Event.getTypeLabel(res, event.getKindTypeColumn(kind),
18037de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        event.getLabel()).toString();
1804f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
18056bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            text = DateUtils.formatDate(context, dataString);
180697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(text, header,
180797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1808eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1809eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
18103bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell            final String dataString = relation.buildDataStringForDisplay(context, kind);
1811eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1812eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1813eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1814eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1815eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
181623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_relation_entry);
1817eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
181897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
181997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1820f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
182123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                text = Relation.getTypeLabel(res,
18226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        relation.getKindTypeColumn(kind),
1823f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1824f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1825eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1826eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
18275f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn            String phoneLabel = null;
1828eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
182923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.call_other)).append(" ");
18303bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                header = sBidiFormatter.unicodeWrap(phone.buildDataStringForDisplay(context, kind),
1831c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell                        TextDirectionHeuristics.LTR);
18322a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
183397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.phoneLabelsGroup), dataItem.getMimeType(),
183497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1835f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
183601b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                    final int kindTypeColumn = phone.getKindTypeColumn(kind);
183701b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                    final String label = phone.getLabel();
18385f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    phoneLabel = label;
183901b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                    if (kindTypeColumn == Phone.TYPE_CUSTOM && TextUtils.isEmpty(label)) {
184001b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                        text = "";
184101b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                    } else {
184201b376805a56281f4b18e041e9676c8ee1ecd235Tingting Wang                        text = Phone.getTypeLabel(res, kindTypeColumn, label).toString();
18435f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                        phoneLabel= text;
18443df785b0ad0c0292adfc03a4f804d1b38053de63Tingting Wang                        primaryContentDescription.append(text).append(" ");
18453df785b0ad0c0292adfc03a4f804d1b38053de63Tingting Wang                    }
1846f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
184723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
1848b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                phoneContentDescription = com.android.contacts.common.util.ContactDisplayUtils
1849b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                        .getTelephoneTtsSpannable(primaryContentDescription.toString(), header);
185023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_phone_24dp);
185148290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_phone_24dp;
18526bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (PhoneCapabilityTester.isPhone(context)) {
1853eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
185416339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1855dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
18561cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                        Uri.fromParts(ContactsUtils.SCHEME_SMSTO, phone.getNumber(), null));
185748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
185823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateIcon = res.getDrawable(R.drawable.ic_message_24dp);
185923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(res.getString(R.string.sms_custom, header));
186048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
18615f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                if (CallUtil.isCallWithSubjectSupported(context)) {
18625f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdIcon = res.getDrawable(R.drawable.ic_call_note_white_24dp);
18635f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdAction = Entry.ACTION_CALL_WITH_SUBJECT;
18645f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdContentDescription =
18655f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            res.getString(R.string.call_with_a_note);
18665f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn
18675f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    // Create a bundle containing the data the call subject dialog requires.
18685f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras = new Bundle();
18695f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putLong(CallSubjectDialog.ARG_PHOTO_ID,
18705f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            contactData.getPhotoId());
18715f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putParcelable(CallSubjectDialog.ARG_PHOTO_URI,
18725f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            UriUtils.parseUriOrNull(contactData.getPhotoUri()));
18735f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putParcelable(CallSubjectDialog.ARG_CONTACT_URI,
18745f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            contactData.getLookupUri());
18755f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putString(CallSubjectDialog.ARG_NAME_OR_NUMBER,
18765f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            contactData.getDisplayName());
18775f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putBoolean(CallSubjectDialog.ARG_IS_BUSINESS, false);
18785f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putString(CallSubjectDialog.ARG_NUMBER,
18795f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            phone.getNumber());
18805f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putString(CallSubjectDialog.ARG_DISPLAY_NUMBER,
18815f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            phone.getFormattedPhoneNumber());
18825f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdExtras.putString(CallSubjectDialog.ARG_NUMBER_LABEL,
18835f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                            phoneLabel);
18845f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                } else if (CallUtil.isVideoEnabled(context)) {
18855f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    // Add video call button if supported
188623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    thirdIcon = res.getDrawable(R.drawable.ic_videocam);
18875f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    thirdAction = Entry.ACTION_INTENT;
188848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdIntent = CallUtil.getVideoCallIntent(phone.getNumber(),
188948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                            CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY);
189048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdContentDescription =
189123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            res.getString(R.string.description_video_call);
189248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                }
1893eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1894eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1895eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1896eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1897eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
189823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.email_other)).append(" ");
18991cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                final Uri mailUri = Uri.fromParts(ContactsUtils.SCHEME_MAILTO, address, null);
1900eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1901eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
19022a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
190397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.emailLabelsGroup), dataItem.getMimeType(),
190497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1905f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
190623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = Email.getTypeLabel(res, email.getKindTypeColumn(kind),
1907f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
190823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1909f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
191023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
191123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_email_24dp);
191248290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_email_24dp;
1913eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1914eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1915eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1916eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1917eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
191823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.map_other)).append(" ");
1919eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1920eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
19212a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
192297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.postalLabelsGroup), dataItem.getMimeType(),
192397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1924f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
192523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = StructuredPostal.getTypeLabel(res,
1926f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
192723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1928f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
192923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
19306a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIntent =
19316a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                        StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
193223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateIcon = res.getDrawable(R.drawable.ic_directions_24dp);
193323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(res.getString(
193423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                        R.string.content_description_directions)).append(" ").append(header);
193523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_place_24dp);
193648290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_place_24dp;
1937eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1938eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
1939593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1940593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            final String address = sip.getSipAddress();
1941593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            if (!TextUtils.isEmpty(address)) {
1942593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                primaryContentDescription.append(res.getString(R.string.call_other)).append(
1943593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        " ");
1944593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                if (PhoneCapabilityTester.isSipPhone(context)) {
19451cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                    final Uri callUri = Uri.fromParts(PhoneAccount.SCHEME_SIP, address, null);
1946eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
194716339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1948593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                header = address;
1949593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                entryContextMenuInfo = new EntryContextMenuInfo(header,
1950593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        res.getString(R.string.phoneLabelsGroup), dataItem.getMimeType(),
1951593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        dataItem.getId(), dataItem.isSuperPrimary());
1952593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                if (sip.hasKindTypeColumn(kind)) {
1953593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                    text = SipAddress.getTypeLabel(res,
1954593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                            sip.getKindTypeColumn(kind), sip.getLabel()).toString();
1955593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                    primaryContentDescription.append(text).append(" ");
1956593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                }
1957593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                primaryContentDescription.append(header);
1958593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                icon = res.getDrawable(R.drawable.ic_dialer_sip_black_24dp);
1959593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                iconResourceId = R.drawable.ic_dialer_sip_black_24dp;
1960edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1961eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
19628025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            // If the name is already set and this is not the super primary value then leave the
19638025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            // current value. This way we show the super primary value when we are able to.
19648025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            if (dataItem.isSuperPrimary() || aboutCardName.value == null
19658025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    || aboutCardName.value.isEmpty()) {
19668025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
19678025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                if (!TextUtils.isEmpty(givenName)) {
19688025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    aboutCardName.value = res.getString(R.string.about_card_title) +
19698025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                            " " + givenName;
19708025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                } else {
19718025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    aboutCardName.value = res.getString(R.string.about_card_title);
19728025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                }
1973eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1974eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1975eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
19766bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = dataItem.buildDataStringForDisplay(context, kind);
1977eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1978eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
197960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
198060e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
1981e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1982e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
1983e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
1984e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
19856a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                // Build advanced entry for known 3p types. Otherwise default to ResolveCache icon.
1986e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
1987e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
19886a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // If a secondDataItem is available, use it to build an entry with
19896a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // alternate actions
19906a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        if (secondDataItem != null) {
199123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
19926a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            alternateIcon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
19936a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            final GPlusOrHangoutsDataItemModel itemModel =
19946a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    new GPlusOrHangoutsDataItemModel(intent, alternateIntent,
19956a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            dataItem, secondDataItem, alternateContentDescription,
19966a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            header, text, context);
19976a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
199897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            populateGPlusOrHangoutsDataItemModel(itemModel);
199997b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            intent = itemModel.intent;
200097b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateIntent = itemModel.alternateIntent;
200197b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateContentDescription = itemModel.alternateContentDescription;
200297b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            header = itemModel.header;
200397b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            text = itemModel.text;
20046a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        } else {
20056a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            if (GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(
20066a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    intent.getDataString())) {
20076a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
20086a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            } else {
20096a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
20106a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            }
2011e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
2012e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
2013e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
20146a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // If a secondDataItem is available, use it to build an entry with
20156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // alternate actions
20166a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        if (secondDataItem != null) {
201723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            icon = res.getDrawable(R.drawable.ic_hangout_24dp);
20186a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            alternateIcon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
201997b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            final GPlusOrHangoutsDataItemModel itemModel =
20206a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    new GPlusOrHangoutsDataItemModel(intent, alternateIntent,
20216a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            dataItem, secondDataItem, alternateContentDescription,
20226a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            header, text, context);
20236a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
202497b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            populateGPlusOrHangoutsDataItemModel(itemModel);
202597b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            intent = itemModel.intent;
202697b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateIntent = itemModel.alternateIntent;
202797b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateContentDescription = itemModel.alternateContentDescription;
202897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            header = itemModel.header;
202997b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            text = itemModel.text;
20306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        } else {
20316a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            if (HANGOUTS_DATA_5_VIDEO.equals(intent.getDataString())) {
20326a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
20336a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            } else {
20346a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_hangout_24dp);
20356a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            }
2036e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
2037e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
2038e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
203997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        entryContextMenuInfo = new EntryContextMenuInfo(header, mimetype,
204097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                                dataItem.getMimeType(), dataItem.getId(),
204197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                                dataItem.isSuperPrimary());
20426bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        icon = ResolveCache.getInstance(context).getIcon(
2043e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
2044e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
2045e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
2046e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
2047e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
204848ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
2049e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
2050e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
2051eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
2052b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2053eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
2054eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
20556bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, intent)) {
2056eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
2057eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
2058eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
2059eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
2060dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
2061dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
20626bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, alternateIntent)) {
2063dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
2064f0d04c11b649e953177e79722d05fa048e53273dJay Shrauner            } else if (TextUtils.isEmpty(alternateContentDescription)) {
2065f0d04c11b649e953177e79722d05fa048e53273dJay Shrauner                // Attempt to use package manager to find a suitable content description if needed
206623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(getIntentResolveLabel(alternateIntent, context));
2067dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
2068dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
2069dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
2070eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
2071eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
2072eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
2073eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
2074eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
2075eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
2076fa238949855fba3b28a57fad3d585b13e80362bbBrian Attwell        // Ignore dataIds from the Me profile.
2077ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
2078ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
2079ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
208023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon,
2081b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                phoneContentDescription == null
2082b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                        ? new SpannableString(primaryContentDescription.toString())
2083b6949dc7ed1b0ab47dc6a39b3ae0d42e029be2b4Tingting Wang                        : phoneContentDescription,
20847ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jang                intent, alternateIcon, alternateIntent,
208523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.toString(), shouldApplyColor, isEditable,
20865f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription, thirdAction,
20875f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                thirdExtras, iconResourceId);
2088eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
2089eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
20906bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<Entry> dataItemsToEntries(List<DataItem> dataItems,
20916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            MutableString aboutCardTitleOut) {
20926a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Hangouts and G+ use two data items to create one entry.
20936a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        if (dataItems.get(0).getMimeType().equals(MIMETYPE_GPLUS_PROFILE) ||
20946a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataItems.get(0).getMimeType().equals(MIMETYPE_HANGOUTS)) {
20956a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            return gPlusOrHangoutsDataItemsToEntries(dataItems);
20966a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        } else {
20976a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            final List<Entry> entries = new ArrayList<>();
20986a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            for (DataItem dataItem : dataItems) {
20996a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                final Entry entry = dataItemToEntry(dataItem, /* secondDataItem = */ null,
21006a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        this, mContactData, aboutCardTitleOut);
21016a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                if (entry != null) {
21026a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    entries.add(entry);
21036a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
21046a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            }
21056a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            return entries;
21066a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
21076a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
21086a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
21096a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    /**
21106a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * G+ and Hangout entries are unique in that a single ExpandingEntryCardView.Entry consists
21116a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * of two data items. This method attempts to build each entry using the two data items if
21126a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * they are available. If there are more or less than two data items, a fall back is used
21136a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * and each data item gets its own entry.
21146a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     */
21156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private List<Entry> gPlusOrHangoutsDataItemsToEntries(List<DataItem> dataItems) {
2116eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
21176a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        final Map<Long, List<DataItem>> buckets = new HashMap<>();
21186a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Put the data items into buckets based on the raw contact id
2119eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
21206a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            List<DataItem> bucket = buckets.get(dataItem.getRawContactId());
21216a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            if (bucket == null) {
21226a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                bucket = new ArrayList<>();
21236a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                buckets.put(dataItem.getRawContactId(), bucket);
21246a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            }
21256a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            bucket.add(dataItem);
21266a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
21276a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
21286a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Use the buckets to build entries. If a bucket contains two data items, build the special
21296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // entry, otherwise fall back to the normal entry.
21306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        for (List<DataItem> bucket : buckets.values()) {
21316a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            if (bucket.size() == 2) {
21326a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                // Use the pair to build an entry
21336a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                final Entry entry = dataItemToEntry(bucket.get(0),
21346a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        /* secondDataItem = */ bucket.get(1), this, mContactData,
21356a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        /* aboutCardName = */ null);
21366a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                if (entry != null) {
21376a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    entries.add(entry);
21386a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
21396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            } else {
21406a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                for (DataItem dataItem : bucket) {
21416a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    final Entry entry = dataItemToEntry(dataItem, /* secondDataItem = */ null,
21426a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            this, mContactData, /* aboutCardName = */ null);
21436a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    if (entry != null) {
21446a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        entries.add(entry);
21456a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    }
21466a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
2147eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
2148eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
2149eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
2150edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
2151edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
21526a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    /**
21536a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * Used for statically passing around G+ or Hangouts data items and entry fields to
21546a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * populateGPlusOrHangoutsDataItemModel.
21556a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     */
21566a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final class GPlusOrHangoutsDataItemModel {
21576a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Intent intent;
21586a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Intent alternateIntent;
21596a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public DataItem dataItem;
21606a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public DataItem secondDataItem;
21616a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public StringBuilder alternateContentDescription;
21626a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public String header;
21636a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public String text;
21646a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Context context;
21656a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
21666a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public GPlusOrHangoutsDataItemModel(Intent intent, Intent alternateIntent, DataItem dataItem,
21676a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                DataItem secondDataItem, StringBuilder alternateContentDescription, String header,
21686a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                String text, Context context) {
21696a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.intent = intent;
21706a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.alternateIntent = alternateIntent;
21716a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.dataItem = dataItem;
21726a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.secondDataItem = secondDataItem;
21736a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.alternateContentDescription = alternateContentDescription;
21746a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.header = header;
21756a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.text = text;
21766a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.context = context;
21776a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
21786a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
21796a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
21806a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static void populateGPlusOrHangoutsDataItemModel(
21816a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            GPlusOrHangoutsDataItemModel dataModel) {
21826a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        final Intent secondIntent = new Intent(Intent.ACTION_VIEW);
21836a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        secondIntent.setDataAndType(ContentUris.withAppendedId(Data.CONTENT_URI,
21846a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.secondDataItem.getId()), dataModel.secondDataItem.getMimeType());
21856a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // There is no guarantee the order the data items come in. Second
21866a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // data item does not necessarily mean it's the alternate.
21876a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Hangouts video and Add to circles should be alternate. Swap if needed
21886a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        if (HANGOUTS_DATA_5_VIDEO.equals(
21896a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.dataItem.getContentValues().getAsString(Data.DATA5)) ||
21906a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(
21916a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        dataModel.dataItem.getContentValues().getAsString(Data.DATA5))) {
21926a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateIntent = dataModel.intent;
21936a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateContentDescription = new StringBuilder(dataModel.header);
21946a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
21956a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.intent = secondIntent;
21966a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.header = dataModel.secondDataItem.buildDataStringForDisplay(dataModel.context,
21976a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    dataModel.secondDataItem.getDataKind());
21986a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.text = dataModel.secondDataItem.getDataKind().typeColumn;
21996a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        } else if (HANGOUTS_DATA_5_MESSAGE.equals(
22006a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.dataItem.getContentValues().getAsString(Data.DATA5)) ||
22016a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                GPLUS_PROFILE_DATA_5_VIEW_PROFILE.equals(
22026a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        dataModel.dataItem.getContentValues().getAsString(Data.DATA5))) {
22036a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateIntent = secondIntent;
22046a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateContentDescription = new StringBuilder(
22056a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    dataModel.secondDataItem.buildDataStringForDisplay(dataModel.context,
22066a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            dataModel.secondDataItem.getDataKind()));
22076a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
22086a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
22096a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
22106bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static String getIntentResolveLabel(Intent intent, Context context) {
22116bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent,
2212dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
2213dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
2214dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
2215dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
2216dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
2217dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
2218dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
2219dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
22206bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            bestResolve = ResolveCache.getInstance(context).getBestResolve(intent, matches);
2221dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
2222dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
2223dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
2224dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
2225dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
2226dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
22276bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return String.valueOf(bestResolve.loadLabel(context.getPackageManager()));
2228dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
2229dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
2230edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
223131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
223231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
223331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
223431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
223531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
223631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
223731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
223831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
223931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
2240faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
224131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
2242faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
2243faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
2244929a62f5c1bcdc3773270f7d189d88c0464b36adJay Shrauner                if (imageViewDrawable instanceof BitmapDrawable && mContactData != null
224595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData() != null
224695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData().length > 0) {
224795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // Perform the color analysis on the thumbnail instead of the full sized
224895c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // image, so that our results will be as similar as possible to the Bugle
224995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // app.
225095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    final Bitmap bitmap = BitmapFactory.decodeByteArray(
225195c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData(), 0,
225295c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData().length);
225395c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    try {
225495c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        final int primaryColor = colorFromBitmap(bitmap);
225595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        if (primaryColor != 0) {
225695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
225795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                                    primaryColor);
225895c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        }
225995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    } finally {
226095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        bitmap.recycle();
2261faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
22628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
22638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
2264faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
2265a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
226631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
2267a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
226831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
226931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
227031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
2271faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
2272faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
22738571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
22748571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
22758571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
22768571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
22778571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
22788571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
22798571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
22808571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
22818571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
22828571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
22838571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
22848571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
2285faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
228631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
228731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
228831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
228931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
229031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
2291faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
22929b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
22938e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mColorFilterColor = palette.mPrimaryColor;
22948e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mScroller.setHeaderTintColor(mColorFilterColor);
2295faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
22969b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
22978571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
22989b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
22998e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell                new PorterDuffColorFilter(mColorFilterColor, PorterDuff.Mode.SRC_ATOP);
23008e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mContactCard.setColorAndFilter(mColorFilterColor, mColorFilter);
23018e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mRecentCard.setColorAndFilter(mColorFilterColor, mColorFilter);
23028e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mAboutCard.setColorAndFilter(mColorFilterColor, mColorFilter);
23039b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
23049b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
23058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
23068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller == null) {
23078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
23088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
23098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
23108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
23118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
23128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
23138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
23148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
23158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
23168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
2317847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
2318847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell                getWindow().getStatusBarColor(), desiredStatusBarColor);
2319847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
2320847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setEvaluator(new ArgbEvaluator());
2321847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.start();
23228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
23238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
23248a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
23258a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
23268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
23278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
2328a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
2329a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
23308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
23318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
23328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
23338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
2334b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
2335eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
2336b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
2337ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            if (interaction == null) {
2338ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                continue;
2339ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            }
2340ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
2341ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
2342b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
2343b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
2344b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
2345b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
2346b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
234723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    interaction.getContentDescription(this),
2348b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
2349dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
2350dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
2351dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
235248ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
23532a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
235448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* EntryContextMenuInfo = */ null,
235548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
235648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
235748290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* thirdContentDescription = */ null,
23585f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* thirdAction = */ Entry.ACTION_NONE,
23595f87e923b0300173ad68f73a47b0dca7348d6a1fTyler Gunn                    /* thirdActionExtras = */ null,
236048290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    interaction.getIconResourceId()));
2361b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2362b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
2363b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
2364b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2365eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
2366851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
2367cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
2368851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
2369405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
2370cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
2371cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2372cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
2373851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
23748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
2375930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            try {
23768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
2377930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (isFinishing()) {
2378930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    return;
2379930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                }
2380930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (data.isError()) {
238102ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    // This means either the contact is invalid or we had an
238202ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    // internal error such as an acore crash.
238302ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    Log.i(TAG, "Failed to load contact: " + ((ContactLoader)loader).getLookupUri());
238402ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
238502ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                            Toast.LENGTH_LONG).show();
238602ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    finish();
238702ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    return;
2388930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                }
2389930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (data.isNotFound()) {
23908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
23918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
23928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
2393930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    finish();
2394930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    return;
23958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
2396cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2397930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                bindContactData(data);
2398cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2399930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            } finally {
2400930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                Trace.endSection();
2401930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            }
2402cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
2403cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2404cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
2405851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
2406cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
2407cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
2408cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
2409d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
2410d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
2411b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
2412d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
24138571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
2414cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
2415cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
2416b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2417b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
2418b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
2419b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
24208477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
24218477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
24228477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
2423b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
2424b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
2425b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
2426b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
2427b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
24288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
24298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
24308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
24318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
24328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
24338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
24348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
24358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
2436eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
2437b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
2438b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2439b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2440b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
2441b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
2442b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
2443b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
2444b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
2445b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
2446ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
2447b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
2448b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
2449899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
2450ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
2451ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
2452ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
2453ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
2454ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
2455899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
2456899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
2457ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
2458899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
2459899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
2460899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
2461899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
2462899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
2463ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
2464ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
2465ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
2466ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
2467ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
2468b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
2469b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
2470b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2471b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2472b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2473b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
2474b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
2475b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
2476b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2477b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
2478b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
2479b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
2480b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2481b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2482b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2483b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
2484b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
2485b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2486b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
2487b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2488b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
2489b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
2490b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
2491b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2492b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
2493eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
249430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        final List<List<Entry>> interactionsWrapper = new ArrayList<>();
2495b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2496ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        // Serialize mRecentLoaderResults into a single list. This should be done on the main
2497ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        // thread to avoid races against mRecentLoaderResults edits.
2498ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
2499ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            allInteractions.addAll(loaderInteractions);
2500ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        }
2501ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell
250230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        mRecentDataTask = new AsyncTask<Void, Void, Void>() {
2503b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
250430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            protected Void doInBackground(Void... params) {
250530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("sort recent loader results");
250630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
250730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // Sort the interactions by most recent
250830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
250930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    @Override
251030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    public int compare(ContactInteraction a, ContactInteraction b) {
2511ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a == null && b == null) {
2512ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 0;
2513ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2514ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a == null) {
2515ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 1;
2516ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2517ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (b == null) {
2518ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return -1;
2519ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2520ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a.getInteractionDate() > b.getInteractionDate()) {
2521ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return -1;
2522ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2523ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a.getInteractionDate() == b.getInteractionDate()) {
2524ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 0;
2525ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2526ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        return 1;
252730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    }
252830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                });
252930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
253030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
253130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("contactInteractionsToEntries");
253230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
253330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // Wrap each interaction in its own list so that an icon is displayed for each entry
253430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                for (Entry contactInteraction : contactInteractionsToEntries(allInteractions)) {
253530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    List<Entry> entryListWrapper = new ArrayList<>(1);
253630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    entryListWrapper.add(contactInteraction);
253730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    interactionsWrapper.add(entryListWrapper);
253830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
253930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
254030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
254130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                return null;
2542b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
254330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
254430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            @Override
254530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            protected void onPostExecute(Void aVoid) {
254630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                super.onPostExecute(aVoid);
254730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("initialize recents card");
254830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
254930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                if (allInteractions.size() > 0) {
255030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mRecentCard.initialize(interactionsWrapper,
2551b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
2552c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
255330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                            mExpandingEntryCardViewListener, mScroller);
255430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mRecentCard.setVisibility(View.VISIBLE);
255530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
2556eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
255730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
255830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
255930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // About card is initialized along with the contact card, but since it appears after
256030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // the recent card in the UI, we hold off until making it visible until the recent
256130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // card is also ready to avoid stuttering.
256230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                if (mAboutCard.shouldShow()) {
256330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mAboutCard.setVisibility(View.VISIBLE);
256430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                } else {
256530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mAboutCard.setVisibility(View.GONE);
256630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
256730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                mRecentDataTask = null;
256830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            }
256930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        };
257030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        mRecentDataTask.execute();
2571b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
25728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
25738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
25748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
25758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
25768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
25778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
25788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
25798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
25808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
25818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
25828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
25838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
258430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        if (mRecentDataTask != null) {
258530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            mRecentDataTask.cancel(/* mayInterruptIfRunning = */ false);
258630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        }
25878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
258823889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
2589e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    @Override
2590e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    public void onDestroy() {
2591e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        super.onDestroy();
2592e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        if (mAggregationSuggestionEngine != null) {
2593e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang            mAggregationSuggestionEngine.quit();
2594e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang        }
2595e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang    }
2596e3a7c4f3215294b7699c2960ffe0128d3401745fTingting Wang
259723889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
2598d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
2599d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
2600d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
2601d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
2602d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2603d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2604a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    /**
2605a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     * Returns true if it is possible to share the current contact.
2606a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     */
2607a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    private boolean isContactShareable() {
2608a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
2609a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    }
2610a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
26116095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
26125a7a23bdb698b8e741a425c9617c5e33e6314cddWalter Jang        return EditorIntents.createCompactEditContactIntent(
26131e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                mContactData.getLookupUri(),
26141e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                mHasComputedThemeColor
26151e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                        ? new MaterialPalette(mColorFilterColor, mStatusBarColor) : null,
26163e5ae0db65e842d49d047568fb5d6f331298da32Walter Jang                mContactData.getPhotoId());
26176095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
26186095369885edcca566a812b551886e29c7ff8039Brian Attwell
26196095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
2620a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        mHasIntentLaunched = true;
2621c00a0b5370f0714f6af1ea9b3e29b10f25e91af0Zheng Fu        mContactLoader.cacheResult();
26226095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
2623d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2624d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
26255d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang    private void deleteContact() {
26265d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang        final Uri contactUri = mContactData.getLookupUri();
26275d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang        ContactDeletionInteraction.start(this, contactUri, /* finishActivityWhenDone =*/ true);
26285d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang    }
26295d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang
2630d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
2631d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
263263176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        if (mContactData != null) {
2633d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
2634d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
2635d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
2636d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
2637d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2638d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
2639333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
2640d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
2641d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
2642d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2643d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
2644eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
264563176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell                    QuickContactActivity.this, mContactData.getLookupUri(), !isStarred);
2646d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
264735ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell
264835ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            final CharSequence accessibilityText = !isStarred
264935ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    ? getResources().getText(R.string.description_action_menu_add_star)
265035ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    : getResources().getText(R.string.description_action_menu_remove_star);
265135ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // Accessibility actions need to have an associated view. We can't access the MenuItem's
265235ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // underlying view, so put this accessibility action on the root view.
265335ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            mScroller.announceForAccessibility(accessibilityText);
2654d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
2655d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2656d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2657752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
2658752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
2659652936f204b15097d030e870dda5054d8115cbf3Brian Attwell        final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
2660752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
2661552ee569d46ae5c63207a162157171b249243e56Yorke Lee        intent.setType(Contacts.CONTENT_VCARD_TYPE);
26628dcb4ed13da40fc609e6913708c3d730ba4f0574Brian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
2663752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2664752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
2665752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
2666752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
2667752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2668752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
2669a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
2670652936f204b15097d030e870dda5054d8115cbf3Brian Attwell            ImplicitIntentsUtil.startActivityOutsideApp(this, chooseIntent);
2671eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
2672752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
2673752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
2674752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
2675752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2676752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
2677752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
2678752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
2679752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
2680752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
2681752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
2682752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2683752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
2684752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
2685752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
2686752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
2687752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
2688752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
2689752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2690752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
2691752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
2692168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                        final String displayName = mContactData.getDisplayName();
2693168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                        final String toastMessage = TextUtils.isEmpty(displayName)
2694168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                                ? getString(R.string.createContactShortcutSuccessful_NoName)
2695168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                                : getString(R.string.createContactShortcutSuccessful, displayName);
2696168331d85c5be43a816eb24fd37a2ebdbe61ac53Tingting Wang                        Toast.makeText(QuickContactActivity.this, toastMessage,
2697752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
2698752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
2699752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2700752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
270163176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        builder.createContactShortcutIntent(mContactData.getLookupUri());
2702752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
2703752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
270466965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell    private boolean isShortcutCreatable() {
27051c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner        if (mContactData == null || mContactData.isUserProfile() ||
27061c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                mContactData.isDirectoryEntry()) {
27078d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell            return false;
27088d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell        }
270966965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        final Intent createShortcutIntent = new Intent();
271066965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        createShortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
271166965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        final List<ResolveInfo> receivers = getPackageManager()
271266965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell                .queryBroadcastReceivers(createShortcutIntent, 0);
271366965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        return receivers != null && receivers.size() > 0;
271466965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell    }
271566965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell
2716d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2717d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
2718eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
2719d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
2720d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
2721d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2722d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2723d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2724d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
2725d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
2726d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
2727333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
2728d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
2729d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
2730a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
2731d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
2732d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
2733d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
2734d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
2735d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
273630cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
27372e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
2738d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
2739d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
27402e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
2741d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
2742d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
2743d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
2744a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
27455d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            final MenuItem deleteMenuItem = menu.findItem(R.id.menu_delete);
274682ed2b5380e134705f96e7e1f1586a25f8ac403fWalter Jang            deleteMenuItem.setVisible(isContactEditable() && !mContactData.isUserProfile());
27475d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang
2748a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            final MenuItem shareMenuItem = menu.findItem(R.id.menu_share);
2749a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            shareMenuItem.setVisible(isContactShareable());
2750a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
275166965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell            final MenuItem shortcutMenuItem = menu.findItem(R.id.menu_create_contact_shortcut);
275266965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell            shortcutMenuItem.setVisible(isShortcutCreatable());
275366965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell
275456bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            final MenuItem helpMenu = menu.findItem(R.id.menu_help);
275556bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            helpMenu.setVisible(HelpUtils.isHelpAndFeedbackAvailable());
275656bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell
27578a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
2758d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
27598a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
2760d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2761d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2762d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2763d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
2764d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
2765d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
2766d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
2767d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
2768d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
2769d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
2770ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // This action is used to launch the contact selector, with the option of
2771ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // creating a new contact. Creating a new contact is an INSERT, while selecting
2772ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // an exisiting one is an edit. The fields in the edit screen will be
2773ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // prepopulated with data.
2774ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2775ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
2776ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.setType(Contacts.CONTENT_ITEM_TYPE);
2777ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2778fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    ArrayList<ContentValues> values = mContactData.getContentValues();
2779fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell
2780fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    // Only pre-fill the name field if the provided display name is an nickname
2781fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    // or better (e.g. structured name, nickname)
2782fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    if (mContactData.getDisplayNameSource() >= DisplayNameSources.NICKNAME) {
2783ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.NAME, mContactData.getDisplayName());
2784fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    } else if (mContactData.getDisplayNameSource()
2785fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                            == DisplayNameSources.ORGANIZATION) {
2786fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // This is probably an organization. Instead of copying the organization
2787fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // name into a name entry, copy it into the organization entry. This
2788fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // way we will still consider the contact an organization.
2789fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        final ContentValues organization = new ContentValues();
2790fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        organization.put(Organization.COMPANY, mContactData.getDisplayName());
2791fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        organization.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
2792fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        values.add(organization);
2793ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2794fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell
2795ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Last time used and times used are aggregated values from the usage stat
2796ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // table. They need to be removed from data values so the SQL table can insert
2797ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // properly
2798ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    for (ContentValues value : values) {
2799ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.LAST_TIME_USED);
2800ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.TIMES_USED);
2801ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2802ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.putExtra(Intents.Insert.DATA, values);
2803ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2804ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // If the contact can only export to the same account, add it to the intent.
2805ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Otherwise the ContactEditorFragment will show a dialog for selecting an
2806ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // account.
2807ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    if (mContactData.getDirectoryExportSupport() ==
2808ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                            Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY) {
28094a1c574cd62eb7ca1e0fcc3a61e5378e5e0787feBrian Attwell                        intent.putExtra(Intents.Insert.EXTRA_ACCOUNT,
2810ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                new Account(mContactData.getDirectoryAccountName(),
2811ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                        mContactData.getDirectoryAccountType()));
28124a1c574cd62eb7ca1e0fcc3a61e5378e5e0787feBrian Attwell                        intent.putExtra(Intents.Insert.EXTRA_DATA_SET,
2813ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                mContactData.getRawContacts().get(0).getDataSet());
2814ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2815ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2816f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    // Add this flag to disable the delete menu option on directory contact joins
2817f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    // with local contacts. The delete option is ambiguous when joining contacts.
2818f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    intent.putExtra(ContactEditorFragment.INTENT_EXTRA_DISABLE_DELETE_MENU_OPTION,
2819f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                            true);
2820f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos
2821ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    startActivityForResult(intent, REQUEST_CODE_CONTACT_SELECTION_ACTIVITY);
2822d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
2823d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
2824d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
2825d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
2826d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
2827d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
28285d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            case R.id.menu_delete:
28291c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                if (isContactEditable()) {
28301c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                    deleteContact();
28311c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                }
28325d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang                return true;
2833752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
283449de62f06d24653484adebd69123eeff2850a757Jay Shrauner                if (isContactShareable()) {
283549de62f06d24653484adebd69123eeff2850a757Jay Shrauner                    shareContact();
283649de62f06d24653484adebd69123eeff2850a757Jay Shrauner                }
2837752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
2838752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
28391c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                if (isShortcutCreatable()) {
28401c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                    createLauncherShortcutWithContact();
28411c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                }
2842752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
284356bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            case R.id.menu_help:
284456bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell                HelpUtils.launchHelpAndFeedbackForContactScreen(this);
284556bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell                return true;
2846d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
2847d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
2848d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
284923889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
2850edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
2851