1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.fragment
;
21 import android
.app
.AlertDialog
;
22 import android
.app
.Dialog
;
23 import android
.content
.DialogInterface
;
24 import android
.os
.Bundle
;
25 import android
.util
.Log
;
27 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
29 public class ConfirmationDialogFragment
extends SherlockDialogFragment
{
31 public final static String ARG_CONF_RESOURCE_ID
= "resource_id";
32 public final static String ARG_CONF_ARGUMENTS
= "string_array";
34 public final static String ARG_POSITIVE_BTN_RES
= "positive_btn_res";
35 public final static String ARG_NEUTRAL_BTN_RES
= "neutral_btn_res";
36 public final static String ARG_NEGATIVE_BTN_RES
= "negative_btn_res";
38 public static final String FTAG_CONFIRMATION
= "CONFIRMATION_FRAGMENT";
40 private ConfirmationDialogFragmentListener mListener
;
43 * Public factory method to create new ConfirmationDialogFragment instances.
45 * @param string_id Resource id for a message to show in the dialog.
46 * @param arguments Arguments to complete the message, if it's a format string.
47 * @param posBtn Resource id for the text of the positive button.
48 * @param neuBtn Resource id for the text of the neutral button.
49 * @param negBtn Resource id for the text of the negative button.
50 * @return Dialog ready to show.
52 public static ConfirmationDialogFragment
newInstance(int string_id
, String
[] arguments
, int posBtn
, int neuBtn
, int negBtn
) {
53 ConfirmationDialogFragment frag
= new ConfirmationDialogFragment();
54 Bundle args
= new Bundle();
55 args
.putInt(ARG_CONF_RESOURCE_ID
, string_id
);
56 args
.putStringArray(ARG_CONF_ARGUMENTS
, arguments
);
57 args
.putInt(ARG_POSITIVE_BTN_RES
, posBtn
);
58 args
.putInt(ARG_NEUTRAL_BTN_RES
, neuBtn
);
59 args
.putInt(ARG_NEGATIVE_BTN_RES
, negBtn
);
60 frag
.setArguments(args
);
64 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener
) {
69 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
70 Object
[] confirmationTarget
= getArguments().getStringArray(ARG_CONF_ARGUMENTS
);
71 int resourceId
= getArguments().getInt(ARG_CONF_RESOURCE_ID
, -1);
72 int posBtn
= getArguments().getInt(ARG_POSITIVE_BTN_RES
, -1);
73 int neuBtn
= getArguments().getInt(ARG_NEUTRAL_BTN_RES
, -1);
74 int negBtn
= getArguments().getInt(ARG_NEGATIVE_BTN_RES
, -1);
76 if (confirmationTarget
== null
|| resourceId
== -1) {
77 Log
.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
81 AlertDialog
.Builder builder
= new AlertDialog
.Builder(getActivity())
82 .setIcon(android
.R
.drawable
.ic_dialog_alert
)
83 .setMessage(String
.format(getString(resourceId
), confirmationTarget
))
84 .setTitle(android
.R
.string
.dialog_alert_title
);
85 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
86 builder
.setIconAttribute(android
.R
.attr
.alertDialogIcon
);
90 builder
.setPositiveButton(posBtn
,
91 new DialogInterface
.OnClickListener() {
92 public void onClick(DialogInterface dialog
, int whichButton
) {
93 mListener
.onConfirmation(getTag());
98 builder
.setNeutralButton(neuBtn
,
99 new DialogInterface
.OnClickListener() {
100 public void onClick(DialogInterface dialog
, int whichButton
) {
101 mListener
.onNeutral(getTag());
106 builder
.setNegativeButton(negBtn
,
107 new DialogInterface
.OnClickListener() {
109 public void onClick(DialogInterface dialog
, int which
) {
110 mListener
.onCancel(getTag());
114 return builder
.create();
118 public interface ConfirmationDialogFragmentListener
{
119 public void onConfirmation(String callerTag
);
120 public void onNeutral(String callerTag
);
121 public void onCancel(String callerTag
);