1 /*
2  * Copyright (C) 2018 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 import android.util.Log;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.appcompat.app.AlertDialog;
28 import androidx.fragment.app.Fragment;
29 import androidx.fragment.app.FragmentManager;
30 
31 import com.android.settings.R;
32 import com.android.settings.SettingsPreferenceFragment;
33 import com.android.settings.applications.ProcessStatsSummary;
34 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
35 
36 public class DisableDevSettingsDialogFragment extends InstrumentedDialogFragment
37         implements DialogInterface.OnClickListener {
38 
39     public static final String TAG = "DisableDevSettingDlg";
40 
41     @VisibleForTesting
newInstance()42     static DisableDevSettingsDialogFragment newInstance() {
43         final DisableDevSettingsDialogFragment dialog = new DisableDevSettingsDialogFragment();
44         return dialog;
45     }
46 
show(SettingsPreferenceFragment host)47     public static void show(SettingsPreferenceFragment host) {
48         final DisableDevSettingsDialogFragment dialog = new DisableDevSettingsDialogFragment();
49         dialog.setTargetFragment(host, 0 /* requestCode */);
50         // We need to handle data changes and switch state based on which button user clicks,
51         // therefore we should enforce user to click one of the buttons
52         // by disallowing dialog dismiss through tapping outside of dialog bounds.
53         dialog.setCancelable(false);
54         final FragmentManager manager = host.getActivity().getSupportFragmentManager();
55         dialog.show(manager, TAG);
56     }
57 
58     @Override
getMetricsCategory()59     public int getMetricsCategory() {
60         return SettingsEnums.DIALOG_DISABLE_DEVELOPMENT_OPTIONS;
61     }
62 
63     @Override
onCreateDialog(Bundle savedInstanceState)64     public Dialog onCreateDialog(Bundle savedInstanceState) {
65         // Reuse the same text of disable_hw_offload_dialog.
66         // The text is generic enough to be used for turning off Dev options.
67         return new AlertDialog.Builder(getActivity())
68                 .setMessage(R.string.bluetooth_disable_hw_offload_dialog_message)
69                 .setTitle(R.string.bluetooth_disable_hw_offload_dialog_title)
70                 .setPositiveButton(
71                         R.string.bluetooth_disable_hw_offload_dialog_confirm, this)
72                 .setNegativeButton(
73                         R.string.bluetooth_disable_hw_offload_dialog_cancel, this)
74                 .create();
75     }
76 
77     @Override
onClick(DialogInterface dialog, int which)78     public void onClick(DialogInterface dialog, int which) {
79         Fragment fragment = getTargetFragment();
80         if (!(fragment instanceof DevelopmentSettingsDashboardFragment)
81                 && !(fragment instanceof ProcessStatsSummary)) {
82             Log.e(TAG, "getTargetFragment return unexpected type");
83         }
84 
85         if (fragment instanceof DevelopmentSettingsDashboardFragment) {
86             final DevelopmentSettingsDashboardFragment host =
87                     (DevelopmentSettingsDashboardFragment) fragment;
88             if (which == DialogInterface.BUTTON_POSITIVE) {
89                 host.onDisableDevelopmentOptionsConfirmed();
90                 PowerManager pm = getContext().getSystemService(PowerManager.class);
91                 pm.reboot(null);
92             } else {
93                 host.onDisableDevelopmentOptionsRejected();
94             }
95         } else if (fragment instanceof ProcessStatsSummary) {
96             final ProcessStatsSummary host =
97                     (ProcessStatsSummary) fragment;
98             if (which == DialogInterface.BUTTON_POSITIVE) {
99                 host.onRebootDialogConfirmed();
100                 PowerManager pm = getContext().getSystemService(PowerManager.class);
101                 pm.reboot(null);
102             } else {
103                 host.onRebootDialogCanceled();
104             }
105         }
106     }
107 }
108