ConfirmationDialogFragment.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. package com.owncloud.android.ui.dialog;
  21. import android.app.Dialog;
  22. import android.content.DialogInterface;
  23. import android.os.Bundle;
  24. import android.support.v4.app.DialogFragment;
  25. import android.support.v7.app.AlertDialog;
  26. import com.owncloud.android.R;
  27. public class ConfirmationDialogFragment extends DialogFragment {
  28. public final static String ARG_MESSAGE_RESOURCE_ID = "resource_id";
  29. public final static String ARG_MESSAGE_ARGUMENTS = "string_array";
  30. private static final String ARG_TITLE_ID = "title_id";
  31. public final static String ARG_POSITIVE_BTN_RES = "positive_btn_res";
  32. public final static String ARG_NEUTRAL_BTN_RES = "neutral_btn_res";
  33. public final static String ARG_NEGATIVE_BTN_RES = "negative_btn_res";
  34. public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
  35. private ConfirmationDialogFragmentListener mListener;
  36. /**
  37. * Public factory method to create new ConfirmationDialogFragment instances.
  38. *
  39. * @param messageResId Resource id for a message to show in the dialog.
  40. * @param messageArguments Arguments to complete the message, if it's a format string. May be null.
  41. * @param titleResId Resource id for a text to show in the title.
  42. * 0 for default alert title, -1 for no title.
  43. * @param posBtn Resource id for the text of the positive button. -1 for no positive button.
  44. * @param neuBtn Resource id for the text of the neutral button. -1 for no neutral button.
  45. * @param negBtn Resource id for the text of the negative button. -1 for no negative button.
  46. * @return Dialog ready to show.
  47. */
  48. public static ConfirmationDialogFragment newInstance(
  49. int messageResId,
  50. String[] messageArguments,
  51. int titleResId,
  52. int posBtn,
  53. int neuBtn,
  54. int negBtn
  55. ) {
  56. if (messageResId == -1) {
  57. throw new IllegalStateException("Calling confirmation dialog without message resource");
  58. }
  59. ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
  60. Bundle args = new Bundle();
  61. args.putInt(ARG_MESSAGE_RESOURCE_ID, messageResId);
  62. args.putStringArray(ARG_MESSAGE_ARGUMENTS, messageArguments);
  63. args.putInt(ARG_TITLE_ID, titleResId);
  64. args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
  65. args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
  66. args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
  67. frag.setArguments(args);
  68. return frag;
  69. }
  70. public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
  71. mListener = listener;
  72. }
  73. @Override
  74. public Dialog onCreateDialog(Bundle savedInstanceState) {
  75. Object[] messageArguments = getArguments().getStringArray(ARG_MESSAGE_ARGUMENTS);
  76. int messageId = getArguments().getInt(ARG_MESSAGE_RESOURCE_ID, -1);
  77. int titleId = getArguments().getInt(ARG_TITLE_ID, -1);
  78. int posBtn = getArguments().getInt(ARG_POSITIVE_BTN_RES, -1);
  79. int neuBtn = getArguments().getInt(ARG_NEUTRAL_BTN_RES, -1);
  80. int negBtn = getArguments().getInt(ARG_NEGATIVE_BTN_RES, -1);
  81. if (messageArguments == null) {
  82. messageArguments = new String[]{};
  83. }
  84. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Theme_ownCloud_Dialog)
  85. .setIcon(R.drawable.ic_warning)
  86. .setIconAttribute(android.R.attr.alertDialogIcon)
  87. .setMessage(String.format(getString(messageId), messageArguments));
  88. if (titleId == 0) {
  89. builder.setTitle(android.R.string.dialog_alert_title);
  90. } else if (titleId != -1) {
  91. builder.setTitle(titleId);
  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. }
  104. if (neuBtn != -1) {
  105. builder.setNeutralButton(neuBtn,
  106. new DialogInterface.OnClickListener() {
  107. public void onClick(DialogInterface dialog, int whichButton) {
  108. if (mListener != null) {
  109. mListener.onNeutral(getTag());
  110. }
  111. dialog.dismiss();
  112. }
  113. });
  114. }
  115. if (negBtn != -1) {
  116. builder.setNegativeButton(negBtn,
  117. new DialogInterface.OnClickListener() {
  118. @Override
  119. public void onClick(DialogInterface dialog, int which) {
  120. if (mListener != null) {
  121. mListener.onCancel(getTag());
  122. }
  123. dialog.dismiss();
  124. }
  125. });
  126. }
  127. return builder.create();
  128. }
  129. public interface ConfirmationDialogFragmentListener {
  130. public void onConfirmation(String callerTag);
  131. public void onNeutral(String callerTag);
  132. public void onCancel(String callerTag);
  133. }
  134. }