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