• 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.car.settings.bluetooth;
18 
19 import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
20 
21 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm;
22 import static com.android.car.settings.enterprise.EnterpriseUtils.onClickWhileDisabled;
23 
24 import android.car.drivingstate.CarUxRestrictions;
25 import android.content.Context;
26 import android.os.UserManager;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceGroup;
31 
32 import com.android.car.settings.common.FragmentController;
33 import com.android.settingslib.bluetooth.LocalBluetoothManager;
34 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
35 
36 /**
37  * Displays toggles for Bluetooth profiles supported by a device. Toggling a profile on will set it
38  * as preferred and attempt a connection. Toggling a profile off will disconnect the profile. If no
39  * profiles are supported, the preference is hidden.
40  */
41 public class BluetoothDeviceProfilesPreferenceController extends
42         BluetoothDevicePreferenceController<PreferenceGroup> {
43 
44     private final Preference.OnPreferenceChangeListener mProfileChangeListener =
45             (preference, newValue) -> {
46                 boolean isChecked = (boolean) newValue;
47                 BluetoothDeviceProfilePreference profilePref =
48                         (BluetoothDeviceProfilePreference) preference;
49                 LocalBluetoothProfile profile = profilePref.getProfile();
50                 profile.setEnabled(profilePref.getCachedDevice().getDevice(), isChecked);
51                 return true;
52             };
53 
BluetoothDeviceProfilesPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)54     public BluetoothDeviceProfilesPreferenceController(Context context, String preferenceKey,
55             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
56         super(context, preferenceKey, fragmentController, uxRestrictions);
57     }
58 
59 
60     @VisibleForTesting
BluetoothDeviceProfilesPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, LocalBluetoothManager localBluetoothManager, UserManager userManager)61     BluetoothDeviceProfilesPreferenceController(Context context, String preferenceKey,
62             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
63             LocalBluetoothManager localBluetoothManager, UserManager userManager) {
64         super(context, preferenceKey, fragmentController, uxRestrictions, localBluetoothManager,
65                 userManager);
66     }
67 
68     @Override
getPreferenceType()69     protected Class<PreferenceGroup> getPreferenceType() {
70         return PreferenceGroup.class;
71     }
72 
73     @Override
updateState(PreferenceGroup preferenceGroup)74     protected void updateState(PreferenceGroup preferenceGroup) {
75         for (LocalBluetoothProfile profile : getCachedDevice().getProfiles()) {
76             Preference profilePref = preferenceGroup.findPreference(profile.toString());
77             if (profilePref == null) {
78                 profilePref = new BluetoothDeviceProfilePreference(getContext(), profile,
79                         getCachedDevice());
80                 profilePref.setOnPreferenceChangeListener(mProfileChangeListener);
81                 if (hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_BLUETOOTH)) {
82                     setClickableWhileDisabled(profilePref, /* clickable= */ true, p ->
83                             onClickWhileDisabled(getContext(),
84                                     getFragmentController(), DISALLOW_CONFIG_BLUETOOTH));
85                 }
86                 preferenceGroup.addPreference(profilePref);
87             }
88         }
89         for (LocalBluetoothProfile removedProfile : getCachedDevice().getRemovedProfiles()) {
90             Preference prefToRemove = preferenceGroup.findPreference(removedProfile.toString());
91             if (prefToRemove != null) {
92                 preferenceGroup.removePreference(prefToRemove);
93             }
94         }
95         preferenceGroup.setVisible(preferenceGroup.getPreferenceCount() > 0);
96     }
97 }
98