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