1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.adservices.ui.settings; 17 18 import static com.android.adservices.ui.settings.DialogFragmentManager.sIsShowing; 19 20 import android.app.AlertDialog; 21 import android.app.Dialog; 22 import android.content.DialogInterface; 23 import android.content.DialogInterface.OnClickListener; 24 import android.os.Build; 25 import android.os.Bundle; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 import androidx.annotation.RequiresApi; 30 import androidx.fragment.app.DialogFragment; 31 import androidx.fragment.app.FragmentManager; 32 33 import java.io.Serializable; 34 35 /** Child class of DialogFragment for the Speed Bump Dialog of the AdServices Settings App. */ 36 // TODO(b/269798827): Enable for R. 37 @RequiresApi(Build.VERSION_CODES.S) 38 public class SpeedBumpDialogFragment extends DialogFragment { 39 private static final String ARG_TITLE = "title"; 40 private static final String ARG_MESSAGE = "message"; 41 private static final String ARG_POS_BTN_TEXT = "positiveButtonText"; 42 private static final String ARG_NEG_BTN_TEXT = "negativeButtonText"; 43 private static final String ARG_POS_BTN_LISTENER = "positiveButtonListener"; 44 45 private static boolean sHasNegButton; 46 47 /** 48 * create a new instance of SpeedBumpDialogFragment 49 * 50 * @param title dialog title 51 * @param message dialog message 52 * @param positiveButtonText string of positive button 53 * @param negativeButtonText string of negative button, pass in empty string if there is no 54 * negative button 55 * @param positiveButtonListener Action listener of positive button 56 * @return a new instance of SpeedBumpDialogFragment 57 */ newInstance( @onNull String title, @NonNull String message, @NonNull String positiveButtonText, @NonNull String negativeButtonText, @NonNull OnClickListener positiveButtonListener)58 public static SpeedBumpDialogFragment newInstance( 59 @NonNull String title, 60 @NonNull String message, 61 @NonNull String positiveButtonText, 62 @NonNull String negativeButtonText, 63 @NonNull OnClickListener positiveButtonListener) { 64 SpeedBumpDialogFragment fragment = new SpeedBumpDialogFragment(); 65 66 Bundle args = new Bundle(); 67 sHasNegButton = !negativeButtonText.equals(""); 68 args.putString(ARG_TITLE, title); 69 args.putString(ARG_MESSAGE, message); 70 args.putString(ARG_POS_BTN_TEXT, positiveButtonText); 71 args.putString(ARG_NEG_BTN_TEXT, negativeButtonText); 72 73 SerializableOnClickListener posListenerSerializable = 74 new SerializableOnClickListener(positiveButtonListener); 75 args.putSerializable(ARG_POS_BTN_LISTENER, posListenerSerializable); 76 77 fragment.setArguments(args); 78 return fragment; 79 } 80 81 @NonNull 82 @Override onCreateDialog(Bundle savedInstanceState)83 public Dialog onCreateDialog(Bundle savedInstanceState) { 84 String title = getArguments().getString(ARG_TITLE); 85 String message = getArguments().getString(ARG_MESSAGE); 86 String positiveButtonText = getArguments().getString(ARG_POS_BTN_TEXT); 87 String negativeButtonTextId = getArguments().getString(ARG_NEG_BTN_TEXT); 88 89 OnClickListener positiveButtonListener = 90 (OnClickListener) getArguments().getSerializable(ARG_POS_BTN_LISTENER); 91 92 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 93 builder.setTitle(title) 94 .setMessage(message) 95 .setPositiveButton(positiveButtonText, positiveButtonListener); 96 if (sHasNegButton) { 97 builder.setNegativeButton(negativeButtonTextId, getNegativeOnClickListener()); 98 } 99 return builder.create(); 100 } 101 102 @Override show(@onNull FragmentManager manager, @Nullable String tag)103 public void show(@NonNull FragmentManager manager, @Nullable String tag) { 104 if (this.getDialog() != null && this.getDialog().isShowing()) return; 105 super.show(manager, tag); 106 } 107 108 @Override onDismiss(@onNull DialogInterface dialog)109 public void onDismiss(@NonNull DialogInterface dialog) { 110 super.onDismiss(dialog); 111 sIsShowing = false; 112 } 113 getNegativeOnClickListener()114 private static OnClickListener getNegativeOnClickListener() { 115 return (dialogInterface, buttonId) -> sIsShowing = false; 116 } 117 118 static class SerializableOnClickListener implements OnClickListener, Serializable { 119 private final OnClickListener mListener; 120 SerializableOnClickListener(OnClickListener positiveButtonListener)121 SerializableOnClickListener(OnClickListener positiveButtonListener) { 122 this.mListener = positiveButtonListener; 123 } 124 125 @Override onClick(DialogInterface dialogInterface, int i)126 public void onClick(DialogInterface dialogInterface, int i) { 127 this.mListener.onClick(dialogInterface, i); 128 } 129 } 130 } 131