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; |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 20 | import com.google.common.annotations.VisibleForTesting; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 21 | |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.ContentUris; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 24 | import android.content.ContentValues; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Entity; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 27 | import android.content.Entity.NamedContentValues; |
Jeff Hamilton | 3c46291 | 2010-05-15 02:20:01 -0500 | [diff] [blame] | 28 | import android.content.Loader; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.PackageManager.NameNotFoundException; |
| 31 | import android.content.res.Resources; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 32 | import android.database.Cursor; |
| 33 | import android.net.Uri; |
| 34 | import android.os.AsyncTask; |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 36 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 37 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 38 | import android.provider.ContactsContract.Contacts; |
| 39 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 40 | import android.provider.ContactsContract.Directory; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 41 | import android.provider.ContactsContract.DisplayNameSources; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 42 | import android.provider.ContactsContract.Groups; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 43 | import android.provider.ContactsContract.RawContacts; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 44 | import android.text.TextUtils; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 45 | import android.util.Log; |
| 46 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 47 | import java.io.ByteArrayOutputStream; |
| 48 | import java.io.IOException; |
| 49 | import java.io.InputStream; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 50 | import java.util.ArrayList; |
| 51 | import java.util.HashMap; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 52 | import java.util.List; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Loads a single Contact and all it constituent RawContacts. |
| 56 | */ |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 57 | public class ContactLoader extends Loader<ContactLoader.Result> { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 58 | private static final String TAG = "ContactLoader"; |
| 59 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 60 | private Uri mLookupUri; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 61 | private boolean mLoadGroupMetaData; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 62 | private Result mContact; |
| 63 | private ForceLoadContentObserver mObserver; |
| 64 | private boolean mDestroyed; |
| 65 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 66 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 67 | public interface Listener { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 68 | public void onContactLoaded(Result contact); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * The result of a load operation. Contains all data necessary to display the contact. |
| 73 | */ |
| 74 | public static final class Result { |
| 75 | /** |
| 76 | * Singleton instance that represents "No Contact Found" |
| 77 | */ |
| 78 | public static final Result NOT_FOUND = new Result(); |
| 79 | |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Singleton instance that represents an error, e.g. because of an invalid Uri |
| 82 | * TODO: We should come up with something nicer here. Maybe use an Either type so |
| 83 | * that we can capture the Exception? |
| 84 | */ |
| 85 | public static final Result ERROR = new Result(); |
| 86 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 87 | private final Uri mLookupUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 88 | private final Uri mUri; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 89 | private final long mDirectoryId; |
| 90 | private final String mLookupKey; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 91 | private final long mId; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 92 | private final long mNameRawContactId; |
| 93 | private final int mDisplayNameSource; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 94 | private final long mPhotoId; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 95 | private final String mPhotoUri; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 96 | private final String mDisplayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 97 | private final String mAltDisplayName; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 98 | private final String mPhoneticName; |
| 99 | private final boolean mStarred; |
| 100 | private final Integer mPresence; |
| 101 | private final ArrayList<Entity> mEntities; |
| 102 | private final HashMap<Long, DataStatus> mStatuses; |
| 103 | private final String mStatus; |
| 104 | private final Long mStatusTimestamp; |
| 105 | private final Integer mStatusLabel; |
| 106 | private final String mStatusResPackage; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 107 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 108 | private String mDirectoryDisplayName; |
| 109 | private String mDirectoryType; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 110 | private String mDirectoryAccountType; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 111 | private String mDirectoryAccountName; |
| 112 | private int mDirectoryExportSupport; |
| 113 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 114 | private ArrayList<GroupMetaData> mGroups; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 115 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 116 | private boolean mLoadingPhoto; |
| 117 | private byte[] mPhotoBinaryData; |
| 118 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Constructor for case "no contact found". This must only be used for the |
| 121 | * final {@link Result#NOT_FOUND} singleton |
| 122 | */ |
| 123 | private Result() { |
| 124 | mLookupUri = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 125 | mUri = null; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 126 | mDirectoryId = -1; |
| 127 | mLookupKey = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 128 | mId = -1; |
| 129 | mEntities = null; |
| 130 | mStatuses = null; |
| 131 | mNameRawContactId = -1; |
| 132 | mDisplayNameSource = DisplayNameSources.UNDEFINED; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 133 | mPhotoId = -1; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 134 | mPhotoUri = null; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 135 | mDisplayName = null; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 136 | mAltDisplayName = null; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 137 | mPhoneticName = null; |
| 138 | mStarred = false; |
| 139 | mPresence = null; |
| 140 | mStatus = null; |
| 141 | mStatusTimestamp = null; |
| 142 | mStatusLabel = null; |
| 143 | mStatusResPackage = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Constructor to call when contact was found |
| 148 | */ |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 149 | private Result(Uri uri, Uri lookupUri, long directoryId, String lookupKey, long id, |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 150 | long nameRawContactId, int displayNameSource, long photoId, String photoUri, |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 151 | String displayName, String altDisplayName, String phoneticName, boolean starred, |
| 152 | Integer presence, String status, Long statusTimestamp, Integer statusLabel, |
| 153 | String statusResPackage) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 154 | mLookupUri = lookupUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 155 | mUri = uri; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 156 | mDirectoryId = directoryId; |
| 157 | mLookupKey = lookupKey; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 158 | mId = id; |
| 159 | mEntities = new ArrayList<Entity>(); |
| 160 | mStatuses = new HashMap<Long, DataStatus>(); |
| 161 | mNameRawContactId = nameRawContactId; |
| 162 | mDisplayNameSource = displayNameSource; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 163 | mPhotoId = photoId; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 164 | mPhotoUri = photoUri; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 165 | mDisplayName = displayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 166 | mAltDisplayName = altDisplayName; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 167 | mPhoneticName = phoneticName; |
| 168 | mStarred = starred; |
| 169 | mPresence = presence; |
| 170 | mStatus = status; |
| 171 | mStatusTimestamp = statusTimestamp; |
| 172 | mStatusLabel = statusLabel; |
| 173 | mStatusResPackage = statusResPackage; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 176 | private Result(Result from) { |
| 177 | mLookupUri = from.mLookupUri; |
| 178 | mUri = from.mUri; |
| 179 | mDirectoryId = from.mDirectoryId; |
| 180 | mLookupKey = from.mLookupKey; |
| 181 | mId = from.mId; |
| 182 | mNameRawContactId = from.mNameRawContactId; |
| 183 | mDisplayNameSource = from.mDisplayNameSource; |
| 184 | mPhotoId = from.mPhotoId; |
| 185 | mPhotoUri = from.mPhotoUri; |
| 186 | mDisplayName = from.mDisplayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 187 | mAltDisplayName = from.mAltDisplayName; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 188 | mPhoneticName = from.mPhoneticName; |
| 189 | mStarred = from.mStarred; |
| 190 | mPresence = from.mPresence; |
| 191 | mEntities = from.mEntities; |
| 192 | mStatuses = from.mStatuses; |
| 193 | mStatus = from.mStatus; |
| 194 | mStatusTimestamp = from.mStatusTimestamp; |
| 195 | mStatusLabel = from.mStatusLabel; |
| 196 | mStatusResPackage = from.mStatusResPackage; |
| 197 | |
| 198 | mDirectoryDisplayName = from.mDirectoryDisplayName; |
| 199 | mDirectoryType = from.mDirectoryType; |
| 200 | mDirectoryAccountType = from.mDirectoryAccountType; |
| 201 | mDirectoryAccountName = from.mDirectoryAccountName; |
| 202 | mDirectoryExportSupport = from.mDirectoryExportSupport; |
| 203 | |
| 204 | mGroups = from.mGroups; |
| 205 | |
| 206 | mLoadingPhoto = from.mLoadingPhoto; |
| 207 | mPhotoBinaryData = from.mPhotoBinaryData; |
| 208 | } |
| 209 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 210 | /** |
| 211 | * @param exportSupport See {@link Directory#EXPORT_SUPPORT}. |
| 212 | */ |
Daniel Lehmann | 1ad4d1b | 2010-10-18 19:20:41 -0700 | [diff] [blame] | 213 | private void setDirectoryMetaData(String displayName, String directoryType, |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 214 | String accountType, String accountName, int exportSupport) { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 215 | mDirectoryDisplayName = displayName; |
| 216 | mDirectoryType = directoryType; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 217 | mDirectoryAccountType = accountType; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 218 | mDirectoryAccountName = accountName; |
| 219 | mDirectoryExportSupport = exportSupport; |
| 220 | } |
| 221 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 222 | private void setLoadingPhoto(boolean flag) { |
| 223 | mLoadingPhoto = flag; |
| 224 | } |
| 225 | |
| 226 | private void setPhotoBinaryData(byte[] photoBinaryData) { |
| 227 | mPhotoBinaryData = photoBinaryData; |
| 228 | } |
| 229 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 230 | public Uri getLookupUri() { |
| 231 | return mLookupUri; |
| 232 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 233 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 234 | public String getLookupKey() { |
| 235 | return mLookupKey; |
| 236 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 237 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 238 | public Uri getUri() { |
| 239 | return mUri; |
| 240 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 241 | |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 242 | @VisibleForTesting |
| 243 | /*package*/ long getId() { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 244 | return mId; |
| 245 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 246 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 247 | public long getNameRawContactId() { |
| 248 | return mNameRawContactId; |
| 249 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 250 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 251 | public int getDisplayNameSource() { |
| 252 | return mDisplayNameSource; |
| 253 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 254 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 255 | public long getPhotoId() { |
| 256 | return mPhotoId; |
| 257 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 258 | |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 259 | public String getPhotoUri() { |
| 260 | return mPhotoUri; |
| 261 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 262 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 263 | public String getDisplayName() { |
| 264 | return mDisplayName; |
| 265 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 266 | |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 267 | public String getAltDisplayName() { |
| 268 | return mAltDisplayName; |
| 269 | } |
| 270 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 271 | public String getPhoneticName() { |
| 272 | return mPhoneticName; |
| 273 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 274 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 275 | public boolean getStarred() { |
| 276 | return mStarred; |
| 277 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 278 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 279 | public Integer getPresence() { |
| 280 | return mPresence; |
| 281 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 282 | |
Daniel Lehmann | 2d4f759 | 2010-10-12 23:31:31 -0700 | [diff] [blame] | 283 | public String getSocialSnippet() { |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 284 | return mStatus; |
| 285 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 286 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 287 | public Long getStatusTimestamp() { |
| 288 | return mStatusTimestamp; |
| 289 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 290 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 291 | public Integer getStatusLabel() { |
| 292 | return mStatusLabel; |
| 293 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 294 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 295 | public String getStatusResPackage() { |
| 296 | return mStatusResPackage; |
| 297 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 298 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 299 | public ArrayList<Entity> getEntities() { |
| 300 | return mEntities; |
| 301 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 302 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 303 | public HashMap<Long, DataStatus> getStatuses() { |
| 304 | return mStatuses; |
| 305 | } |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 306 | |
| 307 | public long getDirectoryId() { |
| 308 | return mDirectoryId; |
| 309 | } |
| 310 | |
| 311 | public boolean isDirectoryEntry() { |
Dmitri Plotnikov | 5f72c1f | 2010-09-01 21:21:04 -0700 | [diff] [blame] | 312 | return mDirectoryId != -1 && mDirectoryId != Directory.DEFAULT |
| 313 | && mDirectoryId != Directory.LOCAL_INVISIBLE; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | public int getDirectoryExportSupport() { |
| 317 | return mDirectoryExportSupport; |
| 318 | } |
| 319 | |
| 320 | public String getDirectoryDisplayName() { |
| 321 | return mDirectoryDisplayName; |
| 322 | } |
| 323 | |
| 324 | public String getDirectoryType() { |
| 325 | return mDirectoryType; |
| 326 | } |
| 327 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 328 | public String getDirectoryAccountType() { |
| 329 | return mDirectoryAccountType; |
| 330 | } |
| 331 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 332 | public String getDirectoryAccountName() { |
| 333 | return mDirectoryAccountName; |
| 334 | } |
| 335 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 336 | public boolean isLoadingPhoto() { |
| 337 | return mLoadingPhoto; |
| 338 | } |
| 339 | |
| 340 | public byte[] getPhotoBinaryData() { |
| 341 | return mPhotoBinaryData; |
| 342 | } |
| 343 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 344 | public ArrayList<ContentValues> getContentValues() { |
| 345 | if (mEntities.size() != 1) { |
| 346 | throw new IllegalStateException( |
| 347 | "Cannot extract content values from an aggregated contact"); |
| 348 | } |
| 349 | |
| 350 | Entity entity = mEntities.get(0); |
| 351 | ArrayList<ContentValues> result = new ArrayList<ContentValues>(); |
| 352 | ArrayList<NamedContentValues> subValues = entity.getSubValues(); |
| 353 | if (subValues != null) { |
| 354 | int size = subValues.size(); |
| 355 | for (int i = 0; i < size; i++) { |
| 356 | NamedContentValues pair = subValues.get(i); |
| 357 | if (Data.CONTENT_URI.equals(pair.uri)) { |
| 358 | result.add(pair.values); |
| 359 | } |
| 360 | } |
| 361 | } |
Dmitri Plotnikov | 40ec3a8 | 2010-11-10 11:25:33 -0800 | [diff] [blame] | 362 | |
| 363 | // If the photo was loaded using the URI, create an entry for the photo |
| 364 | // binary data. |
| 365 | if (mPhotoId == 0 && mPhotoBinaryData != null) { |
| 366 | ContentValues photo = new ContentValues(); |
| 367 | photo.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE); |
| 368 | photo.put(Photo.PHOTO, mPhotoBinaryData); |
| 369 | result.add(photo); |
| 370 | } |
| 371 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 372 | return result; |
| 373 | } |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 374 | |
Daniel Lehmann | 1ad4d1b | 2010-10-18 19:20:41 -0700 | [diff] [blame] | 375 | private void addGroupMetaData(GroupMetaData group) { |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 376 | if (mGroups == null) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 377 | mGroups = new ArrayList<GroupMetaData>(); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 378 | } |
| 379 | mGroups.add(group); |
| 380 | } |
| 381 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 382 | public List<GroupMetaData> getGroupMetaData() { |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 383 | return mGroups; |
| 384 | } |
| 385 | } |
| 386 | |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 387 | private static class ContactQuery { |
| 388 | // 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] | 389 | final static String[] COLUMNS = new String[] { |
| 390 | Contacts.NAME_RAW_CONTACT_ID, |
| 391 | Contacts.DISPLAY_NAME_SOURCE, |
| 392 | Contacts.LOOKUP_KEY, |
| 393 | Contacts.DISPLAY_NAME, |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 394 | Contacts.DISPLAY_NAME_ALTERNATIVE, |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 395 | Contacts.PHONETIC_NAME, |
| 396 | Contacts.PHOTO_ID, |
| 397 | Contacts.STARRED, |
| 398 | Contacts.CONTACT_PRESENCE, |
| 399 | Contacts.CONTACT_STATUS, |
| 400 | Contacts.CONTACT_STATUS_TIMESTAMP, |
| 401 | Contacts.CONTACT_STATUS_RES_PACKAGE, |
| 402 | Contacts.CONTACT_STATUS_LABEL, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 403 | Contacts.Entity.CONTACT_ID, |
| 404 | Contacts.Entity.RAW_CONTACT_ID, |
| 405 | |
| 406 | RawContacts.ACCOUNT_NAME, |
| 407 | RawContacts.ACCOUNT_TYPE, |
| 408 | RawContacts.DIRTY, |
| 409 | RawContacts.VERSION, |
| 410 | RawContacts.SOURCE_ID, |
| 411 | RawContacts.SYNC1, |
| 412 | RawContacts.SYNC2, |
| 413 | RawContacts.SYNC3, |
| 414 | RawContacts.SYNC4, |
| 415 | RawContacts.DELETED, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 416 | RawContacts.NAME_VERIFIED, |
| 417 | |
| 418 | Contacts.Entity.DATA_ID, |
| 419 | Data.DATA1, |
| 420 | Data.DATA2, |
| 421 | Data.DATA3, |
| 422 | Data.DATA4, |
| 423 | Data.DATA5, |
| 424 | Data.DATA6, |
| 425 | Data.DATA7, |
| 426 | Data.DATA8, |
| 427 | Data.DATA9, |
| 428 | Data.DATA10, |
| 429 | Data.DATA11, |
| 430 | Data.DATA12, |
| 431 | Data.DATA13, |
| 432 | Data.DATA14, |
| 433 | Data.DATA15, |
| 434 | Data.SYNC1, |
| 435 | Data.SYNC2, |
| 436 | Data.SYNC3, |
| 437 | Data.SYNC4, |
| 438 | Data.DATA_VERSION, |
| 439 | Data.IS_PRIMARY, |
| 440 | Data.IS_SUPER_PRIMARY, |
| 441 | Data.MIMETYPE, |
| 442 | Data.RES_PACKAGE, |
| 443 | |
| 444 | GroupMembership.GROUP_SOURCE_ID, |
| 445 | |
| 446 | Data.PRESENCE, |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 447 | Data.CHAT_CAPABILITY, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 448 | Data.STATUS, |
| 449 | Data.STATUS_RES_PACKAGE, |
| 450 | Data.STATUS_ICON, |
| 451 | Data.STATUS_LABEL, |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 452 | Data.STATUS_TIMESTAMP, |
| 453 | |
| 454 | Contacts.PHOTO_URI, |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 455 | }; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 456 | |
| 457 | public final static int NAME_RAW_CONTACT_ID = 0; |
| 458 | public final static int DISPLAY_NAME_SOURCE = 1; |
| 459 | public final static int LOOKUP_KEY = 2; |
| 460 | public final static int DISPLAY_NAME = 3; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 461 | public final static int ALT_DISPLAY_NAME = 4; |
| 462 | public final static int PHONETIC_NAME = 5; |
| 463 | public final static int PHOTO_ID = 6; |
| 464 | public final static int STARRED = 7; |
| 465 | public final static int CONTACT_PRESENCE = 8; |
| 466 | public final static int CONTACT_STATUS = 9; |
| 467 | public final static int CONTACT_STATUS_TIMESTAMP = 10; |
| 468 | public final static int CONTACT_STATUS_RES_PACKAGE = 11; |
| 469 | public final static int CONTACT_STATUS_LABEL = 12; |
| 470 | public final static int CONTACT_ID = 13; |
| 471 | public final static int RAW_CONTACT_ID = 14; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 472 | |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 473 | public final static int ACCOUNT_NAME = 15; |
| 474 | public final static int ACCOUNT_TYPE = 16; |
| 475 | public final static int DIRTY = 17; |
| 476 | public final static int VERSION = 18; |
| 477 | public final static int SOURCE_ID = 19; |
| 478 | public final static int SYNC1 = 20; |
| 479 | public final static int SYNC2 = 21; |
| 480 | public final static int SYNC3 = 22; |
| 481 | public final static int SYNC4 = 23; |
| 482 | public final static int DELETED = 24; |
Daniel Lehmann | 4ff3128 | 2011-07-11 14:38:36 -0700 | [diff] [blame] | 483 | public final static int NAME_VERIFIED = 25; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 484 | |
Daniel Lehmann | 4ff3128 | 2011-07-11 14:38:36 -0700 | [diff] [blame] | 485 | public final static int DATA_ID = 26; |
| 486 | public final static int DATA1 = 27; |
| 487 | public final static int DATA2 = 28; |
| 488 | public final static int DATA3 = 29; |
| 489 | public final static int DATA4 = 30; |
| 490 | public final static int DATA5 = 31; |
| 491 | public final static int DATA6 = 32; |
| 492 | public final static int DATA7 = 33; |
| 493 | public final static int DATA8 = 34; |
| 494 | public final static int DATA9 = 35; |
| 495 | public final static int DATA10 = 36; |
| 496 | public final static int DATA11 = 37; |
| 497 | public final static int DATA12 = 38; |
| 498 | public final static int DATA13 = 39; |
| 499 | public final static int DATA14 = 40; |
| 500 | public final static int DATA15 = 41; |
| 501 | public final static int DATA_SYNC1 = 42; |
| 502 | public final static int DATA_SYNC2 = 43; |
| 503 | public final static int DATA_SYNC3 = 44; |
| 504 | public final static int DATA_SYNC4 = 45; |
| 505 | public final static int DATA_VERSION = 46; |
| 506 | public final static int IS_PRIMARY = 47; |
| 507 | public final static int IS_SUPERPRIMARY = 48; |
| 508 | public final static int MIMETYPE = 49; |
| 509 | public final static int RES_PACKAGE = 50; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 510 | |
Daniel Lehmann | 4ff3128 | 2011-07-11 14:38:36 -0700 | [diff] [blame] | 511 | public final static int GROUP_SOURCE_ID = 51; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 512 | |
Daniel Lehmann | 4ff3128 | 2011-07-11 14:38:36 -0700 | [diff] [blame] | 513 | public final static int PRESENCE = 52; |
| 514 | public final static int CHAT_CAPABILITY = 53; |
| 515 | public final static int STATUS = 54; |
| 516 | public final static int STATUS_RES_PACKAGE = 55; |
| 517 | public final static int STATUS_ICON = 56; |
| 518 | public final static int STATUS_LABEL = 57; |
| 519 | public final static int STATUS_TIMESTAMP = 58; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 520 | |
Daniel Lehmann | 4ff3128 | 2011-07-11 14:38:36 -0700 | [diff] [blame] | 521 | public final static int PHOTO_URI = 59; |
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); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 765 | cursorColumnToContentValues(cursor, cv, ContactQuery.NAME_VERIFIED); |
| 766 | |
| 767 | return cv; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Extracts Data level columns from the cursor. |
| 772 | */ |
| 773 | private ContentValues loadData(Cursor cursor) { |
| 774 | ContentValues cv = new ContentValues(); |
| 775 | |
| 776 | cv.put(Data._ID, cursor.getLong(ContactQuery.DATA_ID)); |
| 777 | |
| 778 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA1); |
| 779 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA2); |
| 780 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA3); |
| 781 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA4); |
| 782 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA5); |
| 783 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA6); |
| 784 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA7); |
| 785 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA8); |
| 786 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA9); |
| 787 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA10); |
| 788 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA11); |
| 789 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA12); |
| 790 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA13); |
| 791 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA14); |
| 792 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA15); |
| 793 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC1); |
| 794 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC2); |
| 795 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC3); |
| 796 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC4); |
| 797 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_VERSION); |
| 798 | cursorColumnToContentValues(cursor, cv, ContactQuery.IS_PRIMARY); |
| 799 | cursorColumnToContentValues(cursor, cv, ContactQuery.IS_SUPERPRIMARY); |
| 800 | cursorColumnToContentValues(cursor, cv, ContactQuery.MIMETYPE); |
| 801 | cursorColumnToContentValues(cursor, cv, ContactQuery.RES_PACKAGE); |
| 802 | cursorColumnToContentValues(cursor, cv, ContactQuery.GROUP_SOURCE_ID); |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 803 | cursorColumnToContentValues(cursor, cv, ContactQuery.CHAT_CAPABILITY); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 804 | |
| 805 | return cv; |
| 806 | } |
| 807 | |
| 808 | private void cursorColumnToContentValues( |
| 809 | Cursor cursor, ContentValues values, int index) { |
| 810 | switch (cursor.getType(index)) { |
| 811 | case Cursor.FIELD_TYPE_NULL: |
| 812 | // don't put anything in the content values |
| 813 | break; |
| 814 | case Cursor.FIELD_TYPE_INTEGER: |
| 815 | values.put(ContactQuery.COLUMNS[index], cursor.getLong(index)); |
| 816 | break; |
| 817 | case Cursor.FIELD_TYPE_STRING: |
| 818 | values.put(ContactQuery.COLUMNS[index], cursor.getString(index)); |
| 819 | break; |
| 820 | case Cursor.FIELD_TYPE_BLOB: |
| 821 | values.put(ContactQuery.COLUMNS[index], cursor.getBlob(index)); |
| 822 | break; |
| 823 | default: |
| 824 | throw new IllegalStateException("Invalid or unhandled data type"); |
| 825 | } |
| 826 | } |
| 827 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 828 | private void loadDirectoryMetaData(Result result) { |
| 829 | long directoryId = result.getDirectoryId(); |
| 830 | |
| 831 | Cursor cursor = getContext().getContentResolver().query( |
| 832 | ContentUris.withAppendedId(Directory.CONTENT_URI, directoryId), |
| 833 | DirectoryQuery.COLUMNS, null, null, null); |
| 834 | if (cursor == null) { |
| 835 | return; |
| 836 | } |
| 837 | try { |
| 838 | if (cursor.moveToFirst()) { |
| 839 | final String displayName = cursor.getString(DirectoryQuery.DISPLAY_NAME); |
| 840 | final String packageName = cursor.getString(DirectoryQuery.PACKAGE_NAME); |
| 841 | final int typeResourceId = cursor.getInt(DirectoryQuery.TYPE_RESOURCE_ID); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 842 | final String accountType = cursor.getString(DirectoryQuery.ACCOUNT_TYPE); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 843 | final String accountName = cursor.getString(DirectoryQuery.ACCOUNT_NAME); |
| 844 | final int exportSupport = cursor.getInt(DirectoryQuery.EXPORT_SUPPORT); |
| 845 | String directoryType = null; |
| 846 | if (!TextUtils.isEmpty(packageName)) { |
| 847 | PackageManager pm = getContext().getPackageManager(); |
| 848 | try { |
| 849 | Resources resources = pm.getResourcesForApplication(packageName); |
| 850 | directoryType = resources.getString(typeResourceId); |
| 851 | } catch (NameNotFoundException e) { |
| 852 | Log.w(TAG, "Contact directory resource not found: " |
| 853 | + packageName + "." + typeResourceId); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | result.setDirectoryMetaData( |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 858 | displayName, directoryType, accountType, accountName, exportSupport); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 859 | } |
| 860 | } finally { |
| 861 | cursor.close(); |
| 862 | } |
| 863 | } |
| 864 | |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 865 | /** |
| 866 | * Loads groups meta-data for all groups associated with all constituent raw contacts' |
| 867 | * accounts. |
| 868 | */ |
| 869 | private void loadGroupMetaData(Result result) { |
| 870 | StringBuilder selection = new StringBuilder(); |
| 871 | ArrayList<String> selectionArgs = new ArrayList<String>(); |
| 872 | for (Entity entity : result.mEntities) { |
| 873 | ContentValues values = entity.getEntityValues(); |
| 874 | String accountName = values.getAsString(RawContacts.ACCOUNT_NAME); |
| 875 | String accountType = values.getAsString(RawContacts.ACCOUNT_TYPE); |
| 876 | if (accountName != null && accountType != null) { |
| 877 | if (selection.length() != 0) { |
| 878 | selection.append(" OR "); |
| 879 | } |
| 880 | selection.append( |
| 881 | "(" + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?)"); |
| 882 | selectionArgs.add(accountName); |
| 883 | selectionArgs.add(accountType); |
| 884 | } |
| 885 | } |
| 886 | Cursor cursor = getContext().getContentResolver().query(Groups.CONTENT_URI, |
| 887 | GroupQuery.COLUMNS, selection.toString(), selectionArgs.toArray(new String[0]), |
| 888 | null); |
| 889 | try { |
| 890 | while (cursor.moveToNext()) { |
| 891 | final String accountName = cursor.getString(GroupQuery.ACCOUNT_NAME); |
| 892 | final String accountType = cursor.getString(GroupQuery.ACCOUNT_TYPE); |
| 893 | final long groupId = cursor.getLong(GroupQuery.ID); |
| 894 | final String title = cursor.getString(GroupQuery.TITLE); |
| 895 | final boolean defaultGroup = cursor.isNull(GroupQuery.AUTO_ADD) |
| 896 | ? false |
| 897 | : cursor.getInt(GroupQuery.AUTO_ADD) != 0; |
| 898 | final boolean favorites = cursor.isNull(GroupQuery.FAVORITES) |
| 899 | ? false |
| 900 | : cursor.getInt(GroupQuery.FAVORITES) != 0; |
| 901 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 902 | result.addGroupMetaData(new GroupMetaData( |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 903 | accountName, accountType, groupId, title, defaultGroup, favorites)); |
| 904 | } |
| 905 | } finally { |
| 906 | cursor.close(); |
| 907 | } |
| 908 | } |
| 909 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 910 | @Override |
| 911 | protected void onPostExecute(Result result) { |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 912 | unregisterObserver(); |
| 913 | |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 914 | // The creator isn't interested in any further updates |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 915 | if (mDestroyed || result == null) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 916 | return; |
| 917 | } |
| 918 | |
| 919 | mContact = result; |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 920 | |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 921 | if (result != Result.ERROR && result != Result.NOT_FOUND) { |
| 922 | mLookupUri = result.getLookupUri(); |
| 923 | |
| 924 | if (!result.isDirectoryEntry()) { |
| 925 | Log.i(TAG, "Registering content observer for " + mLookupUri); |
| 926 | if (mObserver == null) { |
| 927 | mObserver = new ForceLoadContentObserver(); |
| 928 | } |
| 929 | getContext().getContentResolver().registerContentObserver( |
| 930 | mLookupUri, true, mObserver); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 931 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 932 | |
| 933 | if (mContact.getPhotoBinaryData() == null && mContact.getPhotoUri() != null) { |
| 934 | mContact.setLoadingPhoto(true); |
| 935 | new AsyncPhotoLoader().execute(mContact.getPhotoUri()); |
| 936 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 937 | } |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 938 | |
| 939 | deliverResult(mContact); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 943 | private class AsyncPhotoLoader extends AsyncTask<String, Void, byte[]> { |
| 944 | |
| 945 | private static final int BUFFER_SIZE = 1024*16; |
| 946 | |
| 947 | @Override |
| 948 | protected byte[] doInBackground(String... params) { |
| 949 | Uri uri = Uri.parse(params[0]); |
| 950 | byte[] data = null; |
| 951 | try { |
| 952 | InputStream is = getContext().getContentResolver().openInputStream(uri); |
| 953 | if (is != null) { |
| 954 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 955 | try { |
| 956 | byte[] mBuffer = new byte[BUFFER_SIZE]; |
| 957 | |
| 958 | int size; |
| 959 | while ((size = is.read(mBuffer)) != -1) { |
| 960 | baos.write(mBuffer, 0, size); |
| 961 | } |
| 962 | data = baos.toByteArray(); |
| 963 | } finally { |
| 964 | is.close(); |
| 965 | } |
| 966 | } else { |
| 967 | Log.v(TAG, "Cannot load photo " + uri); |
| 968 | } |
| 969 | } catch (IOException e) { |
| 970 | Log.e(TAG, "Cannot load photo " + uri, e); |
| 971 | } |
| 972 | |
| 973 | return data; |
| 974 | } |
| 975 | |
| 976 | @Override |
| 977 | protected void onPostExecute(byte[] data) { |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 978 | if (mContact != null) { |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 979 | mContact = new Result(mContact); |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 980 | mContact.setPhotoBinaryData(data); |
| 981 | mContact.setLoadingPhoto(false); |
| 982 | deliverResult(mContact); |
| 983 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
Daniel Lehmann | 3a12077 | 2010-06-21 16:21:35 -0700 | [diff] [blame] | 987 | private void unregisterObserver() { |
| 988 | if (mObserver != null) { |
| 989 | getContext().getContentResolver().unregisterContentObserver(mObserver); |
| 990 | mObserver = null; |
| 991 | } |
| 992 | } |
| 993 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 994 | public ContactLoader(Context context, Uri lookupUri) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 995 | this(context, lookupUri, false); |
| 996 | } |
| 997 | |
| 998 | public ContactLoader(Context context, Uri lookupUri, boolean loadGroupMetaData) { |
Daniel Lehmann | 74a2dc5 | 2010-04-15 16:52:33 -0700 | [diff] [blame] | 999 | super(context); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1000 | mLookupUri = lookupUri; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 1001 | mLoadGroupMetaData = loadGroupMetaData; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Dmitri Plotnikov | 5a30d9a | 2010-11-23 14:59:50 -0800 | [diff] [blame] | 1004 | public Uri getLookupUri() { |
| 1005 | return mLookupUri; |
| 1006 | } |
| 1007 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1008 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1009 | protected void onStartLoading() { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1010 | if (mContact != null) { |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 1011 | deliverResult(mContact); |
Dmitri Plotnikov | 97e90c6 | 2011-01-03 11:58:13 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | if (takeContentChanged() || mContact == null) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1015 | forceLoad(); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1020 | protected void onForceLoad() { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1021 | final LoadContactTask task = new LoadContactTask(); |
Daniel Lehmann | 74a2dc5 | 2010-04-15 16:52:33 -0700 | [diff] [blame] | 1022 | task.execute((Void[])null); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1026 | protected void onReset() { |
Dianne Hackborn | 4ef95cc | 2010-12-16 00:44:33 -0800 | [diff] [blame] | 1027 | unregisterObserver(); |
| 1028 | mContact = null; |
| 1029 | mDestroyed = true; |
| 1030 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1031 | } |