Version up: 1.3.22
[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
30 public class ConfirmationDialogFragment extends SherlockDialogFragment {
31
32 public final static String ARG_CONF_RESOURCE_ID = "resource_id";
33 public final static String ARG_CONF_ARGUMENTS = "string_array";
34
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";
38
39 private ConfirmationDialogFragmentListener mListener;
40
41 /**
42 * Public factory method to create new ConfirmationDialogFragment instances.
43 *
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.
50 */
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);
60 return frag;
61 }
62
63 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
64 mListener = listener;
65 }
66
67 @Override
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);
74
75 if (confirmationTarget == null || resourceId == -1) {
76 Log.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
77 return null;
78 }
79
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);
86 }
87
88 if (posBtn != -1)
89 builder.setPositiveButton(posBtn,
90 new DialogInterface.OnClickListener() {
91 public void onClick(DialogInterface dialog, int whichButton) {
92 mListener.onConfirmation(getTag());
93 }
94 });
95 if (neuBtn != -1)
96 builder.setNeutralButton(neuBtn,
97 new DialogInterface.OnClickListener() {
98 public void onClick(DialogInterface dialog, int whichButton) {
99 mListener.onNeutral(getTag());
100 }
101 });
102 if (negBtn != -1)
103 builder.setNegativeButton(negBtn,
104 new DialogInterface.OnClickListener() {
105 @Override
106 public void onClick(DialogInterface dialog, int which) {
107 mListener.onCancel(getTag());
108 }
109 });
110 return builder.create();
111 }
112
113
114 public interface ConfirmationDialogFragmentListener {
115 public void onConfirmation(String callerTag);
116 public void onNeutral(String callerTag);
117 public void onCancel(String callerTag);
118 }
119
120 }
121