Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -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 | |
| 17 | package com.android.contacts.interactions; |
| 18 | |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 21 | import android.app.Fragment; |
| 22 | import android.app.FragmentManager; |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 23 | import android.app.LoaderManager; |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 24 | import android.app.LoaderManager.LoaderCallbacks; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.CursorLoader; |
| 27 | import android.content.DialogInterface; |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 28 | import android.content.DialogInterface.OnDismissListener; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 29 | import android.content.Loader; |
| 30 | import android.database.Cursor; |
| 31 | import android.net.Uri; |
| 32 | import android.os.Bundle; |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 33 | import android.provider.ContactsContract.Contacts; |
| 34 | import android.provider.ContactsContract.Contacts.Entity; |
Jay Shrauner | e320c0b | 2015-03-05 12:45:18 -0800 | [diff] [blame] | 35 | import android.text.TextUtils; |
Jay Shrauner | 053f723 | 2015-03-02 12:47:54 -0800 | [diff] [blame] | 36 | import android.util.Log; |
Wenyi Wang | 687d218 | 2015-10-28 17:03:18 -0700 | [diff] [blame] | 37 | import android.widget.Toast; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 38 | |
| 39 | import com.android.contacts.ContactSaveService; |
| 40 | import com.android.contacts.R; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 41 | import com.android.contacts.model.AccountTypeManager; |
| 42 | import com.android.contacts.model.account.AccountType; |
| 43 | import com.android.contacts.preference.ContactsPreferences; |
| 44 | import com.android.contacts.util.ContactDisplayUtils; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 45 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 46 | import com.google.common.annotations.VisibleForTesting; |
| 47 | import com.google.common.collect.Sets; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 48 | |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 49 | import java.util.HashSet; |
| 50 | |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 51 | /** |
| 52 | * An interaction invoked to delete a contact. |
| 53 | */ |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 54 | public class ContactDeletionInteraction extends Fragment |
| 55 | implements LoaderCallbacks<Cursor>, OnDismissListener { |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 56 | |
Jay Shrauner | 053f723 | 2015-03-02 12:47:54 -0800 | [diff] [blame] | 57 | private static final String TAG = "ContactDeletionInteraction"; |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 58 | private static final String FRAGMENT_TAG = "deleteContact"; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 59 | |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 60 | private static final String KEY_ACTIVE = "active"; |
Dmitri Plotnikov | cc42b45 | 2011-01-20 11:44:44 -0800 | [diff] [blame] | 61 | private static final String KEY_CONTACT_URI = "contactUri"; |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 62 | private static final String KEY_FINISH_WHEN_DONE = "finishWhenDone"; |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 63 | public static final String ARG_CONTACT_URI = "contactUri"; |
Brian Attwell | 8a6f4ad | 2014-06-06 21:54:53 -0700 | [diff] [blame] | 64 | public static final int RESULT_CODE_DELETED = 3; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 65 | |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 66 | private static final String[] ENTITY_PROJECTION = new String[] { |
| 67 | Entity.RAW_CONTACT_ID, //0 |
| 68 | Entity.ACCOUNT_TYPE, //1 |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 69 | Entity.DATA_SET, // 2 |
| 70 | Entity.CONTACT_ID, // 3 |
| 71 | Entity.LOOKUP_KEY, // 4 |
James Laskey | 4beed7e | 2016-09-15 13:52:19 -0700 | [diff] [blame] | 72 | Entity.DISPLAY_NAME, // 5 |
James Laskey | e5a140a | 2016-10-18 15:43:42 -0700 | [diff] [blame] | 73 | Entity.DISPLAY_NAME_ALTERNATIVE, // 6 |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 76 | private static final int COLUMN_INDEX_RAW_CONTACT_ID = 0; |
| 77 | private static final int COLUMN_INDEX_ACCOUNT_TYPE = 1; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 78 | private static final int COLUMN_INDEX_DATA_SET = 2; |
| 79 | private static final int COLUMN_INDEX_CONTACT_ID = 3; |
| 80 | private static final int COLUMN_INDEX_LOOKUP_KEY = 4; |
James Laskey | 4beed7e | 2016-09-15 13:52:19 -0700 | [diff] [blame] | 81 | private static final int COLUMN_INDEX_DISPLAY_NAME = 5; |
James Laskey | e5a140a | 2016-10-18 15:43:42 -0700 | [diff] [blame] | 82 | private static final int COLUMN_INDEX_DISPLAY_NAME_ALT = 6; |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 83 | |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 84 | private boolean mActive; |
| 85 | private Uri mContactUri; |
James Laskey | 4beed7e | 2016-09-15 13:52:19 -0700 | [diff] [blame] | 86 | private String mDisplayName; |
James Laskey | e5a140a | 2016-10-18 15:43:42 -0700 | [diff] [blame] | 87 | private String mDisplayNameAlt; |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 88 | private boolean mFinishActivityWhenDone; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 89 | private Context mContext; |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 90 | private AlertDialog mDialog; |
| 91 | |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 92 | /** This is a wrapper around the fragment's loader manager to be used only during testing. */ |
Brian Attwell | 09bfb91 | 2014-12-18 20:11:17 -0800 | [diff] [blame] | 93 | private TestLoaderManagerBase mTestLoaderManager; |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 94 | |
| 95 | @VisibleForTesting |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 96 | int mMessageId; |
| 97 | |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 98 | /** |
| 99 | * Starts the interaction. |
| 100 | * |
| 101 | * @param activity the activity within which to start the interaction |
| 102 | * @param contactUri the URI of the contact to delete |
| 103 | * @param finishActivityWhenDone whether to finish the activity upon completion of the |
| 104 | * interaction |
| 105 | * @return the newly created interaction |
| 106 | */ |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 107 | public static ContactDeletionInteraction start( |
| 108 | Activity activity, Uri contactUri, boolean finishActivityWhenDone) { |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 109 | return startWithTestLoaderManager(activity, contactUri, finishActivityWhenDone, null); |
| 110 | } |
| 111 | |
| 112 | /** |
Brian Attwell | 09bfb91 | 2014-12-18 20:11:17 -0800 | [diff] [blame] | 113 | * Starts the interaction and optionally set up a {@link TestLoaderManagerBase}. |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 114 | * |
| 115 | * @param activity the activity within which to start the interaction |
| 116 | * @param contactUri the URI of the contact to delete |
| 117 | * @param finishActivityWhenDone whether to finish the activity upon completion of the |
| 118 | * interaction |
Brian Attwell | 09bfb91 | 2014-12-18 20:11:17 -0800 | [diff] [blame] | 119 | * @param testLoaderManager the {@link TestLoaderManagerBase} to use to load the data, may be null |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 120 | * in which case the default {@link LoaderManager} is used |
| 121 | * @return the newly created interaction |
| 122 | */ |
| 123 | @VisibleForTesting |
| 124 | static ContactDeletionInteraction startWithTestLoaderManager( |
| 125 | Activity activity, Uri contactUri, boolean finishActivityWhenDone, |
Brian Attwell | 09bfb91 | 2014-12-18 20:11:17 -0800 | [diff] [blame] | 126 | TestLoaderManagerBase testLoaderManager) { |
Jay Shrauner | 5dcf5c3 | 2015-09-16 13:56:57 -0700 | [diff] [blame] | 127 | if (contactUri == null || activity.isDestroyed()) { |
Dmitri Plotnikov | ac0e18e | 2011-02-23 17:23:53 -0800 | [diff] [blame] | 128 | return null; |
| 129 | } |
| 130 | |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 131 | FragmentManager fragmentManager = activity.getFragmentManager(); |
| 132 | ContactDeletionInteraction fragment = |
| 133 | (ContactDeletionInteraction) fragmentManager.findFragmentByTag(FRAGMENT_TAG); |
| 134 | if (fragment == null) { |
| 135 | fragment = new ContactDeletionInteraction(); |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 136 | fragment.setTestLoaderManager(testLoaderManager); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 137 | fragment.setContactUri(contactUri); |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 138 | fragment.setFinishActivityWhenDone(finishActivityWhenDone); |
Minh Pham | 4b25da7 | 2011-08-25 13:50:38 -0700 | [diff] [blame] | 139 | fragmentManager.beginTransaction().add(fragment, FRAGMENT_TAG) |
| 140 | .commitAllowingStateLoss(); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 141 | } else { |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 142 | fragment.setTestLoaderManager(testLoaderManager); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 143 | fragment.setContactUri(contactUri); |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 144 | fragment.setFinishActivityWhenDone(finishActivityWhenDone); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 145 | } |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 146 | return fragment; |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 150 | public LoaderManager getLoaderManager() { |
| 151 | // Return the TestLoaderManager if one is set up. |
| 152 | LoaderManager loaderManager = super.getLoaderManager(); |
| 153 | if (mTestLoaderManager != null) { |
| 154 | // Set the delegate: this operation is idempotent, so let's just do it every time. |
| 155 | mTestLoaderManager.setDelegate(loaderManager); |
| 156 | return mTestLoaderManager; |
| 157 | } else { |
| 158 | return loaderManager; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** Sets the TestLoaderManager that is used to wrap the actual LoaderManager in tests. */ |
Brian Attwell | 09bfb91 | 2014-12-18 20:11:17 -0800 | [diff] [blame] | 163 | private void setTestLoaderManager(TestLoaderManagerBase mockLoaderManager) { |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 164 | mTestLoaderManager = mockLoaderManager; |
| 165 | } |
| 166 | |
| 167 | @Override |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 168 | public void onAttach(Activity activity) { |
| 169 | super.onAttach(activity); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 170 | mContext = activity; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Dmitri Plotnikov | 0279fca | 2011-02-23 17:47:31 -0800 | [diff] [blame] | 173 | @Override |
| 174 | public void onDestroyView() { |
| 175 | super.onDestroyView(); |
| 176 | if (mDialog != null && mDialog.isShowing()) { |
| 177 | mDialog.setOnDismissListener(null); |
| 178 | mDialog.dismiss(); |
| 179 | mDialog = null; |
| 180 | } |
| 181 | } |
| 182 | |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 183 | public void setContactUri(Uri contactUri) { |
| 184 | mContactUri = contactUri; |
| 185 | mActive = true; |
| 186 | if (isStarted()) { |
| 187 | Bundle args = new Bundle(); |
| 188 | args.putParcelable(ARG_CONTACT_URI, mContactUri); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 189 | getLoaderManager().restartLoader(R.id.dialog_delete_contact_loader_id, args, this); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 190 | } |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 193 | private void setFinishActivityWhenDone(boolean finishActivityWhenDone) { |
| 194 | this.mFinishActivityWhenDone = finishActivityWhenDone; |
| 195 | |
| 196 | } |
| 197 | |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 198 | /* Visible for testing */ |
| 199 | boolean isStarted() { |
| 200 | return isAdded(); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void onStart() { |
| 205 | if (mActive) { |
| 206 | Bundle args = new Bundle(); |
| 207 | args.putParcelable(ARG_CONTACT_URI, mContactUri); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 208 | getLoaderManager().initLoader(R.id.dialog_delete_contact_loader_id, args, this); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 209 | } |
| 210 | super.onStart(); |
| 211 | } |
| 212 | |
| 213 | @Override |
Dmitri Plotnikov | 0279fca | 2011-02-23 17:47:31 -0800 | [diff] [blame] | 214 | public void onStop() { |
| 215 | super.onStop(); |
| 216 | if (mDialog != null) { |
| 217 | mDialog.hide(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 222 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 223 | Uri contactUri = args.getParcelable(ARG_CONTACT_URI); |
| 224 | return new CursorLoader(mContext, |
| 225 | Uri.withAppendedPath(contactUri, Entity.CONTENT_DIRECTORY), ENTITY_PROJECTION, |
| 226 | null, null, null); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
| 231 | if (mDialog != null) { |
| 232 | mDialog.dismiss(); |
| 233 | mDialog = null; |
| 234 | } |
| 235 | |
| 236 | if (!mActive) { |
| 237 | return; |
| 238 | } |
| 239 | |
Jay Shrauner | 053f723 | 2015-03-02 12:47:54 -0800 | [diff] [blame] | 240 | if (cursor == null || cursor.isClosed()) { |
| 241 | Log.e(TAG, "Failed to load contacts"); |
| 242 | return; |
| 243 | } |
| 244 | |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 245 | long contactId = 0; |
| 246 | String lookupKey = null; |
| 247 | |
| 248 | // This cursor may contain duplicate raw contacts, so we need to de-dupe them first |
| 249 | HashSet<Long> readOnlyRawContacts = Sets.newHashSet(); |
| 250 | HashSet<Long> writableRawContacts = Sets.newHashSet(); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 251 | |
Dmitri Plotnikov | a07fa5f | 2011-01-09 11:56:50 -0800 | [diff] [blame] | 252 | AccountTypeManager accountTypes = AccountTypeManager.getInstance(getActivity()); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 253 | cursor.moveToPosition(-1); |
| 254 | while (cursor.moveToNext()) { |
| 255 | final long rawContactId = cursor.getLong(COLUMN_INDEX_RAW_CONTACT_ID); |
| 256 | final String accountType = cursor.getString(COLUMN_INDEX_ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 257 | final String dataSet = cursor.getString(COLUMN_INDEX_DATA_SET); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 258 | contactId = cursor.getLong(COLUMN_INDEX_CONTACT_ID); |
| 259 | lookupKey = cursor.getString(COLUMN_INDEX_LOOKUP_KEY); |
James Laskey | 4beed7e | 2016-09-15 13:52:19 -0700 | [diff] [blame] | 260 | mDisplayName = cursor.getString(COLUMN_INDEX_DISPLAY_NAME); |
James Laskey | e5a140a | 2016-10-18 15:43:42 -0700 | [diff] [blame] | 261 | mDisplayNameAlt = cursor.getString(COLUMN_INDEX_DISPLAY_NAME_ALT); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 262 | AccountType type = accountTypes.getAccountType(accountType, dataSet); |
Daniel Lehmann | 96e87fd | 2011-09-20 21:31:37 -0700 | [diff] [blame] | 263 | boolean writable = type == null || type.areContactsWritable(); |
| 264 | if (writable) { |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 265 | writableRawContacts.add(rawContactId); |
Daniel Lehmann | 96e87fd | 2011-09-20 21:31:37 -0700 | [diff] [blame] | 266 | } else { |
| 267 | readOnlyRawContacts.add(rawContactId); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 268 | } |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 269 | } |
Jay Shrauner | e320c0b | 2015-03-05 12:45:18 -0800 | [diff] [blame] | 270 | if (TextUtils.isEmpty(lookupKey)) { |
| 271 | Log.e(TAG, "Failed to find contact lookup key"); |
| 272 | getActivity().finish(); |
| 273 | return; |
| 274 | } |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 275 | |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 276 | int readOnlyCount = readOnlyRawContacts.size(); |
| 277 | int writableCount = writableRawContacts.size(); |
Tingting Wang | 168331d | 2015-10-30 12:00:57 -0700 | [diff] [blame] | 278 | int positiveButtonId = android.R.string.ok; |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 279 | if (readOnlyCount > 0 && writableCount > 0) { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 280 | mMessageId = R.string.readOnlyContactDeleteConfirmation; |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 281 | } else if (readOnlyCount > 0 && writableCount == 0) { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 282 | mMessageId = R.string.readOnlyContactWarning; |
Tingting Wang | 07bc676 | 2015-10-30 15:19:38 -0700 | [diff] [blame] | 283 | positiveButtonId = R.string.readOnlyContactWarning_positive_button; |
Dmitri Plotnikov | 355d0f4 | 2010-11-03 17:13:51 -0700 | [diff] [blame] | 284 | } else if (readOnlyCount == 0 && writableCount > 1) { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 285 | mMessageId = R.string.multipleContactDeleteConfirmation; |
Tingting Wang | 168331d | 2015-10-30 12:00:57 -0700 | [diff] [blame] | 286 | positiveButtonId = R.string.deleteConfirmation_positive_button; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 287 | } else { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 288 | mMessageId = R.string.deleteConfirmation; |
Tingting Wang | 168331d | 2015-10-30 12:00:57 -0700 | [diff] [blame] | 289 | positiveButtonId = R.string.deleteConfirmation_positive_button; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 292 | final Uri contactUri = Contacts.getLookupUri(contactId, lookupKey); |
Tingting Wang | 168331d | 2015-10-30 12:00:57 -0700 | [diff] [blame] | 293 | showDialog(mMessageId, positiveButtonId, contactUri); |
Daisuke Miyakawa | d7c148b | 2011-03-31 18:33:35 -0700 | [diff] [blame] | 294 | |
| 295 | // We don't want onLoadFinished() calls any more, which may come when the database is |
| 296 | // updating. |
| 297 | getLoaderManager().destroyLoader(R.id.dialog_delete_contact_loader_id); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Daisuke Miyakawa | d7c148b | 2011-03-31 18:33:35 -0700 | [diff] [blame] | 300 | @Override |
Dianne Hackborn | 4ef95cc | 2010-12-16 00:44:33 -0800 | [diff] [blame] | 301 | public void onLoaderReset(Loader<Cursor> loader) { |
| 302 | } |
Dmitri Plotnikov | 69f9e6f | 2011-01-03 15:47:24 -0800 | [diff] [blame] | 303 | |
Tingting Wang | 168331d | 2015-10-30 12:00:57 -0700 | [diff] [blame] | 304 | private void showDialog(int messageId, int positiveButtonId, final Uri contactUri) { |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 305 | mDialog = new AlertDialog.Builder(getActivity()) |
Dmitri Plotnikov | 4292dfa | 2011-01-23 10:21:18 -0800 | [diff] [blame] | 306 | .setIconAttribute(android.R.attr.alertDialogIcon) |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 307 | .setMessage(messageId) |
| 308 | .setNegativeButton(android.R.string.cancel, null) |
Tingting Wang | 168331d | 2015-10-30 12:00:57 -0700 | [diff] [blame] | 309 | .setPositiveButton(positiveButtonId, |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 310 | new DialogInterface.OnClickListener() { |
| 311 | @Override |
| 312 | public void onClick(DialogInterface dialog, int whichButton) { |
| 313 | doDeleteContact(contactUri); |
| 314 | } |
| 315 | } |
| 316 | ) |
| 317 | .create(); |
| 318 | |
| 319 | mDialog.setOnDismissListener(this); |
| 320 | mDialog.show(); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void onDismiss(DialogInterface dialog) { |
| 325 | mActive = false; |
| 326 | mDialog = null; |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void onSaveInstanceState(Bundle outState) { |
| 331 | super.onSaveInstanceState(outState); |
| 332 | outState.putBoolean(KEY_ACTIVE, mActive); |
Dmitri Plotnikov | cc42b45 | 2011-01-20 11:44:44 -0800 | [diff] [blame] | 333 | outState.putParcelable(KEY_CONTACT_URI, mContactUri); |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 334 | outState.putBoolean(KEY_FINISH_WHEN_DONE, mFinishActivityWhenDone); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void onActivityCreated(Bundle savedInstanceState) { |
| 339 | super.onActivityCreated(savedInstanceState); |
| 340 | if (savedInstanceState != null) { |
| 341 | mActive = savedInstanceState.getBoolean(KEY_ACTIVE); |
Dmitri Plotnikov | cc42b45 | 2011-01-20 11:44:44 -0800 | [diff] [blame] | 342 | mContactUri = savedInstanceState.getParcelable(KEY_CONTACT_URI); |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 343 | mFinishActivityWhenDone = savedInstanceState.getBoolean(KEY_FINISH_WHEN_DONE); |
Dmitri Plotnikov | 66bca8f | 2010-11-24 16:59:24 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | protected void doDeleteContact(Uri contactUri) { |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 348 | mContext.startService(ContactSaveService.createDeleteContactIntent(mContext, contactUri)); |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 349 | if (isAdded() && mFinishActivityWhenDone) { |
Brian Attwell | 8a6f4ad | 2014-06-06 21:54:53 -0700 | [diff] [blame] | 350 | getActivity().setResult(RESULT_CODE_DELETED); |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 351 | getActivity().finish(); |
James Laskey | 4beed7e | 2016-09-15 13:52:19 -0700 | [diff] [blame] | 352 | final String deleteToastMessage; |
James Laskey | e5a140a | 2016-10-18 15:43:42 -0700 | [diff] [blame] | 353 | final String name = ContactDisplayUtils.getPreferredDisplayName(mDisplayName, |
| 354 | mDisplayNameAlt, new ContactsPreferences(mContext)); |
| 355 | if (TextUtils.isEmpty(name)) { |
James Laskey | 4beed7e | 2016-09-15 13:52:19 -0700 | [diff] [blame] | 356 | deleteToastMessage = getResources().getQuantityString( |
| 357 | R.plurals.contacts_deleted_toast, /* quantity */ 1); |
| 358 | } else { |
| 359 | deleteToastMessage = getResources().getString( |
James Laskey | e5a140a | 2016-10-18 15:43:42 -0700 | [diff] [blame] | 360 | R.string.contacts_deleted_one_named_toast, name); |
James Laskey | 4beed7e | 2016-09-15 13:52:19 -0700 | [diff] [blame] | 361 | } |
Wenyi Wang | 687d218 | 2015-10-28 17:03:18 -0700 | [diff] [blame] | 362 | Toast.makeText(mContext, deleteToastMessage, Toast.LENGTH_LONG).show(); |
Dmitri Plotnikov | c9eda37 | 2011-02-23 08:58:13 -0800 | [diff] [blame] | 363 | } |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 364 | } |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 365 | } |