QuickContactActivity.java revision d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9
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
198477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.Animator;
208477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.Animator.AnimatorListener;
218477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.AnimatorListenerAdapter;
228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.animation.ArgbEvaluator;
23b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport android.animation.ObjectAnimator;
24edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.app.Activity;
25d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.app.Fragment;
26cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.app.LoaderManager.LoaderCallbacks;
27eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.app.SearchManager;
28752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.content.ActivityNotFoundException;
29edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.ContentUris;
30ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.content.ContentValues;
31edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.Intent;
32cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.content.Loader;
33dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.PackageManager;
34dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.ResolveInfo;
3531b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.Bitmap;
368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.Color;
37eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuff;
38eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuffColorFilter;
3931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.BitmapDrawable;
408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.drawable.ColorDrawable;
4131b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.Drawable;
42eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.ParseException;
43edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.net.Uri;
44eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.WebAddress;
4531b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.os.AsyncTask;
46edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.os.Bundle;
478a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.os.Trace;
48eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.CalendarContract;
49752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract;
50edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Email;
51eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Event;
52eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;
53eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Identity;
54eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Im;
55eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Nickname;
56eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Note;
57eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Organization;
58edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Phone;
59eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Relation;
60edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
61edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
62edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Website;
63752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract.Contacts;
6460e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulosimport android.provider.ContactsContract.Data;
65eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.DisplayNameSources;
66ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.provider.ContactsContract.DataUsageFeedback;
67edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.QuickContact;
68edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.RawContacts;
6931b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
70edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
71edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
72eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.util.Pair;
73d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
74d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
75eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
76edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View;
77edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View.OnClickListener;
7810d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawaimport android.view.WindowManager;
79edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.ImageView;
800cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulosimport android.widget.LinearLayout;
81edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
82d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
83edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
8472b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
85d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
862d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
87e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
88eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
89eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
90eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
91d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.editor.SelectAccountDialogFragment;
9231b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport com.android.contacts.common.lettertiles.LetterTileDrawable;
93752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder;
94752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
950d5588da244d0992c3ff8f25d0875fdf95a8c644Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
96cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.Contact;
97cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.ContactLoader;
98cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.RawContact;
99428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.AccountType;
100d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.model.account.AccountWithDataSet;
101cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.DataItem;
102428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.dataitem.DataKind;
103cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.EmailDataItem;
104eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.EventDataItem;
105cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.ImDataItem;
106eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NicknameDataItem;
107eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NoteDataItem;
108eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.OrganizationDataItem;
109b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.common.model.dataitem.PhoneDataItem;
110eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.RelationDataItem;
111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.SipAddressDataItem;
112eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredNameDataItem;
113eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredPostalDataItem;
114eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.WebsiteDataItem;
115eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.util.DateUtils;
116faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils;
117faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
118d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwellimport com.android.contacts.common.util.ViewUtil;
119333091ae754ddfc25714c14b9b89534be24379f9Paul Soulosimport com.android.contacts.detail.ContactDisplayUtils;
120899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
121ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
123b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
124b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1252d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
126e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
127e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
128eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
129b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
130eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
131b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
132b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
133e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.base.Preconditions;
134e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
135e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
1362d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
137899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
138eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
139b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
140b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
141eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
142edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
143edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
144b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
145edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
146edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
147edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
148edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
149edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
150edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
151d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellpublic class QuickContactActivity extends ContactsActivity {
1528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
1548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
1558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
1568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
1578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
1588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
1598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
160edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
161edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1629b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
1639b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1649b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
1658a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
1669b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xB2, 0, 0, 0);
167ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
168dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
169752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
170752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
171752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
172edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
173edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
174edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
175edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
176e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
177e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
178e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE = "Add to circle";
179e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
180e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
181e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_HANGOUTS_VIDEO = "Start video call";
182e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
183edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
184edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
1858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
1868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
1878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
188edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1896219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee    private ImageView mPhotoView;
1900d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell    private View mTransparentView;
191eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
1926095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
193b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
194eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
195eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
196eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * This list contains all the {@link DataItem}s. Each nested list contains all data items of a
197eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * specific mimetype in sorted order, using mWithinMimeTypeDataItemComparator. The mimetype
198eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * lists are sorted using mAmongstMimeTypeDataItemComparator.
199eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
200eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private List<List<DataItem>> mDataItemsList;
201eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
202eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * A map between a mimetype string and the corresponding list of data items. The data items
203eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * are in sorted order using mWithinMimeTypeDataItemComparator.
204eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
205eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Map<String, List<DataItem>> mDataItemsMap;
206b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
207d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private AsyncTask<Void, Void, Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>
209eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mEntriesAndActionsTask;
2109b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
211a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
2128477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsWaitingForOtherPieceOfExitAnimation;
2138477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
2148571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
215edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
2166cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
2179815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
2189b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
2199815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
2209758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
2219758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
222edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
223cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
224edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
225edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
226edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
227edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
228edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
229cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
230405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
231edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
232a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private static final List<String> SORTED_ABOUT_CARD_MIMETYPES = Lists.newArrayList(
233a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Nickname.CONTENT_ITEM_TYPE,
234a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name is inserted after nickname if it is available.
235a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // No mimetype for phonetic name exists.
236a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Website.CONTENT_ITEM_TYPE,
237a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Organization.CONTENT_ITEM_TYPE,
238a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Event.CONTENT_ITEM_TYPE,
239a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Relation.CONTENT_ITEM_TYPE,
240a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Im.CONTENT_ITEM_TYPE,
241a153dba33c2152332c5706178a367f52b0550723Paul Soulos            GroupMembership.CONTENT_ITEM_TYPE,
242a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Identity.CONTENT_ITEM_TYPE,
243a153dba33c2152332c5706178a367f52b0550723Paul Soulos            Note.CONTENT_ITEM_TYPE);
244eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
245b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
246b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
247b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
248ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
249ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
250ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
251b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
252b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
253b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
254ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
255ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
256899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
257ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
258ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
259899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
260899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
261899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
262899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            180L * 24L * 60L * 60L * 1000L /* 180 days */;
263899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
264899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            36L * 60L * 60L * 1000L /* 36 hours */;
265899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
266ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
267ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
268ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
2696095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
2706095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
2716095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
272ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
273ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
274ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
275ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
276ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
277ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
278a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults = new HashMap<>();
279b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
280d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
2816cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
2822d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
2832d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
2842d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
285ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data Id is stored as the entry view id
286ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            final int dataId = v.getId();
2876095369885edcca566a812b551886e29c7ff8039Brian Attwell            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
2886095369885edcca566a812b551886e29c7ff8039Brian Attwell                editContact();
2896095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
2906095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
2916095369885edcca566a812b551886e29c7ff8039Brian Attwell            final Object intentObject = v.getTag();
292ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (intentObject == null || !(intentObject instanceof Intent)) {
293ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Intent tag was not used correctly");
2942d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
2952d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
296ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            final Intent intent = (Intent) intentObject;
297ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
298ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
299ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
300ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
301ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
30220bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            final Uri intentUri = intent.getData();
30320bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos            if ((intentUri != null && intentUri.getScheme() != null &&
30420bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                    intentUri.getScheme().equals(CallUtil.SCHEME_SMSTO)) ||
305ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
306ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
307ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
308ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
309ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
310ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
31120bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                final Uri dataUsageUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
312ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
313ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
314ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
315ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                final boolean successful = getContentResolver().update(
31620bdf9d2d21e1c82513f798ef4205b9d1713996dPaul Soulos                        dataUsageUri, new ContentValues(), null, null) > 0;
317ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                if (!successful) {
318ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    Log.w(TAG, "DataUsageFeedback increment failed");
319ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
320ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
321ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
322ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
323ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
324ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            startActivity(intent);
3252d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
3262d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
3272d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
328e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
329e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
330e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
331e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
332e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
333e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
3340cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
3350cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        @Override
3360cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        public void onExpand(int heightDelta) {
3370cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos            mScroller.prepareForExpandingScrollChild();
3380cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        }
339e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
340e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
341d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
342d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
343d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
344d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
345d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
346d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
347d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
348d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
349d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
350d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
351d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
352d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
353d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
354d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
355d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
356d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
357d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
358d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
359d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
360d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
361d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
362d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
363d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
364d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
365d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
366d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
367d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
368d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
369d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
370d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
371b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
372b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
373b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
374b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
3758477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsWaitingForOtherPieceOfExitAnimation) {
3768477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                finish();
3778477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                return;
3788477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
3798477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = false;
380b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
3818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
3828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
3838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
3848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
3858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
3868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
3878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
3888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
3898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
3908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
3918477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
3928477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
3938477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
3948477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            // Remove the window shim now that we are starting an Activity exit animation.
3958477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            final int duration = getResources().getInteger(android.R.integer.config_shortAnimTime);
3969b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final ObjectAnimator animator = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0xFF, 0);
3978477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            animator.addListener(mExitWindowShimAnimationListener);
3988477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            animator.setDuration(duration).start();
3998477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = true;
4008477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
4018477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
4028477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    };
4038477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
4048477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    final AnimatorListener mExitWindowShimAnimationListener = new AnimatorListenerAdapter() {
4058477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
4068477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onAnimationEnd(Animator animation) {
4078477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsWaitingForOtherPieceOfExitAnimation) {
4088477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                finish();
4098477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                return;
4108477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
4118477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = false;
4128477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
413b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
414b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
415eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
416eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
417eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
418eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
419eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
420eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
421eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
422eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
423eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
424eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
425eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
426eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
427eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
428eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
429eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
430eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
431eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
432eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
433eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
434eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
435eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
436eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
437eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
438eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
439eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
440eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
441eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
442eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
443eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
444eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
445eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
446eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
447eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
448eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
449eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
450eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
451cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
452cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
453cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 1. Times used
454cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 2. Last time used
455cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 3. Statically defined
456cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
457eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
458eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
459eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
460eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
461eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem lhs = lhsList.get(0);
462eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem rhs = rhsList.get(0);
463eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
464eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
465eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
466eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
467eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
468eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
469eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
470eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
471eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
472eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
473eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
474eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
475eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
476eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
477eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
478eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
479eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
480eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
481eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Times used and last time used are the same. Resort to statically defined.
482eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String lhsMimeType = lhs.getMimeType();
483eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String rhsMimeType = rhs.getMimeType();
484eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (String mimeType : LEADING_MIMETYPES) {
485eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (lhsMimeType.equals(mimeType)) {
486eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return -1;
487eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                } else if (rhsMimeType.equals(mimeType)) {
488eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return 1;
489eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
490eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
491eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
492eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
493eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
494eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
495edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
4968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
4978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
4988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
499edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getWindow().setStatusBarColor(Color.TRANSPARENT);
5012426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
502d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
503dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
50410d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
50510d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
50610d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
50710d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
508edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
509edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
510a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
511a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
5120cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
5130cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos
514eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
5156095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
516b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
517eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
518edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5196095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
520eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
521eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
52223889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
523b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
524b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
525b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
526edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
527eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
528eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
529d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mPhotoView = (ImageView) findViewById(R.id.photo);
5300d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        mTransparentView = findViewById(R.id.transparent_view);
5310d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
5320d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            mTransparentView.setOnClickListener(new OnClickListener() {
5330d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
5340d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
5350d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
5360d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
5370d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
5380d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
539edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
540d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        // Allow a shadow to be shown under the toolbar.
541d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell        ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
542d41ab8ace81a2e44f8b28aa8782679b9e9cca1b9Brian Attwell
543d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
544d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
5459b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
5469b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
5479b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
5489b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
5496219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
5508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
5518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5529b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
5539b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
5548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (!mHasAlreadyBeenOpened) {
5558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            final int duration = getResources().getInteger(android.R.integer.config_shortAnimTime);
5569b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            ObjectAnimator.ofInt(mWindowScrim, "alpha", 0, 0xFF).setDuration(duration).start();
5578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
558edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
559c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
560ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
561ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
562ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
5638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5649b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
5659b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
566d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
567d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
568d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
569d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
570d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
571d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
572d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
573d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
574d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
57523889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
5769b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
5779b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
578ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
579ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
580ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
581ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
582ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
583ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
584ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
585ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
586ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
587ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
588ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
589ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
590ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
591ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
592a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
593a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
5949b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
595ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
596ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
5979b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
5989b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
5998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
6008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
6018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
602405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
603405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
6048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
6058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED) {
6068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // The contact that we were showing has been deleted.
6078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
608b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
6098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
610dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
6118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
6128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
6138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
6148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
6158571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
616d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
617d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
618d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
6199b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
6209b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
6219b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
6229b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
6239b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilter.getColor());
6249b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
6259b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
6269b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
627d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
628d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
629d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
630d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
631d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
632d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
633d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
634d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
635d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
636d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE,
637d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                QuickContact.MODE_LARGE);
638d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
639d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
640d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mLookupUri = Preconditions.checkNotNull(lookupUri, "missing lookupUri");
641d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
642d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
643d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
644d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
645d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
646d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
647d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // we need to restart the loader and reload the new contact.
648899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            for (int interactionLoaderId : mRecentLoaderIds) {
649899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                getLoaderManager().destroyLoader(interactionLoaderId);
650899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            }
651405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
652405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
653d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
6542d150da246632b1649999cfabed776133b097775Brian Attwell
6552d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
6562426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
657edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
658b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
6598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
6608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
6618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
6628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
663c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
664b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
665b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
66681281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
667d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
6689b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
669f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString());
6709b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
67181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
67281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
67381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
674f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell    private void setHeaderNameText(String value) {
675d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
6769b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
677f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell                mScroller.setTitle(value);
6789b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
67981281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
68081281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
68181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
68281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
683edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
684edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
685edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
686edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
687edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
688edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
689edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
690edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
691edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
692edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
693edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
694edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
695edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
696edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
697cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
698edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
6998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
7008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
7016cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
702d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
703edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
7058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
706dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
7076219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
70831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
7094936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        analyzeWhitenessOfPhotoAsynchronously();
710333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos        setHeaderNameText(ContactDisplayUtils.getDisplayName(this, data).toString());
711edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
7128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
713dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
714eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mEntriesAndActionsTask = new AsyncTask<Void, Void,
715eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>() {
7168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
718eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            protected Pair<List<List<DataItem>>, Map<String, List<DataItem>>> doInBackground(
719eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
720eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
7218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
7228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
724eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            protected void onPostExecute(Pair<List<List<DataItem>>,
725eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Map<String, List<DataItem>>> dataItemsPair) {
726eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                super.onPostExecute(dataItemsPair);
727eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mDataItemsList = dataItemsPair.first;
728eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mDataItemsMap = dataItemsPair.second;
7298a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
7308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
7318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
7328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
733eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    bindDataToCards();
7348a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
7358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
7368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
7378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
7388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
7398a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
7408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
741eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void bindDataToCards() {
742eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        startInteractionLoaders();
743eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        populateContactAndAboutCard();
744eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
745eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
746eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void startInteractionLoaders() {
747eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<DataItem> phoneDataItems = mDataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
748eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
749eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
750eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
751eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
752eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
753eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
754eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
755ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
756eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
757eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
758eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
7598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
7608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
761ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
762ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
763ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
764ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
765ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
766ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
767ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
768ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
7698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
770899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
7718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
772eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
773899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
774eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<DataItem> emailDataItems = mDataItemsMap.get(Email.CONTENT_ITEM_TYPE);
775eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
776eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
777eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
778eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
779eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
780eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
781eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
782ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
783eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
784899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
785899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
786ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
787899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
7888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
7898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
7908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
7928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
7938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
7948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
7958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
7968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
7978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
7988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
7998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
8008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
8018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
8028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
8038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
804a153dba33c2152332c5706178a367f52b0550723Paul Soulos    private List<List<Entry>> buildAboutCardEntries() {
805a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
806a153dba33c2152332c5706178a367f52b0550723Paul Soulos        for (String mimetype : SORTED_ABOUT_CARD_MIMETYPES) {
807a153dba33c2152332c5706178a367f52b0550723Paul Soulos            final List<DataItem> mimeTypeItems = mDataItemsMap.get(mimetype);
808a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (mimeTypeItems == null) {
809a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
810a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
811a153dba33c2152332c5706178a367f52b0550723Paul Soulos            final List<Entry> aboutEntries = dataItemsToEntries(mimeTypeItems);
812a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutEntries.size() > 0) {
813a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(aboutEntries);
814a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
815a153dba33c2152332c5706178a367f52b0550723Paul Soulos        }
816a153dba33c2152332c5706178a367f52b0550723Paul Soulos        return aboutCardEntries;
817a153dba33c2152332c5706178a367f52b0550723Paul Soulos    }
818a153dba33c2152332c5706178a367f52b0550723Paul Soulos
819eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void populateContactAndAboutCard() {
820eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
821eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
82260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        final List<List<Entry>> contactCardEntries = new ArrayList<>();
823a153dba33c2152332c5706178a367f52b0550723Paul Soulos        final List<List<Entry>> aboutCardEntries = buildAboutCardEntries();
824eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
825eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (int i = 0; i < mDataItemsList.size(); ++i) {
826eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final List<DataItem> dataItemsByMimeType = mDataItemsList.get(i);
827eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final DataItem topDataItem = dataItemsByMimeType.get(0);
828a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
829a153dba33c2152332c5706178a367f52b0550723Paul Soulos                // About card mimetypes are built in buildAboutCardEntries, skip here
830a153dba33c2152332c5706178a367f52b0550723Paul Soulos                continue;
83160e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            } else {
83260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                List<Entry> contactEntries = dataItemsToEntries(mDataItemsList.get(i));
83360e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                if (contactEntries.size() > 0) {
83460e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                    contactCardEntries.add(contactEntries);
835eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
836eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
837eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
838eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
839eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
840eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
841eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
842c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mContactCard.isExpanded(),
84389966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                    /* isAlwaysExpanded = */ false,
8440cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener,
8450cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mScroller);
846eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
847eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
848eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
849eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
850eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
8518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
852eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
853c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        // Phonetic name is not a data item, so the entry needs to be created separately
854c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        final String phoneticName = mContactData.getPhoneticName();
855c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        if (!TextUtils.isEmpty(phoneticName)) {
856c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            Entry phoneticEntry = new Entry(/* viewId = */ -1,
857c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* icon = */ null,
858c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    getResources().getString(R.string.name_phonetic),
859c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    phoneticName,
860c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* text = */ null,
861c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* intent = */ null,
862c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIcon = */ null,
863c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateIntent = */ null,
864c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* alternateContentDescription = */ null,
865c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* shouldApplyColor = */ false,
866c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    /* isEditable = */ false
867c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos                    );
868c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            List<Entry> phoneticList = new ArrayList<>();
869c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos            phoneticList.add(phoneticEntry);
870a153dba33c2152332c5706178a367f52b0550723Paul Soulos            // Phonetic name comes after nickname. Check to see if the first entry type is nickname
871a153dba33c2152332c5706178a367f52b0550723Paul Soulos            if (aboutCardEntries.get(0).get(0).getHeader().equals(
872a153dba33c2152332c5706178a367f52b0550723Paul Soulos                    getResources().getString(R.string.header_nickname_entry))) {
873a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(1, phoneticList);
874a153dba33c2152332c5706178a367f52b0550723Paul Soulos            } else {
875a153dba33c2152332c5706178a367f52b0550723Paul Soulos                aboutCardEntries.add(0, phoneticList);
876a153dba33c2152332c5706178a367f52b0550723Paul Soulos            }
877c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos        }
878c6e110946e2c51be6276591b85ac8abe169534a0Paul Soulos
879eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.initialize(aboutCardEntries,
880eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                /* numInitialVisibleEntries = */ 1,
881eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                /* isExpanded = */ true,
88289966b4ed8cc9d385e5ac6b4a7e9e08ada387eb0Paul Soulos                /* isAlwaysExpanded = */ true,
8830cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                mExpandingEntryCardViewListener,
8840cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                mScroller);
8856095369885edcca566a812b551886e29c7ff8039Brian Attwell
8866095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
8876095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
8886095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
8896095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
8906095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
8916095369885edcca566a812b551886e29c7ff8039Brian Attwell
892a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
893a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
894a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
895a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
896a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
897eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
898eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
899eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
900eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
9016095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
9026095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
9036095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
9046095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
9056095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
9066095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
9076095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
908dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* subHeader = */ null, /* text = */ null, getEditContactIntent(),
909dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
910714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
911dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* isEditable = */ false);
9126095369885edcca566a812b551886e29c7ff8039Brian Attwell
9136095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
9146095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
9156095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
9166095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
917dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* text = */ null, getEditContactIntent(), /* alternateIcon = */ null,
918dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
919714455bba22b99d168a2e864dfbc74a6e30dfdb6Paul Soulos                /* shouldApplyColor = */ true, /* isEditable = */ false);
9206095369885edcca566a812b551886e29c7ff8039Brian Attwell
9216095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
9226095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
9236095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
9246095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
9256095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
9266095369885edcca566a812b551886e29c7ff8039Brian Attwell
9276095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
9286095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
9296095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
9306095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
931c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ true,
9320cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                /* isAlwaysExpanded = */ true, mExpandingEntryCardViewListener, mScroller);
9336095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
9346095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
9356095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
9366095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
9376095369885edcca566a812b551886e29c7ff8039Brian Attwell
9386095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
939eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
940eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
941eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
942eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
943eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
944eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
945eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Pair<List<List<DataItem>>, Map<String, List<DataItem>>> generateDataModelFromContact(
946eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
947eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
948eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
949eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
9508bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
9518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
952851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
953851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
954eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
955eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
956851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
957eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
958eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
95947b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
96047b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
96147b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
962eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
963cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
964eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
965b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
966eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
967eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
968899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
969eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
970edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
971eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
972eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
973eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
974eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
975edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
976eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
977edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
978edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
9798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
980edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
981eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
98216339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
98316339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
984eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
985eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
986eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
98716339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
988eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
989eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
990eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
991eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
992eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
993eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
994eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
995eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
996edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
997eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
998edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
999eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
1000eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
1001eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
1002eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
100316339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
1004eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return new Pair<>(dataItemsList, dataItemsMap);
1005eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1006edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1007eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
1008eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
1009eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
1010eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
1011eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
1012eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
1013eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Entry dataItemToEntry(DataItem dataItem) {
1014eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
1015eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
1016eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
1017eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
1018eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
1019eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
1020eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
102148ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
1022dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
1023dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
1024dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        String alternateContentDescription = null;
1025eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
1026eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1027eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
1028eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1029eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
1030eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
1031eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = ContactsUtils.buildImIntent(this, im).first;
1032eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
10337de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            final int protocol;
10347de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (!im.isProtocolValid()) {
10357de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = Im.PROTOCOL_CUSTOM;
10367de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
10377de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
10387de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
10397de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            if (protocol == Im.PROTOCOL_CUSTOM) {
10407de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // If the protocol is custom, display the "IM" entry header as well to distinguish
10417de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                // this entry from other ones
10427de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                header = getResources().getString(R.string.header_im_entry);
10437de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = Im.getProtocolLabel(getResources(), protocol,
10447de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
10457de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                text = im.getData();
10467de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            } else {
10477de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                header = Im.getProtocolLabel(getResources(), protocol,
10487de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        im.getCustomProtocol()).toString();
10497de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = im.getData();
10507de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos            }
1051eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
1052eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
1053eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_organization_entry);
1054eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
1055eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
1056eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
1057eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
1058eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
1059eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
1060eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                (mContactData.getNameRawContactId() == dataItem.getRawContactId());
1061eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1062eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
1063eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
1064eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                && mContactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
1065eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1066eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
1067eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = getResources().getString(R.string.header_nickname_entry);
1068eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
1069eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1070eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
1071eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
1072eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_note_entry);
1073eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
1074eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
1075eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
1076eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_website_entry);
1077eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
1078eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
1079eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final WebAddress webAddress = new WebAddress(website.buildDataString(this, kind));
1080eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1081eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
1082eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.e(TAG, "Couldn't parse website: " + website.buildDataString(this, kind));
1083eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1084eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1085eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
1086eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String dataString = event.buildDataString(this, kind);
1087eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1088eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1089eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1090eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1091eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1092eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1093eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1094eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1095eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1096eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_event_entry);
1097f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
10987de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                subHeader = Event.getTypeLabel(getResources(), event.getKindTypeColumn(kind),
10997de6f85b0ee66deda13505f545c6b7125b2ac462Paul Soulos                        event.getLabel()).toString();
1100f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1101eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = DateUtils.formatDate(this, dataString);
1102eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1103eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
1104eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String dataString = relation.buildDataString(this, kind);
1105eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1106eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1107eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1108eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1109eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1110eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_relation_entry);
1111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
1112f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
1113f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                text = Relation.getTypeLabel(getResources(), relation.getKindTypeColumn(kind),
1114f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1115f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1116eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1117eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
1118eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
1119eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = phone.buildDataString(this, kind);
1120f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
1121f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = Phone.getTypeLabel(getResources(), phone.getKindTypeColumn(kind),
1122f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            phone.getLabel()).toString();
1123f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1124eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_phone_24dp);
1125eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (PhoneCapabilityTester.isPhone(this)) {
1126eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
112716339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1128dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
1129dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                        Uri.fromParts(CallUtil.SCHEME_SMSTO, phone.getNumber(), null));
1130dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon = getResources().getDrawable(R.drawable.ic_message_24dp);
1131dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateContentDescription = getResources().getString(R.string.sms_other);
1132eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1133eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1134eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1135eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1136eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
1137eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri mailUri = Uri.fromParts(CallUtil.SCHEME_MAILTO, address, null);
1138eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1139eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
1140f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
1141f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = Email.getTypeLabel(getResources(), email.getKindTypeColumn(kind),
1142f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
1143f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1144eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_email_24dp);
1145eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1146eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1147eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1148eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1149eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
1150eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1151eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
1152f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
1153f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = StructuredPostal.getTypeLabel(getResources(),
1154f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
1155f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
11566a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIntent =
11576a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                        StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
11586a4d2736d772cc5cbb79d04e115f2d6117826860Paul Soulos                alternateIcon = getResources().getDrawable(R.drawable.ic_directions_24dp);
1159eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_place_24dp);
1160eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1161eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
1162eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (PhoneCapabilityTester.isSipPhone(this)) {
1163eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1164eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final String address = sip.getSipAddress();
1165eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (!TextUtils.isEmpty(address)) {
1166eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    final Uri callUri = Uri.fromParts(CallUtil.SCHEME_SIP, address, null);
1167eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
11681cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    header = address;
11691cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    if (sip.hasKindTypeColumn(kind)) {
11701cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                        text = SipAddress.getTypeLabel(getResources(), sip.getKindTypeColumn(kind),
11711cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                            sip.getLabel()).toString();
11721cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    }
1173eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // Note that this item will get a SIP-specific variant
1174eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // of the "call phone" icon, rather than the standard
1175eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // app icon for the Phone app (which we show for
1176eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // regular phone numbers.)  That's because the phone
1177eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // app explicitly specifies an android:icon attribute
1178eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // for the SIP-related intent-filters in its manifest.
11791cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    icon = ResolveCache.getInstance(this).getIcon(sip.getMimeType(), intent);
11801cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    // Call mutate to create a new Drawable.ConstantState for color filtering
11811cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    if (icon != null) {
11821cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                        icon.mutate();
11831cda01b35a37f79b22e907cc970314b8c0a9e00cPaul Soulos                    }
118416339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1185edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1186eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
1187eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
1188eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(givenName)) {
1189eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mAboutCard.setTitle(getResources().getString(R.string.about_card_title) +
1190eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        " " + givenName);
1191eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
1192eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mAboutCard.setTitle(getResources().getString(R.string.about_card_title));
1193eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1194eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1195eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
1196eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = dataItem.buildDataStringForDisplay(this, kind);
1197eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1198eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
119960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
120060e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
1201e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1202e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
1203e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
1204e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1205e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                // Attempt to use known icons for known 3p types. Otherwise default to ResolveCache
1206e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
1207e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
1208e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE.equals(
1209e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                intent.getDataString())) {
1210e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(
1211e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                    R.drawable.ic_add_to_circles_black_24);
1212e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
1213e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_google_plus_24dp);
1214e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1215e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1216e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
1217e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_HANGOUTS_VIDEO.equals(intent.getDataString())) {
1218e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_hangout_video_24dp);
1219e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
1220e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_hangout_24dp);
1221e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1222e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1223e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
1224e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        icon = ResolveCache.getInstance(this).getIcon(
1225e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
1226e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
1227e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
1228e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
1229e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
123048ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
1231e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
1232e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
1233eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1234b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1235eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
1236eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
1237eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!PhoneCapabilityTester.isIntentRegistered(this, intent)) {
1238eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
1239eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1240eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1241eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1242dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
1243dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
1244dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (!PhoneCapabilityTester.isIntentRegistered(this, alternateIntent)) {
1245dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
1246dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1247dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1248dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Attempt to use package manager to find a suitable content description if needed
1249dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (TextUtils.isEmpty(alternateContentDescription)) {
1250dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateContentDescription = getIntentResolveLabel(alternateIntent);
1251dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1252dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1253dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1254eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
1255eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
1256eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
1257eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
1258eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1259eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1260ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
1261ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
1262ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
1263dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon, intent,
1264dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon, alternateIntent, alternateContentDescription, shouldApplyColor,
1265dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                isEditable);
1266eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1267eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1268eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private List<Entry> dataItemsToEntries(List<DataItem> dataItems) {
1269eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1270eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
1271eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Entry entry = dataItemToEntry(dataItem);
1272eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (entry != null) {
1273eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                entries.add(entry);
1274eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1275eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1276eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
1277edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1278edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1279dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    private String getIntentResolveLabel(Intent intent) {
1280dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent,
1281dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
1282dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1283dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
1284dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
1285dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
1286dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
1287dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
1288dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
1289dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = ResolveCache.getInstance(this).getBestResolve(intent, matches);
1290dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1291dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1292dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
1293dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
1294dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1295dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1296dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return String.valueOf(bestResolve.loadLabel(getPackageManager()));
1297dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
1298dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1299edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
130031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
130131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
130231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
130331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
130431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
130531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
130631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
130731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
130831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1309faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
131031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1311faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
1312faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
131331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
13148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
1315faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = colorFromBitmap(bitmap);
1316faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    if (primaryColor != 0) {
1317a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                        return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
1318faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                                primaryColor);
1319faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
13208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
13218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
1322faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
1323a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
132431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
1325a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
132631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
132731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
132831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1329faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
1330faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
13318571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
13328571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
13338571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
13348571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
13358571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
13368571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
13378571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
13388571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
13398571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
13408571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
13418571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
13428571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
1343faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
134431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
134531b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
134631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
134731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
134831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
13494936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    /**
13504936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * Examine how many white pixels are in the bitmap in order to determine whether or not
13514936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * we need gradient overlays on top of the image.
13524936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     */
13534936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    private void analyzeWhitenessOfPhotoAsynchronously() {
13544936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
13554936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        new AsyncTask<Void, Void, Boolean>() {
13564936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
13574936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected Boolean doInBackground(Void... params) {
13584936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
13594936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
13604936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    return WhitenessUtils.isBitmapWhiteAtTopOrBottom(bitmap);
13614936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                }
13624936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                return !(imageViewDrawable instanceof LetterTileDrawable);
13634936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
13644936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
13654936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
13664936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected void onPostExecute(Boolean isWhite) {
13674936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                super.onPostExecute(isWhite);
13684936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                mScroller.setUseGradient(isWhite);
13694936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
13704936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        }.execute();
13714936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    }
13724936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
1373faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
13749b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
1375faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        final int primaryColor = palette.mPrimaryColor;
1376faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mScroller.setHeaderTintColor(primaryColor);
1377faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
13789b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
13798571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
13809b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
1381faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                new PorterDuffColorFilter(primaryColor, PorterDuff.Mode.SRC_ATOP);
1382faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mContactCard.setColorAndFilter(primaryColor, mColorFilter);
1383faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mRecentCard.setColorAndFilter(primaryColor, mColorFilter);
1384faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mAboutCard.setColorAndFilter(primaryColor, mColorFilter);
13859b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
13869b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
13878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
13888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller == null) {
13898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
13908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
13918a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
13928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
13938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
13948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
13958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
13968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
13978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
13988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
1399847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
1400847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell                getWindow().getStatusBarColor(), desiredStatusBarColor);
1401847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
1402847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.setEvaluator(new ArgbEvaluator());
1403847bf2cd3946b1801c83c745d7183aed5143b44cBrian Attwell        animation.start();
14048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
14058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
14068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
14078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
14088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
14098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
1410a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
1411a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
14128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
14138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
14148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
14158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1416b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
1417eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1418b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
1419ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
1420ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
1421b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
1422b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
1423b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
1424b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
1425b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
1426b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
1427dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
1428dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
1429dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
143048ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
1431b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* isEditable = */ false));
1432b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1433b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
1434b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1435b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1436eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
1437851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
1438cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1439851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
1440405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
1441cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1442cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1443cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1444851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
14458a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
14468a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1447cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (isFinishing()) {
1448cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1449cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1450cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isError()) {
1451cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // This shouldn't ever happen, so throw an exception. The {@link ContactLoader}
1452cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // should log the actual exception.
1453cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                throw new IllegalStateException("Failed to load contact", data.getException());
1454cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1455cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isNotFound()) {
14568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (mHasAlreadyBeenOpened) {
14578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    finish();
14588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                } else {
14598a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
14608a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
14618a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
14628a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
1463cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1464cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1465cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1466b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            bindContactData(data);
1467cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
14688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.endSection();
1469cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1470cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1471cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1472851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
1473cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
1474cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
1475cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1476d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
1477d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
1478b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
1479d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
14808571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
1481cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1482cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
1483b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1484b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
1485b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
1486b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
14878477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
14888477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
14898477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
1490b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
1491b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
1492b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
1493b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
1494b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
14958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
14968a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
14978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
14988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
14998a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
15008a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
15018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
15028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1503eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
1504b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
1505b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1506b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1507b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
1508b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Log.v(TAG, "onCreateLoader");
1509b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
1510b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
1511b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
1512b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    Log.v(TAG, "LOADER_SMS_ID");
1513b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
1514b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
1515ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1516b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
1517b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
1518899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
1519899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    Log.v(TAG, "LOADER_CALENDAR_ID");
1520ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
1521ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
1522ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
1523ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
1524ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
1525899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
1526899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
1527ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
1528899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
1529899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
1530899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
1531899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
1532899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
1533ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
1534ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    Log.v(TAG, "LOADER_CALL_LOG_ID");
1535ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
1536ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
1537ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1538ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
1539b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1540b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
1541b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1542b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1543b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1544b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
1545b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
1546b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
1547b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1548b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
1549b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
1550b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1551b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1552b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1553b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1554b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
1555b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
1556b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1557b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
1558b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1559b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
1560b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
1561b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1562b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1563b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
1564eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
1565b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
1566b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            allInteractions.addAll(loaderInteractions);
1567b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1568b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1569b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        // Sort the interactions by most recent
1570b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
1571b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
1572b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            public int compare(ContactInteraction a, ContactInteraction b) {
1573b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                return a.getInteractionDate() >= b.getInteractionDate() ? -1 : 1;
1574b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1575b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        });
1576b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
157797ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        // Wrap each interaction in its own list so that an icon is displayed for each entry
157860e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        List<List<Entry>> interactionsWrapper = new ArrayList<>();
157997ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        for (Entry contactInteraction : contactInteractionsToEntries(allInteractions)) {
158097ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            List<Entry> entryListWrapper = new ArrayList<>(1);
158197ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            entryListWrapper.add(contactInteraction);
158297ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos            interactionsWrapper.add(entryListWrapper);
158397ed5019d75bd6afdfd5f3a8150161d9d9441275Paul Soulos        }
1584b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        if (allInteractions.size() > 0) {
158560e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            mRecentCard.initialize(interactionsWrapper,
1586b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
1587c8e2a91807b581194566eb45019fa16e2268b462Paul Soulos                    /* isExpanded = */ mRecentCard.isExpanded(), /* isAlwaysExpanded = */ false,
15880cda9aeb01f1922fce2a9e87ae4c0146c177b4f0Paul Soulos                    mExpandingEntryCardViewListener, mScroller);
1589b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentCard.setVisibility(View.VISIBLE);
1590b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1591eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1592eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // About card is initialized along with the contact card, but since it appears after
1593eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // the recent card in the UI, we hold off until making it visible until the recent card
1594eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // is also ready to avoid stuttering.
1595eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (mAboutCard.shouldShow()) {
1596eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1597eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1598eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.GONE);
1599eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1600b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
16018a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
16028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
16038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
16048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
16058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
16068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
16078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
16088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
16098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
16108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
16118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
16128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
16138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
161423889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
161523889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
1616d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
1617d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
1618d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
1619d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1620d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1621d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1622a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    /**
1623a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     * Returns true if it is possible to share the current contact.
1624a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell     */
1625a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    private boolean isContactShareable() {
1626a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1627a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell    }
1628a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
16296095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
1630d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Intent intent = new Intent(Intent.ACTION_EDIT, mLookupUri);
1631d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mContactLoader.cacheResult();
1632d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
16336095369885edcca566a812b551886e29c7ff8039Brian Attwell        return intent;
16346095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
16356095369885edcca566a812b551886e29c7ff8039Brian Attwell
16366095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
16376095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
1638d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1639d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1640d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
1641d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
1642d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mLookupUri != null) {
1643d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
1644d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
1645d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
1646d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
1647d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1648d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
1649333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
1650d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1651d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
1652d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1653d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
1654eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
1655d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    QuickContactActivity.this, mLookupUri, !isStarred);
1656d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
165735ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell
165835ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            final CharSequence accessibilityText = !isStarred
165935ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    ? getResources().getText(R.string.description_action_menu_add_star)
166035ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell                    : getResources().getText(R.string.description_action_menu_remove_star);
166135ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // Accessibility actions need to have an associated view. We can't access the MenuItem's
166235ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            // underlying view, so put this accessibility action on the root view.
166335ccdba7b8b2270204f7f96d824d258e408fe6efBrian Attwell            mScroller.announceForAccessibility(accessibilityText);
1664d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
1665d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1666d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1667752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1668752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Calls into the contacts provider to get a pre-authorized version of the given URI.
1669752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1670752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private Uri getPreAuthorizedUri(Uri uri) {
1671752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle uriBundle = new Bundle();
1672752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        uriBundle.putParcelable(ContactsContract.Authorization.KEY_URI_TO_AUTHORIZE, uri);
1673752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle authResponse = getContentResolver().call(
1674752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.AUTHORITY_URI,
1675752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.Authorization.AUTHORIZATION_METHOD,
1676752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                null,
1677752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                uriBundle);
1678752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (authResponse != null) {
1679752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return (Uri) authResponse.getParcelable(
1680752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    ContactsContract.Authorization.KEY_AUTHORIZED_URI);
1681752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        } else {
1682752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return uri;
1683752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1684752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
16852d150da246632b1649999cfabed776133b097775Brian Attwell
1686752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
1687752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
1688752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
1689752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (mContactData.isUserProfile()) {
1690752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // User is sharing the profile.  We don't want to force the receiver to have
1691752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // the highly-privileged READ_PROFILE permission, so we need to request a
1692752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // pre-authorized URI from the provider.
1693752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            shareUri = getPreAuthorizedUri(shareUri);
1694752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1695752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1696752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
1697752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.setType(Contacts.CONTENT_VCARD_TYPE);
1698752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
1699752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1700752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
1701752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
1702752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
1703752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1704752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
1705752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            this.startActivity(chooseIntent);
1706eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
1707752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
1708752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1709752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1710752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1711752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1712752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
1713752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1714752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
1715752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
1716752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
1717752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1718752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
1719752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
1720752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
1721752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
1722752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
1723752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
1724752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1725752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
1726752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
1727752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        Toast.makeText(QuickContactActivity.this,
1728752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                R.string.createContactShortcutSuccessful,
1729752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
1730752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
1731752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1732752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
1733752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        builder.createContactShortcutIntent(mLookupUri);
1734752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1735752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1736d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1737d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
1738eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
1739d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
1740d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
1741d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1742d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1743d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1744d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
1745d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
1746d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
1747333091ae754ddfc25714c14b9b89534be24379f9Paul Soulos            ContactDisplayUtils.configureStarredMenuItem(starredMenuItem,
1748d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1749d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
1750a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
1751d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
1752d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
1753d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
1754d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
1755d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
175630cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
17572e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
1758d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
1759d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
17602e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
1761d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
1762d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
1763d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
1764a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
1765a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            final MenuItem shareMenuItem = menu.findItem(R.id.menu_share);
1766a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell            shareMenuItem.setVisible(isContactShareable());
1767a41d6d1c9d87fe81630426f537bdb864c1ee6db4Brian Attwell
17688a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
1769d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
17708a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
1771d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1772d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1773d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1774d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
1775d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
1776d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
1777d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
1778d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1779d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
1780d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
1781d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    DirectoryContactUtil.addToMyContacts(mContactData, this, getFragmentManager(),
1782d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                            mSelectAccountFragmentListener);
1783d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
1784d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
1785d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
1786d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
1787d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
1788d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1789752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
1790752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                shareContact();
1791752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
1792752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
1793752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                createLauncherShortcutWithContact();
1794752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
1795d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
1796d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
1797d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
179823889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
1799edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
1800