From ac148a1ef782ce281dce3a3cf79331f2c40fe344 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 1 Apr 2011 03:17:47 +0200 Subject: [PATCH] Extracted confirmation dialog creation to ConfirmationDialog Confirmation dialogs in MessageList and MessageView now use the new ConfirmationDialog class. --- .../fsck/k9/activity/ConfirmationDialog.java | 67 ++++++++++++++ src/com/fsck/k9/activity/MessageList.java | 90 +++++++------------ src/com/fsck/k9/activity/MessageView.java | 40 +++------ 3 files changed, 112 insertions(+), 85 deletions(-) create mode 100644 src/com/fsck/k9/activity/ConfirmationDialog.java diff --git a/src/com/fsck/k9/activity/ConfirmationDialog.java b/src/com/fsck/k9/activity/ConfirmationDialog.java new file mode 100644 index 000000000..e61039cd4 --- /dev/null +++ b/src/com/fsck/k9/activity/ConfirmationDialog.java @@ -0,0 +1,67 @@ +package com.fsck.k9.activity; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; + +public class ConfirmationDialog { + + /** + * Creates a customized confirmation dialog ({@link AlertDialog}). + * + * @param activity The activity this dialog is created for. + * @param dialogId The id that was used with {@link Activity#showDialog(int)} + * @param title The resource id of the text to display in the dialog title + * @param message The text to display in the main dialog area + * @param confirmButton The resource id of the text to display in the confirm button + * @param cancelButton The resource id of the text to display in the cancel button + * @param action The action to perform if the user presses the confirm button + * @return A confirmation dialog with the supplied arguments + */ + public static Dialog create(final Activity activity, final int dialogId, final int title, + final String message, final int confirmButton, final int cancelButton, + final Runnable action) { + + final AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setTitle(title); + builder.setMessage(message); + builder.setPositiveButton(confirmButton, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + activity.dismissDialog(dialogId); + action.run(); + } + }); + builder.setNegativeButton(cancelButton, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + activity.dismissDialog(dialogId); + } + }); + return builder.create(); + } + + /** + * Creates a customized confirmation dialog ({@link AlertDialog}). + * + * @param activity The activity this dialog is created for. + * @param dialogId The id that was used with {@link Activity#showDialog(int)} + * @param title The resource id of the text to display in the dialog title + * @param message The resource id of text to display in the main dialog area + * @param confirmButton The resource id of the text to display in the confirm button + * @param cancelButton The resource id of the text to display in the cancel button + * @param action The action to perform if the user presses the confirm button + * @return A confirmation dialog with the supplied arguments + * @see #create(Activity,int,int,String,int,int,Runnable) + */ + public static Dialog create(final Activity activity, final int dialogId, final int title, + final int message, final int confirmButton, final int cancelButton, + final Runnable action) { + + return create(activity, dialogId, title, activity.getString(message), confirmButton, + cancelButton, action); + } +} diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index 1162ac5e9..6b3730080 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -11,7 +11,6 @@ import java.util.Map; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.res.ColorStateList; import android.graphics.Typeface; @@ -1262,9 +1261,41 @@ public class MessageList public Dialog onCreateDialog(int id) { switch (id) { case DIALOG_MARK_ALL_AS_READ: - return createMarkAllAsReadDialog(); + return ConfirmationDialog.create(this, id, + R.string.mark_all_as_read_dlg_title, + getString(R.string.mark_all_as_read_dlg_instructions_fmt, + mCurrentFolder.displayName), + R.string.okay_action, + R.string.cancel_action, + new Runnable() { + @Override + public void run() { + try { + mController.markAllMessagesRead(mAccount, mCurrentFolder.name); + + synchronized (mAdapter.messages) { + for (MessageInfoHolder holder : mAdapter.messages) { + holder.read = true; + } + } + mHandler.sortMessages(); + } catch (Exception e) { + // Ignore + } + } + }); case R.id.dialog_confirm_spam: - return createConfirmSpamDialog(id); + return ConfirmationDialog.create(this, id, + R.string.dialog_confirm_spam_title, + R.string.dialog_confirm_spam_message, + R.string.dialog_confirm_spam_confirm_button, + R.string.dialog_confirm_spam_cancel_button, + new Runnable() { + @Override + public void run() { + moveToSpamFolder(mSelectedMessage); + } + }); } return super.onCreateDialog(id); @@ -1286,59 +1317,6 @@ public class MessageList } } - private Dialog createMarkAllAsReadDialog() { - return new AlertDialog.Builder(this) - .setTitle(R.string.mark_all_as_read_dlg_title) - .setMessage(getString(R.string.mark_all_as_read_dlg_instructions_fmt, - mCurrentFolder.displayName)) - .setPositiveButton(R.string.okay_action, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - dismissDialog(DIALOG_MARK_ALL_AS_READ); - - try { - mController.markAllMessagesRead(mAccount, mCurrentFolder.name); - - synchronized (mAdapter.messages) { - for (MessageInfoHolder holder : mAdapter.messages) { - holder.read = true; - } - } - mHandler.sortMessages(); - } catch (Exception e) { - // Ignore - } - } - }) - .setNegativeButton(R.string.cancel_action, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - dismissDialog(DIALOG_MARK_ALL_AS_READ); - } - }) - .create(); - } - - private Dialog createConfirmSpamDialog(final int id) { - final AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.dialog_confirm_spam_title); - builder.setMessage(R.string.dialog_confirm_spam_message); - builder.setPositiveButton(R.string.dialog_confirm_spam_confirm_button, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dismissDialog(id); - moveToSpamFolder(mSelectedMessage); - } - }); - builder.setNegativeButton(R.string.dialog_confirm_spam_cancel_button, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dismissDialog(id); - } - }); - return builder.create(); - } - private void onToggleRead(MessageInfoHolder holder) { mController.setFlag(holder.message.getFolder().getAccount(), holder.message.getFolder().getName(), new String[] { holder.uid }, Flag.SEEN, !holder.read); holder.read = !holder.read; diff --git a/src/com/fsck/k9/activity/MessageView.java b/src/com/fsck/k9/activity/MessageView.java index b937a66ab..8c120ca9a 100644 --- a/src/com/fsck/k9/activity/MessageView.java +++ b/src/com/fsck/k9/activity/MessageView.java @@ -1,10 +1,8 @@ package com.fsck.k9.activity; -import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; import android.net.Uri; @@ -563,32 +561,6 @@ public class MessageView extends K9Activity implements OnClickListener { } } - /** - * @param id - * @return Never null - */ - protected Dialog createConfirmDeleteDialog(final int id) { - final AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.dialog_confirm_delete_title); - builder.setMessage(R.string.dialog_confirm_delete_message); - builder.setPositiveButton(R.string.dialog_confirm_delete_confirm_button, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dismissDialog(id); - delete(); - } - }); - builder.setNegativeButton(R.string.dialog_confirm_delete_cancel_button, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dismissDialog(id); - } - }); - return builder.create(); - } - private void delete() { if (mMessage != null) { // Disable the delete button after it's tapped (to try to prevent @@ -944,7 +916,17 @@ public class MessageView extends K9Activity implements OnClickListener { protected Dialog onCreateDialog(final int id) { switch (id) { case R.id.dialog_confirm_delete: - return createConfirmDeleteDialog(id); + return ConfirmationDialog.create(this, id, + R.string.dialog_confirm_delete_title, + R.string.dialog_confirm_delete_message, + R.string.dialog_confirm_delete_confirm_button, + R.string.dialog_confirm_delete_cancel_button, + new Runnable() { + @Override + public void run() { + delete(); + } + }); case R.id.dialog_attachment_progress: ProgressDialog d = new ProgressDialog(this); d.setIndeterminate(true);