Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 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 | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 18 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 19 | import com.android.contacts.model.AccountTypeManager; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 20 | import com.android.contacts.model.AccountWithDataSet; |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame^] | 21 | import com.android.contacts.model.EntityDelta; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 22 | import com.android.contacts.model.EntityDeltaList; |
| 23 | import com.android.contacts.model.EntityModifier; |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 24 | import com.google.android.collect.Lists; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 25 | import com.google.android.collect.Sets; |
| 26 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 27 | import android.app.Activity; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 28 | import android.app.IntentService; |
| 29 | import android.content.ContentProviderOperation; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 30 | import android.content.ContentProviderOperation.Builder; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 31 | import android.content.ContentProviderResult; |
| 32 | import android.content.ContentResolver; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 33 | import android.content.ContentUris; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 34 | import android.content.ContentValues; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 35 | import android.content.Context; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 36 | import android.content.Intent; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 37 | import android.content.OperationApplicationException; |
| 38 | import android.database.Cursor; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 39 | import android.net.Uri; |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 40 | import android.os.Handler; |
| 41 | import android.os.Looper; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 42 | import android.os.Parcelable; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 43 | import android.os.RemoteException; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 44 | import android.provider.ContactsContract; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 45 | import android.provider.ContactsContract.AggregationExceptions; |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 46 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 47 | import android.provider.ContactsContract.Contacts; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 48 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 49 | import android.provider.ContactsContract.Groups; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 50 | import android.provider.ContactsContract.Profile; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 51 | import android.provider.ContactsContract.RawContacts; |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame^] | 52 | import android.provider.ContactsContract.RawContactsEntity; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 53 | import android.util.Log; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 54 | import android.widget.Toast; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 55 | |
| 56 | import java.util.ArrayList; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 57 | import java.util.HashSet; |
| 58 | import java.util.List; |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 59 | import java.util.concurrent.CopyOnWriteArrayList; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 60 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 61 | /** |
| 62 | * A service responsible for saving changes to the content provider. |
| 63 | */ |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 64 | public class ContactSaveService extends IntentService { |
| 65 | private static final String TAG = "ContactSaveService"; |
| 66 | |
Katherine Kuan | a007e44 | 2011-07-07 09:25:34 -0700 | [diff] [blame] | 67 | /** Set to true in order to view logs on content provider operations */ |
| 68 | private static final boolean DEBUG = false; |
| 69 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 70 | public static final String ACTION_NEW_RAW_CONTACT = "newRawContact"; |
| 71 | |
| 72 | public static final String EXTRA_ACCOUNT_NAME = "accountName"; |
| 73 | public static final String EXTRA_ACCOUNT_TYPE = "accountType"; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 74 | public static final String EXTRA_DATA_SET = "dataSet"; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 75 | public static final String EXTRA_CONTENT_VALUES = "contentValues"; |
| 76 | public static final String EXTRA_CALLBACK_INTENT = "callbackIntent"; |
| 77 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 78 | public static final String ACTION_SAVE_CONTACT = "saveContact"; |
| 79 | public static final String EXTRA_CONTACT_STATE = "state"; |
| 80 | public static final String EXTRA_SAVE_MODE = "saveMode"; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 81 | public static final String EXTRA_SAVE_IS_PROFILE = "saveIsProfile"; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 82 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 83 | public static final String ACTION_CREATE_GROUP = "createGroup"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 84 | public static final String ACTION_RENAME_GROUP = "renameGroup"; |
| 85 | public static final String ACTION_DELETE_GROUP = "deleteGroup"; |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 86 | public static final String ACTION_UPDATE_GROUP = "updateGroup"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 87 | public static final String EXTRA_GROUP_ID = "groupId"; |
| 88 | public static final String EXTRA_GROUP_LABEL = "groupLabel"; |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 89 | public static final String EXTRA_RAW_CONTACTS_TO_ADD = "rawContactsToAdd"; |
| 90 | public static final String EXTRA_RAW_CONTACTS_TO_REMOVE = "rawContactsToRemove"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 91 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 92 | public static final String ACTION_SET_STARRED = "setStarred"; |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 93 | public static final String ACTION_DELETE_CONTACT = "delete"; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 94 | public static final String EXTRA_CONTACT_URI = "contactUri"; |
| 95 | public static final String EXTRA_STARRED_FLAG = "starred"; |
| 96 | |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 97 | public static final String ACTION_SET_SUPER_PRIMARY = "setSuperPrimary"; |
| 98 | public static final String ACTION_CLEAR_PRIMARY = "clearPrimary"; |
| 99 | public static final String EXTRA_DATA_ID = "dataId"; |
| 100 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 101 | public static final String ACTION_JOIN_CONTACTS = "joinContacts"; |
| 102 | public static final String EXTRA_CONTACT_ID1 = "contactId1"; |
| 103 | public static final String EXTRA_CONTACT_ID2 = "contactId2"; |
| 104 | public static final String EXTRA_CONTACT_WRITABLE = "contactWritable"; |
| 105 | |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 106 | public static final String ACTION_SET_SEND_TO_VOICEMAIL = "sendToVoicemail"; |
| 107 | public static final String EXTRA_SEND_TO_VOICEMAIL_FLAG = "sendToVoicemailFlag"; |
| 108 | |
| 109 | public static final String ACTION_SET_RINGTONE = "setRingtone"; |
| 110 | public static final String EXTRA_CUSTOM_RINGTONE = "customRingtone"; |
| 111 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 112 | private static final HashSet<String> ALLOWED_DATA_COLUMNS = Sets.newHashSet( |
| 113 | Data.MIMETYPE, |
| 114 | Data.IS_PRIMARY, |
| 115 | Data.DATA1, |
| 116 | Data.DATA2, |
| 117 | Data.DATA3, |
| 118 | Data.DATA4, |
| 119 | Data.DATA5, |
| 120 | Data.DATA6, |
| 121 | Data.DATA7, |
| 122 | Data.DATA8, |
| 123 | Data.DATA9, |
| 124 | Data.DATA10, |
| 125 | Data.DATA11, |
| 126 | Data.DATA12, |
| 127 | Data.DATA13, |
| 128 | Data.DATA14, |
| 129 | Data.DATA15 |
| 130 | ); |
| 131 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 132 | private static final int PERSIST_TRIES = 3; |
| 133 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 134 | public interface Listener { |
| 135 | public void onServiceCompleted(Intent callbackIntent); |
| 136 | } |
| 137 | |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 138 | private static final CopyOnWriteArrayList<Listener> sListeners = |
| 139 | new CopyOnWriteArrayList<Listener>(); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 140 | |
| 141 | private Handler mMainHandler; |
| 142 | |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 143 | public ContactSaveService() { |
| 144 | super(TAG); |
| 145 | setIntentRedelivery(true); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 146 | mMainHandler = new Handler(Looper.getMainLooper()); |
| 147 | } |
| 148 | |
| 149 | public static void registerListener(Listener listener) { |
| 150 | if (!(listener instanceof Activity)) { |
| 151 | throw new ClassCastException("Only activities can be registered to" |
| 152 | + " receive callback from " + ContactSaveService.class.getName()); |
| 153 | } |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 154 | sListeners.add(0, listener); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public static void unregisterListener(Listener listener) { |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 158 | sListeners.remove(listener); |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 162 | public Object getSystemService(String name) { |
| 163 | Object service = super.getSystemService(name); |
| 164 | if (service != null) { |
| 165 | return service; |
| 166 | } |
| 167 | |
| 168 | return getApplicationContext().getSystemService(name); |
| 169 | } |
| 170 | |
| 171 | @Override |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 172 | protected void onHandleIntent(Intent intent) { |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 173 | String action = intent.getAction(); |
| 174 | if (ACTION_NEW_RAW_CONTACT.equals(action)) { |
| 175 | createRawContact(intent); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 176 | } else if (ACTION_SAVE_CONTACT.equals(action)) { |
| 177 | saveContact(intent); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 178 | } else if (ACTION_CREATE_GROUP.equals(action)) { |
| 179 | createGroup(intent); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 180 | } else if (ACTION_RENAME_GROUP.equals(action)) { |
| 181 | renameGroup(intent); |
| 182 | } else if (ACTION_DELETE_GROUP.equals(action)) { |
| 183 | deleteGroup(intent); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 184 | } else if (ACTION_UPDATE_GROUP.equals(action)) { |
| 185 | updateGroup(intent); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 186 | } else if (ACTION_SET_STARRED.equals(action)) { |
| 187 | setStarred(intent); |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 188 | } else if (ACTION_SET_SUPER_PRIMARY.equals(action)) { |
| 189 | setSuperPrimary(intent); |
| 190 | } else if (ACTION_CLEAR_PRIMARY.equals(action)) { |
| 191 | clearPrimary(intent); |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 192 | } else if (ACTION_DELETE_CONTACT.equals(action)) { |
| 193 | deleteContact(intent); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 194 | } else if (ACTION_JOIN_CONTACTS.equals(action)) { |
| 195 | joinContacts(intent); |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 196 | } else if (ACTION_SET_SEND_TO_VOICEMAIL.equals(action)) { |
| 197 | setSendToVoicemail(intent); |
| 198 | } else if (ACTION_SET_RINGTONE.equals(action)) { |
| 199 | setRingtone(intent); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 203 | /** |
| 204 | * Creates an intent that can be sent to this service to create a new raw contact |
| 205 | * using data presented as a set of ContentValues. |
| 206 | */ |
| 207 | public static Intent createNewRawContactIntent(Context context, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 208 | ArrayList<ContentValues> values, AccountWithDataSet account, |
| 209 | Class<?> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 210 | Intent serviceIntent = new Intent( |
| 211 | context, ContactSaveService.class); |
| 212 | serviceIntent.setAction(ContactSaveService.ACTION_NEW_RAW_CONTACT); |
| 213 | if (account != null) { |
| 214 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_NAME, account.name); |
| 215 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_TYPE, account.type); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 216 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_SET, account.dataSet); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 217 | } |
| 218 | serviceIntent.putParcelableArrayListExtra( |
| 219 | ContactSaveService.EXTRA_CONTENT_VALUES, values); |
| 220 | |
| 221 | // Callback intent will be invoked by the service once the new contact is |
| 222 | // created. The service will put the URI of the new contact as "data" on |
| 223 | // the callback intent. |
| 224 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 225 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 226 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 227 | return serviceIntent; |
| 228 | } |
| 229 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 230 | private void createRawContact(Intent intent) { |
| 231 | String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME); |
| 232 | String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 233 | String dataSet = intent.getStringExtra(EXTRA_DATA_SET); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 234 | List<ContentValues> valueList = intent.getParcelableArrayListExtra(EXTRA_CONTENT_VALUES); |
| 235 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 236 | |
| 237 | ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
| 238 | operations.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) |
| 239 | .withValue(RawContacts.ACCOUNT_NAME, accountName) |
| 240 | .withValue(RawContacts.ACCOUNT_TYPE, accountType) |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 241 | .withValue(RawContacts.DATA_SET, dataSet) |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 242 | .build()); |
| 243 | |
| 244 | int size = valueList.size(); |
| 245 | for (int i = 0; i < size; i++) { |
| 246 | ContentValues values = valueList.get(i); |
| 247 | values.keySet().retainAll(ALLOWED_DATA_COLUMNS); |
| 248 | operations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) |
| 249 | .withValueBackReference(Data.RAW_CONTACT_ID, 0) |
| 250 | .withValues(values) |
| 251 | .build()); |
| 252 | } |
| 253 | |
| 254 | ContentResolver resolver = getContentResolver(); |
| 255 | ContentProviderResult[] results; |
| 256 | try { |
| 257 | results = resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
| 258 | } catch (Exception e) { |
| 259 | throw new RuntimeException("Failed to store new contact", e); |
| 260 | } |
| 261 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 262 | Uri rawContactUri = results[0].uri; |
| 263 | callbackIntent.setData(RawContacts.getContactLookupUri(resolver, rawContactUri)); |
| 264 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 265 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 268 | /** |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 269 | * Creates an intent that can be sent to this service to create a new raw contact |
| 270 | * using data presented as a set of ContentValues. |
| 271 | */ |
| 272 | public static Intent createSaveContactIntent(Context context, EntityDeltaList state, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 273 | String saveModeExtraKey, int saveMode, boolean isProfile, Class<?> callbackActivity, |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 274 | String callbackAction) { |
| 275 | Intent serviceIntent = new Intent( |
| 276 | context, ContactSaveService.class); |
| 277 | serviceIntent.setAction(ContactSaveService.ACTION_SAVE_CONTACT); |
| 278 | serviceIntent.putExtra(EXTRA_CONTACT_STATE, (Parcelable) state); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 279 | serviceIntent.putExtra(EXTRA_SAVE_IS_PROFILE, isProfile); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 280 | |
| 281 | // Callback intent will be invoked by the service once the contact is |
| 282 | // saved. The service will put the URI of the new contact as "data" on |
| 283 | // the callback intent. |
| 284 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 285 | callbackIntent.putExtra(saveModeExtraKey, saveMode); |
| 286 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 287 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 288 | return serviceIntent; |
| 289 | } |
| 290 | |
| 291 | private void saveContact(Intent intent) { |
| 292 | EntityDeltaList state = intent.getParcelableExtra(EXTRA_CONTACT_STATE); |
| 293 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 294 | boolean isProfile = intent.getBooleanExtra(EXTRA_SAVE_IS_PROFILE, false); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 295 | |
| 296 | // Trim any empty fields, and RawContacts, before persisting |
| 297 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this); |
| 298 | EntityModifier.trimEmpty(state, accountTypes); |
| 299 | |
| 300 | Uri lookupUri = null; |
| 301 | |
| 302 | final ContentResolver resolver = getContentResolver(); |
| 303 | |
| 304 | // Attempt to persist changes |
| 305 | int tries = 0; |
| 306 | while (tries++ < PERSIST_TRIES) { |
| 307 | try { |
| 308 | // Build operations and try applying |
| 309 | final ArrayList<ContentProviderOperation> diff = state.buildDiff(); |
Katherine Kuan | a007e44 | 2011-07-07 09:25:34 -0700 | [diff] [blame] | 310 | if (DEBUG) { |
| 311 | Log.v(TAG, "Content Provider Operations:"); |
| 312 | for (ContentProviderOperation operation : diff) { |
| 313 | Log.v(TAG, operation.toString()); |
| 314 | } |
| 315 | } |
| 316 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 317 | ContentProviderResult[] results = null; |
| 318 | if (!diff.isEmpty()) { |
| 319 | results = resolver.applyBatch(ContactsContract.AUTHORITY, diff); |
| 320 | } |
| 321 | |
| 322 | final long rawContactId = getRawContactId(state, diff, results); |
| 323 | if (rawContactId == -1) { |
| 324 | throw new IllegalStateException("Could not determine RawContact ID after save"); |
| 325 | } |
| 326 | final Uri rawContactUri = ContentUris.withAppendedId( |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 327 | isProfile ? Profile.CONTENT_RAW_CONTACTS_URI : RawContacts.CONTENT_URI, |
| 328 | rawContactId); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 329 | lookupUri = RawContacts.getContactLookupUri(resolver, rawContactUri); |
| 330 | Log.v(TAG, "Saved contact. New URI: " + lookupUri); |
| 331 | break; |
| 332 | |
| 333 | } catch (RemoteException e) { |
| 334 | // Something went wrong, bail without success |
| 335 | Log.e(TAG, "Problem persisting user edits", e); |
| 336 | break; |
| 337 | |
| 338 | } catch (OperationApplicationException e) { |
| 339 | // Version consistency failed, re-parent change and try again |
| 340 | Log.w(TAG, "Version consistency failed, re-parenting: " + e.toString()); |
| 341 | final StringBuilder sb = new StringBuilder(RawContacts._ID + " IN("); |
| 342 | boolean first = true; |
| 343 | final int count = state.size(); |
| 344 | for (int i = 0; i < count; i++) { |
| 345 | Long rawContactId = state.getRawContactId(i); |
| 346 | if (rawContactId != null && rawContactId != -1) { |
| 347 | if (!first) { |
| 348 | sb.append(','); |
| 349 | } |
| 350 | sb.append(rawContactId); |
| 351 | first = false; |
| 352 | } |
| 353 | } |
| 354 | sb.append(")"); |
| 355 | |
| 356 | if (first) { |
| 357 | throw new IllegalStateException("Version consistency failed for a new contact"); |
| 358 | } |
| 359 | |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame^] | 360 | final EntityDeltaList newState = EntityDeltaList.fromQuery( |
| 361 | isProfile |
| 362 | ? RawContactsEntity.PROFILE_CONTENT_URI |
| 363 | : RawContactsEntity.CONTENT_URI, |
| 364 | resolver, sb.toString(), null, null); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 365 | state = EntityDeltaList.mergeAfter(newState, state); |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame^] | 366 | |
| 367 | // Update the new state to use profile URIs if appropriate. |
| 368 | if (isProfile) { |
| 369 | for (EntityDelta delta : state) { |
| 370 | delta.setProfileQueryUri(); |
| 371 | } |
| 372 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | callbackIntent.setData(lookupUri); |
| 377 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 378 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | private long getRawContactId(EntityDeltaList state, |
| 382 | final ArrayList<ContentProviderOperation> diff, |
| 383 | final ContentProviderResult[] results) { |
| 384 | long rawContactId = state.findRawContactId(); |
| 385 | if (rawContactId != -1) { |
| 386 | return rawContactId; |
| 387 | } |
| 388 | |
| 389 | final int diffSize = diff.size(); |
| 390 | for (int i = 0; i < diffSize; i++) { |
| 391 | ContentProviderOperation operation = diff.get(i); |
| 392 | if (operation.getType() == ContentProviderOperation.TYPE_INSERT |
| 393 | && operation.getUri().getEncodedPath().contains( |
| 394 | RawContacts.CONTENT_URI.getEncodedPath())) { |
| 395 | return ContentUris.parseId(results[i].uri); |
| 396 | } |
| 397 | } |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | /** |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 402 | * Creates an intent that can be sent to this service to create a new group as |
| 403 | * well as add new members at the same time. |
| 404 | * |
| 405 | * @param context of the application |
| 406 | * @param account in which the group should be created |
| 407 | * @param label is the name of the group (cannot be null) |
| 408 | * @param rawContactsToAdd is an array of raw contact IDs for contacts that |
| 409 | * should be added to the group |
| 410 | * @param callbackActivity is the activity to send the callback intent to |
| 411 | * @param callbackAction is the intent action for the callback intent |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 412 | */ |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 413 | public static Intent createNewGroupIntent(Context context, AccountWithDataSet account, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 414 | String label, long[] rawContactsToAdd, Class<?> callbackActivity, |
| 415 | String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 416 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 417 | serviceIntent.setAction(ContactSaveService.ACTION_CREATE_GROUP); |
| 418 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_TYPE, account.type); |
| 419 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_NAME, account.name); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 420 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_SET, account.dataSet); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 421 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, label); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 422 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_ADD, rawContactsToAdd); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 423 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 424 | // Callback intent will be invoked by the service once the new group is |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 425 | // created. |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 426 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 427 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 428 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 429 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 430 | return serviceIntent; |
| 431 | } |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 432 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 433 | private void createGroup(Intent intent) { |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 434 | String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); |
| 435 | String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME); |
| 436 | String dataSet = intent.getStringExtra(EXTRA_DATA_SET); |
| 437 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 438 | final long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 439 | |
| 440 | ContentValues values = new ContentValues(); |
| 441 | values.put(Groups.ACCOUNT_TYPE, accountType); |
| 442 | values.put(Groups.ACCOUNT_NAME, accountName); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 443 | values.put(Groups.DATA_SET, dataSet); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 444 | values.put(Groups.TITLE, label); |
| 445 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 446 | final ContentResolver resolver = getContentResolver(); |
| 447 | |
| 448 | // Create the new group |
| 449 | final Uri groupUri = resolver.insert(Groups.CONTENT_URI, values); |
| 450 | |
| 451 | // If there's no URI, then the insertion failed. Abort early because group members can't be |
| 452 | // added if the group doesn't exist |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 453 | if (groupUri == null) { |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 454 | Log.e(TAG, "Couldn't create group with label " + label); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 458 | // Add new group members |
| 459 | addMembersToGroup(resolver, rawContactsToAdd, ContentUris.parseId(groupUri)); |
| 460 | |
| 461 | // TODO: Move this into the contact editor where it belongs. This needs to be integrated |
| 462 | // with the way other intent extras that are passed to the {@link ContactEditorActivity}. |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 463 | values.clear(); |
| 464 | values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); |
| 465 | values.put(GroupMembership.GROUP_ROW_ID, ContentUris.parseId(groupUri)); |
| 466 | |
| 467 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 468 | callbackIntent.setData(groupUri); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 469 | // TODO: This can be taken out when the above TODO is addressed |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 470 | callbackIntent.putExtra(ContactsContract.Intents.Insert.DATA, Lists.newArrayList(values)); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 471 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 475 | * Creates an intent that can be sent to this service to rename a group. |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 476 | */ |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 477 | public static Intent createGroupRenameIntent(Context context, long groupId, String newLabel, |
| 478 | Class<?> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 479 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 480 | serviceIntent.setAction(ContactSaveService.ACTION_RENAME_GROUP); |
| 481 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
| 482 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, newLabel); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 483 | |
| 484 | // Callback intent will be invoked by the service once the group is renamed. |
| 485 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 486 | callbackIntent.setAction(callbackAction); |
| 487 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 488 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 489 | return serviceIntent; |
| 490 | } |
| 491 | |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 492 | private void renameGroup(Intent intent) { |
| 493 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 494 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
| 495 | |
| 496 | if (groupId == -1) { |
| 497 | Log.e(TAG, "Invalid arguments for renameGroup request"); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | ContentValues values = new ContentValues(); |
| 502 | values.put(Groups.TITLE, label); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 503 | final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); |
| 504 | getContentResolver().update(groupUri, values, null, null); |
| 505 | |
| 506 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 507 | callbackIntent.setData(groupUri); |
| 508 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 512 | * Creates an intent that can be sent to this service to delete a group. |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 513 | */ |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 514 | public static Intent createGroupDeletionIntent(Context context, long groupId) { |
| 515 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 516 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_GROUP); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 517 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 518 | return serviceIntent; |
| 519 | } |
| 520 | |
| 521 | private void deleteGroup(Intent intent) { |
| 522 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 523 | if (groupId == -1) { |
| 524 | Log.e(TAG, "Invalid arguments for deleteGroup request"); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | getContentResolver().delete( |
| 529 | ContentUris.withAppendedId(Groups.CONTENT_URI, groupId), null, null); |
| 530 | } |
| 531 | |
| 532 | /** |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 533 | * Creates an intent that can be sent to this service to rename a group as |
| 534 | * well as add and remove members from the group. |
| 535 | * |
| 536 | * @param context of the application |
| 537 | * @param groupId of the group that should be modified |
| 538 | * @param newLabel is the updated name of the group (can be null if the name |
| 539 | * should not be updated) |
| 540 | * @param rawContactsToAdd is an array of raw contact IDs for contacts that |
| 541 | * should be added to the group |
| 542 | * @param rawContactsToRemove is an array of raw contact IDs for contacts |
| 543 | * that should be removed from the group |
| 544 | * @param callbackActivity is the activity to send the callback intent to |
| 545 | * @param callbackAction is the intent action for the callback intent |
| 546 | */ |
| 547 | public static Intent createGroupUpdateIntent(Context context, long groupId, String newLabel, |
| 548 | long[] rawContactsToAdd, long[] rawContactsToRemove, |
| 549 | Class<?> callbackActivity, String callbackAction) { |
| 550 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 551 | serviceIntent.setAction(ContactSaveService.ACTION_UPDATE_GROUP); |
| 552 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
| 553 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, newLabel); |
| 554 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_ADD, rawContactsToAdd); |
| 555 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_REMOVE, |
| 556 | rawContactsToRemove); |
| 557 | |
| 558 | // Callback intent will be invoked by the service once the group is updated |
| 559 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 560 | callbackIntent.setAction(callbackAction); |
| 561 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 562 | |
| 563 | return serviceIntent; |
| 564 | } |
| 565 | |
| 566 | private void updateGroup(Intent intent) { |
| 567 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 568 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
| 569 | long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD); |
| 570 | long[] rawContactsToRemove = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_REMOVE); |
| 571 | |
| 572 | if (groupId == -1) { |
| 573 | Log.e(TAG, "Invalid arguments for updateGroup request"); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | final ContentResolver resolver = getContentResolver(); |
| 578 | final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); |
| 579 | |
| 580 | // Update group name if necessary |
| 581 | if (label != null) { |
| 582 | ContentValues values = new ContentValues(); |
| 583 | values.put(Groups.TITLE, label); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 584 | resolver.update(groupUri, values, null, null); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 587 | // Add and remove members if necessary |
| 588 | addMembersToGroup(resolver, rawContactsToAdd, groupId); |
| 589 | removeMembersFromGroup(resolver, rawContactsToRemove, groupId); |
| 590 | |
| 591 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 592 | callbackIntent.setData(groupUri); |
| 593 | deliverCallback(callbackIntent); |
| 594 | } |
| 595 | |
| 596 | private void addMembersToGroup(ContentResolver resolver, long[] rawContactsToAdd, |
| 597 | long groupId) { |
| 598 | if (rawContactsToAdd == null) { |
| 599 | return; |
| 600 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 601 | for (long rawContactId : rawContactsToAdd) { |
| 602 | try { |
| 603 | final ArrayList<ContentProviderOperation> rawContactOperations = |
| 604 | new ArrayList<ContentProviderOperation>(); |
| 605 | |
| 606 | // Build an assert operation to ensure the contact is not already in the group |
| 607 | final ContentProviderOperation.Builder assertBuilder = ContentProviderOperation |
| 608 | .newAssertQuery(Data.CONTENT_URI); |
| 609 | assertBuilder.withSelection(Data.RAW_CONTACT_ID + "=? AND " + |
| 610 | Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", |
| 611 | new String[] { String.valueOf(rawContactId), |
| 612 | GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); |
| 613 | assertBuilder.withExpectedCount(0); |
| 614 | rawContactOperations.add(assertBuilder.build()); |
| 615 | |
| 616 | // Build an insert operation to add the contact to the group |
| 617 | final ContentProviderOperation.Builder insertBuilder = ContentProviderOperation |
| 618 | .newInsert(Data.CONTENT_URI); |
| 619 | insertBuilder.withValue(Data.RAW_CONTACT_ID, rawContactId); |
| 620 | insertBuilder.withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); |
| 621 | insertBuilder.withValue(GroupMembership.GROUP_ROW_ID, groupId); |
| 622 | rawContactOperations.add(insertBuilder.build()); |
| 623 | |
| 624 | if (DEBUG) { |
| 625 | for (ContentProviderOperation operation : rawContactOperations) { |
| 626 | Log.v(TAG, operation.toString()); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // Apply batch |
| 631 | ContentProviderResult[] results = null; |
| 632 | if (!rawContactOperations.isEmpty()) { |
| 633 | results = resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations); |
| 634 | } |
| 635 | } catch (RemoteException e) { |
| 636 | // Something went wrong, bail without success |
| 637 | Log.e(TAG, "Problem persisting user edits for raw contact ID " + |
| 638 | String.valueOf(rawContactId), e); |
| 639 | } catch (OperationApplicationException e) { |
| 640 | // The assert could have failed because the contact is already in the group, |
| 641 | // just continue to the next contact |
| 642 | Log.w(TAG, "Assert failed in adding raw contact ID " + |
| 643 | String.valueOf(rawContactId) + ". Already exists in group " + |
| 644 | String.valueOf(groupId), e); |
| 645 | } |
| 646 | } |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 647 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 648 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 649 | private void removeMembersFromGroup(ContentResolver resolver, long[] rawContactsToRemove, |
| 650 | long groupId) { |
| 651 | if (rawContactsToRemove == null) { |
| 652 | return; |
| 653 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 654 | for (long rawContactId : rawContactsToRemove) { |
| 655 | // Apply the delete operation on the data row for the given raw contact's |
| 656 | // membership in the given group. If no contact matches the provided selection, then |
| 657 | // nothing will be done. Just continue to the next contact. |
| 658 | getContentResolver().delete(Data.CONTENT_URI, Data.RAW_CONTACT_ID + "=? AND " + |
| 659 | Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", |
| 660 | new String[] { String.valueOf(rawContactId), |
| 661 | GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); |
| 662 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 666 | * Creates an intent that can be sent to this service to star or un-star a contact. |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 667 | */ |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 668 | public static Intent createSetStarredIntent(Context context, Uri contactUri, boolean value) { |
| 669 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 670 | serviceIntent.setAction(ContactSaveService.ACTION_SET_STARRED); |
| 671 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 672 | serviceIntent.putExtra(ContactSaveService.EXTRA_STARRED_FLAG, value); |
| 673 | |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 674 | return serviceIntent; |
| 675 | } |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 676 | |
| 677 | private void setStarred(Intent intent) { |
| 678 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 679 | boolean value = intent.getBooleanExtra(EXTRA_STARRED_FLAG, false); |
| 680 | if (contactUri == null) { |
| 681 | Log.e(TAG, "Invalid arguments for setStarred request"); |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | final ContentValues values = new ContentValues(1); |
| 686 | values.put(Contacts.STARRED, value); |
| 687 | getContentResolver().update(contactUri, values, null, null); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 688 | } |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 689 | |
| 690 | /** |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 691 | * Creates an intent that can be sent to this service to set the redirect to voicemail. |
| 692 | */ |
| 693 | public static Intent createSetSendToVoicemail(Context context, Uri contactUri, |
| 694 | boolean value) { |
| 695 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 696 | serviceIntent.setAction(ContactSaveService.ACTION_SET_SEND_TO_VOICEMAIL); |
| 697 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 698 | serviceIntent.putExtra(ContactSaveService.EXTRA_SEND_TO_VOICEMAIL_FLAG, value); |
| 699 | |
| 700 | return serviceIntent; |
| 701 | } |
| 702 | |
| 703 | private void setSendToVoicemail(Intent intent) { |
| 704 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 705 | boolean value = intent.getBooleanExtra(EXTRA_SEND_TO_VOICEMAIL_FLAG, false); |
| 706 | if (contactUri == null) { |
| 707 | Log.e(TAG, "Invalid arguments for setRedirectToVoicemail"); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | final ContentValues values = new ContentValues(1); |
| 712 | values.put(Contacts.SEND_TO_VOICEMAIL, value); |
| 713 | getContentResolver().update(contactUri, values, null, null); |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Creates an intent that can be sent to this service to save the contact's ringtone. |
| 718 | */ |
| 719 | public static Intent createSetRingtone(Context context, Uri contactUri, |
| 720 | String value) { |
| 721 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 722 | serviceIntent.setAction(ContactSaveService.ACTION_SET_RINGTONE); |
| 723 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 724 | serviceIntent.putExtra(ContactSaveService.EXTRA_CUSTOM_RINGTONE, value); |
| 725 | |
| 726 | return serviceIntent; |
| 727 | } |
| 728 | |
| 729 | private void setRingtone(Intent intent) { |
| 730 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 731 | String value = intent.getStringExtra(EXTRA_CUSTOM_RINGTONE); |
| 732 | if (contactUri == null) { |
| 733 | Log.e(TAG, "Invalid arguments for setRingtone"); |
| 734 | return; |
| 735 | } |
| 736 | ContentValues values = new ContentValues(1); |
| 737 | values.put(Contacts.CUSTOM_RINGTONE, value); |
| 738 | getContentResolver().update(contactUri, values, null, null); |
| 739 | } |
| 740 | |
| 741 | /** |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 742 | * Creates an intent that sets the selected data item as super primary (default) |
| 743 | */ |
| 744 | public static Intent createSetSuperPrimaryIntent(Context context, long dataId) { |
| 745 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 746 | serviceIntent.setAction(ContactSaveService.ACTION_SET_SUPER_PRIMARY); |
| 747 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_ID, dataId); |
| 748 | return serviceIntent; |
| 749 | } |
| 750 | |
| 751 | private void setSuperPrimary(Intent intent) { |
| 752 | long dataId = intent.getLongExtra(EXTRA_DATA_ID, -1); |
| 753 | if (dataId == -1) { |
| 754 | Log.e(TAG, "Invalid arguments for setSuperPrimary request"); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | // Update the primary values in the data record. |
| 759 | ContentValues values = new ContentValues(1); |
| 760 | values.put(Data.IS_SUPER_PRIMARY, 1); |
| 761 | values.put(Data.IS_PRIMARY, 1); |
| 762 | |
| 763 | getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, dataId), |
| 764 | values, null, null); |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Creates an intent that clears the primary flag of all data items that belong to the same |
| 769 | * raw_contact as the given data item. Will only clear, if the data item was primary before |
| 770 | * this call |
| 771 | */ |
| 772 | public static Intent createClearPrimaryIntent(Context context, long dataId) { |
| 773 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 774 | serviceIntent.setAction(ContactSaveService.ACTION_CLEAR_PRIMARY); |
| 775 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_ID, dataId); |
| 776 | return serviceIntent; |
| 777 | } |
| 778 | |
| 779 | private void clearPrimary(Intent intent) { |
| 780 | long dataId = intent.getLongExtra(EXTRA_DATA_ID, -1); |
| 781 | if (dataId == -1) { |
| 782 | Log.e(TAG, "Invalid arguments for clearPrimary request"); |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | // Update the primary values in the data record. |
| 787 | ContentValues values = new ContentValues(1); |
| 788 | values.put(Data.IS_SUPER_PRIMARY, 0); |
| 789 | values.put(Data.IS_PRIMARY, 0); |
| 790 | |
| 791 | getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, dataId), |
| 792 | values, null, null); |
| 793 | } |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 794 | |
| 795 | /** |
| 796 | * Creates an intent that can be sent to this service to delete a contact. |
| 797 | */ |
| 798 | public static Intent createDeleteContactIntent(Context context, Uri contactUri) { |
| 799 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 800 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_CONTACT); |
| 801 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 802 | return serviceIntent; |
| 803 | } |
| 804 | |
| 805 | private void deleteContact(Intent intent) { |
| 806 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 807 | if (contactUri == null) { |
| 808 | Log.e(TAG, "Invalid arguments for deleteContact request"); |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | getContentResolver().delete(contactUri, null, null); |
| 813 | } |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 814 | |
| 815 | /** |
| 816 | * Creates an intent that can be sent to this service to join two contacts. |
| 817 | */ |
| 818 | public static Intent createJoinContactsIntent(Context context, long contactId1, |
| 819 | long contactId2, boolean contactWritable, |
| 820 | Class<?> callbackActivity, String callbackAction) { |
| 821 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 822 | serviceIntent.setAction(ContactSaveService.ACTION_JOIN_CONTACTS); |
| 823 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_ID1, contactId1); |
| 824 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_ID2, contactId2); |
| 825 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_WRITABLE, contactWritable); |
| 826 | |
| 827 | // Callback intent will be invoked by the service once the contacts are joined. |
| 828 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 829 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 830 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 831 | |
| 832 | return serviceIntent; |
| 833 | } |
| 834 | |
| 835 | |
| 836 | private interface JoinContactQuery { |
| 837 | String[] PROJECTION = { |
| 838 | RawContacts._ID, |
| 839 | RawContacts.CONTACT_ID, |
| 840 | RawContacts.NAME_VERIFIED, |
| 841 | RawContacts.DISPLAY_NAME_SOURCE, |
| 842 | }; |
| 843 | |
| 844 | String SELECTION = RawContacts.CONTACT_ID + "=? OR " + RawContacts.CONTACT_ID + "=?"; |
| 845 | |
| 846 | int _ID = 0; |
| 847 | int CONTACT_ID = 1; |
| 848 | int NAME_VERIFIED = 2; |
| 849 | int DISPLAY_NAME_SOURCE = 3; |
| 850 | } |
| 851 | |
| 852 | private void joinContacts(Intent intent) { |
| 853 | long contactId1 = intent.getLongExtra(EXTRA_CONTACT_ID1, -1); |
| 854 | long contactId2 = intent.getLongExtra(EXTRA_CONTACT_ID2, -1); |
| 855 | boolean writable = intent.getBooleanExtra(EXTRA_CONTACT_WRITABLE, false); |
| 856 | if (contactId1 == -1 || contactId2 == -1) { |
| 857 | Log.e(TAG, "Invalid arguments for joinContacts request"); |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | final ContentResolver resolver = getContentResolver(); |
| 862 | |
| 863 | // Load raw contact IDs for all raw contacts involved - currently edited and selected |
| 864 | // in the join UIs |
| 865 | Cursor c = resolver.query(RawContacts.CONTENT_URI, |
| 866 | JoinContactQuery.PROJECTION, |
| 867 | JoinContactQuery.SELECTION, |
| 868 | new String[]{String.valueOf(contactId1), String.valueOf(contactId2)}, null); |
| 869 | |
| 870 | long rawContactIds[]; |
| 871 | long verifiedNameRawContactId = -1; |
| 872 | try { |
| 873 | int maxDisplayNameSource = -1; |
| 874 | rawContactIds = new long[c.getCount()]; |
| 875 | for (int i = 0; i < rawContactIds.length; i++) { |
| 876 | c.moveToPosition(i); |
| 877 | long rawContactId = c.getLong(JoinContactQuery._ID); |
| 878 | rawContactIds[i] = rawContactId; |
| 879 | int nameSource = c.getInt(JoinContactQuery.DISPLAY_NAME_SOURCE); |
| 880 | if (nameSource > maxDisplayNameSource) { |
| 881 | maxDisplayNameSource = nameSource; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | // Find an appropriate display name for the joined contact: |
| 886 | // if should have a higher DisplayNameSource or be the name |
| 887 | // of the original contact that we are joining with another. |
| 888 | if (writable) { |
| 889 | for (int i = 0; i < rawContactIds.length; i++) { |
| 890 | c.moveToPosition(i); |
| 891 | if (c.getLong(JoinContactQuery.CONTACT_ID) == contactId1) { |
| 892 | int nameSource = c.getInt(JoinContactQuery.DISPLAY_NAME_SOURCE); |
| 893 | if (nameSource == maxDisplayNameSource |
| 894 | && (verifiedNameRawContactId == -1 |
| 895 | || c.getInt(JoinContactQuery.NAME_VERIFIED) != 0)) { |
| 896 | verifiedNameRawContactId = c.getLong(JoinContactQuery._ID); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | } finally { |
| 902 | c.close(); |
| 903 | } |
| 904 | |
| 905 | // For each pair of raw contacts, insert an aggregation exception |
| 906 | ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
| 907 | for (int i = 0; i < rawContactIds.length; i++) { |
| 908 | for (int j = 0; j < rawContactIds.length; j++) { |
| 909 | if (i != j) { |
| 910 | buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | // Mark the original contact as "name verified" to make sure that the contact |
| 916 | // display name does not change as a result of the join |
| 917 | if (verifiedNameRawContactId != -1) { |
| 918 | Builder builder = ContentProviderOperation.newUpdate( |
| 919 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, verifiedNameRawContactId)); |
| 920 | builder.withValue(RawContacts.NAME_VERIFIED, 1); |
| 921 | operations.add(builder.build()); |
| 922 | } |
| 923 | |
| 924 | boolean success = false; |
| 925 | // Apply all aggregation exceptions as one batch |
| 926 | try { |
| 927 | resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 928 | showToast(R.string.contactsJoinedMessage); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 929 | success = true; |
| 930 | } catch (RemoteException e) { |
| 931 | Log.e(TAG, "Failed to apply aggregation exception batch", e); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 932 | showToast(R.string.contactSavedErrorToast); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 933 | } catch (OperationApplicationException e) { |
| 934 | Log.e(TAG, "Failed to apply aggregation exception batch", e); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 935 | showToast(R.string.contactSavedErrorToast); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 939 | if (success) { |
| 940 | Uri uri = RawContacts.getContactLookupUri(resolver, |
| 941 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactIds[0])); |
| 942 | callbackIntent.setData(uri); |
| 943 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 944 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /** |
| 948 | * Construct a {@link AggregationExceptions#TYPE_KEEP_TOGETHER} ContentProviderOperation. |
| 949 | */ |
| 950 | private void buildJoinContactDiff(ArrayList<ContentProviderOperation> operations, |
| 951 | long rawContactId1, long rawContactId2) { |
| 952 | Builder builder = |
| 953 | ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI); |
| 954 | builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER); |
| 955 | builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1); |
| 956 | builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2); |
| 957 | operations.add(builder.build()); |
| 958 | } |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 959 | |
| 960 | /** |
| 961 | * Shows a toast on the UI thread. |
| 962 | */ |
| 963 | private void showToast(final int message) { |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 964 | mMainHandler.post(new Runnable() { |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 965 | |
| 966 | @Override |
| 967 | public void run() { |
| 968 | Toast.makeText(ContactSaveService.this, message, Toast.LENGTH_LONG).show(); |
| 969 | } |
| 970 | }); |
| 971 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 972 | |
| 973 | private void deliverCallback(final Intent callbackIntent) { |
| 974 | mMainHandler.post(new Runnable() { |
| 975 | |
| 976 | @Override |
| 977 | public void run() { |
| 978 | deliverCallbackOnUiThread(callbackIntent); |
| 979 | } |
| 980 | }); |
| 981 | } |
| 982 | |
| 983 | void deliverCallbackOnUiThread(final Intent callbackIntent) { |
| 984 | // TODO: this assumes that if there are multiple instances of the same |
| 985 | // activity registered, the last one registered is the one waiting for |
| 986 | // the callback. Validity of this assumption needs to be verified. |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 987 | for (Listener listener : sListeners) { |
| 988 | if (callbackIntent.getComponent().equals( |
| 989 | ((Activity) listener).getIntent().getComponent())) { |
| 990 | listener.onServiceCompleted(callbackIntent); |
| 991 | return; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | } |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 995 | } |