File: bluetooth_feature_pod_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (325 lines) | stat: -rw-r--r-- 12,208 bytes parent folder | download | duplicates (6)
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
// 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_feature_pod_controller.h"

#include <string>

#include "ash/ash_element_identifiers.h"
#include "ash/constants/quick_settings_catalogs.h"
#include "ash/public/cpp/bluetooth_config_service.h"
#include "ash/public/cpp/hats_bluetooth_revamp_trigger.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/bluetooth/bluetooth_state_cache.h"
#include "ash/system/bluetooth/hid_preserving_controller/hid_preserving_bluetooth_state_service.h"
#include "ash/system/unified/feature_tile.h"
#include "ash/system/unified/quick_settings_metrics_util.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/i18n/number_formatting.h"
#include "base/strings/string_number_conversions.h"
#include "chromeos/ash/services/bluetooth_config/public/cpp/cros_bluetooth_config_util.h"
#include "chromeos/constants/chromeos_features.h"
#include "mojo/public/cpp/bindings/clone_traits.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/view_class_properties.h"

namespace ash {
namespace {

using bluetooth_config::GetPairedDeviceName;
using bluetooth_config::mojom::BluetoothModificationState;
using bluetooth_config::mojom::BluetoothSystemPropertiesPtr;
using bluetooth_config::mojom::BluetoothSystemState;
using bluetooth_config::mojom::DeviceConnectionState;

BluetoothSystemState GetInitialSystemState() {
  // Synchronously query the initial state so the feature tile doesn't flash
  // with the wrong state. See b/266996235
  return Shell::Get()->bluetooth_state_cache()->system_state();
}

}  // namespace

BluetoothFeaturePodController::BluetoothFeaturePodController(
    UnifiedSystemTrayController* tray_controller)
    : system_state_(GetInitialSystemState()),
      tray_controller_(tray_controller) {
  GetBluetoothConfigService(
      remote_cros_bluetooth_config_.BindNewPipeAndPassReceiver());
  remote_cros_bluetooth_config_->ObserveSystemProperties(
      cros_system_properties_observer_receiver_.BindNewPipeAndPassRemote());

  GetHidPreservingBluetoothStateControllerService(
      remote_hid_preserving_bluetooth_.BindNewPipeAndPassReceiver());
}

BluetoothFeaturePodController::~BluetoothFeaturePodController() = default;

std::unique_ptr<FeatureTile> BluetoothFeaturePodController::CreateTile(
    bool compact) {
  auto tile = std::make_unique<FeatureTile>(
      base::BindRepeating(&BluetoothFeaturePodController::OnLabelPressed,
                          weak_factory_.GetWeakPtr()));
  tile_ = tile.get();
  tile_->SetIconClickable(true);
  tile_->SetIconClickCallback(
      base::BindRepeating(&BluetoothFeaturePodController::OnIconPressed,
                          weak_factory_.GetWeakPtr()));
  tile_->icon_button()->SetProperty(views::kElementIdentifierKey,
                                    kBluetoothFeatureTileToggleElementId);
  tile_->CreateDecorativeDrillInArrow();
  tile_->drill_in_arrow()->SetProperty(
      views::kElementIdentifierKey, kBluetoothFeatureTileDrillInArrowElementId);
  // UpdateTileStateIfExists() will update visibility.
  tile_->SetVisible(false);
  UpdateTileStateIfExists();
  return tile;
}

QsFeatureCatalogName BluetoothFeaturePodController::GetCatalogName() {
  return QsFeatureCatalogName::kBluetooth;
}

bool BluetoothFeaturePodController::IsBluetoothAvailable() const {
  return system_state_ != BluetoothSystemState::kUnavailable;
}

void BluetoothFeaturePodController::OnIconPressed() {
  if (!IsButtonEnabled()) {
    return;
  }

  const bool is_toggled = IsButtonToggled();
  remote_hid_preserving_bluetooth_->TryToSetBluetoothEnabledState(
      !is_toggled, mojom::HidWarningDialogSource::kQuickSettings);

  TrackToggleUMA(/*target_toggle_state=*/!is_toggled);

  if (auto* hats_bluetooth_revamp_trigger = HatsBluetoothRevampTrigger::Get()) {
    hats_bluetooth_revamp_trigger->TryToShowSurvey();
  }
}

void BluetoothFeaturePodController::OnLabelPressed() {
  if (!IsButtonEnabled()) {
    return;
  }

  TrackDiveInUMA();

  if (auto* hats_bluetooth_revamp_trigger = HatsBluetoothRevampTrigger::Get()) {
    hats_bluetooth_revamp_trigger->TryToShowSurvey();
  }
  tray_controller_->ShowBluetoothDetailedView();
}

BluetoothFeaturePodController::BluetoothDeviceNameAndBatteryInfo::
    BluetoothDeviceNameAndBatteryInfo(
        const std::u16string& device_name,
        bluetooth_config::mojom::DeviceBatteryInfoPtr battery_info)
    : device_name(device_name), battery_info(std::move(battery_info)) {}

BluetoothFeaturePodController::BluetoothDeviceNameAndBatteryInfo::
    ~BluetoothDeviceNameAndBatteryInfo() = default;

bool BluetoothFeaturePodController::DoesFirstConnectedDeviceHaveBatteryInfo()
    const {
  return first_connected_device_.has_value() &&
         first_connected_device_.value().battery_info &&
         (first_connected_device_.value().battery_info->default_properties ||
          first_connected_device_.value().battery_info->left_bud_info ||
          first_connected_device_.value().battery_info->right_bud_info ||
          first_connected_device_.value().battery_info->case_info);
}

const gfx::VectorIcon& BluetoothFeaturePodController::ComputeButtonIcon()
    const {
  if (!IsButtonToggled()) {
    return kUnifiedMenuBluetoothDisabledIcon;
  }

  if (first_connected_device_.has_value())
    return kUnifiedMenuBluetoothConnectedIcon;

  return kUnifiedMenuBluetoothIcon;
}

std::u16string BluetoothFeaturePodController::ComputeButtonLabel() const {
  if (IsButtonToggled() && first_connected_device_.has_value() &&
      connected_device_count_ == 1) {
    return first_connected_device_.value().device_name;
  }
  return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH);
}

