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