Fixed lock of buttons in details view when the 'Keep both' option in conflicts dialog...
[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 public static ConfirmationDialogFragment newInstance(int string_id, String[] arguments, int posBtn, int neuBtn, int negBtn) {
41 ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
42 Bundle args = new Bundle();
43 args.putInt(ARG_CONF_RESOURCE_ID, string_id);
44 args.putStringArray(ARG_CONF_ARGUMENTS, arguments);
45 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
46 args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
47 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
48 frag.setArguments(args);
49 return frag;
50 }
51
52 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
53 mListener = listener;
54 }
55
56 @Override
57 public Dialog onCreateDialog(Bundle savedInstanceState) {
58 Object[] confirmationTarget = getArguments().getStringArray(ARG_CONF_ARGUMENTS);
59 int resourceId = getArguments().getInt(ARG_CONF_RESOURCE_ID, -1);
60 int posBtn = getArguments().getInt(ARG_POSITIVE_BTN_RES, -1);
61 int neuBtn = getArguments().getInt(ARG_NEUTRAL_BTN_RES, -1);
62 int negBtn = getArguments().getInt(ARG_NEGATIVE_BTN_RES, -1);
63
64 if (confirmationTarget == null || resourceId == -1) {
65 Log.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
66 return null;
67 }
68
69 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
70 .setIcon(android.R.drawable.ic_dialog_alert)
71 .setMessage(String.format(getString(resourceId), confirmationTarget))
72 .setTitle(android.R.string.dialog_alert_title);
73 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
74 builder.setIconAttribute(android.R.attr.alertDialogIcon);
75 }
76
77 if (posBtn != -1)
78 builder.setPositiveButton(posBtn,
79 new DialogInterface.OnClickListener() {
80 public void onClick(DialogInterface dialog, int whichButton) {
81 mListener.onConfirmation(getTag());
82 }
83 });
84 if (neuBtn != -1)
85 builder.setNeutralButton(neuBtn,
86 new DialogInterface.OnClickListener() {
87 public void onClick(DialogInterface dialog, int whichButton) {
88 mListener.onNeutral(getTag());
89 }
90 });
91 if (negBtn != -1)
92 builder.setNegativeButton(negBtn,
93 new DialogInterface.OnClickListener() {
94 @Override
95 public void onClick(DialogInterface dialog, int which) {
96 mListener.onCancel(getTag());
97 }
98 });
99 return builder.create();
100 }
101
102
103 public interface ConfirmationDialogFragmentListener {
104 public void onConfirmation(String callerTag);
105 public void onNeutral(String callerTag);
106 public void onCancel(String callerTag);
107 }
108
109 }
110