int BluetoothFeaturePodController::
    GetFirstConnectedDeviceBatteryLevelForDisplay() const {
  // If there are any multiple battery details, we should prioritize showing
  // them over the default battery in order to match the Quick Settings
  // Bluetooth sub-page battery details shown. Android only shows the left bud
  // if there are multiple batteries, so we match that here, and if it doesn't
  // exist, we prioritize the right bud battery, then the case battery, if
  // they exist over the default battery in order to match any detailed
  // battery shown on the sub-page.
  if (first_connected_device_.value().battery_info->left_bud_info)
    return first_connected_device_.value()
        .battery_info->left_bud_info->battery_percentage;

  if (first_connected_device_.value().battery_info->right_bud_info)
    return first_connected_device_.value()
        .battery_info->right_bud_info->battery_percentage;

  if (first_connected_device_.value().battery_info->case_info)
    return first_connected_device_.value()
        .battery_info->case_info->battery_percentage;

  return first_connected_device_.value()
      .battery_info->default_properties->battery_percentage;
}

std::u16string BluetoothFeaturePodController::ComputeButtonSubLabel() const {
  if (chromeos::features::IsBluetoothWifiQSPodRefreshEnabled() &&
      !IsBluetoothAvailable()) {
    return l10n_util::GetStringUTF16(
        IDS_ASH_STATUS_TRAY_BLUETOOTH_UNAVAILABLE_SHORT);
  }
  if (!IsButtonToggled()) {
    return l10n_util::GetStringUTF16(
        IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_SHORT);
  }
  if (!first_connected_device_.has_value()) {
    return l10n_util::GetStringUTF16(
        IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_SHORT);
  }
  if (connected_device_count_ == 1) {
    if (DoesFirstConnectedDeviceHaveBatteryInfo()) {
      return l10n_util::GetStringFUTF16(
          IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
          base::NumberToString16(
              GetFirstConnectedDeviceBatteryLevelForDisplay()));
    }
    return l10n_util::GetStringUTF16(
        IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_LABEL);
  }

  return l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_LABEL,
      base::FormatNumber(connected_device_count_));
}

