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