• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.annotation.VisibleForTesting;
27 import androidx.appcompat.app.AlertDialog;
28 import androidx.fragment.app.Fragment;
29 import androidx.fragment.app.FragmentManager;
30 import androidx.lifecycle.ViewModelProvider;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
34 
35 /** Dialog fragment for reboot confirmation when enabling certain features. */
36 public class RebootConfirmationDialogFragment extends InstrumentedDialogFragment
37         implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
38 
39     @VisibleForTesting
40     static final String TAG = "DevOptionRebootDlg";
41 
42     private RebootConfirmationDialogViewModel mViewModel;
43 
44     /** Show an instance of this dialog. */
show(Fragment fragment, int messageId, RebootConfirmationDialogHost host)45     public static void show(Fragment fragment, int messageId, RebootConfirmationDialogHost host) {
46         show(fragment, messageId, R.string.reboot_dialog_reboot_later, host);
47     }
48 
49     /** Show an instance of this dialog with cancel button string set as cancelButtonId */
show( Fragment fragment, int messageId, int cancelButtonId, RebootConfirmationDialogHost host)50     public static void show(
51             Fragment fragment,
52             int messageId,
53             int cancelButtonId,
54             RebootConfirmationDialogHost host) {
55         final FragmentManager manager = fragment.requireActivity().getSupportFragmentManager();
56         if (manager.findFragmentByTag(TAG) == null) {
57             final RebootConfirmationDialogFragment dialog =
58                     new RebootConfirmationDialogFragment();
59             RebootConfirmationDialogViewModel mViewModel = new ViewModelProvider(
60                     fragment.requireActivity()).get(
61                     RebootConfirmationDialogViewModel.class);
62             mViewModel.setMessageId(messageId);
63             mViewModel.setCancelButtonId(cancelButtonId);
64             mViewModel.setHost(host);
65             dialog.show(manager, TAG);
66         }
67     }
68 
69     @Override
getMetricsCategory()70     public int getMetricsCategory() {
71         return SettingsEnums.REBOOT_CONFIRMATION_DIALOG;
72     }
73 
74     @Override
onCreate(@ullable Bundle savedInstanceState)75     public void onCreate(@Nullable Bundle savedInstanceState) {
76         super.onCreate(savedInstanceState);
77         mViewModel = new ViewModelProvider(requireActivity()).get(
78                 RebootConfirmationDialogViewModel.class);
79     }
80 
81     @Override
onCreateDialog(@ullable Bundle savedInstances)82     public Dialog onCreateDialog(@Nullable Bundle savedInstances) {
83         int messageId = mViewModel.getMessageId();
84         int cancelButtonId = mViewModel.getCancelButtonId();
85         return new AlertDialog.Builder(requireActivity())
86                 .setMessage(messageId)
87                 .setPositiveButton(R.string.reboot_dialog_reboot_now, this)
88                 .setNegativeButton(cancelButtonId, this)
89                 .create();
90     }
91 
92     @Override
onClick(DialogInterface dialog, int which)93     public void onClick(DialogInterface dialog, int which) {
94         RebootConfirmationDialogHost host = mViewModel.getHost();
95         if (host == null) return;
96         if (which == DialogInterface.BUTTON_POSITIVE) {
97             host.onRebootConfirmed(requireContext());
98         } else {
99             host.onRebootCancelled();
100         }
101     }
102 
103     @Override
onDismiss(@onNull DialogInterface dialog)104     public void onDismiss(@NonNull DialogInterface dialog) {
105         super.onDismiss(dialog);
106         RebootConfirmationDialogHost host = mViewModel.getHost();
107         if (host != null) {
108             host.onRebootDialogDismissed();
109         }
110     }
111 }
112