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