1 /*
2  * Copyright 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 import android.os.PowerManager;
24 
25 import androidx.appcompat.app.AlertDialog;
26 import androidx.fragment.app.FragmentManager;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
30 
31 /**
32  * The NFC log type switch should reboot the device to take effect,
33  * the dialog is to ask the user to reboot the device.
34  */
35 public class NfcRebootDialog extends InstrumentedDialogFragment
36         implements DialogInterface.OnClickListener {
37 
38     public static final String TAG = "NfcRebootDialog";
39 
40     /**
41      * The function to show the Dialog.
42      */
show(DevelopmentSettingsDashboardFragment host)43     public static void show(DevelopmentSettingsDashboardFragment host) {
44         final FragmentManager manager = host.getActivity().getSupportFragmentManager();
45         if (manager.findFragmentByTag(TAG) == null) {
46             final NfcRebootDialog dialog = new NfcRebootDialog();
47             dialog.setTargetFragment(host, 0 /* requestCode */);
48             dialog.show(manager, TAG);
49         }
50     }
51 
52     @Override
getMetricsCategory()53     public int getMetricsCategory() {
54         return SettingsEnums.DIALOG_NFC_ENABLE_DETAIL_LOG;
55     }
56 
57     @Override
onCreateDialog(Bundle savedInstanceState)58     public Dialog onCreateDialog(Bundle savedInstanceState) {
59         return new AlertDialog.Builder(getActivity())
60                 .setMessage(R.string.nfc_reboot_dialog_message)
61                 .setTitle(R.string.nfc_reboot_dialog_title)
62                 .setPositiveButton(
63                         R.string.nfc_reboot_dialog_confirm, this)
64                 .setNegativeButton(
65                         android.R.string.cancel, this)
66                 .create();
67     }
68 
69     @Override
onClick(DialogInterface dialog, int which)70     public void onClick(DialogInterface dialog, int which) {
71         final OnNfcRebootDialogConfirmedListener host =
72                 (OnNfcRebootDialogConfirmedListener) getTargetFragment();
73         if (host == null) {
74             return;
75         }
76         if (which == DialogInterface.BUTTON_POSITIVE) {
77             host.onNfcRebootDialogConfirmed();
78             PowerManager pm = getContext().getSystemService(PowerManager.class);
79             pm.reboot(null);
80         } else {
81             host.onNfcRebootDialogCanceled();
82         }
83     }
84 
85     /**
86      * Interface for EnableAdbWarningDialog callbacks.
87      */
88     public interface OnNfcRebootDialogConfirmedListener {
89         /**
90          * Called when the user presses enable on the warning dialog.
91          */
onNfcRebootDialogConfirmed()92         void onNfcRebootDialogConfirmed();
93 
94         /**
95          * Called when the user presses cancel on the warning dialog.
96          */
onNfcRebootDialogCanceled()97         void onNfcRebootDialogCanceled();
98     }
99 }
100