• 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.setPreferred(profilePref.getCachedDevice().getDevice(), isChecked);
43                 if (isChecked) {
44                     getCachedDevice().connectProfile(profile);
45                 } else {
46                     getCachedDevice().disconnect(profile);
47                 }
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     @Override
getPreferenceType()57     protected Class<PreferenceGroup> getPreferenceType() {
58         return PreferenceGroup.class;
59     }
60 
61     @Override
updateState(PreferenceGroup preferenceGroup)62     protected void updateState(PreferenceGroup preferenceGroup) {
63         for (LocalBluetoothProfile profile : getCachedDevice().getProfiles()) {
64             Preference profilePref = preferenceGroup.findPreference(profile.toString());
65             if (profilePref == null) {
66                 profilePref = new BluetoothDeviceProfilePreference(getContext(), profile,
67                         getCachedDevice());
68                 profilePref.setOnPreferenceChangeListener(mProfileChangeListener);
69                 preferenceGroup.addPreference(profilePref);
70             }
71         }
72         for (LocalBluetoothProfile removedProfile : getCachedDevice().getRemovedProfiles()) {
73             Preference prefToRemove = preferenceGroup.findPreference(removedProfile.toString());
74             if (prefToRemove != null) {
75                 preferenceGroup.removePreference(prefToRemove);
76             }
77         }
78         preferenceGroup.setVisible(preferenceGroup.getPreferenceCount() > 0);
79     }
80 }
81