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.content.Context;
20 import android.os.SystemProperties;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.SwitchPreference;
24 
25 import com.android.settings.core.PreferenceControllerMixin;
26 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
27 
28 public class BluetoothA2dpHwOffloadPreferenceController extends DeveloperOptionsPreferenceController
29         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
30 
31     private static final String PREFERENCE_KEY = "bluetooth_disable_a2dp_hw_offload";
32     private final DevelopmentSettingsDashboardFragment mFragment;
33 
34     static final String A2DP_OFFLOAD_DISABLED_PROPERTY = "persist.bluetooth.a2dp_offload.disabled";
35     static final String A2DP_OFFLOAD_SUPPORTED_PROPERTY = "ro.bluetooth.a2dp_offload.supported";
36 
BluetoothA2dpHwOffloadPreferenceController(Context context, DevelopmentSettingsDashboardFragment fragment)37     public BluetoothA2dpHwOffloadPreferenceController(Context context,
38             DevelopmentSettingsDashboardFragment fragment) {
39         super(context);
40         mFragment = fragment;
41     }
42 
43     @Override
getPreferenceKey()44     public String getPreferenceKey() {
45         return PREFERENCE_KEY;
46     }
47 
48     @Override
onPreferenceChange(Preference preference, Object newValue)49     public boolean onPreferenceChange(Preference preference, Object newValue) {
50         BluetoothA2dpHwOffloadRebootDialog.show(mFragment, this);
51         return false;
52     }
53 
54     @Override
updateState(Preference preference)55     public void updateState(Preference preference) {
56         super.updateState(preference);
57         final boolean offloadSupported =
58                 SystemProperties.getBoolean(A2DP_OFFLOAD_SUPPORTED_PROPERTY, false);
59         if (offloadSupported) {
60             final boolean offloadDisabled =
61                     SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
62             ((SwitchPreference) mPreference).setChecked(offloadDisabled);
63         } else {
64             mPreference.setEnabled(false);
65             ((SwitchPreference) mPreference).setChecked(true);
66         }
67     }
68 
69     @Override
onDeveloperOptionsSwitchDisabled()70     protected void onDeveloperOptionsSwitchDisabled() {
71         super.onDeveloperOptionsSwitchDisabled();
72         final boolean offloadSupported =
73                 SystemProperties.getBoolean(A2DP_OFFLOAD_SUPPORTED_PROPERTY, false);
74         if (offloadSupported) {
75             ((SwitchPreference) mPreference).setChecked(false);
76             SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, "false");
77         }
78     }
79 
isDefaultValue()80     public boolean isDefaultValue() {
81         final boolean offloadSupported =
82                 SystemProperties.getBoolean(A2DP_OFFLOAD_SUPPORTED_PROPERTY, false);
83         final boolean offloadDisabled =
84                     SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
85         return offloadSupported ? !offloadDisabled : true;
86     }
87 
onA2dpHwDialogConfirmed()88     public void onA2dpHwDialogConfirmed() {
89         final boolean offloadDisabled =
90                 SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
91         SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(!offloadDisabled));
92     }
93 
94 }
95