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