• 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 android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 
24 import androidx.preference.PreferenceGroup;
25 
26 import com.android.car.settings.R;
27 import com.android.car.settings.common.CarUxRestrictionsHelper;
28 import com.android.car.settings.common.FragmentController;
29 import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
30 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
31 
32 /**
33  * Displays a list of bonded (paired) Bluetooth devices. Clicking on a device will attempt a
34  * connection with that device. If a device is already connected, a click will prompt the user to
35  * disconnect. Devices are shown with an addition affordance which launches the device details page.
36  */
37 public class BluetoothBondedDevicesPreferenceController extends
38         BluetoothDevicesGroupPreferenceController {
39 
BluetoothBondedDevicesPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)40     public BluetoothBondedDevicesPreferenceController(Context context, String preferenceKey,
41             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
42         super(context, preferenceKey, fragmentController, uxRestrictions);
43     }
44 
45     @Override
getDeviceFilter()46     protected BluetoothDeviceFilter.Filter getDeviceFilter() {
47         return BluetoothDeviceFilter.BONDED_DEVICE_FILTER;
48     }
49 
50     @Override
createDevicePreference(CachedBluetoothDevice cachedDevice)51     protected BluetoothDevicePreference createDevicePreference(CachedBluetoothDevice cachedDevice) {
52         BluetoothDevicePreference pref = super.createDevicePreference(cachedDevice);
53         pref.setSecondaryActionIcon(R.drawable.ic_settings_gear);
54         pref.setOnSecondaryActionClickListener(() -> getFragmentController().launchFragment(
55                 BluetoothDeviceDetailsFragment.newInstance(cachedDevice)));
56         return pref;
57     }
58 
59     @Override
onDeviceClicked(CachedBluetoothDevice cachedDevice)60     protected void onDeviceClicked(CachedBluetoothDevice cachedDevice) {
61         if (cachedDevice.isConnected()) {
62             getFragmentController().showDialog(
63                     BluetoothDisconnectConfirmDialogFragment.newInstance(cachedDevice),
64                     /* tag= */ null);
65         } else {
66             cachedDevice.connect(/* connectAllProfiles= */ true);
67         }
68     }
69 
70     @Override
onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState)71     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
72         refreshUi();
73     }
74 
75     @Override
updateState(PreferenceGroup preferenceGroup)76     protected void updateState(PreferenceGroup preferenceGroup) {
77         super.updateState(preferenceGroup);
78 
79         boolean hasUserRestriction = getUserManager().hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH);
80         updateActionVisibility(preferenceGroup, !hasUserRestriction);
81     }
82 
83     @Override
onApplyUxRestrictions(CarUxRestrictions uxRestrictions)84     protected void onApplyUxRestrictions(CarUxRestrictions uxRestrictions) {
85         super.onApplyUxRestrictions(uxRestrictions);
86 
87         if (CarUxRestrictionsHelper.isNoSetup(uxRestrictions)) {
88             updateActionVisibility(getPreference(), /* isActionVisible= */ false);
89         }
90     }
91 
updateActionVisibility(PreferenceGroup group, boolean isActionVisible)92     private void updateActionVisibility(PreferenceGroup group, boolean isActionVisible) {
93         for (int i = 0; i < group.getPreferenceCount(); i++) {
94             ((BluetoothDevicePreference) group.getPreference(i)).setSecondaryActionVisible(
95                     isActionVisible);
96         }
97     }
98 }
99