1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/bluetooth/bluetooth_device_list_item_view.h"
#include <string>
#include <string_view>
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/typography.h"
#include "ash/system/bluetooth/bluetooth_device_list_item_battery_view.h"
#include "ash/system/bluetooth/bluetooth_device_list_item_multiple_battery_view.h"
#include "ash/system/tray/hover_highlight_view.h"
#include "ash/system/tray/tray_utils.h"
#include "base/check.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "chromeos/ash/services/bluetooth_config/public/cpp/cros_bluetooth_config_util.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "chromeos/ui/vector_icons/vector_icons.h"
#include "mojo/public/cpp/bindings/clone_traits.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/label.h"
namespace ash {
namespace {
using bluetooth_config::GetPairedDeviceName;
using bluetooth_config::mojom::BatteryPropertiesPtr;
using bluetooth_config::mojom::DeviceBatteryInfoPtr;
using bluetooth_config::mojom::DeviceConnectionState;
using bluetooth_config::mojom::DeviceType;
using bluetooth_config::mojom::PairedBluetoothDevicePropertiesPtr;
constexpr int kEnterpriseManagedIconSizeDip = 20;
bool HasMultipleBatteryInfos(const DeviceBatteryInfoPtr& battery_info) {
DCHECK(battery_info);
return battery_info->left_bud_info || battery_info->case_info ||
battery_info->right_bud_info;
}
// Returns the text ID corresponding to the provided |device_connection_state|.
int GetDeviceConnectionStateA11yTextId(
const DeviceConnectionState device_connection_state) {
switch (device_connection_state) {
case DeviceConnectionState::kConnected:
return IDS_BLUETOOTH_A11Y_DEVICE_CONNECTION_STATE_CONNECTED;
case DeviceConnectionState::kConnecting:
return IDS_BLUETOOTH_A11Y_DEVICE_CONNECTION_STATE_CONNECTING;
case DeviceConnectionState::kNotConnected:
return IDS_BLUETOOTH_A11Y_DEVICE_CONNECTION_STATE_NOT_CONNECTED;
}
NOTREACHED();
}
// Returns the text ID corresponding to the provided |device_type|.
int GetDeviceTypeA11yTextId(const DeviceType device_type) {
switch (device_type) {
case DeviceType::kComputer:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_COMPUTER;
case DeviceType::kPhone:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_PHONE;
case DeviceType::kHeadset:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_HEADSET;
case DeviceType::kVideoCamera:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_VIDEO_CAMERA;
case DeviceType::kGameController:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_GAME_CONTROLLER;
case DeviceType::kKeyboard:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_KEYBOARD;
case DeviceType::kKeyboardMouseCombo:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_KEYBOARD_MOUSE_COMBO;
case DeviceType::kMouse:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_MOUSE;
case DeviceType::kTablet:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_TABLET;
case DeviceType::kUnknown:
return IDS_BLUETOOTH_A11Y_DEVICE_TYPE_UNKNOWN;
}
NOTREACHED();
}
// Returns the formatted a11y text describing the battery information of the
// provided |battery_info|.
const std::u16string GetDeviceBatteryA11yText(
const DeviceBatteryInfoPtr& battery_info) {
if (!battery_info) {
return std::u16string();
}
if (HasMultipleBatteryInfos(battery_info)) {
std::u16string battery_text;
auto add_battery_text_if_exists =
[&battery_text](const BatteryPropertiesPtr& battery_properties,
int text_id) {
if (!battery_properties) {
return;
}
if (!battery_text.empty()) {
battery_text = base::StrCat({battery_text, u" "});
}
battery_text = base::StrCat(
{battery_text,
l10n_util::GetStringFUTF16(
text_id, base::NumberToString16(
battery_properties->battery_percentage))});
};
add_battery_text_if_exists(
battery_info->left_bud_info,
IDS_BLUETOOTH_A11Y_DEVICE_NAMED_BATTERY_INFO_LEFT_BUD);
add_battery_text_if_exists(
battery_info->case_info,
IDS_BLUETOOTH_A11Y_DEVICE_NAMED_BATTERY_INFO_CASE);
add_battery_text_if_exists(
battery_info->right_bud_info,
IDS_BLUETOOTH_A11Y_DEVICE_NAMED_BATTERY_INFO_RIGHT_BUD);
return battery_text;
}
if (battery_info->default_properties) {
return l10n_util::GetStringFUTF16(
IDS_BLUETOOTH_A11Y_DEVICE_BATTERY_INFO,
base::NumberToString16(
battery_info->default_properties->battery_percentage));
}
return std::u16string();
}
// Returns the icon corresponding to the provided |device_type| and
// |connection_state|.
const gfx::VectorIcon& GetDeviceIcon(const DeviceType device_type) {
switch (device_type) {
case DeviceType::kComputer:
return ash::kSystemMenuComputerIcon;
case DeviceType::kPhone:
return ash::kSystemMenuPhoneIcon;
case DeviceType::kHeadset:
return ash::kSystemMenuHeadsetIcon;
case DeviceType::kVideoCamera:
return ash::kSystemMenuVideocamIcon;
case DeviceType::kGameController:
return ash::kSystemMenuGamepadIcon;
case DeviceType::kKeyboard:
return ash::kSystemMenuKeyboardIcon;
case DeviceType::kKeyboardMouseCombo:
return ash::kSystemMenuKeyboardIcon;
case DeviceType::kMouse:
return ash::kSystemMenuMouseIcon;
case DeviceType::kTablet:
return ash::kSystemMenuTabletIcon;
case DeviceType::kUnknown:
return ash::kSystemMenuBluetoothIcon;
}
NOTREACHED();
}
} // namespace
BluetoothDeviceListItemView::BluetoothDeviceListItemView(
ViewClickListener* listener)
: HoverHighlightView(listener) {}
BluetoothDeviceListItemView::~BluetoothDeviceListItemView() = default;
void BluetoothDeviceListItemView::UpdateDeviceProperties(
size_t device_index,
size_t total_device_count,
const PairedBluetoothDevicePropertiesPtr& device_properties) {
device_properties_ = mojo::Clone(device_properties);
// We can only add an icon and label if the view has not already been
// populated with one or both of these views. For simplicity, instead of
// trying to determine which views exist and modifying them, and creating the
// missing views, we instead clear all of the views and recreate them.
if (is_populated()) {
Reset();
}
const DeviceType& device_type =
device_properties_->device_properties->device_type;
AddIconAndLabel(ui::ImageModel::FromVectorIcon(
GetDeviceIcon(device_type),
static_cast<ui::ColorId>(cros_tokens::kCrosSysOnSurface)),
GetPairedDeviceName(device_properties_));
text_label()->SetEnabledColor(cros_tokens::kCrosSysOnSurface);
TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosButton2,
*text_label());
UpdateAccessibleName(device_index, total_device_count);
// Adds an icon to indicate that the device supports profiles or services that
// are disabled or blocked by enterprise policy.
if (device_properties->device_properties->is_blocked_by_policy) {
AddRightIcon(ui::ImageModel::FromVectorIcon(
chromeos::kEnterpriseIcon,
static_cast<ui::ColorId>(cros_tokens::kCrosSysOnSurface),
kEnterpriseManagedIconSizeDip),
/*icon_size=*/kEnterpriseManagedIconSizeDip);
}
const DeviceConnectionState& connection_state =
device_properties_->device_properties->connection_state;
// Adds a sub-label to show that the device is in the process of connecting.
if (connection_state == DeviceConnectionState::kConnecting) {
SetupConnectingScrollListItem(this);
} else if (connection_state == DeviceConnectionState::kConnected) {
UpdateBatteryInfo(device_properties_->device_properties->battery_info);
}
}
void BluetoothDeviceListItemView::UpdateAccessibleName(
size_t device_index,
size_t total_device_count) {
DCHECK(device_properties_);
// It is not best practice to concatenate translated strings together, but in
// this case we would have an explosion in the number of strings if we had a
// unique string for each permutation. Instead, below we concatenate related
// but complete sentences here with a hard stop, e.g. a period.
// Add the device name information.
std::u16string a11y_text = l10n_util::GetStringFUTF16(
IDS_BLUETOOTH_A11Y_DEVICE_NAME, base::NumberToString16(device_index + 1),
base::NumberToString16(total_device_count),
GetPairedDeviceName(device_properties_));
// Add the device connection status information.
a11y_text = base::StrCat(
{a11y_text, u" ",
l10n_util::GetStringUTF16(GetDeviceConnectionStateA11yTextId(
device_properties_->device_properties->connection_state))});
// Add the device type information.
a11y_text =
base::StrCat({a11y_text, u" ",
l10n_util::GetStringUTF16(GetDeviceTypeA11yTextId(
device_properties_->device_properties->device_type))});
const std::u16string battery_text = GetDeviceBatteryA11yText(
device_properties_->device_properties->battery_info);
if (!battery_text.empty()) {
a11y_text = base::StrCat({a11y_text, u" ", battery_text});
}
GetViewAccessibility().SetName(a11y_text);
}
void BluetoothDeviceListItemView::UpdateBatteryInfo(
const DeviceBatteryInfoPtr& battery_info) {
if (!battery_info || (!battery_info->default_properties &&
!HasMultipleBatteryInfos(battery_info))) {
sub_row()->RemoveAllChildViews();
return;
}
if (HasMultipleBatteryInfos(battery_info)) {
UpdateMultipleBatteryView(battery_info);
return;
}
UpdateSingleBatteryView(battery_info);
}
void BluetoothDeviceListItemView::UpdateMultipleBatteryView(
const DeviceBatteryInfoPtr& battery_info) {
// Remove battery view if it is not a multiple battery view.
if (!sub_row()->children().empty()) {
DCHECK(sub_row()->children().size() == 1);
if (sub_row()->children().at(0)->GetClassName() !=
BluetoothDeviceListItemMultipleBatteryView::kViewClassName) {
sub_row()->RemoveAllChildViews();
}
}
BluetoothDeviceListItemMultipleBatteryView* battery_view = nullptr;
// Add multiple battery view if missing.
if (sub_row()->children().empty()) {
battery_view = sub_row()->AddChildView(
std::make_unique<BluetoothDeviceListItemMultipleBatteryView>());
} else {
DCHECK_EQ(1u, sub_row()->children().size());
battery_view = static_cast<BluetoothDeviceListItemMultipleBatteryView*>(
sub_row()->children().at(0));
}
// Update multiple battery view.
battery_view->UpdateBatteryInfo(battery_info);
}
void BluetoothDeviceListItemView::UpdateSingleBatteryView(
const DeviceBatteryInfoPtr& battery_info) {
// Remove battery view if it is not a single battery view.
if (!sub_row()->children().empty()) {
DCHECK(sub_row()->children().size() == 1);
if (sub_row()->children().at(0)->GetClassName() !=
BluetoothDeviceListItemBatteryView::kViewClassName) {
sub_row()->RemoveAllChildViews();
}
}
BluetoothDeviceListItemBatteryView* battery_view = nullptr;
// Add single battery view if missing.
if (sub_row()->children().empty()) {
battery_view = sub_row()->AddChildView(
std::make_unique<BluetoothDeviceListItemBatteryView>());
} else {
DCHECK_EQ(1u, sub_row()->children().size());
battery_view = static_cast<BluetoothDeviceListItemBatteryView*>(
sub_row()->children().at(0));
}
// Update single battery view.
battery_view->UpdateBatteryInfo(
battery_info->default_properties->battery_percentage,
IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_ONLY_LABEL);
}
BEGIN_METADATA(BluetoothDeviceListItemView)
END_METADATA
} // namespace ash
|