Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1 | /* |
Daniel Lehmann | ef3f8f0 | 2010-07-26 18:55:25 -0700 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 17 | package com.android.contacts; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 18 | |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 19 | import com.android.contacts.model.AccountType; |
| 20 | import com.android.contacts.model.AccountTypeManager; |
Makoto Onuki | 6ad227f | 2011-08-15 13:46:59 -0700 | [diff] [blame] | 21 | import com.android.contacts.model.AccountTypeWithDataSet; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 22 | import com.android.contacts.model.EntityDeltaList; |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 23 | import com.android.contacts.util.ContactLoaderUtils; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 24 | import com.android.contacts.util.DataStatus; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 25 | import com.android.contacts.util.StreamItemEntry; |
| 26 | import com.android.contacts.util.StreamItemPhotoEntry; |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 27 | import com.android.contacts.util.UriUtils; |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 28 | import com.google.common.annotations.VisibleForTesting; |
Daniel Lehmann | 49ea2d2 | 2012-04-17 11:17:12 -0700 | [diff] [blame] | 29 | import com.google.common.collect.Lists; |
Makoto Onuki | 6ad227f | 2011-08-15 13:46:59 -0700 | [diff] [blame] | 30 | import com.google.common.collect.Maps; |
Makoto Onuki | aba2b83 | 2011-08-12 15:44:53 -0700 | [diff] [blame] | 31 | import com.google.common.collect.Sets; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 32 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 33 | import android.content.AsyncTaskLoader; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 34 | import android.content.ContentResolver; |
| 35 | import android.content.ContentUris; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 36 | import android.content.ContentValues; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 37 | import android.content.Context; |
| 38 | import android.content.Entity; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 39 | import android.content.Entity.NamedContentValues; |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 40 | import android.content.Intent; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 41 | import android.content.pm.PackageManager; |
| 42 | import android.content.pm.PackageManager.NameNotFoundException; |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 43 | import android.content.res.AssetFileDescriptor; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 44 | import android.content.res.Resources; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 45 | import android.database.Cursor; |
| 46 | import android.net.Uri; |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 47 | import android.provider.ContactsContract; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 48 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 49 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 50 | import android.provider.ContactsContract.Contacts; |
| 51 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 52 | import android.provider.ContactsContract.Directory; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 53 | import android.provider.ContactsContract.DisplayNameSources; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 54 | import android.provider.ContactsContract.Groups; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 55 | import android.provider.ContactsContract.RawContacts; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 56 | import android.provider.ContactsContract.StreamItemPhotos; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 57 | import android.provider.ContactsContract.StreamItems; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 58 | import android.text.TextUtils; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 59 | import android.util.Log; |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 60 | import android.util.LongSparseArray; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 61 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 62 | import java.io.ByteArrayOutputStream; |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 63 | import java.io.FileInputStream; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 64 | import java.io.IOException; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 65 | import java.util.ArrayList; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 66 | import java.util.Collections; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 67 | import java.util.List; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 68 | import java.util.Map; |
Makoto Onuki | aba2b83 | 2011-08-12 15:44:53 -0700 | [diff] [blame] | 69 | import java.util.Set; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Loads a single Contact and all it constituent RawContacts. |
| 73 | */ |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 74 | public class ContactLoader extends AsyncTaskLoader<ContactLoader.Result> { |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 75 | private static final String TAG = ContactLoader.class.getSimpleName(); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 76 | |
Makoto Onuki | da9cdc1 | 2012-02-27 16:11:50 -0800 | [diff] [blame] | 77 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 78 | |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 79 | /** A short-lived cache that can be set by {@link #cacheResult()} */ |
| 80 | private static Result sCachedResult = null; |
| 81 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 82 | private final Uri mRequestedUri; |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 83 | private Uri mLookupUri; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 84 | private boolean mLoadGroupMetaData; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 85 | private boolean mLoadStreamItems; |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 86 | private boolean mLoadInvitableAccountTypes; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 87 | private Result mContact; |
| 88 | private ForceLoadContentObserver mObserver; |
Makoto Onuki | aba2b83 | 2011-08-12 15:44:53 -0700 | [diff] [blame] | 89 | private final Set<Long> mNotifiedRawContactIds = Sets.newHashSet(); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 90 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 91 | public interface Listener { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 92 | public void onContactLoaded(Result contact); |
| 93 | } |
| 94 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 95 | public ContactLoader(Context context, Uri lookupUri) { |
| 96 | this(context, lookupUri, false, false, false); |
| 97 | } |
| 98 | |
| 99 | public ContactLoader(Context context, Uri lookupUri, boolean loadGroupMetaData, |
| 100 | boolean loadStreamItems, boolean loadInvitableAccountTypes) { |
| 101 | super(context); |
| 102 | mLookupUri = lookupUri; |
| 103 | mRequestedUri = lookupUri; |
| 104 | mLoadGroupMetaData = loadGroupMetaData; |
| 105 | mLoadStreamItems = loadStreamItems; |
| 106 | mLoadInvitableAccountTypes = loadInvitableAccountTypes; |
| 107 | } |
| 108 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 109 | /** |
| 110 | * The result of a load operation. Contains all data necessary to display the contact. |
| 111 | */ |
| 112 | public static final class Result { |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 113 | private enum Status { |
| 114 | /** Contact is successfully loaded */ |
| 115 | LOADED, |
| 116 | /** There was an error loading the contact */ |
| 117 | ERROR, |
| 118 | /** Contact is not found */ |
| 119 | NOT_FOUND, |
| 120 | } |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 121 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 122 | private final Uri mRequestedUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 123 | private final Uri mLookupUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 124 | private final Uri mUri; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 125 | private final long mDirectoryId; |
| 126 | private final String mLookupKey; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 127 | private final long mId; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 128 | private final long mNameRawContactId; |
| 129 | private final int mDisplayNameSource; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 130 | private final long mPhotoId; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 131 | private final String mPhotoUri; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 132 | private final String mDisplayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 133 | private final String mAltDisplayName; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 134 | private final String mPhoneticName; |
| 135 | private final boolean mStarred; |
| 136 | private final Integer mPresence; |
| 137 | private final ArrayList<Entity> mEntities; |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 138 | private ArrayList<StreamItemEntry> mStreamItems; |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 139 | private final LongSparseArray<DataStatus> mStatuses; |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 140 | private ArrayList<AccountType> mInvitableAccountTypes; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 141 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 142 | private String mDirectoryDisplayName; |
| 143 | private String mDirectoryType; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 144 | private String mDirectoryAccountType; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 145 | private String mDirectoryAccountName; |
| 146 | private int mDirectoryExportSupport; |
| 147 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 148 | private ArrayList<GroupMetaData> mGroups; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 149 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 150 | private byte[] mPhotoBinaryData; |
Makoto Onuki | 870a87e | 2011-08-12 13:40:31 -0700 | [diff] [blame] | 151 | private final boolean mSendToVoicemail; |
| 152 | private final String mCustomRingtone; |
| 153 | private final boolean mIsUserProfile; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 154 | |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 155 | private final Status mStatus; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 156 | private final Exception mException; |
| 157 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 158 | /** |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 159 | * Constructor for special results, namely "no contact found" and "error". |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 160 | */ |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 161 | private Result(Uri requestedUri, Status status, Exception exception) { |
| 162 | if (status == Status.ERROR && exception == null) { |
| 163 | throw new IllegalArgumentException("ERROR result must have exception"); |
| 164 | } |
| 165 | mStatus = status; |
| 166 | mException = exception; |
| 167 | mRequestedUri = requestedUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 168 | mLookupUri = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 169 | mUri = null; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 170 | mDirectoryId = -1; |
| 171 | mLookupKey = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 172 | mId = -1; |
| 173 | mEntities = null; |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 174 | mStreamItems = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 175 | mStatuses = null; |
| 176 | mNameRawContactId = -1; |
| 177 | mDisplayNameSource = DisplayNameSources.UNDEFINED; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 178 | mPhotoId = -1; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 179 | mPhotoUri = null; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 180 | mDisplayName = null; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 181 | mAltDisplayName = null; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 182 | mPhoneticName = null; |
| 183 | mStarred = false; |
| 184 | mPresence = null; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 185 | mInvitableAccountTypes = null; |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 186 | mSendToVoicemail = false; |
| 187 | mCustomRingtone = null; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 188 | mIsUserProfile = false; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 189 | } |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 190 | |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 191 | private static Result forError(Uri requestedUri, Exception exception) { |
| 192 | return new Result(requestedUri, Status.ERROR, exception); |
| 193 | } |
| 194 | |
| 195 | private static Result forNotFound(Uri requestedUri) { |
| 196 | return new Result(requestedUri, Status.NOT_FOUND, null); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Constructor to call when contact was found |
| 201 | */ |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 202 | private Result(Uri requestedUri, Uri uri, Uri lookupUri, long directoryId, String lookupKey, |
| 203 | long id, long nameRawContactId, int displayNameSource, long photoId, |
| 204 | String photoUri, String displayName, String altDisplayName, String phoneticName, |
| 205 | boolean starred, Integer presence, boolean sendToVoicemail, String customRingtone, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 206 | boolean isUserProfile) { |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 207 | mStatus = Status.LOADED; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 208 | mException = null; |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 209 | mRequestedUri = requestedUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 210 | mLookupUri = lookupUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 211 | mUri = uri; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 212 | mDirectoryId = directoryId; |
| 213 | mLookupKey = lookupKey; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 214 | mId = id; |
| 215 | mEntities = new ArrayList<Entity>(); |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 216 | mStreamItems = null; |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 217 | mStatuses = new LongSparseArray<DataStatus>(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 218 | mNameRawContactId = nameRawContactId; |
| 219 | mDisplayNameSource = displayNameSource; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 220 | mPhotoId = photoId; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 221 | mPhotoUri = photoUri; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 222 | mDisplayName = displayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 223 | mAltDisplayName = altDisplayName; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 224 | mPhoneticName = phoneticName; |
| 225 | mStarred = starred; |
| 226 | mPresence = presence; |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 227 | mInvitableAccountTypes = null; |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 228 | mSendToVoicemail = sendToVoicemail; |
| 229 | mCustomRingtone = customRingtone; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 230 | mIsUserProfile = isUserProfile; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 233 | private Result(Result from) { |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 234 | mStatus = from.mStatus; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 235 | mException = from.mException; |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 236 | mRequestedUri = from.mRequestedUri; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 237 | mLookupUri = from.mLookupUri; |
| 238 | mUri = from.mUri; |
| 239 | mDirectoryId = from.mDirectoryId; |
| 240 | mLookupKey = from.mLookupKey; |
| 241 | mId = from.mId; |
| 242 | mNameRawContactId = from.mNameRawContactId; |
| 243 | mDisplayNameSource = from.mDisplayNameSource; |
| 244 | mPhotoId = from.mPhotoId; |
| 245 | mPhotoUri = from.mPhotoUri; |
| 246 | mDisplayName = from.mDisplayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 247 | mAltDisplayName = from.mAltDisplayName; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 248 | mPhoneticName = from.mPhoneticName; |
| 249 | mStarred = from.mStarred; |
| 250 | mPresence = from.mPresence; |
| 251 | mEntities = from.mEntities; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 252 | mStreamItems = from.mStreamItems; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 253 | mStatuses = from.mStatuses; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 254 | mInvitableAccountTypes = from.mInvitableAccountTypes; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 255 | |
| 256 | mDirectoryDisplayName = from.mDirectoryDisplayName; |
| 257 | mDirectoryType = from.mDirectoryType; |
| 258 | mDirectoryAccountType = from.mDirectoryAccountType; |
| 259 | mDirectoryAccountName = from.mDirectoryAccountName; |
| 260 | mDirectoryExportSupport = from.mDirectoryExportSupport; |
| 261 | |
| 262 | mGroups = from.mGroups; |
| 263 | |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 264 | mPhotoBinaryData = from.mPhotoBinaryData; |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 265 | mSendToVoicemail = from.mSendToVoicemail; |
| 266 | mCustomRingtone = from.mCustomRingtone; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 267 | mIsUserProfile = from.mIsUserProfile; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 270 | /** |
| 271 | * @param exportSupport See {@link Directory#EXPORT_SUPPORT}. |
| 272 | */ |
Daniel Lehmann | 1ad4d1b | 2010-10-18 19:20:41 -0700 | [diff] [blame] | 273 | private void setDirectoryMetaData(String displayName, String directoryType, |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 274 | String accountType, String accountName, int exportSupport) { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 275 | mDirectoryDisplayName = displayName; |
| 276 | mDirectoryType = directoryType; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 277 | mDirectoryAccountType = accountType; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 278 | mDirectoryAccountName = accountName; |
| 279 | mDirectoryExportSupport = exportSupport; |
| 280 | } |
| 281 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 282 | private void setPhotoBinaryData(byte[] photoBinaryData) { |
| 283 | mPhotoBinaryData = photoBinaryData; |
| 284 | } |
| 285 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 286 | /** |
| 287 | * Returns the URI for the contact that contains both the lookup key and the ID. This is |
| 288 | * the best URI to reference a contact. |
| 289 | * For directory contacts, this is the same a the URI as returned by {@link #getUri()} |
| 290 | */ |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 291 | public Uri getLookupUri() { |
| 292 | return mLookupUri; |
| 293 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 294 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 295 | public String getLookupKey() { |
| 296 | return mLookupKey; |
| 297 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 298 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Returns the contact Uri that was passed to the provider to make the query. This is |
| 301 | * the same as the requested Uri, unless the requested Uri doesn't specify a Contact: |
| 302 | * If it either references a Raw-Contact or a Person (a pre-Eclair style Uri), this Uri will |
| 303 | * always reference the full aggregate contact. |
| 304 | */ |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 305 | public Uri getUri() { |
| 306 | return mUri; |
| 307 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 308 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 309 | /** |
| 310 | * Returns the URI for which this {@link ContactLoader) was initially requested. |
| 311 | */ |
| 312 | public Uri getRequestedUri() { |
| 313 | return mRequestedUri; |
| 314 | } |
| 315 | |
Dave Santoro | 6fa7384 | 2011-09-28 14:37:06 -0700 | [diff] [blame] | 316 | /** |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 317 | * Instantiate a new EntityDeltaList for this contact. |
| 318 | */ |
| 319 | public EntityDeltaList createEntityDeltaList() { |
| 320 | return EntityDeltaList.fromIterator(getEntities().iterator()); |
| 321 | } |
| 322 | |
| 323 | /** |
Dave Santoro | 6fa7384 | 2011-09-28 14:37:06 -0700 | [diff] [blame] | 324 | * Returns the contact ID. |
| 325 | */ |
Makoto Onuki | 9830610 | 2011-11-28 15:16:58 -0800 | [diff] [blame] | 326 | @VisibleForTesting |
| 327 | /* package */ long getId() { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 328 | return mId; |
| 329 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 330 | |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 331 | /** |
| 332 | * @return true when an exception happened during loading, in which case |
| 333 | * {@link #getException} returns the actual exception object. |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 334 | * Note {@link #isNotFound()} and {@link #isError()} are mutually exclusive; If |
| 335 | * {@link #isError()} is {@code true}, {@link #isNotFound()} is always {@code false}, |
| 336 | * and vice versa. |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 337 | */ |
| 338 | public boolean isError() { |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 339 | return mStatus == Status.ERROR; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | public Exception getException() { |
| 343 | return mException; |
| 344 | } |
| 345 | |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 346 | /** |
| 347 | * @return true when the specified contact is not found. |
| 348 | * Note {@link #isNotFound()} and {@link #isError()} are mutually exclusive; If |
| 349 | * {@link #isError()} is {@code true}, {@link #isNotFound()} is always {@code false}, |
| 350 | * and vice versa. |
| 351 | */ |
| 352 | public boolean isNotFound() { |
| 353 | return mStatus == Status.NOT_FOUND; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * @return true if the specified contact is successfully loaded. |
| 358 | * i.e. neither {@link #isError()} nor {@link #isNotFound()}. |
| 359 | */ |
| 360 | public boolean isLoaded() { |
| 361 | return mStatus == Status.LOADED; |
| 362 | } |
| 363 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 364 | public long getNameRawContactId() { |
| 365 | return mNameRawContactId; |
| 366 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 367 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 368 | public int getDisplayNameSource() { |
| 369 | return mDisplayNameSource; |
| 370 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 371 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 372 | public long getPhotoId() { |
| 373 | return mPhotoId; |
| 374 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 375 | |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 376 | public String getPhotoUri() { |
| 377 | return mPhotoUri; |
| 378 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 379 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 380 | public String getDisplayName() { |
| 381 | return mDisplayName; |
| 382 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 383 | |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 384 | public String getAltDisplayName() { |
| 385 | return mAltDisplayName; |
| 386 | } |
| 387 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 388 | public String getPhoneticName() { |
| 389 | return mPhoneticName; |
| 390 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 391 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 392 | public boolean getStarred() { |
| 393 | return mStarred; |
| 394 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 395 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 396 | public Integer getPresence() { |
| 397 | return mPresence; |
| 398 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 399 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 400 | public ArrayList<AccountType> getInvitableAccountTypes() { |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 401 | return mInvitableAccountTypes; |
| 402 | } |
| 403 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 404 | public ArrayList<Entity> getEntities() { |
| 405 | return mEntities; |
| 406 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 407 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 408 | public ArrayList<StreamItemEntry> getStreamItems() { |
| 409 | return mStreamItems; |
| 410 | } |
| 411 | |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 412 | public LongSparseArray<DataStatus> getStatuses() { |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 413 | return mStatuses; |
| 414 | } |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 415 | |
| 416 | public long getDirectoryId() { |
| 417 | return mDirectoryId; |
| 418 | } |
| 419 | |
| 420 | public boolean isDirectoryEntry() { |
Dmitri Plotnikov | 5f72c1f | 2010-09-01 21:21:04 -0700 | [diff] [blame] | 421 | return mDirectoryId != -1 && mDirectoryId != Directory.DEFAULT |
| 422 | && mDirectoryId != Directory.LOCAL_INVISIBLE; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Josh Gargus | 187c816 | 2012-03-13 17:06:53 -0700 | [diff] [blame] | 425 | /** |
| 426 | * @return true if this is a contact (not group, etc.) with at least one |
Josh Gargus | 84edfd9 | 2012-03-15 18:25:58 -0700 | [diff] [blame] | 427 | * writable raw-contact, and false otherwise. |
Josh Gargus | 187c816 | 2012-03-13 17:06:53 -0700 | [diff] [blame] | 428 | */ |
Josh Gargus | 84edfd9 | 2012-03-15 18:25:58 -0700 | [diff] [blame] | 429 | public boolean isWritableContact(final Context context) { |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 430 | return getFirstWritableRawContactId(context) != -1; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Return the ID of the first raw-contact in the contact data that belongs to a |
| 435 | * contact-writable account, or -1 if no such entity exists. |
| 436 | */ |
| 437 | public long getFirstWritableRawContactId(final Context context) { |
| 438 | // Directory entries are non-writable |
| 439 | if (isDirectoryEntry()) return -1; |
| 440 | |
| 441 | // Iterate through raw-contacts; if we find a writable on, return its ID. |
Josh Gargus | 187c816 | 2012-03-13 17:06:53 -0700 | [diff] [blame] | 442 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(context); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 443 | for (Entity entity : getEntities()) { |
| 444 | ContentValues values = entity.getEntityValues(); |
| 445 | String type = values.getAsString(RawContacts.ACCOUNT_TYPE); |
| 446 | String dataSet = values.getAsString(RawContacts.DATA_SET); |
| 447 | |
| 448 | AccountType accountType = accountTypes.getAccountType(type, dataSet); |
| 449 | if (accountType != null && accountType.areContactsWritable()) { |
| 450 | return values.getAsLong(RawContacts._ID); |
| 451 | } |
Josh Gargus | 187c816 | 2012-03-13 17:06:53 -0700 | [diff] [blame] | 452 | } |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 453 | // No writable raw-contact was found. |
| 454 | return -1; |
Josh Gargus | 187c816 | 2012-03-13 17:06:53 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 457 | public int getDirectoryExportSupport() { |
| 458 | return mDirectoryExportSupport; |
| 459 | } |
| 460 | |
| 461 | public String getDirectoryDisplayName() { |
| 462 | return mDirectoryDisplayName; |
| 463 | } |
| 464 | |
| 465 | public String getDirectoryType() { |
| 466 | return mDirectoryType; |
| 467 | } |
| 468 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 469 | public String getDirectoryAccountType() { |
| 470 | return mDirectoryAccountType; |
| 471 | } |
| 472 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 473 | public String getDirectoryAccountName() { |
| 474 | return mDirectoryAccountName; |
| 475 | } |
| 476 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 477 | public byte[] getPhotoBinaryData() { |
| 478 | return mPhotoBinaryData; |
| 479 | } |
| 480 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 481 | public ArrayList<ContentValues> getContentValues() { |
| 482 | if (mEntities.size() != 1) { |
| 483 | throw new IllegalStateException( |
| 484 | "Cannot extract content values from an aggregated contact"); |
| 485 | } |
| 486 | |
| 487 | Entity entity = mEntities.get(0); |
| 488 | ArrayList<ContentValues> result = new ArrayList<ContentValues>(); |
| 489 | ArrayList<NamedContentValues> subValues = entity.getSubValues(); |
| 490 | if (subValues != null) { |
| 491 | int size = subValues.size(); |
| 492 | for (int i = 0; i < size; i++) { |
| 493 | NamedContentValues pair = subValues.get(i); |
| 494 | if (Data.CONTENT_URI.equals(pair.uri)) { |
| 495 | result.add(pair.values); |
| 496 | } |
| 497 | } |
| 498 | } |
Dmitri Plotnikov | 40ec3a8 | 2010-11-10 11:25:33 -0800 | [diff] [blame] | 499 | |
| 500 | // If the photo was loaded using the URI, create an entry for the photo |
| 501 | // binary data. |
| 502 | if (mPhotoId == 0 && mPhotoBinaryData != null) { |
| 503 | ContentValues photo = new ContentValues(); |
| 504 | photo.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE); |
| 505 | photo.put(Photo.PHOTO, mPhotoBinaryData); |
| 506 | result.add(photo); |
| 507 | } |
| 508 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 509 | return result; |
| 510 | } |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 511 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 512 | public List<GroupMetaData> getGroupMetaData() { |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 513 | return mGroups; |
| 514 | } |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 515 | |
| 516 | public boolean isSendToVoicemail() { |
| 517 | return mSendToVoicemail; |
| 518 | } |
| 519 | |
| 520 | public String getCustomRingtone() { |
| 521 | return mCustomRingtone; |
| 522 | } |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 523 | |
| 524 | public boolean isUserProfile() { |
| 525 | return mIsUserProfile; |
| 526 | } |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 529 | /** |
| 530 | * Projection used for the query that loads all data for the entire contact (except for |
| 531 | * social stream items). |
| 532 | */ |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 533 | private static class ContactQuery { |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 534 | final static String[] COLUMNS = new String[] { |
| 535 | Contacts.NAME_RAW_CONTACT_ID, |
| 536 | Contacts.DISPLAY_NAME_SOURCE, |
| 537 | Contacts.LOOKUP_KEY, |
| 538 | Contacts.DISPLAY_NAME, |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 539 | Contacts.DISPLAY_NAME_ALTERNATIVE, |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 540 | Contacts.PHONETIC_NAME, |
| 541 | Contacts.PHOTO_ID, |
| 542 | Contacts.STARRED, |
| 543 | Contacts.CONTACT_PRESENCE, |
| 544 | Contacts.CONTACT_STATUS, |
| 545 | Contacts.CONTACT_STATUS_TIMESTAMP, |
| 546 | Contacts.CONTACT_STATUS_RES_PACKAGE, |
| 547 | Contacts.CONTACT_STATUS_LABEL, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 548 | Contacts.Entity.CONTACT_ID, |
| 549 | Contacts.Entity.RAW_CONTACT_ID, |
| 550 | |
| 551 | RawContacts.ACCOUNT_NAME, |
| 552 | RawContacts.ACCOUNT_TYPE, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 553 | RawContacts.DATA_SET, |
| 554 | RawContacts.ACCOUNT_TYPE_AND_DATA_SET, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 555 | RawContacts.DIRTY, |
| 556 | RawContacts.VERSION, |
| 557 | RawContacts.SOURCE_ID, |
| 558 | RawContacts.SYNC1, |
| 559 | RawContacts.SYNC2, |
| 560 | RawContacts.SYNC3, |
| 561 | RawContacts.SYNC4, |
| 562 | RawContacts.DELETED, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 563 | RawContacts.NAME_VERIFIED, |
| 564 | |
| 565 | Contacts.Entity.DATA_ID, |
| 566 | Data.DATA1, |
| 567 | Data.DATA2, |
| 568 | Data.DATA3, |
| 569 | Data.DATA4, |
| 570 | Data.DATA5, |
| 571 | Data.DATA6, |
| 572 | Data.DATA7, |
| 573 | Data.DATA8, |
| 574 | Data.DATA9, |
| 575 | Data.DATA10, |
| 576 | Data.DATA11, |
| 577 | Data.DATA12, |
| 578 | Data.DATA13, |
| 579 | Data.DATA14, |
| 580 | Data.DATA15, |
| 581 | Data.SYNC1, |
| 582 | Data.SYNC2, |
| 583 | Data.SYNC3, |
| 584 | Data.SYNC4, |
| 585 | Data.DATA_VERSION, |
| 586 | Data.IS_PRIMARY, |
| 587 | Data.IS_SUPER_PRIMARY, |
| 588 | Data.MIMETYPE, |
| 589 | Data.RES_PACKAGE, |
| 590 | |
| 591 | GroupMembership.GROUP_SOURCE_ID, |
| 592 | |
| 593 | Data.PRESENCE, |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 594 | Data.CHAT_CAPABILITY, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 595 | Data.STATUS, |
| 596 | Data.STATUS_RES_PACKAGE, |
| 597 | Data.STATUS_ICON, |
| 598 | Data.STATUS_LABEL, |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 599 | Data.STATUS_TIMESTAMP, |
| 600 | |
| 601 | Contacts.PHOTO_URI, |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 602 | Contacts.SEND_TO_VOICEMAIL, |
| 603 | Contacts.CUSTOM_RINGTONE, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 604 | Contacts.IS_USER_PROFILE, |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 605 | }; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 606 | |
| 607 | public final static int NAME_RAW_CONTACT_ID = 0; |
| 608 | public final static int DISPLAY_NAME_SOURCE = 1; |
| 609 | public final static int LOOKUP_KEY = 2; |
| 610 | public final static int DISPLAY_NAME = 3; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 611 | public final static int ALT_DISPLAY_NAME = 4; |
| 612 | public final static int PHONETIC_NAME = 5; |
| 613 | public final static int PHOTO_ID = 6; |
| 614 | public final static int STARRED = 7; |
| 615 | public final static int CONTACT_PRESENCE = 8; |
| 616 | public final static int CONTACT_STATUS = 9; |
| 617 | public final static int CONTACT_STATUS_TIMESTAMP = 10; |
| 618 | public final static int CONTACT_STATUS_RES_PACKAGE = 11; |
| 619 | public final static int CONTACT_STATUS_LABEL = 12; |
| 620 | public final static int CONTACT_ID = 13; |
| 621 | public final static int RAW_CONTACT_ID = 14; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 622 | |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 623 | public final static int ACCOUNT_NAME = 15; |
| 624 | public final static int ACCOUNT_TYPE = 16; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 625 | public final static int DATA_SET = 17; |
| 626 | public final static int ACCOUNT_TYPE_AND_DATA_SET = 18; |
| 627 | public final static int DIRTY = 19; |
| 628 | public final static int VERSION = 20; |
| 629 | public final static int SOURCE_ID = 21; |
| 630 | public final static int SYNC1 = 22; |
| 631 | public final static int SYNC2 = 23; |
| 632 | public final static int SYNC3 = 24; |
| 633 | public final static int SYNC4 = 25; |
| 634 | public final static int DELETED = 26; |
| 635 | public final static int NAME_VERIFIED = 27; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 636 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 637 | public final static int DATA_ID = 28; |
| 638 | public final static int DATA1 = 29; |
| 639 | public final static int DATA2 = 30; |
| 640 | public final static int DATA3 = 31; |
| 641 | public final static int DATA4 = 32; |
| 642 | public final static int DATA5 = 33; |
| 643 | public final static int DATA6 = 34; |
| 644 | public final static int DATA7 = 35; |
| 645 | public final static int DATA8 = 36; |
| 646 | public final static int DATA9 = 37; |
| 647 | public final static int DATA10 = 38; |
| 648 | public final static int DATA11 = 39; |
| 649 | public final static int DATA12 = 40; |
| 650 | public final static int DATA13 = 41; |
| 651 | public final static int DATA14 = 42; |
| 652 | public final static int DATA15 = 43; |
| 653 | public final static int DATA_SYNC1 = 44; |
| 654 | public final static int DATA_SYNC2 = 45; |
| 655 | public final static int DATA_SYNC3 = 46; |
| 656 | public final static int DATA_SYNC4 = 47; |
| 657 | public final static int DATA_VERSION = 48; |
| 658 | public final static int IS_PRIMARY = 49; |
| 659 | public final static int IS_SUPERPRIMARY = 50; |
| 660 | public final static int MIMETYPE = 51; |
| 661 | public final static int RES_PACKAGE = 52; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 662 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 663 | public final static int GROUP_SOURCE_ID = 53; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 664 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 665 | public final static int PRESENCE = 54; |
| 666 | public final static int CHAT_CAPABILITY = 55; |
| 667 | public final static int STATUS = 56; |
| 668 | public final static int STATUS_RES_PACKAGE = 57; |
| 669 | public final static int STATUS_ICON = 58; |
| 670 | public final static int STATUS_LABEL = 59; |
| 671 | public final static int STATUS_TIMESTAMP = 60; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 672 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 673 | public final static int PHOTO_URI = 61; |
| 674 | public final static int SEND_TO_VOICEMAIL = 62; |
| 675 | public final static int CUSTOM_RINGTONE = 63; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 676 | public final static int IS_USER_PROFILE = 64; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 677 | } |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 678 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 679 | /** |
| 680 | * Projection used for the query that loads all data for the entire contact. |
| 681 | */ |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 682 | private static class DirectoryQuery { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 683 | final static String[] COLUMNS = new String[] { |
| 684 | Directory.DISPLAY_NAME, |
| 685 | Directory.PACKAGE_NAME, |
| 686 | Directory.TYPE_RESOURCE_ID, |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 687 | Directory.ACCOUNT_TYPE, |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 688 | Directory.ACCOUNT_NAME, |
| 689 | Directory.EXPORT_SUPPORT, |
| 690 | }; |
| 691 | |
| 692 | public final static int DISPLAY_NAME = 0; |
| 693 | public final static int PACKAGE_NAME = 1; |
| 694 | public final static int TYPE_RESOURCE_ID = 2; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 695 | public final static int ACCOUNT_TYPE = 3; |
| 696 | public final static int ACCOUNT_NAME = 4; |
| 697 | public final static int EXPORT_SUPPORT = 5; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 700 | private static class GroupQuery { |
| 701 | final static String[] COLUMNS = new String[] { |
| 702 | Groups.ACCOUNT_NAME, |
| 703 | Groups.ACCOUNT_TYPE, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 704 | Groups.DATA_SET, |
| 705 | Groups.ACCOUNT_TYPE_AND_DATA_SET, |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 706 | Groups._ID, |
| 707 | Groups.TITLE, |
| 708 | Groups.AUTO_ADD, |
| 709 | Groups.FAVORITES, |
| 710 | }; |
| 711 | |
| 712 | public final static int ACCOUNT_NAME = 0; |
| 713 | public final static int ACCOUNT_TYPE = 1; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 714 | public final static int DATA_SET = 2; |
| 715 | public final static int ACCOUNT_TYPE_AND_DATA_SET = 3; |
| 716 | public final static int ID = 4; |
| 717 | public final static int TITLE = 5; |
| 718 | public final static int AUTO_ADD = 6; |
| 719 | public final static int FAVORITES = 7; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 722 | @Override |
| 723 | public Result loadInBackground() { |
| 724 | try { |
| 725 | final ContentResolver resolver = getContext().getContentResolver(); |
| 726 | final Uri uriCurrentFormat = ContactLoaderUtils.ensureIsContactUri( |
| 727 | resolver, mLookupUri); |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 728 | final Result cachedResult = sCachedResult; |
| 729 | sCachedResult = null; |
| 730 | // Is this the same Uri as what we had before already? In that case, reuse that result |
| 731 | final Result result; |
| 732 | final boolean resultIsCached; |
| 733 | if (cachedResult != null && |
| 734 | UriUtils.areEqual(cachedResult.getLookupUri(), mLookupUri)) { |
| 735 | // We are using a cached result from earlier. Below, we should make sure |
| 736 | // we are not doing any more network or disc accesses |
| 737 | result = cachedResult; |
| 738 | resultIsCached = true; |
| 739 | } else { |
| 740 | result = loadContactEntity(resolver, uriCurrentFormat); |
| 741 | resultIsCached = false; |
| 742 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 743 | if (!result.isNotFound()) { |
| 744 | if (result.isDirectoryEntry()) { |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 745 | if (!resultIsCached) { |
| 746 | loadDirectoryMetaData(result); |
| 747 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 748 | } else if (mLoadGroupMetaData) { |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 749 | if (result.getGroupMetaData() == null) { |
| 750 | loadGroupMetaData(result); |
| 751 | } |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 752 | } |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 753 | if (mLoadStreamItems && result.getStreamItems() == null) { |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 754 | loadStreamItems(result); |
| 755 | } |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 756 | if (!resultIsCached) loadPhotoBinaryData(result); |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 757 | |
| 758 | // Note ME profile should never have "Add connection" |
Daniel Lehmann | 49ea2d2 | 2012-04-17 11:17:12 -0700 | [diff] [blame] | 759 | if (mLoadInvitableAccountTypes && result.getInvitableAccountTypes() == null) { |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 760 | loadInvitableAccountTypes(result); |
| 761 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 762 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 763 | return result; |
| 764 | } catch (Exception e) { |
| 765 | Log.e(TAG, "Error loading the contact: " + mLookupUri, e); |
| 766 | return Result.forError(mRequestedUri, e); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | private Result loadContactEntity(ContentResolver resolver, Uri contactUri) { |
| 771 | Uri entityUri = Uri.withAppendedPath(contactUri, Contacts.Entity.CONTENT_DIRECTORY); |
| 772 | Cursor cursor = resolver.query(entityUri, ContactQuery.COLUMNS, null, null, |
| 773 | Contacts.Entity.RAW_CONTACT_ID); |
| 774 | if (cursor == null) { |
| 775 | Log.e(TAG, "No cursor returned in loadContactEntity"); |
| 776 | return Result.forNotFound(mRequestedUri); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 779 | try { |
| 780 | if (!cursor.moveToFirst()) { |
| 781 | cursor.close(); |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 782 | return Result.forNotFound(mRequestedUri); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 783 | } |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 784 | |
Maurice Chu | 8a0dd0f | 2012-04-12 16:16:32 -0700 | [diff] [blame] | 785 | // Create the loaded result starting with the Contact data. |
| 786 | Result result = loadContactHeaderData(cursor, contactUri); |
| 787 | |
| 788 | // Fill in the raw contacts, which is wrapped in an Entity and any |
| 789 | // status data. Initially, result has empty entities and statuses. |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 790 | long currentRawContactId = -1; |
| 791 | Entity entity = null; |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 792 | ArrayList<Entity> entities = result.getEntities(); |
| 793 | LongSparseArray<DataStatus> statuses = result.getStatuses(); |
| 794 | for (; !cursor.isAfterLast(); cursor.moveToNext()) { |
| 795 | long rawContactId = cursor.getLong(ContactQuery.RAW_CONTACT_ID); |
| 796 | if (rawContactId != currentRawContactId) { |
Maurice Chu | 8a0dd0f | 2012-04-12 16:16:32 -0700 | [diff] [blame] | 797 | // First time to see this raw contact id, so create a new entity, and |
| 798 | // add it to the result's entities. |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 799 | currentRawContactId = rawContactId; |
| 800 | entity = new android.content.Entity(loadRawContact(cursor)); |
| 801 | entities.add(entity); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 802 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 803 | if (!cursor.isNull(ContactQuery.DATA_ID)) { |
| 804 | ContentValues data = loadData(cursor); |
| 805 | entity.addSubValue(ContactsContract.Data.CONTENT_URI, data); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 806 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 807 | if (!cursor.isNull(ContactQuery.PRESENCE) |
| 808 | || !cursor.isNull(ContactQuery.STATUS)) { |
| 809 | final DataStatus status = new DataStatus(cursor); |
| 810 | final long dataId = cursor.getLong(ContactQuery.DATA_ID); |
| 811 | statuses.put(dataId, status); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 812 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 813 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 814 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 815 | |
| 816 | return result; |
| 817 | } finally { |
| 818 | cursor.close(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 819 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 820 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 821 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 822 | /** |
| 823 | * Looks for the photo data item in entities. If found, creates a new Bitmap instance. If |
| 824 | * not found, returns null |
| 825 | */ |
| 826 | private void loadPhotoBinaryData(Result contactData) { |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 827 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 828 | // If we have a photo URI, try loading that first. |
| 829 | String photoUri = contactData.getPhotoUri(); |
| 830 | if (photoUri != null) { |
| 831 | try { |
| 832 | AssetFileDescriptor fd = getContext().getContentResolver() |
| 833 | .openAssetFileDescriptor(Uri.parse(photoUri), "r"); |
| 834 | byte[] buffer = new byte[16 * 1024]; |
| 835 | FileInputStream fis = fd.createInputStream(); |
| 836 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 837 | try { |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 838 | int size; |
| 839 | while ((size = fis.read(buffer)) != -1) { |
| 840 | baos.write(buffer, 0, size); |
| 841 | } |
| 842 | contactData.setPhotoBinaryData(baos.toByteArray()); |
| 843 | } finally { |
| 844 | fis.close(); |
| 845 | fd.close(); |
| 846 | } |
| 847 | return; |
| 848 | } catch (IOException ioe) { |
| 849 | // Just fall back to the case below. |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | // If we couldn't load from a file, fall back to the data blob. |
| 854 | final long photoId = contactData.getPhotoId(); |
| 855 | if (photoId <= 0) { |
| 856 | // No photo ID |
| 857 | return; |
| 858 | } |
| 859 | |
| 860 | for (Entity entity : contactData.getEntities()) { |
| 861 | for (NamedContentValues subValue : entity.getSubValues()) { |
| 862 | final ContentValues entryValues = subValue.values; |
| 863 | final long dataId = entryValues.getAsLong(Data._ID); |
| 864 | if (dataId == photoId) { |
| 865 | final String mimeType = entryValues.getAsString(Data.MIMETYPE); |
| 866 | // Correct Data Id but incorrect MimeType? Don't load |
| 867 | if (!Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 868 | return; |
| 869 | } |
| 870 | contactData.setPhotoBinaryData(entryValues.getAsByteArray(Photo.PHOTO)); |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * Sets the "invitable" account types to {@link Result#mInvitableAccountTypes}. |
| 879 | */ |
| 880 | private void loadInvitableAccountTypes(Result contactData) { |
Daniel Lehmann | 49ea2d2 | 2012-04-17 11:17:12 -0700 | [diff] [blame] | 881 | final ArrayList<AccountType> resultList = Lists.newArrayList(); |
| 882 | if (!contactData.isUserProfile()) { |
| 883 | Map<AccountTypeWithDataSet, AccountType> invitables = |
| 884 | AccountTypeManager.getInstance(getContext()).getUsableInvitableAccountTypes(); |
| 885 | if (!invitables.isEmpty()) { |
| 886 | final Map<AccountTypeWithDataSet, AccountType> resultMap = |
| 887 | Maps.newHashMap(invitables); |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 888 | |
Daniel Lehmann | 49ea2d2 | 2012-04-17 11:17:12 -0700 | [diff] [blame] | 889 | // Remove the ones that already have a raw contact in the current contact |
| 890 | for (Entity entity : contactData.getEntities()) { |
| 891 | final ContentValues values = entity.getEntityValues(); |
| 892 | final AccountTypeWithDataSet type = AccountTypeWithDataSet.get( |
| 893 | values.getAsString(RawContacts.ACCOUNT_TYPE), |
| 894 | values.getAsString(RawContacts.DATA_SET)); |
| 895 | resultMap.remove(type); |
| 896 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 897 | |
Daniel Lehmann | 49ea2d2 | 2012-04-17 11:17:12 -0700 | [diff] [blame] | 898 | resultList.addAll(resultMap.values()); |
| 899 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | // Set to mInvitableAccountTypes |
Daniel Lehmann | 49ea2d2 | 2012-04-17 11:17:12 -0700 | [diff] [blame] | 903 | contactData.mInvitableAccountTypes = resultList; |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Extracts Contact level columns from the cursor. |
| 908 | */ |
| 909 | private Result loadContactHeaderData(final Cursor cursor, Uri contactUri) { |
| 910 | final String directoryParameter = |
| 911 | contactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY); |
| 912 | final long directoryId = directoryParameter == null |
| 913 | ? Directory.DEFAULT |
| 914 | : Long.parseLong(directoryParameter); |
| 915 | final long contactId = cursor.getLong(ContactQuery.CONTACT_ID); |
| 916 | final String lookupKey = cursor.getString(ContactQuery.LOOKUP_KEY); |
| 917 | final long nameRawContactId = cursor.getLong(ContactQuery.NAME_RAW_CONTACT_ID); |
| 918 | final int displayNameSource = cursor.getInt(ContactQuery.DISPLAY_NAME_SOURCE); |
| 919 | final String displayName = cursor.getString(ContactQuery.DISPLAY_NAME); |
| 920 | final String altDisplayName = cursor.getString(ContactQuery.ALT_DISPLAY_NAME); |
| 921 | final String phoneticName = cursor.getString(ContactQuery.PHONETIC_NAME); |
| 922 | final long photoId = cursor.getLong(ContactQuery.PHOTO_ID); |
| 923 | final String photoUri = cursor.getString(ContactQuery.PHOTO_URI); |
| 924 | final boolean starred = cursor.getInt(ContactQuery.STARRED) != 0; |
| 925 | final Integer presence = cursor.isNull(ContactQuery.CONTACT_PRESENCE) |
| 926 | ? null |
| 927 | : cursor.getInt(ContactQuery.CONTACT_PRESENCE); |
| 928 | final boolean sendToVoicemail = cursor.getInt(ContactQuery.SEND_TO_VOICEMAIL) == 1; |
| 929 | final String customRingtone = cursor.getString(ContactQuery.CUSTOM_RINGTONE); |
| 930 | final boolean isUserProfile = cursor.getInt(ContactQuery.IS_USER_PROFILE) == 1; |
| 931 | |
| 932 | Uri lookupUri; |
| 933 | if (directoryId == Directory.DEFAULT || directoryId == Directory.LOCAL_INVISIBLE) { |
| 934 | lookupUri = ContentUris.withAppendedId( |
| 935 | Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey), contactId); |
| 936 | } else { |
| 937 | lookupUri = contactUri; |
| 938 | } |
| 939 | |
| 940 | return new Result(mRequestedUri, contactUri, lookupUri, directoryId, lookupKey, |
| 941 | contactId, nameRawContactId, displayNameSource, photoId, photoUri, displayName, |
| 942 | altDisplayName, phoneticName, starred, presence, sendToVoicemail, |
| 943 | customRingtone, isUserProfile); |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * Extracts RawContact level columns from the cursor. |
| 948 | */ |
| 949 | private ContentValues loadRawContact(Cursor cursor) { |
| 950 | ContentValues cv = new ContentValues(); |
| 951 | |
| 952 | cv.put(RawContacts._ID, cursor.getLong(ContactQuery.RAW_CONTACT_ID)); |
| 953 | |
| 954 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_NAME); |
| 955 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_TYPE); |
| 956 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SET); |
| 957 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_TYPE_AND_DATA_SET); |
| 958 | cursorColumnToContentValues(cursor, cv, ContactQuery.DIRTY); |
| 959 | cursorColumnToContentValues(cursor, cv, ContactQuery.VERSION); |
| 960 | cursorColumnToContentValues(cursor, cv, ContactQuery.SOURCE_ID); |
| 961 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC1); |
| 962 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC2); |
| 963 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC3); |
| 964 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC4); |
| 965 | cursorColumnToContentValues(cursor, cv, ContactQuery.DELETED); |
| 966 | cursorColumnToContentValues(cursor, cv, ContactQuery.CONTACT_ID); |
| 967 | cursorColumnToContentValues(cursor, cv, ContactQuery.STARRED); |
| 968 | cursorColumnToContentValues(cursor, cv, ContactQuery.NAME_VERIFIED); |
| 969 | |
| 970 | return cv; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * Extracts Data level columns from the cursor. |
| 975 | */ |
| 976 | private ContentValues loadData(Cursor cursor) { |
| 977 | ContentValues cv = new ContentValues(); |
| 978 | |
| 979 | cv.put(Data._ID, cursor.getLong(ContactQuery.DATA_ID)); |
| 980 | |
| 981 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA1); |
| 982 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA2); |
| 983 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA3); |
| 984 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA4); |
| 985 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA5); |
| 986 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA6); |
| 987 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA7); |
| 988 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA8); |
| 989 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA9); |
| 990 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA10); |
| 991 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA11); |
| 992 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA12); |
| 993 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA13); |
| 994 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA14); |
| 995 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA15); |
| 996 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC1); |
| 997 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC2); |
| 998 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC3); |
| 999 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC4); |
| 1000 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_VERSION); |
| 1001 | cursorColumnToContentValues(cursor, cv, ContactQuery.IS_PRIMARY); |
| 1002 | cursorColumnToContentValues(cursor, cv, ContactQuery.IS_SUPERPRIMARY); |
| 1003 | cursorColumnToContentValues(cursor, cv, ContactQuery.MIMETYPE); |
| 1004 | cursorColumnToContentValues(cursor, cv, ContactQuery.RES_PACKAGE); |
| 1005 | cursorColumnToContentValues(cursor, cv, ContactQuery.GROUP_SOURCE_ID); |
| 1006 | cursorColumnToContentValues(cursor, cv, ContactQuery.CHAT_CAPABILITY); |
| 1007 | |
| 1008 | return cv; |
| 1009 | } |
| 1010 | |
| 1011 | private void cursorColumnToContentValues( |
| 1012 | Cursor cursor, ContentValues values, int index) { |
| 1013 | switch (cursor.getType(index)) { |
| 1014 | case Cursor.FIELD_TYPE_NULL: |
| 1015 | // don't put anything in the content values |
| 1016 | break; |
| 1017 | case Cursor.FIELD_TYPE_INTEGER: |
| 1018 | values.put(ContactQuery.COLUMNS[index], cursor.getLong(index)); |
| 1019 | break; |
| 1020 | case Cursor.FIELD_TYPE_STRING: |
| 1021 | values.put(ContactQuery.COLUMNS[index], cursor.getString(index)); |
| 1022 | break; |
| 1023 | case Cursor.FIELD_TYPE_BLOB: |
| 1024 | values.put(ContactQuery.COLUMNS[index], cursor.getBlob(index)); |
| 1025 | break; |
| 1026 | default: |
| 1027 | throw new IllegalStateException("Invalid or unhandled data type"); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | private void loadDirectoryMetaData(Result result) { |
| 1032 | long directoryId = result.getDirectoryId(); |
| 1033 | |
| 1034 | Cursor cursor = getContext().getContentResolver().query( |
| 1035 | ContentUris.withAppendedId(Directory.CONTENT_URI, directoryId), |
| 1036 | DirectoryQuery.COLUMNS, null, null, null); |
| 1037 | if (cursor == null) { |
| 1038 | return; |
| 1039 | } |
| 1040 | try { |
| 1041 | if (cursor.moveToFirst()) { |
| 1042 | final String displayName = cursor.getString(DirectoryQuery.DISPLAY_NAME); |
| 1043 | final String packageName = cursor.getString(DirectoryQuery.PACKAGE_NAME); |
| 1044 | final int typeResourceId = cursor.getInt(DirectoryQuery.TYPE_RESOURCE_ID); |
| 1045 | final String accountType = cursor.getString(DirectoryQuery.ACCOUNT_TYPE); |
| 1046 | final String accountName = cursor.getString(DirectoryQuery.ACCOUNT_NAME); |
| 1047 | final int exportSupport = cursor.getInt(DirectoryQuery.EXPORT_SUPPORT); |
| 1048 | String directoryType = null; |
| 1049 | if (!TextUtils.isEmpty(packageName)) { |
| 1050 | PackageManager pm = getContext().getPackageManager(); |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 1051 | try { |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1052 | Resources resources = pm.getResourcesForApplication(packageName); |
| 1053 | directoryType = resources.getString(typeResourceId); |
| 1054 | } catch (NameNotFoundException e) { |
| 1055 | Log.w(TAG, "Contact directory resource not found: " |
| 1056 | + packageName + "." + typeResourceId); |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1057 | } |
| 1058 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1059 | |
| 1060 | result.setDirectoryMetaData( |
| 1061 | displayName, directoryType, accountType, accountName, exportSupport); |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1062 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1063 | } finally { |
| 1064 | cursor.close(); |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1065 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1066 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1067 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1068 | /** |
| 1069 | * Loads groups meta-data for all groups associated with all constituent raw contacts' |
| 1070 | * accounts. |
| 1071 | */ |
| 1072 | private void loadGroupMetaData(Result result) { |
| 1073 | StringBuilder selection = new StringBuilder(); |
| 1074 | ArrayList<String> selectionArgs = new ArrayList<String>(); |
| 1075 | for (Entity entity : result.mEntities) { |
| 1076 | ContentValues values = entity.getEntityValues(); |
| 1077 | String accountName = values.getAsString(RawContacts.ACCOUNT_NAME); |
| 1078 | String accountType = values.getAsString(RawContacts.ACCOUNT_TYPE); |
| 1079 | String dataSet = values.getAsString(RawContacts.DATA_SET); |
| 1080 | if (accountName != null && accountType != null) { |
| 1081 | if (selection.length() != 0) { |
| 1082 | selection.append(" OR "); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 1083 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1084 | selection.append( |
| 1085 | "(" + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?"); |
| 1086 | selectionArgs.add(accountName); |
| 1087 | selectionArgs.add(accountType); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 1088 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1089 | if (dataSet != null) { |
| 1090 | selection.append(" AND " + Groups.DATA_SET + "=?"); |
| 1091 | selectionArgs.add(dataSet); |
Dave Santoro | a4400d5 | 2011-09-02 16:14:53 -0700 | [diff] [blame] | 1092 | } else { |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1093 | selection.append(" AND " + Groups.DATA_SET + " IS NULL"); |
| 1094 | } |
| 1095 | selection.append(")"); |
| 1096 | } |
| 1097 | } |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 1098 | final ArrayList<GroupMetaData> groupList = new ArrayList<GroupMetaData>(); |
| 1099 | final Cursor cursor = getContext().getContentResolver().query(Groups.CONTENT_URI, |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1100 | GroupQuery.COLUMNS, selection.toString(), selectionArgs.toArray(new String[0]), |
| 1101 | null); |
| 1102 | try { |
| 1103 | while (cursor.moveToNext()) { |
| 1104 | final String accountName = cursor.getString(GroupQuery.ACCOUNT_NAME); |
| 1105 | final String accountType = cursor.getString(GroupQuery.ACCOUNT_TYPE); |
| 1106 | final String dataSet = cursor.getString(GroupQuery.DATA_SET); |
| 1107 | final long groupId = cursor.getLong(GroupQuery.ID); |
| 1108 | final String title = cursor.getString(GroupQuery.TITLE); |
| 1109 | final boolean defaultGroup = cursor.isNull(GroupQuery.AUTO_ADD) |
| 1110 | ? false |
| 1111 | : cursor.getInt(GroupQuery.AUTO_ADD) != 0; |
| 1112 | final boolean favorites = cursor.isNull(GroupQuery.FAVORITES) |
| 1113 | ? false |
| 1114 | : cursor.getInt(GroupQuery.FAVORITES) != 0; |
| 1115 | |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 1116 | groupList.add(new GroupMetaData( |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1117 | accountName, accountType, dataSet, groupId, title, defaultGroup, |
| 1118 | favorites)); |
| 1119 | } |
| 1120 | } finally { |
| 1121 | cursor.close(); |
| 1122 | } |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 1123 | result.mGroups = groupList; |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * Loads all stream items and stream item photos belonging to this contact. |
| 1128 | */ |
| 1129 | private void loadStreamItems(Result result) { |
| 1130 | Cursor cursor = getContext().getContentResolver().query( |
| 1131 | Contacts.CONTENT_LOOKUP_URI.buildUpon() |
| 1132 | .appendPath(result.getLookupKey()) |
| 1133 | .appendPath(Contacts.StreamItems.CONTENT_DIRECTORY).build(), |
| 1134 | null, null, null, null); |
| 1135 | LongSparseArray<StreamItemEntry> streamItemsById = |
| 1136 | new LongSparseArray<StreamItemEntry>(); |
| 1137 | ArrayList<StreamItemEntry> streamItems = new ArrayList<StreamItemEntry>(); |
| 1138 | try { |
| 1139 | while (cursor.moveToNext()) { |
| 1140 | StreamItemEntry streamItem = new StreamItemEntry(cursor); |
| 1141 | streamItemsById.put(streamItem.getId(), streamItem); |
| 1142 | streamItems.add(streamItem); |
| 1143 | } |
| 1144 | } finally { |
| 1145 | cursor.close(); |
| 1146 | } |
| 1147 | |
| 1148 | // Pre-decode all HTMLs |
| 1149 | final long start = System.currentTimeMillis(); |
| 1150 | for (StreamItemEntry streamItem : streamItems) { |
| 1151 | streamItem.decodeHtml(getContext()); |
| 1152 | } |
| 1153 | final long end = System.currentTimeMillis(); |
| 1154 | if (DEBUG) { |
| 1155 | Log.d(TAG, "Decoded HTML for " + streamItems.size() + " items, took " |
| 1156 | + (end - start) + " ms"); |
| 1157 | } |
| 1158 | |
| 1159 | // Now retrieve any photo records associated with the stream items. |
| 1160 | if (!streamItems.isEmpty()) { |
| 1161 | if (result.isUserProfile()) { |
| 1162 | // If the stream items we're loading are for the profile, we can't bulk-load the |
| 1163 | // stream items with a custom selection. |
| 1164 | for (StreamItemEntry entry : streamItems) { |
| 1165 | Cursor siCursor = getContext().getContentResolver().query( |
| 1166 | Uri.withAppendedPath( |
| 1167 | ContentUris.withAppendedId( |
| 1168 | StreamItems.CONTENT_URI, entry.getId()), |
| 1169 | StreamItems.StreamItemPhotos.CONTENT_DIRECTORY), |
| 1170 | null, null, null, null); |
Dave Santoro | a4400d5 | 2011-09-02 16:14:53 -0700 | [diff] [blame] | 1171 | try { |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1172 | while (siCursor.moveToNext()) { |
| 1173 | entry.addPhoto(new StreamItemPhotoEntry(siCursor)); |
Dave Santoro | a4400d5 | 2011-09-02 16:14:53 -0700 | [diff] [blame] | 1174 | } |
| 1175 | } finally { |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1176 | siCursor.close(); |
Dave Santoro | a4400d5 | 2011-09-02 16:14:53 -0700 | [diff] [blame] | 1177 | } |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1178 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1179 | } else { |
| 1180 | String[] streamItemIdArr = new String[streamItems.size()]; |
| 1181 | StringBuilder streamItemPhotoSelection = new StringBuilder(); |
| 1182 | streamItemPhotoSelection.append(StreamItemPhotos.STREAM_ITEM_ID + " IN ("); |
| 1183 | for (int i = 0; i < streamItems.size(); i++) { |
| 1184 | if (i > 0) { |
| 1185 | streamItemPhotoSelection.append(","); |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1186 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1187 | streamItemPhotoSelection.append("?"); |
| 1188 | streamItemIdArr[i] = String.valueOf(streamItems.get(i).getId()); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1189 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1190 | streamItemPhotoSelection.append(")"); |
| 1191 | Cursor sipCursor = getContext().getContentResolver().query( |
| 1192 | StreamItems.CONTENT_PHOTO_URI, |
| 1193 | null, streamItemPhotoSelection.toString(), streamItemIdArr, |
| 1194 | StreamItemPhotos.STREAM_ITEM_ID); |
| 1195 | try { |
| 1196 | while (sipCursor.moveToNext()) { |
| 1197 | long streamItemId = sipCursor.getLong( |
| 1198 | sipCursor.getColumnIndex(StreamItemPhotos.STREAM_ITEM_ID)); |
| 1199 | StreamItemEntry streamItem = streamItemsById.get(streamItemId); |
| 1200 | streamItem.addPhoto(new StreamItemPhotoEntry(sipCursor)); |
| 1201 | } |
| 1202 | } finally { |
| 1203 | sipCursor.close(); |
| 1204 | } |
| 1205 | } |
| 1206 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1207 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1208 | // Set the sorted stream items on the result. |
| 1209 | Collections.sort(streamItems); |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 1210 | result.mStreamItems = streamItems; |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | @Override |
| 1214 | public void deliverResult(Result result) { |
| 1215 | unregisterObserver(); |
| 1216 | |
| 1217 | // The creator isn't interested in any further updates |
| 1218 | if (isReset() || result == null) { |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | mContact = result; |
| 1223 | |
| 1224 | if (result.isLoaded()) { |
| 1225 | mLookupUri = result.getLookupUri(); |
| 1226 | |
| 1227 | if (!result.isDirectoryEntry()) { |
| 1228 | Log.i(TAG, "Registering content observer for " + mLookupUri); |
| 1229 | if (mObserver == null) { |
| 1230 | mObserver = new ForceLoadContentObserver(); |
| 1231 | } |
| 1232 | getContext().getContentResolver().registerContentObserver( |
| 1233 | mLookupUri, true, mObserver); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1234 | } |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1235 | |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1236 | // inform the source of the data that this contact is being looked at |
| 1237 | postViewNotificationToSyncAdapter(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1238 | } |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1239 | |
| 1240 | super.deliverResult(mContact); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1243 | /** |
| 1244 | * Posts a message to the contributing sync adapters that have opted-in, notifying them |
| 1245 | * that the contact has just been loaded |
| 1246 | */ |
| 1247 | private void postViewNotificationToSyncAdapter() { |
| 1248 | Context context = getContext(); |
| 1249 | for (Entity entity : mContact.getEntities()) { |
| 1250 | final ContentValues entityValues = entity.getEntityValues(); |
Makoto Onuki | aba2b83 | 2011-08-12 15:44:53 -0700 | [diff] [blame] | 1251 | final long rawContactId = entityValues.getAsLong(RawContacts.Entity._ID); |
| 1252 | if (mNotifiedRawContactIds.contains(rawContactId)) { |
| 1253 | continue; // Already notified for this raw contact. |
| 1254 | } |
| 1255 | mNotifiedRawContactIds.add(rawContactId); |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1256 | final String type = entityValues.getAsString(RawContacts.ACCOUNT_TYPE); |
| 1257 | final String dataSet = entityValues.getAsString(RawContacts.DATA_SET); |
Flavio Lerda | 59a887e | 2011-08-14 18:13:17 +0100 | [diff] [blame] | 1258 | final AccountType accountType = AccountTypeManager.getInstance(context).getAccountType( |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1259 | type, dataSet); |
| 1260 | final String serviceName = accountType.getViewContactNotifyServiceClassName(); |
| 1261 | final String resPackageName = accountType.resPackageName; |
| 1262 | if (!TextUtils.isEmpty(serviceName) && !TextUtils.isEmpty(resPackageName)) { |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1263 | final Uri uri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId); |
| 1264 | final Intent intent = new Intent(); |
| 1265 | intent.setClassName(resPackageName, serviceName); |
| 1266 | intent.setAction(Intent.ACTION_VIEW); |
| 1267 | intent.setDataAndType(uri, RawContacts.CONTENT_ITEM_TYPE); |
| 1268 | try { |
| 1269 | context.startService(intent); |
| 1270 | } catch (Exception e) { |
| 1271 | Log.e(TAG, "Error sending message to source-app", e); |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
Daniel Lehmann | 3a12077 | 2010-06-21 16:21:35 -0700 | [diff] [blame] | 1277 | private void unregisterObserver() { |
| 1278 | if (mObserver != null) { |
| 1279 | getContext().getContentResolver().unregisterContentObserver(mObserver); |
| 1280 | mObserver = null; |
| 1281 | } |
| 1282 | } |
| 1283 | |
Daniel Lehmann | 2a45e35 | 2012-02-13 15:03:58 -0800 | [diff] [blame] | 1284 | /** |
| 1285 | * Sets whether to load stream items. Will trigger a reload if the value has changed. |
| 1286 | * At the moment, this is only used for debugging purposes |
| 1287 | */ |
| 1288 | public void setLoadStreamItems(boolean value) { |
| 1289 | if (mLoadStreamItems != value) { |
| 1290 | mLoadStreamItems = value; |
| 1291 | onContentChanged(); |
| 1292 | } |
| 1293 | } |
| 1294 | |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 1295 | /** |
| 1296 | * Fully upgrades this ContactLoader to one with all lists fully loaded. When done, the |
| 1297 | * new result will be delivered |
| 1298 | */ |
| 1299 | public void upgradeToFullContact() { |
| 1300 | mLoadGroupMetaData = true; |
| 1301 | mLoadInvitableAccountTypes = true; |
| 1302 | mLoadStreamItems = true; |
| 1303 | |
| 1304 | // Cache the current result, so that we only load the "missing" parts of the contact. |
| 1305 | cacheResult(); |
| 1306 | |
| 1307 | // Our load parameters have changed, so let's pretend the data has changed. Its the same |
| 1308 | // thing, essentially. |
| 1309 | onContentChanged(); |
| 1310 | } |
| 1311 | |
Daniel Lehmann | 2a45e35 | 2012-02-13 15:03:58 -0800 | [diff] [blame] | 1312 | public boolean getLoadStreamItems() { |
| 1313 | return mLoadStreamItems; |
| 1314 | } |
| 1315 | |
Dmitri Plotnikov | 5a30d9a | 2010-11-23 14:59:50 -0800 | [diff] [blame] | 1316 | public Uri getLookupUri() { |
| 1317 | return mLookupUri; |
| 1318 | } |
| 1319 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1320 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1321 | protected void onStartLoading() { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1322 | if (mContact != null) { |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 1323 | deliverResult(mContact); |
Dmitri Plotnikov | 97e90c6 | 2011-01-03 11:58:13 -0800 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | if (takeContentChanged() || mContact == null) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1327 | forceLoad(); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | @Override |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1332 | protected void onStopLoading() { |
| 1333 | cancelLoad(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1337 | protected void onReset() { |
Daniel Lehmann | 72ff4df | 2012-02-28 20:03:01 -0800 | [diff] [blame] | 1338 | super.onReset(); |
| 1339 | cancelLoad(); |
Dianne Hackborn | 4ef95cc | 2010-12-16 00:44:33 -0800 | [diff] [blame] | 1340 | unregisterObserver(); |
| 1341 | mContact = null; |
Dianne Hackborn | 4ef95cc | 2010-12-16 00:44:33 -0800 | [diff] [blame] | 1342 | } |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 1343 | |
| 1344 | /** |
| 1345 | * Caches the result, which is useful when we switch from activity to activity, using the same |
| 1346 | * contact. If the next load is for a different contact, the cached result will be dropped |
| 1347 | */ |
| 1348 | public void cacheResult() { |
| 1349 | sCachedResult = mContact; |
| 1350 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1351 | } |