• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.settings.development.BluetoothLeAudioHwOffloadPreferenceController.LE_AUDIO_OFFLOAD_DISABLED_PROPERTY;
20 
21 import android.content.Context;
22 import android.os.SystemProperties;
23 
24 import androidx.annotation.Nullable;
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
31 
32 public class BluetoothA2dpHwOffloadPreferenceController extends DeveloperOptionsPreferenceController
33         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
34 
35     private static final String PREFERENCE_KEY = "bluetooth_disable_a2dp_hw_offload";
36     @Nullable private final DevelopmentSettingsDashboardFragment mFragment;
37 
38     static final String A2DP_OFFLOAD_DISABLED_PROPERTY = "persist.bluetooth.a2dp_offload.disabled";
39     static final String A2DP_OFFLOAD_SUPPORTED_PROPERTY = "ro.bluetooth.a2dp_offload.supported";
40 
41     @VisibleForTesting
42     boolean mChanged = false;
43 
BluetoothA2dpHwOffloadPreferenceController(Context context, @Nullable DevelopmentSettingsDashboardFragment fragment)44     public BluetoothA2dpHwOffloadPreferenceController(Context context,
45             @Nullable DevelopmentSettingsDashboardFragment fragment) {
46         super(context);
47         mFragment = fragment;
48     }
49 
50     @Override
getPreferenceKey()51     public String getPreferenceKey() {
52         return PREFERENCE_KEY;
53     }
54 
55     @Override
onPreferenceChange(Preference preference, Object newValue)56     public boolean onPreferenceChange(Preference preference, Object newValue) {
57         BluetoothRebootDialog.show(mFragment);
58         mChanged = true;
59         return false;
60     }
61 
62     @Override
updateState(Preference preference)63     public void updateState(Preference preference) {
64         super.updateState(preference);
65         final boolean offloadSupported =
66                 SystemProperties.getBoolean(A2DP_OFFLOAD_SUPPORTED_PROPERTY, false);
67         if (offloadSupported) {
68             final boolean offloadDisabled =
69                     SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
70             ((TwoStatePreference) mPreference).setChecked(offloadDisabled);
71         } else {
72             mPreference.setEnabled(false);
73             ((TwoStatePreference) mPreference).setChecked(true);
74         }
75     }
76 
77     @Override
onDeveloperOptionsSwitchDisabled()78     protected void onDeveloperOptionsSwitchDisabled() {
79         super.onDeveloperOptionsSwitchDisabled();
80         final boolean offloadSupported =
81                 SystemProperties.getBoolean(A2DP_OFFLOAD_SUPPORTED_PROPERTY, false);
82         if (offloadSupported) {
83             ((TwoStatePreference) mPreference).setChecked(false);
84             SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, "false");
85         }
86     }
87 
isDefaultValue()88     public boolean isDefaultValue() {
89         final boolean offloadSupported =
90                 SystemProperties.getBoolean(A2DP_OFFLOAD_SUPPORTED_PROPERTY, false);
91         final boolean offloadDisabled =
92                     SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
93         return offloadSupported ? !offloadDisabled : true;
94     }
95 
96     /**
97      * Called when the RebootDialog confirm is clicked.
98      */
onRebootDialogConfirmed()99     public void onRebootDialogConfirmed() {
100         if (!mChanged) {
101             return;
102         }
103         final boolean offloadDisabled =
104                 SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
105         SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(!offloadDisabled));
106         if (!offloadDisabled) {
107             SystemProperties.set(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY,
108                     Boolean.toString(!offloadDisabled));
109         }
110     }
111 
112     /**
113      * Called when the RebootDialog cancel is clicked.
114      */
onRebootDialogCanceled()115     public void onRebootDialogCanceled() {
116         mChanged = false;
117     }
118 }
119