QuickContactActivity.java revision d5b686e73dc50d90fd44dc5c738d243a81a6cc31
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;
48edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Email;
49eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Event;
50eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;
51eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Identity;
52eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Im;
53eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Nickname;
54eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Note;
55eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Organization;
56edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Phone;
57eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Relation;
58edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
59edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
60edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Website;
61752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract.Contacts;
6260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulosimport android.provider.ContactsContract.Data;
63ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.provider.ContactsContract.Directory;
64eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.DisplayNameSources;
65ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.provider.ContactsContract.DataUsageFeedback;
66ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.provider.ContactsContract.Intents;
67edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.QuickContact;
68edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.RawContacts;
6931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
700319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.PhoneAccount;
710319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.TelecomManager;
72c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwellimport android.text.BidiFormatter;
737ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jangimport android.text.SpannableString;
74c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwellimport android.text.TextDirectionHeuristics;
75edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
76edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
772a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu;
782a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu.ContextMenuInfo;
79d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
80d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
81eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
82f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chenimport android.view.MotionEvent;
83edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View;
84edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View.OnClickListener;
852a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.View.OnCreateContextMenuListener;
8610d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawaimport android.view.WindowManager;
87edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
88d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
89edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
9072b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
91d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
922d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
93e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
94eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
952a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.common.ClipboardUtils;
96eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
97eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
98bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwellimport com.android.contacts.common.activity.RequestPermissionsActivity;
99d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.editor.SelectAccountDialogFragment;
10070e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chenimport com.android.contacts.common.interactions.TouchPointManager;
10131b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport com.android.contacts.common.lettertiles.LetterTileDrawable;
102752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder;
103752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
1040d5588da244d0992c3ff8f25d0875fdf95a8c644Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
105cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.Contact;
106cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.ContactLoader;
107cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.RawContact;
108428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.AccountType;
109d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.model.account.AccountWithDataSet;
110cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.DataItem;
111428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.dataitem.DataKind;
112cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.EmailDataItem;
113eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.EventDataItem;
114cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.ImDataItem;
115eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NicknameDataItem;
116eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NoteDataItem;
117eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.OrganizationDataItem;
118b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.common.model.dataitem.PhoneDataItem;
119eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.RelationDataItem;
120eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.SipAddressDataItem;
121eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredNameDataItem;
122eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredPostalDataItem;
123eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.WebsiteDataItem;
124c6100ffd22ae176a3e84a1062d8cb92d955faef2Brian Attwellimport com.android.contacts.common.util.ImplicitIntentsUtil;
125eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.util.DateUtils;
126faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils;
127faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
128d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwellimport com.android.contacts.common.util.ViewUtil;
129333091ae754ddfc25714c14b9b89534be24379f9Paul Soulosimport com.android.contacts.detail.ContactDisplayUtils;
130f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulosimport com.android.contacts.editor.ContactEditorFragment;
1315a7a23bdb698b8e741a425c9617c5e33e6314cddWalter Jangimport com.android.contacts.editor.EditorIntents;
132899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
133ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
135b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
136b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1372d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
1382a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryContextMenuInfo;
1392ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryTag;
140e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
141493f8d14b1d974a2939a110ce89fa68e0c915c36Brian Attwellimport com.android.contacts.quickcontact.WebAddress.ParseException;
142e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
143eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
144b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
145eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
146b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
147b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
14802eaf11669d25fd885606b3b0700358323532cddBrian Attwellimport com.android.contacts.widget.QuickContactImageView;
14956bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwellimport com.android.contactsbind.HelpUtils;
15056bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell
151e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
152e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
15382a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shraunerimport java.lang.SecurityException;
1542d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
155899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
156eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
157b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
158b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
159eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
160edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
161edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
162b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
16312ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shraunerimport java.util.concurrent.ConcurrentHashMap;
164edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
165edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
166edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
167edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
168edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
169edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
170d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellpublic class QuickContactActivity extends ContactsActivity {
1718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
1738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
1748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
1758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
1768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
1778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
1788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
179edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
180edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1819b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
1829b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1839b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
1848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
1850b267fec5cb5b2d836057b16d0085be349be5243Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xC8, 0, 0, 0);
186ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos    private static final int REQUEST_CODE_CONTACT_SELECTION_ACTIVITY = 2;
187ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
188dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
189752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
190752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
191752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
192edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
193edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
194edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
195edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
196e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
197e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
1986a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE = "addtocircle";
1996a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String GPLUS_PROFILE_DATA_5_VIEW_PROFILE = "view";
200e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
201e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
2026a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String HANGOUTS_DATA_5_VIDEO = "hangout";
2036a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String HANGOUTS_DATA_5_MESSAGE = "conversation";
20448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos    private static final String CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY =
20548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos            "com.android.contacts.quickcontact.QuickContactActivity";
206e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
20763176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell    /**
20863176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * The URI used to load the the Contact. Once the contact is loaded, use Contact#getLookupUri()
20963176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * instead of referencing this URI.
21063176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     */
211edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
212edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
2138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
21405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell    private String mExtraPrioritizedMimeType;
2158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
2168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
21797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private boolean mOnlyOnePhoneNumber;
21897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private boolean mOnlyOneEmail;
219edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
22002eaf11669d25fd885606b3b0700358323532cddBrian Attwell    private QuickContactImageView mPhotoView;
221eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
2226095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
223b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
224eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
225b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
226d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
2276bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private AsyncTask<Void, Void, Cp2DataCardModel> mEntriesAndActionsTask;
22830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell    private AsyncTask<Void, Void, Void> mRecentDataTask;
2296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
2306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * The last copy of Cp2DataCardModel that was passed to {@link #populateContactAndAboutCard}.
2316bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
2326bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel mCachedCp2DataCardModel;
233f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    /**
234f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  This scrim's opacity is controlled in two different ways. 1) Before the initial entrance
235f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  animation finishes, the opacity is animated by a value animator. This is designed to
236f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  distract the user from the length of the initial loading time. 2) After the initial
237f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  entrance animation, the opacity is directly related to scroll position.
238f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     */
2399b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
240f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    private boolean mIsEntranceAnimationFinished;
241a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
2428477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
2438571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
244edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
245a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    /**
246a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * Used to stop the ExpandingEntry cards from adjusting between an entry click and the intent
247a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * being launched.
248a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     */
249a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    private boolean mHasIntentLaunched;
250a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
2516cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
2529815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
2539b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
2548e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell    private int mColorFilterColor;
2559815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
2569758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
2579758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
258edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
259cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
260edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
261edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
262edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
263edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
264edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
265cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
266405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
267edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
268a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private static final List<String> SORTED_ABOUT_CARD_MIMETYPES = Lists.newArrayList(
269a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Nickname.CONTENT_ITEM_TYPE,
270a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name is inserted after nickname if it is available.
271a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // No mimetype for phonetic name exists.
272a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Website.CONTENT_ITEM_TYPE,
273a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Organization.CONTENT_ITEM_TYPE,
274a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Event.CONTENT_ITEM_TYPE,
275a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Relation.CONTENT_ITEM_TYPE,
276a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Im.CONTENT_ITEM_TYPE,
277a153dba33c2152332c5706178a367f52b0550723Paul Soulos            GroupMembership.CONTENT_ITEM_TYPE,
278a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Identity.CONTENT_ITEM_TYPE,
279a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Note.CONTENT_ITEM_TYPE);
280eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
281c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell    private static final BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
282c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell
283b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
284b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
285b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
286ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
287ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
288ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
289b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
290b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
291b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
292ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
293ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
294899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
295ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
296ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
297899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
298899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
299899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
30040d7a65c6e68967f3f486715b194c144d93c296fPaul Soulos            1L * 24L * 60L * 60L * 1000L /* 1 day */;
301899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
30240d7a65c6e68967f3f486715b194c144d93c296fPaul Soulos            7L * 24L * 60L * 60L * 1000L /* 7 days */;
303899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
304ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
305ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
306ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
3076095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
3086095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
3096095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
310ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
311ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
312ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
313ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
314ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
315ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
31612ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner    /**
31712ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * ConcurrentHashMap constructor params: 4 is initial table size, 0.9f is
31812ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * load factor before resizing, 1 means we only expect a single thread to
31912ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * write to the map so make only a single shard
32012ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     */
32112ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults =
32212ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner        new ConcurrentHashMap<>(4, 0.9f, 1);
323b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
324d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
3256cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
3262d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
3272d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
3282d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
3292ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Object entryTagObject = v.getTag();
3302ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (entryTagObject == null || !(entryTagObject instanceof EntryTag)) {
3312ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                Log.w(TAG, "EntryTag was not used correctly");
3326095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
3336095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
3342ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final EntryTag entryTag = (EntryTag) entryTagObject;
3352ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Intent intent = entryTag.getIntent();
3362ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final int dataId = entryTag.getId();
3372ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos
3382ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
3392ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                editContact();
3402d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
3412d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
342ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
34319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            // Pass the touch point through the intent for use in the InCallUI
34419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            if (Intent.ACTION_CALL.equals(intent.getAction())) {
34519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                if (TouchPointManager.getInstance().hasValidPoint()) {
34619c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    Bundle extras = new Bundle();
34719c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    extras.putParcelable(TouchPointManager.TOUCH_POINT,
34819c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                            TouchPointManager.getInstance().getPoint());
34919c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                    intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
35019c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                }
35119c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            }
35219c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
35319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
35419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
35519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            mHasIntentLaunched = true;
35619c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            try {
35719c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                startActivity(intent);
35819c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            } catch (SecurityException ex) {
35919c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
36019c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        Toast.LENGTH_SHORT).show();
36119c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Log.e(TAG, "QuickContacts does not have permission to launch "
36219c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        + intent);
36319c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            } catch (ActivityNotFoundException ex) {
36419c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
36519c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO                        Toast.LENGTH_SHORT).show();
36619c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO            }
36719c83e3f0f88c8598ab174660ae7a844d7d13df1LIANGTAO GAO
368ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
369ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
370ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
371ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
37220bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            final Uri intentUri = intent.getData();
37320bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            if ((intentUri != null && intentUri.getScheme() != null &&
3741cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                    intentUri.getScheme().equals(ContactsUtils.SCHEME_SMSTO)) ||
375ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
376ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
377ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
378ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
379ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
380ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
38120bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                final Uri dataUsageUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
382ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
383ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
384ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
3859881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                try {
3869881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    final boolean successful = getContentResolver().update(
3879881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                            dataUsageUri, new ContentValues(), null, null) > 0;
3889881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    if (!successful) {
3899881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                        Log.w(TAG, "DataUsageFeedback increment failed");
3909881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    }
3919881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                } catch (SecurityException ex) {
3929881882e769fbf66d728159322e1d8b5e572e36cJay Shrauner                    Log.w(TAG, "DataUsageFeedback increment failed", ex);
393ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
394ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
395ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
396ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
397ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
39870e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            // Pass the touch point through the intent for use in the InCallUI
39970e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            if (Intent.ACTION_CALL.equals(intent.getAction())) {
4001f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                if (TouchPointManager.getInstance().hasValidPoint()) {
40170e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                    Bundle extras = new Bundle();
4021f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                    extras.putParcelable(TouchPointManager.TOUCH_POINT,
4031f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                            TouchPointManager.getInstance().getPoint());
4040319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunn                    intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
40570e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                }
40670e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            }
40770e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen
408793e73c859ed839c28cc37d80d62bde0a361c78aPaul Soulos            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
409793e73c859ed839c28cc37d80d62bde0a361c78aPaul Soulos
410a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
411417309a770568d7b278abc91c46a739020e48157Jay Shrauner            try {
412c6100ffd22ae176a3e84a1062d8cb92d955faef2Brian Attwell                ImplicitIntentsUtil.startActivityInAppIfPossible(QuickContactActivity.this, intent);
41382a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner            } catch (SecurityException ex) {
41482a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
41582a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                        Toast.LENGTH_SHORT).show();
41682a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                Log.e(TAG, "QuickContacts does not have permission to launch "
41782a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                        + intent);
418417309a770568d7b278abc91c46a739020e48157Jay Shrauner            } catch (ActivityNotFoundException ex) {
419417309a770568d7b278abc91c46a739020e48157Jay Shrauner                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
420417309a770568d7b278abc91c46a739020e48157Jay Shrauner                        Toast.LENGTH_SHORT).show();
421417309a770568d7b278abc91c46a739020e48157Jay Shrauner            }
4222d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
4232d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
4242d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
425e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
426e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
427e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
428e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
429e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
430e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
4310cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
4320cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        @Override
433245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        public void onExpand() {
434245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell            mScroller.setDisableTouchesForSuppressLayout(/* areTouchesDisabled = */ true);
435245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        }
436245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell
437245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        @Override
438245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        public void onExpandDone() {
439245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell            mScroller.setDisableTouchesForSuppressLayout(/* areTouchesDisabled = */ false);
4400cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        }
441e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
442e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
44397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private interface ContextMenuIds {
44497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int COPY_TEXT = 0;
44597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int CLEAR_DEFAULT = 1;
44697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int SET_DEFAULT = 2;
44797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    }
44897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
4492a4207fb39330e840436215c896cde911489e111Paul Soulos    private final OnCreateContextMenuListener mEntryContextMenuListener =
4502a4207fb39330e840436215c896cde911489e111Paul Soulos            new OnCreateContextMenuListener() {
4512a4207fb39330e840436215c896cde911489e111Paul Soulos        @Override
4522a4207fb39330e840436215c896cde911489e111Paul Soulos        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4532a4207fb39330e840436215c896cde911489e111Paul Soulos            if (menuInfo == null) {
4542a4207fb39330e840436215c896cde911489e111Paul Soulos                return;
4552a4207fb39330e840436215c896cde911489e111Paul Soulos            }
45697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            final EntryContextMenuInfo info = (EntryContextMenuInfo) menuInfo;
4572a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.setHeaderTitle(info.getCopyText());
45897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            menu.add(ContextMenu.NONE, ContextMenuIds.COPY_TEXT,
45997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    ContextMenu.NONE, getString(R.string.copy_text));
46097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
46197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Don't allow setting or clearing of defaults for non-editable contacts
46297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (!isContactEditable()) {
46397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return;
46497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
46597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
46697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            final String selectedMimeType = info.getMimeType();
46797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
46897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Defaults to true will only enable the detail to be copied to the clipboard.
46997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            boolean onlyOneOfMimeType = true;
47097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
47197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Only allow primary support for Phone and Email content types
47297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (Phone.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
47397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                onlyOneOfMimeType = mOnlyOnePhoneNumber;
47497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            } else if (Email.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
47597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                onlyOneOfMimeType = mOnlyOneEmail;
47697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
47797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
47897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Checking for previously set default
47997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (info.isSuperPrimary()) {
48097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                menu.add(ContextMenu.NONE, ContextMenuIds.CLEAR_DEFAULT,
48197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        ContextMenu.NONE, getString(R.string.clear_default));
48297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            } else if (!onlyOneOfMimeType) {
48397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                menu.add(ContextMenu.NONE, ContextMenuIds.SET_DEFAULT,
48497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        ContextMenu.NONE, getString(R.string.set_default));
48597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
4862a4207fb39330e840436215c896cde911489e111Paul Soulos        }
4872a4207fb39330e840436215c896cde911489e111Paul Soulos    };
4882a4207fb39330e840436215c896cde911489e111Paul Soulos
4892a4207fb39330e840436215c896cde911489e111Paul Soulos    @Override
4902a4207fb39330e840436215c896cde911489e111Paul Soulos    public boolean onContextItemSelected(MenuItem item) {
4912a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo menuInfo;
4922a4207fb39330e840436215c896cde911489e111Paul Soulos        try {
4932a4207fb39330e840436215c896cde911489e111Paul Soulos            menuInfo = (EntryContextMenuInfo) item.getMenuInfo();
4942a4207fb39330e840436215c896cde911489e111Paul Soulos        } catch (ClassCastException e) {
4952a4207fb39330e840436215c896cde911489e111Paul Soulos            Log.e(TAG, "bad menuInfo", e);
4962a4207fb39330e840436215c896cde911489e111Paul Soulos            return false;
4972a4207fb39330e840436215c896cde911489e111Paul Soulos        }
4982a4207fb39330e840436215c896cde911489e111Paul Soulos
49997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        switch (item.getItemId()) {
50097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.COPY_TEXT:
50197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                ClipboardUtils.copyText(this, menuInfo.getCopyLabel(), menuInfo.getCopyText(),
50297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        true);
50397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
50497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.SET_DEFAULT:
50597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                final Intent setIntent = ContactSaveService.createSetSuperPrimaryIntent(this,
50697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        menuInfo.getId());
50797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                this.startService(setIntent);
50897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
50997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.CLEAR_DEFAULT:
51097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                final Intent clearIntent = ContactSaveService.createClearPrimaryIntent(this,
51197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        menuInfo.getId());
51297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                this.startService(clearIntent);
51397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
51497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            default:
51597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                throw new IllegalArgumentException("Unknown menu option " + item.getItemId());
51697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
5172a4207fb39330e840436215c896cde911489e111Paul Soulos    }
5182a4207fb39330e840436215c896cde911489e111Paul Soulos
519d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
520d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
521d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
522d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
523d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
524d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
525d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
526d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
527d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
528d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
529d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
530d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
531d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
532d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
533d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
534d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
535d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
536d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
537d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
538d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
539d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
540d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
541d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
542d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
543d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
544d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
545d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
546d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
547d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
548d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
549b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
550b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
551b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
552b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
553f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            finish();
554b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
5558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
5578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
5588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
5598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
5608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
5628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
5638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
5648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
5658477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
5668477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
5678477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
5688477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
5698477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
5708477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
5718477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
572f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onEntranceAnimationDone() {
573f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            mIsEntranceAnimationFinished = true;
574f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        }
575f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
576f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        @Override
577f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onTransparentViewHeightChange(float ratio) {
578f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            if (mIsEntranceAnimationFinished) {
579f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                mWindowScrim.setAlpha((int) (0xFF * ratio));
5808477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
5818477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
582b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
583b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
584eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
585eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
586eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
587eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
588eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
589eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
590eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
591eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
592eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
593eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
594eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
595eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
596eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
597eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
598eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
599eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
600eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
601eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
602eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
603eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
604eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
605eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
606eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
607eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
608eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
609eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
610eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
611eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
612eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
613eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
614eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
615eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
616eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
617eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
618eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
619eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
620cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
621cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
62205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 1. Whether one of the mimetypes is the prioritized mimetype
62305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 2. Number of times used
62405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 3. Last time used
62505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell     * 4. Statically defined
626cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
627eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
628eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
629eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
630eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
63105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final DataItem lhs = lhsList.get(0);
63205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final DataItem rhs = rhsList.get(0);
63305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final String lhsMimeType = lhs.getMimeType();
63405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final String rhsMimeType = rhs.getMimeType();
63505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell
63605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 1. Whether one of the mimetypes is the prioritized mimetype
63705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            if (!TextUtils.isEmpty(mExtraPrioritizedMimeType) && !lhsMimeType.equals(rhsMimeType)) {
63805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                if (rhsMimeType.equals(mExtraPrioritizedMimeType)) {
63905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    return 1;
64005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                }
64105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                if (lhsMimeType.equals(mExtraPrioritizedMimeType)) {
64205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    return -1;
64305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                }
64405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            }
64505287bf0cea303b93802a629aa883fb6322ca342Brian Attwell
64605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 2. Number of times used
647eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
648eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
649eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
650eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
651eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
652eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
653eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
65405287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 3. Last time used
655eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
656eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
657eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
658eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
659eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
660eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
661eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
662eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
663eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
664eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
665eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
66605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            // 4. Resort to a statically defined mimetype order.
66705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            if (!lhsMimeType.equals(rhsMimeType)) {
66805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                for (String mimeType : LEADING_MIMETYPES) {
66905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    if (lhsMimeType.equals(mimeType)) {
67005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                        return -1;
67105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    } else if (rhsMimeType.equals(mimeType)) {
67205287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                        return 1;
67305287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    }
674eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
675eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
676eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
677eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
678eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
679eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
680edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
681f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    public boolean dispatchTouchEvent(MotionEvent ev) {
682f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
683f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen            TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
684f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        }
685f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        return super.dispatchTouchEvent(ev);
686f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    }
687f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen
688f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    @Override
6898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
6908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
6918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
692edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
693bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell        if (RequestPermissionsActivity.startPermissionActivity(this)) {
694bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell            return;
695bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell        }
696bdd32641ae2c2bc6214608cc5712dfb2b96e0305Brian Attwell
6978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getWindow().setStatusBarColor(Color.TRANSPARENT);
6982426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
699d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
700dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
70110d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
70210d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
70310d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
70410d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
705edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
706edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
707a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
708a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
7090cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
7100cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
711eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
7126095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
713b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
714eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
715edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7166095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
717eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
718eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
71923889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
7202a4207fb39330e840436215c896cde911489e111Paul Soulos        mContactCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
721b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
722b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
723b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
724edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
725eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
7262a4207fb39330e840436215c896cde911489e111Paul Soulos        mAboutCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
727eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
72802eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView = (QuickContactImageView) findViewById(R.id.photo);
72963176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        final View transparentView = findViewById(R.id.transparent_view);
7300d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
73163176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell            transparentView.setOnClickListener(new OnClickListener() {
7320d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
7330d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
7340d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
7350d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
7360d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
7370d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
738edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
739d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        // Allow a shadow to be shown under the toolbar.
740d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
741d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell
742d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
743d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
7449b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
7459b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
7469b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
7479b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
7486219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
7498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
750f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = mHasAlreadyBeenOpened;
7519b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
752f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mWindowScrim.setAlpha(0);
7539b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
754edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
755c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
756ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
757ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
758ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
7598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7609b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
7619b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
762d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
763d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
764d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
765d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
766d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
767d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
768d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
769d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
770d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
77123889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
772f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ true,
773f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                new Runnable() {
774f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    @Override
775f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    public void run() {
776f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        if (!mHasAlreadyBeenOpened) {
777f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // The initial scrim opacity must match the scrim opacity that would be
778f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // achieved by scrolling to the starting position.
779f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final float alphaRatio = mExtraMode == MODE_FULLY_EXPANDED ?
780f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    1 : mScroller.getStartingTransparentHeightRatio();
781f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int duration = getResources().getInteger(
782f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    android.R.integer.config_shortAnimTime);
783f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int desiredAlpha = (int) (0xFF * alphaRatio);
784f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            ObjectAnimator o = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0,
785f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    desiredAlpha).setDuration(duration);
786f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
787f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            o.start();
788f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        }
789f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    }
790f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                });
791f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
7929b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
7939b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
794ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
795ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
796ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
797ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
798ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
799ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
800ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
801ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
802ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
803ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
804ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
805ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
806ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
807ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
808a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
809a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
8109b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
811ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
812ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
8139b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
8149b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
8158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
8168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
818405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
819405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
8208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
8218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED) {
8228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // The contact that we were showing has been deleted.
8238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
824ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos        } else if (requestCode == REQUEST_CODE_CONTACT_SELECTION_ACTIVITY &&
825ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                resultCode != RESULT_CANCELED) {
826ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos            processIntent(data);
827b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
8288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
829dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
8308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
8318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
8328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
8338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
834f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = true;
8358571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
836d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
837d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
838d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
8399b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
8409b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
8419b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
8429b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
8438e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilterColor);
8449b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
8459b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
8469b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
847d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
848f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos        if (intent == null) {
849f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos            finish();
850f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos            return;
851f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos        }
852d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
853d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
854d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
855d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
856d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
857d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
858d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
859d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
86005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE, QuickContact.MODE_LARGE);
86105287bf0cea303b93802a629aa883fb6322ca342Brian Attwell        mExtraPrioritizedMimeType = getIntent().getStringExtra(QuickContact.EXTRA_PRIORITIZED_MIMETYPE);
862d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
863d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
864fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        if (lookupUri == null) {
865fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner            finish();
866fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner            return;
867fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        }
868fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        mLookupUri = lookupUri;
869d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
870d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
871d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
872d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
873d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
874d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
875d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // we need to restart the loader and reload the new contact.
87659a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            destroyInteractionLoaders();
877405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
878405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
87959a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            mCachedCp2DataCardModel = null;
880d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
8812d150da246632b1649999cfabed776133b097775Brian Attwell
8822d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
8832426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
884edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
88559a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell    private void destroyInteractionLoaders() {
88659a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        for (int interactionLoaderId : mRecentLoaderIds) {
88759a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            getLoaderManager().destroyLoader(interactionLoaderId);
88859a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        }
88959a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell    }
89059a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell
891b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
8928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
8938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
8948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
8958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
896c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
897b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
898b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
89981281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
900d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
9019b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
902f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString());
9039b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
90481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
90581281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
90681281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
907f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell    private void setHeaderNameText(String value) {
908d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
9099b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
910f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell                mScroller.setTitle(value);
9119b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
91281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
91381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
91481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
91581281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
916edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
917edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
918edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
919edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
920edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
921edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
922edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
923edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
924edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
925edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
926edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
927edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
928edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
929edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
930cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
931edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
9328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
9338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
9346cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
935d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
936edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
9378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
9388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
939dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
94002eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView.setIsBusiness(mContactData.isDisplayNameFromOrganization());
9416219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
94231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
943d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang        String phoneticName = ContactDisplayUtils.getPhoneticName(this, data);
944d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang        String displayName = ContactDisplayUtils.getDisplayName(this, data).toString();
945d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang        if (mContactData.getDisplayNameSource() != DisplayNameSources.STRUCTURED_PHONETIC_NAME
946d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang                && !TextUtils.isEmpty(phoneticName)) {
947d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang            displayName = getResources().getString(
948d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang                    R.string.quick_contact_display_name_with_phonetic, displayName, phoneticName);
949d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang            setHeaderNameText(displayName);
950d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang        } else {
951d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang            setHeaderNameText(displayName);
952d5b686e73dc50d90fd44dc5c738d243a81a6cc31Tingting Wang        }
953edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
9548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
955dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
9566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mEntriesAndActionsTask = new AsyncTask<Void, Void, Cp2DataCardModel>() {
9578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
9588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
9596bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected Cp2DataCardModel doInBackground(
960eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
961eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
9628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
9638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
9648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
9656bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected void onPostExecute(Cp2DataCardModel cardDataModel) {
9666bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                super.onPostExecute(cardDataModel);
9678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
9688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
9698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
9708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
9716bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    bindDataToCards(cardDataModel);
9728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
9738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
9748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
9758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
9768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
9778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
9788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
9796bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void bindDataToCards(Cp2DataCardModel cp2DataCardModel) {
9806bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        startInteractionLoaders(cp2DataCardModel);
9816bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        populateContactAndAboutCard(cp2DataCardModel);
982eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
983eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
9846bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void startInteractionLoaders(Cp2DataCardModel cp2DataCardModel) {
9856bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Map<String, List<DataItem>> dataItemsMap = cp2DataCardModel.dataItemsMap;
9866bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> phoneDataItems = dataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
98797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        if (phoneDataItems != null && phoneDataItems.size() == 1) {
98897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            mOnlyOnePhoneNumber = true;
98997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
990eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
991eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
992eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
993eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
994eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
995eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
996eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
997ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
998eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
999eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1000eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
10018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
10028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
1003ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
1004ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
1005ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
1006ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
1007ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
1008ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
1009ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
1010ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
10118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
1012899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
10138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1014eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1015899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
10166bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> emailDataItems = dataItemsMap.get(Email.CONTENT_ITEM_TYPE);
101797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        if (emailDataItems != null && emailDataItems.size() == 1) {
101897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            mOnlyOneEmail = true;
101997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
1020eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
1021eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
1022eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
1023eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
1024eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
1025eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1026eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1027ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
1028eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
1029899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
1030899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
1031ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
1032899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
10338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
10348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
10358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
10368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
10378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
10388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
10398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
10408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
10418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
10428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
10438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
10448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
10458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
10468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
10478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
10488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
10496bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<List<Entry>> buildAboutCardEntries(Map<String, List<DataItem>> dataItemsMap) {
1050a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
1051a153dba33c2152332c5706178a367f52b0550723Paul Soulos        for (String mimetype : SORTED_ABOUT_CARD_MIMETYPES) {
10526bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> mimeTypeItems = dataItemsMap.get(mimetype);
1053a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (mimeTypeItems == null) {
1054a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
1055a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
10566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // Set aboutCardTitleOut = null, since SORTED_ABOUT_CARD_MIMETYPES doesn't contain
10576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // the name mimetype.
10586bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<Entry> aboutEntries = dataItemsToEntries(mimeTypeItems,
10596bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    /* aboutCardTitleOut = */ null);
1060a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutEntries.size() > 0) {
1061a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(aboutEntries);
1062a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
1063a153dba33c2152332c5706178a367f52b0550723Paul Soulos        }
1064a153dba33c2152332c5706178a367f52b0550723Paul Soulos        return aboutCardEntries;
1065a153dba33c2152332c5706178a367f52b0550723Paul Soulos    }
1066a153dba33c2152332c5706178a367f52b0550723Paul Soulos
1067a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    @Override
1068a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    protected void onResume() {
1069a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        super.onResume();
1070a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        // If returning from a launched activity, repopulate the contact and about card
1071a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        if (mHasIntentLaunched) {
1072a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = false;
10736bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            populateContactAndAboutCard(mCachedCp2DataCardModel);
1074a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
107559a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell
107659a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // When exiting the activity and resuming, we want to force a full reload of all the
107759a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // interaction data in case something changed in the background. On screen rotation,
107859a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // we don't need to do this. And, mCachedCp2DataCardModel will be null, so we won't.
107959a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        if (mCachedCp2DataCardModel != null) {
108059a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            destroyInteractionLoaders();
108159a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            startInteractionLoaders(mCachedCp2DataCardModel);
108259a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        }
1083a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    }
1084a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
10856bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void populateContactAndAboutCard(Cp2DataCardModel cp2DataCardModel) {
10866bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mCachedCp2DataCardModel = cp2DataCardModel;
10876bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (mHasIntentLaunched || cp2DataCardModel == null) {
1088a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            return;
1089a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
1090eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
1091eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
10926bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = cp2DataCardModel.contactCardEntries;
10936bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = cp2DataCardModel.aboutCardEntries;
10946bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final String customAboutCardName = cp2DataCardModel.customAboutCardName;
1095eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1096eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
109705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell            final boolean firstEntriesArePrioritizedMimeType =
109805287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    !TextUtils.isEmpty(mExtraPrioritizedMimeType) &&
109905287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mCachedCp2DataCardModel.dataItemsMap.containsKey(mExtraPrioritizedMimeType) &&
110005287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mCachedCp2DataCardModel.dataItemsMap.get(mExtraPrioritizedMimeType).size() != 0;
1101eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
1102eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
1103c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mContactCard.isExpanded(),
110489966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                    /* isAlwaysExpanded = */ false,
11050cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener,
110605287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    mScroller,
110705287bf0cea303b93802a629aa883fb6322ca342Brian Attwell                    firstEntriesArePrioritizedMimeType);
1108eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
1109eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1110eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
1111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1112eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
11138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1114eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
1115c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        // Phonetic name is not a data item, so the entry needs to be created separately
1116c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        final String phoneticName = mContactData.getPhoneticName();
1117c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        if (!TextUtils.isEmpty(phoneticName)) {
1118c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            Entry phoneticEntry = new Entry(/* viewId = */ -1,
1119c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* icon = */ null,
1120c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    getResources().getString(R.string.name_phonetic),
1121c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    phoneticName,
11228a3fe52802135be2402a5b216325615fb796a509Walter Jang                    /* subHeaderIcon = */ null,
1123c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* text = */ null,
11248a3fe52802135be2402a5b216325615fb796a509Walter Jang                    /* textIcon = */ null,
112523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    /* primaryContentDescription = */ null,
1126c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* intent = */ null,
1127c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIcon = */ null,
1128c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIntent = */ null,
1129c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateContentDescription = */ null,
1130c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* shouldApplyColor = */ false,
11312a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
11322a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ new EntryContextMenuInfo(phoneticName,
113397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                            getResources().getString(R.string.name_phonetic),
113497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                            /* mimeType = */ null, /* id = */ -1, /* isPrimary = */ false),
113548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
113648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
113748290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* thirdContentDescription = */ null,
113848290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* iconResourceId = */ 0);
1139c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            List<Entry> phoneticList = new ArrayList<>();
1140c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            phoneticList.add(phoneticEntry);
1141a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name comes after nickname. Check to see if the first entry type is nickname
1142a0fa4c18dc111ceea9ff47f35ee01fecc1003578Paul Soulos            if (aboutCardEntries.size() > 0 && aboutCardEntries.get(0).get(0).getHeader().equals(
1143a153dba33c2152332c5706178a367f52b0550723Paul Soulos                    getResources().getString(R.string.header_nickname_entry))) {
1144a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(1, phoneticList);
1145a153dba33c2152332c5706178a367f52b0550723Paul Soulos            } else {
1146a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(0, phoneticList);
1147a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
1148c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        }
1149c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos
11506bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (!TextUtils.isEmpty(customAboutCardName)) {
11516bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            mAboutCard.setTitle(customAboutCardName);
11526bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
11536bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
11543b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox        mAboutCard.initialize(aboutCardEntries,
11553b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* numInitialVisibleEntries = */ 1,
11563b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* isExpanded = */ true,
11573b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                /* isAlwaysExpanded = */ true,
11583b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                mExpandingEntryCardViewListener,
11593b1a8b2f6349d55ae8ef502d9d2f9451f24eff50kungaox                mScroller);
11606095369885edcca566a812b551886e29c7ff8039Brian Attwell
11616095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
11626095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
11636095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
11646095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
11656095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
11666095369885edcca566a812b551886e29c7ff8039Brian Attwell
1167a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
1168a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
1169a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
1170a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1171a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
1172eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1173eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1174eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1175eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
11766095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
11776095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
11786095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
11796095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
11806095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
11816095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
11826095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
11838a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* subHeader = */ null, /* subHeaderIcon = */ null, /* text = */ null,
11848a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* textIcon = */ null, /* primaryContentDescription = */ null,
118523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                getEditContactIntent(),
1186dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
1187714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
118848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* isEditable = */ false, /* EntryContextMenuInfo = */ null,
118948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* thirdIcon = */ null, /* thirdIntent = */ null,
119048290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                /* thirdContentDescription = */ null, R.drawable.ic_phone_24dp);
11916095369885edcca566a812b551886e29c7ff8039Brian Attwell
11926095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
11936095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
11946095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
11956095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
11968a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* subHeaderIcon = */ null,
11978a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* text = */ null, /* textIcon = */ null, /* primaryContentDescription = */ null,
119823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                getEditContactIntent(), /* alternateIcon = */ null,
1199dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
12002a4207fb39330e840436215c896cde911489e111Paul Soulos                /* shouldApplyColor = */ true, /* isEditable = */ false,
120148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* EntryContextMenuInfo = */ null, /* thirdIcon = */ null,
120248290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                /* thirdIntent = */ null, /* thirdContentDescription = */ null,
120348290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                R.drawable.ic_email_24dp);
12046095369885edcca566a812b551886e29c7ff8039Brian Attwell
12056095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
12066095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
12076095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
12086095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
12096095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
12106095369885edcca566a812b551886e29c7ff8039Brian Attwell
12116095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
12126095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
12136095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
12146095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
1215c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
12160cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener, mScroller);
12176095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
12186095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
12196095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
12206095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
12216095369885edcca566a812b551886e29c7ff8039Brian Attwell
12226095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
1223eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
1224eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
1225eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
1226eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
1227eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
1228eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
12296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel generateDataModelFromContact(
1230eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
1231eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
1232eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1233eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
12348bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
12358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
1236851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
1237851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
1238eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
1239eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1240851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
1241eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
1242eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
124347b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
124447b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
124547b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
1246eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
1247cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1248eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
1249b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1250eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
1251eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
1252899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
1253eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
1254edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1255eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
1256eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
1257eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
1258eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
1259edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
1260eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
1261edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1262edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
12638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1264edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1265eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
126616339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
126716339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
1268eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
1269eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
1270eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
127116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
1272eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
1273eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
1274eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
1275eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
1276eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
1277eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
1278eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
1279eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
1280edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1281eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1282edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1283eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
1284eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
1285eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
1286eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
128716339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
12886bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.beginSection("cp2 data items to entries");
12896bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
12906bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = new ArrayList<>();
12916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = buildAboutCardEntries(dataItemsMap);
12926bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final MutableString aboutCardName = new MutableString();
12936bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
12946bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        for (int i = 0; i < dataItemsList.size(); ++i) {
12956bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> dataItemsByMimeType = dataItemsList.get(i);
12966bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final DataItem topDataItem = dataItemsByMimeType.get(0);
12976bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
12986bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                // About card mimetypes are built in buildAboutCardEntries, skip here
12996bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                continue;
13006bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            } else {
13016bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                List<Entry> contactEntries = dataItemsToEntries(dataItemsList.get(i),
13026bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        aboutCardName);
13036bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (contactEntries.size() > 0) {
13046bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    contactCardEntries.add(contactEntries);
13056bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                }
13066bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            }
13076bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
13086bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
13096bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.endSection();
13106bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
13116bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Cp2DataCardModel dataModel = new Cp2DataCardModel();
13126bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.customAboutCardName = aboutCardName.value;
13136bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.aboutCardEntries = aboutCardEntries;
13146bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.contactCardEntries = contactCardEntries;
13156bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.dataItemsMap = dataItemsMap;
13166bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return dataModel;
13176bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
13186bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
13196bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
13206bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * Class used to hold the About card and Contact cards' data model that gets generated
13216bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * on a background thread. All data is from CP2.
13226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
13236bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class Cp2DataCardModel {
13246bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        /**
13256bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * A map between a mimetype string and the corresponding list of data items. The data items
13266bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * are in sorted order using mWithinMimeTypeDataItemComparator.
13276bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         */
13286bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public Map<String, List<DataItem>> dataItemsMap;
13296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> aboutCardEntries;
13306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> contactCardEntries;
13316bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String customAboutCardName;
13326bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
13336bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
13346bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class MutableString {
13356bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String value;
1336eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1337edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1338eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
1339eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
1340eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
13416bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
13426bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * This runs on a background thread. This is set as static to avoid accidentally adding
13436bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * additional dependencies on unsafe things (like the Activity).
13446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
1345eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
13466a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * @param secondDataItem A second {@link DataItem} to help build a full entry for some
13476a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     *  mimetypes
1348eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
1349eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
13506a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static Entry dataItemToEntry(DataItem dataItem, DataItem secondDataItem,
13516bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            Context context, Contact contactData,
13526bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final MutableString aboutCardName) {
1353eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
1354eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
1355eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
1356eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
1357eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
1358eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
135923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        StringBuilder primaryContentDescription = new StringBuilder();
1360eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
136148ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
1362dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
1363dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
136423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        StringBuilder alternateContentDescription = new StringBuilder();
1365eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
13662a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo entryContextMenuInfo = null;
136748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Drawable thirdIcon = null;
136848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Intent thirdIntent = null;
136948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        String thirdContentDescription = null;
137048290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos        int iconResourceId = 0;
1371eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
13726bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        context = context.getApplicationContext();
137323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        final Resources res = context.getResources();
1374eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
1375eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1376eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
1377eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
13786bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            intent = ContactsUtils.buildImIntent(context, im).first;
1379eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
13807de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            final int protocol;
13817de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (!im.isProtocolValid()) {
13827de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = Im.PROTOCOL_CUSTOM;
13837de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
13847de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
13857de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
13867de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (protocol == Im.PROTOCOL_CUSTOM) {
13877de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // If the protocol is custom, display the "IM" entry header as well to distinguish
13887de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // this entry from other ones
138923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = res.getString(R.string.header_im_entry);
139023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                subHeader = Im.getProtocolLabel(res, protocol,
13917de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
13927de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                text = im.getData();
13937de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
139423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = Im.getProtocolLabel(res, protocol,
13957de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
13967de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = im.getData();
13977de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
139897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(im.getData(), header,
139997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1400eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
1401eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
140223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_organization_entry);
1403eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
140497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
140597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1406eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
1407eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
1408eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
1409eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
1410eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
14116bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                (contactData.getNameRawContactId() == dataItem.getRawContactId());
1412eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1413eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
1414eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
14156bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                && contactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
1416eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1417eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
141823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = res.getString(R.string.header_nickname_entry);
1419eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
142097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
142197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1422eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1423eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
1424eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
142523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_note_entry);
1426eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
142797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
142897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1429eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
1430eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
143123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_website_entry);
1432eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
143397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
143497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1435eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
14363bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                final WebAddress webAddress = new WebAddress(website.buildDataStringForDisplay
14373bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                        (context, kind));
1438eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1439eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
14403bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                Log.e(TAG, "Couldn't parse website: " + website.buildDataStringForDisplay(
14413bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                        context, kind));
1442eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1443eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1444eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
14453bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell            final String dataString = event.buildDataStringForDisplay(context, kind);
1446eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1447eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1448eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1449eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1450eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1451eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1452eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1453eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1454eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
145523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_event_entry);
1456f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
145723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                subHeader = Event.getTypeLabel(res, event.getKindTypeColumn(kind),
14587de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        event.getLabel()).toString();
1459f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
14606bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            text = DateUtils.formatDate(context, dataString);
146197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(text, header,
146297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1463eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1464eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
14653bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell            final String dataString = relation.buildDataStringForDisplay(context, kind);
1466eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1467eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1468eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1469eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1470eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
147123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_relation_entry);
1472eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
147397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
147497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1475f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
147623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                text = Relation.getTypeLabel(res,
14776bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        relation.getKindTypeColumn(kind),
1478f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1479f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1480eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1481eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
1482eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
148323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.call_other)).append(" ");
14843bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                header = sBidiFormatter.unicodeWrap(phone.buildDataStringForDisplay(context, kind),
1485c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell                        TextDirectionHeuristics.LTR);
14862a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
148797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.phoneLabelsGroup), dataItem.getMimeType(),
148897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1489f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
149023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = Phone.getTypeLabel(res, phone.getKindTypeColumn(kind),
1491f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            phone.getLabel()).toString();
14923df785b0ad0c0292adfc03a4f804d1b38053de63Tingting Wang                    if (!TextUtils.isEmpty(text)) {
14933df785b0ad0c0292adfc03a4f804d1b38053de63Tingting Wang                        primaryContentDescription.append(text).append(" ");
14943df785b0ad0c0292adfc03a4f804d1b38053de63Tingting Wang                    }
1495f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
149623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
149723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_phone_24dp);
149848290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_phone_24dp;
14996bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (PhoneCapabilityTester.isPhone(context)) {
1500eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
150116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1502dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
15031cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                        Uri.fromParts(ContactsUtils.SCHEME_SMSTO, phone.getNumber(), null));
150448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
150523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateIcon = res.getDrawable(R.drawable.ic_message_24dp);
150623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(res.getString(R.string.sms_custom, header));
150748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
150848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                // Add video call button if supported
1509f764730a34da560db87206f9a48390d4c8085365Paul Soulos                if (CallUtil.isVideoEnabled(context)) {
151023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    thirdIcon = res.getDrawable(R.drawable.ic_videocam);
151148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdIntent = CallUtil.getVideoCallIntent(phone.getNumber(),
151248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                            CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY);
151348fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdContentDescription =
151423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            res.getString(R.string.description_video_call);
151548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                }
1516eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1517eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1518eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1519eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1520eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
152123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.email_other)).append(" ");
15221cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                final Uri mailUri = Uri.fromParts(ContactsUtils.SCHEME_MAILTO, address, null);
1523eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1524eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
15252a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
152697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.emailLabelsGroup), dataItem.getMimeType(),
152797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1528f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
152923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = Email.getTypeLabel(res, email.getKindTypeColumn(kind),
1530f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
153123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1532f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
153323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
153423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_email_24dp);
153548290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_email_24dp;
1536eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1537eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1538eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1539eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1540eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
154123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.map_other)).append(" ");
1542eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1543eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
15442a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
154597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.postalLabelsGroup), dataItem.getMimeType(),
154697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1547f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
154823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = StructuredPostal.getTypeLabel(res,
1549f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
155023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1551f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
155223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
15536a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIntent =
15546a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                        StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
155523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateIcon = res.getDrawable(R.drawable.ic_directions_24dp);
155623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(res.getString(
155723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                        R.string.content_description_directions)).append(" ").append(header);
155823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_place_24dp);
155948290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_place_24dp;
1560eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1561eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
1562593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1563593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            final String address = sip.getSipAddress();
1564593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            if (!TextUtils.isEmpty(address)) {
1565593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                primaryContentDescription.append(res.getString(R.string.call_other)).append(
1566593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        " ");
1567593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                if (PhoneCapabilityTester.isSipPhone(context)) {
15681cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                    final Uri callUri = Uri.fromParts(PhoneAccount.SCHEME_SIP, address, null);
1569eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
157016339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1571593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                header = address;
1572593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                entryContextMenuInfo = new EntryContextMenuInfo(header,
1573593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        res.getString(R.string.phoneLabelsGroup), dataItem.getMimeType(),
1574593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        dataItem.getId(), dataItem.isSuperPrimary());
1575593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                if (sip.hasKindTypeColumn(kind)) {
1576593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                    text = SipAddress.getTypeLabel(res,
1577593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                            sip.getKindTypeColumn(kind), sip.getLabel()).toString();
1578593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                    primaryContentDescription.append(text).append(" ");
1579593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                }
1580593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                primaryContentDescription.append(header);
1581593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                icon = res.getDrawable(R.drawable.ic_dialer_sip_black_24dp);
1582593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                iconResourceId = R.drawable.ic_dialer_sip_black_24dp;
1583edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1584eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
15858025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            // If the name is already set and this is not the super primary value then leave the
15868025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            // current value. This way we show the super primary value when we are able to.
15878025f801f5153917781a9496cabdd81ae1b6deafWalter Jang            if (dataItem.isSuperPrimary() || aboutCardName.value == null
15888025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    || aboutCardName.value.isEmpty()) {
15898025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
15908025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                if (!TextUtils.isEmpty(givenName)) {
15918025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    aboutCardName.value = res.getString(R.string.about_card_title) +
15928025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                            " " + givenName;
15938025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                } else {
15948025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                    aboutCardName.value = res.getString(R.string.about_card_title);
15958025f801f5153917781a9496cabdd81ae1b6deafWalter Jang                }
1596eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1597eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1598eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
15996bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = dataItem.buildDataStringForDisplay(context, kind);
1600eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1601eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
160260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
160360e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
1604e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1605e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
1606e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
1607e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
16086a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                // Build advanced entry for known 3p types. Otherwise default to ResolveCache icon.
1609e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
1610e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
16116a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // If a secondDataItem is available, use it to build an entry with
16126a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // alternate actions
16136a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        if (secondDataItem != null) {
161423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
16156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            alternateIcon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
16166a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            final GPlusOrHangoutsDataItemModel itemModel =
16176a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    new GPlusOrHangoutsDataItemModel(intent, alternateIntent,
16186a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            dataItem, secondDataItem, alternateContentDescription,
16196a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            header, text, context);
16206a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
162197b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            populateGPlusOrHangoutsDataItemModel(itemModel);
162297b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            intent = itemModel.intent;
162397b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateIntent = itemModel.alternateIntent;
162497b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateContentDescription = itemModel.alternateContentDescription;
162597b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            header = itemModel.header;
162697b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            text = itemModel.text;
16276a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        } else {
16286a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            if (GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(
16296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    intent.getDataString())) {
16306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
16316a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            } else {
16326a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
16336a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            }
1634e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1635e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1636e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
16376a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // If a secondDataItem is available, use it to build an entry with
16386a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // alternate actions
16396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        if (secondDataItem != null) {
164023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            icon = res.getDrawable(R.drawable.ic_hangout_24dp);
16416a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            alternateIcon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
164297b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            final GPlusOrHangoutsDataItemModel itemModel =
16436a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    new GPlusOrHangoutsDataItemModel(intent, alternateIntent,
16446a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            dataItem, secondDataItem, alternateContentDescription,
16456a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            header, text, context);
16466a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
164797b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            populateGPlusOrHangoutsDataItemModel(itemModel);
164897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            intent = itemModel.intent;
164997b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateIntent = itemModel.alternateIntent;
165097b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateContentDescription = itemModel.alternateContentDescription;
165197b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            header = itemModel.header;
165297b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            text = itemModel.text;
16536a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        } else {
16546a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            if (HANGOUTS_DATA_5_VIDEO.equals(intent.getDataString())) {
16556a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
16566a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            } else {
16576a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_hangout_24dp);
16586a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            }
1659e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1660e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1661e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
166297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        entryContextMenuInfo = new EntryContextMenuInfo(header, mimetype,
166397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                                dataItem.getMimeType(), dataItem.getId(),
166497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                                dataItem.isSuperPrimary());
16656bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        icon = ResolveCache.getInstance(context).getIcon(
1666e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
1667e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
1668e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
1669e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
1670e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
167148ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
1672e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
1673e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
1674eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1675b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1676eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
1677eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
16786bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, intent)) {
1679eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
1680eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1681eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1682eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1683dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
1684dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
16856bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, alternateIntent)) {
1686dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
1687f0d04c11b649e953177e79722d05fa048e53273dJay Shrauner            } else if (TextUtils.isEmpty(alternateContentDescription)) {
1688f0d04c11b649e953177e79722d05fa048e53273dJay Shrauner                // Attempt to use package manager to find a suitable content description if needed
168923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(getIntentResolveLabel(alternateIntent, context));
1690dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1691dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1692dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1693eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
1694eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
1695eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
1696eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
1697eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1698eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1699fa238949855fba3b28a57fad3d585b13e80362bbBrian Attwell        // Ignore dataIds from the Me profile.
1700ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
1701ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
1702ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
170323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon,
17047ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jang                new SpannableString(primaryContentDescription.toString()),
17057ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jang                intent, alternateIcon, alternateIntent,
170623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.toString(), shouldApplyColor, isEditable,
170748290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription,
170848290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId);
1709eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1710eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
17116bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<Entry> dataItemsToEntries(List<DataItem> dataItems,
17126bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            MutableString aboutCardTitleOut) {
17136a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Hangouts and G+ use two data items to create one entry.
17146a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        if (dataItems.get(0).getMimeType().equals(MIMETYPE_GPLUS_PROFILE) ||
17156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataItems.get(0).getMimeType().equals(MIMETYPE_HANGOUTS)) {
17166a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            return gPlusOrHangoutsDataItemsToEntries(dataItems);
17176a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        } else {
17186a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            final List<Entry> entries = new ArrayList<>();
17196a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            for (DataItem dataItem : dataItems) {
17206a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                final Entry entry = dataItemToEntry(dataItem, /* secondDataItem = */ null,
17216a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        this, mContactData, aboutCardTitleOut);
17226a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                if (entry != null) {
17236a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    entries.add(entry);
17246a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
17256a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            }
17266a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            return entries;
17276a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
17286a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
17296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
17306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    /**
17316a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * G+ and Hangout entries are unique in that a single ExpandingEntryCardView.Entry consists
17326a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * of two data items. This method attempts to build each entry using the two data items if
17336a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * they are available. If there are more or less than two data items, a fall back is used
17346a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * and each data item gets its own entry.
17356a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     */
17366a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private List<Entry> gPlusOrHangoutsDataItemsToEntries(List<DataItem> dataItems) {
1737eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
17386a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        final Map<Long, List<DataItem>> buckets = new HashMap<>();
17396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Put the data items into buckets based on the raw contact id
1740eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
17416a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            List<DataItem> bucket = buckets.get(dataItem.getRawContactId());
17426a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            if (bucket == null) {
17436a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                bucket = new ArrayList<>();
17446a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                buckets.put(dataItem.getRawContactId(), bucket);
17456a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            }
17466a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            bucket.add(dataItem);
17476a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
17486a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
17496a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Use the buckets to build entries. If a bucket contains two data items, build the special
17506a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // entry, otherwise fall back to the normal entry.
17516a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        for (List<DataItem> bucket : buckets.values()) {
17526a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            if (bucket.size() == 2) {
17536a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                // Use the pair to build an entry
17546a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                final Entry entry = dataItemToEntry(bucket.get(0),
17556a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        /* secondDataItem = */ bucket.get(1), this, mContactData,
17566a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        /* aboutCardName = */ null);
17576a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                if (entry != null) {
17586a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    entries.add(entry);
17596a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
17606a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            } else {
17616a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                for (DataItem dataItem : bucket) {
17626a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    final Entry entry = dataItemToEntry(dataItem, /* secondDataItem = */ null,
17636a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            this, mContactData, /* aboutCardName = */ null);
17646a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    if (entry != null) {
17656a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        entries.add(entry);
17666a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    }
17676a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
1768eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1769eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1770eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
1771edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1772edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
17736a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    /**
17746a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * Used for statically passing around G+ or Hangouts data items and entry fields to
17756a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * populateGPlusOrHangoutsDataItemModel.
17766a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     */
17776a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final class GPlusOrHangoutsDataItemModel {
17786a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Intent intent;
17796a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Intent alternateIntent;
17806a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public DataItem dataItem;
17816a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public DataItem secondDataItem;
17826a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public StringBuilder alternateContentDescription;
17836a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public String header;
17846a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public String text;
17856a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Context context;
17866a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
17876a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public GPlusOrHangoutsDataItemModel(Intent intent, Intent alternateIntent, DataItem dataItem,
17886a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                DataItem secondDataItem, StringBuilder alternateContentDescription, String header,
17896a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                String text, Context context) {
17906a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.intent = intent;
17916a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.alternateIntent = alternateIntent;
17926a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.dataItem = dataItem;
17936a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.secondDataItem = secondDataItem;
17946a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.alternateContentDescription = alternateContentDescription;
17956a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.header = header;
17966a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.text = text;
17976a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.context = context;
17986a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
17996a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
18006a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
18016a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static void populateGPlusOrHangoutsDataItemModel(
18026a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            GPlusOrHangoutsDataItemModel dataModel) {
18036a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        final Intent secondIntent = new Intent(Intent.ACTION_VIEW);
18046a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        secondIntent.setDataAndType(ContentUris.withAppendedId(Data.CONTENT_URI,
18056a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.secondDataItem.getId()), dataModel.secondDataItem.getMimeType());
18066a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // There is no guarantee the order the data items come in. Second
18076a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // data item does not necessarily mean it's the alternate.
18086a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Hangouts video and Add to circles should be alternate. Swap if needed
18096a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        if (HANGOUTS_DATA_5_VIDEO.equals(
18106a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.dataItem.getContentValues().getAsString(Data.DATA5)) ||
18116a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(
18126a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        dataModel.dataItem.getContentValues().getAsString(Data.DATA5))) {
18136a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateIntent = dataModel.intent;
18146a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateContentDescription = new StringBuilder(dataModel.header);
18156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
18166a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.intent = secondIntent;
18176a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.header = dataModel.secondDataItem.buildDataStringForDisplay(dataModel.context,
18186a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    dataModel.secondDataItem.getDataKind());
18196a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.text = dataModel.secondDataItem.getDataKind().typeColumn;
18206a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        } else if (HANGOUTS_DATA_5_MESSAGE.equals(
18216a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.dataItem.getContentValues().getAsString(Data.DATA5)) ||
18226a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                GPLUS_PROFILE_DATA_5_VIEW_PROFILE.equals(
18236a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        dataModel.dataItem.getContentValues().getAsString(Data.DATA5))) {
18246a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateIntent = secondIntent;
18256a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateContentDescription = new StringBuilder(
18266a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    dataModel.secondDataItem.buildDataStringForDisplay(dataModel.context,
18276a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            dataModel.secondDataItem.getDataKind()));
18286a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
18296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
18306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
18316bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static String getIntentResolveLabel(Intent intent, Context context) {
18326bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent,
1833dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
1834dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1835dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
1836dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
1837dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
1838dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
1839dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
1840dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
18416bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            bestResolve = ResolveCache.getInstance(context).getBestResolve(intent, matches);
1842dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1843dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1844dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
1845dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
1846dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1847dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
18486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return String.valueOf(bestResolve.loadLabel(context.getPackageManager()));
1849dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
1850dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1851edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
185231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
185331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
185431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
185531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
185631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
185731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
185831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
185931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
186031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1861faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
186231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1863faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
1864faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
1865929a62f5c1bcdc3773270f7d189d88c0464b36adJay Shrauner                if (imageViewDrawable instanceof BitmapDrawable && mContactData != null
186695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData() != null
186795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData().length > 0) {
186895c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // Perform the color analysis on the thumbnail instead of the full sized
186995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // image, so that our results will be as similar as possible to the Bugle
187095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // app.
187195c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    final Bitmap bitmap = BitmapFactory.decodeByteArray(
187295c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData(), 0,
187395c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData().length);
187495c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    try {
187595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        final int primaryColor = colorFromBitmap(bitmap);
187695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        if (primaryColor != 0) {
187795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
187895c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                                    primaryColor);
187995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        }
188095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    } finally {
188195c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        bitmap.recycle();
1882faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
18838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
18848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
1885faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
1886a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
188731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
1888a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
188931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
189031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
189131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1892faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
1893faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
18948571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
18958571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
18968571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
18978571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
18988571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
18998571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
19008571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
19018571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
19028571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
19038571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
19048571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
19058571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
1906faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
190731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
190831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
190931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
191031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
191131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
1912faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
19139b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
19148e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mColorFilterColor = palette.mPrimaryColor;
19158e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mScroller.setHeaderTintColor(mColorFilterColor);
1916faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
19179b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
19188571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
19199b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
19208e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell                new PorterDuffColorFilter(mColorFilterColor, PorterDuff.Mode.SRC_ATOP);
19218e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mContactCard.setColorAndFilter(mColorFilterColor, mColorFilter);
19228e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mRecentCard.setColorAndFilter(mColorFilterColor, mColorFilter);
19238e29faf7730f744b8f6ff117cae04b5bca1235dbBrian Attwell        mAboutCard.setColorAndFilter(mColorFilterColor, mColorFilter);
19249b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
19259b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
19268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
19278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller == null) {
19288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
19298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
19308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
19318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
19328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
19338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
19348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
19358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
19368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
19378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
1938847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
1939847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell                getWindow().getStatusBarColor(), desiredStatusBarColor);
1940847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
1941847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setEvaluator(new ArgbEvaluator());
1942847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.start();
19438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
19448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
19458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
19468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
19478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
19488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
1949a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
1950a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
19518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
19528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
19538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
19548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1955b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
1956eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1957b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
1958ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            if (interaction == null) {
1959ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                continue;
1960ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            }
1961ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
1962ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
1963b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
1964b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
1965b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
1966b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
1967b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
196823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    interaction.getContentDescription(this),
1969b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
1970dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
1971dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
1972dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
197348ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
19742a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
197548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* EntryContextMenuInfo = */ null,
197648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
197748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
197848290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* thirdContentDescription = */ null,
197948290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    interaction.getIconResourceId()));
1980b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1981b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
1982b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1983b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1984eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
1985851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
1986cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1987851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
1988405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
1989cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1990cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1991cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1992851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
19938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
1994930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            try {
19958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1996930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (isFinishing()) {
1997930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    return;
1998930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                }
1999930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (data.isError()) {
200002ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    // This means either the contact is invalid or we had an
200102ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    // internal error such as an acore crash.
200202ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    Log.i(TAG, "Failed to load contact: " + ((ContactLoader)loader).getLookupUri());
200302ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
200402ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                            Toast.LENGTH_LONG).show();
200502ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    finish();
200602ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    return;
2007930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                }
2008930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (data.isNotFound()) {
20098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
20108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
20118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
2012930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    finish();
2013930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    return;
20148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
2015cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2016930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                bindContactData(data);
2017cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2018930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            } finally {
2019930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                Trace.endSection();
2020930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            }
2021cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
2022cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
2023cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
2024851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
2025cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
2026cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
2027cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
2028d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
2029d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
2030b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
2031d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
20328571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
2033cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
2034cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
2035b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2036b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
2037b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
2038b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
20398477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
20408477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
20418477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
2042b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
2043b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
2044b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
2045b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
2046b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
20478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
20488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
20498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
20508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
20518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
20528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
20538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
20548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
2055eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
2056b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
2057b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2058b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2059b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
2060b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
2061b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
2062b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
2063b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
2064b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
2065ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
2066b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
2067b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
2068899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
2069ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
2070ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
2071ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
2072ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
2073ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
2074899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
2075899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
2076ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
2077899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
2078899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
2079899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
2080899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
2081899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
2082ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
2083ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
2084ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
2085ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
2086ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
2087b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
2088b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
2089b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2090b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2091b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2092b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
2093b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
2094b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
2095b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2096b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
2097b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
2098b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
2099b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2100b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2101b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2102b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
2103b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
2104b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2105b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
2106b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2107b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
2108b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
2109b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
2110b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2111b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
2112eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
211330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        final List<List<Entry>> interactionsWrapper = new ArrayList<>();
2114b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2115ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        // Serialize mRecentLoaderResults into a single list. This should be done on the main
2116ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        // thread to avoid races against mRecentLoaderResults edits.
2117ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
2118ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell            allInteractions.addAll(loaderInteractions);
2119ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell        }
2120ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell
212130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        mRecentDataTask = new AsyncTask<Void, Void, Void>() {
2122b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
212330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            protected Void doInBackground(Void... params) {
212430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("sort recent loader results");
212530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
212630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // Sort the interactions by most recent
212730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
212830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    @Override
212930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    public int compare(ContactInteraction a, ContactInteraction b) {
2130ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a == null && b == null) {
2131ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 0;
2132ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2133ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a == null) {
2134ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 1;
2135ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2136ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (b == null) {
2137ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return -1;
2138ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2139ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a.getInteractionDate() > b.getInteractionDate()) {
2140ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return -1;
2141ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2142ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        if (a.getInteractionDate() == b.getInteractionDate()) {
2143ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                            return 0;
2144ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        }
2145ed1882813d7c6b87543d8006a9d02aa55fef8b0dBrian Attwell                        return 1;
214630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    }
214730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                });
214830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
214930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
215030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("contactInteractionsToEntries");
215130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
215230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // Wrap each interaction in its own list so that an icon is displayed for each entry
215330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                for (Entry contactInteraction : contactInteractionsToEntries(allInteractions)) {
215430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    List<Entry> entryListWrapper = new ArrayList<>(1);
215530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    entryListWrapper.add(contactInteraction);
215630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    interactionsWrapper.add(entryListWrapper);
215730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
215830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
215930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
216030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                return null;
2161b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
216230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
216330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            @Override
216430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            protected void onPostExecute(Void aVoid) {
216530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                super.onPostExecute(aVoid);
216630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("initialize recents card");
216730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
216830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                if (allInteractions.size() > 0) {
216930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mRecentCard.initialize(interactionsWrapper,
2170b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
2171c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
217230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                            mExpandingEntryCardViewListener, mScroller);
217330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mRecentCard.setVisibility(View.VISIBLE);
217430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
2175eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
217630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
217730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
217830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // About card is initialized along with the contact card, but since it appears after
217930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // the recent card in the UI, we hold off until making it visible until the recent
218030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // card is also ready to avoid stuttering.
218130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                if (mAboutCard.shouldShow()) {
218230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mAboutCard.setVisibility(View.VISIBLE);
218330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                } else {
218430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mAboutCard.setVisibility(View.GONE);
218530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
218630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                mRecentDataTask = null;
218730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            }
218830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        };
218930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        mRecentDataTask.execute();
2190b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
21918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
21928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
21938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
21948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
21958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
21968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
21978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
21988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
21998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
22008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
22018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
22028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
220330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        if (mRecentDataTask != null) {
220430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            mRecentDataTask.cancel(/* mayInterruptIfRunning = */ false);
220530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        }
22068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
220723889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
220823889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
2209d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
2210d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
2211d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
2212d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
2213d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2214d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2215a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    /**
2216a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     * Returns true if it is possible to share the current contact.
2217a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     */
2218a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    private boolean isContactShareable() {
2219a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
2220a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    }
2221a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
22226095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
22235a7a23bdb698b8e741a425c9617c5e33e6314cddWalter Jang        return EditorIntents.createCompactEditContactIntent(
22241e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                mContactData.getLookupUri(),
22251e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                mHasComputedThemeColor
22261e8801bc9bc60bdd1c95f582c460590272cfad64Walter Jang                        ? new MaterialPalette(mColorFilterColor, mStatusBarColor) : null,
2227ac679af994faece43536f1e8b2613f30c50300e1Walter Jang                /* updatedPhotos =*/ null,
2228398cd4b1fbfa7d2fabfc4addf6a156a841a5aa6aWalter Jang                mContactData.getPhotoId(),
2229398cd4b1fbfa7d2fabfc4addf6a156a841a5aa6aWalter Jang                mContactData.getNameRawContactId());
22306095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
22316095369885edcca566a812b551886e29c7ff8039Brian Attwell
22326095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
2233a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        mHasIntentLaunched = true;
2234c00a0b5370f0714f6af1ea9b3e29b10f25e91af0Zheng Fu        mContactLoader.cacheResult();
22356095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
2236d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2237d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
22385d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang    private void deleteContact() {
22395d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang        final Uri contactUri = mContactData.getLookupUri();
22405d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang        ContactDeletionInteraction.start(this, contactUri, /* finishActivityWhenDone =*/ true);
22415d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang    }
22425d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang
2243d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
2244d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
224563176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        if (mContactData != null) {
2246d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
2247d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
2248d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
2249d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
2250d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2251d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
2252333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
2253d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
2254d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
2255d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2256d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
2257eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
225863176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell                    QuickContactActivity.this, mContactData.getLookupUri(), !isStarred);
2259d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
226035ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell
226135ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            final CharSequence accessibilityText = !isStarred
226235ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    ? getResources().getText(R.string.description_action_menu_add_star)
226335ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    : getResources().getText(R.string.description_action_menu_remove_star);
226435ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // Accessibility actions need to have an associated view. We can't access the MenuItem's
226535ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // underlying view, so put this accessibility action on the root view.
226635ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            mScroller.announceForAccessibility(accessibilityText);
2267d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
2268d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2269d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2270752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
2271752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
2272652936f204b15097d030e870dda5054d8115cbf3Brian Attwell        final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
2273752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
2274652936f204b15097d030e870dda5054d8115cbf3Brian Attwell        // Even though the data doesn't need to be set for ACTION_SEND, it does need
2275652936f204b15097d030e870dda5054d8115cbf3Brian Attwell        // to be set so that FLAG_GRANT_READ_URI_PERMISSION can create a URI permission grant.
22768dcb4ed13da40fc609e6913708c3d730ba4f0574Brian Attwell        intent.setDataAndType(shareUri, Contacts.CONTENT_VCARD_TYPE);
22778dcb4ed13da40fc609e6913708c3d730ba4f0574Brian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
22788dcb4ed13da40fc609e6913708c3d730ba4f0574Brian Attwell        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
2279752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2280752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
2281752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
2282752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
2283752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2284752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
2285a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
2286652936f204b15097d030e870dda5054d8115cbf3Brian Attwell            ImplicitIntentsUtil.startActivityOutsideApp(this, chooseIntent);
2287eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
2288752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
2289752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
2290752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
2291752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2292752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
2293752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
2294752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
2295752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
2296752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
2297752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
2298752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2299752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
2300752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
2301752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
2302752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
2303752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
2304752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
2305752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2306752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
2307752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
2308752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        Toast.makeText(QuickContactActivity.this,
2309752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                R.string.createContactShortcutSuccessful,
2310752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
2311752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
2312752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2313752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
231463176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        builder.createContactShortcutIntent(mContactData.getLookupUri());
2315752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
2316752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
231766965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell    private boolean isShortcutCreatable() {
23181c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner        if (mContactData == null || mContactData.isUserProfile() ||
23191c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                mContactData.isDirectoryEntry()) {
23208d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell            return false;
23218d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell        }
232266965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        final Intent createShortcutIntent = new Intent();
232366965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        createShortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
232466965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        final List<ResolveInfo> receivers = getPackageManager()
232566965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell                .queryBroadcastReceivers(createShortcutIntent, 0);
232666965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        return receivers != null && receivers.size() > 0;
232766965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell    }
232866965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell
2329d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2330d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
2331eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
2332d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
2333d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
2334d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2335d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2336d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2337d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
2338d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
2339d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
2340333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
2341d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
2342d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
2343a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
2344d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
2345d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
2346d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
2347d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
2348d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
234930cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
23502e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
2351d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
2352d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
23532e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
2354d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
2355d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
2356d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
2357a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
23585d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            final MenuItem deleteMenuItem = menu.findItem(R.id.menu_delete);
23595d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            deleteMenuItem.setVisible(isContactEditable());
23605d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang
2361a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            final MenuItem shareMenuItem = menu.findItem(R.id.menu_share);
2362a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            shareMenuItem.setVisible(isContactShareable());
2363a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
236466965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell            final MenuItem shortcutMenuItem = menu.findItem(R.id.menu_create_contact_shortcut);
236566965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell            shortcutMenuItem.setVisible(isShortcutCreatable());
236666965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell
236756bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            final MenuItem helpMenu = menu.findItem(R.id.menu_help);
236856bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            helpMenu.setVisible(HelpUtils.isHelpAndFeedbackAvailable());
236956bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell
23708a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
2371d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
23728a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
2373d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2374d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2375d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2376d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
2377d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
2378d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
2379d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
2380d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
2381d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
2382d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
2383ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // This action is used to launch the contact selector, with the option of
2384ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // creating a new contact. Creating a new contact is an INSERT, while selecting
2385ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // an exisiting one is an edit. The fields in the edit screen will be
2386ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // prepopulated with data.
2387ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2388ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
2389ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.setType(Contacts.CONTENT_ITEM_TYPE);
2390ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2391fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    ArrayList<ContentValues> values = mContactData.getContentValues();
2392fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell
2393fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    // Only pre-fill the name field if the provided display name is an nickname
2394fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    // or better (e.g. structured name, nickname)
2395fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    if (mContactData.getDisplayNameSource() >= DisplayNameSources.NICKNAME) {
2396ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.NAME, mContactData.getDisplayName());
2397fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    } else if (mContactData.getDisplayNameSource()
2398fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                            == DisplayNameSources.ORGANIZATION) {
2399fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // This is probably an organization. Instead of copying the organization
2400fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // name into a name entry, copy it into the organization entry. This
2401fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // way we will still consider the contact an organization.
2402fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        final ContentValues organization = new ContentValues();
2403fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        organization.put(Organization.COMPANY, mContactData.getDisplayName());
2404fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        organization.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
2405fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        values.add(organization);
2406ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2407fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell
2408ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Last time used and times used are aggregated values from the usage stat
2409ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // table. They need to be removed from data values so the SQL table can insert
2410ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // properly
2411ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    for (ContentValues value : values) {
2412ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.LAST_TIME_USED);
2413ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.TIMES_USED);
2414ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2415ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.putExtra(Intents.Insert.DATA, values);
2416ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2417ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // If the contact can only export to the same account, add it to the intent.
2418ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Otherwise the ContactEditorFragment will show a dialog for selecting an
2419ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // account.
2420ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    if (mContactData.getDirectoryExportSupport() ==
2421ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                            Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY) {
24224a1c574cd62eb7ca1e0fcc3a61e5378e5e0787feBrian Attwell                        intent.putExtra(Intents.Insert.EXTRA_ACCOUNT,
2423ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                new Account(mContactData.getDirectoryAccountName(),
2424ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                        mContactData.getDirectoryAccountType()));
24254a1c574cd62eb7ca1e0fcc3a61e5378e5e0787feBrian Attwell                        intent.putExtra(Intents.Insert.EXTRA_DATA_SET,
2426ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                mContactData.getRawContacts().get(0).getDataSet());
2427ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2428ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2429f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    // Add this flag to disable the delete menu option on directory contact joins
2430f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    // with local contacts. The delete option is ambiguous when joining contacts.
2431f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    intent.putExtra(ContactEditorFragment.INTENT_EXTRA_DISABLE_DELETE_MENU_OPTION,
2432f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                            true);
2433f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos
2434ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    startActivityForResult(intent, REQUEST_CODE_CONTACT_SELECTION_ACTIVITY);
2435d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
2436d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
2437d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
2438d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
2439d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
2440d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
24415d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            case R.id.menu_delete:
24421c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                if (isContactEditable()) {
24431c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                    deleteContact();
24441c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                }
24455d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang                return true;
2446752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
244749de62f06d24653484adebd69123eeff2850a757Jay Shrauner                if (isContactShareable()) {
244849de62f06d24653484adebd69123eeff2850a757Jay Shrauner                    shareContact();
244949de62f06d24653484adebd69123eeff2850a757Jay Shrauner                }
2450752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
2451752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
24521c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                if (isShortcutCreatable()) {
24531c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                    createLauncherShortcutWithContact();
24541c06ce7ba10cb95183a704d5835e8005c023f726Jay Shrauner                }
2455752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
245656bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell            case R.id.menu_help:
245756bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell                HelpUtils.launchHelpAndFeedbackForContactScreen(this);
245856bcc2fc455dcb434545fcdeb07c5f916dece847Brian Attwell                return true;
2459d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
2460d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
2461d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
246223889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
2463edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
2464