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