bluetooth_feature_pod_controller_legacy.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2018 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_feature_pod_controller_legacy.h"
  5. #include <utility>
  6. #include "ash/resources/vector_icons/vector_icons.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/shell.h"
  9. #include "ash/strings/grit/ash_strings.h"
  10. #include "ash/system/bluetooth/tray_bluetooth_helper.h"
  11. #include "ash/system/unified/feature_pod_button.h"
  12. #include "ash/system/unified/unified_system_tray_controller.h"
  13. #include "base/i18n/number_formatting.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "services/device/public/cpp/bluetooth/bluetooth_utils.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. using device::mojom::BluetoothDeviceInfo;
  18. using device::mojom::BluetoothSystem;
  19. namespace ash {
  20. BluetoothFeaturePodControllerLegacy::BluetoothFeaturePodControllerLegacy(
  21. UnifiedSystemTrayController* tray_controller)
  22. : tray_controller_(tray_controller) {
  23. Shell::Get()->tray_bluetooth_helper()->AddObserver(this);
  24. }
  25. BluetoothFeaturePodControllerLegacy::~BluetoothFeaturePodControllerLegacy() {
  26. auto* helper = Shell::Get()->tray_bluetooth_helper();
  27. if (helper)
  28. helper->RemoveObserver(this);
  29. }
  30. FeaturePodButton* BluetoothFeaturePodControllerLegacy::CreateButton() {
  31. DCHECK(!button_);
  32. button_ = new FeaturePodButton(this);
  33. button_->ShowDetailedViewArrow();
  34. UpdateButton();
  35. return button_;
  36. }
  37. void BluetoothFeaturePodControllerLegacy::OnIconPressed() {
  38. bool was_enabled = button_->IsToggled();
  39. Shell::Get()->tray_bluetooth_helper()->SetBluetoothEnabled(!was_enabled);
  40. // If Bluetooth was disabled, show device list as well as enabling Bluetooth.
  41. if (!was_enabled)
  42. tray_controller_->ShowBluetoothDetailedView();
  43. }
  44. void BluetoothFeaturePodControllerLegacy::OnLabelPressed() {
  45. Shell::Get()->tray_bluetooth_helper()->SetBluetoothEnabled(true);
  46. tray_controller_->ShowBluetoothDetailedView();
  47. }
  48. SystemTrayItemUmaType BluetoothFeaturePodControllerLegacy::GetUmaType() const {
  49. return SystemTrayItemUmaType::UMA_BLUETOOTH;
  50. }
  51. void BluetoothFeaturePodControllerLegacy::UpdateButton() {
  52. bool is_available =
  53. Shell::Get()->tray_bluetooth_helper()->IsBluetoothStateAvailable();
  54. button_->SetVisible(is_available);
  55. if (!is_available)
  56. return;
  57. // Bluetooth power setting is always mutable in login screen before any
  58. // user logs in. The changes will affect local state preferences.
  59. //
  60. // Otherwise, the bluetooth setting should be mutable only if:
  61. // * the active user is the primary user, and
  62. // * the session is not in lock screen
  63. // The changes will affect the primary user's preferences.
  64. SessionControllerImpl* session_controller =
  65. Shell::Get()->session_controller();
  66. button_->SetEnabled(!session_controller->IsActiveUserSessionStarted() ||
  67. (session_controller->IsUserPrimary() &&
  68. !session_controller->IsScreenLocked()));
  69. bool is_enabled =
  70. Shell::Get()->tray_bluetooth_helper()->GetBluetoothState() ==
  71. BluetoothSystem::State::kPoweredOn;
  72. button_->SetToggled(is_enabled);
  73. if (!is_enabled) {
  74. button_->SetVectorIcon(kUnifiedMenuBluetoothLegacyIcon);
  75. button_->SetLabel(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH));
  76. button_->SetSubLabel(l10n_util::GetStringUTF16(
  77. IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_SHORT));
  78. SetTooltipState(l10n_util::GetStringUTF16(
  79. IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_TOOLTIP));
  80. return;
  81. }
  82. BluetoothDeviceList connected_devices;
  83. for (auto& device :
  84. Shell::Get()->tray_bluetooth_helper()->GetAvailableBluetoothDevices()) {
  85. if (device->connection_state ==
  86. BluetoothDeviceInfo::ConnectionState::kConnected) {
  87. connected_devices.push_back(device->Clone());
  88. }
  89. }
  90. if (connected_devices.size() > 1) {
  91. const size_t device_count = connected_devices.size();
  92. button_->SetVectorIcon(kUnifiedMenuBluetoothConnectedLegacyIcon);
  93. button_->SetLabel(l10n_util::GetStringUTF16(
  94. IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_LABEL_LEGACY));
  95. button_->SetSubLabel(base::FormatNumber(device_count));
  96. SetTooltipState(l10n_util::GetPluralStringFUTF16(
  97. IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_TOOLTIP_LEGACY,
  98. device_count));
  99. } else if (connected_devices.size() == 1) {
  100. const device::mojom::BluetoothDeviceInfoPtr& device =
  101. connected_devices.back();
  102. const std::u16string device_name =
  103. device::GetBluetoothDeviceNameForDisplay(device);
  104. button_->SetVectorIcon(kUnifiedMenuBluetoothConnectedLegacyIcon);
  105. button_->SetLabel(device_name);
  106. if (device->battery_info) {
  107. button_->SetSubLabel(l10n_util::GetStringFUTF16(
  108. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
  109. base::NumberToString16(device->battery_info->battery_percentage)));
  110. } else {
  111. button_->SetSubLabel(l10n_util::GetStringUTF16(
  112. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_LABEL));
  113. }
  114. SetTooltipState(l10n_util::GetStringFUTF16(
  115. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_TOOLTIP_LEGACY,
  116. device_name));
  117. } else {
  118. button_->SetVectorIcon(kUnifiedMenuBluetoothLegacyIcon);
  119. button_->SetLabel(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH));
  120. button_->SetSubLabel(
  121. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_SHORT));
  122. SetTooltipState(l10n_util::GetStringUTF16(
  123. IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_TOOLTIP));
  124. }
  125. }
  126. void BluetoothFeaturePodControllerLegacy::SetTooltipState(
  127. const std::u16string& tooltip_state) {
  128. if (button_->GetEnabled()) {
  129. button_->SetIconTooltip(l10n_util::GetStringFUTF16(
  130. IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP, tooltip_state));
  131. button_->SetLabelTooltip(l10n_util::GetStringFUTF16(
  132. IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP, tooltip_state));
  133. } else {
  134. // Do not show "Toggle" text in tooltip when the button is disabled (e.g.
  135. // when the screen is locked or for secondary users).
  136. button_->SetIconTooltip(tooltip_state);
  137. button_->SetLabelTooltip(tooltip_state);
  138. }
  139. }
  140. void BluetoothFeaturePodControllerLegacy::OnBluetoothSystemStateChanged() {
  141. UpdateButton();
  142. }
  143. void BluetoothFeaturePodControllerLegacy::OnBluetoothScanStateChanged() {
  144. UpdateButton();
  145. }
  146. void BluetoothFeaturePodControllerLegacy::OnBluetoothDeviceListChanged() {
  147. UpdateButton();
  148. }
  149. } // namespace ash