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 17 package com.android.settings.development; 18 19 import android.app.Dialog; 20 import android.app.settings.SettingsEnums; 21 import android.content.DialogInterface; 22 import android.os.Bundle; 23 24 import androidx.appcompat.app.AlertDialog; 25 import androidx.fragment.app.Fragment; 26 import androidx.fragment.app.FragmentManager; 27 28 import com.android.settings.R; 29 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 30 31 /** Dialog fragment for reboot confirmation when enabling certain features. */ 32 public class RebootConfirmationDialogFragment extends InstrumentedDialogFragment 33 implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener { 34 35 private static final String TAG = "FreeformPrefRebootDlg"; 36 37 private final int mMessageId; 38 private final int mCancelButtonId; 39 private final RebootConfirmationDialogHost mHost; 40 41 /** Show an instance of this dialog. */ show(Fragment fragment, int messageId, RebootConfirmationDialogHost host)42 public static void show(Fragment fragment, int messageId, RebootConfirmationDialogHost host) { 43 show(fragment, messageId, R.string.reboot_dialog_reboot_later, host); 44 } 45 46 /** Show an instance of this dialog with cancel button string set as cancelButtonId */ show( Fragment fragment, int messageId, int cancelButtonId, RebootConfirmationDialogHost host)47 public static void show( 48 Fragment fragment, 49 int messageId, 50 int cancelButtonId, 51 RebootConfirmationDialogHost host) { 52 final FragmentManager manager = fragment.getActivity().getSupportFragmentManager(); 53 if (manager.findFragmentByTag(TAG) == null) { 54 final RebootConfirmationDialogFragment dialog = 55 new RebootConfirmationDialogFragment(messageId, cancelButtonId, host); 56 dialog.show(manager, TAG); 57 } 58 } 59 RebootConfirmationDialogFragment( int messageId, int cancelButtonId, RebootConfirmationDialogHost host)60 private RebootConfirmationDialogFragment( 61 int messageId, int cancelButtonId, RebootConfirmationDialogHost host) { 62 mMessageId = messageId; 63 mCancelButtonId = cancelButtonId; 64 mHost = host; 65 } 66 67 @Override getMetricsCategory()68 public int getMetricsCategory() { 69 return SettingsEnums.REBOOT_CONFIRMATION_DIALOG; 70 } 71 72 @Override onCreateDialog(Bundle savedInstances)73 public Dialog onCreateDialog(Bundle savedInstances) { 74 return new AlertDialog.Builder(getActivity()) 75 .setMessage(mMessageId) 76 .setPositiveButton(R.string.reboot_dialog_reboot_now, this) 77 .setNegativeButton(mCancelButtonId, this) 78 .create(); 79 } 80 81 @Override onClick(DialogInterface dialog, int which)82 public void onClick(DialogInterface dialog, int which) { 83 if (which == DialogInterface.BUTTON_POSITIVE) { 84 mHost.onRebootConfirmed(getContext()); 85 } else { 86 mHost.onRebootCancelled(); 87 } 88 } 89 90 @Override onDismiss(DialogInterface dialog)91 public void onDismiss(DialogInterface dialog) { 92 super.onDismiss(dialog); 93 mHost.onRebootDialogDismissed(); 94 } 95 } 96