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.bluetooth.BluetoothProfile.STATE_CONNECTED;
20 
21 import android.content.Context;
22 
23 import androidx.preference.SwitchPreference;
24 
25 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
26 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
27 import com.android.settingslib.bluetooth.PanProfile;
28 
29 /**
30  * Preference that represents a {@link LocalBluetoothProfile} for a {@link CachedBluetoothDevice}.
31  */
32 public class BluetoothDeviceProfilePreference extends SwitchPreference {
33 
34     private final LocalBluetoothProfile mProfile;
35     private final CachedBluetoothDevice mCachedDevice;
36     private final CachedBluetoothDevice.Callback mDeviceCallback = this::refreshUi;
37 
BluetoothDeviceProfilePreference(Context context, LocalBluetoothProfile profile, CachedBluetoothDevice cachedDevice)38     public BluetoothDeviceProfilePreference(Context context, LocalBluetoothProfile profile,
39             CachedBluetoothDevice cachedDevice) {
40         super(context);
41         mProfile = profile;
42         mCachedDevice = cachedDevice;
43         setKey(profile.toString());
44         setTitle(profile.getNameResource(cachedDevice.getDevice()));
45     }
46 
47     /**
48      * Returns the {@link LocalBluetoothProfile} represented by this preference.
49      */
getProfile()50     public LocalBluetoothProfile getProfile() {
51         return mProfile;
52     }
53 
54     /**
55      * Returns the {@link CachedBluetoothDevice} used to construct this preference.
56      */
getCachedDevice()57     public CachedBluetoothDevice getCachedDevice() {
58         return mCachedDevice;
59     }
60 
61     @Override
onAttached()62     public void onAttached() {
63         super.onAttached();
64         mCachedDevice.registerCallback(mDeviceCallback);
65         refreshUi();
66     }
67 
68     @Override
onDetached()69     public void onDetached() {
70         super.onDetached();
71         mCachedDevice.unregisterCallback(mDeviceCallback);
72     }
73 
refreshUi()74     private void refreshUi() {
75         setEnabled(!mCachedDevice.isBusy());
76         if (mProfile instanceof PanProfile) {
77             setChecked(
78                     mProfile.getConnectionStatus(mCachedDevice.getDevice()) == STATE_CONNECTED);
79         } else {
80             setChecked(mProfile.isEnabled(mCachedDevice.getDevice()));
81         }
82     }
83 }
84