std::u16string BluetoothFeaturePodController::ComputeTooltip() const {
  if (chromeos::features::IsBluetoothWifiQSPodRefreshEnabled() &&
      !IsBluetoothAvailable()) {
    return l10n_util::GetStringUTF16(
        IDS_ASH_STATUS_TRAY_BLUETOOTH_UNAVAILABLE_TOOLTIP);
  }
  if (!first_connected_device_.has_value())
    return l10n_util::GetStringUTF16(
        IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_TOOLTIP);

  if (connected_device_count_ == 1) {
    if (DoesFirstConnectedDeviceHaveBatteryInfo()) {
      return l10n_util::GetStringFUTF16(
          IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_WITH_BATTERY_TOOLTIP,
          first_connected_device_.value().device_name,
          base::NumberToString16(
              GetFirstConnectedDeviceBatteryLevelForDisplay()));
    }
    return l10n_util::GetStringFUTF16(
        IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_TOOLTIP,
        first_connected_device_.value().device_name);
  }

  return l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_TOOLTIP,
      base::FormatNumber(connected_device_count_));
}

bool BluetoothFeaturePodController::IsButtonEnabled() const {
  return tile_->GetEnabled();
}

bool BluetoothFeaturePodController::IsButtonToggled() const {
  return tile_->IsToggled();
}


void BluetoothFeaturePodController::UpdateTileStateIfExists() {
  if (!tile_) {
    return;
  }
  if (!chromeos::features::IsBluetoothWifiQSPodRefreshEnabled() &&
      !IsBluetoothAvailable()) {
    tile_->SetVisible(false);
    tile_->SetEnabled(false);
    return;
  }

  // If the button's visibility changes from invisible to visible, log its
  // visibility.
  if (!tile_->GetVisible()) {
    TrackVisibilityUMA();
  }
  tile_->SetToggled(
      bluetooth_config::IsBluetoothEnabledOrEnabling(system_state_));
  tile_->SetEnabled(modification_state_ ==
                    BluetoothModificationState::kCanModifyBluetooth);
  tile_->SetVisible(true);
  tile_->SetVectorIcon(ComputeButtonIcon());
  tile_->SetLabel(ComputeButtonLabel());
  tile_->SetSubLabel(ComputeButtonSubLabel());

  if (!tile_->IsToggled()) {
    if (chromeos::features::IsBluetoothWifiQSPodRefreshEnabled() &&
        !IsBluetoothAvailable()) {
      tile_->SetTooltipText(l10n_util::GetStringUTF16(
          IDS_ASH_STATUS_TRAY_BLUETOOTH_UNAVAILABLE_TOOLTIP));
      tile_->icon_button()->SetEnabled(false);
      return;
    }
    std::u16string tooltip = l10n_util::GetStringFUTF16(
        IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
        l10n_util::GetStringUTF16(
            IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_TOOLTIP));
    tile_->SetIconButtonTooltipText(tooltip);
    tile_->SetTooltipText(tooltip);
    return;
  }
  std::u16string tooltip_core = ComputeTooltip();
  tile_->SetIconButtonTooltipText(l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP, tooltip_core));
  tile_->SetTooltipText(l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP, tooltip_core));
}

void BluetoothFeaturePodController::OnPropertiesUpdated(
    BluetoothSystemPropertiesPtr properties) {
  connected_device_count_ = 0;
  first_connected_device_.reset();

  // Counts the number of connected devices and caches the name and battery
  // information of the first connected device found.
  for (const auto& paired_device : properties->paired_devices) {
    if (paired_device->device_properties->connection_state !=
        DeviceConnectionState::kConnected) {
      continue;
    }
    ++connected_device_count_;
    if (first_connected_device_.has_value())
      continue;
    first_connected_device_.emplace(
        GetPairedDeviceName(paired_device),
        mojo::Clone(paired_device->device_properties->battery_info));
  }
  modification_state_ = properties->modification_state;
  system_state_ = properties->system_state;
  UpdateTileStateIfExists();
}

}  // namespace ash