Bugfix für Lollipop AlertDialog Bug (see styles), moved away from android referenced...
[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.support.v7.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.DialogInterface;
26 import android.os.Bundle;
27 import android.support.v4.app.DialogFragment;
28
29 import com.owncloud.android.R;
30 import com.owncloud.android.lib.common.utils.Log_OC;
31
32
33 public class ConfirmationDialogFragment extends DialogFragment {
34
35 public final static String ARG_CONF_RESOURCE_ID = "resource_id";
36 public final static String ARG_CONF_ARGUMENTS = "string_array";
37
38 public final static String ARG_POSITIVE_BTN_RES = "positive_btn_res";
39 public final static String ARG_NEUTRAL_BTN_RES = "neutral_btn_res";
40 public final static String ARG_NEGATIVE_BTN_RES = "negative_btn_res";
41
42 public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
43
44 private ConfirmationDialogFragmentListener mListener;
45
46 /**
47 * Public factory method to create new ConfirmationDialogFragment instances.
48 *
49 * @param string_id Resource id for a message to show in the dialog.
50 * @param arguments Arguments to complete the message, if it's a format string.
51 * @param posBtn Resource id for the text of the positive button.
52 * @param neuBtn Resource id for the text of the neutral button.
53 * @param negBtn Resource id for the text of the negative button.
54 * @return Dialog ready to show.
55 */
56 public static ConfirmationDialogFragment newInstance(int string_id, String[] arguments, int posBtn, int neuBtn, int negBtn) {
57 ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
58 Bundle args = new Bundle();
59 args.putInt(ARG_CONF_RESOURCE_ID, string_id);
60 args.putStringArray(ARG_CONF_ARGUMENTS, arguments);
61 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
62 args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
63 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
64 frag.setArguments(args);
65 return frag;
66 }
67
68 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
69 mListener = listener;
70 }
71
72 @Override
73 public Dialog onCreateDialog(Bundle savedInstanceState) {
74 Object[] confirmationTarget = getArguments().getStringArray(ARG_CONF_ARGUMENTS);
75 int resourceId = getArguments().getInt(ARG_CONF_RESOURCE_ID, -1);
76 int posBtn = getArguments().getInt(ARG_POSITIVE_BTN_RES, -1);
77 int neuBtn = getArguments().getInt(ARG_NEUTRAL_BTN_RES, -1);
78 int negBtn = getArguments().getInt(ARG_NEGATIVE_BTN_RES, -1);
79
80 if (confirmationTarget == null || resourceId == -1) {
81 Log_OC.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
82 return null;
83 }
84
85 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Theme_ownCloud_Dialog)
86 .setIcon(R.drawable.ic_warning)
87 .setMessage(String.format(getString(resourceId), confirmationTarget))
88 .setTitle(android.R.string.dialog_alert_title);
89 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
90 builder.setIconAttribute(android.R.attr.alertDialogIcon);
91 }
92
93 if (posBtn != -1)
94 builder.setPositiveButton(posBtn,
95 new DialogInterface.OnClickListener() {
96 public void onClick(DialogInterface dialog, int whichButton) {
97 if (mListener != null) {
98 mListener.onConfirmation(getTag());
99 }
100 dialog.dismiss();
101 }
102 });
103 if (neuBtn != -1)
104 builder.setNeutralButton(neuBtn,
105 new DialogInterface.OnClickListener() {
106 public void onClick(DialogInterface dialog, int whichButton) {
107 if (mListener != null) {
108 mListener.onNeutral(getTag());
109 }
110 dialog.dismiss();
111 }
112 });
113 if (negBtn != -1)
114 builder.setNegativeButton(negBtn,
115 new DialogInterface.OnClickListener() {
116 @Override
117 public void onClick(DialogInterface dialog, int which) {
118 if (mListener != null) {
119 mListener.onCancel(getTag());
120 }
121 dialog.dismiss();
122 }
123 });
124 return builder.create();
125 }
126
127
128 public interface ConfirmationDialogFragmentListener {
129 public void onConfirmation(String callerTag);
130 public void onNeutral(String callerTag);
131 public void onCancel(String callerTag);
132 }
133
134 }
135