• 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 android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.os.SystemProperties;
22 import android.util.Pair;
23 
24 import androidx.annotation.NonNull;
25 import androidx.preference.Preference;
26 
27 import com.android.car.apps.common.util.Themes;
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.ButtonPreference;
30 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
31 
32 /**
33  * Preference which represents a specific {@link CachedBluetoothDevice}. The title, icon, and
34  * summary are kept in sync with the device when the preference is shown. When the device is busy,
35  * the preference is disabled. The equality and sort order of this preference is determined by the
36  * underlying cached device {@link CachedBluetoothDevice#equals(Object)} and {@link
37  * CachedBluetoothDevice#compareTo(CachedBluetoothDevice)}. If two devices are considered equal, the
38  * default preference sort ordering is used (see {@link #compareTo(Preference)}.
39  */
40 public class BluetoothDevicePreference extends ButtonPreference {
41     private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY =
42             "persist.bluetooth.showdeviceswithoutnames";
43 
44     private final CachedBluetoothDevice mCachedDevice;
45     private final boolean mShowDevicesWithoutNames;
46     private final CachedBluetoothDevice.Callback mDeviceCallback = this::refreshUi;
47 
BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice)48     public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) {
49         super(context);
50         mCachedDevice = cachedDevice;
51         mShowDevicesWithoutNames = SystemProperties.getBoolean(
52                 BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY, false);
53         // Hide action by default.
54         showAction(false);
55     }
56 
57     /**
58      * Returns the {@link CachedBluetoothDevice} represented by this preference.
59      */
getCachedDevice()60     public CachedBluetoothDevice getCachedDevice() {
61         return mCachedDevice;
62     }
63 
64     @Override
onAttached()65     public void onAttached() {
66         super.onAttached();
67         mCachedDevice.registerCallback(mDeviceCallback);
68         refreshUi();
69     }
70 
71     @Override
onDetached()72     public void onDetached() {
73         super.onDetached();
74         mCachedDevice.unregisterCallback(mDeviceCallback);
75     }
76 
refreshUi()77     private void refreshUi() {
78         setTitle(mCachedDevice.getName());
79         setSummary(mCachedDevice.getCarConnectionSummary());
80 
81         final Pair<Drawable, String> pair = com.android.settingslib.bluetooth.BluetoothUtils
82                 .getBtClassDrawableWithDescription(getContext(), mCachedDevice);
83         if (pair.first != null) {
84             setIcon(pair.first);
85             getIcon().setTintList(Themes.getAttrColorStateList(getContext(), R.attr.iconColor));
86         }
87 
88         setEnabled(!mCachedDevice.isBusy());
89         setVisible(mShowDevicesWithoutNames || mCachedDevice.hasHumanReadableName());
90 
91         // Notify since the ordering may have changed.
92         notifyHierarchyChanged();
93     }
94 
95     @Override
equals(Object o)96     public boolean equals(Object o) {
97         if (!(o instanceof BluetoothDevicePreference)) {
98             return false;
99         }
100         return mCachedDevice.equals(((BluetoothDevicePreference) o).mCachedDevice);
101     }
102 
103     @Override
hashCode()104     public int hashCode() {
105         return mCachedDevice.hashCode();
106     }
107 
108     @Override
compareTo(@onNull Preference another)109     public int compareTo(@NonNull Preference another) {
110         if (!(another instanceof BluetoothDevicePreference)) {
111             // Rely on default sort.
112             return super.compareTo(another);
113         }
114 
115         return mCachedDevice
116                 .compareTo(((BluetoothDevicePreference) another).mCachedDevice);
117     }
118 }
119