Give the user a chance to move the files selected for an upload when there is not...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ConfirmationDialogFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.ui.fragment;
20
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 import android.util.Log;
26
27 import com.actionbarsherlock.app.SherlockDialogFragment;
28
29 public class ConfirmationDialogFragment extends SherlockDialogFragment {
30
31 public final static String ARG_CONF_RESOURCE_ID = "resource_id";
32 public final static String ARG_CONF_ARGUMENTS = "string_array";
33
34 public final static String ARG_POSITIVE_BTN_RES = "positive_btn_res";
35 public final static String ARG_NEUTRAL_BTN_RES = "neutral_btn_res";
36 public final static String ARG_NEGATIVE_BTN_RES = "negative_btn_res";
37
38 private ConfirmationDialogFragmentListener mListener;
39
40 /**
41 * Public factory method to create new ConfirmationDialogFragment instances.
42 *
43 * @param string_id Resource id for a message to show in the dialog.
44 * @param arguments Arguments to complete the message, if it's a format string.
45 * @param posBtn Resource id for the text of the positive button.
46 * @param neuBtn Resource id for the text of the neutral button.
47 * @param negBtn Resource id for the text of the negative button.
48 * @return Dialog ready to show.
49 */
50 public static ConfirmationDialogFragment newInstance(int string_id, String[] arguments, int posBtn, int neuBtn, int negBtn) {
51 ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
52 Bundle args = new Bundle();
53 args.putInt(ARG_CONF_RESOURCE_ID, string_id);
54 args.putStringArray(ARG_CONF_ARGUMENTS, arguments);
55 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
56 args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
57 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
58 frag.setArguments(args);
59 return frag;
60 }
61
62 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
63 mListener = listener;
64 }
65
66 @Override
67 public Dialog onCreateDialog(Bundle savedInstanceState) {
68 Object[] confirmationTarget = getArguments().getStringArray(ARG_CONF_ARGUMENTS);
69 int resourceId = getArguments().getInt(ARG_CONF_RESOURCE_ID, -1);
70 int posBtn = getArguments().getInt(ARG_POSITIVE_BTN_RES, -1);
71 int neuBtn = getArguments().getInt(ARG_NEUTRAL_BTN_RES, -1);
72 int negBtn = getArguments().getInt(ARG_NEGATIVE_BTN_RES, -1);
73
74 if (confirmationTarget == null || resourceId == -1) {
75 Log.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
76 return null;
77 }
78
79 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
80 .setIcon(android.R.drawable.ic_dialog_alert)
81 .setMessage(String.format(getString(resourceId), confirmationTarget))
82 .setTitle(android.R.string.dialog_alert_title);
83 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
84 builder.setIconAttribute(android.R.attr.alertDialogIcon);
85 }
86
87 if (posBtn != -1)
88 builder.setPositiveButton(posBtn,
89 new DialogInterface.OnClickListener() {
90 public void onClick(DialogInterface dialog, int whichButton) {
91 mListener.onConfirmation(getTag());
92 }
93 });
94 if (neuBtn != -1)
95 builder.setNeutralButton(neuBtn,
96 new DialogInterface.OnClickListener() {
97 public void onClick(DialogInterface dialog, int whichButton) {
98 mListener.onNeutral(getTag());
99 }
100 });
101 if (negBtn != -1)
102 builder.setNegativeButton(negBtn,
103 new DialogInterface.OnClickListener() {
104 @Override
105 public void onClick(DialogInterface dialog, int which) {
106 mListener.onCancel(getTag());
107 }
108 });
109 return builder.create();
110 }
111
112
113 public interface ConfirmationDialogFragmentListener {
114 public void onConfirmation(String callerTag);
115 public void onNeutral(String callerTag);
116 public void onCancel(String callerTag);
117 }
118
119 }
120