OC-2332: rewrite getAvailableRemotePath from UpdateFileOperation, using ExistenceCehe...
[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 version 2,
7 * as published by the Free Software Foundation.
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
26 import com.actionbarsherlock.app.SherlockDialogFragment;
27 import com.owncloud.android.utils.Log_OC;
28
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 public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
40
41 private ConfirmationDialogFragmentListener mListener;
42
43 /**
44 * Public factory method to create new ConfirmationDialogFragment instances.
45 *
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.
52 */
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);
62 return frag;
63 }
64
65 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
66 mListener = listener;
67 }
68
69 @Override
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);
76
77 if (confirmationTarget == null || resourceId == -1) {
78 Log_OC.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
79 return null;
80 }
81
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);
88 }
89
90 if (posBtn != -1)
91 builder.setPositiveButton(posBtn,
92 new DialogInterface.OnClickListener() {
93 public void onClick(DialogInterface dialog, int whichButton) {
94 mListener.onConfirmation(getTag());
95 dialog.dismiss();
96 }
97 });
98 if (neuBtn != -1)
99 builder.setNeutralButton(neuBtn,
100 new DialogInterface.OnClickListener() {
101 public void onClick(DialogInterface dialog, int whichButton) {
102 mListener.onNeutral(getTag());
103 dialog.dismiss();
104 }
105 });
106 if (negBtn != -1)
107 builder.setNegativeButton(negBtn,
108 new DialogInterface.OnClickListener() {
109 @Override
110 public void onClick(DialogInterface dialog, int which) {
111 mListener.onCancel(getTag());
112 dialog.dismiss();
113 }
114 });
115 return builder.create();
116 }
117
118
119 public interface ConfirmationDialogFragmentListener {
120 public void onConfirmation(String callerTag);
121 public void onNeutral(String callerTag);
122 public void onCancel(String callerTag);
123 }
124
125 }
126