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; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 21 | import com.android.contacts.util.DataStatus; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 22 | import com.android.contacts.util.StreamItemEntry; |
| 23 | import com.android.contacts.util.StreamItemPhotoEntry; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 24 | import com.google.android.collect.Lists; |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 25 | import com.google.common.annotations.VisibleForTesting; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 26 | |
| 27 | import android.content.ContentResolver; |
| 28 | import android.content.ContentUris; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 29 | import android.content.ContentValues; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.content.Entity; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 32 | import android.content.Entity.NamedContentValues; |
Jeff Hamilton | 3c46291 | 2010-05-15 02:20:01 -0500 | [diff] [blame] | 33 | import android.content.Loader; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.pm.PackageManager.NameNotFoundException; |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 36 | import android.content.res.AssetFileDescriptor; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 37 | import android.content.res.Resources; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 38 | import android.database.Cursor; |
| 39 | import android.net.Uri; |
| 40 | import android.os.AsyncTask; |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 41 | import android.provider.ContactsContract; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 42 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 43 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 44 | import android.provider.ContactsContract.Contacts; |
| 45 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 46 | import android.provider.ContactsContract.Directory; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 47 | import android.provider.ContactsContract.DisplayNameSources; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 48 | import android.provider.ContactsContract.Groups; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 49 | import android.provider.ContactsContract.RawContacts; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 50 | import android.provider.ContactsContract.StreamItemPhotos; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 51 | import android.provider.ContactsContract.StreamItems; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 52 | import android.text.TextUtils; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 53 | import android.util.Log; |
| 54 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 55 | import java.io.ByteArrayOutputStream; |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 56 | import java.io.FileInputStream; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 57 | import java.io.IOException; |
| 58 | import java.io.InputStream; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 59 | import java.util.ArrayList; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 60 | import java.util.Collections; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 61 | import java.util.HashMap; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 62 | import java.util.List; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 63 | import java.util.Map; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Loads a single Contact and all it constituent RawContacts. |
| 67 | */ |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 68 | public class ContactLoader extends Loader<ContactLoader.Result> { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 69 | private static final String TAG = "ContactLoader"; |
| 70 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 71 | private Uri mLookupUri; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 72 | private boolean mLoadGroupMetaData; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 73 | private boolean mLoadStreamItems; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 74 | private final boolean mLoadInvitableAccountTypes; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 75 | private Result mContact; |
| 76 | private ForceLoadContentObserver mObserver; |
| 77 | private boolean mDestroyed; |
| 78 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 79 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 80 | public interface Listener { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 81 | public void onContactLoaded(Result contact); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * The result of a load operation. Contains all data necessary to display the contact. |
| 86 | */ |
| 87 | public static final class Result { |
| 88 | /** |
| 89 | * Singleton instance that represents "No Contact Found" |
| 90 | */ |
| 91 | public static final Result NOT_FOUND = new Result(); |
| 92 | |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Singleton instance that represents an error, e.g. because of an invalid Uri |
| 95 | * TODO: We should come up with something nicer here. Maybe use an Either type so |
| 96 | * that we can capture the Exception? |
| 97 | */ |
| 98 | public static final Result ERROR = new Result(); |
| 99 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 100 | private final Uri mLookupUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 101 | private final Uri mUri; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 102 | private final long mDirectoryId; |
| 103 | private final String mLookupKey; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 104 | private final long mId; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 105 | private final long mNameRawContactId; |
| 106 | private final int mDisplayNameSource; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 107 | private final long mPhotoId; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 108 | private final String mPhotoUri; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 109 | private final String mDisplayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 110 | private final String mAltDisplayName; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 111 | private final String mPhoneticName; |
| 112 | private final boolean mStarred; |
| 113 | private final Integer mPresence; |
| 114 | private final ArrayList<Entity> mEntities; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 115 | private ArrayList<StreamItemEntry> mStreamItems; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 116 | private final HashMap<Long, DataStatus> mStatuses; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 117 | private final ArrayList<AccountType> mInvitableAccountTypes; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 118 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 119 | private String mDirectoryDisplayName; |
| 120 | private String mDirectoryType; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 121 | private String mDirectoryAccountType; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 122 | private String mDirectoryAccountName; |
| 123 | private int mDirectoryExportSupport; |
| 124 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 125 | private ArrayList<GroupMetaData> mGroups; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 126 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 127 | private boolean mLoadingPhoto; |
| 128 | private byte[] mPhotoBinaryData; |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 129 | private boolean mSendToVoicemail; |
| 130 | private String mCustomRingtone; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 131 | private boolean mIsUserProfile; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 132 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Constructor for case "no contact found". This must only be used for the |
| 135 | * final {@link Result#NOT_FOUND} singleton |
| 136 | */ |
| 137 | private Result() { |
| 138 | mLookupUri = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 139 | mUri = null; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 140 | mDirectoryId = -1; |
| 141 | mLookupKey = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 142 | mId = -1; |
| 143 | mEntities = null; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 144 | mStreamItems = new ArrayList<StreamItemEntry>(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 145 | mStatuses = null; |
| 146 | mNameRawContactId = -1; |
| 147 | mDisplayNameSource = DisplayNameSources.UNDEFINED; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 148 | mPhotoId = -1; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 149 | mPhotoUri = null; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 150 | mDisplayName = null; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 151 | mAltDisplayName = null; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 152 | mPhoneticName = null; |
| 153 | mStarred = false; |
| 154 | mPresence = null; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 155 | mInvitableAccountTypes = null; |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 156 | mSendToVoicemail = false; |
| 157 | mCustomRingtone = null; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 158 | mIsUserProfile = false; |
| 159 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Constructor to call when contact was found |
| 164 | */ |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 165 | private Result(Uri uri, Uri lookupUri, long directoryId, String lookupKey, long id, |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 166 | long nameRawContactId, int displayNameSource, long photoId, String photoUri, |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 167 | String displayName, String altDisplayName, String phoneticName, boolean starred, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 168 | Integer presence, boolean sendToVoicemail, String customRingtone, |
| 169 | boolean isUserProfile) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 170 | mLookupUri = lookupUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 171 | mUri = uri; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 172 | mDirectoryId = directoryId; |
| 173 | mLookupKey = lookupKey; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 174 | mId = id; |
| 175 | mEntities = new ArrayList<Entity>(); |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 176 | mStreamItems = new ArrayList<StreamItemEntry>(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 177 | mStatuses = new HashMap<Long, DataStatus>(); |
| 178 | mNameRawContactId = nameRawContactId; |
| 179 | mDisplayNameSource = displayNameSource; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 180 | mPhotoId = photoId; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 181 | mPhotoUri = photoUri; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 182 | mDisplayName = displayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 183 | mAltDisplayName = altDisplayName; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 184 | mPhoneticName = phoneticName; |
| 185 | mStarred = starred; |
| 186 | mPresence = presence; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 187 | mInvitableAccountTypes = Lists.newArrayList(); |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 188 | mSendToVoicemail = sendToVoicemail; |
| 189 | mCustomRingtone = customRingtone; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 190 | mIsUserProfile = isUserProfile; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 193 | private Result(Result from) { |
| 194 | mLookupUri = from.mLookupUri; |
| 195 | mUri = from.mUri; |
| 196 | mDirectoryId = from.mDirectoryId; |
| 197 | mLookupKey = from.mLookupKey; |
| 198 | mId = from.mId; |
| 199 | mNameRawContactId = from.mNameRawContactId; |
| 200 | mDisplayNameSource = from.mDisplayNameSource; |
| 201 | mPhotoId = from.mPhotoId; |
| 202 | mPhotoUri = from.mPhotoUri; |
| 203 | mDisplayName = from.mDisplayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 204 | mAltDisplayName = from.mAltDisplayName; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 205 | mPhoneticName = from.mPhoneticName; |
| 206 | mStarred = from.mStarred; |
| 207 | mPresence = from.mPresence; |
| 208 | mEntities = from.mEntities; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 209 | mStreamItems = from.mStreamItems; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 210 | mStatuses = from.mStatuses; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 211 | mInvitableAccountTypes = from.mInvitableAccountTypes; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 212 | |
| 213 | mDirectoryDisplayName = from.mDirectoryDisplayName; |
| 214 | mDirectoryType = from.mDirectoryType; |
| 215 | mDirectoryAccountType = from.mDirectoryAccountType; |
| 216 | mDirectoryAccountName = from.mDirectoryAccountName; |
| 217 | mDirectoryExportSupport = from.mDirectoryExportSupport; |
| 218 | |
| 219 | mGroups = from.mGroups; |
| 220 | |
| 221 | mLoadingPhoto = from.mLoadingPhoto; |
| 222 | mPhotoBinaryData = from.mPhotoBinaryData; |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 223 | mSendToVoicemail = from.mSendToVoicemail; |
| 224 | mCustomRingtone = from.mCustomRingtone; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 225 | mIsUserProfile = from.mIsUserProfile; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 228 | /** |
| 229 | * @param exportSupport See {@link Directory#EXPORT_SUPPORT}. |
| 230 | */ |
Daniel Lehmann | 1ad4d1b | 2010-10-18 19:20:41 -0700 | [diff] [blame] | 231 | private void setDirectoryMetaData(String displayName, String directoryType, |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 232 | String accountType, String accountName, int exportSupport) { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 233 | mDirectoryDisplayName = displayName; |
| 234 | mDirectoryType = directoryType; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 235 | mDirectoryAccountType = accountType; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 236 | mDirectoryAccountName = accountName; |
| 237 | mDirectoryExportSupport = exportSupport; |
| 238 | } |
| 239 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 240 | private void setLoadingPhoto(boolean flag) { |
| 241 | mLoadingPhoto = flag; |
| 242 | } |
| 243 | |
| 244 | private void setPhotoBinaryData(byte[] photoBinaryData) { |
| 245 | mPhotoBinaryData = photoBinaryData; |
| 246 | } |
| 247 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 248 | public Uri getLookupUri() { |
| 249 | return mLookupUri; |
| 250 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 251 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 252 | public String getLookupKey() { |
| 253 | return mLookupKey; |
| 254 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 255 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 256 | public Uri getUri() { |
| 257 | return mUri; |
| 258 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 259 | |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 260 | @VisibleForTesting |
| 261 | /*package*/ long getId() { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 262 | return mId; |
| 263 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 264 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 265 | public long getNameRawContactId() { |
| 266 | return mNameRawContactId; |
| 267 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 268 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 269 | public int getDisplayNameSource() { |
| 270 | return mDisplayNameSource; |
| 271 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 272 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 273 | public long getPhotoId() { |
| 274 | return mPhotoId; |
| 275 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 276 | |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 277 | public String getPhotoUri() { |
| 278 | return mPhotoUri; |
| 279 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 280 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 281 | public String getDisplayName() { |
| 282 | return mDisplayName; |
| 283 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 284 | |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 285 | public String getAltDisplayName() { |
| 286 | return mAltDisplayName; |
| 287 | } |
| 288 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 289 | public String getPhoneticName() { |
| 290 | return mPhoneticName; |
| 291 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 292 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 293 | public boolean getStarred() { |
| 294 | return mStarred; |
| 295 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 296 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 297 | public Integer getPresence() { |
| 298 | return mPresence; |
| 299 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 300 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 301 | public ArrayList<AccountType> getInvitableAccountTypes() { |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 302 | return mInvitableAccountTypes; |
| 303 | } |
| 304 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 305 | public ArrayList<Entity> getEntities() { |
| 306 | return mEntities; |
| 307 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 308 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 309 | public ArrayList<StreamItemEntry> getStreamItems() { |
| 310 | return mStreamItems; |
| 311 | } |
| 312 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 313 | public HashMap<Long, DataStatus> getStatuses() { |
| 314 | return mStatuses; |
| 315 | } |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 316 | |
| 317 | public long getDirectoryId() { |
| 318 | return mDirectoryId; |
| 319 | } |
| 320 | |
| 321 | public boolean isDirectoryEntry() { |
Dmitri Plotnikov | 5f72c1f | 2010-09-01 21:21:04 -0700 | [diff] [blame] | 322 | return mDirectoryId != -1 && mDirectoryId != Directory.DEFAULT |
| 323 | && mDirectoryId != Directory.LOCAL_INVISIBLE; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | public int getDirectoryExportSupport() { |
| 327 | return mDirectoryExportSupport; |
| 328 | } |
| 329 | |
| 330 | public String getDirectoryDisplayName() { |
| 331 | return mDirectoryDisplayName; |
| 332 | } |
| 333 | |
| 334 | public String getDirectoryType() { |
| 335 | return mDirectoryType; |
| 336 | } |
| 337 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 338 | public String getDirectoryAccountType() { |
| 339 | return mDirectoryAccountType; |
| 340 | } |
| 341 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 342 | public String getDirectoryAccountName() { |
| 343 | return mDirectoryAccountName; |
| 344 | } |
| 345 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 346 | public boolean isLoadingPhoto() { |
| 347 | return mLoadingPhoto; |
| 348 | } |
| 349 | |
| 350 | public byte[] getPhotoBinaryData() { |
| 351 | return mPhotoBinaryData; |
| 352 | } |
| 353 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 354 | public ArrayList<ContentValues> getContentValues() { |
| 355 | if (mEntities.size() != 1) { |
| 356 | throw new IllegalStateException( |
| 357 | "Cannot extract content values from an aggregated contact"); |
| 358 | } |
| 359 | |
| 360 | Entity entity = mEntities.get(0); |
| 361 | ArrayList<ContentValues> result = new ArrayList<ContentValues>(); |
| 362 | ArrayList<NamedContentValues> subValues = entity.getSubValues(); |
| 363 | if (subValues != null) { |
| 364 | int size = subValues.size(); |
| 365 | for (int i = 0; i < size; i++) { |
| 366 | NamedContentValues pair = subValues.get(i); |
| 367 | if (Data.CONTENT_URI.equals(pair.uri)) { |
| 368 | result.add(pair.values); |
| 369 | } |
| 370 | } |
| 371 | } |
Dmitri Plotnikov | 40ec3a8 | 2010-11-10 11:25:33 -0800 | [diff] [blame] | 372 | |
| 373 | // If the photo was loaded using the URI, create an entry for the photo |
| 374 | // binary data. |
| 375 | if (mPhotoId == 0 && mPhotoBinaryData != null) { |
| 376 | ContentValues photo = new ContentValues(); |
| 377 | photo.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE); |
| 378 | photo.put(Photo.PHOTO, mPhotoBinaryData); |
| 379 | result.add(photo); |
| 380 | } |
| 381 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 382 | return result; |
| 383 | } |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 384 | |
Daniel Lehmann | 1ad4d1b | 2010-10-18 19:20:41 -0700 | [diff] [blame] | 385 | private void addGroupMetaData(GroupMetaData group) { |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 386 | if (mGroups == null) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 387 | mGroups = new ArrayList<GroupMetaData>(); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 388 | } |
| 389 | mGroups.add(group); |
| 390 | } |
| 391 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 392 | public List<GroupMetaData> getGroupMetaData() { |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 393 | return mGroups; |
| 394 | } |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 395 | |
| 396 | public boolean isSendToVoicemail() { |
| 397 | return mSendToVoicemail; |
| 398 | } |
| 399 | |
| 400 | public String getCustomRingtone() { |
| 401 | return mCustomRingtone; |
| 402 | } |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 403 | |
| 404 | public boolean isUserProfile() { |
| 405 | return mIsUserProfile; |
| 406 | } |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 409 | /** |
| 410 | * Projection used for the query that loads all data for the entire contact (except for |
| 411 | * social stream items). |
| 412 | */ |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 413 | private static class ContactQuery { |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 414 | final static String[] COLUMNS = new String[] { |
| 415 | Contacts.NAME_RAW_CONTACT_ID, |
| 416 | Contacts.DISPLAY_NAME_SOURCE, |
| 417 | Contacts.LOOKUP_KEY, |
| 418 | Contacts.DISPLAY_NAME, |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 419 | Contacts.DISPLAY_NAME_ALTERNATIVE, |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 420 | Contacts.PHONETIC_NAME, |
| 421 | Contacts.PHOTO_ID, |
| 422 | Contacts.STARRED, |
| 423 | Contacts.CONTACT_PRESENCE, |
| 424 | Contacts.CONTACT_STATUS, |
| 425 | Contacts.CONTACT_STATUS_TIMESTAMP, |
| 426 | Contacts.CONTACT_STATUS_RES_PACKAGE, |
| 427 | Contacts.CONTACT_STATUS_LABEL, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 428 | Contacts.Entity.CONTACT_ID, |
| 429 | Contacts.Entity.RAW_CONTACT_ID, |
| 430 | |
| 431 | RawContacts.ACCOUNT_NAME, |
| 432 | RawContacts.ACCOUNT_TYPE, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 433 | RawContacts.DATA_SET, |
| 434 | RawContacts.ACCOUNT_TYPE_AND_DATA_SET, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 435 | RawContacts.DIRTY, |
| 436 | RawContacts.VERSION, |
| 437 | RawContacts.SOURCE_ID, |
| 438 | RawContacts.SYNC1, |
| 439 | RawContacts.SYNC2, |
| 440 | RawContacts.SYNC3, |
| 441 | RawContacts.SYNC4, |
| 442 | RawContacts.DELETED, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 443 | RawContacts.NAME_VERIFIED, |
| 444 | |
| 445 | Contacts.Entity.DATA_ID, |
| 446 | Data.DATA1, |
| 447 | Data.DATA2, |
| 448 | Data.DATA3, |
| 449 | Data.DATA4, |
| 450 | Data.DATA5, |
| 451 | Data.DATA6, |
| 452 | Data.DATA7, |
| 453 | Data.DATA8, |
| 454 | Data.DATA9, |
| 455 | Data.DATA10, |
| 456 | Data.DATA11, |
| 457 | Data.DATA12, |
| 458 | Data.DATA13, |
| 459 | Data.DATA14, |
| 460 | Data.DATA15, |
| 461 | Data.SYNC1, |
| 462 | Data.SYNC2, |
| 463 | Data.SYNC3, |
| 464 | Data.SYNC4, |
| 465 | Data.DATA_VERSION, |
| 466 | Data.IS_PRIMARY, |
| 467 | Data.IS_SUPER_PRIMARY, |
| 468 | Data.MIMETYPE, |
| 469 | Data.RES_PACKAGE, |
| 470 | |
| 471 | GroupMembership.GROUP_SOURCE_ID, |
| 472 | |
| 473 | Data.PRESENCE, |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 474 | Data.CHAT_CAPABILITY, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 475 | Data.STATUS, |
| 476 | Data.STATUS_RES_PACKAGE, |
| 477 | Data.STATUS_ICON, |
| 478 | Data.STATUS_LABEL, |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 479 | Data.STATUS_TIMESTAMP, |
| 480 | |
| 481 | Contacts.PHOTO_URI, |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 482 | Contacts.SEND_TO_VOICEMAIL, |
| 483 | Contacts.CUSTOM_RINGTONE, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 484 | Contacts.IS_USER_PROFILE, |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 485 | }; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 486 | |
| 487 | public final static int NAME_RAW_CONTACT_ID = 0; |
| 488 | public final static int DISPLAY_NAME_SOURCE = 1; |
| 489 | public final static int LOOKUP_KEY = 2; |
| 490 | public final static int DISPLAY_NAME = 3; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 491 | public final static int ALT_DISPLAY_NAME = 4; |
| 492 | public final static int PHONETIC_NAME = 5; |
| 493 | public final static int PHOTO_ID = 6; |
| 494 | public final static int STARRED = 7; |
| 495 | public final static int CONTACT_PRESENCE = 8; |
| 496 | public final static int CONTACT_STATUS = 9; |
| 497 | public final static int CONTACT_STATUS_TIMESTAMP = 10; |
| 498 | public final static int CONTACT_STATUS_RES_PACKAGE = 11; |
| 499 | public final static int CONTACT_STATUS_LABEL = 12; |
| 500 | public final static int CONTACT_ID = 13; |
| 501 | public final static int RAW_CONTACT_ID = 14; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 502 | |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 503 | public final static int ACCOUNT_NAME = 15; |
| 504 | public final static int ACCOUNT_TYPE = 16; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 505 | public final static int DATA_SET = 17; |
| 506 | public final static int ACCOUNT_TYPE_AND_DATA_SET = 18; |
| 507 | public final static int DIRTY = 19; |
| 508 | public final static int VERSION = 20; |
| 509 | public final static int SOURCE_ID = 21; |
| 510 | public final static int SYNC1 = 22; |
| 511 | public final static int SYNC2 = 23; |
| 512 | public final static int SYNC3 = 24; |
| 513 | public final static int SYNC4 = 25; |
| 514 | public final static int DELETED = 26; |
| 515 | public final static int NAME_VERIFIED = 27; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 516 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 517 | public final static int DATA_ID = 28; |
| 518 | public final static int DATA1 = 29; |
| 519 | public final static int DATA2 = 30; |
| 520 | public final static int DATA3 = 31; |
| 521 | public final static int DATA4 = 32; |
| 522 | public final static int DATA5 = 33; |
| 523 | public final static int DATA6 = 34; |
| 524 | public final static int DATA7 = 35; |
| 525 | public final static int DATA8 = 36; |
| 526 | public final static int DATA9 = 37; |
| 527 | public final static int DATA10 = 38; |
| 528 | public final static int DATA11 = 39; |
| 529 | public final static int DATA12 = 40; |
| 530 | public final static int DATA13 = 41; |
| 531 | public final static int DATA14 = 42; |
| 532 | public final static int DATA15 = 43; |
| 533 | public final static int DATA_SYNC1 = 44; |
| 534 | public final static int DATA_SYNC2 = 45; |
| 535 | public final static int DATA_SYNC3 = 46; |
| 536 | public final static int DATA_SYNC4 = 47; |
| 537 | public final static int DATA_VERSION = 48; |
| 538 | public final static int IS_PRIMARY = 49; |
| 539 | public final static int IS_SUPERPRIMARY = 50; |
| 540 | public final static int MIMETYPE = 51; |
| 541 | public final static int RES_PACKAGE = 52; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 542 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 543 | public final static int GROUP_SOURCE_ID = 53; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 544 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 545 | public final static int PRESENCE = 54; |
| 546 | public final static int CHAT_CAPABILITY = 55; |
| 547 | public final static int STATUS = 56; |
| 548 | public final static int STATUS_RES_PACKAGE = 57; |
| 549 | public final static int STATUS_ICON = 58; |
| 550 | public final static int STATUS_LABEL = 59; |
| 551 | public final static int STATUS_TIMESTAMP = 60; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 552 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 553 | public final static int PHOTO_URI = 61; |
| 554 | public final static int SEND_TO_VOICEMAIL = 62; |
| 555 | public final static int CUSTOM_RINGTONE = 63; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 556 | public final static int IS_USER_PROFILE = 64; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 557 | } |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 558 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 559 | /** |
| 560 | * Projection used for the query that loads all data for the entire contact. |
| 561 | */ |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 562 | private static class DirectoryQuery { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 563 | final static String[] COLUMNS = new String[] { |
| 564 | Directory.DISPLAY_NAME, |
| 565 | Directory.PACKAGE_NAME, |
| 566 | Directory.TYPE_RESOURCE_ID, |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 567 | Directory.ACCOUNT_TYPE, |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 568 | Directory.ACCOUNT_NAME, |
| 569 | Directory.EXPORT_SUPPORT, |
| 570 | }; |
| 571 | |
| 572 | public final static int DISPLAY_NAME = 0; |
| 573 | public final static int PACKAGE_NAME = 1; |
| 574 | public final static int TYPE_RESOURCE_ID = 2; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 575 | public final static int ACCOUNT_TYPE = 3; |
| 576 | public final static int ACCOUNT_NAME = 4; |
| 577 | public final static int EXPORT_SUPPORT = 5; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 580 | private static class GroupQuery { |
| 581 | final static String[] COLUMNS = new String[] { |
| 582 | Groups.ACCOUNT_NAME, |
| 583 | Groups.ACCOUNT_TYPE, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 584 | Groups.DATA_SET, |
| 585 | Groups.ACCOUNT_TYPE_AND_DATA_SET, |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 586 | Groups._ID, |
| 587 | Groups.TITLE, |
| 588 | Groups.AUTO_ADD, |
| 589 | Groups.FAVORITES, |
| 590 | }; |
| 591 | |
| 592 | public final static int ACCOUNT_NAME = 0; |
| 593 | public final static int ACCOUNT_TYPE = 1; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 594 | public final static int DATA_SET = 2; |
| 595 | public final static int ACCOUNT_TYPE_AND_DATA_SET = 3; |
| 596 | public final static int ID = 4; |
| 597 | public final static int TITLE = 5; |
| 598 | public final static int AUTO_ADD = 6; |
| 599 | public final static int FAVORITES = 7; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 602 | private final class LoadContactTask extends AsyncTask<Void, Void, Result> { |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 603 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 604 | @Override |
| 605 | protected Result doInBackground(Void... args) { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 606 | try { |
| 607 | final ContentResolver resolver = getContext().getContentResolver(); |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 608 | final Uri uriCurrentFormat = ensureIsContactUri(resolver, mLookupUri); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 609 | Result result = loadContactEntity(resolver, uriCurrentFormat); |
Dmitri Plotnikov | 217245c | 2010-09-18 13:04:50 -0700 | [diff] [blame] | 610 | if (result != Result.NOT_FOUND) { |
| 611 | if (result.isDirectoryEntry()) { |
| 612 | loadDirectoryMetaData(result); |
| 613 | } else if (mLoadGroupMetaData) { |
| 614 | loadGroupMetaData(result); |
| 615 | } |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 616 | if (mLoadStreamItems) { |
| 617 | loadStreamItems(result); |
| 618 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 619 | loadPhotoBinaryData(result); |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 620 | if (mLoadInvitableAccountTypes) { |
| 621 | loadInvitableAccountTypes(result); |
| 622 | } |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 623 | } |
| 624 | return result; |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 625 | } catch (Exception e) { |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 626 | Log.e(TAG, "Error loading the contact: " + mLookupUri, e); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 627 | return Result.ERROR; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 628 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 632 | * Transforms the given Uri and returns a Lookup-Uri that represents the contact. |
| 633 | * For legacy contacts, a raw-contact lookup is performed. |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 634 | * @param resolver |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 635 | */ |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 636 | private Uri ensureIsContactUri(final ContentResolver resolver, final Uri uri) { |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 637 | if (uri == null) throw new IllegalArgumentException("uri must not be null"); |
| 638 | |
| 639 | final String authority = uri.getAuthority(); |
| 640 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 641 | // Current Style Uri? |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 642 | if (ContactsContract.AUTHORITY.equals(authority)) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 643 | final String type = resolver.getType(uri); |
| 644 | // Contact-Uri? Good, return it |
| 645 | if (Contacts.CONTENT_ITEM_TYPE.equals(type)) { |
| 646 | return uri; |
| 647 | } |
| 648 | |
| 649 | // RawContact-Uri? Transform it to ContactUri |
| 650 | if (RawContacts.CONTENT_ITEM_TYPE.equals(type)) { |
| 651 | final long rawContactId = ContentUris.parseId(uri); |
| 652 | return RawContacts.getContactLookupUri(getContext().getContentResolver(), |
| 653 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId)); |
| 654 | } |
| 655 | |
| 656 | // Anything else? We don't know what this is |
| 657 | throw new IllegalArgumentException("uri format is unknown"); |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | // Legacy Style? Convert to RawContact |
| 661 | final String OBSOLETE_AUTHORITY = "contacts"; |
| 662 | if (OBSOLETE_AUTHORITY.equals(authority)) { |
| 663 | // Legacy Format. Convert to RawContact-Uri and then lookup the contact |
| 664 | final long rawContactId = ContentUris.parseId(uri); |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 665 | return RawContacts.getContactLookupUri(resolver, |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 666 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId)); |
| 667 | } |
| 668 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 669 | throw new IllegalArgumentException("uri authority is unknown"); |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 672 | private Result loadContactEntity(ContentResolver resolver, Uri contactUri) { |
| 673 | Uri entityUri = Uri.withAppendedPath(contactUri, Contacts.Entity.CONTENT_DIRECTORY); |
| 674 | Cursor cursor = resolver.query(entityUri, ContactQuery.COLUMNS, null, null, |
| 675 | Contacts.Entity.RAW_CONTACT_ID); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 676 | if (cursor == null) { |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 677 | Log.e(TAG, "No cursor returned in loadContactEntity"); |
| 678 | return Result.NOT_FOUND; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 679 | } |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 680 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 681 | try { |
| 682 | if (!cursor.moveToFirst()) { |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 683 | cursor.close(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 684 | return Result.NOT_FOUND; |
| 685 | } |
| 686 | |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 687 | long currentRawContactId = -1; |
| 688 | Entity entity = null; |
| 689 | Result result = loadContactHeaderData(cursor, contactUri); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 690 | ArrayList<Entity> entities = result.getEntities(); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 691 | HashMap<Long, DataStatus> statuses = result.getStatuses(); |
| 692 | for (; !cursor.isAfterLast(); cursor.moveToNext()) { |
| 693 | long rawContactId = cursor.getLong(ContactQuery.RAW_CONTACT_ID); |
| 694 | if (rawContactId != currentRawContactId) { |
| 695 | currentRawContactId = rawContactId; |
| 696 | entity = new android.content.Entity(loadRawContact(cursor)); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 697 | entities.add(entity); |
| 698 | } |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 699 | if (!cursor.isNull(ContactQuery.DATA_ID)) { |
| 700 | ContentValues data = loadData(cursor); |
| 701 | entity.addSubValue(ContactsContract.Data.CONTENT_URI, data); |
| 702 | |
| 703 | if (!cursor.isNull(ContactQuery.PRESENCE) |
| 704 | || !cursor.isNull(ContactQuery.STATUS)) { |
| 705 | final DataStatus status = new DataStatus(cursor); |
| 706 | final long dataId = cursor.getLong(ContactQuery.DATA_ID); |
| 707 | statuses.put(dataId, status); |
| 708 | } |
| 709 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 710 | } |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 711 | |
| 712 | return result; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 713 | } finally { |
| 714 | cursor.close(); |
| 715 | } |
| 716 | } |
| 717 | |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 718 | /** |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 719 | * Looks for the photo data item in entities. If found, creates a new Bitmap instance. If |
| 720 | * not found, returns null |
| 721 | */ |
| 722 | private void loadPhotoBinaryData(Result contactData) { |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 723 | |
| 724 | // If we have a photo URI, try loading that first. |
| 725 | String photoUri = contactData.getPhotoUri(); |
| 726 | if (photoUri != null) { |
| 727 | try { |
| 728 | AssetFileDescriptor fd = getContext().getContentResolver() |
| 729 | .openAssetFileDescriptor(Uri.parse(photoUri), "r"); |
| 730 | byte[] buffer = new byte[16 * 1024]; |
| 731 | FileInputStream fis = fd.createInputStream(); |
| 732 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 733 | try { |
| 734 | int size; |
| 735 | while ((size = fis.read(buffer)) != -1) { |
| 736 | baos.write(buffer, 0, size); |
| 737 | } |
| 738 | contactData.setPhotoBinaryData(baos.toByteArray()); |
| 739 | } finally { |
| 740 | fis.close(); |
| 741 | fd.close(); |
| 742 | } |
| 743 | return; |
| 744 | } catch (IOException ioe) { |
| 745 | // Just fall back to the case below. |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // If we couldn't load from a file, fall back to the data blob. |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 750 | final long photoId = contactData.getPhotoId(); |
| 751 | if (photoId <= 0) { |
| 752 | // No photo ID |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | for (Entity entity : contactData.getEntities()) { |
| 757 | for (NamedContentValues subValue : entity.getSubValues()) { |
| 758 | final ContentValues entryValues = subValue.values; |
| 759 | final long dataId = entryValues.getAsLong(Data._ID); |
| 760 | if (dataId == photoId) { |
| 761 | final String mimeType = entryValues.getAsString(Data.MIMETYPE); |
| 762 | // Correct Data Id but incorrect MimeType? Don't load |
| 763 | if (!Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 764 | return; |
| 765 | } |
| 766 | contactData.setPhotoBinaryData(entryValues.getAsByteArray(Photo.PHOTO)); |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
Makoto Onuki | 3e6991e | 2011-07-24 14:51:20 -0700 | [diff] [blame] | 773 | /** |
| 774 | * Sets the "invitable" account types to {@link Result#mInvitableAccountTypes}. |
| 775 | * |
| 776 | * TODO Exclude the ones with no raw contacts in the database. |
| 777 | */ |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 778 | private void loadInvitableAccountTypes(Result contactData) { |
| 779 | Map<String, AccountType> allInvitables = |
| 780 | AccountTypeManager.getInstance(getContext()).getInvitableAccountTypes(); |
| 781 | if (allInvitables.isEmpty()) { |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | HashMap<String, AccountType> result = new HashMap<String, AccountType>(allInvitables); |
| 786 | |
| 787 | // Remove the ones that already has a raw contact in the current contact |
| 788 | for (Entity entity : contactData.getEntities()) { |
| 789 | final String type = entity.getEntityValues().getAsString(RawContacts.ACCOUNT_TYPE); |
| 790 | if (!TextUtils.isEmpty(type)) { |
| 791 | result.remove(type); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | // Set to mInvitableAccountTypes |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 796 | contactData.mInvitableAccountTypes.addAll(result.values()); |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 799 | /** |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 800 | * Extracts Contact level columns from the cursor. |
| 801 | */ |
| 802 | private Result loadContactHeaderData(final Cursor cursor, Uri contactUri) { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 803 | final String directoryParameter = |
| 804 | contactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY); |
| 805 | final long directoryId = directoryParameter == null |
| 806 | ? Directory.DEFAULT |
| 807 | : Long.parseLong(directoryParameter); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 808 | final long contactId = cursor.getLong(ContactQuery.CONTACT_ID); |
| 809 | final String lookupKey = cursor.getString(ContactQuery.LOOKUP_KEY); |
| 810 | final long nameRawContactId = cursor.getLong(ContactQuery.NAME_RAW_CONTACT_ID); |
| 811 | final int displayNameSource = cursor.getInt(ContactQuery.DISPLAY_NAME_SOURCE); |
| 812 | final String displayName = cursor.getString(ContactQuery.DISPLAY_NAME); |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 813 | final String altDisplayName = cursor.getString(ContactQuery.ALT_DISPLAY_NAME); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 814 | final String phoneticName = cursor.getString(ContactQuery.PHONETIC_NAME); |
| 815 | final long photoId = cursor.getLong(ContactQuery.PHOTO_ID); |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 816 | final String photoUri = cursor.getString(ContactQuery.PHOTO_URI); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 817 | final boolean starred = cursor.getInt(ContactQuery.STARRED) != 0; |
| 818 | final Integer presence = cursor.isNull(ContactQuery.CONTACT_PRESENCE) |
| 819 | ? null |
| 820 | : cursor.getInt(ContactQuery.CONTACT_PRESENCE); |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 821 | final boolean sendToVoicemail = cursor.getInt(ContactQuery.SEND_TO_VOICEMAIL) == 1; |
| 822 | final String customRingtone = cursor.getString(ContactQuery.CUSTOM_RINGTONE); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 823 | final boolean isUserProfile = cursor.getInt(ContactQuery.IS_USER_PROFILE) == 1; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 824 | |
Dmitri Plotnikov | 1536ea1 | 2010-10-29 11:51:05 -0700 | [diff] [blame] | 825 | Uri lookupUri; |
| 826 | if (directoryId == Directory.DEFAULT || directoryId == Directory.LOCAL_INVISIBLE) { |
| 827 | lookupUri = ContentUris.withAppendedId( |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 828 | Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey), contactId); |
Dmitri Plotnikov | 1536ea1 | 2010-10-29 11:51:05 -0700 | [diff] [blame] | 829 | } else { |
| 830 | lookupUri = contactUri; |
| 831 | } |
| 832 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 833 | return new Result(contactUri, lookupUri, directoryId, lookupKey, contactId, |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 834 | nameRawContactId, displayNameSource, photoId, photoUri, displayName, |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 835 | altDisplayName, phoneticName, starred, presence, sendToVoicemail, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame^] | 836 | customRingtone, isUserProfile); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Extracts RawContact level columns from the cursor. |
| 841 | */ |
| 842 | private ContentValues loadRawContact(Cursor cursor) { |
| 843 | ContentValues cv = new ContentValues(); |
| 844 | |
| 845 | cv.put(RawContacts._ID, cursor.getLong(ContactQuery.RAW_CONTACT_ID)); |
| 846 | |
| 847 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_NAME); |
| 848 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 849 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SET); |
| 850 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_TYPE_AND_DATA_SET); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 851 | cursorColumnToContentValues(cursor, cv, ContactQuery.DIRTY); |
| 852 | cursorColumnToContentValues(cursor, cv, ContactQuery.VERSION); |
| 853 | cursorColumnToContentValues(cursor, cv, ContactQuery.SOURCE_ID); |
| 854 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC1); |
| 855 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC2); |
| 856 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC3); |
| 857 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC4); |
| 858 | cursorColumnToContentValues(cursor, cv, ContactQuery.DELETED); |
| 859 | cursorColumnToContentValues(cursor, cv, ContactQuery.CONTACT_ID); |
| 860 | cursorColumnToContentValues(cursor, cv, ContactQuery.STARRED); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 861 | cursorColumnToContentValues(cursor, cv, ContactQuery.NAME_VERIFIED); |
| 862 | |
| 863 | return cv; |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Extracts Data level columns from the cursor. |
| 868 | */ |
| 869 | private ContentValues loadData(Cursor cursor) { |
| 870 | ContentValues cv = new ContentValues(); |
| 871 | |
| 872 | cv.put(Data._ID, cursor.getLong(ContactQuery.DATA_ID)); |
| 873 | |
| 874 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA1); |
| 875 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA2); |
| 876 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA3); |
| 877 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA4); |
| 878 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA5); |
| 879 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA6); |
| 880 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA7); |
| 881 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA8); |
| 882 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA9); |
| 883 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA10); |
| 884 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA11); |
| 885 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA12); |
| 886 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA13); |
| 887 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA14); |
| 888 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA15); |
| 889 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC1); |
| 890 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC2); |
| 891 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC3); |
| 892 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC4); |
| 893 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_VERSION); |
| 894 | cursorColumnToContentValues(cursor, cv, ContactQuery.IS_PRIMARY); |
| 895 | cursorColumnToContentValues(cursor, cv, ContactQuery.IS_SUPERPRIMARY); |
| 896 | cursorColumnToContentValues(cursor, cv, ContactQuery.MIMETYPE); |
| 897 | cursorColumnToContentValues(cursor, cv, ContactQuery.RES_PACKAGE); |
| 898 | cursorColumnToContentValues(cursor, cv, ContactQuery.GROUP_SOURCE_ID); |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 899 | cursorColumnToContentValues(cursor, cv, ContactQuery.CHAT_CAPABILITY); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 900 | |
| 901 | return cv; |
| 902 | } |
| 903 | |
| 904 | private void cursorColumnToContentValues( |
| 905 | Cursor cursor, ContentValues values, int index) { |
| 906 | switch (cursor.getType(index)) { |
| 907 | case Cursor.FIELD_TYPE_NULL: |
| 908 | // don't put anything in the content values |
| 909 | break; |
| 910 | case Cursor.FIELD_TYPE_INTEGER: |
| 911 | values.put(ContactQuery.COLUMNS[index], cursor.getLong(index)); |
| 912 | break; |
| 913 | case Cursor.FIELD_TYPE_STRING: |
| 914 | values.put(ContactQuery.COLUMNS[index], cursor.getString(index)); |
| 915 | break; |
| 916 | case Cursor.FIELD_TYPE_BLOB: |
| 917 | values.put(ContactQuery.COLUMNS[index], cursor.getBlob(index)); |
| 918 | break; |
| 919 | default: |
| 920 | throw new IllegalStateException("Invalid or unhandled data type"); |
| 921 | } |
| 922 | } |
| 923 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 924 | private void loadDirectoryMetaData(Result result) { |
| 925 | long directoryId = result.getDirectoryId(); |
| 926 | |
| 927 | Cursor cursor = getContext().getContentResolver().query( |
| 928 | ContentUris.withAppendedId(Directory.CONTENT_URI, directoryId), |
| 929 | DirectoryQuery.COLUMNS, null, null, null); |
| 930 | if (cursor == null) { |
| 931 | return; |
| 932 | } |
| 933 | try { |
| 934 | if (cursor.moveToFirst()) { |
| 935 | final String displayName = cursor.getString(DirectoryQuery.DISPLAY_NAME); |
| 936 | final String packageName = cursor.getString(DirectoryQuery.PACKAGE_NAME); |
| 937 | final int typeResourceId = cursor.getInt(DirectoryQuery.TYPE_RESOURCE_ID); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 938 | final String accountType = cursor.getString(DirectoryQuery.ACCOUNT_TYPE); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 939 | final String accountName = cursor.getString(DirectoryQuery.ACCOUNT_NAME); |
| 940 | final int exportSupport = cursor.getInt(DirectoryQuery.EXPORT_SUPPORT); |
| 941 | String directoryType = null; |
| 942 | if (!TextUtils.isEmpty(packageName)) { |
| 943 | PackageManager pm = getContext().getPackageManager(); |
| 944 | try { |
| 945 | Resources resources = pm.getResourcesForApplication(packageName); |
| 946 | directoryType = resources.getString(typeResourceId); |
| 947 | } catch (NameNotFoundException e) { |
| 948 | Log.w(TAG, "Contact directory resource not found: " |
| 949 | + packageName + "." + typeResourceId); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | result.setDirectoryMetaData( |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 954 | displayName, directoryType, accountType, accountName, exportSupport); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 955 | } |
| 956 | } finally { |
| 957 | cursor.close(); |
| 958 | } |
| 959 | } |
| 960 | |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 961 | /** |
| 962 | * Loads groups meta-data for all groups associated with all constituent raw contacts' |
| 963 | * accounts. |
| 964 | */ |
| 965 | private void loadGroupMetaData(Result result) { |
| 966 | StringBuilder selection = new StringBuilder(); |
| 967 | ArrayList<String> selectionArgs = new ArrayList<String>(); |
| 968 | for (Entity entity : result.mEntities) { |
| 969 | ContentValues values = entity.getEntityValues(); |
| 970 | String accountName = values.getAsString(RawContacts.ACCOUNT_NAME); |
| 971 | String accountType = values.getAsString(RawContacts.ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 972 | String dataSet = values.getAsString(RawContacts.DATA_SET); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 973 | if (accountName != null && accountType != null) { |
| 974 | if (selection.length() != 0) { |
| 975 | selection.append(" OR "); |
| 976 | } |
| 977 | selection.append( |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 978 | "(" + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?"); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 979 | selectionArgs.add(accountName); |
| 980 | selectionArgs.add(accountType); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 981 | |
| 982 | if (dataSet != null) { |
| 983 | selection.append(" AND " + Groups.DATA_SET + "=?"); |
| 984 | selectionArgs.add(dataSet); |
| 985 | } else { |
| 986 | selection.append(" AND " + Groups.DATA_SET + " IS NULL"); |
| 987 | } |
| 988 | selection.append(")"); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | Cursor cursor = getContext().getContentResolver().query(Groups.CONTENT_URI, |
| 992 | GroupQuery.COLUMNS, selection.toString(), selectionArgs.toArray(new String[0]), |
| 993 | null); |
| 994 | try { |
| 995 | while (cursor.moveToNext()) { |
| 996 | final String accountName = cursor.getString(GroupQuery.ACCOUNT_NAME); |
| 997 | final String accountType = cursor.getString(GroupQuery.ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 998 | final String dataSet = cursor.getString(GroupQuery.DATA_SET); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 999 | final long groupId = cursor.getLong(GroupQuery.ID); |
| 1000 | final String title = cursor.getString(GroupQuery.TITLE); |
| 1001 | final boolean defaultGroup = cursor.isNull(GroupQuery.AUTO_ADD) |
| 1002 | ? false |
| 1003 | : cursor.getInt(GroupQuery.AUTO_ADD) != 0; |
| 1004 | final boolean favorites = cursor.isNull(GroupQuery.FAVORITES) |
| 1005 | ? false |
| 1006 | : cursor.getInt(GroupQuery.FAVORITES) != 0; |
| 1007 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 1008 | result.addGroupMetaData(new GroupMetaData( |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 1009 | accountName, accountType, dataSet, groupId, title, defaultGroup, |
| 1010 | favorites)); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } finally { |
| 1013 | cursor.close(); |
| 1014 | } |
| 1015 | } |
| 1016 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1017 | /** |
| 1018 | * Loads all stream items and stream item photos belonging to this contact. |
| 1019 | */ |
| 1020 | private void loadStreamItems(Result result) { |
| 1021 | Cursor cursor = getContext().getContentResolver().query( |
| 1022 | Contacts.CONTENT_LOOKUP_URI.buildUpon() |
| 1023 | .appendPath(result.getLookupKey()) |
| 1024 | .appendPath(Contacts.StreamItems.CONTENT_DIRECTORY).build(), |
| 1025 | null, null, null, null); |
| 1026 | Map<Long, StreamItemEntry> streamItemsById = new HashMap<Long, StreamItemEntry>(); |
| 1027 | ArrayList<StreamItemEntry> streamItems = new ArrayList<StreamItemEntry>(); |
| 1028 | try { |
| 1029 | while (cursor.moveToNext()) { |
| 1030 | StreamItemEntry streamItem = new StreamItemEntry(cursor); |
| 1031 | streamItemsById.put(streamItem.getId(), streamItem); |
| 1032 | streamItems.add(streamItem); |
| 1033 | } |
| 1034 | } finally { |
| 1035 | cursor.close(); |
| 1036 | } |
| 1037 | |
| 1038 | // Now retrieve any photo records associated with the stream items. |
| 1039 | String[] streamItemIdArr = new String[streamItems.size()]; |
| 1040 | StringBuilder streamItemPhotoSelection = new StringBuilder(); |
| 1041 | if (!streamItems.isEmpty()) { |
| 1042 | streamItemPhotoSelection.append(StreamItemPhotos.STREAM_ITEM_ID + " IN ("); |
| 1043 | for (int i = 0; i < streamItems.size(); i++) { |
| 1044 | if (i > 0) { |
| 1045 | streamItemPhotoSelection.append(","); |
| 1046 | } |
| 1047 | streamItemPhotoSelection.append("?"); |
| 1048 | streamItemIdArr[i] = String.valueOf(streamItems.get(i).getId()); |
| 1049 | } |
| 1050 | streamItemPhotoSelection.append(")"); |
| 1051 | cursor = getContext().getContentResolver().query(StreamItems.CONTENT_PHOTO_URI, |
| 1052 | null, streamItemPhotoSelection.toString(), streamItemIdArr, |
| 1053 | StreamItemPhotos.STREAM_ITEM_ID); |
| 1054 | try { |
| 1055 | while (cursor.moveToNext()) { |
| 1056 | long streamItemId = cursor.getLong( |
| 1057 | cursor.getColumnIndex(StreamItemPhotos.STREAM_ITEM_ID)); |
| 1058 | StreamItemEntry streamItem = streamItemsById.get(streamItemId); |
| 1059 | streamItem.addPhoto(new StreamItemPhotoEntry(cursor)); |
| 1060 | } |
| 1061 | } finally { |
| 1062 | cursor.close(); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | // Set the sorted stream items on the result. |
| 1067 | Collections.sort(streamItems); |
| 1068 | result.mStreamItems.addAll(streamItems); |
| 1069 | } |
| 1070 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1071 | @Override |
| 1072 | protected void onPostExecute(Result result) { |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1073 | unregisterObserver(); |
| 1074 | |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 1075 | // The creator isn't interested in any further updates |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1076 | if (mDestroyed || result == null) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | mContact = result; |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1081 | |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1082 | if (result != Result.ERROR && result != Result.NOT_FOUND) { |
| 1083 | mLookupUri = result.getLookupUri(); |
| 1084 | |
| 1085 | if (!result.isDirectoryEntry()) { |
| 1086 | Log.i(TAG, "Registering content observer for " + mLookupUri); |
| 1087 | if (mObserver == null) { |
| 1088 | mObserver = new ForceLoadContentObserver(); |
| 1089 | } |
| 1090 | getContext().getContentResolver().registerContentObserver( |
| 1091 | mLookupUri, true, mObserver); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1092 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1093 | |
| 1094 | if (mContact.getPhotoBinaryData() == null && mContact.getPhotoUri() != null) { |
| 1095 | mContact.setLoadingPhoto(true); |
| 1096 | new AsyncPhotoLoader().execute(mContact.getPhotoUri()); |
| 1097 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1098 | } |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1099 | |
| 1100 | deliverResult(mContact); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1104 | private class AsyncPhotoLoader extends AsyncTask<String, Void, byte[]> { |
| 1105 | |
| 1106 | private static final int BUFFER_SIZE = 1024*16; |
| 1107 | |
| 1108 | @Override |
| 1109 | protected byte[] doInBackground(String... params) { |
| 1110 | Uri uri = Uri.parse(params[0]); |
| 1111 | byte[] data = null; |
| 1112 | try { |
| 1113 | InputStream is = getContext().getContentResolver().openInputStream(uri); |
| 1114 | if (is != null) { |
| 1115 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 1116 | try { |
| 1117 | byte[] mBuffer = new byte[BUFFER_SIZE]; |
| 1118 | |
| 1119 | int size; |
| 1120 | while ((size = is.read(mBuffer)) != -1) { |
| 1121 | baos.write(mBuffer, 0, size); |
| 1122 | } |
| 1123 | data = baos.toByteArray(); |
| 1124 | } finally { |
| 1125 | is.close(); |
| 1126 | } |
| 1127 | } else { |
| 1128 | Log.v(TAG, "Cannot load photo " + uri); |
| 1129 | } |
| 1130 | } catch (IOException e) { |
| 1131 | Log.e(TAG, "Cannot load photo " + uri, e); |
| 1132 | } |
| 1133 | |
| 1134 | return data; |
| 1135 | } |
| 1136 | |
| 1137 | @Override |
| 1138 | protected void onPostExecute(byte[] data) { |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1139 | if (mContact != null) { |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 1140 | mContact = new Result(mContact); |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1141 | mContact.setPhotoBinaryData(data); |
| 1142 | mContact.setLoadingPhoto(false); |
| 1143 | deliverResult(mContact); |
| 1144 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
Daniel Lehmann | 3a12077 | 2010-06-21 16:21:35 -0700 | [diff] [blame] | 1148 | private void unregisterObserver() { |
| 1149 | if (mObserver != null) { |
| 1150 | getContext().getContentResolver().unregisterContentObserver(mObserver); |
| 1151 | mObserver = null; |
| 1152 | } |
| 1153 | } |
| 1154 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 1155 | public ContactLoader(Context context, Uri lookupUri) { |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 1156 | this(context, lookupUri, false, false, false); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1159 | public ContactLoader(Context context, Uri lookupUri, boolean loadGroupMetaData, |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 1160 | boolean loadStreamItems, boolean loadInvitableAccountTypes) { |
Daniel Lehmann | 74a2dc5 | 2010-04-15 16:52:33 -0700 | [diff] [blame] | 1161 | super(context); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1162 | mLookupUri = lookupUri; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 1163 | mLoadGroupMetaData = loadGroupMetaData; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1164 | mLoadStreamItems = loadStreamItems; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 1165 | mLoadInvitableAccountTypes = loadInvitableAccountTypes; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Dmitri Plotnikov | 5a30d9a | 2010-11-23 14:59:50 -0800 | [diff] [blame] | 1168 | public Uri getLookupUri() { |
| 1169 | return mLookupUri; |
| 1170 | } |
| 1171 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1172 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1173 | protected void onStartLoading() { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1174 | if (mContact != null) { |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 1175 | deliverResult(mContact); |
Dmitri Plotnikov | 97e90c6 | 2011-01-03 11:58:13 -0800 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | if (takeContentChanged() || mContact == null) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1179 | forceLoad(); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1184 | protected void onForceLoad() { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1185 | final LoadContactTask task = new LoadContactTask(); |
Daniel Lehmann | 74a2dc5 | 2010-04-15 16:52:33 -0700 | [diff] [blame] | 1186 | task.execute((Void[])null); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1190 | protected void onReset() { |
Dianne Hackborn | 4ef95cc | 2010-12-16 00:44:33 -0800 | [diff] [blame] | 1191 | unregisterObserver(); |
| 1192 | mContact = null; |
| 1193 | mDestroyed = true; |
| 1194 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1195 | } |