Index: chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc |
diff --git a/chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc b/chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc |
index 2d111ad5a6de5afe9326cd7ed43894bdb2050fc2..156540d77981b567f8b64e9d24843a7bec37198b 100644 |
--- a/chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc |
+++ b/chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc |
@@ -28,6 +28,27 @@ Browser* GetBrowser() { |
return browser_displayer.browser(); |
} |
+// Anything worse than or equal to this will show 0 bars. |
+const int kMinRSSI = -100; |
+// Anything better than or equal to this will show the maximum bars. |
+const int kMaxRSSI = -55; |
+// Number of RSSI levels used in the signal strength icon. |
+const int kNumLevels = 5; |
+ |
+// Received Signal Strength Indicator (RSSI) is a measurement of the power |
+// present in a received radio signal. |
+int CalculateSignalLevel(int8_t rssi) { |
Jeffrey Yasskin
2016/08/19 17:46:54
Write a test that this returns the right levels.
ortuno
2016/08/19 17:58:36
Could you actually move this to BluetoothDeviceCho
juncai
2016/08/19 22:59:17
Done.
juncai
2016/08/19 22:59:17
Done.
|
+ if (rssi <= kMinRSSI) |
+ return 0; |
+ |
+ if (rssi >= kMaxRSSI) |
+ return kNumLevels - 1; |
+ |
+ double input_range = kMaxRSSI - kMinRSSI; |
+ double output_range = kNumLevels - 1; |
+ return static_cast<int>((rssi - kMinRSSI) * output_range / input_range); |
+} |
+ |
} // namespace |
BluetoothChooserController::BluetoothChooserController( |
@@ -42,6 +63,10 @@ BluetoothChooserController::BluetoothChooserController( |
BluetoothChooserController::~BluetoothChooserController() {} |
+bool BluetoothChooserController::ShouldShowIconBeforeText() const { |
+ return true; |
+} |
+ |
base::string16 BluetoothChooserController::GetNoOptionsText() const { |
return no_devices_text_; |
} |
@@ -52,12 +77,16 @@ base::string16 BluetoothChooserController::GetOkButtonLabel() const { |
} |
size_t BluetoothChooserController::NumOptions() const { |
- return device_ids_.size(); |
+ return devices_.size(); |
+} |
+ |
+int BluetoothChooserController::GetSignalStrengthLevel(size_t index) const { |
+ return devices_[index].signal_strength_level; |
} |
base::string16 BluetoothChooserController::GetOption(size_t index) const { |
- DCHECK_LT(index, device_ids_.size()); |
- const std::string& device_id = device_ids_[index]; |
+ DCHECK_LT(index, devices_.size()); |
+ const std::string& device_id = devices_[index].id; |
const auto& device_name_it = device_id_to_name_map_.find(device_id); |
DCHECK(device_name_it != device_id_to_name_map_.end()); |
const auto& it = device_name_counts_.find(device_name_it->second); |
@@ -87,9 +116,9 @@ void BluetoothChooserController::Select(size_t index) { |
BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); |
return; |
} |
- DCHECK_LT(index, device_ids_.size()); |
+ DCHECK_LT(index, devices_.size()); |
event_handler_.Run(content::BluetoothChooser::Event::SELECTED, |
- device_ids_[index]); |
+ devices_[index].id); |
} |
void BluetoothChooserController::Cancel() { |
@@ -172,6 +201,7 @@ void BluetoothChooserController::AddOrUpdateDevice( |
bool is_gatt_connected, |
bool is_paired, |
const int8_t* rssi) { |
+ int level = rssi ? CalculateSignalLevel(*rssi) : -1; |
Jeffrey Yasskin
2016/08/19 17:46:54
Name this either signal_level or signal_strength_l
juncai
2016/08/19 22:59:17
Done.
|
auto name_it = device_id_to_name_map_.find(device_id); |
if (name_it != device_id_to_name_map_.end()) { |
if (should_update_name) { |
@@ -188,22 +218,25 @@ void BluetoothChooserController::AddOrUpdateDevice( |
++device_name_counts_[device_name]; |
} |
- size_t index = std::distance( |
- device_ids_.begin(), |
- std::find(device_ids_.begin(), device_ids_.end(), device_id)); |
- DCHECK_NE(device_ids_.size(), index); |
+ size_t index; |
+ for (index = 0; index < devices_.size(); ++index) { |
Jeffrey Yasskin
2016/08/19 17:46:54
You could use
auto device_it = std::find_if(device
juncai
2016/08/19 22:59:17
Done.
|
+ if (devices_[index].id == device_id) |
+ break; |
+ } |
+ |
+ DCHECK_NE(devices_.size(), index); |
// http://crbug.com/543466 Update connection and paired status |
- // http://crbug.com/629689 Update RSSI. |
+ devices_[index].signal_strength_level = level; |
if (view()) |
view()->OnOptionUpdated(index); |
return; |
} |
- device_ids_.push_back(device_id); |
+ devices_.push_back({device_id, level}); |
device_id_to_name_map_.insert({device_id, device_name}); |
++device_name_counts_[device_name]; |
if (view()) |
- view()->OnOptionAdded(device_ids_.size() - 1); |
+ view()->OnOptionAdded(devices_.size() - 1); |
} |
void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
@@ -212,13 +245,13 @@ void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
return; |
size_t index = 0; |
- for (const auto& saved_device_id : device_ids_) { |
- if (saved_device_id != device_id) { |
+ for (const auto& saved_device_info : devices_) { |
+ if (saved_device_info.id != device_id) { |
++index; |
continue; |
} |
- device_ids_.erase(device_ids_.begin() + index); |
+ devices_.erase(devices_.begin() + index); |
const auto& it = device_name_counts_.find(name_it->second); |
DCHECK(it != device_name_counts_.end()); |
@@ -240,7 +273,7 @@ void BluetoothChooserController::ResetEventHandler() { |
} |
void BluetoothChooserController::ClearAllDevices() { |
- device_ids_.clear(); |
+ devices_.clear(); |
device_id_to_name_map_.clear(); |
device_name_counts_.clear(); |
} |