QuickContactActivity.java revision 245d3d2000150cbb21111370f42addaeeb1b10a0
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;
48752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract;
49edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Email;
50eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Event;
51eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;
52eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Identity;
53eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Im;
54eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Nickname;
55eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Note;
56eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Organization;
57edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Phone;
58eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Relation;
59edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
60edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
61edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Website;
62752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract.Contacts;
6360e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulosimport android.provider.ContactsContract.Data;
64ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.provider.ContactsContract.Directory;
65eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.DisplayNameSources;
66ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.provider.ContactsContract.DataUsageFeedback;
67ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulosimport android.provider.ContactsContract.Intents;
68edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.QuickContact;
69edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.RawContacts;
7031b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
710319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.PhoneAccount;
720319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.TelecomManager;
73c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwellimport android.text.BidiFormatter;
747ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jangimport android.text.SpannableString;
75c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwellimport android.text.TextDirectionHeuristics;
76edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
77edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
782a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu;
792a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.ContextMenu.ContextMenuInfo;
80d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
81d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
82eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
83f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chenimport android.view.MotionEvent;
84edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View;
85edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View.OnClickListener;
862a4207fb39330e840436215c896cde911489e111Paul Soulosimport android.view.View.OnCreateContextMenuListener;
8710d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawaimport android.view.WindowManager;
88edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
89d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
90edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
9172b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
92d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
932d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
94e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
95eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
962a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.common.ClipboardUtils;
97eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
98eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
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;
124eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.util.DateUtils;
125faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils;
126faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
127d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwellimport com.android.contacts.common.util.ViewUtil;
128333091ae754ddfc25714c14b9b89534be24379f9Paul Soulosimport com.android.contacts.detail.ContactDisplayUtils;
129f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulosimport com.android.contacts.editor.ContactEditorFragment;
130899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
131ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
133b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
134b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1352d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
1362a4207fb39330e840436215c896cde911489e111Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryContextMenuInfo;
1372ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.EntryTag;
138e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
139493f8d14b1d974a2939a110ce89fa68e0c915c36Brian Attwellimport com.android.contacts.quickcontact.WebAddress.ParseException;
140e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
141eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
142b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
143eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
144b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
145b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
14602eaf11669d25fd885606b3b0700358323532cddBrian Attwellimport com.android.contacts.widget.QuickContactImageView;
147e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
148e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
14982a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shraunerimport java.lang.SecurityException;
1502d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
151899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
152eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
153b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
154b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
155eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
156edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
157edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
158b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
15912ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shraunerimport java.util.concurrent.ConcurrentHashMap;
160edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
161edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
162edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
163edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
164edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
165edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
166d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellpublic class QuickContactActivity extends ContactsActivity {
1678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
1698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
1708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
1718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
1728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
1738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
1748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
175edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
176edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1779b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
1789b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1799b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
1808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
1810b267fec5cb5b2d836057b16d0085be349be5243Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xC8, 0, 0, 0);
182ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos    private static final int REQUEST_CODE_CONTACT_SELECTION_ACTIVITY = 2;
183ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
184dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
185752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
186752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
187752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
188edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
189edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
190edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
191edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
192e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
193e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
1946a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE = "addtocircle";
1956a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String GPLUS_PROFILE_DATA_5_VIEW_PROFILE = "view";
196e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
197e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
1986a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String HANGOUTS_DATA_5_VIDEO = "hangout";
1996a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final String HANGOUTS_DATA_5_MESSAGE = "conversation";
20048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos    private static final String CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY =
20148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos            "com.android.contacts.quickcontact.QuickContactActivity";
202e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
20363176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell    /**
20463176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * The URI used to load the the Contact. Once the contact is loaded, use Contact#getLookupUri()
20563176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     * instead of referencing this URI.
20663176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell     */
207edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
208edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
2098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
2108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
2118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
21297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private boolean mOnlyOnePhoneNumber;
21397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private boolean mOnlyOneEmail;
214edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
21502eaf11669d25fd885606b3b0700358323532cddBrian Attwell    private QuickContactImageView mPhotoView;
216eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
2176095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
218b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
219eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
220b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
221d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
2226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private AsyncTask<Void, Void, Cp2DataCardModel> mEntriesAndActionsTask;
22330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell    private AsyncTask<Void, Void, Void> mRecentDataTask;
2246bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
2256bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * The last copy of Cp2DataCardModel that was passed to {@link #populateContactAndAboutCard}.
2266bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
2276bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel mCachedCp2DataCardModel;
228f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    /**
229f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  This scrim's opacity is controlled in two different ways. 1) Before the initial entrance
230f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  animation finishes, the opacity is animated by a value animator. This is designed to
231f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  distract the user from the length of the initial loading time. 2) After the initial
232f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     *  entrance animation, the opacity is directly related to scroll position.
233f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell     */
2349b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
235f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell    private boolean mIsEntranceAnimationFinished;
236a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
2378477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
2388571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
239edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
240a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    /**
241a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * Used to stop the ExpandingEntry cards from adjusting between an entry click and the intent
242a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     * being launched.
243a42ef76251778161d27bc07db214b8c81720e476Paul Soulos     */
244a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    private boolean mHasIntentLaunched;
245a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
2466cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
2479815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
2489b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
2499815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
2509758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
2519758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
252edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
253cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
254edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
255edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
256edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
257edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
258edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
259cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
260405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
261edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
262a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private static final List<String> SORTED_ABOUT_CARD_MIMETYPES = Lists.newArrayList(
263a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Nickname.CONTENT_ITEM_TYPE,
264a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name is inserted after nickname if it is available.
265a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // No mimetype for phonetic name exists.
266a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Website.CONTENT_ITEM_TYPE,
267a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Organization.CONTENT_ITEM_TYPE,
268a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Event.CONTENT_ITEM_TYPE,
269a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Relation.CONTENT_ITEM_TYPE,
270a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Im.CONTENT_ITEM_TYPE,
271a153dba33c2152332c5706178a367f52b0550723Paul Soulos            GroupMembership.CONTENT_ITEM_TYPE,
272a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Identity.CONTENT_ITEM_TYPE,
273a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Note.CONTENT_ITEM_TYPE);
274eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
275c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell    private static final BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
276c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell
277b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
278b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
279b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
280ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
281ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
282ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
283b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
284b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
285b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
286ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
287ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
288899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
289ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
290ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
291899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
292899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
293899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
29440d7a65c6e68967f3f486715b194c144d93c296fPaul Soulos            1L * 24L * 60L * 60L * 1000L /* 1 day */;
295899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
29640d7a65c6e68967f3f486715b194c144d93c296fPaul Soulos            7L * 24L * 60L * 60L * 1000L /* 7 days */;
297899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
298ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
299ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
300ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
3016095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
3026095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
3036095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
304ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
305ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
306ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
307ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
308ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
309ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
31012ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner    /**
31112ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * ConcurrentHashMap constructor params: 4 is initial table size, 0.9f is
31212ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * load factor before resizing, 1 means we only expect a single thread to
31312ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     * write to the map so make only a single shard
31412ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner     */
31512ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults =
31612ac1e6f15ae9e4d4e453e7f28467fdd561939a3Jay Shrauner        new ConcurrentHashMap<>(4, 0.9f, 1);
317b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
318d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
3196cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
3202d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
3212d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
3222d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
3232ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Object entryTagObject = v.getTag();
3242ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (entryTagObject == null || !(entryTagObject instanceof EntryTag)) {
3252ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                Log.w(TAG, "EntryTag was not used correctly");
3266095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
3276095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
3282ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final EntryTag entryTag = (EntryTag) entryTagObject;
3292ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final Intent intent = entryTag.getIntent();
3302ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            final int dataId = entryTag.getId();
3312ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos
3322ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
3332ed2a73d15bd80bced18e5924d3f84c8a9e5e5a5Paul Soulos                editContact();
3342d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
3352d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
336ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
337ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
338ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
339ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
340ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
34120bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            final Uri intentUri = intent.getData();
34220bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            if ((intentUri != null && intentUri.getScheme() != null &&
3431cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                    intentUri.getScheme().equals(ContactsUtils.SCHEME_SMSTO)) ||
344ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
345ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
346ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
347ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
348ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
349ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
35020bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                final Uri dataUsageUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
351ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
352ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
353ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
354ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                final boolean successful = getContentResolver().update(
35520bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                        dataUsageUri, new ContentValues(), null, null) > 0;
356ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                if (!successful) {
357ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    Log.w(TAG, "DataUsageFeedback increment failed");
358ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
359ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
360ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
361ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
362ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
36370e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            // Pass the touch point through the intent for use in the InCallUI
36470e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            if (Intent.ACTION_CALL.equals(intent.getAction())) {
3651f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                if (TouchPointManager.getInstance().hasValidPoint()) {
36670e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                    Bundle extras = new Bundle();
3671f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                    extras.putParcelable(TouchPointManager.TOUCH_POINT,
3681f8a3fda87bf61e579d61e3fdd246548943d070aNancy Chen                            TouchPointManager.getInstance().getPoint());
3690319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunn                    intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
37070e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen                }
37170e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen            }
37270e3f4f45c040a2d09bfb46462242b450cf20b29Nancy Chen
373793e73c859ed839c28cc37d80d62bde0a361c78aPaul Soulos            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
374793e73c859ed839c28cc37d80d62bde0a361c78aPaul Soulos
375a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
376417309a770568d7b278abc91c46a739020e48157Jay Shrauner            try {
377417309a770568d7b278abc91c46a739020e48157Jay Shrauner                startActivity(intent);
37882a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner            } catch (SecurityException ex) {
37982a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
38082a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                        Toast.LENGTH_SHORT).show();
38182a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                Log.e(TAG, "QuickContacts does not have permission to launch "
38282a5a35c1a2b83b4118bcba56625f6f25e9663d0Jay Shrauner                        + intent);
383417309a770568d7b278abc91c46a739020e48157Jay Shrauner            } catch (ActivityNotFoundException ex) {
384417309a770568d7b278abc91c46a739020e48157Jay Shrauner                Toast.makeText(QuickContactActivity.this, R.string.missing_app,
385417309a770568d7b278abc91c46a739020e48157Jay Shrauner                        Toast.LENGTH_SHORT).show();
386417309a770568d7b278abc91c46a739020e48157Jay Shrauner            }
3872d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
3882d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
3892d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
390e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
391e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
392e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
393e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
394e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
395e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
3960cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
3970cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        @Override
398245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        public void onExpand() {
399245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell            mScroller.setDisableTouchesForSuppressLayout(/* areTouchesDisabled = */ true);
400245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        }
401245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell
402245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        @Override
403245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell        public void onExpandDone() {
404245d3d2000150cbb21111370f42addaeeb1b10a0Brian Attwell            mScroller.setDisableTouchesForSuppressLayout(/* areTouchesDisabled = */ false);
4050cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        }
406e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
407e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
40897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    private interface ContextMenuIds {
40997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int COPY_TEXT = 0;
41097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int CLEAR_DEFAULT = 1;
41197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        static final int SET_DEFAULT = 2;
41297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos    }
41397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
4142a4207fb39330e840436215c896cde911489e111Paul Soulos    private final OnCreateContextMenuListener mEntryContextMenuListener =
4152a4207fb39330e840436215c896cde911489e111Paul Soulos            new OnCreateContextMenuListener() {
4162a4207fb39330e840436215c896cde911489e111Paul Soulos        @Override
4172a4207fb39330e840436215c896cde911489e111Paul Soulos        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4182a4207fb39330e840436215c896cde911489e111Paul Soulos            if (menuInfo == null) {
4192a4207fb39330e840436215c896cde911489e111Paul Soulos                return;
4202a4207fb39330e840436215c896cde911489e111Paul Soulos            }
42197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            final EntryContextMenuInfo info = (EntryContextMenuInfo) menuInfo;
4222a4207fb39330e840436215c896cde911489e111Paul Soulos            menu.setHeaderTitle(info.getCopyText());
42397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            menu.add(ContextMenu.NONE, ContextMenuIds.COPY_TEXT,
42497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    ContextMenu.NONE, getString(R.string.copy_text));
42597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
42697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Don't allow setting or clearing of defaults for non-editable contacts
42797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (!isContactEditable()) {
42897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return;
42997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
43097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
43197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            final String selectedMimeType = info.getMimeType();
43297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
43397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Defaults to true will only enable the detail to be copied to the clipboard.
43497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            boolean onlyOneOfMimeType = true;
43597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
43697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Only allow primary support for Phone and Email content types
43797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (Phone.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
43897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                onlyOneOfMimeType = mOnlyOnePhoneNumber;
43997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            } else if (Email.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
44097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                onlyOneOfMimeType = mOnlyOneEmail;
44197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
44297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos
44397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            // Checking for previously set default
44497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            if (info.isSuperPrimary()) {
44597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                menu.add(ContextMenu.NONE, ContextMenuIds.CLEAR_DEFAULT,
44697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        ContextMenu.NONE, getString(R.string.clear_default));
44797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            } else if (!onlyOneOfMimeType) {
44897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                menu.add(ContextMenu.NONE, ContextMenuIds.SET_DEFAULT,
44997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        ContextMenu.NONE, getString(R.string.set_default));
45097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            }
4512a4207fb39330e840436215c896cde911489e111Paul Soulos        }
4522a4207fb39330e840436215c896cde911489e111Paul Soulos    };
4532a4207fb39330e840436215c896cde911489e111Paul Soulos
4542a4207fb39330e840436215c896cde911489e111Paul Soulos    @Override
4552a4207fb39330e840436215c896cde911489e111Paul Soulos    public boolean onContextItemSelected(MenuItem item) {
4562a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo menuInfo;
4572a4207fb39330e840436215c896cde911489e111Paul Soulos        try {
4582a4207fb39330e840436215c896cde911489e111Paul Soulos            menuInfo = (EntryContextMenuInfo) item.getMenuInfo();
4592a4207fb39330e840436215c896cde911489e111Paul Soulos        } catch (ClassCastException e) {
4602a4207fb39330e840436215c896cde911489e111Paul Soulos            Log.e(TAG, "bad menuInfo", e);
4612a4207fb39330e840436215c896cde911489e111Paul Soulos            return false;
4622a4207fb39330e840436215c896cde911489e111Paul Soulos        }
4632a4207fb39330e840436215c896cde911489e111Paul Soulos
46497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        switch (item.getItemId()) {
46597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.COPY_TEXT:
46697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                ClipboardUtils.copyText(this, menuInfo.getCopyLabel(), menuInfo.getCopyText(),
46797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        true);
46897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
46997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.SET_DEFAULT:
47097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                final Intent setIntent = ContactSaveService.createSetSuperPrimaryIntent(this,
47197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        menuInfo.getId());
47297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                this.startService(setIntent);
47397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
47497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            case ContextMenuIds.CLEAR_DEFAULT:
47597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                final Intent clearIntent = ContactSaveService.createClearPrimaryIntent(this,
47697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        menuInfo.getId());
47797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                this.startService(clearIntent);
47897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                return true;
47997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            default:
48097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                throw new IllegalArgumentException("Unknown menu option " + item.getItemId());
48197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
4822a4207fb39330e840436215c896cde911489e111Paul Soulos    }
4832a4207fb39330e840436215c896cde911489e111Paul Soulos
484d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
485d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
486d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
487d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
488d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
489d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
490d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
491d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
492d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
493d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
494d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
495d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
496d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
497d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
498d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
499d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
500d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
501d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
502d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
503d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
504d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
505d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
506d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
507d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
508d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
509d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
510d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
511d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
512d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
513d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
514b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
515b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
516b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
517b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
518f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            finish();
519b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
5208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
5228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
5238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
5248a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
5258a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
5278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
5288a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
5298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
5308477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
5318477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
5328477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
5338477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
5348477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
5358477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
5368477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
537f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onEntranceAnimationDone() {
538f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            mIsEntranceAnimationFinished = true;
539f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        }
540f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
541f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        @Override
542f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        public void onTransparentViewHeightChange(float ratio) {
543f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell            if (mIsEntranceAnimationFinished) {
544f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                mWindowScrim.setAlpha((int) (0xFF * ratio));
5458477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
5468477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
547b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
548b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
549eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
550eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
551eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
552eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
553eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
554eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
555eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
556eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
557eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
558eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
559eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
560eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
561eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
562eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
563eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
564eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
565eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
566eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
567eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
568eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
569eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
570eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
571eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
572eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
573eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
574eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
575eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
576eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
577eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
578eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
579eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
580eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
581eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
582eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
583eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
584eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
585cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
586cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
587cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 1. Times used
588cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 2. Last time used
589cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 3. Statically defined
590cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
591eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
592eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
593eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
594eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
595eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem lhs = lhsList.get(0);
596eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem rhs = rhsList.get(0);
597eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
598eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
599eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
600eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
601eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
602eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
603eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
604eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
605eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
606eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
607eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
608eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
609eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
610eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
611eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
612eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
613eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
614eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
615eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Times used and last time used are the same. Resort to statically defined.
616eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String lhsMimeType = lhs.getMimeType();
617eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String rhsMimeType = rhs.getMimeType();
618eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (String mimeType : LEADING_MIMETYPES) {
619eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (lhsMimeType.equals(mimeType)) {
620eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return -1;
621eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                } else if (rhsMimeType.equals(mimeType)) {
622eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return 1;
623eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
624eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
625eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
626eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
627eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
628eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
629edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
630f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    public boolean dispatchTouchEvent(MotionEvent ev) {
631f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
632f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen            TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
633f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        }
634f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen        return super.dispatchTouchEvent(ev);
635f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    }
636f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen
637f619e432bcebb7f91c9ed88fc384abab16a9cd6aNancy Chen    @Override
6388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
6398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
6408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
641edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
6428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getWindow().setStatusBarColor(Color.TRANSPARENT);
6432426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
644d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
645dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
64610d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
64710d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
64810d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
64910d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
650edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
651edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
652a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
653a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
6540cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
6550cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
656eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
6576095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
658b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
659eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
660edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
6616095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
662eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
663eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
66423889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
6652a4207fb39330e840436215c896cde911489e111Paul Soulos        mContactCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
666b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
667b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
668b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
669edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
670eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
6712a4207fb39330e840436215c896cde911489e111Paul Soulos        mAboutCard.setOnCreateContextMenuListener(mEntryContextMenuListener);
672eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
67302eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView = (QuickContactImageView) findViewById(R.id.photo);
67463176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        final View transparentView = findViewById(R.id.transparent_view);
6750d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
67663176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell            transparentView.setOnClickListener(new OnClickListener() {
6770d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
6780d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
6790d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
6800d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
6810d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
6820d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
683edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
684d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        // Allow a shadow to be shown under the toolbar.
685d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
686d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell
687d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
688d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
6899b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
6909b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
6919b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
6929b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
6936219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
6948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
695f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = mHasAlreadyBeenOpened;
6969b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
697f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mWindowScrim.setAlpha(0);
6989b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
699edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
700c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
701ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
702ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
703ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
7048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7059b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
7069b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
707d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
708d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
709d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
710d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
711d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
712d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
713d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
714d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
715d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
71623889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
717f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ true,
718f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                new Runnable() {
719f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    @Override
720f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    public void run() {
721f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        if (!mHasAlreadyBeenOpened) {
722f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // The initial scrim opacity must match the scrim opacity that would be
723f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            // achieved by scrolling to the starting position.
724f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final float alphaRatio = mExtraMode == MODE_FULLY_EXPANDED ?
725f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    1 : mScroller.getStartingTransparentHeightRatio();
726f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int duration = getResources().getInteger(
727f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    android.R.integer.config_shortAnimTime);
728f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            final int desiredAlpha = (int) (0xFF * alphaRatio);
729f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            ObjectAnimator o = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0,
730f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                                    desiredAlpha).setDuration(duration);
731f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
732f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                            o.start();
733f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                        }
734f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                    }
735f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell                });
736f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell
7379b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
7389b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
739ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
740ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
741ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
742ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
743ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
744ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
745ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
746ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
747ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
748ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
749ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
750ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
751ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
752ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
753a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
754a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
7559b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
756ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
757ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
7589b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
7599b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
7608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
7618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
7628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
763405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
764405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
7658a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
7668a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED) {
7678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // The contact that we were showing has been deleted.
7688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
769ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos        } else if (requestCode == REQUEST_CODE_CONTACT_SELECTION_ACTIVITY &&
770ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                resultCode != RESULT_CANCELED) {
771ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos            processIntent(data);
772b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
7738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
774dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
7758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
7768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
7778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
7788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
779f1076dc20feb3ee0c687c818d70ffc1d25bef909Brian Attwell        mIsEntranceAnimationFinished = true;
7808571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
781d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
782d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
783d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
7849b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
7859b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
7869b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
7879b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
7889b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilter.getColor());
7899b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
7909b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
7919b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
792d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
793f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos        if (intent == null) {
794f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos            finish();
795f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos            return;
796f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos        }
797d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
798d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
799d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
800d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
801d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
802d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
803d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
804d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
805d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE,
806d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                QuickContact.MODE_LARGE);
807d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
808d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
809fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        if (lookupUri == null) {
810fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner            finish();
811fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner            return;
812fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        }
813fcfcae34bd2d74e71ee218cfd7aa3567d7c3fc26Jay Shrauner        mLookupUri = lookupUri;
814d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
815d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
816d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
817d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
818d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
819d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
820d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // we need to restart the loader and reload the new contact.
82159a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            destroyInteractionLoaders();
822405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
823405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
82459a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            mCachedCp2DataCardModel = null;
825d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
8262d150da246632b1649999cfabed776133b097775Brian Attwell
8272d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
8282426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
829edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
83059a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell    private void destroyInteractionLoaders() {
83159a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        for (int interactionLoaderId : mRecentLoaderIds) {
83259a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            getLoaderManager().destroyLoader(interactionLoaderId);
83359a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        }
83459a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell    }
83559a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell
836b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
8378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
8388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
8398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
8408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
841c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
842b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
843b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
84481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
845d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
8469b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
847f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString());
8489b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
84981281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
85081281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
85181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
852f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell    private void setHeaderNameText(String value) {
853d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
8549b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
855f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell                mScroller.setTitle(value);
8569b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
85781281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
85881281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
85981281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
86081281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
861edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
862edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
863edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
864edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
865edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
866edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
867edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
868edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
869edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
870edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
871edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
872edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
873edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
874edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
875cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
876edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
8778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
8788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
8796cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
880d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
881edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
8828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
8838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
884dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
88502eaf11669d25fd885606b3b0700358323532cddBrian Attwell        mPhotoView.setIsBusiness(mContactData.isDisplayNameFromOrganization());
8866219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
88731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
888333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos        setHeaderNameText(ContactDisplayUtils.getDisplayName(this, data).toString());
889edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
8908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
891dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
8926bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mEntriesAndActionsTask = new AsyncTask<Void, Void, Cp2DataCardModel>() {
8938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
8948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
8956bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected Cp2DataCardModel doInBackground(
896eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
897eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
8988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
8998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
9008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
9016bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            protected void onPostExecute(Cp2DataCardModel cardDataModel) {
9026bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                super.onPostExecute(cardDataModel);
9038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
9048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
9058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
9068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
9076bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    bindDataToCards(cardDataModel);
9088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
9098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
9108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
9118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
9128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
9138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
9148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
9156bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void bindDataToCards(Cp2DataCardModel cp2DataCardModel) {
9166bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        startInteractionLoaders(cp2DataCardModel);
9176bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        populateContactAndAboutCard(cp2DataCardModel);
918eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
919eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
9206bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void startInteractionLoaders(Cp2DataCardModel cp2DataCardModel) {
9216bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Map<String, List<DataItem>> dataItemsMap = cp2DataCardModel.dataItemsMap;
9226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> phoneDataItems = dataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
92397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        if (phoneDataItems != null && phoneDataItems.size() == 1) {
92497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            mOnlyOnePhoneNumber = true;
92597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
926eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
927eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
928eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
929eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
930eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
931eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
932eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
933ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
934eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
935eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
936eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
9378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
9388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
939ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
940ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
941ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
942ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
943ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
944ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
945ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
946ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
9478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
948899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
9498a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
950eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
951899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
9526bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<DataItem> emailDataItems = dataItemsMap.get(Email.CONTENT_ITEM_TYPE);
95397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        if (emailDataItems != null && emailDataItems.size() == 1) {
95497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            mOnlyOneEmail = true;
95597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos        }
956eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
957eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
958eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
959eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
960eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
961eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
962eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
963ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
964eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
965899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
966899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
967ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
968899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
9698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
9708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
9718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
9728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
9738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
9748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
9758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
9768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
9778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
9788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
9798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
9808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
9818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
9828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
9838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
9848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
9856bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<List<Entry>> buildAboutCardEntries(Map<String, List<DataItem>> dataItemsMap) {
986a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
987a153dba33c2152332c5706178a367f52b0550723Paul Soulos        for (String mimetype : SORTED_ABOUT_CARD_MIMETYPES) {
9886bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> mimeTypeItems = dataItemsMap.get(mimetype);
989a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (mimeTypeItems == null) {
990a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
991a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
9926bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // Set aboutCardTitleOut = null, since SORTED_ABOUT_CARD_MIMETYPES doesn't contain
9936bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            // the name mimetype.
9946bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<Entry> aboutEntries = dataItemsToEntries(mimeTypeItems,
9956bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    /* aboutCardTitleOut = */ null);
996a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutEntries.size() > 0) {
997a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(aboutEntries);
998a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
999a153dba33c2152332c5706178a367f52b0550723Paul Soulos        }
1000a153dba33c2152332c5706178a367f52b0550723Paul Soulos        return aboutCardEntries;
1001a153dba33c2152332c5706178a367f52b0550723Paul Soulos    }
1002a153dba33c2152332c5706178a367f52b0550723Paul Soulos
1003a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    @Override
1004a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    protected void onResume() {
1005a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        super.onResume();
1006a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        // If returning from a launched activity, repopulate the contact and about card
1007a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        if (mHasIntentLaunched) {
1008a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = false;
10096bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            populateContactAndAboutCard(mCachedCp2DataCardModel);
1010a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
101159a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell
101259a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // When exiting the activity and resuming, we want to force a full reload of all the
101359a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // interaction data in case something changed in the background. On screen rotation,
101459a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        // we don't need to do this. And, mCachedCp2DataCardModel will be null, so we won't.
101559a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        if (mCachedCp2DataCardModel != null) {
101659a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            destroyInteractionLoaders();
101759a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell            startInteractionLoaders(mCachedCp2DataCardModel);
101859a585b78d061c84367ec24c71e137fae55d10ceBrian Attwell        }
1019a42ef76251778161d27bc07db214b8c81720e476Paul Soulos    }
1020a42ef76251778161d27bc07db214b8c81720e476Paul Soulos
10216bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private void populateContactAndAboutCard(Cp2DataCardModel cp2DataCardModel) {
10226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        mCachedCp2DataCardModel = cp2DataCardModel;
10236bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (mHasIntentLaunched || cp2DataCardModel == null) {
1024a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            return;
1025a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        }
1026eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
1027eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
10286bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = cp2DataCardModel.contactCardEntries;
10296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = cp2DataCardModel.aboutCardEntries;
10306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final String customAboutCardName = cp2DataCardModel.customAboutCardName;
1031eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1032eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
1033eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
1034eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
1035c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mContactCard.isExpanded(),
103689966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                    /* isAlwaysExpanded = */ false,
10370cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener,
10380cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mScroller);
1039eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
1040eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1041eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
1042eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1043eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
10448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1045eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
1046c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        // Phonetic name is not a data item, so the entry needs to be created separately
1047c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        final String phoneticName = mContactData.getPhoneticName();
1048c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        if (!TextUtils.isEmpty(phoneticName)) {
1049c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            Entry phoneticEntry = new Entry(/* viewId = */ -1,
1050c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* icon = */ null,
1051c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    getResources().getString(R.string.name_phonetic),
1052c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    phoneticName,
10538a3fe52802135be2402a5b216325615fb796a509Walter Jang                    /* subHeaderIcon = */ null,
1054c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* text = */ null,
10558a3fe52802135be2402a5b216325615fb796a509Walter Jang                    /* textIcon = */ null,
105623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    /* primaryContentDescription = */ null,
1057c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* intent = */ null,
1058c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIcon = */ null,
1059c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIntent = */ null,
1060c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateContentDescription = */ null,
1061c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* shouldApplyColor = */ false,
10622a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
10632a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* EntryContextMenuInfo = */ new EntryContextMenuInfo(phoneticName,
106497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                            getResources().getString(R.string.name_phonetic),
106597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                            /* mimeType = */ null, /* id = */ -1, /* isPrimary = */ false),
106648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
106748fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
106848290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* thirdContentDescription = */ null,
106948290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* iconResourceId = */ 0);
1070c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            List<Entry> phoneticList = new ArrayList<>();
1071c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            phoneticList.add(phoneticEntry);
1072a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name comes after nickname. Check to see if the first entry type is nickname
1073a0fa4c18dc111ceea9ff47f35ee01fecc1003578Paul Soulos            if (aboutCardEntries.size() > 0 && aboutCardEntries.get(0).get(0).getHeader().equals(
1074a153dba33c2152332c5706178a367f52b0550723Paul Soulos                    getResources().getString(R.string.header_nickname_entry))) {
1075a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(1, phoneticList);
1076a153dba33c2152332c5706178a367f52b0550723Paul Soulos            } else {
1077a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(0, phoneticList);
1078a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
1079c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        }
1080c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos
10816bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        if (!TextUtils.isEmpty(customAboutCardName)) {
10826bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            mAboutCard.setTitle(customAboutCardName);
10836bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
10846bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
1085cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos        if (aboutCardEntries.size() > 0) {
1086cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos            mAboutCard.initialize(aboutCardEntries,
1087cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    /* numInitialVisibleEntries = */ 1,
1088cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    /* isExpanded = */ true,
1089cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    /* isAlwaysExpanded = */ true,
1090cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    mExpandingEntryCardViewListener,
1091cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos                    mScroller);
1092cc5ec22992ee61d130cb2ee99a038fb1761b8d35Paul Soulos        }
10936095369885edcca566a812b551886e29c7ff8039Brian Attwell
10946095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
10956095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
10966095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
10976095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
10986095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
10996095369885edcca566a812b551886e29c7ff8039Brian Attwell
1100a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
1101a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
1102a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
1103a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1104a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
1105eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1106eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1107eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1108eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
11096095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
11106095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
11116095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
11126095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
11136095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
11146095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
11156095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
11168a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* subHeader = */ null, /* subHeaderIcon = */ null, /* text = */ null,
11178a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* textIcon = */ null, /* primaryContentDescription = */ null,
111823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                getEditContactIntent(),
1119dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
1120714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
112148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* isEditable = */ false, /* EntryContextMenuInfo = */ null,
112248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* thirdIcon = */ null, /* thirdIntent = */ null,
112348290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                /* thirdContentDescription = */ null, R.drawable.ic_phone_24dp);
11246095369885edcca566a812b551886e29c7ff8039Brian Attwell
11256095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
11266095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
11276095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
11286095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
11298a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* subHeaderIcon = */ null,
11308a3fe52802135be2402a5b216325615fb796a509Walter Jang                /* text = */ null, /* textIcon = */ null, /* primaryContentDescription = */ null,
113123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                getEditContactIntent(), /* alternateIcon = */ null,
1132dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
11332a4207fb39330e840436215c896cde911489e111Paul Soulos                /* shouldApplyColor = */ true, /* isEditable = */ false,
113448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                /* EntryContextMenuInfo = */ null, /* thirdIcon = */ null,
113548290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                /* thirdIntent = */ null, /* thirdContentDescription = */ null,
113648290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                R.drawable.ic_email_24dp);
11376095369885edcca566a812b551886e29c7ff8039Brian Attwell
11386095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
11396095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
11406095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
11416095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
11426095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
11436095369885edcca566a812b551886e29c7ff8039Brian Attwell
11446095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
11456095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
11466095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
11476095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
1148c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
11490cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener, mScroller);
11506095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
11516095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
11526095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
11536095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
11546095369885edcca566a812b551886e29c7ff8039Brian Attwell
11556095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
1156eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
1157eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
1158eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
1159eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
1160eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
1161eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
11626bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private Cp2DataCardModel generateDataModelFromContact(
1163eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
1164eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
1165eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1166eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
11678bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
11688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
1169851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
1170851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
1171eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
1172eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1173851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
1174eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
1175eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
117647b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
117747b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
117847b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
1179eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
1180cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1181eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
1182b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1183eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
1184eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
1185899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
1186eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
1187edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1188eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
1189eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
1190eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
1191eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
1192edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
1193eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
1194edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1195edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
11968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
1197edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1198eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
119916339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
120016339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
1201eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
1202eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
1203eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
120416339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
1205eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
1206eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
1207eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
1208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
1209eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
1210eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
1211eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
1212eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
1213edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
1214eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
1215edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1216eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
1217eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
1218eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
1219eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
122016339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
12216bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.beginSection("cp2 data items to entries");
12226bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
12236bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> contactCardEntries = new ArrayList<>();
12246bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<List<Entry>> aboutCardEntries = buildAboutCardEntries(dataItemsMap);
12256bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final MutableString aboutCardName = new MutableString();
12266bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
12276bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        for (int i = 0; i < dataItemsList.size(); ++i) {
12286bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final List<DataItem> dataItemsByMimeType = dataItemsList.get(i);
12296bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final DataItem topDataItem = dataItemsByMimeType.get(0);
12306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
12316bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                // About card mimetypes are built in buildAboutCardEntries, skip here
12326bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                continue;
12336bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            } else {
12346bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                List<Entry> contactEntries = dataItemsToEntries(dataItemsList.get(i),
12356bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        aboutCardName);
12366bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (contactEntries.size() > 0) {
12376bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                    contactCardEntries.add(contactEntries);
12386bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                }
12396bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            }
12406bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        }
12416bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
12426bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        Trace.endSection();
12436bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
12446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final Cp2DataCardModel dataModel = new Cp2DataCardModel();
12456bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.customAboutCardName = aboutCardName.value;
12466bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.aboutCardEntries = aboutCardEntries;
12476bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.contactCardEntries = contactCardEntries;
12486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        dataModel.dataItemsMap = dataItemsMap;
12496bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return dataModel;
12506bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
12516bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
12526bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    /**
12536bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * Class used to hold the About card and Contact cards' data model that gets generated
12546bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * on a background thread. All data is from CP2.
12556bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     */
12566bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class Cp2DataCardModel {
12576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        /**
12586bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * A map between a mimetype string and the corresponding list of data items. The data items
12596bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         * are in sorted order using mWithinMimeTypeDataItemComparator.
12606bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell         */
12616bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public Map<String, List<DataItem>> dataItemsMap;
12626bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> aboutCardEntries;
12636bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public List<List<Entry>> contactCardEntries;
12646bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String customAboutCardName;
12656bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    }
12666bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell
12676bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static class MutableString {
12686bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        public String value;
1269eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1270edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1271eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
1272eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
1273eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
12746bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
12756bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * This runs on a background thread. This is set as static to avoid accidentally adding
12766bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     * additional dependencies on unsafe things (like the Activity).
12776bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell     *
1278eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
12796a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * @param secondDataItem A second {@link DataItem} to help build a full entry for some
12806a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     *  mimetypes
1281eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
1282eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
12836a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static Entry dataItemToEntry(DataItem dataItem, DataItem secondDataItem,
12846bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            Context context, Contact contactData,
12856bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            final MutableString aboutCardName) {
1286eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
1287eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
1288eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
1289eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
1290eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
1291eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
129223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        StringBuilder primaryContentDescription = new StringBuilder();
1293eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
129448ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
1295dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
1296dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
129723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        StringBuilder alternateContentDescription = new StringBuilder();
1298eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
12992a4207fb39330e840436215c896cde911489e111Paul Soulos        EntryContextMenuInfo entryContextMenuInfo = null;
130048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Drawable thirdIcon = null;
130148fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        Intent thirdIntent = null;
130248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos        String thirdContentDescription = null;
130348290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos        int iconResourceId = 0;
1304eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
13056bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        context = context.getApplicationContext();
130623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        final Resources res = context.getResources();
1307eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
1308eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1309eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
1310eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
13116bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            intent = ContactsUtils.buildImIntent(context, im).first;
1312eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
13137de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            final int protocol;
13147de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (!im.isProtocolValid()) {
13157de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = Im.PROTOCOL_CUSTOM;
13167de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
13177de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
13187de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
13197de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (protocol == Im.PROTOCOL_CUSTOM) {
13207de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // If the protocol is custom, display the "IM" entry header as well to distinguish
13217de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // this entry from other ones
132223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = res.getString(R.string.header_im_entry);
132323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                subHeader = Im.getProtocolLabel(res, protocol,
13247de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
13257de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                text = im.getData();
13267de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
132723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = Im.getProtocolLabel(res, protocol,
13287de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
13297de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = im.getData();
13307de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
133197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(im.getData(), header,
133297f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1333eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
1334eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
133523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_organization_entry);
1336eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
133797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
133897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1339eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
1340eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
1341eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
1342eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
1343eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
13446bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                (contactData.getNameRawContactId() == dataItem.getRawContactId());
1345eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1346eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
1347eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
13486bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                && contactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
1349eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1350eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
135123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                header = res.getString(R.string.header_nickname_entry);
1352eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
135397f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
135497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1355eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1356eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
1357eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
135823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_note_entry);
1359eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
136097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
136197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1362eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
1363eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
136423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_website_entry);
1365eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
136697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
136797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1368eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
13693bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                final WebAddress webAddress = new WebAddress(website.buildDataStringForDisplay
13703bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                        (context, kind));
1371eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1372eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
13733bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                Log.e(TAG, "Couldn't parse website: " + website.buildDataStringForDisplay(
13743bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                        context, kind));
1375eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1376eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1377eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
13783bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell            final String dataString = event.buildDataStringForDisplay(context, kind);
1379eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1380eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1381eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1382eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1383eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1384eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1385eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1386eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1387eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
138823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_event_entry);
1389f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
139023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                subHeader = Event.getTypeLabel(res, event.getKindTypeColumn(kind),
13917de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        event.getLabel()).toString();
1392f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
13936bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            text = DateUtils.formatDate(context, dataString);
139497f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(text, header,
139597f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1396eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1397eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
13983bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell            final String dataString = relation.buildDataStringForDisplay(context, kind);
1399eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1400eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1401eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1402eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1403eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
140423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos            header = res.getString(R.string.header_relation_entry);
1405eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
140697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header,
140797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
1408f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
140923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                text = Relation.getTypeLabel(res,
14106bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        relation.getKindTypeColumn(kind),
1411f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1412f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1413eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1414eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
1415eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
141623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.call_other)).append(" ");
14173bcf09eac69251c05cd4e386badfa292ba56b4f0Brian Attwell                header = sBidiFormatter.unicodeWrap(phone.buildDataStringForDisplay(context, kind),
1418c62cc7931593b4137f8a507689b653e1e15e1260Brian Attwell                        TextDirectionHeuristics.LTR);
14192a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
142097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.phoneLabelsGroup), dataItem.getMimeType(),
142197f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1422f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
142323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = Phone.getTypeLabel(res, phone.getKindTypeColumn(kind),
1424f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            phone.getLabel()).toString();
142523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1426f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
142723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
142823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_phone_24dp);
142948290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_phone_24dp;
14306bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                if (PhoneCapabilityTester.isPhone(context)) {
1431eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
143216339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1433dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
14341cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                        Uri.fromParts(ContactsUtils.SCHEME_SMSTO, phone.getNumber(), null));
143548fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
143623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateIcon = res.getDrawable(R.drawable.ic_message_24dp);
143723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(res.getString(R.string.sms_custom, header));
143848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos
143948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                // Add video call button if supported
1440f764730a34da560db87206f9a48390d4c8085365Paul Soulos                if (CallUtil.isVideoEnabled(context)) {
144123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    thirdIcon = res.getDrawable(R.drawable.ic_videocam);
144248fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdIntent = CallUtil.getVideoCallIntent(phone.getNumber(),
144348fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                            CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY);
144448fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    thirdContentDescription =
144523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            res.getString(R.string.description_video_call);
144648fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                }
1447eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1448eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1449eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1450eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1451eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
145223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.email_other)).append(" ");
14531cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                final Uri mailUri = Uri.fromParts(ContactsUtils.SCHEME_MAILTO, address, null);
1454eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1455eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
14562a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
145797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.emailLabelsGroup), dataItem.getMimeType(),
145897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1459f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
146023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = Email.getTypeLabel(res, email.getKindTypeColumn(kind),
1461f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
146223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1463f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
146423e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
146523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_email_24dp);
146648290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_email_24dp;
1467eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1468eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1469eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1470eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1471eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
147223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(res.getString(R.string.map_other)).append(" ");
1473eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1474eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
14752a4207fb39330e840436215c896cde911489e111Paul Soulos                entryContextMenuInfo = new EntryContextMenuInfo(header,
147697f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        res.getString(R.string.postalLabelsGroup), dataItem.getMimeType(),
147797f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        dataItem.getId(), dataItem.isSuperPrimary());
1478f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
147923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    text = StructuredPostal.getTypeLabel(res,
1480f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
148123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    primaryContentDescription.append(text).append(" ");
1482f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
148323e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                primaryContentDescription.append(header);
14846a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIntent =
14856a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                        StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
148623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateIcon = res.getDrawable(R.drawable.ic_directions_24dp);
148723e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(res.getString(
148823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                        R.string.content_description_directions)).append(" ").append(header);
148923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                icon = res.getDrawable(R.drawable.ic_place_24dp);
149048290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId = R.drawable.ic_place_24dp;
1491eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1492eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
1493593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1494593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            final String address = sip.getSipAddress();
1495593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell            if (!TextUtils.isEmpty(address)) {
1496593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                primaryContentDescription.append(res.getString(R.string.call_other)).append(
1497593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        " ");
1498593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                if (PhoneCapabilityTester.isSipPhone(context)) {
14991cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner                    final Uri callUri = Uri.fromParts(PhoneAccount.SCHEME_SIP, address, null);
1500eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
150116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1502593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                header = address;
1503593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                entryContextMenuInfo = new EntryContextMenuInfo(header,
1504593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        res.getString(R.string.phoneLabelsGroup), dataItem.getMimeType(),
1505593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                        dataItem.getId(), dataItem.isSuperPrimary());
1506593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                if (sip.hasKindTypeColumn(kind)) {
1507593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                    text = SipAddress.getTypeLabel(res,
1508593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                            sip.getKindTypeColumn(kind), sip.getLabel()).toString();
1509593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                    primaryContentDescription.append(text).append(" ");
1510593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                }
1511593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                primaryContentDescription.append(header);
1512593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                icon = res.getDrawable(R.drawable.ic_dialer_sip_black_24dp);
1513593e5a7f2551b7946ada9913408f649e530202c8Brian Attwell                iconResourceId = R.drawable.ic_dialer_sip_black_24dp;
1514edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1515eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
1516eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
1517eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(givenName)) {
151823e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                aboutCardName.value = res.getString(R.string.about_card_title) +
15196bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        " " + givenName;
1520eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
152123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                aboutCardName.value = res.getString(R.string.about_card_title);
1522eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1523eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1524eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
15256bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            header = dataItem.buildDataStringForDisplay(context, kind);
1526eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1527eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
152860e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
152960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
1530e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1531e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
1532e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
1533e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
15346a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                // Build advanced entry for known 3p types. Otherwise default to ResolveCache icon.
1535e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
1536e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
15376a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // If a secondDataItem is available, use it to build an entry with
15386a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // alternate actions
15396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        if (secondDataItem != null) {
154023e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
15416a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            alternateIcon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
15426a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            final GPlusOrHangoutsDataItemModel itemModel =
15436a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    new GPlusOrHangoutsDataItemModel(intent, alternateIntent,
15446a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            dataItem, secondDataItem, alternateContentDescription,
15456a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            header, text, context);
15466a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
154797b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            populateGPlusOrHangoutsDataItemModel(itemModel);
154897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            intent = itemModel.intent;
154997b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateIntent = itemModel.alternateIntent;
155097b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateContentDescription = itemModel.alternateContentDescription;
155197b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            header = itemModel.header;
155297b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            text = itemModel.text;
15536a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        } else {
15546a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            if (GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(
15556a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    intent.getDataString())) {
15566a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
15576a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            } else {
15586a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
15596a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            }
1560e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1561e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1562e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
15636a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // If a secondDataItem is available, use it to build an entry with
15646a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        // alternate actions
15656a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        if (secondDataItem != null) {
156623e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                            icon = res.getDrawable(R.drawable.ic_hangout_24dp);
15676a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            alternateIcon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
156897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            final GPlusOrHangoutsDataItemModel itemModel =
15696a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                    new GPlusOrHangoutsDataItemModel(intent, alternateIntent,
15706a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            dataItem, secondDataItem, alternateContentDescription,
15716a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                            header, text, context);
15726a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
157397b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            populateGPlusOrHangoutsDataItemModel(itemModel);
157497b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            intent = itemModel.intent;
157597b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateIntent = itemModel.alternateIntent;
157697b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            alternateContentDescription = itemModel.alternateContentDescription;
157797b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            header = itemModel.header;
157897b1e2d6206ad8f64af6bc935451654204706661Paul Soulos                            text = itemModel.text;
15796a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        } else {
15806a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            if (HANGOUTS_DATA_5_VIDEO.equals(intent.getDataString())) {
15816a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
15826a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            } else {
15836a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                                icon = res.getDrawable(R.drawable.ic_hangout_24dp);
15846a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            }
1585e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1586e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1587e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
158897f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                        entryContextMenuInfo = new EntryContextMenuInfo(header, mimetype,
158997f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                                dataItem.getMimeType(), dataItem.getId(),
159097f27809c3e6153ce0bc5050bc5ee466a646e8ecPaul Soulos                                dataItem.isSuperPrimary());
15916bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell                        icon = ResolveCache.getInstance(context).getIcon(
1592e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
1593e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
1594e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
1595e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
1596e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
159748ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
1598e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
1599e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
1600eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1601b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1602eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
1603eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
16046bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, intent)) {
1605eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
1606eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1607eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1608eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1609dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
1610dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
16116bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            if (!PhoneCapabilityTester.isIntentRegistered(context, alternateIntent)) {
1612dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
1613f0d04c11b649e953177e79722d05fa048e53273dJay Shrauner            } else if (TextUtils.isEmpty(alternateContentDescription)) {
1614f0d04c11b649e953177e79722d05fa048e53273dJay Shrauner                // Attempt to use package manager to find a suitable content description if needed
161523e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.append(getIntentResolveLabel(alternateIntent, context));
1616dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1617dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1618dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1619eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
1620eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
1621eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
1622eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
1623eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1624eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1625fa238949855fba3b28a57fad3d585b13e80362bbBrian Attwell        // Ignore dataIds from the Me profile.
1626ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
1627ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
1628ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
162923e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon,
16307ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jang                new SpannableString(primaryContentDescription.toString()),
16317ce5352a70e8aaf120bf4f7bd05d595f46abb080Walter Jang                intent, alternateIcon, alternateIntent,
163223e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                alternateContentDescription.toString(), shouldApplyColor, isEditable,
163348290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription,
163448290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                iconResourceId);
1635eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1636eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
16376bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private List<Entry> dataItemsToEntries(List<DataItem> dataItems,
16386bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            MutableString aboutCardTitleOut) {
16396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Hangouts and G+ use two data items to create one entry.
16406a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        if (dataItems.get(0).getMimeType().equals(MIMETYPE_GPLUS_PROFILE) ||
16416a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataItems.get(0).getMimeType().equals(MIMETYPE_HANGOUTS)) {
16426a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            return gPlusOrHangoutsDataItemsToEntries(dataItems);
16436a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        } else {
16446a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            final List<Entry> entries = new ArrayList<>();
16456a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            for (DataItem dataItem : dataItems) {
16466a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                final Entry entry = dataItemToEntry(dataItem, /* secondDataItem = */ null,
16476a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        this, mContactData, aboutCardTitleOut);
16486a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                if (entry != null) {
16496a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    entries.add(entry);
16506a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
16516a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            }
16526a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            return entries;
16536a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
16546a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
16556a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
16566a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    /**
16576a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * G+ and Hangout entries are unique in that a single ExpandingEntryCardView.Entry consists
16586a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * of two data items. This method attempts to build each entry using the two data items if
16596a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * they are available. If there are more or less than two data items, a fall back is used
16606a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * and each data item gets its own entry.
16616a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     */
16626a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private List<Entry> gPlusOrHangoutsDataItemsToEntries(List<DataItem> dataItems) {
1663eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
16646a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        final Map<Long, List<DataItem>> buckets = new HashMap<>();
16656a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Put the data items into buckets based on the raw contact id
1666eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
16676a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            List<DataItem> bucket = buckets.get(dataItem.getRawContactId());
16686a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            if (bucket == null) {
16696a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                bucket = new ArrayList<>();
16706a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                buckets.put(dataItem.getRawContactId(), bucket);
16716a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            }
16726a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            bucket.add(dataItem);
16736a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
16746a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
16756a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Use the buckets to build entries. If a bucket contains two data items, build the special
16766a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // entry, otherwise fall back to the normal entry.
16776a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        for (List<DataItem> bucket : buckets.values()) {
16786a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            if (bucket.size() == 2) {
16796a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                // Use the pair to build an entry
16806a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                final Entry entry = dataItemToEntry(bucket.get(0),
16816a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        /* secondDataItem = */ bucket.get(1), this, mContactData,
16826a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        /* aboutCardName = */ null);
16836a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                if (entry != null) {
16846a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    entries.add(entry);
16856a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
16866a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            } else {
16876a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                for (DataItem dataItem : bucket) {
16886a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    final Entry entry = dataItemToEntry(dataItem, /* secondDataItem = */ null,
16896a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            this, mContactData, /* aboutCardName = */ null);
16906a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    if (entry != null) {
16916a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        entries.add(entry);
16926a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    }
16936a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                }
1694eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1695eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1696eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
1697edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1698edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
16996a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    /**
17006a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * Used for statically passing around G+ or Hangouts data items and entry fields to
17016a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     * populateGPlusOrHangoutsDataItemModel.
17026a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos     */
17036a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static final class GPlusOrHangoutsDataItemModel {
17046a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Intent intent;
17056a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Intent alternateIntent;
17066a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public DataItem dataItem;
17076a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public DataItem secondDataItem;
17086a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public StringBuilder alternateContentDescription;
17096a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public String header;
17106a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public String text;
17116a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public Context context;
17126a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
17136a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        public GPlusOrHangoutsDataItemModel(Intent intent, Intent alternateIntent, DataItem dataItem,
17146a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                DataItem secondDataItem, StringBuilder alternateContentDescription, String header,
17156a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                String text, Context context) {
17166a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.intent = intent;
17176a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.alternateIntent = alternateIntent;
17186a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.dataItem = dataItem;
17196a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.secondDataItem = secondDataItem;
17206a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.alternateContentDescription = alternateContentDescription;
17216a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.header = header;
17226a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.text = text;
17236a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            this.context = context;
17246a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
17256a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
17266a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
17276a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    private static void populateGPlusOrHangoutsDataItemModel(
17286a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            GPlusOrHangoutsDataItemModel dataModel) {
17296a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        final Intent secondIntent = new Intent(Intent.ACTION_VIEW);
17306a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        secondIntent.setDataAndType(ContentUris.withAppendedId(Data.CONTENT_URI,
17316a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.secondDataItem.getId()), dataModel.secondDataItem.getMimeType());
17326a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // There is no guarantee the order the data items come in. Second
17336a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // data item does not necessarily mean it's the alternate.
17346a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        // Hangouts video and Add to circles should be alternate. Swap if needed
17356a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        if (HANGOUTS_DATA_5_VIDEO.equals(
17366a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.dataItem.getContentValues().getAsString(Data.DATA5)) ||
17376a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(
17386a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        dataModel.dataItem.getContentValues().getAsString(Data.DATA5))) {
17396a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateIntent = dataModel.intent;
17406a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateContentDescription = new StringBuilder(dataModel.header);
17416a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
17426a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.intent = secondIntent;
17436a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.header = dataModel.secondDataItem.buildDataStringForDisplay(dataModel.context,
17446a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    dataModel.secondDataItem.getDataKind());
17456a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.text = dataModel.secondDataItem.getDataKind().typeColumn;
17466a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        } else if (HANGOUTS_DATA_5_MESSAGE.equals(
17476a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                dataModel.dataItem.getContentValues().getAsString(Data.DATA5)) ||
17486a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                GPLUS_PROFILE_DATA_5_VIEW_PROFILE.equals(
17496a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                        dataModel.dataItem.getContentValues().getAsString(Data.DATA5))) {
17506a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateIntent = secondIntent;
17516a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos            dataModel.alternateContentDescription = new StringBuilder(
17526a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                    dataModel.secondDataItem.buildDataStringForDisplay(dataModel.context,
17536a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos                            dataModel.secondDataItem.getDataKind()));
17546a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos        }
17556a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos    }
17566a2a1a7d87cd3c59d0e09abce0739ab4175a670ePaul Soulos
17576bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell    private static String getIntentResolveLabel(Intent intent, Context context) {
17586bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        final List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent,
1759dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
1760dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1761dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
1762dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
1763dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
1764dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
1765dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
1766dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
17676bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell            bestResolve = ResolveCache.getInstance(context).getBestResolve(intent, matches);
1768dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1769dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1770dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
1771dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
1772dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1773dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
17746bb01347eab60f95deafdfe523b0c368707210f3Brian Attwell        return String.valueOf(bestResolve.loadLabel(context.getPackageManager()));
1775dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
1776dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1777edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
177831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
177931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
178031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
178131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
178231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
178331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
178431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
178531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
178631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1787faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
178831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1789faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
1790faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
1791929a62f5c1bcdc3773270f7d189d88c0464b36adJay Shrauner                if (imageViewDrawable instanceof BitmapDrawable && mContactData != null
179295c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData() != null
179395c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        && mContactData.getThumbnailPhotoBinaryData().length > 0) {
179495c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // Perform the color analysis on the thumbnail instead of the full sized
179595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // image, so that our results will be as similar as possible to the Bugle
179695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    // app.
179795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    final Bitmap bitmap = BitmapFactory.decodeByteArray(
179895c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData(), 0,
179995c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            mContactData.getThumbnailPhotoBinaryData().length);
180095c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    try {
180195c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        final int primaryColor = colorFromBitmap(bitmap);
180295c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        if (primaryColor != 0) {
180395c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                            return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
180495c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                                    primaryColor);
180595c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        }
180695c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                    } finally {
180795c268e8dae6ed6b3bf6a205b02eadd995dec2c0Brian Attwell                        bitmap.recycle();
1808faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
18098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
18108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
1811faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
1812a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
181331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
1814a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
181531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
181631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
181731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1818faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
1819faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
18208571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
18218571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
18228571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
18238571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
18248571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
18258571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
18268571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
18278571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
18288571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
18298571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
18308571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
18318571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
1832faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
183331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
183431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
183531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
183631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
183731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
1838faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
18399b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
1840faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        final int primaryColor = palette.mPrimaryColor;
1841faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mScroller.setHeaderTintColor(primaryColor);
1842faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
18439b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
18448571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
18459b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
1846faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                new PorterDuffColorFilter(primaryColor, PorterDuff.Mode.SRC_ATOP);
1847faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mContactCard.setColorAndFilter(primaryColor, mColorFilter);
1848faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mRecentCard.setColorAndFilter(primaryColor, mColorFilter);
1849faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mAboutCard.setColorAndFilter(primaryColor, mColorFilter);
18509b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
18519b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
18528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
18538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller == null) {
18548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
18558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
18568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
18578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
18588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
18598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
18608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
18618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
18628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
18638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
1864847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
1865847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell                getWindow().getStatusBarColor(), desiredStatusBarColor);
1866847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
1867847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setEvaluator(new ArgbEvaluator());
1868847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.start();
18698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
18708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
18718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
18728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
18738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
18748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
1875a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
1876a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
18778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
18788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
18798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
18808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1881b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
1882eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1883b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
1884ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
1885ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
1886b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
1887b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
1888b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
1889b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
1890b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
189123e2836c1e9cbe8996a1344301e69d67bb617891Paul Soulos                    interaction.getContentDescription(this),
1892b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
1893dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
1894dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
1895dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
189648ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
18972a4207fb39330e840436215c896cde911489e111Paul Soulos                    /* isEditable = */ false,
189848fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* EntryContextMenuInfo = */ null,
189948fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIcon = */ null,
190048fc91217042fd854f0e96c2028c188e50a5e21dPaul Soulos                    /* thirdIntent = */ null,
190148290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    /* thirdContentDescription = */ null,
190248290bed7c6a8bd5e7be8b206dddacf9047a945fPaul Soulos                    interaction.getIconResourceId()));
1903b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1904b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
1905b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1906b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1907eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
1908851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
1909cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1910851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
1911405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
1912cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1913cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1914cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1915851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
19168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
1917930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            try {
19188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1919930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (isFinishing()) {
1920930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    return;
1921930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                }
1922930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (data.isError()) {
192302ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    // This means either the contact is invalid or we had an
192402ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    // internal error such as an acore crash.
192502ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    Log.i(TAG, "Failed to load contact: " + ((ContactLoader)loader).getLookupUri());
192602ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
192702ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                            Toast.LENGTH_LONG).show();
192802ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    finish();
192902ecc3f456a2e9d9fc2713583d387b708ae458d9Jay Shrauner                    return;
1930930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                }
1931930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                if (data.isNotFound()) {
19328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
19338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
19348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
1935930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    finish();
1936930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                    return;
19378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
1938cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1939930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                bindContactData(data);
1940cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1941930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            } finally {
1942930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell                Trace.endSection();
1943930da3ae6e392777986ed0722c5480caf1bd9e7eBrian Attwell            }
1944cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1945cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1946cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1947851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
1948cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
1949cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
1950cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1951d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
1952d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
1953b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
1954d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
19558571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
1956cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1957cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
1958b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1959b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
1960b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
1961b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
19628477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
19638477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
19648477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
1965b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
1966b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
1967b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
1968b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
1969b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
19708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
19718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
19728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
19738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
19748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
19758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
19768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
19778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1978eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
1979b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
1980b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1981b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1982b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
1983b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
1984b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
1985b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
1986b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
1987b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
1988ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1989b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
1990b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
1991899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
1992ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
1993ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
1994ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
1995ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
1996ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
1997899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
1998899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
1999ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
2000899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
2001899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
2002899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
2003899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
2004899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
2005ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
2006ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
2007ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
2008ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
2009ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
2010b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
2011b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
2012b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2013b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2014b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2015b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
2016b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
2017b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
2018b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2019b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
2020b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
2021b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
2022b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2023b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2024b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
2025b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
2026b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
2027b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
2028b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
2029b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2030b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
2031b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
2032b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
2033b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
2034b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
2035eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
203630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        final List<List<Entry>> interactionsWrapper = new ArrayList<>();
2037b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
203830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        mRecentDataTask = new AsyncTask<Void, Void, Void>() {
2039b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
204030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            protected Void doInBackground(Void... params) {
204130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("sort recent loader results");
204230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
204330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
204430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    allInteractions.addAll(loaderInteractions);
204530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
204630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
204730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // Sort the interactions by most recent
204830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
204930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    @Override
205030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    public int compare(ContactInteraction a, ContactInteraction b) {
205130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                        return a.getInteractionDate() >= b.getInteractionDate() ? -1 : 1;
205230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    }
205330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                });
205430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
205530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
205630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("contactInteractionsToEntries");
205730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
205830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // Wrap each interaction in its own list so that an icon is displayed for each entry
205930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                for (Entry contactInteraction : contactInteractionsToEntries(allInteractions)) {
206030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    List<Entry> entryListWrapper = new ArrayList<>(1);
206130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    entryListWrapper.add(contactInteraction);
206230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    interactionsWrapper.add(entryListWrapper);
206330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
206430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
206530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
206630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                return null;
2067b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
206830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
206930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            @Override
207030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            protected void onPostExecute(Void aVoid) {
207130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                super.onPostExecute(aVoid);
207230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.beginSection("initialize recents card");
207330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
207430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                if (allInteractions.size() > 0) {
207530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mRecentCard.initialize(interactionsWrapper,
2076b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
2077c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
207830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                            mExpandingEntryCardViewListener, mScroller);
207930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mRecentCard.setVisibility(View.VISIBLE);
208030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
2081eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
208230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                Trace.endSection();
208330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell
208430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // About card is initialized along with the contact card, but since it appears after
208530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // the recent card in the UI, we hold off until making it visible until the recent
208630e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                // card is also ready to avoid stuttering.
208730e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                if (mAboutCard.shouldShow()) {
208830e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mAboutCard.setVisibility(View.VISIBLE);
208930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                } else {
209030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                    mAboutCard.setVisibility(View.GONE);
209130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                }
209230e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell                mRecentDataTask = null;
209330e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            }
209430e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        };
209530e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        mRecentDataTask.execute();
2096b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
20978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
20988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
20998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
21008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
21018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
21028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
21038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
21048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
21058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
21068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
21078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
21088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
210930e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        if (mRecentDataTask != null) {
211030e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell            mRecentDataTask.cancel(/* mayInterruptIfRunning = */ false);
211130e1ef1927a1a21370b6b5dedd73540152062475Brian Attwell        }
21128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
211323889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
211423889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
2115d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
2116d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
2117d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
2118d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
2119d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2120d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2121a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    /**
2122a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     * Returns true if it is possible to share the current contact.
2123a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     */
2124a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    private boolean isContactShareable() {
2125a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
2126a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    }
2127a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
21286095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
212963176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        final Intent intent = new Intent(Intent.ACTION_EDIT, mContactData.getLookupUri());
2130d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
21316095369885edcca566a812b551886e29c7ff8039Brian Attwell        return intent;
21326095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
21336095369885edcca566a812b551886e29c7ff8039Brian Attwell
21346095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
2135a42ef76251778161d27bc07db214b8c81720e476Paul Soulos        mHasIntentLaunched = true;
2136c00a0b5370f0714f6af1ea9b3e29b10f25e91af0Zheng Fu        mContactLoader.cacheResult();
21376095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
2138d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2139d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
21405d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang    private void deleteContact() {
21415d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang        final Uri contactUri = mContactData.getLookupUri();
21425d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang        ContactDeletionInteraction.start(this, contactUri, /* finishActivityWhenDone =*/ true);
21435d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang    }
21445d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang
2145d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
2146d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
214763176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        if (mContactData != null) {
2148d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
2149d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
2150d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
2151d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
2152d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2153d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
2154333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
2155d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
2156d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
2157d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2158d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
2159eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
216063176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell                    QuickContactActivity.this, mContactData.getLookupUri(), !isStarred);
2161d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
216235ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell
216335ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            final CharSequence accessibilityText = !isStarred
216435ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    ? getResources().getText(R.string.description_action_menu_add_star)
216535ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    : getResources().getText(R.string.description_action_menu_remove_star);
216635ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // Accessibility actions need to have an associated view. We can't access the MenuItem's
216735ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // underlying view, so put this accessibility action on the root view.
216835ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            mScroller.announceForAccessibility(accessibilityText);
2169d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
2170d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2171d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2172752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
2173752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Calls into the contacts provider to get a pre-authorized version of the given URI.
2174752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
2175752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private Uri getPreAuthorizedUri(Uri uri) {
2176752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle uriBundle = new Bundle();
2177752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        uriBundle.putParcelable(ContactsContract.Authorization.KEY_URI_TO_AUTHORIZE, uri);
2178752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle authResponse = getContentResolver().call(
2179752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.AUTHORITY_URI,
2180752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.Authorization.AUTHORIZATION_METHOD,
2181752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                null,
2182752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                uriBundle);
2183752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (authResponse != null) {
2184752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return (Uri) authResponse.getParcelable(
2185752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    ContactsContract.Authorization.KEY_AUTHORIZED_URI);
2186752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        } else {
2187752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return uri;
2188752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
2189752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
21902d150da246632b1649999cfabed776133b097775Brian Attwell
2191752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
2192752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
2193752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
2194752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (mContactData.isUserProfile()) {
2195752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // User is sharing the profile.  We don't want to force the receiver to have
2196752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // the highly-privileged READ_PROFILE permission, so we need to request a
2197752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // pre-authorized URI from the provider.
2198752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            shareUri = getPreAuthorizedUri(shareUri);
2199752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
2200752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2201752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
2202752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.setType(Contacts.CONTENT_VCARD_TYPE);
2203752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
2204752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2205752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
2206752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
2207752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
2208752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2209752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
2210a42ef76251778161d27bc07db214b8c81720e476Paul Soulos            mHasIntentLaunched = true;
2211752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            this.startActivity(chooseIntent);
2212eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
2213752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
2214752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
2215752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
2216752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2217752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
2218752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
2219752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
2220752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
2221752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
2222752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
2223752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2224752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
2225752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
2226752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
2227752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
2228752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
2229752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
2230752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2231752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
2232752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
2233752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        Toast.makeText(QuickContactActivity.this,
2234752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                R.string.createContactShortcutSuccessful,
2235752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
2236752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
2237752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
2238752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
223963176c96f33b5a0bcb25816c80889bb11e5c7152Brian Attwell        builder.createContactShortcutIntent(mContactData.getLookupUri());
2240752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
2241752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
224266965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell    private boolean isShortcutCreatable() {
22438d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell        if (mContactData == null || mContactData.isUserProfile()) {
22448d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell            return false;
22458d0557eb013b2808402e491271fe0f13ffcfabefBrian Attwell        }
224666965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        final Intent createShortcutIntent = new Intent();
224766965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        createShortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
224866965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        final List<ResolveInfo> receivers = getPackageManager()
224966965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell                .queryBroadcastReceivers(createShortcutIntent, 0);
225066965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell        return receivers != null && receivers.size() > 0;
225166965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell    }
225266965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell
2253d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2254d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
2255eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
2256d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
2257d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
2258d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2259d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2260d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2261d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
2262d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
2263d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
2264333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
2265d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
2266d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
2267a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
2268d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
2269d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
2270d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
2271d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
2272d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
227330cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
22742e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
2275d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
2276d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
22772e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
2278d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
2279d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
2280d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
2281a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
22825d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            final MenuItem deleteMenuItem = menu.findItem(R.id.menu_delete);
22835d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            deleteMenuItem.setVisible(isContactEditable());
22845d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang
2285a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            final MenuItem shareMenuItem = menu.findItem(R.id.menu_share);
2286a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            shareMenuItem.setVisible(isContactShareable());
2287a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
228866965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell            final MenuItem shortcutMenuItem = menu.findItem(R.id.menu_create_contact_shortcut);
228966965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell            shortcutMenuItem.setVisible(isShortcutCreatable());
229066965e14e741be40f3f5ad6aa21a75795f9a1eb1Brian Attwell
22918a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
2292d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
22938a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
2294d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
2295d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
2296d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
2297d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
2298d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
2299d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
2300d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
2301d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
2302d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
2303d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
2304ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // This action is used to launch the contact selector, with the option of
2305ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // creating a new contact. Creating a new contact is an INSERT, while selecting
2306ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // an exisiting one is an edit. The fields in the edit screen will be
2307ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // prepopulated with data.
2308ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2309ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
2310ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.setType(Contacts.CONTENT_ITEM_TYPE);
2311ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2312fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    ArrayList<ContentValues> values = mContactData.getContentValues();
2313fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell
2314fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    // Only pre-fill the name field if the provided display name is an nickname
2315fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    // or better (e.g. structured name, nickname)
2316fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    if (mContactData.getDisplayNameSource() >= DisplayNameSources.NICKNAME) {
2317ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.NAME, mContactData.getDisplayName());
2318fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                    } else if (mContactData.getDisplayNameSource()
2319fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                            == DisplayNameSources.ORGANIZATION) {
2320fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // This is probably an organization. Instead of copying the organization
2321fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // name into a name entry, copy it into the organization entry. This
2322fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        // way we will still consider the contact an organization.
2323fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        final ContentValues organization = new ContentValues();
2324fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        organization.put(Organization.COMPANY, mContactData.getDisplayName());
2325fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        organization.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
2326fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell                        values.add(organization);
2327ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2328fc423b4040a0ec1728ee32ff40ef430f3e16e9dcBrian Attwell
2329ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Last time used and times used are aggregated values from the usage stat
2330ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // table. They need to be removed from data values so the SQL table can insert
2331ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // properly
2332ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    for (ContentValues value : values) {
2333ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.LAST_TIME_USED);
2334ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        value.remove(Data.TIMES_USED);
2335ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2336ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    intent.putExtra(Intents.Insert.DATA, values);
2337ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2338ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // If the contact can only export to the same account, add it to the intent.
2339ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // Otherwise the ContactEditorFragment will show a dialog for selecting an
2340ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    // account.
2341ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    if (mContactData.getDirectoryExportSupport() ==
2342ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                            Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY) {
2343ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.ACCOUNT,
2344ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                new Account(mContactData.getDirectoryAccountName(),
2345ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                        mContactData.getDirectoryAccountType()));
2346ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                        intent.putExtra(Intents.Insert.DATA_SET,
2347ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                                mContactData.getRawContacts().get(0).getDataSet());
2348ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    }
2349ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos
2350f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    // Add this flag to disable the delete menu option on directory contact joins
2351f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    // with local contacts. The delete option is ambiguous when joining contacts.
2352f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                    intent.putExtra(ContactEditorFragment.INTENT_EXTRA_DISABLE_DELETE_MENU_OPTION,
2353f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos                            true);
2354f19dda97bfdef84e3f39ba069a981004a5c797b1Paul Soulos
2355ab54ea15274dd3975c82a6aac29ba268aaed0799Paul Soulos                    startActivityForResult(intent, REQUEST_CODE_CONTACT_SELECTION_ACTIVITY);
2356d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
2357d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
2358d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
2359d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
2360d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
2361d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
23625d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang            case R.id.menu_delete:
23635d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang                deleteContact();
23645d2e262ab3658118a12fbf6d0edab76484368c24Walter Jang                return true;
2365752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
236649de62f06d24653484adebd69123eeff2850a757Jay Shrauner                if (isContactShareable()) {
236749de62f06d24653484adebd69123eeff2850a757Jay Shrauner                    shareContact();
236849de62f06d24653484adebd69123eeff2850a757Jay Shrauner                }
2369752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
2370752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
2371752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                createLauncherShortcutWithContact();
2372752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
2373d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
2374d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
2375d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
237623889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
2377edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
2378