48aabe5c755fa9c876e83a1d98293ad138afd57b
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / ConfirmationDialogFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.ui.dialog;
22
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.DialogInterface;
26 import android.os.Bundle;
27
28 import com.actionbarsherlock.app.SherlockDialogFragment;
29 import com.owncloud.android.lib.common.utils.Log_OC;
30
31
32 public class ConfirmationDialogFragment extends SherlockDialogFragment {
33
34 public final static String ARG_CONF_RESOURCE_ID = "resource_id";
35 public final static String ARG_CONF_ARGUMENTS = "string_array";
36
37 public final static String ARG_POSITIVE_BTN_RES = "positive_btn_res";
38 public final static String ARG_NEUTRAL_BTN_RES = "neutral_btn_res";
39 public final static String ARG_NEGATIVE_BTN_RES = "negative_btn_res";
40
41 public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
42
43 private ConfirmationDialogFragmentListener mListener;
44
45 /**
46 * Public factory method to create new ConfirmationDialogFragment instances.
47 *
48 * @param string_id Resource id for a message to show in the dialog.
49 * @param arguments Arguments to complete the message, if it's a format string.
50 * @param posBtn Resource id for the text of the positive button.
51 * @param neuBtn Resource id for the text of the neutral button.
52 * @param negBtn Resource id for the text of the negative button.
53 * @return Dialog ready to show.
54 */
55 public static ConfirmationDialogFragment newInstance(int string_id, String[] arguments, int posBtn, int neuBtn, int negBtn) {
56 ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
57 Bundle args = new Bundle();
58 args.putInt(ARG_CONF_RESOURCE_ID, string_id);
59 args.putStringArray(ARG_CONF_ARGUMENTS, arguments);
60 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
61 args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
62 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
63 frag.setArguments(args);
64 return frag;
65 }
66
67 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
68 mListener = listener;
69 }
70
71 @Override
72 public Dialog onCreateDialog(Bundle savedInstanceState) {
73 Object[] confirmationTarget = getArguments().getStringArray(ARG_CONF_ARGUMENTS);
74 int resourceId = getArguments().getInt(ARG_CONF_RESOURCE_ID, -1);
75 int posBtn = getArguments().getInt(ARG_POSITIVE_BTN_RES, -1);
76 int neuBtn = getArguments().getInt(ARG_NEUTRAL_BTN_RES, -1);
77 int negBtn = getArguments().getInt(ARG_NEGATIVE_BTN_RES, -1);
78
79 if (confirmationTarget == null || resourceId == -1) {
80 Log_OC.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
81 return null;
82 }
83
84 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
85 .setIcon(android.R.drawable.ic_dialog_alert)
86 .setMessage(String.format(getString(resourceId), confirmationTarget))
87 .setTitle(android.R.string.dialog_alert_title);
88 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
89 builder.setIconAttribute(android.R.attr.alertDialogIcon);
90 }
91
92 if (posBtn != -1)
93 builder.setPositiveButton(posBtn,
94 new DialogInterface.OnClickListener() {
95 public void onClick(DialogInterface dialog, int whichButton) {
96 if (mListener != null) {
97 mListener.onConfirmation(getTag());
98 }
99 dialog.dismiss();
100 }
101 });
102 if (neuBtn != -1)
103 builder.setNeutralButton(neuBtn,
104 new DialogInterface.OnClickListener() {
105 public void onClick(DialogInterface dialog, int whichButton) {
106 if (mListener != null) {
107 mListener.onNeutral(getTag());
108 }
109 dialog.dismiss();
110 }
111 });
112 if (negBtn != -1)
113 builder.setNegativeButton(negBtn,
114 new DialogInterface.OnClickListener() {
115 @Override
116 public void onClick(DialogInterface dialog, int which) {
117 if (mListener != null) {
118 mListener.onCancel(getTag());
119 }
120 dialog.dismiss();
121 }
122 });
123 return builder.create();
124 }
125
126
127 public interface ConfirmationDialogFragmentListener {
128 public void onConfirmation(String callerTag);
129 public void onNeutral(String callerTag);
130 public void onCancel(String callerTag);
131 }
132
133 }
134