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