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