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