ConfirmationDialogFragment.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. package com.owncloud.android.ui.fragment;
  19. import android.app.AlertDialog;
  20. import android.app.Dialog;
  21. import android.content.DialogInterface;
  22. import android.os.Bundle;
  23. import com.actionbarsherlock.app.SherlockDialogFragment;
  24. import com.owncloud.android.Log_OC;
  25. public class ConfirmationDialogFragment extends SherlockDialogFragment {
  26. public final static String ARG_CONF_RESOURCE_ID = "resource_id";
  27. public final static String ARG_CONF_ARGUMENTS = "string_array";
  28. public final static String ARG_POSITIVE_BTN_RES = "positive_btn_res";
  29. public final static String ARG_NEUTRAL_BTN_RES = "neutral_btn_res";
  30. public final static String ARG_NEGATIVE_BTN_RES = "negative_btn_res";
  31. public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
  32. private ConfirmationDialogFragmentListener mListener;
  33. /**
  34. * Public factory method to create new ConfirmationDialogFragment instances.
  35. *
  36. * @param string_id Resource id for a message to show in the dialog.
  37. * @param arguments Arguments to complete the message, if it's a format string.
  38. * @param posBtn Resource id for the text of the positive button.
  39. * @param neuBtn Resource id for the text of the neutral button.
  40. * @param negBtn Resource id for the text of the negative button.
  41. * @return Dialog ready to show.
  42. */
  43. public static ConfirmationDialogFragment newInstance(int string_id, String[] arguments, int posBtn, int neuBtn, int negBtn) {
  44. ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
  45. Bundle args = new Bundle();
  46. args.putInt(ARG_CONF_RESOURCE_ID, string_id);
  47. args.putStringArray(ARG_CONF_ARGUMENTS, arguments);
  48. args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
  49. args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
  50. args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
  51. frag.setArguments(args);
  52. return frag;
  53. }
  54. public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
  55. mListener = listener;
  56. }
  57. @Override
  58. public Dialog onCreateDialog(Bundle savedInstanceState) {
  59. Object[] confirmationTarget = getArguments().getStringArray(ARG_CONF_ARGUMENTS);
  60. int resourceId = getArguments().getInt(ARG_CONF_RESOURCE_ID, -1);
  61. int posBtn = getArguments().getInt(ARG_POSITIVE_BTN_RES, -1);
  62. int neuBtn = getArguments().getInt(ARG_NEUTRAL_BTN_RES, -1);
  63. int negBtn = getArguments().getInt(ARG_NEGATIVE_BTN_RES, -1);
  64. if (confirmationTarget == null || resourceId == -1) {
  65. Log_OC.wtf(getTag(), "Calling confirmation dialog without resource or arguments");
  66. return null;
  67. }
  68. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
  69. .setIcon(android.R.drawable.ic_dialog_alert)
  70. .setMessage(String.format(getString(resourceId), confirmationTarget))
  71. .setTitle(android.R.string.dialog_alert_title);
  72. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
  73. builder.setIconAttribute(android.R.attr.alertDialogIcon);
  74. }
  75. if (posBtn != -1)
  76. builder.setPositiveButton(posBtn,
  77. new DialogInterface.OnClickListener() {
  78. public void onClick(DialogInterface dialog, int whichButton) {
  79. mListener.onConfirmation(getTag());
  80. dialog.dismiss();
  81. }
  82. });
  83. if (neuBtn != -1)
  84. builder.setNeutralButton(neuBtn,
  85. new DialogInterface.OnClickListener() {
  86. public void onClick(DialogInterface dialog, int whichButton) {
  87. mListener.onNeutral(getTag());
  88. dialog.dismiss();
  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. dialog.dismiss();
  98. }
  99. });
  100. return builder.create();
  101. }
  102. public interface ConfirmationDialogFragmentListener {
  103. public void onConfirmation(String callerTag);
  104. public void onNeutral(String callerTag);
  105. public void onCancel(String callerTag);
  106. }
  107. }