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 | |
Jay Shrauner | 615ed9c | 2015-07-29 11:27:56 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.WRITE_CONTACTS; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 20 | import android.app.Activity; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 21 | import android.app.IntentService; |
| 22 | import android.content.ContentProviderOperation; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 23 | import android.content.ContentProviderOperation.Builder; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 24 | import android.content.ContentProviderResult; |
| 25 | import android.content.ContentResolver; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 26 | import android.content.ContentUris; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 27 | import android.content.ContentValues; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 28 | import android.content.Context; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 30 | import android.content.OperationApplicationException; |
| 31 | import android.database.Cursor; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
Daniel Lehmann | c42ea4e | 2012-02-16 21:22:37 -0800 | [diff] [blame] | 33 | import android.os.Bundle; |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.Looper; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 36 | import android.os.Parcelable; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 37 | import android.os.RemoteException; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 38 | import android.provider.ContactsContract; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 39 | import android.provider.ContactsContract.AggregationExceptions; |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 40 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 41 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 42 | import android.provider.ContactsContract.Contacts; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 43 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 44 | import android.provider.ContactsContract.Groups; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 45 | import android.provider.ContactsContract.Profile; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 46 | import android.provider.ContactsContract.RawContacts; |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 47 | import android.provider.ContactsContract.RawContactsEntity; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 48 | import android.util.Log; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 49 | import android.widget.Toast; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 50 | |
Wenyi Wang | dd7d456 | 2015-12-08 13:33:43 -0800 | [diff] [blame] | 51 | import com.android.contacts.activities.ContactEditorBaseActivity; |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 52 | import com.android.contacts.common.compat.CompatUtils; |
Chiao Cheng | d7ca03e | 2012-10-24 15:14:08 -0700 | [diff] [blame] | 53 | import com.android.contacts.common.database.ContactUpdateUtils; |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 54 | import com.android.contacts.common.model.AccountTypeManager; |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 55 | import com.android.contacts.common.model.CPOWrapper; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 56 | import com.android.contacts.common.model.RawContactDelta; |
| 57 | import com.android.contacts.common.model.RawContactDeltaList; |
| 58 | import com.android.contacts.common.model.RawContactModifier; |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 59 | import com.android.contacts.common.model.account.AccountType; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 60 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Jay Shrauner | 615ed9c | 2015-07-29 11:27:56 -0700 | [diff] [blame] | 61 | import com.android.contacts.common.util.PermissionsUtil; |
Wenyi Wang | aac0e66 | 2015-12-18 17:17:33 -0800 | [diff] [blame] | 62 | import com.android.contacts.compat.PinnedPositionsCompat; |
Walter Jang | 0ae4a93 | 2016-02-19 15:01:32 -0800 | [diff] [blame] | 63 | import com.android.contacts.activities.ContactEditorBaseActivity.ContactEditor.SaveMode; |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 64 | import com.android.contacts.util.ContactPhotoUtils; |
| 65 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 66 | import com.google.common.collect.Lists; |
| 67 | import com.google.common.collect.Sets; |
| 68 | |
Daniel Lehmann | c42ea4e | 2012-02-16 21:22:37 -0800 | [diff] [blame] | 69 | import java.util.ArrayList; |
| 70 | import java.util.HashSet; |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 71 | import java.util.Iterator; |
Daniel Lehmann | c42ea4e | 2012-02-16 21:22:37 -0800 | [diff] [blame] | 72 | import java.util.List; |
| 73 | import java.util.concurrent.CopyOnWriteArrayList; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 74 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 75 | /** |
| 76 | * A service responsible for saving changes to the content provider. |
| 77 | */ |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 78 | public class ContactSaveService extends IntentService { |
| 79 | private static final String TAG = "ContactSaveService"; |
| 80 | |
Katherine Kuan | a007e44 | 2011-07-07 09:25:34 -0700 | [diff] [blame] | 81 | /** Set to true in order to view logs on content provider operations */ |
| 82 | private static final boolean DEBUG = false; |
| 83 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 84 | public static final String ACTION_NEW_RAW_CONTACT = "newRawContact"; |
| 85 | |
| 86 | public static final String EXTRA_ACCOUNT_NAME = "accountName"; |
| 87 | public static final String EXTRA_ACCOUNT_TYPE = "accountType"; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 88 | public static final String EXTRA_DATA_SET = "dataSet"; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 89 | public static final String EXTRA_CONTENT_VALUES = "contentValues"; |
| 90 | public static final String EXTRA_CALLBACK_INTENT = "callbackIntent"; |
| 91 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 92 | public static final String ACTION_SAVE_CONTACT = "saveContact"; |
| 93 | public static final String EXTRA_CONTACT_STATE = "state"; |
| 94 | public static final String EXTRA_SAVE_MODE = "saveMode"; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 95 | public static final String EXTRA_SAVE_IS_PROFILE = "saveIsProfile"; |
Dave Santoro | 36d24d7 | 2011-09-25 17:08:10 -0700 | [diff] [blame] | 96 | public static final String EXTRA_SAVE_SUCCEEDED = "saveSucceeded"; |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 97 | public static final String EXTRA_UPDATED_PHOTOS = "updatedPhotos"; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 98 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 99 | public static final String ACTION_CREATE_GROUP = "createGroup"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 100 | public static final String ACTION_RENAME_GROUP = "renameGroup"; |
| 101 | public static final String ACTION_DELETE_GROUP = "deleteGroup"; |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 102 | public static final String ACTION_UPDATE_GROUP = "updateGroup"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 103 | public static final String EXTRA_GROUP_ID = "groupId"; |
| 104 | public static final String EXTRA_GROUP_LABEL = "groupLabel"; |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 105 | public static final String EXTRA_RAW_CONTACTS_TO_ADD = "rawContactsToAdd"; |
| 106 | public static final String EXTRA_RAW_CONTACTS_TO_REMOVE = "rawContactsToRemove"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 107 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 108 | public static final String ACTION_SET_STARRED = "setStarred"; |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 109 | public static final String ACTION_DELETE_CONTACT = "delete"; |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 110 | public static final String ACTION_DELETE_MULTIPLE_CONTACTS = "deleteMultipleContacts"; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 111 | public static final String EXTRA_CONTACT_URI = "contactUri"; |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 112 | public static final String EXTRA_CONTACT_IDS = "contactIds"; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 113 | public static final String EXTRA_STARRED_FLAG = "starred"; |
| 114 | |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 115 | public static final String ACTION_SET_SUPER_PRIMARY = "setSuperPrimary"; |
| 116 | public static final String ACTION_CLEAR_PRIMARY = "clearPrimary"; |
| 117 | public static final String EXTRA_DATA_ID = "dataId"; |
| 118 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 119 | public static final String ACTION_JOIN_CONTACTS = "joinContacts"; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 120 | public static final String ACTION_JOIN_SEVERAL_CONTACTS = "joinSeveralContacts"; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 121 | public static final String EXTRA_CONTACT_ID1 = "contactId1"; |
| 122 | public static final String EXTRA_CONTACT_ID2 = "contactId2"; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 123 | |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 124 | public static final String ACTION_SET_SEND_TO_VOICEMAIL = "sendToVoicemail"; |
| 125 | public static final String EXTRA_SEND_TO_VOICEMAIL_FLAG = "sendToVoicemailFlag"; |
| 126 | |
| 127 | public static final String ACTION_SET_RINGTONE = "setRingtone"; |
| 128 | public static final String EXTRA_CUSTOM_RINGTONE = "customRingtone"; |
| 129 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 130 | private static final HashSet<String> ALLOWED_DATA_COLUMNS = Sets.newHashSet( |
| 131 | Data.MIMETYPE, |
| 132 | Data.IS_PRIMARY, |
| 133 | Data.DATA1, |
| 134 | Data.DATA2, |
| 135 | Data.DATA3, |
| 136 | Data.DATA4, |
| 137 | Data.DATA5, |
| 138 | Data.DATA6, |
| 139 | Data.DATA7, |
| 140 | Data.DATA8, |
| 141 | Data.DATA9, |
| 142 | Data.DATA10, |
| 143 | Data.DATA11, |
| 144 | Data.DATA12, |
| 145 | Data.DATA13, |
| 146 | Data.DATA14, |
| 147 | Data.DATA15 |
| 148 | ); |
| 149 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 150 | private static final int PERSIST_TRIES = 3; |
| 151 | |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 152 | private static final int MAX_CONTACTS_PROVIDER_BATCH_SIZE = 499; |
| 153 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 154 | public interface Listener { |
| 155 | public void onServiceCompleted(Intent callbackIntent); |
| 156 | } |
| 157 | |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 158 | private static final CopyOnWriteArrayList<Listener> sListeners = |
| 159 | new CopyOnWriteArrayList<Listener>(); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 160 | |
| 161 | private Handler mMainHandler; |
| 162 | |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 163 | public ContactSaveService() { |
| 164 | super(TAG); |
| 165 | setIntentRedelivery(true); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 166 | mMainHandler = new Handler(Looper.getMainLooper()); |
| 167 | } |
| 168 | |
| 169 | public static void registerListener(Listener listener) { |
| 170 | if (!(listener instanceof Activity)) { |
| 171 | throw new ClassCastException("Only activities can be registered to" |
| 172 | + " receive callback from " + ContactSaveService.class.getName()); |
| 173 | } |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 174 | sListeners.add(0, listener); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | public static void unregisterListener(Listener listener) { |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 178 | sListeners.remove(listener); |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Wenyi Wang | dd7d456 | 2015-12-08 13:33:43 -0800 | [diff] [blame] | 181 | /** |
| 182 | * Returns true if the ContactSaveService was started successfully and false if an exception |
| 183 | * was thrown and a Toast error message was displayed. |
| 184 | */ |
| 185 | public static boolean startService(Context context, Intent intent, int saveMode) { |
| 186 | try { |
| 187 | context.startService(intent); |
| 188 | } catch (Exception exception) { |
| 189 | final int resId; |
| 190 | switch (saveMode) { |
| 191 | case ContactEditorBaseActivity.ContactEditor.SaveMode.SPLIT: |
| 192 | resId = R.string.contactUnlinkErrorToast; |
| 193 | break; |
| 194 | case ContactEditorBaseActivity.ContactEditor.SaveMode.RELOAD: |
| 195 | resId = R.string.contactJoinErrorToast; |
| 196 | break; |
| 197 | case ContactEditorBaseActivity.ContactEditor.SaveMode.CLOSE: |
| 198 | resId = R.string.contactSavedErrorToast; |
| 199 | break; |
| 200 | default: |
| 201 | resId = R.string.contactGenericErrorToast; |
| 202 | } |
| 203 | Toast.makeText(context, resId, Toast.LENGTH_SHORT).show(); |
| 204 | return false; |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Utility method that starts service and handles exception. |
| 211 | */ |
| 212 | public static void startService(Context context, Intent intent) { |
| 213 | try { |
| 214 | context.startService(intent); |
| 215 | } catch (Exception exception) { |
| 216 | Toast.makeText(context, R.string.contactGenericErrorToast, Toast.LENGTH_SHORT).show(); |
| 217 | } |
| 218 | } |
| 219 | |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 220 | @Override |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 221 | public Object getSystemService(String name) { |
| 222 | Object service = super.getSystemService(name); |
| 223 | if (service != null) { |
| 224 | return service; |
| 225 | } |
| 226 | |
| 227 | return getApplicationContext().getSystemService(name); |
| 228 | } |
| 229 | |
| 230 | @Override |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 231 | protected void onHandleIntent(Intent intent) { |
Jay Shrauner | 3a7cc76 | 2014-12-01 17:16:33 -0800 | [diff] [blame] | 232 | if (intent == null) { |
| 233 | Log.d(TAG, "onHandleIntent: could not handle null intent"); |
| 234 | return; |
| 235 | } |
Jay Shrauner | 615ed9c | 2015-07-29 11:27:56 -0700 | [diff] [blame] | 236 | if (!PermissionsUtil.hasPermission(this, WRITE_CONTACTS)) { |
| 237 | Log.w(TAG, "No WRITE_CONTACTS permission, unable to write to CP2"); |
| 238 | // TODO: add more specific error string such as "Turn on Contacts |
| 239 | // permission to update your contacts" |
| 240 | showToast(R.string.contactSavedErrorToast); |
| 241 | return; |
| 242 | } |
| 243 | |
Daisuke Miyakawa | 2f21c44 | 2012-03-22 19:12:31 -0700 | [diff] [blame] | 244 | // Call an appropriate method. If we're sure it affects how incoming phone calls are |
| 245 | // handled, then notify the fact to in-call screen. |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 246 | String action = intent.getAction(); |
| 247 | if (ACTION_NEW_RAW_CONTACT.equals(action)) { |
| 248 | createRawContact(intent); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 249 | } else if (ACTION_SAVE_CONTACT.equals(action)) { |
| 250 | saveContact(intent); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 251 | } else if (ACTION_CREATE_GROUP.equals(action)) { |
| 252 | createGroup(intent); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 253 | } else if (ACTION_RENAME_GROUP.equals(action)) { |
| 254 | renameGroup(intent); |
| 255 | } else if (ACTION_DELETE_GROUP.equals(action)) { |
| 256 | deleteGroup(intent); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 257 | } else if (ACTION_UPDATE_GROUP.equals(action)) { |
| 258 | updateGroup(intent); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 259 | } else if (ACTION_SET_STARRED.equals(action)) { |
| 260 | setStarred(intent); |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 261 | } else if (ACTION_SET_SUPER_PRIMARY.equals(action)) { |
| 262 | setSuperPrimary(intent); |
| 263 | } else if (ACTION_CLEAR_PRIMARY.equals(action)) { |
| 264 | clearPrimary(intent); |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 265 | } else if (ACTION_DELETE_MULTIPLE_CONTACTS.equals(action)) { |
| 266 | deleteMultipleContacts(intent); |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 267 | } else if (ACTION_DELETE_CONTACT.equals(action)) { |
| 268 | deleteContact(intent); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 269 | } else if (ACTION_JOIN_CONTACTS.equals(action)) { |
| 270 | joinContacts(intent); |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 271 | } else if (ACTION_JOIN_SEVERAL_CONTACTS.equals(action)) { |
| 272 | joinSeveralContacts(intent); |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 273 | } else if (ACTION_SET_SEND_TO_VOICEMAIL.equals(action)) { |
| 274 | setSendToVoicemail(intent); |
| 275 | } else if (ACTION_SET_RINGTONE.equals(action)) { |
| 276 | setRingtone(intent); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 280 | /** |
| 281 | * Creates an intent that can be sent to this service to create a new raw contact |
| 282 | * using data presented as a set of ContentValues. |
| 283 | */ |
| 284 | public static Intent createNewRawContactIntent(Context context, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 285 | ArrayList<ContentValues> values, AccountWithDataSet account, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 286 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 287 | Intent serviceIntent = new Intent( |
| 288 | context, ContactSaveService.class); |
| 289 | serviceIntent.setAction(ContactSaveService.ACTION_NEW_RAW_CONTACT); |
| 290 | if (account != null) { |
| 291 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_NAME, account.name); |
| 292 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_TYPE, account.type); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 293 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_SET, account.dataSet); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 294 | } |
| 295 | serviceIntent.putParcelableArrayListExtra( |
| 296 | ContactSaveService.EXTRA_CONTENT_VALUES, values); |
| 297 | |
| 298 | // Callback intent will be invoked by the service once the new contact is |
| 299 | // created. The service will put the URI of the new contact as "data" on |
| 300 | // the callback intent. |
| 301 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 302 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 303 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 304 | return serviceIntent; |
| 305 | } |
| 306 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 307 | private void createRawContact(Intent intent) { |
| 308 | String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME); |
| 309 | String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 310 | String dataSet = intent.getStringExtra(EXTRA_DATA_SET); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 311 | List<ContentValues> valueList = intent.getParcelableArrayListExtra(EXTRA_CONTENT_VALUES); |
| 312 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 313 | |
| 314 | ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
| 315 | operations.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) |
| 316 | .withValue(RawContacts.ACCOUNT_NAME, accountName) |
| 317 | .withValue(RawContacts.ACCOUNT_TYPE, accountType) |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 318 | .withValue(RawContacts.DATA_SET, dataSet) |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 319 | .build()); |
| 320 | |
| 321 | int size = valueList.size(); |
| 322 | for (int i = 0; i < size; i++) { |
| 323 | ContentValues values = valueList.get(i); |
| 324 | values.keySet().retainAll(ALLOWED_DATA_COLUMNS); |
| 325 | operations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) |
| 326 | .withValueBackReference(Data.RAW_CONTACT_ID, 0) |
| 327 | .withValues(values) |
| 328 | .build()); |
| 329 | } |
| 330 | |
| 331 | ContentResolver resolver = getContentResolver(); |
| 332 | ContentProviderResult[] results; |
| 333 | try { |
| 334 | results = resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
| 335 | } catch (Exception e) { |
| 336 | throw new RuntimeException("Failed to store new contact", e); |
| 337 | } |
| 338 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 339 | Uri rawContactUri = results[0].uri; |
| 340 | callbackIntent.setData(RawContacts.getContactLookupUri(resolver, rawContactUri)); |
| 341 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 342 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 345 | /** |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 346 | * Creates an intent that can be sent to this service to create a new raw contact |
| 347 | * using data presented as a set of ContentValues. |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 348 | * This variant is more convenient to use when there is only one photo that can |
| 349 | * possibly be updated, as in the Contact Details screen. |
| 350 | * @param rawContactId identifies a writable raw-contact whose photo is to be updated. |
| 351 | * @param updatedPhotoPath denotes a temporary file containing the contact's new photo. |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 352 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 353 | public static Intent createSaveContactIntent(Context context, RawContactDeltaList state, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 354 | String saveModeExtraKey, int saveMode, boolean isProfile, |
| 355 | Class<? extends Activity> callbackActivity, String callbackAction, long rawContactId, |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 356 | Uri updatedPhotoPath) { |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 357 | Bundle bundle = new Bundle(); |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 358 | bundle.putParcelable(String.valueOf(rawContactId), updatedPhotoPath); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 359 | return createSaveContactIntent(context, state, saveModeExtraKey, saveMode, isProfile, |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 360 | callbackActivity, callbackAction, bundle, |
| 361 | /* joinContactIdExtraKey */ null, /* joinContactId */ null); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Creates an intent that can be sent to this service to create a new raw contact |
| 366 | * using data presented as a set of ContentValues. |
| 367 | * This variant is used when multiple contacts' photos may be updated, as in the |
| 368 | * Contact Editor. |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 369 | * |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 370 | * @param updatedPhotos maps each raw-contact's ID to the file-path of the new photo. |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 371 | * @param joinContactIdExtraKey the key used to pass the joinContactId in the callback intent. |
| 372 | * @param joinContactId the raw contact ID to join to the contact after doing the save. |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 373 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 374 | public static Intent createSaveContactIntent(Context context, RawContactDeltaList state, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 375 | String saveModeExtraKey, int saveMode, boolean isProfile, |
| 376 | Class<? extends Activity> callbackActivity, String callbackAction, |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 377 | Bundle updatedPhotos, String joinContactIdExtraKey, Long joinContactId) { |
Walter Jang | 0ae4a93 | 2016-02-19 15:01:32 -0800 | [diff] [blame] | 378 | |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 379 | // Don't pass read-only RawContactDeltas in RawContactDeltaList to contact save service, |
| 380 | // because 1. read-only RawContactDeltas are not writable anyway; 2. read-only |
| 381 | // RawContactDeltas may be problematic, see b/23896510. |
Walter Jang | 0ae4a93 | 2016-02-19 15:01:32 -0800 | [diff] [blame] | 382 | // Except when we must create aggregation exceptions between the raw contacts |
| 383 | if (!(saveMode == SaveMode.JOIN || saveMode == SaveMode.SPLIT)) { |
| 384 | removeReadOnlyContacts(context, state); |
| 385 | } |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 386 | |
| 387 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 388 | serviceIntent.setAction(ContactSaveService.ACTION_SAVE_CONTACT); |
| 389 | serviceIntent.putExtra(EXTRA_CONTACT_STATE, (Parcelable) state); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 390 | serviceIntent.putExtra(EXTRA_SAVE_IS_PROFILE, isProfile); |
benny.lin | 3a4e7a2 | 2014-01-08 10:58:08 +0800 | [diff] [blame] | 391 | serviceIntent.putExtra(EXTRA_SAVE_MODE, saveMode); |
| 392 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 393 | if (updatedPhotos != null) { |
| 394 | serviceIntent.putExtra(EXTRA_UPDATED_PHOTOS, (Parcelable) updatedPhotos); |
| 395 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 396 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 397 | if (callbackActivity != null) { |
| 398 | // Callback intent will be invoked by the service once the contact is |
| 399 | // saved. The service will put the URI of the new contact as "data" on |
| 400 | // the callback intent. |
| 401 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 402 | callbackIntent.putExtra(saveModeExtraKey, saveMode); |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 403 | if (joinContactIdExtraKey != null && joinContactId != null) { |
| 404 | callbackIntent.putExtra(joinContactIdExtraKey, joinContactId); |
| 405 | } |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 406 | callbackIntent.setAction(callbackAction); |
| 407 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 408 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 409 | return serviceIntent; |
| 410 | } |
| 411 | |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 412 | private static void removeReadOnlyContacts(Context context, RawContactDeltaList state) { |
| 413 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 414 | Log.v(TAG, "Before trimming: " + state.size()); |
| 415 | } |
| 416 | int countReadOnly = 0; |
| 417 | final Iterator<RawContactDelta> iterator = state.iterator(); |
| 418 | while (iterator.hasNext()) { |
| 419 | final RawContactDelta rawContactDelta = iterator.next(); |
| 420 | final AccountType accountType = rawContactDelta.getRawContactAccountType(context); |
| 421 | if (accountType != null && !accountType.areContactsWritable()) { |
| 422 | countReadOnly++; |
| 423 | iterator.remove(); |
| 424 | } |
| 425 | } |
| 426 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 427 | Log.v(TAG, "# of read-only removed: " + countReadOnly); |
| 428 | Log.v(TAG, "After trimming: " + state.size()); |
| 429 | } |
| 430 | } |
| 431 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 432 | private void saveContact(Intent intent) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 433 | RawContactDeltaList state = intent.getParcelableExtra(EXTRA_CONTACT_STATE); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 434 | boolean isProfile = intent.getBooleanExtra(EXTRA_SAVE_IS_PROFILE, false); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 435 | Bundle updatedPhotos = intent.getParcelableExtra(EXTRA_UPDATED_PHOTOS); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 436 | |
Jay Shrauner | 0809978 | 2015-03-25 14:17:11 -0700 | [diff] [blame] | 437 | if (state == null) { |
| 438 | Log.e(TAG, "Invalid arguments for saveContact request"); |
| 439 | return; |
| 440 | } |
| 441 | |
benny.lin | 3a4e7a2 | 2014-01-08 10:58:08 +0800 | [diff] [blame] | 442 | int saveMode = intent.getIntExtra(EXTRA_SAVE_MODE, -1); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 443 | // Trim any empty fields, and RawContacts, before persisting |
| 444 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 445 | RawContactModifier.trimEmpty(state, accountTypes); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 446 | |
| 447 | Uri lookupUri = null; |
| 448 | |
| 449 | final ContentResolver resolver = getContentResolver(); |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 450 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 451 | boolean succeeded = false; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 452 | |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 453 | // Keep track of the id of a newly raw-contact (if any... there can be at most one). |
| 454 | long insertedRawContactId = -1; |
| 455 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 456 | // Attempt to persist changes |
| 457 | int tries = 0; |
| 458 | while (tries++ < PERSIST_TRIES) { |
| 459 | try { |
| 460 | // Build operations and try applying |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 461 | final ArrayList<CPOWrapper> diffWrapper = state.buildDiffWrapper(); |
| 462 | |
| 463 | final ArrayList<ContentProviderOperation> diff = Lists.newArrayList(); |
| 464 | |
| 465 | for (CPOWrapper cpoWrapper : diffWrapper) { |
| 466 | diff.add(cpoWrapper.getOperation()); |
| 467 | } |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 468 | |
Katherine Kuan | a007e44 | 2011-07-07 09:25:34 -0700 | [diff] [blame] | 469 | if (DEBUG) { |
| 470 | Log.v(TAG, "Content Provider Operations:"); |
| 471 | for (ContentProviderOperation operation : diff) { |
| 472 | Log.v(TAG, operation.toString()); |
| 473 | } |
| 474 | } |
| 475 | |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 476 | int numberProcessed = 0; |
| 477 | boolean batchFailed = false; |
| 478 | final ContentProviderResult[] results = new ContentProviderResult[diff.size()]; |
| 479 | while (numberProcessed < diff.size()) { |
| 480 | final int subsetCount = applyDiffSubset(diff, numberProcessed, results, resolver); |
| 481 | if (subsetCount == -1) { |
Jay Shrauner | 511561d | 2015-04-02 10:35:33 -0700 | [diff] [blame] | 482 | Log.w(TAG, "Resolver.applyBatch failed in saveContacts"); |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 483 | batchFailed = true; |
| 484 | break; |
| 485 | } else { |
| 486 | numberProcessed += subsetCount; |
Jay Shrauner | 511561d | 2015-04-02 10:35:33 -0700 | [diff] [blame] | 487 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 490 | if (batchFailed) { |
| 491 | // Retry save |
| 492 | continue; |
| 493 | } |
| 494 | |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 495 | final long rawContactId = getRawContactId(state, diffWrapper, results); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 496 | if (rawContactId == -1) { |
| 497 | throw new IllegalStateException("Could not determine RawContact ID after save"); |
| 498 | } |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 499 | // We don't have to check to see if the value is still -1. If we reach here, |
| 500 | // the previous loop iteration didn't succeed, so any ID that we obtained is bogus. |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 501 | insertedRawContactId = getInsertedRawContactId(diffWrapper, results); |
Dave Santoro | 7c34c0a | 2011-09-12 14:58:20 -0700 | [diff] [blame] | 502 | if (isProfile) { |
| 503 | // Since the profile supports local raw contacts, which may have been completely |
| 504 | // removed if all information was removed, we need to do a special query to |
| 505 | // get the lookup URI for the profile contact (if it still exists). |
| 506 | Cursor c = resolver.query(Profile.CONTENT_URI, |
| 507 | new String[] {Contacts._ID, Contacts.LOOKUP_KEY}, |
| 508 | null, null, null); |
Jay Shrauner | e320c0b | 2015-03-05 12:45:18 -0800 | [diff] [blame] | 509 | if (c == null) { |
| 510 | continue; |
| 511 | } |
Dave Santoro | 7c34c0a | 2011-09-12 14:58:20 -0700 | [diff] [blame] | 512 | try { |
Erik | 162b7e3 | 2011-09-20 15:23:55 -0700 | [diff] [blame] | 513 | if (c.moveToFirst()) { |
| 514 | final long contactId = c.getLong(0); |
| 515 | final String lookupKey = c.getString(1); |
| 516 | lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
| 517 | } |
Dave Santoro | 7c34c0a | 2011-09-12 14:58:20 -0700 | [diff] [blame] | 518 | } finally { |
| 519 | c.close(); |
| 520 | } |
| 521 | } else { |
| 522 | final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, |
| 523 | rawContactId); |
| 524 | lookupUri = RawContacts.getContactLookupUri(resolver, rawContactUri); |
| 525 | } |
Jay Shrauner | e320c0b | 2015-03-05 12:45:18 -0800 | [diff] [blame] | 526 | if (lookupUri != null) { |
| 527 | Log.v(TAG, "Saved contact. New URI: " + lookupUri); |
| 528 | } |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 529 | |
| 530 | // We can change this back to false later, if we fail to save the contact photo. |
| 531 | succeeded = true; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 532 | break; |
| 533 | |
| 534 | } catch (RemoteException e) { |
| 535 | // Something went wrong, bail without success |
| 536 | Log.e(TAG, "Problem persisting user edits", e); |
| 537 | break; |
| 538 | |
Jay Shrauner | 57fca18 | 2014-01-17 14:20:50 -0800 | [diff] [blame] | 539 | } catch (IllegalArgumentException e) { |
| 540 | // This is thrown by applyBatch on malformed requests |
| 541 | Log.e(TAG, "Problem persisting user edits", e); |
| 542 | showToast(R.string.contactSavedErrorToast); |
| 543 | break; |
| 544 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 545 | } catch (OperationApplicationException e) { |
| 546 | // Version consistency failed, re-parent change and try again |
| 547 | Log.w(TAG, "Version consistency failed, re-parenting: " + e.toString()); |
| 548 | final StringBuilder sb = new StringBuilder(RawContacts._ID + " IN("); |
| 549 | boolean first = true; |
| 550 | final int count = state.size(); |
| 551 | for (int i = 0; i < count; i++) { |
| 552 | Long rawContactId = state.getRawContactId(i); |
| 553 | if (rawContactId != null && rawContactId != -1) { |
| 554 | if (!first) { |
| 555 | sb.append(','); |
| 556 | } |
| 557 | sb.append(rawContactId); |
| 558 | first = false; |
| 559 | } |
| 560 | } |
| 561 | sb.append(")"); |
| 562 | |
| 563 | if (first) { |
Brian Attwell | 3b6c628 | 2014-02-12 17:53:31 -0800 | [diff] [blame] | 564 | throw new IllegalStateException( |
| 565 | "Version consistency failed for a new contact", e); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 568 | final RawContactDeltaList newState = RawContactDeltaList.fromQuery( |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 569 | isProfile |
| 570 | ? RawContactsEntity.PROFILE_CONTENT_URI |
| 571 | : RawContactsEntity.CONTENT_URI, |
| 572 | resolver, sb.toString(), null, null); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 573 | state = RawContactDeltaList.mergeAfter(newState, state); |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 574 | |
| 575 | // Update the new state to use profile URIs if appropriate. |
| 576 | if (isProfile) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 577 | for (RawContactDelta delta : state) { |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 578 | delta.setProfileQueryUri(); |
| 579 | } |
| 580 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 584 | // Now save any updated photos. We do this at the end to ensure that |
| 585 | // the ContactProvider already knows about newly-created contacts. |
| 586 | if (updatedPhotos != null) { |
| 587 | for (String key : updatedPhotos.keySet()) { |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 588 | Uri photoUri = updatedPhotos.getParcelable(key); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 589 | long rawContactId = Long.parseLong(key); |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 590 | |
| 591 | // If the raw-contact ID is negative, we are saving a new raw-contact; |
| 592 | // replace the bogus ID with the new one that we actually saved the contact at. |
| 593 | if (rawContactId < 0) { |
| 594 | rawContactId = insertedRawContactId; |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Jay Shrauner | 511561d | 2015-04-02 10:35:33 -0700 | [diff] [blame] | 597 | // If the save failed, insertedRawContactId will be -1 |
Jay Shrauner | c4698fb | 2015-04-30 12:08:52 -0700 | [diff] [blame] | 598 | if (rawContactId < 0 || !saveUpdatedPhoto(rawContactId, photoUri, saveMode)) { |
Jay Shrauner | 511561d | 2015-04-02 10:35:33 -0700 | [diff] [blame] | 599 | succeeded = false; |
| 600 | } |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 604 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 605 | if (callbackIntent != null) { |
| 606 | if (succeeded) { |
| 607 | // Mark the intent to indicate that the save was successful (even if the lookup URI |
| 608 | // is now null). For local contacts or the local profile, it's possible that the |
| 609 | // save triggered removal of the contact, so no lookup URI would exist.. |
| 610 | callbackIntent.putExtra(EXTRA_SAVE_SUCCEEDED, true); |
| 611 | } |
| 612 | callbackIntent.setData(lookupUri); |
| 613 | deliverCallback(callbackIntent); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 614 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 617 | /** |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 618 | * Splits "diff" into subsets based on "MAX_CONTACTS_PROVIDER_BATCH_SIZE", applies each of the |
| 619 | * subsets, adds the returned array to "results". |
| 620 | * |
| 621 | * @return the size of the array, if not null; -1 when the array is null. |
| 622 | */ |
| 623 | private int applyDiffSubset(ArrayList<ContentProviderOperation> diff, int offset, |
| 624 | ContentProviderResult[] results, ContentResolver resolver) |
| 625 | throws RemoteException, OperationApplicationException { |
| 626 | final int subsetCount = Math.min(diff.size() - offset, MAX_CONTACTS_PROVIDER_BATCH_SIZE); |
| 627 | final ArrayList<ContentProviderOperation> subset = new ArrayList<>(); |
| 628 | subset.addAll(diff.subList(offset, offset + subsetCount)); |
| 629 | final ContentProviderResult[] subsetResult = resolver.applyBatch(ContactsContract |
| 630 | .AUTHORITY, subset); |
| 631 | if (subsetResult == null || (offset + subsetResult.length) > results.length) { |
| 632 | return -1; |
| 633 | } |
| 634 | for (ContentProviderResult c : subsetResult) { |
| 635 | results[offset++] = c; |
| 636 | } |
| 637 | return subsetResult.length; |
| 638 | } |
| 639 | |
| 640 | /** |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 641 | * Save updated photo for the specified raw-contact. |
| 642 | * @return true for success, false for failure |
| 643 | */ |
benny.lin | 3a4e7a2 | 2014-01-08 10:58:08 +0800 | [diff] [blame] | 644 | private boolean saveUpdatedPhoto(long rawContactId, Uri photoUri, int saveMode) { |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 645 | final Uri outputUri = Uri.withAppendedPath( |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 646 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), |
| 647 | RawContacts.DisplayPhoto.CONTENT_DIRECTORY); |
| 648 | |
benny.lin | 3a4e7a2 | 2014-01-08 10:58:08 +0800 | [diff] [blame] | 649 | return ContactPhotoUtils.savePhotoFromUriToUri(this, photoUri, outputUri, (saveMode == 0)); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 652 | /** |
| 653 | * Find the ID of an existing or newly-inserted raw-contact. If none exists, return -1. |
| 654 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 655 | private long getRawContactId(RawContactDeltaList state, |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 656 | final ArrayList<CPOWrapper> diffWrapper, |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 657 | final ContentProviderResult[] results) { |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 658 | long existingRawContactId = state.findRawContactId(); |
| 659 | if (existingRawContactId != -1) { |
| 660 | return existingRawContactId; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 663 | return getInsertedRawContactId(diffWrapper, results); |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Find the ID of a newly-inserted raw-contact. If none exists, return -1. |
| 668 | */ |
| 669 | private long getInsertedRawContactId( |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 670 | final ArrayList<CPOWrapper> diffWrapper, final ContentProviderResult[] results) { |
Jay Shrauner | 568f4e7 | 2014-11-26 08:16:25 -0800 | [diff] [blame] | 671 | if (results == null) { |
| 672 | return -1; |
| 673 | } |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 674 | final int diffSize = diffWrapper.size(); |
Jay Shrauner | 3d7edc3 | 2014-11-10 09:58:23 -0800 | [diff] [blame] | 675 | final int numResults = results.length; |
| 676 | for (int i = 0; i < diffSize && i < numResults; i++) { |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 677 | final CPOWrapper cpoWrapper = diffWrapper.get(i); |
| 678 | final boolean isInsert = CompatUtils.isInsertCompat(cpoWrapper); |
| 679 | if (isInsert && cpoWrapper.getOperation().getUri().getEncodedPath().contains( |
| 680 | RawContacts.CONTENT_URI.getEncodedPath())) { |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 681 | return ContentUris.parseId(results[i].uri); |
| 682 | } |
| 683 | } |
| 684 | return -1; |
| 685 | } |
| 686 | |
| 687 | /** |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 688 | * Creates an intent that can be sent to this service to create a new group as |
| 689 | * well as add new members at the same time. |
| 690 | * |
| 691 | * @param context of the application |
| 692 | * @param account in which the group should be created |
| 693 | * @param label is the name of the group (cannot be null) |
| 694 | * @param rawContactsToAdd is an array of raw contact IDs for contacts that |
| 695 | * should be added to the group |
| 696 | * @param callbackActivity is the activity to send the callback intent to |
| 697 | * @param callbackAction is the intent action for the callback intent |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 698 | */ |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 699 | public static Intent createNewGroupIntent(Context context, AccountWithDataSet account, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 700 | String label, long[] rawContactsToAdd, Class<? extends Activity> callbackActivity, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 701 | String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 702 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 703 | serviceIntent.setAction(ContactSaveService.ACTION_CREATE_GROUP); |
| 704 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_TYPE, account.type); |
| 705 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_NAME, account.name); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 706 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_SET, account.dataSet); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 707 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, label); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 708 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_ADD, rawContactsToAdd); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 709 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 710 | // 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] | 711 | // created. |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 712 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 713 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 714 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 715 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 716 | return serviceIntent; |
| 717 | } |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 718 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 719 | private void createGroup(Intent intent) { |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 720 | String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); |
| 721 | String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME); |
| 722 | String dataSet = intent.getStringExtra(EXTRA_DATA_SET); |
| 723 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 724 | final long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 725 | |
| 726 | ContentValues values = new ContentValues(); |
| 727 | values.put(Groups.ACCOUNT_TYPE, accountType); |
| 728 | values.put(Groups.ACCOUNT_NAME, accountName); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 729 | values.put(Groups.DATA_SET, dataSet); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 730 | values.put(Groups.TITLE, label); |
| 731 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 732 | final ContentResolver resolver = getContentResolver(); |
| 733 | |
| 734 | // Create the new group |
| 735 | final Uri groupUri = resolver.insert(Groups.CONTENT_URI, values); |
| 736 | |
| 737 | // If there's no URI, then the insertion failed. Abort early because group members can't be |
| 738 | // added if the group doesn't exist |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 739 | if (groupUri == null) { |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 740 | Log.e(TAG, "Couldn't create group with label " + label); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 744 | // Add new group members |
| 745 | addMembersToGroup(resolver, rawContactsToAdd, ContentUris.parseId(groupUri)); |
| 746 | |
| 747 | // TODO: Move this into the contact editor where it belongs. This needs to be integrated |
| 748 | // 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] | 749 | values.clear(); |
| 750 | values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); |
| 751 | values.put(GroupMembership.GROUP_ROW_ID, ContentUris.parseId(groupUri)); |
| 752 | |
| 753 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 754 | callbackIntent.setData(groupUri); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 755 | // TODO: This can be taken out when the above TODO is addressed |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 756 | callbackIntent.putExtra(ContactsContract.Intents.Insert.DATA, Lists.newArrayList(values)); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 757 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 761 | * 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] | 762 | */ |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 763 | public static Intent createGroupRenameIntent(Context context, long groupId, String newLabel, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 764 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 765 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 766 | serviceIntent.setAction(ContactSaveService.ACTION_RENAME_GROUP); |
| 767 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
| 768 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, newLabel); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 769 | |
| 770 | // Callback intent will be invoked by the service once the group is renamed. |
| 771 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 772 | callbackIntent.setAction(callbackAction); |
| 773 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 774 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 775 | return serviceIntent; |
| 776 | } |
| 777 | |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 778 | private void renameGroup(Intent intent) { |
| 779 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 780 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
| 781 | |
| 782 | if (groupId == -1) { |
| 783 | Log.e(TAG, "Invalid arguments for renameGroup request"); |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | ContentValues values = new ContentValues(); |
| 788 | values.put(Groups.TITLE, label); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 789 | final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); |
| 790 | getContentResolver().update(groupUri, values, null, null); |
| 791 | |
| 792 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 793 | callbackIntent.setData(groupUri); |
| 794 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 798 | * 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] | 799 | */ |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 800 | public static Intent createGroupDeletionIntent(Context context, long groupId) { |
| 801 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 802 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_GROUP); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 803 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 804 | return serviceIntent; |
| 805 | } |
| 806 | |
| 807 | private void deleteGroup(Intent intent) { |
| 808 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 809 | if (groupId == -1) { |
| 810 | Log.e(TAG, "Invalid arguments for deleteGroup request"); |
| 811 | return; |
| 812 | } |
| 813 | |
| 814 | getContentResolver().delete( |
| 815 | ContentUris.withAppendedId(Groups.CONTENT_URI, groupId), null, null); |
| 816 | } |
| 817 | |
| 818 | /** |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 819 | * Creates an intent that can be sent to this service to rename a group as |
| 820 | * well as add and remove members from the group. |
| 821 | * |
| 822 | * @param context of the application |
| 823 | * @param groupId of the group that should be modified |
| 824 | * @param newLabel is the updated name of the group (can be null if the name |
| 825 | * should not be updated) |
| 826 | * @param rawContactsToAdd is an array of raw contact IDs for contacts that |
| 827 | * should be added to the group |
| 828 | * @param rawContactsToRemove is an array of raw contact IDs for contacts |
| 829 | * that should be removed from the group |
| 830 | * @param callbackActivity is the activity to send the callback intent to |
| 831 | * @param callbackAction is the intent action for the callback intent |
| 832 | */ |
| 833 | public static Intent createGroupUpdateIntent(Context context, long groupId, String newLabel, |
| 834 | long[] rawContactsToAdd, long[] rawContactsToRemove, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 835 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 836 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 837 | serviceIntent.setAction(ContactSaveService.ACTION_UPDATE_GROUP); |
| 838 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
| 839 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, newLabel); |
| 840 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_ADD, rawContactsToAdd); |
| 841 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_REMOVE, |
| 842 | rawContactsToRemove); |
| 843 | |
| 844 | // Callback intent will be invoked by the service once the group is updated |
| 845 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 846 | callbackIntent.setAction(callbackAction); |
| 847 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 848 | |
| 849 | return serviceIntent; |
| 850 | } |
| 851 | |
| 852 | private void updateGroup(Intent intent) { |
| 853 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 854 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
| 855 | long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD); |
| 856 | long[] rawContactsToRemove = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_REMOVE); |
| 857 | |
| 858 | if (groupId == -1) { |
| 859 | Log.e(TAG, "Invalid arguments for updateGroup request"); |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | final ContentResolver resolver = getContentResolver(); |
| 864 | final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); |
| 865 | |
| 866 | // Update group name if necessary |
| 867 | if (label != null) { |
| 868 | ContentValues values = new ContentValues(); |
| 869 | values.put(Groups.TITLE, label); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 870 | resolver.update(groupUri, values, null, null); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 873 | // Add and remove members if necessary |
| 874 | addMembersToGroup(resolver, rawContactsToAdd, groupId); |
| 875 | removeMembersFromGroup(resolver, rawContactsToRemove, groupId); |
| 876 | |
| 877 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 878 | callbackIntent.setData(groupUri); |
| 879 | deliverCallback(callbackIntent); |
| 880 | } |
| 881 | |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 882 | private static void addMembersToGroup(ContentResolver resolver, long[] rawContactsToAdd, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 883 | long groupId) { |
| 884 | if (rawContactsToAdd == null) { |
| 885 | return; |
| 886 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 887 | for (long rawContactId : rawContactsToAdd) { |
| 888 | try { |
| 889 | final ArrayList<ContentProviderOperation> rawContactOperations = |
| 890 | new ArrayList<ContentProviderOperation>(); |
| 891 | |
| 892 | // Build an assert operation to ensure the contact is not already in the group |
| 893 | final ContentProviderOperation.Builder assertBuilder = ContentProviderOperation |
| 894 | .newAssertQuery(Data.CONTENT_URI); |
| 895 | assertBuilder.withSelection(Data.RAW_CONTACT_ID + "=? AND " + |
| 896 | Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", |
| 897 | new String[] { String.valueOf(rawContactId), |
| 898 | GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); |
| 899 | assertBuilder.withExpectedCount(0); |
| 900 | rawContactOperations.add(assertBuilder.build()); |
| 901 | |
| 902 | // Build an insert operation to add the contact to the group |
| 903 | final ContentProviderOperation.Builder insertBuilder = ContentProviderOperation |
| 904 | .newInsert(Data.CONTENT_URI); |
| 905 | insertBuilder.withValue(Data.RAW_CONTACT_ID, rawContactId); |
| 906 | insertBuilder.withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); |
| 907 | insertBuilder.withValue(GroupMembership.GROUP_ROW_ID, groupId); |
| 908 | rawContactOperations.add(insertBuilder.build()); |
| 909 | |
| 910 | if (DEBUG) { |
| 911 | for (ContentProviderOperation operation : rawContactOperations) { |
| 912 | Log.v(TAG, operation.toString()); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // Apply batch |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 917 | if (!rawContactOperations.isEmpty()) { |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 918 | resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 919 | } |
| 920 | } catch (RemoteException e) { |
| 921 | // Something went wrong, bail without success |
| 922 | Log.e(TAG, "Problem persisting user edits for raw contact ID " + |
| 923 | String.valueOf(rawContactId), e); |
| 924 | } catch (OperationApplicationException e) { |
| 925 | // The assert could have failed because the contact is already in the group, |
| 926 | // just continue to the next contact |
| 927 | Log.w(TAG, "Assert failed in adding raw contact ID " + |
| 928 | String.valueOf(rawContactId) + ". Already exists in group " + |
| 929 | String.valueOf(groupId), e); |
| 930 | } |
| 931 | } |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 932 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 933 | |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 934 | private static void removeMembersFromGroup(ContentResolver resolver, long[] rawContactsToRemove, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 935 | long groupId) { |
| 936 | if (rawContactsToRemove == null) { |
| 937 | return; |
| 938 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 939 | for (long rawContactId : rawContactsToRemove) { |
| 940 | // Apply the delete operation on the data row for the given raw contact's |
| 941 | // membership in the given group. If no contact matches the provided selection, then |
| 942 | // nothing will be done. Just continue to the next contact. |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 943 | resolver.delete(Data.CONTENT_URI, Data.RAW_CONTACT_ID + "=? AND " + |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 944 | Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", |
| 945 | new String[] { String.valueOf(rawContactId), |
| 946 | GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); |
| 947 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 951 | * 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] | 952 | */ |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 953 | public static Intent createSetStarredIntent(Context context, Uri contactUri, boolean value) { |
| 954 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 955 | serviceIntent.setAction(ContactSaveService.ACTION_SET_STARRED); |
| 956 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 957 | serviceIntent.putExtra(ContactSaveService.EXTRA_STARRED_FLAG, value); |
| 958 | |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 959 | return serviceIntent; |
| 960 | } |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 961 | |
| 962 | private void setStarred(Intent intent) { |
| 963 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 964 | boolean value = intent.getBooleanExtra(EXTRA_STARRED_FLAG, false); |
| 965 | if (contactUri == null) { |
| 966 | Log.e(TAG, "Invalid arguments for setStarred request"); |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | final ContentValues values = new ContentValues(1); |
| 971 | values.put(Contacts.STARRED, value); |
| 972 | getContentResolver().update(contactUri, values, null, null); |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 973 | |
| 974 | // Undemote the contact if necessary |
| 975 | final Cursor c = getContentResolver().query(contactUri, new String[] {Contacts._ID}, |
| 976 | null, null, null); |
Jay Shrauner | c12a280 | 2014-11-24 10:07:31 -0800 | [diff] [blame] | 977 | if (c == null) { |
| 978 | return; |
| 979 | } |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 980 | try { |
| 981 | if (c.moveToFirst()) { |
| 982 | final long id = c.getLong(0); |
Yorke Lee | bbb8c99 | 2013-09-23 16:20:53 -0700 | [diff] [blame] | 983 | |
| 984 | // Don't bother undemoting if this contact is the user's profile. |
| 985 | if (id < Profile.MIN_ID) { |
Wenyi Wang | aac0e66 | 2015-12-18 17:17:33 -0800 | [diff] [blame] | 986 | PinnedPositionsCompat.undemote(getContentResolver(), id); |
Yorke Lee | bbb8c99 | 2013-09-23 16:20:53 -0700 | [diff] [blame] | 987 | } |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 988 | } |
| 989 | } finally { |
| 990 | c.close(); |
| 991 | } |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 992 | } |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 993 | |
| 994 | /** |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 995 | * Creates an intent that can be sent to this service to set the redirect to voicemail. |
| 996 | */ |
| 997 | public static Intent createSetSendToVoicemail(Context context, Uri contactUri, |
| 998 | boolean value) { |
| 999 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1000 | serviceIntent.setAction(ContactSaveService.ACTION_SET_SEND_TO_VOICEMAIL); |
| 1001 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 1002 | serviceIntent.putExtra(ContactSaveService.EXTRA_SEND_TO_VOICEMAIL_FLAG, value); |
| 1003 | |
| 1004 | return serviceIntent; |
| 1005 | } |
| 1006 | |
| 1007 | private void setSendToVoicemail(Intent intent) { |
| 1008 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 1009 | boolean value = intent.getBooleanExtra(EXTRA_SEND_TO_VOICEMAIL_FLAG, false); |
| 1010 | if (contactUri == null) { |
| 1011 | Log.e(TAG, "Invalid arguments for setRedirectToVoicemail"); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | final ContentValues values = new ContentValues(1); |
| 1016 | values.put(Contacts.SEND_TO_VOICEMAIL, value); |
| 1017 | getContentResolver().update(contactUri, values, null, null); |
| 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * Creates an intent that can be sent to this service to save the contact's ringtone. |
| 1022 | */ |
| 1023 | public static Intent createSetRingtone(Context context, Uri contactUri, |
| 1024 | String value) { |
| 1025 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1026 | serviceIntent.setAction(ContactSaveService.ACTION_SET_RINGTONE); |
| 1027 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 1028 | serviceIntent.putExtra(ContactSaveService.EXTRA_CUSTOM_RINGTONE, value); |
| 1029 | |
| 1030 | return serviceIntent; |
| 1031 | } |
| 1032 | |
| 1033 | private void setRingtone(Intent intent) { |
| 1034 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 1035 | String value = intent.getStringExtra(EXTRA_CUSTOM_RINGTONE); |
| 1036 | if (contactUri == null) { |
| 1037 | Log.e(TAG, "Invalid arguments for setRingtone"); |
| 1038 | return; |
| 1039 | } |
| 1040 | ContentValues values = new ContentValues(1); |
| 1041 | values.put(Contacts.CUSTOM_RINGTONE, value); |
| 1042 | getContentResolver().update(contactUri, values, null, null); |
| 1043 | } |
| 1044 | |
| 1045 | /** |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 1046 | * Creates an intent that sets the selected data item as super primary (default) |
| 1047 | */ |
| 1048 | public static Intent createSetSuperPrimaryIntent(Context context, long dataId) { |
| 1049 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1050 | serviceIntent.setAction(ContactSaveService.ACTION_SET_SUPER_PRIMARY); |
| 1051 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_ID, dataId); |
| 1052 | return serviceIntent; |
| 1053 | } |
| 1054 | |
| 1055 | private void setSuperPrimary(Intent intent) { |
| 1056 | long dataId = intent.getLongExtra(EXTRA_DATA_ID, -1); |
| 1057 | if (dataId == -1) { |
| 1058 | Log.e(TAG, "Invalid arguments for setSuperPrimary request"); |
| 1059 | return; |
| 1060 | } |
| 1061 | |
Chiao Cheng | d7ca03e | 2012-10-24 15:14:08 -0700 | [diff] [blame] | 1062 | ContactUpdateUtils.setSuperPrimary(this, dataId); |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /** |
| 1066 | * Creates an intent that clears the primary flag of all data items that belong to the same |
| 1067 | * raw_contact as the given data item. Will only clear, if the data item was primary before |
| 1068 | * this call |
| 1069 | */ |
| 1070 | public static Intent createClearPrimaryIntent(Context context, long dataId) { |
| 1071 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1072 | serviceIntent.setAction(ContactSaveService.ACTION_CLEAR_PRIMARY); |
| 1073 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_ID, dataId); |
| 1074 | return serviceIntent; |
| 1075 | } |
| 1076 | |
| 1077 | private void clearPrimary(Intent intent) { |
| 1078 | long dataId = intent.getLongExtra(EXTRA_DATA_ID, -1); |
| 1079 | if (dataId == -1) { |
| 1080 | Log.e(TAG, "Invalid arguments for clearPrimary request"); |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | // Update the primary values in the data record. |
| 1085 | ContentValues values = new ContentValues(1); |
| 1086 | values.put(Data.IS_SUPER_PRIMARY, 0); |
| 1087 | values.put(Data.IS_PRIMARY, 0); |
| 1088 | |
| 1089 | getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, dataId), |
| 1090 | values, null, null); |
| 1091 | } |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 1092 | |
| 1093 | /** |
| 1094 | * Creates an intent that can be sent to this service to delete a contact. |
| 1095 | */ |
| 1096 | public static Intent createDeleteContactIntent(Context context, Uri contactUri) { |
| 1097 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1098 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_CONTACT); |
| 1099 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 1100 | return serviceIntent; |
| 1101 | } |
| 1102 | |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 1103 | /** |
| 1104 | * Creates an intent that can be sent to this service to delete multiple contacts. |
| 1105 | */ |
| 1106 | public static Intent createDeleteMultipleContactsIntent(Context context, |
| 1107 | long[] contactIds) { |
| 1108 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1109 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_MULTIPLE_CONTACTS); |
| 1110 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_IDS, contactIds); |
| 1111 | return serviceIntent; |
| 1112 | } |
| 1113 | |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 1114 | private void deleteContact(Intent intent) { |
| 1115 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 1116 | if (contactUri == null) { |
| 1117 | Log.e(TAG, "Invalid arguments for deleteContact request"); |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | getContentResolver().delete(contactUri, null, null); |
| 1122 | } |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1123 | |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 1124 | private void deleteMultipleContacts(Intent intent) { |
| 1125 | final long[] contactIds = intent.getLongArrayExtra(EXTRA_CONTACT_IDS); |
| 1126 | if (contactIds == null) { |
| 1127 | Log.e(TAG, "Invalid arguments for deleteMultipleContacts request"); |
| 1128 | return; |
| 1129 | } |
| 1130 | for (long contactId : contactIds) { |
| 1131 | final Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); |
| 1132 | getContentResolver().delete(contactUri, null, null); |
| 1133 | } |
Wenyi Wang | 687d218 | 2015-10-28 17:03:18 -0700 | [diff] [blame] | 1134 | final String deleteToastMessage = getResources().getQuantityString(R.plurals |
| 1135 | .contacts_deleted_toast, contactIds.length); |
| 1136 | mMainHandler.post(new Runnable() { |
| 1137 | @Override |
| 1138 | public void run() { |
| 1139 | Toast.makeText(ContactSaveService.this, deleteToastMessage, Toast.LENGTH_LONG) |
| 1140 | .show(); |
| 1141 | } |
| 1142 | }); |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1145 | /** |
| 1146 | * Creates an intent that can be sent to this service to join two contacts. |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1147 | * The resulting contact uses the name from {@param contactId1} if possible. |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1148 | */ |
| 1149 | public static Intent createJoinContactsIntent(Context context, long contactId1, |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1150 | long contactId2, Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1151 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1152 | serviceIntent.setAction(ContactSaveService.ACTION_JOIN_CONTACTS); |
| 1153 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_ID1, contactId1); |
| 1154 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_ID2, contactId2); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1155 | |
| 1156 | // Callback intent will be invoked by the service once the contacts are joined. |
| 1157 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 1158 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1159 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 1160 | |
| 1161 | return serviceIntent; |
| 1162 | } |
| 1163 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1164 | /** |
| 1165 | * Creates an intent to join all raw contacts inside {@param contactIds}'s contacts. |
| 1166 | * No special attention is paid to where the resulting contact's name is taken from. |
| 1167 | */ |
| 1168 | public static Intent createJoinSeveralContactsIntent(Context context, long[] contactIds) { |
| 1169 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1170 | serviceIntent.setAction(ContactSaveService.ACTION_JOIN_SEVERAL_CONTACTS); |
| 1171 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_IDS, contactIds); |
| 1172 | return serviceIntent; |
| 1173 | } |
| 1174 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1175 | |
| 1176 | private interface JoinContactQuery { |
| 1177 | String[] PROJECTION = { |
| 1178 | RawContacts._ID, |
| 1179 | RawContacts.CONTACT_ID, |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1180 | RawContacts.DISPLAY_NAME_SOURCE, |
| 1181 | }; |
| 1182 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1183 | int _ID = 0; |
| 1184 | int CONTACT_ID = 1; |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1185 | int DISPLAY_NAME_SOURCE = 2; |
| 1186 | } |
| 1187 | |
| 1188 | private interface ContactEntityQuery { |
| 1189 | String[] PROJECTION = { |
| 1190 | Contacts.Entity.DATA_ID, |
| 1191 | Contacts.Entity.CONTACT_ID, |
| 1192 | Contacts.Entity.IS_SUPER_PRIMARY, |
| 1193 | }; |
| 1194 | String SELECTION = Data.MIMETYPE + " = '" + StructuredName.CONTENT_ITEM_TYPE + "'" + |
| 1195 | " AND " + StructuredName.DISPLAY_NAME + "=" + Contacts.DISPLAY_NAME + |
| 1196 | " AND " + StructuredName.DISPLAY_NAME + " IS NOT NULL " + |
| 1197 | " AND " + StructuredName.DISPLAY_NAME + " != '' "; |
| 1198 | |
| 1199 | int DATA_ID = 0; |
| 1200 | int CONTACT_ID = 1; |
| 1201 | int IS_SUPER_PRIMARY = 2; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1204 | private void joinSeveralContacts(Intent intent) { |
| 1205 | final long[] contactIds = intent.getLongArrayExtra(EXTRA_CONTACT_IDS); |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1206 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1207 | // Load raw contact IDs for all contacts involved. |
| 1208 | long rawContactIds[] = getRawContactIdsForAggregation(contactIds); |
| 1209 | if (rawContactIds == null) { |
| 1210 | Log.e(TAG, "Invalid arguments for joinSeveralContacts request"); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1211 | return; |
| 1212 | } |
| 1213 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1214 | // For each pair of raw contacts, insert an aggregation exception |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1215 | final ContentResolver resolver = getContentResolver(); |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1216 | // The maximum number of operations per batch (aka yield point) is 500. See b/22480225 |
| 1217 | final int batchSize = MAX_CONTACTS_PROVIDER_BATCH_SIZE; |
| 1218 | final ArrayList<ContentProviderOperation> operations = new ArrayList<>(batchSize); |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1219 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1220 | for (int j = 0; j < rawContactIds.length; j++) { |
| 1221 | if (i != j) { |
| 1222 | buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]); |
| 1223 | } |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1224 | // Before we get to 500 we need to flush the operations list |
| 1225 | if (operations.size() > 0 && operations.size() % batchSize == 0) { |
| 1226 | if (!applyJoinOperations(resolver, operations)) { |
| 1227 | return; |
| 1228 | } |
| 1229 | operations.clear(); |
| 1230 | } |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1231 | } |
| 1232 | } |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1233 | if (operations.size() > 0 && !applyJoinOperations(resolver, operations)) { |
| 1234 | return; |
| 1235 | } |
| 1236 | showToast(R.string.contactsJoinedMessage); |
| 1237 | } |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1238 | |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1239 | /** Returns true if the batch was successfully applied and false otherwise. */ |
| 1240 | private boolean applyJoinOperations(ContentResolver resolver, |
| 1241 | ArrayList<ContentProviderOperation> operations) { |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1242 | try { |
| 1243 | resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1244 | return true; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1245 | } catch (RemoteException | OperationApplicationException e) { |
| 1246 | Log.e(TAG, "Failed to apply aggregation exception batch", e); |
| 1247 | showToast(R.string.contactSavedErrorToast); |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1248 | return false; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | |
| 1253 | private void joinContacts(Intent intent) { |
| 1254 | long contactId1 = intent.getLongExtra(EXTRA_CONTACT_ID1, -1); |
| 1255 | long contactId2 = intent.getLongExtra(EXTRA_CONTACT_ID2, -1); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1256 | |
| 1257 | // Load raw contact IDs for all raw contacts involved - currently edited and selected |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1258 | // in the join UIs. |
| 1259 | long rawContactIds[] = getRawContactIdsForAggregation(contactId1, contactId2); |
| 1260 | if (rawContactIds == null) { |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1261 | Log.e(TAG, "Invalid arguments for joinContacts request"); |
Jay Shrauner | c12a280 | 2014-11-24 10:07:31 -0800 | [diff] [blame] | 1262 | return; |
| 1263 | } |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1264 | |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1265 | ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1266 | |
| 1267 | // For each pair of raw contacts, insert an aggregation exception |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1268 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1269 | for (int j = 0; j < rawContactIds.length; j++) { |
| 1270 | if (i != j) { |
| 1271 | buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]); |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1276 | final ContentResolver resolver = getContentResolver(); |
| 1277 | |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1278 | // Use the name for contactId1 as the name for the newly aggregated contact. |
| 1279 | final Uri contactId1Uri = ContentUris.withAppendedId( |
| 1280 | Contacts.CONTENT_URI, contactId1); |
| 1281 | final Uri entityUri = Uri.withAppendedPath( |
| 1282 | contactId1Uri, Contacts.Entity.CONTENT_DIRECTORY); |
| 1283 | Cursor c = resolver.query(entityUri, |
| 1284 | ContactEntityQuery.PROJECTION, ContactEntityQuery.SELECTION, null, null); |
| 1285 | if (c == null) { |
| 1286 | Log.e(TAG, "Unable to open Contacts DB cursor"); |
| 1287 | showToast(R.string.contactSavedErrorToast); |
| 1288 | return; |
| 1289 | } |
| 1290 | long dataIdToAddSuperPrimary = -1; |
| 1291 | try { |
| 1292 | if (c.moveToFirst()) { |
| 1293 | dataIdToAddSuperPrimary = c.getLong(ContactEntityQuery.DATA_ID); |
| 1294 | } |
| 1295 | } finally { |
| 1296 | c.close(); |
| 1297 | } |
| 1298 | |
| 1299 | // Mark the name from contactId1 IS_SUPER_PRIMARY to make sure that the contact |
| 1300 | // display name does not change as a result of the join. |
| 1301 | if (dataIdToAddSuperPrimary != -1) { |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1302 | Builder builder = ContentProviderOperation.newUpdate( |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1303 | ContentUris.withAppendedId(Data.CONTENT_URI, dataIdToAddSuperPrimary)); |
| 1304 | builder.withValue(Data.IS_SUPER_PRIMARY, 1); |
| 1305 | builder.withValue(Data.IS_PRIMARY, 1); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1306 | operations.add(builder.build()); |
| 1307 | } |
| 1308 | |
| 1309 | boolean success = false; |
| 1310 | // Apply all aggregation exceptions as one batch |
| 1311 | try { |
| 1312 | resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1313 | showToast(R.string.contactsJoinedMessage); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1314 | success = true; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1315 | } catch (RemoteException | OperationApplicationException e) { |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1316 | Log.e(TAG, "Failed to apply aggregation exception batch", e); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1317 | showToast(R.string.contactSavedErrorToast); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 1321 | if (success) { |
| 1322 | Uri uri = RawContacts.getContactLookupUri(resolver, |
| 1323 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactIds[0])); |
| 1324 | callbackIntent.setData(uri); |
| 1325 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1326 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1329 | private long[] getRawContactIdsForAggregation(long[] contactIds) { |
| 1330 | if (contactIds == null) { |
| 1331 | return null; |
| 1332 | } |
| 1333 | |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1334 | final ContentResolver resolver = getContentResolver(); |
| 1335 | long rawContactIds[]; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1336 | |
| 1337 | final StringBuilder queryBuilder = new StringBuilder(); |
| 1338 | final String stringContactIds[] = new String[contactIds.length]; |
| 1339 | for (int i = 0; i < contactIds.length; i++) { |
| 1340 | queryBuilder.append(RawContacts.CONTACT_ID + "=?"); |
| 1341 | stringContactIds[i] = String.valueOf(contactIds[i]); |
| 1342 | if (contactIds[i] == -1) { |
| 1343 | return null; |
| 1344 | } |
| 1345 | if (i == contactIds.length -1) { |
| 1346 | break; |
| 1347 | } |
| 1348 | queryBuilder.append(" OR "); |
| 1349 | } |
| 1350 | |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1351 | final Cursor c = resolver.query(RawContacts.CONTENT_URI, |
| 1352 | JoinContactQuery.PROJECTION, |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1353 | queryBuilder.toString(), |
| 1354 | stringContactIds, null); |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1355 | if (c == null) { |
| 1356 | Log.e(TAG, "Unable to open Contacts DB cursor"); |
| 1357 | showToast(R.string.contactSavedErrorToast); |
| 1358 | return null; |
| 1359 | } |
| 1360 | try { |
| 1361 | if (c.getCount() < 2) { |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1362 | Log.e(TAG, "Not enough raw contacts to aggregate together."); |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1363 | return null; |
| 1364 | } |
| 1365 | rawContactIds = new long[c.getCount()]; |
| 1366 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1367 | c.moveToPosition(i); |
| 1368 | long rawContactId = c.getLong(JoinContactQuery._ID); |
| 1369 | rawContactIds[i] = rawContactId; |
| 1370 | } |
| 1371 | } finally { |
| 1372 | c.close(); |
| 1373 | } |
| 1374 | return rawContactIds; |
| 1375 | } |
| 1376 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1377 | private long[] getRawContactIdsForAggregation(long contactId1, long contactId2) { |
| 1378 | return getRawContactIdsForAggregation(new long[] {contactId1, contactId2}); |
| 1379 | } |
| 1380 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1381 | /** |
| 1382 | * Construct a {@link AggregationExceptions#TYPE_KEEP_TOGETHER} ContentProviderOperation. |
| 1383 | */ |
| 1384 | private void buildJoinContactDiff(ArrayList<ContentProviderOperation> operations, |
| 1385 | long rawContactId1, long rawContactId2) { |
| 1386 | Builder builder = |
| 1387 | ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI); |
| 1388 | builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER); |
| 1389 | builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1); |
| 1390 | builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2); |
| 1391 | operations.add(builder.build()); |
| 1392 | } |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1393 | |
| 1394 | /** |
| 1395 | * Shows a toast on the UI thread. |
| 1396 | */ |
| 1397 | private void showToast(final int message) { |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1398 | mMainHandler.post(new Runnable() { |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1399 | |
| 1400 | @Override |
| 1401 | public void run() { |
| 1402 | Toast.makeText(ContactSaveService.this, message, Toast.LENGTH_LONG).show(); |
| 1403 | } |
| 1404 | }); |
| 1405 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1406 | |
| 1407 | private void deliverCallback(final Intent callbackIntent) { |
| 1408 | mMainHandler.post(new Runnable() { |
| 1409 | |
| 1410 | @Override |
| 1411 | public void run() { |
| 1412 | deliverCallbackOnUiThread(callbackIntent); |
| 1413 | } |
| 1414 | }); |
| 1415 | } |
| 1416 | |
| 1417 | void deliverCallbackOnUiThread(final Intent callbackIntent) { |
| 1418 | // TODO: this assumes that if there are multiple instances of the same |
| 1419 | // activity registered, the last one registered is the one waiting for |
| 1420 | // the callback. Validity of this assumption needs to be verified. |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 1421 | for (Listener listener : sListeners) { |
| 1422 | if (callbackIntent.getComponent().equals( |
| 1423 | ((Activity) listener).getIntent().getComponent())) { |
| 1424 | listener.onServiceCompleted(callbackIntent); |
| 1425 | return; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | } |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 1429 | } |