1 /*
2  * Copyright 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 
25 import androidx.appcompat.app.AlertDialog;
26 import androidx.fragment.app.FragmentManager;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
30 
31 public class BluetoothA2dpHwOffloadRebootDialog extends InstrumentedDialogFragment
32         implements DialogInterface.OnClickListener {
33 
34     public static final String TAG = "BluetoothA2dpHwOffloadReboot";
35 
show(DevelopmentSettingsDashboardFragment host, BluetoothA2dpHwOffloadPreferenceController controller)36     public static void show(DevelopmentSettingsDashboardFragment host,
37             BluetoothA2dpHwOffloadPreferenceController controller) {
38         final FragmentManager manager = host.getActivity().getSupportFragmentManager();
39         if (manager.findFragmentByTag(TAG) == null) {
40             final BluetoothA2dpHwOffloadRebootDialog dialog =
41                     new BluetoothA2dpHwOffloadRebootDialog();
42             dialog.setTargetFragment(host, 0 /* requestCode */);
43             dialog.show(manager, TAG);
44         }
45     }
46 
47     @Override
getMetricsCategory()48     public int getMetricsCategory() {
49         return SettingsEnums.DIALOG_BLUETOOTH_DISABLE_A2DP_HW_OFFLOAD;
50     }
51 
52     @Override
onCreateDialog(Bundle savedInstanceState)53     public Dialog onCreateDialog(Bundle savedInstanceState) {
54         return new AlertDialog.Builder(getActivity())
55                 .setMessage(R.string.bluetooth_disable_a2dp_hw_offload_dialog_message)
56                 .setTitle(R.string.bluetooth_disable_a2dp_hw_offload_dialog_title)
57                 .setPositiveButton(
58                         R.string.bluetooth_disable_a2dp_hw_offload_dialog_confirm, this)
59                 .setNegativeButton(
60                         R.string.bluetooth_disable_a2dp_hw_offload_dialog_cancel, this)
61                 .create();
62     }
63 
64     @Override
onClick(DialogInterface dialog, int which)65     public void onClick(DialogInterface dialog, int which) {
66         final OnA2dpHwDialogConfirmedListener host =
67                 (OnA2dpHwDialogConfirmedListener) getTargetFragment();
68         if (host == null) {
69             return;
70         }
71         if (which == DialogInterface.BUTTON_POSITIVE) {
72             host.onA2dpHwDialogConfirmed();
73             PowerManager pm = getContext().getSystemService(PowerManager.class);
74             pm.reboot(null);
75         }
76     }
77 
78     public interface OnA2dpHwDialogConfirmedListener {
79         /**
80          * Called when the user presses reboot on the warning dialog.
81          */
onA2dpHwDialogConfirmed()82         void onA2dpHwDialogConfirmed();
83     }
84 }
85