ConfirmationDialogFragment.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2012 Bartek Przybylski Copyright (C) 2015 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  10. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. * details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with this program. If not, see
  14. * <http://www.gnu.org/licenses/>.
  15. */
  16. package com.owncloud.android.ui.dialog;
  17. import android.app.Activity;
  18. import android.app.Dialog;
  19. import android.os.Bundle;
  20. import com.owncloud.android.R;
  21. import com.owncloud.android.utils.ThemeUtils;
  22. import androidx.annotation.NonNull;
  23. import androidx.appcompat.app.AlertDialog;
  24. import androidx.fragment.app.DialogFragment;
  25. public class ConfirmationDialogFragment extends DialogFragment {
  26. final static String ARG_MESSAGE_RESOURCE_ID = "resource_id";
  27. final static String ARG_MESSAGE_ARGUMENTS = "string_array";
  28. private static final String ARG_TITLE_ID = "title_id";
  29. final static String ARG_POSITIVE_BTN_RES = "positive_btn_res";
  30. final static String ARG_NEUTRAL_BTN_RES = "neutral_btn_res";
  31. final static String ARG_NEGATIVE_BTN_RES = "negative_btn_res";
  32. public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
  33. private ConfirmationDialogFragmentListener mListener;
  34. /**
  35. * Public factory method to create new ConfirmationDialogFragment instances.
  36. *
  37. * @param messageResId Resource id for a message to show in the dialog.
  38. * @param messageArguments Arguments to complete the message, if it's a format string. May be null.
  39. * @param titleResId Resource id for a text to show in the title. 0 for default alert title, -1 for no title.
  40. * @param posBtn Resource id for the text of the positive button. -1 for no positive button.
  41. * @param neuBtn Resource id for the text of the neutral button. -1 for no neutral button.
  42. * @param negBtn Resource id for the text of the negative button. -1 for no negative button.
  43. * @return Dialog ready to show.
  44. */
  45. public static ConfirmationDialogFragment newInstance(int messageResId, String[] messageArguments, int titleResId,
  46. int posBtn, int neuBtn, int negBtn) {
  47. if (messageResId == -1) {
  48. throw new IllegalStateException("Calling confirmation dialog without message resource");
  49. }
  50. ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
  51. Bundle args = new Bundle();
  52. args.putInt(ARG_MESSAGE_RESOURCE_ID, messageResId);
  53. args.putStringArray(ARG_MESSAGE_ARGUMENTS, messageArguments);
  54. args.putInt(ARG_TITLE_ID, titleResId);
  55. args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
  56. args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
  57. args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
  58. frag.setArguments(args);
  59. return frag;
  60. }
  61. @Override
  62. public void onStart() {
  63. super.onStart();
  64. int color = ThemeUtils.primaryAccentColor(getContext());
  65. AlertDialog alertDialog = (AlertDialog) getDialog();
  66. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  67. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  68. }
  69. public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
  70. mListener = listener;
  71. }
  72. @NonNull
  73. @Override
  74. public Dialog onCreateDialog(Bundle savedInstanceState) {
  75. Bundle arguments = getArguments();
  76. if (arguments == null) {
  77. throw new IllegalArgumentException("Arguments may not be null");
  78. }
  79. Activity activity = getActivity();
  80. if (activity == null) {
  81. throw new IllegalArgumentException("Activity may not be null");
  82. }
  83. Object[] messageArguments = arguments.getStringArray(ARG_MESSAGE_ARGUMENTS);
  84. int messageId = arguments.getInt(ARG_MESSAGE_RESOURCE_ID, -1);
  85. int titleId = arguments.getInt(ARG_TITLE_ID, -1);
  86. int posBtn = arguments.getInt(ARG_POSITIVE_BTN_RES, -1);
  87. int neuBtn = arguments.getInt(ARG_NEUTRAL_BTN_RES, -1);
  88. int negBtn = arguments.getInt(ARG_NEGATIVE_BTN_RES, -1);
  89. if (messageArguments == null) {
  90. messageArguments = new String[]{};
  91. }
  92. AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.Theme_ownCloud_Dialog)
  93. .setIcon(R.drawable.ic_warning)
  94. .setIconAttribute(android.R.attr.alertDialogIcon)
  95. .setMessage(String.format(getString(messageId), messageArguments));
  96. if (titleId == 0) {
  97. builder.setTitle(android.R.string.dialog_alert_title);
  98. } else if (titleId != -1) {
  99. builder.setTitle(titleId);
  100. }
  101. if (posBtn != -1) {
  102. builder.setPositiveButton(posBtn, (dialog, whichButton) -> {
  103. if (mListener != null) {
  104. mListener.onConfirmation(getTag());
  105. }
  106. dialog.dismiss();
  107. });
  108. }
  109. if (neuBtn != -1) {
  110. builder.setNeutralButton(neuBtn, (dialog, whichButton) -> {
  111. if (mListener != null) {
  112. mListener.onNeutral(getTag());
  113. }
  114. dialog.dismiss();
  115. });
  116. }
  117. if (negBtn != -1) {
  118. builder.setNegativeButton(negBtn, (dialog, which) -> {
  119. if (mListener != null) {
  120. mListener.onCancel(getTag());
  121. }
  122. dialog.dismiss();
  123. });
  124. }
  125. return builder.create();
  126. }
  127. public interface ConfirmationDialogFragmentListener {
  128. void onConfirmation(String callerTag);
  129. void onNeutral(String callerTag);
  130. void onCancel(String callerTag);
  131. }
  132. }