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