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