/* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.network.telephony; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.FragmentActivity; import com.android.settings.R; import java.util.ArrayList; /** Fragment to show a confirm dialog. The caller should implement onConfirmListener. */ public class ConfirmDialogFragment extends BaseDialogFragment implements DialogInterface.OnClickListener { private static final String TAG = "ConfirmDialogFragment"; private static final String ARG_TITLE = "title"; private static final String ARG_MSG = "msg"; private static final String ARG_POS_BUTTON_STRING = "pos_button_string"; private static final String ARG_NEG_BUTTON_STRING = "neg_button_string"; private static final String ARG_LIST = "list"; /** * Interface defining the method that will be invoked when the user has done with the dialog. */ public interface OnConfirmListener { /** * @param tag The tag in the caller. * @param confirmed True if the user has clicked the positive button. False if the * user has * clicked the negative button or cancel the dialog. * @param itemPosition It is the position of item, if user selects one of the list item. * If the user select "cancel" or the dialog does not have list, then * the value is -1. */ void onConfirm(int tag, boolean confirmed, int itemPosition); } /** Displays a confirmation dialog which has confirm and cancel buttons. */ public static void show( FragmentActivity activity, Class callbackInterfaceClass, int tagInCaller, String title, String msg, String posButtonString, String negButtonString) { ConfirmDialogFragment fragment = new ConfirmDialogFragment(); Bundle arguments = new Bundle(); arguments.putString(ARG_TITLE, title); arguments.putCharSequence(ARG_MSG, msg); arguments.putString(ARG_POS_BUTTON_STRING, posButtonString); arguments.putString(ARG_NEG_BUTTON_STRING, negButtonString); setListener(activity, null, callbackInterfaceClass, tagInCaller, arguments); fragment.setArguments(arguments); fragment.show(activity.getSupportFragmentManager(), TAG); } /** Displays a confirmation dialog which has confirm and cancel buttons and carrier list.*/ public static void show( FragmentActivity activity, Class callbackInterfaceClass, int tagInCaller, String title, String msg, String posButtonString, String negButtonString, ArrayList list) { ConfirmDialogFragment fragment = new ConfirmDialogFragment(); Bundle arguments = new Bundle(); arguments.putString(ARG_TITLE, title); arguments.putCharSequence(ARG_MSG, msg); arguments.putString(ARG_POS_BUTTON_STRING, posButtonString); arguments.putString(ARG_NEG_BUTTON_STRING, negButtonString); arguments.putStringArrayList(ARG_LIST, list); setListener(activity, null, callbackInterfaceClass, tagInCaller, arguments); fragment.setArguments(arguments); fragment.show(activity.getSupportFragmentManager(), TAG); } @Override public final Dialog onCreateDialog(Bundle savedInstanceState) { String title = getArguments().getString(ARG_TITLE); String message = getArguments().getString(ARG_MSG); String posBtnString = getArguments().getString(ARG_POS_BUTTON_STRING); String negBtnString = getArguments().getString(ARG_NEG_BUTTON_STRING); ArrayList list = getArguments().getStringArrayList(ARG_LIST); Log.i(TAG, "Showing dialog with title =" + title); AlertDialog.Builder builder = new AlertDialog.Builder(getContext()) .setTitle(title) .setPositiveButton(posBtnString, this) .setNegativeButton(negBtnString, this); if (list != null && !list.isEmpty()) { Log.i(TAG, "list =" + list.toString()); View content = LayoutInflater.from(getContext()).inflate( R.layout.sim_confirm_dialog_multiple_enabled_profiles_supported, null); TextView dialogMessage = content.findViewById(R.id.msg); if (!TextUtils.isEmpty(message) && dialogMessage != null) { dialogMessage.setText(message); } final ArrayAdapter arrayAdapterItems = new ArrayAdapter( getContext(), android.R.layout.select_dialog_item, list); final ListView lvItems = content.findViewById(R.id.carrier_list); if (lvItems != null) { lvItems.setAdapter(arrayAdapterItems); lvItems.setChoiceMode(ListView.CHOICE_MODE_NONE); lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Log.i(TAG, "list onClick =" + position); Log.i(TAG, "list item =" + list.get(position)); if (position == list.size() - 1) { // user select the "cancel" item; informCaller(false, -1); } else { informCaller(true, position); } } }); } builder.setView(content); } else { if (!TextUtils.isEmpty(message)) { builder.setMessage(message); } } AlertDialog dialog = builder.create(); dialog.setCanceledOnTouchOutside(false); return dialog; } @Override public void onClick(DialogInterface dialog, int which) { Log.i(TAG, "dialog onClick =" + which); informCaller(which == DialogInterface.BUTTON_POSITIVE, -1); } @Override public void onCancel(DialogInterface dialog) { informCaller(false, -1); } private void informCaller(boolean confirmed, int itemPosition) { OnConfirmListener listener = getListener(OnConfirmListener.class); if (listener == null) { return; } listener.onConfirm(getTagInCaller(), confirmed, itemPosition); } }