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.Dialog; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.os.Bundle; 23 import android.text.TextUtils; 24 import android.util.Log; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.ViewGroup; 28 import android.widget.ArrayAdapter; 29 import android.widget.LinearLayout; 30 import android.widget.ListView; 31 import android.widget.TextView; 32 33 import androidx.annotation.NonNull; 34 import androidx.appcompat.app.AlertDialog; 35 import androidx.fragment.app.FragmentActivity; 36 37 import com.android.settings.R; 38 39 import java.util.ArrayList; 40 import java.util.List; 41 42 /** Fragment to show a confirm dialog. The caller should implement onConfirmListener. */ 43 public class ConfirmDialogFragment extends BaseDialogFragment 44 implements DialogInterface.OnClickListener { 45 private static final String TAG = "ConfirmDialogFragment"; 46 private static final String ARG_TITLE = "title"; 47 private static final String ARG_MSG = "msg"; 48 private static final String ARG_POS_BUTTON_STRING = "pos_button_string"; 49 private static final String ARG_NEG_BUTTON_STRING = "neg_button_string"; 50 private static final String ARG_LIST = "list"; 51 52 /** 53 * Interface defining the method that will be invoked when the user has done with the dialog. 54 */ 55 public interface OnConfirmListener { 56 /** 57 * @param tag The tag in the caller. 58 * @param confirmed True if the user has clicked the positive button. False if the 59 * user has 60 * clicked the negative button or cancel the dialog. 61 * @param itemPosition It is the position of item, if user selects one of the list item. 62 * If the user select "cancel" or the dialog does not have list, then 63 * the value is -1. 64 */ onConfirm(int tag, boolean confirmed, int itemPosition)65 void onConfirm(int tag, boolean confirmed, int itemPosition); 66 } 67 68 /** 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)69 public static <T> void show( 70 FragmentActivity activity, 71 Class<T> callbackInterfaceClass, 72 int tagInCaller, 73 String title, 74 String msg, 75 String posButtonString, 76 String negButtonString) { 77 ConfirmDialogFragment fragment = new ConfirmDialogFragment(); 78 Bundle arguments = new Bundle(); 79 arguments.putString(ARG_TITLE, title); 80 arguments.putCharSequence(ARG_MSG, msg); 81 arguments.putString(ARG_POS_BUTTON_STRING, posButtonString); 82 arguments.putString(ARG_NEG_BUTTON_STRING, negButtonString); 83 setListener(activity, null, callbackInterfaceClass, tagInCaller, arguments); 84 fragment.setArguments(arguments); 85 fragment.show(activity.getSupportFragmentManager(), TAG); 86 } 87 88 /** Displays a confirmation dialog which has confirm and cancel buttons and carrier list.*/ show( FragmentActivity activity, Class<T> callbackInterfaceClass, int tagInCaller, String title, String msg, String posButtonString, String negButtonString, ArrayList<String> list)89 public static <T> void show( 90 FragmentActivity activity, 91 Class<T> callbackInterfaceClass, 92 int tagInCaller, 93 String title, 94 String msg, 95 String posButtonString, 96 String negButtonString, 97 ArrayList<String> list) { 98 ConfirmDialogFragment fragment = new ConfirmDialogFragment(); 99 Bundle arguments = new Bundle(); 100 arguments.putString(ARG_TITLE, title); 101 arguments.putCharSequence(ARG_MSG, msg); 102 arguments.putString(ARG_POS_BUTTON_STRING, posButtonString); 103 arguments.putString(ARG_NEG_BUTTON_STRING, negButtonString); 104 arguments.putStringArrayList(ARG_LIST, list); 105 setListener(activity, null, callbackInterfaceClass, tagInCaller, arguments); 106 fragment.setArguments(arguments); 107 fragment.show(activity.getSupportFragmentManager(), TAG); 108 } 109 110 @Override onCreateDialog(Bundle savedInstanceState)111 public final Dialog onCreateDialog(Bundle savedInstanceState) { 112 String title = getArguments().getString(ARG_TITLE); 113 String message = getArguments().getString(ARG_MSG); 114 String posBtnString = getArguments().getString(ARG_POS_BUTTON_STRING); 115 String negBtnString = getArguments().getString(ARG_NEG_BUTTON_STRING); 116 ArrayList<String> list = getArguments().getStringArrayList(ARG_LIST); 117 118 Log.i(TAG, "Showing dialog with title =" + title); 119 AlertDialog.Builder builder = new AlertDialog.Builder(getContext()) 120 .setPositiveButton(posBtnString, this) 121 .setNegativeButton(negBtnString, this); 122 View content = LayoutInflater.from(getContext()).inflate( 123 R.layout.sim_confirm_dialog_multiple_enabled_profiles_supported, null); 124 125 if (list != null && !list.isEmpty() && content != null) { 126 Log.i(TAG, "list =" + list.toString()); 127 128 if (!TextUtils.isEmpty(title)) { 129 View titleView = LayoutInflater.from(getContext()).inflate( 130 R.layout.sim_confirm_dialog_title_multiple_enabled_profiles_supported, 131 null); 132 TextView titleTextView = titleView.findViewById(R.id.title); 133 titleTextView.setText(title); 134 builder.setCustomTitle(titleTextView); 135 } 136 TextView dialogMessage = content.findViewById(R.id.msg); 137 if (!TextUtils.isEmpty(message) && dialogMessage != null) { 138 dialogMessage.setText(message); 139 dialogMessage.setVisibility(View.VISIBLE); 140 } 141 142 final ListView lvItems = content.findViewById(R.id.carrier_list); 143 if (lvItems != null) { 144 lvItems.setVisibility(View.VISIBLE); 145 lvItems.setAdapter(new ButtonArrayAdapter(getContext(), list)); 146 } 147 final LinearLayout infoOutline = content.findViewById(R.id.info_outline_layout); 148 if (infoOutline != null) { 149 infoOutline.setVisibility(View.VISIBLE); 150 } 151 builder.setView(content); 152 } else { 153 if (!TextUtils.isEmpty(title)) { 154 builder.setTitle(title); 155 } 156 if (!TextUtils.isEmpty(message)) { 157 builder.setMessage(message); 158 } 159 } 160 161 AlertDialog dialog = builder.create(); 162 dialog.setCanceledOnTouchOutside(false); 163 return dialog; 164 } 165 166 @Override onClick(DialogInterface dialog, int which)167 public void onClick(DialogInterface dialog, int which) { 168 Log.i(TAG, "dialog onClick =" + which); 169 170 informCaller(which == DialogInterface.BUTTON_POSITIVE, -1); 171 } 172 173 @Override onCancel(DialogInterface dialog)174 public void onCancel(DialogInterface dialog) { 175 informCaller(false, -1); 176 } 177 informCaller(boolean confirmed, int itemPosition)178 private void informCaller(boolean confirmed, int itemPosition) { 179 OnConfirmListener listener; 180 try { 181 listener = getListener(OnConfirmListener.class); 182 } catch (IllegalArgumentException e) { 183 Log.e(TAG, "Do nothing and return.", e); 184 return; 185 } 186 if (listener == null) { 187 return; 188 } 189 listener.onConfirm(getTagInCaller(), confirmed, itemPosition); 190 } 191 192 private class ButtonArrayAdapter extends ArrayAdapter<String> { 193 private final List<String> mList; 194 ButtonArrayAdapter(Context context, List<String> list)195 ButtonArrayAdapter(Context context, List<String> list) { 196 super(context, R.layout.sim_confirm_dialog_item_multiple_enabled_profiles_supported, 197 list); 198 mList = list; 199 } 200 201 @NonNull 202 @Override getView(int position, View convertView, @NonNull ViewGroup parent)203 public View getView(int position, View convertView, @NonNull ViewGroup parent) { 204 View view = super.getView(position, convertView, parent); 205 view.setOnClickListener(v -> { 206 Log.i(TAG, "list onClick =" + position); 207 Log.i(TAG, "list item =" + mList.get(position)); 208 209 if (position == mList.size() - 1) { 210 // user select the "cancel" item; 211 informCaller(false, -1); 212 } else { 213 informCaller(true, position); 214 } 215 }); 216 return view; 217 } 218 } 219 } 220