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.email.activity; 18 19 import com.android.email.R; 20 21 import android.app.AlertDialog; 22 import android.app.Dialog; 23 import android.app.DialogFragment; 24 import android.app.Fragment; 25 import android.content.Context; 26 import android.content.DialogInterface; 27 import android.content.res.Resources; 28 import android.os.Bundle; 29 30 /** 31 * Confirmation dialog for deleting messages. 32 */ 33 public class DeleteMessageConfirmationDialog extends DialogFragment 34 implements DialogInterface.OnClickListener { 35 private static final String COUNT_MESSAGES_ARG = "count_messages"; 36 37 public interface Callback { 38 public void onDeleteMessageConfirmationDialogOkPressed(); 39 } 40 41 /** 42 * Create a new dialog. 43 * 44 * @param countMessage the number of messages to be deleted 45 * @param callbackFragment fragment that implements {@link Callback}. Or null, in which case 46 * the parent activity must implement {@link Callback}. 47 */ 48 public static DeleteMessageConfirmationDialog newInstance(int countMessage, 49 Fragment callbackFragment) { 50 final DeleteMessageConfirmationDialog dialog = new DeleteMessageConfirmationDialog(); 51 final Bundle args = new Bundle(); 52 args.putInt(COUNT_MESSAGES_ARG, countMessage); 53 dialog.setArguments(args); 54 if (callbackFragment != null) { 55 dialog.setTargetFragment(callbackFragment, 0); 56 } 57 return dialog; 58 } 59 60 @Override 61 public Dialog onCreateDialog(Bundle savedInstanceState) { 62 final int countMessages = getArguments().getInt(COUNT_MESSAGES_ARG); 63 64 final Context context = getActivity(); 65 final Resources res = context.getResources(); 66 final AlertDialog.Builder b = new AlertDialog.Builder(context); 67 b.setTitle(res.getString(R.string.message_delete_dialog_title)) 68 .setIconAttribute(android.R.attr.alertDialogIcon) 69 .setMessage(res.getQuantityString(R.plurals.message_delete_confirm, countMessages)) 70 .setPositiveButton(R.string.okay_action, this) 71 .setNegativeButton(R.string.cancel_action, null); 72 return b.create(); 73 } 74 75 @Override 76 public void onClick(DialogInterface dialog, int which) { 77 switch (which) { 78 case DialogInterface.BUTTON_POSITIVE: 79 getCallback().onDeleteMessageConfirmationDialogOkPressed(); 80 break; 81 } 82 } 83 84 private Callback getCallback() { 85 Fragment targetFragment = getTargetFragment(); 86 if (targetFragment != null) { 87 // If a target is set, it MUST implement Callback. 88 return (Callback) targetFragment; 89 } 90 // If not the parent activity MUST implement Callback. 91 return (Callback) getActivity(); 92 } 93 } 94