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