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 version 2,
7 * as published by the Free Software Foundation.
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
;
26 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
27 import com
.owncloud
.android
.utils
.Log_OC
;
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 public static final String FTAG_CONFIRMATION
= "CONFIRMATION_FRAGMENT";
41 private ConfirmationDialogFragmentListener mListener
;
44 * Public factory method to create new ConfirmationDialogFragment instances.
46 * @param string_id Resource id for a message to show in the dialog.
47 * @param arguments Arguments to complete the message, if it's a format string.
48 * @param posBtn Resource id for the text of the positive button.
49 * @param neuBtn Resource id for the text of the neutral button.
50 * @param negBtn Resource id for the text of the negative button.
51 * @return Dialog ready to show.
53 public static ConfirmationDialogFragment
newInstance(int string_id
, String
[] arguments
, int posBtn
, int neuBtn
, int negBtn
) {
54 ConfirmationDialogFragment frag
= new ConfirmationDialogFragment();
55 Bundle args
= new Bundle();
56 args
.putInt(ARG_CONF_RESOURCE_ID
, string_id
);
57 args
.putStringArray(ARG_CONF_ARGUMENTS
, arguments
);
58 args
.putInt(ARG_POSITIVE_BTN_RES
, posBtn
);
59 args
.putInt(ARG_NEUTRAL_BTN_RES
, neuBtn
);
60 args
.putInt(ARG_NEGATIVE_BTN_RES
, negBtn
);
61 frag
.setArguments(args
);
65 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener
) {
70 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
71 Object
[] confirmationTarget
= getArguments().getStringArray(ARG_CONF_ARGUMENTS
);
72 int resourceId
= getArguments().getInt(ARG_CONF_RESOURCE_ID
, -1);
73 int posBtn
= getArguments().getInt(ARG_POSITIVE_BTN_RES
, -1);
74 int neuBtn
= getArguments().getInt(ARG_NEUTRAL_BTN_RES
, -1);
75 int negBtn
= getArguments().getInt(ARG_NEGATIVE_BTN_RES
, -1);
77 if (confirmationTarget
== null
|| resourceId
== -1) {
78 Log_OC
.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
82 AlertDialog
.Builder builder
= new AlertDialog
.Builder(getActivity())
83 .setIcon(android
.R
.drawable
.ic_dialog_alert
)
84 .setMessage(String
.format(getString(resourceId
), confirmationTarget
))
85 .setTitle(android
.R
.string
.dialog_alert_title
);
86 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
87 builder
.setIconAttribute(android
.R
.attr
.alertDialogIcon
);
91 builder
.setPositiveButton(posBtn
,
92 new DialogInterface
.OnClickListener() {
93 public void onClick(DialogInterface dialog
, int whichButton
) {
94 mListener
.onConfirmation(getTag());
99 builder
.setNeutralButton(neuBtn
,
100 new DialogInterface
.OnClickListener() {
101 public void onClick(DialogInterface dialog
, int whichButton
) {
102 mListener
.onNeutral(getTag());
107 builder
.setNegativeButton(negBtn
,
108 new DialogInterface
.OnClickListener() {
110 public void onClick(DialogInterface dialog
, int which
) {
111 mListener
.onCancel(getTag());
115 return builder
.create();
119 public interface ConfirmationDialogFragmentListener
{
120 public void onConfirmation(String callerTag
);
121 public void onNeutral(String callerTag
);
122 public void onCancel(String callerTag
);