| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/status/network_menu_button.h" | 5 #include "chrome/browser/chromeos/status/network_menu_button.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 // static | 28 // static |
| 29 const int NetworkMenuButton::kThrobDuration = 1000; | 29 const int NetworkMenuButton::kThrobDuration = 1000; |
| 30 | 30 |
| 31 NetworkMenuButton::NetworkMenuButton(StatusAreaHost* host) | 31 NetworkMenuButton::NetworkMenuButton(StatusAreaHost* host) |
| 32 : StatusAreaButton(this), | 32 : StatusAreaButton(this), |
| 33 NetworkMenu(), | 33 NetworkMenu(), |
| 34 host_(host), | 34 host_(host), |
| 35 ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)) { | 35 ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)) { |
| 36 animation_connecting_.SetThrobDuration(kThrobDuration); | 36 animation_connecting_.SetThrobDuration(kThrobDuration); |
| 37 animation_connecting_.SetTweenType(Tween::EASE_IN_OUT); | 37 animation_connecting_.SetTweenType(Tween::EASE_IN_OUT); |
| 38 OnNetworkManagerChanged(CrosLibrary::Get()->GetNetworkLibrary()); | 38 NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary()); |
| 39 CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this); | 39 CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(this); |
| 40 CrosLibrary::Get()->GetNetworkLibrary()->AddCellularDataPlanObserver(this); | |
| 41 } | 40 } |
| 42 | 41 |
| 43 NetworkMenuButton::~NetworkMenuButton() { | 42 NetworkMenuButton::~NetworkMenuButton() { |
| 44 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); | 43 CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this); |
| 45 netlib->RemoveNetworkManagerObserver(this); | |
| 46 netlib->RemoveObserverForAllNetworks(this); | |
| 47 netlib->RemoveCellularDataPlanObserver(this); | |
| 48 } | 44 } |
| 49 | 45 |
| 50 //////////////////////////////////////////////////////////////////////////////// | 46 //////////////////////////////////////////////////////////////////////////////// |
| 51 // NetworkMenuButton, AnimationDelegate implementation: | 47 // NetworkMenuButton, AnimationDelegate implementation: |
| 52 | 48 |
| 53 void NetworkMenuButton::AnimationProgressed(const Animation* animation) { | 49 void NetworkMenuButton::AnimationProgressed(const Animation* animation) { |
| 54 if (animation == &animation_connecting_) { | 50 if (animation == &animation_connecting_) { |
| 55 // Draw animation of bars icon fading in and out. | 51 // Draw animation of bars icon fading in and out. |
| 56 // We are fading between 0 bars and a third of the opacity of 4 bars. | 52 // We are fading between 0 bars and a third of the opacity of 4 bars. |
| 57 // Use the current value of the animation to calculate the alpha value | 53 // Use the current value of the animation to calculate the alpha value |
| (...skipping 12 matching lines...) Expand all Loading... |
| 70 | 66 |
| 71 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
| 72 // NetworkMenuButton, StatusAreaButton implementation: | 68 // NetworkMenuButton, StatusAreaButton implementation: |
| 73 | 69 |
| 74 void NetworkMenuButton::DrawIcon(gfx::Canvas* canvas) { | 70 void NetworkMenuButton::DrawIcon(gfx::Canvas* canvas) { |
| 75 canvas->DrawBitmapInt(IconForDisplay(icon(), badge()), | 71 canvas->DrawBitmapInt(IconForDisplay(icon(), badge()), |
| 76 horizontal_padding(), 0); | 72 horizontal_padding(), 0); |
| 77 } | 73 } |
| 78 | 74 |
| 79 //////////////////////////////////////////////////////////////////////////////// | 75 //////////////////////////////////////////////////////////////////////////////// |
| 80 // NetworkMenuButton, NetworkLibrary::NetworkManagerObserver implementation: | 76 // NetworkMenuButton, NetworkLibrary::Observer implementation: |
| 81 | 77 |
| 82 void NetworkMenuButton::OnNetworkManagerChanged(NetworkLibrary* cros) { | 78 void NetworkMenuButton::NetworkChanged(NetworkLibrary* cros) { |
| 83 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 79 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 84 if (CrosLibrary::Get()->EnsureLoaded()) { | 80 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 85 // Add an observer for the active network, if any | |
| 86 const Network* network = cros->active_network(); | |
| 87 if (active_network_.empty() || network == NULL || | |
| 88 active_network_ != network->service_path()) { | |
| 89 if (!active_network_.empty()) { | |
| 90 cros->RemoveNetworkObserver(active_network_, this); | |
| 91 } | |
| 92 if (network != NULL) { | |
| 93 cros->AddNetworkObserver(network->service_path(), this); | |
| 94 } | |
| 95 } | |
| 96 if (network) | |
| 97 active_network_ = network->service_path(); | |
| 98 else | |
| 99 active_network_ = ""; | |
| 100 | |
| 101 if (cros->wifi_connecting() || cros->cellular_connecting()) { | 81 if (cros->wifi_connecting() || cros->cellular_connecting()) { |
| 102 // Start the connecting animation if not running. | 82 // Start the connecting animation if not running. |
| 103 if (!animation_connecting_.is_animating()) { | 83 if (!animation_connecting_.is_animating()) { |
| 104 animation_connecting_.Reset(); | 84 animation_connecting_.Reset(); |
| 105 animation_connecting_.StartThrobbing(std::numeric_limits<int>::max()); | 85 animation_connecting_.StartThrobbing(std::numeric_limits<int>::max()); |
| 106 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); | 86 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); |
| 107 } | 87 } |
| 108 std::string network_name = cros->wifi_connecting() ? | 88 std::string network_name = cros->wifi_connecting() ? |
| 109 cros->wifi_network()->name() : cros->cellular_network()->name(); | 89 cros->wifi_network()->name() : cros->cellular_network()->name(); |
| 110 bool configuring = cros->wifi_connecting() ? | 90 bool configuring = cros->wifi_connecting() ? |
| 111 cros->wifi_network()->configuring() : | 91 cros->wifi_network()->configuring() : |
| 112 cros->cellular_network()->configuring(); | 92 cros->cellular_network()->configuring(); |
| 113 SetTooltipText( | 93 SetTooltipText( |
| 114 l10n_util::GetStringF(configuring ? | 94 l10n_util::GetStringF(configuring ? |
| 115 IDS_STATUSBAR_NETWORK_CONFIGURING_TOOLTIP : | 95 IDS_STATUSBAR_NETWORK_CONFIGURING_TOOLTIP : |
| 116 IDS_STATUSBAR_NETWORK_CONNECTING_TOOLTIP, | 96 IDS_STATUSBAR_NETWORK_CONNECTING_TOOLTIP, |
| 117 UTF8ToWide(network_name))); | 97 UTF8ToWide(network_name))); |
| 118 } else { | 98 } else { |
| 119 // Stop connecting animation since we are not connecting. | 99 // Stop connecting animation since we are not connecting. |
| 120 animation_connecting_.Stop(); | 100 animation_connecting_.Stop(); |
| 121 if (!cros->Connected()) { | 101 |
| 102 // Always show the higher priority connection first. Ethernet then wifi. |
| 103 if (cros->ethernet_connected()) { |
| 104 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED)); |
| 105 SetTooltipText( |
| 106 l10n_util::GetStringF( |
| 107 IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, |
| 108 l10n_util::GetString(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET))); |
| 109 } else if (cros->wifi_connected()) { |
| 110 SetIcon(IconForNetworkStrength( |
| 111 cros->wifi_network()->strength(), false)); |
| 112 SetTooltipText(l10n_util::GetStringF( |
| 113 IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, |
| 114 UTF8ToWide(cros->wifi_network()->name()))); |
| 115 } else if (cros->cellular_connected()) { |
| 116 const CellularNetwork* cellular = cros->cellular_network(); |
| 117 if (cellular->data_left() == CellularNetwork::DATA_NONE) { |
| 118 // If no data, then we show 0 bars. |
| 119 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); |
| 120 } else { |
| 121 SetIcon(IconForNetworkStrength(cellular)); |
| 122 } |
| 123 SetTooltipText(l10n_util::GetStringF( |
| 124 IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, |
| 125 UTF8ToWide(cellular->name()))); |
| 126 } else { |
| 122 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); | 127 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); |
| 123 SetTooltipText(l10n_util::GetString( | 128 SetTooltipText(l10n_util::GetString( |
| 124 IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)); | 129 IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)); |
| 125 } else { | |
| 126 SetNetworkIcon(network); | |
| 127 } | 130 } |
| 128 } | 131 } |
| 129 SetNetworkBadge(cros, network); | 132 |
| 133 // Figure out whether or not to show a badge. |
| 134 int id = -1; |
| 135 if (cros->Connecting()) { |
| 136 if (cros->cellular_connecting()) { |
| 137 id = IDR_STATUSBAR_NETWORK_3G; |
| 138 } |
| 139 } else if (cros->Connected()) { |
| 140 if (!cros->ethernet_connected() && !cros->wifi_connected() && |
| 141 cros->cellular_connected()) { |
| 142 switch (cros->cellular_network()->data_left()) { |
| 143 case CellularNetwork::DATA_NONE: |
| 144 case CellularNetwork::DATA_VERY_LOW: |
| 145 id = IDR_STATUSBAR_NETWORK_3G_ERROR; |
| 146 break; |
| 147 case CellularNetwork::DATA_LOW: |
| 148 id = IDR_STATUSBAR_NETWORK_3G_WARN; |
| 149 break; |
| 150 case CellularNetwork::DATA_NORMAL: |
| 151 id = IDR_STATUSBAR_NETWORK_3G; |
| 152 break; |
| 153 } |
| 154 } |
| 155 } else { |
| 156 id = IDR_STATUSBAR_NETWORK_DISCONNECTED; |
| 157 } |
| 158 if (id != -1) { |
| 159 SetBadge(*rb.GetBitmapNamed(id)); |
| 160 } else { |
| 161 SetBadge(SkBitmap()); |
| 162 } |
| 130 } else { | 163 } else { |
| 131 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); | 164 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); |
| 132 SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_WARNING)); | 165 SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_WARNING)); |
| 133 SetTooltipText(l10n_util::GetString( | |
| 134 IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)); | |
| 135 } | |
| 136 | |
| 137 SchedulePaint(); | |
| 138 UpdateMenu(); | |
| 139 } | |
| 140 | |
| 141 //////////////////////////////////////////////////////////////////////////////// | |
| 142 // NetworkMenuButton, NetworkLibrary::NetworkObserver implementation: | |
| 143 void NetworkMenuButton::OnNetworkChanged(NetworkLibrary* cros, | |
| 144 const Network* network) { | |
| 145 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 146 if (CrosLibrary::Get()->EnsureLoaded()) { | |
| 147 // Always show the active network connection, if any. | |
| 148 SetNetworkIcon(network); | |
| 149 SetNetworkBadge(cros, network); | |
| 150 } else { | |
| 151 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); | |
| 152 SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_WARNING)); | |
| 153 SetTooltipText(l10n_util::GetString( | 166 SetTooltipText(l10n_util::GetString( |
| 154 IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)); | 167 IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)); |
| 155 } | 168 } |
| 156 | 169 |
| 157 SchedulePaint(); | 170 SchedulePaint(); |
| 158 UpdateMenu(); | 171 UpdateMenu(); |
| 159 } | 172 } |
| 160 | 173 |
| 161 void NetworkMenuButton::OnCellularDataPlanChanged(NetworkLibrary* cros) { | 174 void NetworkMenuButton::CellularDataPlanChanged(NetworkLibrary* cros) { |
| 162 // Call OnNetworkManagerChanged which will update the icon. | 175 // Call NetworkChanged which will update the icon. |
| 163 OnNetworkManagerChanged(cros); | 176 NetworkChanged(cros); |
| 164 } | 177 } |
| 165 | 178 |
| 166 //////////////////////////////////////////////////////////////////////////////// | 179 //////////////////////////////////////////////////////////////////////////////// |
| 167 // NetworkMenuButton, NetworkMenu implementation: | 180 // NetworkMenuButton, NetworkMenu implementation: |
| 168 | 181 |
| 169 bool NetworkMenuButton::IsBrowserMode() const { | 182 bool NetworkMenuButton::IsBrowserMode() const { |
| 170 return host_->IsBrowserMode(); | 183 return host_->IsBrowserMode(); |
| 171 } | 184 } |
| 172 | 185 |
| 173 gfx::NativeWindow NetworkMenuButton::GetNativeWindow() const { | 186 gfx::NativeWindow NetworkMenuButton::GetNativeWindow() const { |
| 174 return host_->GetNativeWindow(); | 187 return host_->GetNativeWindow(); |
| 175 } | 188 } |
| 176 | 189 |
| 177 void NetworkMenuButton::OpenButtonOptions() const { | 190 void NetworkMenuButton::OpenButtonOptions() const { |
| 178 host_->OpenButtonOptions(this); | 191 host_->OpenButtonOptions(this); |
| 179 } | 192 } |
| 180 | 193 |
| 181 bool NetworkMenuButton::ShouldOpenButtonOptions() const { | 194 bool NetworkMenuButton::ShouldOpenButtonOptions() const { |
| 182 return host_->ShouldOpenButtonOptions(this); | 195 return host_->ShouldOpenButtonOptions(this); |
| 183 } | 196 } |
| 184 | 197 |
| 185 //////////////////////////////////////////////////////////////////////////////// | |
| 186 // NetworkMenuButton, private methods | |
| 187 | |
| 188 void NetworkMenuButton::SetNetworkIcon(const Network* network) { | |
| 189 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 190 if (network && network->is_active()) { | |
| 191 if (network->type() == TYPE_ETHERNET) { | |
| 192 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED)); | |
| 193 SetTooltipText( | |
| 194 l10n_util::GetStringF( | |
| 195 IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, | |
| 196 l10n_util::GetString(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET))); | |
| 197 } else if (network->type() == TYPE_WIFI) { | |
| 198 const WifiNetwork* wifi = static_cast<const WifiNetwork*>(network); | |
| 199 SetIcon(IconForNetworkStrength(wifi->strength(), false)); | |
| 200 SetTooltipText(l10n_util::GetStringF( | |
| 201 IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, | |
| 202 UTF8ToWide(wifi->name()))); | |
| 203 } else if (network->type() == TYPE_CELLULAR) { | |
| 204 const CellularNetwork* cellular = | |
| 205 static_cast<const CellularNetwork*>(network); | |
| 206 if (cellular->data_left() == CellularNetwork::DATA_NONE) { | |
| 207 // If no data, then we show 0 bars. | |
| 208 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); | |
| 209 } else { | |
| 210 SetIcon(IconForNetworkStrength(cellular)); | |
| 211 } | |
| 212 SetTooltipText(l10n_util::GetStringF( | |
| 213 IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, | |
| 214 UTF8ToWide(cellular->name()))); | |
| 215 } | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 void NetworkMenuButton::SetNetworkBadge(NetworkLibrary* cros, | |
| 220 const Network* network) { | |
| 221 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 222 // Figure out whether or not to show a badge. | |
| 223 if (network && network->type() == TYPE_CELLULAR && | |
| 224 (network->is_active() || network->connecting())) { | |
| 225 const CellularNetwork* cellular | |
| 226 = static_cast<const CellularNetwork*>(network); | |
| 227 SetBadge(BadgeForNetworkTechnology(cellular)); | |
| 228 } else if (!cros->Connected() && !cros->Connecting()) { | |
| 229 SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_DISCONNECTED)); | |
| 230 } else { | |
| 231 SetBadge(SkBitmap()); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 } // namespace chromeos | 198 } // namespace chromeos |
| OLD | NEW |