Fixed audio preview loop got when a tablet is torned to portrait orientation while...
[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 public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
39
40 private ConfirmationDialogFragmentListener mListener;
41
42 /**
43 * Public factory method to create new ConfirmationDialogFragment instances.
44 *
45 * @param string_id Resource id for a message to show in the dialog.
46 * @param arguments Arguments to complete the message, if it's a format string.
47 * @param posBtn Resource id for the text of the positive button.
48 * @param neuBtn Resource id for the text of the neutral button.
49 * @param negBtn Resource id for the text of the negative button.
50 * @return Dialog ready to show.
51 */
52 public static ConfirmationDialogFragment newInstance(int string_id, String[] arguments, int posBtn, int neuBtn, int negBtn) {
53 ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
54 Bundle args = new Bundle();
55 args.putInt(ARG_CONF_RESOURCE_ID, string_id);
56 args.putStringArray(ARG_CONF_ARGUMENTS, arguments);
57 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
58 args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
59 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
60 frag.setArguments(args);
61 return frag;
62 }
63
64 public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
65 mListener = listener;
66 }
67
68 @Override
69 public Dialog onCreateDialog(Bundle savedInstanceState) {
70 Object[] confirmationTarget = getArguments().getStringArray(ARG_CONF_ARGUMENTS);
71 int resourceId = getArguments().getInt(ARG_CONF_RESOURCE_ID, -1);
72 int posBtn = getArguments().getInt(ARG_POSITIVE_BTN_RES, -1);
73 int neuBtn = getArguments().getInt(ARG_NEUTRAL_BTN_RES, -1);
74 int negBtn = getArguments().getInt(ARG_NEGATIVE_BTN_RES, -1);
75
76 if (confirmationTarget == null || resourceId == -1) {
77 Log.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
78 return null;
79 }
80
81 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
82 .setIcon(android.R.drawable.ic_dialog_alert)
83 .setMessage(String.format(getString(resourceId), confirmationTarget))
84 .setTitle(android.R.string.dialog_alert_title);
85 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
86 builder.setIconAttribute(android.R.attr.alertDialogIcon);
87 }
88
89 if (posBtn != -1)
90 builder.setPositiveButton(posBtn,
91 new DialogInterface.OnClickListener() {
92 public void onClick(DialogInterface dialog, int whichButton) {
93 mListener.onConfirmation(getTag());
94 dialog.dismiss();
95 }
96 });
97 if (neuBtn != -1)
98 builder.setNeutralButton(neuBtn,
99 new DialogInterface.OnClickListener() {
100 public void onClick(DialogInterface dialog, int whichButton) {
101 mListener.onNeutral(getTag());
102 dialog.dismiss();
103 }
104 });
105 if (negBtn != -1)
106 builder.setNegativeButton(negBtn,
107 new DialogInterface.OnClickListener() {
108 @Override
109 public void onClick(DialogInterface dialog, int which) {
110 mListener.onCancel(getTag());
111 dialog.dismiss();
112 }
113 });
114 return builder.create();
115 }
116
117
118 public interface ConfirmationDialogFragmentListener {
119 public void onConfirmation(String callerTag);
120 public void onNeutral(String callerTag);
121 public void onCancel(String callerTag);
122 }
123
124 }
125