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