Commit f1f10378 authored by Giovanni Ortuño Urquidi's avatar Giovanni Ortuño Urquidi Committed by Commit Bot

bluetooth: Refactor UnifiedBluetoothDetailedViewController

Changes UnifiedBluetoothDetailedViewController to update the device list
every 500ms while a scan is active.

Also adds a new observer method specifically for when Bluetooth State
changes and changes UnifiedBluetoothDetailedViewController to use this
method to update the list of devices as well.

Bug: 882346
Change-Id: I13ef2555e6c4c451213afb5d405f707d5f4f19a7
Reviewed-on: https://chromium-review.googlesource.com/c/1314069Reviewed-by: default avatarTetsui Ohkubo <tetsui@chromium.org>
Commit-Queue: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605951}
parent 3c177537
...@@ -131,11 +131,15 @@ void BluetoothFeaturePodController::SetTooltipState( ...@@ -131,11 +131,15 @@ void BluetoothFeaturePodController::SetTooltipState(
IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP, tooltip_state)); IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP, tooltip_state));
} }
void BluetoothFeaturePodController::OnBluetoothRefresh() { void BluetoothFeaturePodController::OnBluetoothSystemStateChanged() {
UpdateButton(); UpdateButton();
} }
void BluetoothFeaturePodController::OnBluetoothDiscoveringChanged() { void BluetoothFeaturePodController::OnBluetoothScanStateChanged() {
UpdateButton();
}
void BluetoothFeaturePodController::OnBluetoothDeviceListChanged() {
UpdateButton(); UpdateButton();
} }
......
...@@ -32,8 +32,9 @@ class BluetoothFeaturePodController : public FeaturePodControllerBase, ...@@ -32,8 +32,9 @@ class BluetoothFeaturePodController : public FeaturePodControllerBase,
void SetTooltipState(const base::string16& tooltip_state); void SetTooltipState(const base::string16& tooltip_state);
// BluetoothObserver: // BluetoothObserver:
void OnBluetoothRefresh() override; void OnBluetoothSystemStateChanged() override;
void OnBluetoothDiscoveringChanged() override; void OnBluetoothScanStateChanged() override;
void OnBluetoothDeviceListChanged() override;
// Unowned. // Unowned.
UnifiedSystemTrayController* const tray_controller_; UnifiedSystemTrayController* const tray_controller_;
......
...@@ -11,8 +11,14 @@ class BluetoothObserver { ...@@ -11,8 +11,14 @@ class BluetoothObserver {
public: public:
virtual ~BluetoothObserver() {} virtual ~BluetoothObserver() {}
virtual void OnBluetoothRefresh() = 0; // Called when the state of Bluetooth in the system changes.
virtual void OnBluetoothDiscoveringChanged() = 0; virtual void OnBluetoothSystemStateChanged() {}
// Called when a Bluetooth scan has started or stopped.
virtual void OnBluetoothScanStateChanged() {}
// Called when a device was added, removed, or changed.
virtual void OnBluetoothDeviceListChanged() {}
}; };
} // namespace ash } // namespace ash
......
...@@ -84,13 +84,13 @@ bool TrayBluetoothHelperExperimental::HasBluetoothDiscoverySession() { ...@@ -84,13 +84,13 @@ bool TrayBluetoothHelperExperimental::HasBluetoothDiscoverySession() {
void TrayBluetoothHelperExperimental::OnStateChanged( void TrayBluetoothHelperExperimental::OnStateChanged(
device::mojom::BluetoothSystem::State state) { device::mojom::BluetoothSystem::State state) {
cached_state_ = state; cached_state_ = state;
Shell::Get()->system_tray_notifier()->NotifyRefreshBluetooth(); Shell::Get()->system_tray_notifier()->NotifyBluetoothSystemStateChanged();
} }
void TrayBluetoothHelperExperimental::OnScanStateChanged( void TrayBluetoothHelperExperimental::OnScanStateChanged(
device::mojom::BluetoothSystem::ScanState state) { device::mojom::BluetoothSystem::ScanState state) {
cached_scan_state_ = state; cached_scan_state_ = state;
Shell::Get()->system_tray_notifier()->NotifyBluetoothDiscoveringChanged(); Shell::Get()->system_tray_notifier()->NotifyBluetoothScanStateChanged();
} }
} // namespace ash } // namespace ash
...@@ -165,34 +165,34 @@ bool TrayBluetoothHelperLegacy::HasBluetoothDiscoverySession() { ...@@ -165,34 +165,34 @@ bool TrayBluetoothHelperLegacy::HasBluetoothDiscoverySession() {
void TrayBluetoothHelperLegacy::AdapterPresentChanged( void TrayBluetoothHelperLegacy::AdapterPresentChanged(
device::BluetoothAdapter* adapter, device::BluetoothAdapter* adapter,
bool present) { bool present) {
GetSystemTrayNotifier()->NotifyRefreshBluetooth(); GetSystemTrayNotifier()->NotifyBluetoothSystemStateChanged();
} }
void TrayBluetoothHelperLegacy::AdapterPoweredChanged( void TrayBluetoothHelperLegacy::AdapterPoweredChanged(
device::BluetoothAdapter* adapter, device::BluetoothAdapter* adapter,
bool powered) { bool powered) {
GetSystemTrayNotifier()->NotifyRefreshBluetooth(); GetSystemTrayNotifier()->NotifyBluetoothSystemStateChanged();
} }
void TrayBluetoothHelperLegacy::AdapterDiscoveringChanged( void TrayBluetoothHelperLegacy::AdapterDiscoveringChanged(
device::BluetoothAdapter* adapter, device::BluetoothAdapter* adapter,
bool discovering) { bool discovering) {
GetSystemTrayNotifier()->NotifyBluetoothDiscoveringChanged(); GetSystemTrayNotifier()->NotifyBluetoothScanStateChanged();
} }
void TrayBluetoothHelperLegacy::DeviceAdded(device::BluetoothAdapter* adapter, void TrayBluetoothHelperLegacy::DeviceAdded(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) { device::BluetoothDevice* device) {
GetSystemTrayNotifier()->NotifyRefreshBluetooth(); GetSystemTrayNotifier()->NotifyBluetoothDeviceListChanged();
} }
void TrayBluetoothHelperLegacy::DeviceChanged(device::BluetoothAdapter* adapter, void TrayBluetoothHelperLegacy::DeviceChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) { device::BluetoothDevice* device) {
GetSystemTrayNotifier()->NotifyRefreshBluetooth(); GetSystemTrayNotifier()->NotifyBluetoothDeviceListChanged();
} }
void TrayBluetoothHelperLegacy::DeviceRemoved(device::BluetoothAdapter* adapter, void TrayBluetoothHelperLegacy::DeviceRemoved(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) { device::BluetoothDevice* device) {
GetSystemTrayNotifier()->NotifyRefreshBluetooth(); GetSystemTrayNotifier()->NotifyBluetoothDeviceListChanged();
} }
void TrayBluetoothHelperLegacy::OnStartDiscoverySession( void TrayBluetoothHelperLegacy::OnStartDiscoverySession(
...@@ -204,7 +204,7 @@ void TrayBluetoothHelperLegacy::OnStartDiscoverySession( ...@@ -204,7 +204,7 @@ void TrayBluetoothHelperLegacy::OnStartDiscoverySession(
return; return;
VLOG(1) << "Claiming new Bluetooth device discovery session."; VLOG(1) << "Claiming new Bluetooth device discovery session.";
discovery_session_ = std::move(discovery_session); discovery_session_ = std::move(discovery_session);
GetSystemTrayNotifier()->NotifyBluetoothDiscoveringChanged(); GetSystemTrayNotifier()->NotifyBluetoothScanStateChanged();
} }
} // namespace ash } // namespace ash
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include "ash/system/bluetooth/unified_bluetooth_detailed_view_controller.h" #include "ash/system/bluetooth/unified_bluetooth_detailed_view_controller.h"
#include <set>
#include <string>
#include "ash/session/session_controller.h" #include "ash/session/session_controller.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/system/bluetooth/bluetooth_detailed_view.h" #include "ash/system/bluetooth/bluetooth_detailed_view.h"
...@@ -63,7 +66,6 @@ UnifiedBluetoothDetailedViewController:: ...@@ -63,7 +66,6 @@ UnifiedBluetoothDetailedViewController::
TrayBluetoothHelper* helper = Shell::Get()->tray_bluetooth_helper(); TrayBluetoothHelper* helper = Shell::Get()->tray_bluetooth_helper();
if (helper && helper->HasBluetoothDiscoverySession()) { if (helper && helper->HasBluetoothDiscoverySession()) {
helper->StopBluetoothDiscovering(); helper->StopBluetoothDiscovering();
view_->HideLoadingIndicator();
} }
} }
...@@ -72,59 +74,69 @@ views::View* UnifiedBluetoothDetailedViewController::CreateView() { ...@@ -72,59 +74,69 @@ views::View* UnifiedBluetoothDetailedViewController::CreateView() {
view_ = new tray::BluetoothDetailedView( view_ = new tray::BluetoothDetailedView(
detailed_view_delegate_.get(), detailed_view_delegate_.get(),
Shell::Get()->session_controller()->login_status()); Shell::Get()->session_controller()->login_status());
Update(); OnBluetoothSystemStateChanged();
return view_; return view_;
} }
void UnifiedBluetoothDetailedViewController::OnBluetoothRefresh() { void UnifiedBluetoothDetailedViewController::OnBluetoothSystemStateChanged() {
Update(); auto* helper = Shell::Get()->tray_bluetooth_helper();
} const BluetoothSystem::State bluetooth_state = helper->GetBluetoothState();
void UnifiedBluetoothDetailedViewController::OnBluetoothDiscoveringChanged() {
Update();
}
void UnifiedBluetoothDetailedViewController::Update() { if (bluetooth_state == BluetoothSystem::State::kPoweredOn) {
// Update immediately for initial device list and // If Bluetooth was just turned on, start discovering.
// when bluetooth is disabled. Shell::Get()->tray_bluetooth_helper()->StartBluetoothDiscovering();
if (view_->IsDeviceScrollListEmpty() || } else {
Shell::Get()->tray_bluetooth_helper()->GetBluetoothState() != // Otherwise stop updating the list of devices.
BluetoothSystem::State::kPoweredOn) {
timer_.Stop(); timer_.Stop();
DoUpdate();
return;
} }
// Return here since an update is already queued. UpdateDeviceListAndUI();
if (timer_.IsRunning()) }
return;
void UnifiedBluetoothDetailedViewController::OnBluetoothScanStateChanged() {
// To avoid delaying showing devices, update the device list and UI
// immediately.
UpdateDeviceListAndUI();
// Update the detailed view after kUpdateFrequencyMs. if (Shell::Get()->tray_bluetooth_helper()->HasBluetoothDiscoverySession()) {
timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdateFrequencyMs), // Update the device list and UI every |kUpdateFrequencyMs|.
this, &UnifiedBluetoothDetailedViewController::DoUpdate); timer_.Start(
FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdateFrequencyMs), this,
&UnifiedBluetoothDetailedViewController::UpdateDeviceListAndUI);
return;
}
timer_.Stop();
} }
void UnifiedBluetoothDetailedViewController::DoUpdate() { void UnifiedBluetoothDetailedViewController::UpdateDeviceListAndUI() {
BluetoothStartDiscovering();
UpdateBluetoothDeviceList(); UpdateBluetoothDeviceList();
// Update UI auto* helper = Shell::Get()->tray_bluetooth_helper();
view_->SetToggleIsOn( bool bluetooth_on =
Shell::Get()->tray_bluetooth_helper()->GetBluetoothState() == helper->GetBluetoothState() == BluetoothSystem::State::kPoweredOn;
BluetoothSystem::State::kPoweredOn);
UpdateDeviceScrollList();
}
void UnifiedBluetoothDetailedViewController::BluetoothStartDiscovering() { // Update toggle.
TrayBluetoothHelper* helper = Shell::Get()->tray_bluetooth_helper(); view_->SetToggleIsOn(bluetooth_on);
if (helper->HasBluetoothDiscoverySession()) {
// Update loading indicator.
if (helper->HasBluetoothDiscoverySession())
view_->ShowLoadingIndicator(); view_->ShowLoadingIndicator();
else
view_->HideLoadingIndicator();
// Update scroll list or show "BT disabled" panel
if (bluetooth_on) {
view_->HideBluetoothDisabledPanel();
view_->UpdateDeviceScrollList(connected_devices_, connecting_devices_,
paired_not_connected_devices_,
discovered_not_paired_devices_);
return; return;
} }
view_->HideLoadingIndicator(); // If Bluetooth is disabled, show a panel which only indicates that it is
if (helper->GetBluetoothState() == BluetoothSystem::State::kPoweredOn) // disabled, instead of the scroller with Bluetooth devices.
helper->StartBluetoothDiscovering(); view_->ShowBluetoothDisabledPanel();
} }
void UnifiedBluetoothDetailedViewController::UpdateBluetoothDeviceList() { void UnifiedBluetoothDetailedViewController::UpdateBluetoothDeviceList() {
...@@ -160,30 +172,4 @@ void UnifiedBluetoothDetailedViewController::UpdateBluetoothDeviceList() { ...@@ -160,30 +172,4 @@ void UnifiedBluetoothDetailedViewController::UpdateBluetoothDeviceList() {
new_discovered_not_paired_devices); new_discovered_not_paired_devices);
} }
void UnifiedBluetoothDetailedViewController::UpdateDeviceScrollList() {
const BluetoothSystem::State bluetooth_state =
Shell::Get()->tray_bluetooth_helper()->GetBluetoothState();
switch (bluetooth_state) {
case BluetoothSystem::State::kUnsupported:
// Bluetooth is always supported on Chrome OS.
NOTREACHED();
return;
case BluetoothSystem::State::kUnavailable:
case BluetoothSystem::State::kPoweredOff:
case BluetoothSystem::State::kTransitioning:
// If Bluetooth is disabled, show a panel which only indicates that it is
// disabled, instead of the scroller with Bluetooth devices.
view_->ShowBluetoothDisabledPanel();
return;
case BluetoothSystem::State::kPoweredOn:
break;
}
view_->HideBluetoothDisabledPanel();
view_->UpdateDeviceScrollList(connected_devices_, connecting_devices_,
paired_not_connected_devices_,
discovered_not_paired_devices_);
}
} // namespace ash } // namespace ash
...@@ -34,15 +34,12 @@ class UnifiedBluetoothDetailedViewController : public DetailedViewController, ...@@ -34,15 +34,12 @@ class UnifiedBluetoothDetailedViewController : public DetailedViewController,
views::View* CreateView() override; views::View* CreateView() override;
// BluetoothObserver: // BluetoothObserver:
void OnBluetoothRefresh() override; void OnBluetoothSystemStateChanged() override;
void OnBluetoothDiscoveringChanged() override; void OnBluetoothScanStateChanged() override;
private: private:
void Update(); void UpdateDeviceListAndUI();
void DoUpdate();
void BluetoothStartDiscovering();
void UpdateBluetoothDeviceList(); void UpdateBluetoothDeviceList();
void UpdateDeviceScrollList();
const std::unique_ptr<DetailedViewDelegate> detailed_view_delegate_; const std::unique_ptr<DetailedViewDelegate> detailed_view_delegate_;
...@@ -54,7 +51,7 @@ class UnifiedBluetoothDetailedViewController : public DetailedViewController, ...@@ -54,7 +51,7 @@ class UnifiedBluetoothDetailedViewController : public DetailedViewController,
BluetoothDeviceList discovered_not_paired_devices_; BluetoothDeviceList discovered_not_paired_devices_;
// Timer used to limit the update frequency. // Timer used to limit the update frequency.
base::OneShotTimer timer_; base::RepeatingTimer timer_;
DISALLOW_COPY_AND_ASSIGN(UnifiedBluetoothDetailedViewController); DISALLOW_COPY_AND_ASSIGN(UnifiedBluetoothDetailedViewController);
}; };
......
...@@ -26,14 +26,19 @@ void SystemTrayNotifier::RemoveBluetoothObserver(BluetoothObserver* observer) { ...@@ -26,14 +26,19 @@ void SystemTrayNotifier::RemoveBluetoothObserver(BluetoothObserver* observer) {
bluetooth_observers_.RemoveObserver(observer); bluetooth_observers_.RemoveObserver(observer);
} }
void SystemTrayNotifier::NotifyRefreshBluetooth() { void SystemTrayNotifier::NotifyBluetoothSystemStateChanged() {
for (auto& observer : bluetooth_observers_) for (auto& observer : bluetooth_observers_)
observer.OnBluetoothRefresh(); observer.OnBluetoothSystemStateChanged();
} }
void SystemTrayNotifier::NotifyBluetoothDiscoveringChanged() { void SystemTrayNotifier::NotifyBluetoothScanStateChanged() {
for (auto& observer : bluetooth_observers_) for (auto& observer : bluetooth_observers_)
observer.OnBluetoothDiscoveringChanged(); observer.OnBluetoothScanStateChanged();
}
void SystemTrayNotifier::NotifyBluetoothDeviceListChanged() {
for (auto& observer : bluetooth_observers_)
observer.OnBluetoothDeviceListChanged();
} }
void SystemTrayNotifier::AddIMEObserver(IMEObserver* observer) { void SystemTrayNotifier::AddIMEObserver(IMEObserver* observer) {
......
...@@ -37,8 +37,9 @@ class ASH_EXPORT SystemTrayNotifier { ...@@ -37,8 +37,9 @@ class ASH_EXPORT SystemTrayNotifier {
// Bluetooth. // Bluetooth.
void AddBluetoothObserver(BluetoothObserver* observer); void AddBluetoothObserver(BluetoothObserver* observer);
void RemoveBluetoothObserver(BluetoothObserver* observer); void RemoveBluetoothObserver(BluetoothObserver* observer);
void NotifyRefreshBluetooth(); void NotifyBluetoothSystemStateChanged();
void NotifyBluetoothDiscoveringChanged(); void NotifyBluetoothScanStateChanged();
void NotifyBluetoothDeviceListChanged();
// Input methods. // Input methods.
void AddIMEObserver(IMEObserver* observer); void AddIMEObserver(IMEObserver* observer);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment