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