1 /* 2 * Copyright (C) 2024 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.Dialog; 20 import android.content.DialogInterface; 21 import android.os.Bundle; 22 import android.text.TextUtils; 23 import android.util.Log; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.widget.TextView; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.appcompat.app.AlertDialog; 31 import androidx.fragment.app.FragmentActivity; 32 33 import com.android.settings.R; 34 35 /** Fragment to show a warning dialog. The caller should implement onConfirmListener. */ 36 public class WarningDialogFragment extends BaseDialogFragment 37 implements DialogInterface.OnClickListener { 38 private static final String TAG = "WarningDialogFragment"; 39 private static final String ARG_TITLE = "title"; 40 private static final String ARG_MSG = "msg"; 41 private static final String ARG_POS_BUTTON_STRING = "pos_button_string"; 42 private static final String ARG_NEG_BUTTON_STRING = "neg_button_string"; 43 44 /** 45 * Interface defining the method that will be invoked when the user has done with the dialog. 46 */ 47 public interface OnConfirmListener { 48 /** 49 * @param tag The tag in the caller. 50 * @param confirmed True if the user has clicked the positive button. False if the user has 51 * clicked the negative button or cancel the dialog. 52 */ onConfirm(int tag, boolean confirmed)53 void onConfirm(int tag, boolean confirmed); 54 } 55 56 /** Displays a confirmation dialog which has confirm and cancel buttons. */ show( FragmentActivity activity, Class<T> callbackInterfaceClass, int tagInCaller, String title, String msg, String posButtonString, String negButtonString)57 static <T> void show( 58 FragmentActivity activity, 59 Class<T> callbackInterfaceClass, 60 int tagInCaller, 61 String title, 62 String msg, 63 String posButtonString, 64 String negButtonString) { 65 WarningDialogFragment fragment = new WarningDialogFragment(); 66 Bundle arguments = new Bundle(); 67 arguments.putString(ARG_TITLE, title); 68 arguments.putCharSequence(ARG_MSG, msg); 69 arguments.putString(ARG_POS_BUTTON_STRING, posButtonString); 70 arguments.putString(ARG_NEG_BUTTON_STRING, negButtonString); 71 setListener(activity, null, callbackInterfaceClass, tagInCaller, arguments); 72 fragment.setArguments(arguments); 73 fragment.show(activity.getSupportFragmentManager(), TAG); 74 } 75 76 @Override 77 @NonNull onCreateDialog(@ullable Bundle savedInstanceState)78 public final Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 79 String title = getArguments().getString(ARG_TITLE); 80 String message = getArguments().getString(ARG_MSG); 81 String leftButton = getArguments().getString(ARG_POS_BUTTON_STRING); 82 String rightButton = getArguments().getString(ARG_NEG_BUTTON_STRING); 83 84 Log.i(TAG, "Showing dialog with title =" + title); 85 AlertDialog.Builder builder = 86 new AlertDialog.Builder(getContext()) 87 .setPositiveButton(rightButton, this) 88 .setNegativeButton(leftButton, this); 89 90 View content = 91 LayoutInflater.from(getContext()) 92 .inflate(R.layout.sim_warning_dialog_wifi_connectivity, null); 93 94 if (content != null) { 95 TextView dialogTitle = content.findViewById(R.id.title); 96 if (!TextUtils.isEmpty(title) && dialogTitle != null) { 97 dialogTitle.setText(title); 98 dialogTitle.setVisibility(View.VISIBLE); 99 } 100 TextView dialogMessage = content.findViewById(R.id.msg); 101 if (!TextUtils.isEmpty(message) && dialogMessage != null) { 102 dialogMessage.setText(message); 103 dialogMessage.setVisibility(View.VISIBLE); 104 } 105 106 builder.setView(content); 107 } else { 108 if (!TextUtils.isEmpty(title)) { 109 builder.setTitle(title); 110 } 111 if (!TextUtils.isEmpty(message)) { 112 builder.setMessage(message); 113 } 114 } 115 116 AlertDialog dialog = builder.create(); 117 dialog.setCanceledOnTouchOutside(false); 118 return dialog; 119 } 120 121 @Override onClick(@onNull DialogInterface dialog, int which)122 public void onClick(@NonNull DialogInterface dialog, int which) { 123 Log.i(TAG, "dialog onClick =" + which); 124 125 // Positions of the buttons have been switch: 126 // negative button = left button = the button to continue 127 informCaller(which == DialogInterface.BUTTON_NEGATIVE); 128 } 129 130 @Override onCancel(@onNull DialogInterface dialog)131 public void onCancel(@NonNull DialogInterface dialog) { 132 informCaller(false); 133 } 134 informCaller(boolean confirmed)135 private void informCaller(boolean confirmed) { 136 OnConfirmListener listener; 137 try { 138 listener = getListener(OnConfirmListener.class); 139 } catch (IllegalArgumentException e) { 140 Log.e(TAG, "Do nothing and return.", e); 141 return; 142 } 143 if (listener == null) { 144 return; 145 } 146 listener.onConfirm(getTagInCaller(), confirmed); 147 } 148 } 149