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