1 /* 2 * Copyright (C) 2020 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 17 package com.android.settings.network.telephony; 18 19 import android.app.Activity; 20 import android.app.AlertDialog; 21 import android.app.Dialog; 22 import android.content.DialogInterface; 23 import android.os.Bundle; 24 import android.text.TextUtils; 25 import android.util.Log; 26 27 /** Fragment to show a confirm dialog. The caller should implement onConfirmListener. */ 28 public class ConfirmDialogFragment extends BaseDialogFragment 29 implements DialogInterface.OnClickListener { 30 private static final String TAG = "ConfirmDialogFragment"; 31 private static final String ARG_TITLE = "title"; 32 private static final String ARG_MSG = "msg"; 33 private static final String ARG_POS_BUTTON_STRING = "pos_button_string"; 34 private static final String ARG_NEG_BUTTON_STRING = "neg_button_string"; 35 36 /** 37 * Interface defining the method that will be invoked when the user has done with the dialog. 38 */ 39 public interface OnConfirmListener { 40 /** 41 * @param tag The tag in the caller. 42 * @param confirmed True if the user has clicked the positive button. False if the user has 43 * clicked the negative button or cancel the dialog. 44 */ onConfirm(int tag, boolean confirmed)45 void onConfirm(int tag, boolean confirmed); 46 } 47 48 /** Displays a confirmation dialog which has confirm and cancel buttons. */ show( Activity activity, Class<T> callbackInterfaceClass, int tagInCaller, String title, String msg, String posButtonString, String negButtonString)49 public static <T> void show( 50 Activity activity, 51 Class<T> callbackInterfaceClass, 52 int tagInCaller, 53 String title, 54 String msg, 55 String posButtonString, 56 String negButtonString) { 57 ConfirmDialogFragment fragment = new ConfirmDialogFragment(); 58 Bundle arguments = new Bundle(); 59 arguments.putString(ARG_TITLE, title); 60 arguments.putCharSequence(ARG_MSG, msg); 61 arguments.putString(ARG_POS_BUTTON_STRING, posButtonString); 62 arguments.putString(ARG_NEG_BUTTON_STRING, negButtonString); 63 setListener(activity, null, callbackInterfaceClass, tagInCaller, arguments); 64 fragment.setArguments(arguments); 65 fragment.show(activity.getFragmentManager(), TAG); 66 } 67 68 @Override onCreateDialog(Bundle savedInstanceState)69 public final Dialog onCreateDialog(Bundle savedInstanceState) { 70 String title = getArguments().getString(ARG_TITLE); 71 String message = getArguments().getString(ARG_MSG); 72 String posBtnString = getArguments().getString(ARG_POS_BUTTON_STRING); 73 String negBtnString = getArguments().getString(ARG_NEG_BUTTON_STRING); 74 75 Log.i("Showing dialog with title = %s", title); 76 AlertDialog.Builder builder = 77 new AlertDialog.Builder(getContext()) 78 .setTitle(title) 79 .setPositiveButton(posBtnString, this) 80 .setNegativeButton(negBtnString, this); 81 82 if (!TextUtils.isEmpty(message)) { 83 builder.setMessage(message); 84 } 85 AlertDialog dialog = builder.show(); 86 dialog.setCanceledOnTouchOutside(false); 87 return dialog; 88 } 89 90 @Override onClick(DialogInterface dialog, int which)91 public void onClick(DialogInterface dialog, int which) { 92 informCaller(which == DialogInterface.BUTTON_POSITIVE); 93 } 94 95 @Override onCancel(DialogInterface dialog)96 public void onCancel(DialogInterface dialog) { 97 informCaller(false); 98 } 99 informCaller(boolean confirmed)100 private void informCaller(boolean confirmed) { 101 OnConfirmListener listener = getListener(OnConfirmListener.class); 102 if (listener == null) { 103 return; 104 } 105 listener.onConfirm(getTagInCaller(), confirmed); 106 } 107 } 108