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