Breaking out contact deletion into a separate object.
Introducing a new type of object, Interaction,
which is neither Activity nor Fragment, but just
support for some particular function, e.g. deletion
of a contact.
Also, moving the query to the background thread.
Change-Id: I52d2607592fe3ca012284d8216003947dba5f188
diff --git a/res/values/ids.xml b/res/values/ids.xml
index 49f1c0b..1846824 100644
--- a/res/values/ids.xml
+++ b/res/values/ids.xml
@@ -54,9 +54,6 @@
<!-- For ContactsListActivity -->
<item type="id" name="dialog_delete_contact_confirmation"/>
- <item type="id" name="dialog_readonly_contact_hide_confirmation"/>
- <item type="id" name="dialog_multiple_contact_delete_confirmation"/>
- <item type="id" name="dialog_readonly_contact_delete_confirmation"/>
<!-- For ExportVCardActivity -->
<item type="id" name="dialog_export_confirmation"/>
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 2801618..4958be6 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -16,6 +16,7 @@
package com.android.contacts;
+import com.android.contacts.interactions.ContactDeletionInteraction;
import com.android.contacts.list.CallOrSmsInitiator;
import com.android.contacts.list.ContactBrowseListContextMenuAdapter;
import com.android.contacts.list.ContactEntryListFragment;
@@ -30,7 +31,6 @@
import com.android.contacts.list.PhoneNumberPickerFragment;
import com.android.contacts.list.PostalAddressPickerFragment;
import com.android.contacts.list.StrequentContactListFragment;
-import com.android.contacts.model.ContactsSource;
import com.android.contacts.model.Sources;
import com.android.contacts.ui.ContactsPreferencesActivity;
import com.android.contacts.util.AccountSelectionUtil;
@@ -44,8 +44,6 @@
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentTransaction;
-import android.app.SearchManager;
-import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
@@ -55,11 +53,9 @@
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
-import android.provider.Settings;
import android.provider.ContactsContract.Contacts;
-import android.provider.ContactsContract.RawContacts;
+import android.provider.Settings;
import android.telephony.TelephonyManager;
-import android.text.TextUtils;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
@@ -73,13 +69,11 @@
import android.widget.TextView;
import android.widget.Toast;
-import java.util.ArrayList;
import java.util.List;
/**
* Displays a list of contacts. Usually is embedded into the ContactsActivity.
*/
-@SuppressWarnings("deprecation")
public class ContactsListActivity extends Activity implements View.OnCreateContextMenuListener {
private static final String TAG = "ContactsListActivity";
@@ -89,33 +83,15 @@
private static final int SUBACTIVITY_DISPLAY_GROUP = 3;
private static final int SUBACTIVITY_SEARCH = 4;
- private static final String[] RAW_CONTACTS_PROJECTION = new String[] {
- RawContacts._ID, //0
- RawContacts.CONTACT_ID, //1
- RawContacts.ACCOUNT_TYPE, //2
- };
-
- private Uri mSelectedContactUri;
-
- private ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
- private int mWritableSourcesCnt;
- private int mReadOnlySourcesCnt;
-
private final String[] sLookupProjection = new String[] {
Contacts.LOOKUP_KEY
};
- private class DeleteClickListener implements DialogInterface.OnClickListener {
- public void onClick(DialogInterface dialog, int which) {
- if (mSelectedContactUri != null) {
- getContentResolver().delete(mSelectedContactUri, null, null);
- }
- }
- }
private ContactsIntentResolver mIntentResolver;
protected ContactEntryListFragment<?> mListFragment;
protected CallOrSmsInitiator mCallOrSmsInitiator;
+ private ContactDeletionInteraction mContactDeletionInteraction;
private int mActionCode;
@@ -124,17 +100,11 @@
private ContactsRequest mRequest;
private SearchEditText mSearchEditText;
+
public ContactsListActivity() {
mIntentResolver = new ContactsIntentResolver(this);
}
- /**
- * Visible for testing: makes queries run on the UI thread.
- */
- /* package */ void runQueriesSynchronously() {
- // TODO
- }
-
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
@@ -376,7 +346,7 @@
}
public void onDeleteContactAction(Uri contactUri) {
- doContactDelete(contactUri);
+ getContactDeletionInteraction().deleteContact(contactUri);
}
public void onFinishAction() {
@@ -514,6 +484,11 @@
@Override
protected Dialog onCreateDialog(int id, Bundle bundle) {
+ Dialog dialog = getContactDeletionInteraction().onCreateDialog(id, bundle);
+ if (dialog != null) {
+ return dialog;
+ }
+
switch (id) {
case R.string.import_from_sim:
case R.string.import_from_sdcard: {
@@ -521,51 +496,23 @@
}
case R.id.dialog_sdcard_not_found: {
return new AlertDialog.Builder(this)
- .setTitle(R.string.no_sdcard_title)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setMessage(R.string.no_sdcard_message)
- .setPositiveButton(android.R.string.ok, null).create();
- }
- case R.id.dialog_delete_contact_confirmation: {
- return new AlertDialog.Builder(this)
- .setTitle(R.string.deleteConfirmation_title)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setMessage(R.string.deleteConfirmation)
- .setNegativeButton(android.R.string.cancel, null)
- .setPositiveButton(android.R.string.ok,
- new DeleteClickListener()).create();
- }
- case R.id.dialog_readonly_contact_hide_confirmation: {
- return new AlertDialog.Builder(this)
- .setTitle(R.string.deleteConfirmation_title)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setMessage(R.string.readOnlyContactWarning)
- .setNegativeButton(android.R.string.cancel, null)
- .setPositiveButton(android.R.string.ok,
- new DeleteClickListener()).create();
- }
- case R.id.dialog_readonly_contact_delete_confirmation: {
- return new AlertDialog.Builder(this)
- .setTitle(R.string.deleteConfirmation_title)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setMessage(R.string.readOnlyContactDeleteConfirmation)
- .setNegativeButton(android.R.string.cancel, null)
- .setPositiveButton(android.R.string.ok,
- new DeleteClickListener()).create();
- }
- case R.id.dialog_multiple_contact_delete_confirmation: {
- return new AlertDialog.Builder(this)
- .setTitle(R.string.deleteConfirmation_title)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setMessage(R.string.multipleContactDeleteConfirmation)
- .setNegativeButton(android.R.string.cancel, null)
- .setPositiveButton(android.R.string.ok,
- new DeleteClickListener()).create();
+ .setTitle(R.string.no_sdcard_title)
+ .setIcon(android.R.drawable.ic_dialog_alert)
+ .setMessage(R.string.no_sdcard_message)
+ .setPositiveButton(android.R.string.ok, null).create();
}
}
return super.onCreateDialog(id, bundle);
}
+ @Override
+ protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
+ if (getContactDeletionInteraction().onPrepareDialog(id, dialog, bundle)) {
+ return;
+ }
+
+ super.onPrepareDialog(id, dialog, bundle);
+ }
/**
* Create a {@link Dialog} that allows the user to pick from a bulk import
* or bulk export task across all contacts.
@@ -790,53 +737,18 @@
return false;
}
- /**
- * Prompt the user before deleting the given {@link Contacts} entry.
- */
- protected void doContactDelete(Uri contactUri) {
- mReadOnlySourcesCnt = 0;
- mWritableSourcesCnt = 0;
- mWritableRawContactIds.clear();
-
- Sources sources = Sources.getInstance(ContactsListActivity.this);
- Cursor c = getContentResolver().query(RawContacts.CONTENT_URI, RAW_CONTACTS_PROJECTION,
- RawContacts.CONTACT_ID + "=" + ContentUris.parseId(contactUri), null,
- null);
- if (c != null) {
- try {
- while (c.moveToNext()) {
- final String accountType = c.getString(2);
- final long rawContactId = c.getLong(0);
- ContactsSource contactsSource = sources.getInflatedSource(accountType,
- ContactsSource.LEVEL_SUMMARY);
- if (contactsSource != null && contactsSource.readOnly) {
- mReadOnlySourcesCnt += 1;
- } else {
- mWritableSourcesCnt += 1;
- mWritableRawContactIds.add(rawContactId);
- }
- }
- } finally {
- c.close();
- }
- }
-
- mSelectedContactUri = contactUri;
- if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt > 0) {
- showDialog(R.id.dialog_readonly_contact_delete_confirmation);
- } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
- showDialog(R.id.dialog_readonly_contact_hide_confirmation);
- } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
- showDialog(R.id.dialog_multiple_contact_delete_confirmation);
- } else {
- showDialog(R.id.dialog_delete_contact_confirmation);
- }
- }
-
private CallOrSmsInitiator getCallOrSmsInitiator() {
if (mCallOrSmsInitiator == null) {
mCallOrSmsInitiator = new CallOrSmsInitiator(this);
}
return mCallOrSmsInitiator;
}
+
+ private ContactDeletionInteraction getContactDeletionInteraction() {
+ if (mContactDeletionInteraction == null) {
+ mContactDeletionInteraction = new ContactDeletionInteraction();
+ mContactDeletionInteraction.attachToActivity(this);
+ }
+ return mContactDeletionInteraction;
+ }
}
diff --git a/src/com/android/contacts/interactions/ContactDeletionInteraction.java b/src/com/android/contacts/interactions/ContactDeletionInteraction.java
new file mode 100644
index 0000000..2f5a290
--- /dev/null
+++ b/src/com/android/contacts/interactions/ContactDeletionInteraction.java
@@ -0,0 +1,200 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.interactions;
+
+import com.android.contacts.R;
+import com.android.contacts.model.ContactsSource;
+import com.android.contacts.model.Sources;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.ContentUris;
+import android.content.Context;
+import android.content.CursorLoader;
+import android.content.DialogInterface;
+import android.content.Loader;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.ContactsContract.RawContacts;
+import android.util.Log;
+
+/**
+ * An interaction invoked to delete a contact.
+ */
+public class ContactDeletionInteraction {
+
+ private static final String TAG = "ContactDeletionInteraction";
+
+ public static final String EXTRA_KEY_CONTACT_URI = "contactUri";
+ public static final String EXTRA_KEY_MESSAGE_ID = "messageId";
+
+ private static final String[] RAW_CONTACTS_PROJECTION = new String[] {
+ RawContacts._ID, //0
+ RawContacts.ACCOUNT_TYPE, //1
+ };
+
+ private final class RawContactLoader extends CursorLoader {
+ private final Uri mContactUri;
+
+ private RawContactLoader(Context context, Uri contactUri) {
+ super(context,
+ RawContacts.CONTENT_URI,
+ RAW_CONTACTS_PROJECTION,
+ RawContacts.CONTACT_ID + "=?",
+ new String[] { String.valueOf(ContentUris.parseId(contactUri)) },
+ null);
+ this.mContactUri = contactUri;
+ }
+
+ @Override
+ public void deliverResult(Cursor data) {
+ if (data == null || data.getCount() == 0) {
+ Log.e(TAG, "No such contact: " + mContactUri);
+ return;
+ }
+
+ showConfirmationDialog(data, mContactUri);
+ }
+ }
+
+ private final class ConfirmationDialogClickListener implements DialogInterface.OnClickListener {
+ private final Uri mContactUri;
+
+ public ConfirmationDialogClickListener(Uri contactUri) {
+ this.mContactUri = contactUri;
+ }
+
+ public void onClick(DialogInterface dialog, int which) {
+ doDeleteContact(mContactUri);
+ }
+ }
+
+ private Context mContext;
+ private CursorLoader mLoader;
+
+ public void attachToActivity(Activity activity) {
+ setContext(activity);
+ }
+
+ /* Visible for testing */
+ void setContext(Context context) {
+ mContext = context;
+ }
+
+ public void deleteContact(Uri contactUri) {
+ if (mLoader != null) {
+ mLoader.destroy();
+ }
+ mLoader = new RawContactLoader(mContext, contactUri);
+ startLoading(mLoader);
+ }
+
+ protected void showConfirmationDialog(Cursor cursor, Uri contactUri) {
+ int writableSourcesCnt = 0;
+ int readOnlySourcesCnt = 0;
+
+ Sources sources = getSources();
+ try {
+ while (cursor.moveToNext()) {
+ final long rawContactId = cursor.getLong(0);
+ final String accountType = cursor.getString(1);
+ ContactsSource contactsSource = sources.getInflatedSource(accountType,
+ ContactsSource.LEVEL_SUMMARY);
+ boolean readonly = contactsSource != null && contactsSource.readOnly;
+ if (readonly) {
+ readOnlySourcesCnt ++;
+ } else {
+ writableSourcesCnt ++;
+ }
+ }
+ } finally {
+ cursor.close();
+ }
+
+ int messageId;
+ if (readOnlySourcesCnt > 0 && writableSourcesCnt > 0) {
+ messageId = R.string.readOnlyContactDeleteConfirmation;
+ } else if (readOnlySourcesCnt > 0 && writableSourcesCnt == 0) {
+ messageId = R.string.readOnlyContactWarning;
+ } else if (readOnlySourcesCnt == 0 && writableSourcesCnt > 1) {
+ messageId = R.string.multipleContactDeleteConfirmation;
+ } else {
+ messageId = R.string.deleteConfirmation;
+ }
+
+ Bundle bundle = new Bundle();
+ bundle.putParcelable(EXTRA_KEY_CONTACT_URI, contactUri);
+ bundle.putInt(EXTRA_KEY_MESSAGE_ID, messageId);
+
+ showDialog(bundle);
+ }
+
+ /**
+ * Creates a delete confirmation dialog and returns it. Returns null if the
+ * id is not for a deletion confirmation.
+ */
+ public Dialog onCreateDialog(int id, Bundle bundle) {
+ if (id != R.id.dialog_delete_contact_confirmation) {
+ return null;
+ }
+
+ return new AlertDialog.Builder(mContext)
+ .setTitle(R.string.deleteConfirmation_title)
+ .setIcon(android.R.drawable.ic_dialog_alert)
+ .setMessage(R.string.deleteConfirmation)
+ .setNegativeButton(android.R.string.cancel, null)
+ .setPositiveButton(android.R.string.ok, null)
+ .create();
+ }
+
+ public boolean onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
+ if (id != R.id.dialog_delete_contact_confirmation) {
+ return false;
+ }
+
+ Uri contactUri = bundle.getParcelable(EXTRA_KEY_CONTACT_URI);
+ int messageId = bundle.getInt(EXTRA_KEY_MESSAGE_ID, R.string.deleteConfirmation);
+
+ ((AlertDialog)dialog).setMessage(mContext.getText(messageId));
+ ((AlertDialog)dialog).setButton(DialogInterface.BUTTON_POSITIVE,
+ mContext.getText(android.R.string.ok),
+ new ConfirmationDialogClickListener(contactUri));
+
+ return true;
+ }
+
+ protected void doDeleteContact(Uri contactUri) {
+ mContext.getContentResolver().delete(contactUri, null, null);
+ }
+
+ /* Visible for testing */
+ void startLoading(Loader<Cursor> loader) {
+ loader.startLoading();
+ }
+
+ /* Visible for testing */
+ Sources getSources() {
+ return Sources.getInstance(mContext);
+ }
+
+ /* Visible for testing */
+ void showDialog(Bundle bundle) {
+ ((Activity)mContext).showDialog(R.id.dialog_delete_contact_confirmation, bundle);
+ }
+}
diff --git a/tests/src/com/android/contacts/interactions/ContactDeletionInteractionTest.java b/tests/src/com/android/contacts/interactions/ContactDeletionInteractionTest.java
new file mode 100644
index 0000000..b2ca81d
--- /dev/null
+++ b/tests/src/com/android/contacts/interactions/ContactDeletionInteractionTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.interactions;
+
+import com.android.contacts.R;
+import com.android.contacts.model.Sources;
+import com.android.contacts.tests.mocks.ContactsMockContext;
+import com.android.contacts.tests.mocks.MockContentProvider;
+import com.android.contacts.tests.mocks.MockContentProvider.Query;
+import com.android.contacts.tests.mocks.MockSources;
+
+import android.content.AsyncTaskLoader;
+import android.content.ContentUris;
+import android.content.Loader;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.ContactsContract.Contacts;
+import android.provider.ContactsContract.RawContacts;
+import android.test.InstrumentationTestCase;
+import android.test.suitebuilder.annotation.Smoke;
+
+/**
+ * Tests for {@link ContactDeletionInteraction}.
+ *
+ * Running all tests:
+ *
+ * runtest contacts
+ * or
+ * adb shell am instrument \
+ * -w com.android.contacts.tests/android.test.InstrumentationTestRunner
+ */
+@Smoke
+public class ContactDeletionInteractionTest extends InstrumentationTestCase {
+
+ private final static class TestContactDeletionInteraction extends ContactDeletionInteraction {
+ public Uri contactUri;
+ public int messageId;
+
+ @Override
+ void startLoading(Loader<Cursor> loader) {
+ // Execute the loader synchronously
+ AsyncTaskLoader<Cursor> atLoader = (AsyncTaskLoader<Cursor>)loader;
+ Cursor data = atLoader.loadInBackground();
+ atLoader.deliverResult(data);
+ }
+
+ @Override
+ void showDialog(Bundle bundle) {
+ contactUri = bundle.getParcelable(EXTRA_KEY_CONTACT_URI);
+ messageId = bundle.getInt(EXTRA_KEY_MESSAGE_ID);
+ }
+
+ @Override
+ Sources getSources() {
+ return new MockSources();
+ }
+ }
+
+ private ContactsMockContext mContext;
+ private MockContentProvider mContactsProvider;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
+ mContactsProvider = mContext.getContactsProvider();
+ }
+
+ public void testSingleWritableRawContact() {
+ expectQuery().returnRow(1, MockSources.WRITABLE_ACCOUNT_TYPE);
+ assertWithMessageId(R.string.deleteConfirmation);
+ }
+
+ public void testReadOnlyRawContacts() {
+ expectQuery().returnRow(1, MockSources.READONLY_ACCOUNT_TYPE);
+ assertWithMessageId(R.string.readOnlyContactWarning);
+ }
+
+ public void testMixOfWritableAndReadOnlyRawContacts() {
+ expectQuery()
+ .returnRow(1, MockSources.WRITABLE_ACCOUNT_TYPE)
+ .returnRow(2, MockSources.READONLY_ACCOUNT_TYPE);
+ assertWithMessageId(R.string.readOnlyContactDeleteConfirmation);
+ }
+
+ public void testMultipleWritableRawContacts() {
+ expectQuery()
+ .returnRow(1, MockSources.WRITABLE_ACCOUNT_TYPE)
+ .returnRow(2, MockSources.WRITABLE_ACCOUNT_TYPE);
+ assertWithMessageId(R.string.multipleContactDeleteConfirmation);
+ }
+
+ private Query expectQuery() {
+ return mContactsProvider.expectQuery(RawContacts.CONTENT_URI)
+ .withProjection(RawContacts._ID, RawContacts.ACCOUNT_TYPE)
+ .withSelection("contact_id=?", "13");
+ }
+
+ private void assertWithMessageId(int messageId) {
+ TestContactDeletionInteraction interaction = new TestContactDeletionInteraction();
+ interaction.setContext(mContext);
+ interaction.deleteContact(ContentUris.withAppendedId(Contacts.CONTENT_URI, 13));
+ assertEquals("content://com.android.contacts/contacts/13",
+ interaction.contactUri.toString());
+ assertEquals(messageId, interaction.messageId);
+ mContactsProvider.verify();
+ }
+}
diff --git a/tests/src/com/android/contacts/tests/mocks/MockSources.java b/tests/src/com/android/contacts/tests/mocks/MockSources.java
new file mode 100644
index 0000000..201928f
--- /dev/null
+++ b/tests/src/com/android/contacts/tests/mocks/MockSources.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.contacts.tests.mocks;
+
+import com.android.contacts.model.ContactsSource;
+import com.android.contacts.model.FallbackSource;
+import com.android.contacts.model.Sources;
+
+/**
+ * A mock {@link Sources} class.
+ */
+public class MockSources extends Sources {
+
+ public static final String WRITABLE_ACCOUNT_TYPE = "writable";
+ public static final String READONLY_ACCOUNT_TYPE = "readonly";
+
+ @Override
+ public ContactsSource getInflatedSource(String accountType, int inflateLevel) {
+ if (accountType.equals(WRITABLE_ACCOUNT_TYPE)) {
+ ContactsSource source = new FallbackSource();
+ source.readOnly = false;
+ return source;
+ }
+
+ if (accountType.equals(READONLY_ACCOUNT_TYPE)) {
+ ContactsSource source = new FallbackSource();
+ source.readOnly = true;
+ return source;
+ }
+
+ return null;
+ }
+}