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