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