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 static com.android.car.settings.enterprise.EnterpriseUtils.getAvailabilityStatusRestricted; 22 23 import android.car.drivingstate.CarUxRestrictions; 24 import android.content.Context; 25 import android.os.UserManager; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 30 import com.android.car.settings.common.FragmentController; 31 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 32 import com.android.settingslib.bluetooth.LocalBluetoothManager; 33 34 /** 35 * Encapsulates common functionality for all {@link BluetoothPreferenceController} instances 36 * which display state of a specific {@link CachedBluetoothDevice}. The controller will refresh 37 * the UI whenever the device properties change. The controller is not available to users with 38 * the {@link UserManager#DISALLOW_CONFIG_BLUETOOTH} restriction. 39 * 40 * @param <V> the upper bound on the type of {@link Preference} on which the controller expects 41 * to operate. 42 */ 43 public abstract class BluetoothDevicePreferenceController<V extends Preference> extends 44 BluetoothPreferenceController<V> { 45 46 private final CachedBluetoothDevice.Callback mDeviceCallback = this::refreshUi; 47 private CachedBluetoothDevice mCachedDevice; 48 BluetoothDevicePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)49 public BluetoothDevicePreferenceController(Context context, String preferenceKey, 50 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 51 super(context, preferenceKey, fragmentController, uxRestrictions); 52 } 53 54 @VisibleForTesting BluetoothDevicePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, LocalBluetoothManager localBluetoothManager, UserManager userManager)55 BluetoothDevicePreferenceController(Context context, String preferenceKey, 56 FragmentController fragmentController, CarUxRestrictions uxRestrictions, 57 LocalBluetoothManager localBluetoothManager, UserManager userManager) { 58 super(context, preferenceKey, fragmentController, uxRestrictions, localBluetoothManager, 59 userManager); 60 } 61 62 /** 63 * Sets the {@link CachedBluetoothDevice} which the controller represents. The device must be 64 * set or an {@link IllegalStateException} will be thrown when this controller begins its 65 * lifecycle. 66 */ setCachedDevice(CachedBluetoothDevice device)67 public void setCachedDevice(CachedBluetoothDevice device) { 68 mCachedDevice = device; 69 } 70 71 /** 72 * Returns the {@link CachedBluetoothDevice} represented by this controller. 73 */ getCachedDevice()74 public CachedBluetoothDevice getCachedDevice() { 75 return mCachedDevice; 76 } 77 78 79 @Override checkInitialized()80 protected void checkInitialized() { 81 if (mCachedDevice == null) { 82 throw new IllegalStateException("Must be initialized with a CachedBluetoothDevice"); 83 } 84 } 85 86 @Override getDefaultAvailabilityStatus()87 protected int getDefaultAvailabilityStatus() { 88 int availabilityStatus = super.getDefaultAvailabilityStatus(); 89 if (availabilityStatus == AVAILABLE 90 && getUserManager().hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)) { 91 return getAvailabilityStatusRestricted(getContext(), DISALLOW_CONFIG_BLUETOOTH); 92 } 93 return availabilityStatus; 94 } 95 96 @Override onStartInternal()97 protected void onStartInternal() { 98 super.onStartInternal(); 99 mCachedDevice.registerCallback(mDeviceCallback); 100 } 101 102 @Override onStopInternal()103 protected void onStopInternal() { 104 super.onStopInternal(); 105 mCachedDevice.unregisterCallback(mDeviceCallback); 106 } 107 } 108