1 /*
2  * Copyright (C) 2024 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.appcompat.app.AlertDialog;
27 import androidx.fragment.app.Fragment;
28 import androidx.fragment.app.FragmentManager;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
32 
33 /** Dialog when user interacts 16K pages developer option */
34 public class Enable16kPagesWarningDialog extends InstrumentedDialogFragment
35         implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
36 
37     public static final String TAG = "Enable16KDialog";
38     private static final String DIALOG_BUNDLE_KEY = "SHOW_16K_DIALOG";
39 
40     private Enable16kbPagesDialogHost mHost;
41 
setHost(@onNull Enable16kbPagesDialogHost host)42     private void setHost(@NonNull Enable16kbPagesDialogHost host) {
43         mHost = host;
44     }
45 
46     /** This method is used to show warning dialog to apply 16K update and reboot */
show( @onNull Fragment hostFragment, @NonNull Enable16kbPagesDialogHost dialogHost, boolean enable16k)47     public static void show(
48             @NonNull Fragment hostFragment,
49             @NonNull Enable16kbPagesDialogHost dialogHost,
50             boolean enable16k) {
51         final FragmentManager manager = hostFragment.getActivity().getSupportFragmentManager();
52         Fragment existingFragment = manager.findFragmentByTag(TAG);
53         if (existingFragment == null) {
54             existingFragment = new Enable16kPagesWarningDialog();
55         }
56 
57         if (existingFragment instanceof Enable16kPagesWarningDialog) {
58             Bundle bundle = new Bundle();
59             bundle.putBoolean(DIALOG_BUNDLE_KEY, enable16k);
60             existingFragment.setArguments(bundle);
61             existingFragment.setTargetFragment(hostFragment, 0 /* requestCode */);
62             ((Enable16kPagesWarningDialog) existingFragment).setHost(dialogHost);
63             ((Enable16kPagesWarningDialog) existingFragment).show(manager, TAG);
64         }
65     }
66 
67     @Override
getMetricsCategory()68     public int getMetricsCategory() {
69         return SettingsEnums.DIALOG_ENABLE_16K_PAGES;
70     }
71 
72     @NonNull
73     @Override
onCreateDialog(@ullable Bundle savedInstanceState)74     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
75         final Bundle bundle = getArguments();
76         boolean is16kDialog = bundle.getBoolean(DIALOG_BUNDLE_KEY);
77         return new AlertDialog.Builder(getActivity())
78                 .setTitle(
79                         is16kDialog
80                                 ? R.string.confirm_enable_16k_pages_title
81                                 : R.string.confirm_enable_4k_pages_title)
82                 .setMessage(
83                         is16kDialog
84                                 ? R.string.confirm_enable_16k_pages_text
85                                 : R.string.confirm_enable_4k_pages_text)
86                 .setPositiveButton(android.R.string.ok, this /* onClickListener */)
87                 .setNegativeButton(android.R.string.cancel, this /* onClickListener */)
88                 .create();
89     }
90 
91     @Override
onClick(@onNull DialogInterface dialog, int buttonId)92     public void onClick(@NonNull DialogInterface dialog, int buttonId) {
93         if (buttonId == DialogInterface.BUTTON_POSITIVE) {
94             mHost.on16kPagesDialogConfirmed();
95         } else {
96             mHost.on16kPagesDialogDismissed();
97         }
98     }
99 
100     @Override
onDismiss(@onNull DialogInterface dialog)101     public void onDismiss(@NonNull DialogInterface dialog) {
102         super.onDismiss(dialog);
103         mHost.on16kPagesDialogDismissed();
104     }
105 }
106