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.internal.annotations.Initializer;
31 import com.android.settings.R;
32 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
33 
34 /** Dialog when user interacts 16K pages developer option and data is f2fs */
35 public class EnableExt4WarningDialog extends InstrumentedDialogFragment
36         implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
37 
38     public static final String TAG = "EnableExt4WarningDialog";
39 
40     private EnableExt4DialogHost mHost;
41 
42     @Initializer
setHost(@onNull EnableExt4DialogHost host)43     private void setHost(@NonNull EnableExt4DialogHost host) {
44         this.mHost = host;
45     }
46 
47     /** This method is used to show warning dialog to reformat data to /ext4 */
show( @onNull Fragment hostFragment, @NonNull EnableExt4DialogHost dialogHost)48     public static void show(
49             @NonNull Fragment hostFragment, @NonNull EnableExt4DialogHost dialogHost) {
50         final FragmentManager manager = hostFragment.getActivity().getSupportFragmentManager();
51         Fragment existingFragment = manager.findFragmentByTag(TAG);
52         if (existingFragment == null) {
53             existingFragment = new EnableExt4WarningDialog();
54         }
55 
56         if (existingFragment instanceof EnableExt4WarningDialog) {
57             existingFragment.setTargetFragment(hostFragment, 0 /* requestCode */);
58             ((EnableExt4WarningDialog) existingFragment).setHost(dialogHost);
59             ((EnableExt4WarningDialog) existingFragment).show(manager, TAG);
60         }
61     }
62 
63     @Override
getMetricsCategory()64     public int getMetricsCategory() {
65         return SettingsEnums.DIALOG_ENABLE_16K_PAGES;
66     }
67 
68     @NonNull
69     @Override
onCreateDialog(@ullable Bundle savedInstanceState)70     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
71         return new AlertDialog.Builder(getActivity())
72                 .setTitle(R.string.confirm_format_ext4_title)
73                 .setIcon(R.drawable.ic_delete_accent)
74                 .setMessage(R.string.confirm_format_ext4_text)
75                 .setPositiveButton(R.string.confirm_ext4_button_text, this /* onClickListener */)
76                 .setNegativeButton(android.R.string.cancel, this /* onClickListener */)
77                 .create();
78     }
79 
80     @Override
onClick(@onNull DialogInterface dialog, int buttonId)81     public void onClick(@NonNull DialogInterface dialog, int buttonId) {
82         if (buttonId == DialogInterface.BUTTON_POSITIVE) {
83             mHost.onExt4DialogConfirmed();
84         } else {
85             mHost.onExt4DialogDismissed();
86         }
87     }
88 
89     @Override
onDismiss(@onNull DialogInterface dialog)90     public void onDismiss(@NonNull DialogInterface dialog) {
91         super.onDismiss(dialog);
92         mHost.onExt4DialogDismissed();
93     }
94 }
95