Fixed regression after merge: removed 'about' entry in menu
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ConfirmationDialogFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
9 *
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.
14 *
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/>.
17 *
18 */
19
20 package com.owncloud.android.ui.fragment;
21
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;
27
28 import com.actionbarsherlock.app.SherlockDialogFragment;
29 import com.owncloud.android.Log_OC;
30
31 public class ConfirmationDialogFragment extends SherlockDialogFragment {
32
33 public final static String ARG_CONF_RESOURCE_ID = "resource_id";
34 public final static String ARG_CONF_ARGUMENTS = "string_array";
35
36 public final static String ARG_POSITIVE_BTN_RES = "positive_btn_res";
37 public final static String ARG_NEUTRAL_BTN_RES = "neutral_btn_res";
38 public final static String ARG_NEGATIVE_BTN_RES = "negative_btn_res";
39
40 public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
41
42 private ConfirmationDialogFragmentListener mListener;
43
44 /**
45 * Public factory method to create new ConfirmationDialogFragment instances.
46 *
47 * @param string_id Resource id for a message to show in the dialog.
48 * @param arguments Arguments to complete the message, if it's a format string.
49 * @param posBtn Resource id for the text of the positive button.
50 * @param neuBtn Resource id for the text of the neutral button.
51 * @param negBtn Resource id for the text of the negative button.
52 * @return Dialog ready to show.
53 */
54 public static ConfirmationDialogFragment newInstance(int string_id, String[] arguments, int posBtn, int neuBtn, int negBtn) {
55 ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
56 Bundle args = new Bundle();
57 args.putInt(ARG_CONF_RESOURCE_ID, string_id);
58 args.putStringArray(ARG_CONF_ARGUMENTS, arguments);
59 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
60 args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
61 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
62 frag.setArguments(args);
63 return frag;
64 }
65
66 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
67 mListener = listener;
68 }
69
70 @Override
71 public Dialog onCreateDialog(Bundle savedInstanceState) {
72 Object[] confirmationTarget = getArguments().getStringArray(ARG_CONF_ARGUMENTS);
73 int resourceId = getArguments().getInt(ARG_CONF_RESOURCE_ID, -1);
74 int posBtn = getArguments().getInt(ARG_POSITIVE_BTN_RES, -1);
75 int neuBtn = getArguments().getInt(ARG_NEUTRAL_BTN_RES, -1);
76 int negBtn = getArguments().getInt(ARG_NEGATIVE_BTN_RES, -1);
77
78 if (confirmationTarget == null || resourceId == -1) {
79 Log_OC.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
80 return null;
81 }
82
83 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
84 .setIcon(android.R.drawable.ic_dialog_alert)
85 .setMessage(String.format(getString(resourceId), confirmationTarget))
86 .setTitle(android.R.string.dialog_alert_title);
87 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
88 builder.setIconAttribute(android.R.attr.alertDialogIcon);
89 }
90
91 if (posBtn != -1)
92 builder.setPositiveButton(posBtn,
93 new DialogInterface.OnClickListener() {
94 public void onClick(DialogInterface dialog, int whichButton) {
95 mListener.onConfirmation(getTag());
96 dialog.dismiss();
97 }
98 });
99 if (neuBtn != -1)
100 builder.setNeutralButton(neuBtn,
101 new DialogInterface.OnClickListener() {
102 public void onClick(DialogInterface dialog, int whichButton) {
103 mListener.onNeutral(getTag());
104 dialog.dismiss();
105 }
106 });
107 if (negBtn != -1)
108 builder.setNegativeButton(negBtn,
109 new DialogInterface.OnClickListener() {
110 @Override
111 public void onClick(DialogInterface dialog, int which) {
112 mListener.onCancel(getTag());
113 dialog.dismiss();
114 }
115 });
116 return builder.create();
117 }
118
119
120 public interface ConfirmationDialogFragmentListener {
121 public void onConfirmation(String callerTag);
122 public void onNeutral(String callerTag);
123 public void onCancel(String callerTag);
124 }
125
126 }
127