1 // Copyright (c) 2012 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.
5 #include "ash/system/bluetooth/tray_bluetooth.h"
7 #include "ash/session/session_state_delegate.h"
9 #include "ash/system/tray/fixed_sized_scroll_view.h"
10 #include "ash/system/tray/hover_highlight_view.h"
11 #include "ash/system/tray/system_tray.h"
12 #include "ash/system/tray/system_tray_delegate.h"
13 #include "ash/system/tray/system_tray_notifier.h"
14 #include "ash/system/tray/throbber_view.h"
15 #include "ash/system/tray/tray_constants.h"
16 #include "ash/system/tray/tray_details_view.h"
17 #include "ash/system/tray/tray_item_more.h"
18 #include "ash/system/tray/tray_popup_header_button.h"
19 #include "grit/ash_resources.h"
20 #include "grit/ash_strings.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/gfx/image/image.h"
24 #include "ui/views/controls/image_view.h"
25 #include "ui/views/controls/label.h"
26 #include "ui/views/layout/box_layout.h"
32 // Updates bluetooth device |device| in the |list|. If it is new, append to the
33 // end of the |list|; otherwise, keep it at the same place, but update the data
34 // with new device info provided by |device|.
35 void UpdateBluetoothDeviceListHelper(BluetoothDeviceList* list,
36 const BluetoothDeviceInfo& device) {
37 for (BluetoothDeviceList::iterator it = list->begin(); it != list->end();
39 if ((*it).address == device.address) {
45 list->push_back(device);
48 // Removes the obsolete BluetoothDevices from |list|, if they are not in the
50 void RemoveObsoleteBluetoothDevicesFromList(
51 BluetoothDeviceList* list,
52 const std::set<std::string>& new_list) {
53 for (BluetoothDeviceList::iterator it = list->begin(); it != list->end();
55 if (new_list.find((*it).address) == new_list.end()) {
57 if (it == list->end())
65 class BluetoothDefaultView : public TrayItemMore {
67 BluetoothDefaultView(SystemTrayItem* owner, bool show_more)
68 : TrayItemMore(owner, show_more) {
69 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
70 SetImage(bundle.GetImageNamed(IDR_AURA_UBER_TRAY_BLUETOOTH).ToImageSkia());
74 ~BluetoothDefaultView() override {}
77 ash::SystemTrayDelegate* delegate =
78 ash::Shell::GetInstance()->system_tray_delegate();
79 if (delegate->GetBluetoothAvailable()) {
80 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
81 const base::string16 label =
82 rb.GetLocalizedString(delegate->GetBluetoothEnabled() ?
83 IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED :
84 IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED);
86 SetAccessibleName(label);
94 DISALLOW_COPY_AND_ASSIGN(BluetoothDefaultView);
97 class BluetoothDetailedView : public TrayDetailsView,
98 public ViewClickListener,
99 public views::ButtonListener {
101 BluetoothDetailedView(SystemTrayItem* owner, user::LoginStatus login)
102 : TrayDetailsView(owner),
104 manage_devices_(NULL),
105 toggle_bluetooth_(NULL),
106 enable_bluetooth_(NULL) {
110 ~BluetoothDetailedView() override {
111 // Stop discovering bluetooth devices when exiting BT detailed view.
112 BluetoothStopDiscovering();
116 BluetoothStartDiscovering();
117 UpdateBluetoothDeviceList();
120 UpdateDeviceScrollList();
127 CreateScrollableList();
128 AppendSettingsEntries();
132 void BluetoothStartDiscovering() {
133 ash::SystemTrayDelegate* delegate =
134 ash::Shell::GetInstance()->system_tray_delegate();
135 bool bluetooth_enabled = delegate->GetBluetoothEnabled();
136 bool bluetooth_discovering = delegate->GetBluetoothDiscovering();
137 if (bluetooth_discovering) {
142 if (bluetooth_enabled) {
143 delegate->BluetoothStartDiscovering();
147 void BluetoothStopDiscovering() {
148 ash::SystemTrayDelegate* delegate =
149 ash::Shell::GetInstance()->system_tray_delegate();
150 if (delegate && delegate->GetBluetoothDiscovering()) {
151 delegate->BluetoothStopDiscovering();
156 void UpdateBluetoothDeviceList() {
157 std::set<std::string> new_connecting_devices;
158 std::set<std::string> new_connected_devices;
159 std::set<std::string> new_paired_not_connected_devices;
160 std::set<std::string> new_discovered_not_paired_devices;
162 BluetoothDeviceList list;
163 Shell::GetInstance()->system_tray_delegate()->
164 GetAvailableBluetoothDevices(&list);
165 for (size_t i = 0; i < list.size(); ++i) {
166 if (list[i].connecting) {
167 list[i].display_name = l10n_util::GetStringFUTF16(
168 IDS_ASH_STATUS_TRAY_BLUETOOTH_CONNECTING, list[i].display_name);
169 new_connecting_devices.insert(list[i].address);
170 UpdateBluetoothDeviceListHelper(&connecting_devices_, list[i]);
171 } else if (list[i].connected && list[i].paired) {
172 new_connected_devices.insert(list[i].address);
173 UpdateBluetoothDeviceListHelper(&connected_devices_, list[i]);
174 } else if (list[i].paired) {
175 new_paired_not_connected_devices.insert(list[i].address);
176 UpdateBluetoothDeviceListHelper(
177 &paired_not_connected_devices_, list[i]);
179 new_discovered_not_paired_devices.insert(list[i].address);
180 UpdateBluetoothDeviceListHelper(
181 &discovered_not_paired_devices_, list[i]);
184 RemoveObsoleteBluetoothDevicesFromList(&connecting_devices_,
185 new_connecting_devices);
186 RemoveObsoleteBluetoothDevicesFromList(&connected_devices_,
187 new_connected_devices);
188 RemoveObsoleteBluetoothDevicesFromList(&paired_not_connected_devices_,
189 new_paired_not_connected_devices);
190 RemoveObsoleteBluetoothDevicesFromList(&discovered_not_paired_devices_,
191 new_discovered_not_paired_devices);
194 void AppendHeaderEntry() {
195 CreateSpecialRow(IDS_ASH_STATUS_TRAY_BLUETOOTH, this);
197 if (login_ == user::LOGGED_IN_LOCKED)
200 throbber_ = new ThrobberView;
201 throbber_->SetTooltipText(
202 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_DISCOVERING));
203 footer()->AddThrobber(throbber_);
205 // Do not allow toggling bluetooth in the lock screen.
206 ash::SystemTrayDelegate* delegate =
207 ash::Shell::GetInstance()->system_tray_delegate();
208 toggle_bluetooth_ = new TrayPopupHeaderButton(this,
209 IDR_AURA_UBER_TRAY_BLUETOOTH_ENABLED,
210 IDR_AURA_UBER_TRAY_BLUETOOTH_DISABLED,
211 IDR_AURA_UBER_TRAY_BLUETOOTH_ENABLED_HOVER,
212 IDR_AURA_UBER_TRAY_BLUETOOTH_DISABLED_HOVER,
213 IDS_ASH_STATUS_TRAY_BLUETOOTH);
214 toggle_bluetooth_->SetToggled(!delegate->GetBluetoothEnabled());
215 toggle_bluetooth_->SetTooltipText(
216 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_DISABLE_BLUETOOTH));
217 toggle_bluetooth_->SetToggledTooltipText(
218 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ENABLE_BLUETOOTH));
219 footer()->AddButton(toggle_bluetooth_);
222 void UpdateHeaderEntry() {
223 if (toggle_bluetooth_) {
224 toggle_bluetooth_->SetToggled(
225 !ash::Shell::GetInstance()->system_tray_delegate()->
226 GetBluetoothEnabled());
230 void UpdateDeviceScrollList() {
232 scroll_content()->RemoveAllChildViews(true);
233 enable_bluetooth_ = NULL;
235 ash::SystemTrayDelegate* delegate =
236 ash::Shell::GetInstance()->system_tray_delegate();
237 bool bluetooth_enabled = delegate->GetBluetoothEnabled();
238 bool blueooth_available = delegate->GetBluetoothAvailable();
239 if (blueooth_available && !bluetooth_enabled &&
243 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ENABLE_BLUETOOTH),
244 gfx::Font::NORMAL, false, true);
247 AppendSameTypeDevicesToScrollList(
248 connected_devices_, true, true, bluetooth_enabled);
249 AppendSameTypeDevicesToScrollList(
250 connecting_devices_, true, false, bluetooth_enabled);
251 AppendSameTypeDevicesToScrollList(
252 paired_not_connected_devices_, false, false, bluetooth_enabled);
253 if (discovered_not_paired_devices_.size() > 0)
254 AddScrollSeparator();
255 AppendSameTypeDevicesToScrollList(
256 discovered_not_paired_devices_, false, false, bluetooth_enabled);
258 // Show user Bluetooth state if there is no bluetooth devices in list.
259 if (device_map_.size() == 0) {
260 if (blueooth_available && bluetooth_enabled) {
262 l10n_util::GetStringUTF16(
263 IDS_ASH_STATUS_TRAY_BLUETOOTH_DISCOVERING),
264 gfx::Font::NORMAL, false, true);
268 scroll_content()->SizeToPreferredSize();
269 static_cast<views::View*>(scroller())->Layout();
272 void AppendSameTypeDevicesToScrollList(const BluetoothDeviceList& list,
276 for (size_t i = 0; i < list.size(); ++i) {
277 HoverHighlightView* container = AddScrollListItem(
278 list[i].display_name,
279 bold? gfx::Font::BOLD : gfx::Font::NORMAL,
281 device_map_[container] = list[i].address;
285 HoverHighlightView* AddScrollListItem(const base::string16& text,
286 gfx::Font::FontStyle style,
289 HoverHighlightView* container = new HoverHighlightView(this);
290 views::Label* label = container->AddCheckableLabel(text, style, checked);
291 label->SetEnabled(enabled);
292 scroll_content()->AddChildView(container);
296 // Add settings entries.
297 void AppendSettingsEntries() {
298 if (!ash::Shell::GetInstance()->
299 system_tray_delegate()->ShouldShowSettings()) {
303 // Add bluetooth device requires a browser window, hide it for non logged in
305 bool userAddingRunning = ash::Shell::GetInstance()
306 ->session_state_delegate()
307 ->IsInSecondaryLoginScreen();
309 if (login_ == user::LOGGED_IN_NONE || login_ == user::LOGGED_IN_LOCKED ||
313 ash::SystemTrayDelegate* delegate =
314 ash::Shell::GetInstance()->system_tray_delegate();
315 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
316 HoverHighlightView* container = new HoverHighlightView(this);
318 rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BLUETOOTH_MANAGE_DEVICES),
321 container->SetEnabled(delegate->GetBluetoothAvailable());
322 AddChildView(container);
323 manage_devices_ = container;
326 // Returns true if the device with |device_id| is found in |device_list|,
327 // and the display_name of the device will be returned in |display_name| if
329 bool FoundDevice(const std::string& device_id,
330 const BluetoothDeviceList& device_list,
331 base::string16* display_name) {
332 for (size_t i = 0; i < device_list.size(); ++i) {
333 if (device_list[i].address == device_id) {
335 *display_name = device_list[i].display_name;
342 // Updates UI of the clicked bluetooth device to show it is being connected
343 // or disconnected if such an operation is going to be performed underway.
344 void UpdateClickedDevice(std::string device_id, views::View* item_container) {
345 base::string16 display_name;
346 if (FoundDevice(device_id, paired_not_connected_devices_,
348 display_name = l10n_util::GetStringFUTF16(
349 IDS_ASH_STATUS_TRAY_BLUETOOTH_CONNECTING, display_name);
351 item_container->RemoveAllChildViews(true);
352 static_cast<HoverHighlightView*>(item_container)->
353 AddCheckableLabel(display_name, gfx::Font::BOLD, false);
354 scroll_content()->SizeToPreferredSize();
355 static_cast<views::View*>(scroller())->Layout();
359 // Overridden from ViewClickListener.
360 void OnViewClicked(views::View* sender) override {
361 ash::SystemTrayDelegate* delegate =
362 ash::Shell::GetInstance()->system_tray_delegate();
363 if (sender == footer()->content()) {
364 TransitionToDefaultView();
365 } else if (sender == manage_devices_) {
366 delegate->ManageBluetoothDevices();
367 } else if (sender == enable_bluetooth_) {
368 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
369 delegate->GetBluetoothEnabled() ?
370 ash::UMA_STATUS_AREA_BLUETOOTH_DISABLED :
371 ash::UMA_STATUS_AREA_BLUETOOTH_ENABLED);
372 delegate->ToggleBluetooth();
374 if (!delegate->GetBluetoothEnabled())
376 std::map<views::View*, std::string>::iterator find;
377 find = device_map_.find(sender);
378 if (find == device_map_.end())
380 std::string device_id = find->second;
381 if (FoundDevice(device_id, connecting_devices_, NULL))
383 UpdateClickedDevice(device_id, sender);
384 delegate->ConnectToBluetoothDevice(device_id);
388 // Overridden from ButtonListener.
389 void ButtonPressed(views::Button* sender, const ui::Event& event) override {
390 ash::SystemTrayDelegate* delegate =
391 ash::Shell::GetInstance()->system_tray_delegate();
392 if (sender == toggle_bluetooth_)
393 delegate->ToggleBluetooth();
398 user::LoginStatus login_;
400 std::map<views::View*, std::string> device_map_;
401 views::View* manage_devices_;
402 ThrobberView* throbber_;
403 TrayPopupHeaderButton* toggle_bluetooth_;
404 HoverHighlightView* enable_bluetooth_;
405 BluetoothDeviceList connected_devices_;
406 BluetoothDeviceList connecting_devices_;
407 BluetoothDeviceList paired_not_connected_devices_;
408 BluetoothDeviceList discovered_not_paired_devices_;
410 DISALLOW_COPY_AND_ASSIGN(BluetoothDetailedView);
415 TrayBluetooth::TrayBluetooth(SystemTray* system_tray)
416 : SystemTrayItem(system_tray),
419 Shell::GetInstance()->system_tray_notifier()->AddBluetoothObserver(this);
422 TrayBluetooth::~TrayBluetooth() {
423 Shell::GetInstance()->system_tray_notifier()->RemoveBluetoothObserver(this);
426 views::View* TrayBluetooth::CreateTrayView(user::LoginStatus status) {
430 views::View* TrayBluetooth::CreateDefaultView(user::LoginStatus status) {
431 CHECK(default_ == NULL);
432 default_ = new tray::BluetoothDefaultView(
433 this, status != user::LOGGED_IN_LOCKED);
437 views::View* TrayBluetooth::CreateDetailedView(user::LoginStatus status) {
438 if (!Shell::GetInstance()->system_tray_delegate()->GetBluetoothAvailable())
440 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
441 ash::UMA_STATUS_AREA_DETAILED_BLUETOOTH_VIEW);
442 CHECK(detailed_ == NULL);
443 detailed_ = new tray::BluetoothDetailedView(this, status);
448 void TrayBluetooth::DestroyTrayView() {
451 void TrayBluetooth::DestroyDefaultView() {
455 void TrayBluetooth::DestroyDetailedView() {
459 void TrayBluetooth::UpdateAfterLoginStatusChange(user::LoginStatus status) {
462 void TrayBluetooth::OnBluetoothRefresh() {
464 default_->UpdateLabel();
469 void TrayBluetooth::OnBluetoothDiscoveringChanged() {