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