bluetooth_device_status_ui_handler.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/bluetooth/bluetooth_device_status_ui_handler.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/constants/notifier_catalogs.h"
  7. #include "ash/public/cpp/bluetooth_config_service.h"
  8. #include "ash/public/cpp/system/toast_data.h"
  9. #include "ash/public/cpp/system/toast_manager.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "base/bind.h"
  12. #include "base/check.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "chromeos/services/bluetooth_config/public/cpp/cros_bluetooth_config_util.h"
  15. #include "device/bluetooth/chromeos/bluetooth_utils.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. namespace ash {
  18. namespace {
  19. using chromeos::bluetooth_config::GetPairedDeviceName;
  20. using chromeos::bluetooth_config::mojom::PairedBluetoothDevicePropertiesPtr;
  21. const char kBluetoothToastIdPrefix[] = "cros_bluetooth_device_toast_id-";
  22. } // namespace
  23. BluetoothDeviceStatusUiHandler::BluetoothDeviceStatusUiHandler() {
  24. DCHECK(ash::features::IsBluetoothRevampEnabled());
  25. // Asynchronously bind to CrosBluetoothConfig so that we don't want to attempt
  26. // to bind to it before it has initialized.
  27. base::ThreadTaskRunnerHandle::Get()->PostTask(
  28. FROM_HERE,
  29. base::BindOnce(&BluetoothDeviceStatusUiHandler::BindToCrosBluetoothConfig,
  30. weak_ptr_factory_.GetWeakPtr()));
  31. }
  32. BluetoothDeviceStatusUiHandler::~BluetoothDeviceStatusUiHandler() = default;
  33. void BluetoothDeviceStatusUiHandler::OnDevicePaired(
  34. PairedBluetoothDevicePropertiesPtr device) {
  35. ash::ToastData toast_data(
  36. /*id=*/GetToastId(device.get()),
  37. ash::ToastCatalogName::kBluetoothDevicePaired,
  38. /*text=*/
  39. l10n_util::GetStringFUTF16(
  40. IDS_ASH_STATUS_TRAY_BLUETOOTH_PAIRED_OR_CONNECTED_TOAST,
  41. GetPairedDeviceName(device)));
  42. ShowToast(toast_data);
  43. device::RecordUiSurfaceDisplayed(device::BluetoothUiSurface::kPairedToast);
  44. }
  45. void BluetoothDeviceStatusUiHandler::OnDeviceDisconnected(
  46. PairedBluetoothDevicePropertiesPtr device) {
  47. ash::ToastData toast_data(
  48. /*id=*/GetToastId(device.get()),
  49. ash::ToastCatalogName::kBluetoothDeviceDisconnected,
  50. /*text=*/
  51. l10n_util::GetStringFUTF16(
  52. IDS_ASH_STATUS_TRAY_BLUETOOTH_DISCONNECTED_TOAST,
  53. GetPairedDeviceName(device)));
  54. ShowToast(toast_data);
  55. device::RecordUiSurfaceDisplayed(
  56. device::BluetoothUiSurface::kDisconnectedToast);
  57. }
  58. void BluetoothDeviceStatusUiHandler::OnDeviceConnected(
  59. PairedBluetoothDevicePropertiesPtr device) {
  60. ash::ToastData toast_data(
  61. /*id=*/GetToastId(device.get()),
  62. ash::ToastCatalogName::kBluetoothDeviceConnected,
  63. /*text=*/
  64. l10n_util::GetStringFUTF16(
  65. IDS_ASH_STATUS_TRAY_BLUETOOTH_PAIRED_OR_CONNECTED_TOAST,
  66. GetPairedDeviceName(device)));
  67. ShowToast(toast_data);
  68. device::RecordUiSurfaceDisplayed(
  69. device::BluetoothUiSurface::kConnectionToast);
  70. }
  71. void BluetoothDeviceStatusUiHandler::ShowToast(
  72. const ash::ToastData& toast_data) {
  73. ash::ToastManager::Get()->Show(toast_data);
  74. }
  75. std::string BluetoothDeviceStatusUiHandler::GetToastId(
  76. const chromeos::bluetooth_config::mojom::PairedBluetoothDeviceProperties*
  77. paired_device_properties) {
  78. return kBluetoothToastIdPrefix +
  79. base::ToLowerASCII(paired_device_properties->device_properties->id);
  80. }
  81. void BluetoothDeviceStatusUiHandler::BindToCrosBluetoothConfig() {
  82. GetBluetoothConfigService(
  83. remote_cros_bluetooth_config_.BindNewPipeAndPassReceiver());
  84. remote_cros_bluetooth_config_->ObserveDeviceStatusChanges(
  85. cros_bluetooth_device_status_observer_receiver_
  86. .BindNewPipeAndPassRemote());
  87. }
  88. } // namespace ash