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.bluetooth.BluetoothProfile;
20 import android.car.drivingstate.CarUxRestrictions;
21 import android.content.Context;
22 import android.graphics.drawable.Drawable;
23 import android.text.TextUtils;
24 import android.util.Pair;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 
29 import com.android.car.settings.R;
30 import com.android.car.settings.common.FragmentController;
31 import com.android.settingslib.bluetooth.BluetoothUtils;
32 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
33 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
34 
35 import java.util.StringJoiner;
36 
37 /**
38  * Displays the name, icon, and status (connected/disconnected, etc.) of a remote Bluetooth device.
39  * When the associated preference is clicked, a dialog is shown to allow the user to update the
40  * display name of the remote device.
41  */
42 public class BluetoothDeviceNamePreferenceController extends
43         BluetoothDevicePreferenceController<Preference> {
44 
BluetoothDeviceNamePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)45     public BluetoothDeviceNamePreferenceController(Context context, String preferenceKey,
46             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
47         super(context, preferenceKey, fragmentController, uxRestrictions);
48     }
49 
50     @Override
getPreferenceType()51     protected Class<Preference> getPreferenceType() {
52         return Preference.class;
53     }
54 
55     @Override
updateState(Preference preference)56     protected void updateState(Preference preference) {
57         CachedBluetoothDevice cachedDevice = getCachedDevice();
58         Pair<Drawable, String> pair =
59                 com.android.settingslib.bluetooth.BluetoothUtils.getBtClassDrawableWithDescription(
60                         getContext(),
61                         cachedDevice);
62         StringJoiner summaryJoiner = new StringJoiner(System.lineSeparator());
63         summaryJoiner.setEmptyValue("");
64 
65         // Manually set "Disconnected" summary since CachedBluetoothDevice.getCarConnectionSummary()
66         // does not return a string when disconnected.
67         // TODO: Move branching logic into getCarConnectionSummary()
68         if (!cachedDevice.isConnected()) {
69             summaryJoiner.add(getContext().getString(BluetoothUtils
70                     .getConnectionStateSummary(BluetoothProfile.STATE_DISCONNECTED)));
71         } else {
72             String summaryText = cachedDevice.getCarConnectionSummary();
73             if (!TextUtils.isEmpty(summaryText)) {
74                 summaryJoiner.add(summaryText);
75             }
76         }
77         // If hearing aids are connected, two battery statuses should be shown.
78         String pairDeviceSummary = getCachedDeviceManager().getSubDeviceSummary(cachedDevice);
79         if (!TextUtils.isEmpty(pairDeviceSummary)) {
80             summaryJoiner.add(pairDeviceSummary);
81         }
82         preference.setTitle(cachedDevice.getName());
83         preference.setIcon(pair.first);
84         preference.getIcon().setTintList(
85                 getContext().getColorStateList(R.color.icon_color_default));
86         preference.setSummary(summaryJoiner.toString());
87     }
88 
89     @Override
handlePreferenceClicked(Preference preference)90     protected boolean handlePreferenceClicked(Preference preference) {
91         getFragmentController().showDialog(
92                 RemoteRenameDialogFragment.newInstance(getCachedDevice()),
93                 RemoteRenameDialogFragment.TAG);
94         return true;
95     }
96 
97     @VisibleForTesting
getCachedDeviceManager()98     CachedBluetoothDeviceManager getCachedDeviceManager() {
99         return getBluetoothManager().getCachedDeviceManager();
100     }
101 }
102