network_dropdown_button.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
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
3// found in the LICENSE file.
4
5#include "chrome/browser/chromeos/status/network_dropdown_button.h"
6
7#include "app/l10n_util.h"
8#include "app/resource_bundle.h"
9#include "base/utf_string_conversions.h"
10#include "chrome/browser/chromeos/cros/cros_library.h"
11#include "chrome/browser/chromeos/options/network_config_view.h"
12#include "chrome/browser/chromeos/status/status_area_host.h"
13#include "gfx/canvas_skia.h"
14#include "grit/generated_resources.h"
15#include "grit/theme_resources.h"
16#include "views/window/window.h"
17
18namespace chromeos {
19
20////////////////////////////////////////////////////////////////////////////////
21// NetworkDropdownButton
22
23// static
24const int NetworkDropdownButton::kThrobDuration = 1000;
25
26NetworkDropdownButton::NetworkDropdownButton(bool browser_mode,
27                                             gfx::NativeWindow parent_window)
28    : MenuButton(NULL,
29                 l10n_util::GetString(IDS_STATUSBAR_NO_NETWORKS_MESSAGE),
30                 this,
31                 true),
32      browser_mode_(browser_mode),
33      ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)),
34      parent_window_(parent_window) {
35  animation_connecting_.SetThrobDuration(kThrobDuration);
36  animation_connecting_.SetTweenType(Tween::LINEAR);
37  NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary());
38  CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(this);
39}
40
41NetworkDropdownButton::~NetworkDropdownButton() {
42  CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this);
43}
44
45////////////////////////////////////////////////////////////////////////////////
46// NetworkMenuButton, AnimationDelegate implementation:
47
48void NetworkDropdownButton::AnimationProgressed(const Animation* animation) {
49  if (animation == &animation_connecting_) {
50    // Figure out which image to draw. We want a value between 0-100.
51    // 0 represents no signal and 100 represents full signal strength.
52    int value = static_cast<int>(animation_connecting_.GetCurrentValue()*100.0);
53    if (value < 0)
54      value = 0;
55    else if (value > 100)
56      value = 100;
57    SetIcon(IconForNetworkStrength(value, true));
58    SchedulePaint();
59  } else {
60    MenuButton::AnimationProgressed(animation);
61  }
62}
63
64void NetworkDropdownButton::Refresh() {
65  NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary());
66}
67
68////////////////////////////////////////////////////////////////////////////////
69// NetworkDropdownButton, NetworkLibrary::Observer implementation:
70
71void NetworkDropdownButton::NetworkChanged(NetworkLibrary* cros) {
72  // Show network that we will actually use. It could be another network than
73  // user selected. For example user selected WiFi network but we have Ethernet
74  // connection and Chrome OS device will actually use Ethernet.
75
76  // This gets called on initialization, so any changes should be reflected
77  // in CrosMock::SetNetworkLibraryStatusAreaExpectations().
78
79  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
80  if (CrosLibrary::Get()->EnsureLoaded()) {
81    // Always show the higher priority connection first. Ethernet then wifi.
82    if (cros->ethernet_connected()) {
83      animation_connecting_.Stop();
84      SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED));
85      SetText(l10n_util::GetString(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET));
86    } else if (cros->wifi_connected()) {
87      animation_connecting_.Stop();
88      SetIcon(IconForNetworkStrength(
89          cros->wifi_network().strength(), true));
90      SetText(ASCIIToWide(cros->wifi_network().name()));
91    } else if (cros->cellular_connected()) {
92      animation_connecting_.Stop();
93      SetIcon(IconForNetworkStrength(
94          cros->cellular_network().strength(), false));
95      SetText(ASCIIToWide(cros->cellular_network().name()));
96    } else if (cros->wifi_connecting() || cros->cellular_connecting()) {
97      if (!animation_connecting_.is_animating()) {
98        animation_connecting_.Reset();
99        animation_connecting_.StartThrobbing(-1);
100        SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS1_BLACK));
101      }
102
103      if (cros->wifi_connecting())
104        SetText(ASCIIToWide(cros->wifi_network().name()));
105      else if (cros->cellular_connecting())
106        SetText(ASCIIToWide(cros->cellular_network().name()));
107    }
108
109    if (!cros->Connected() && !cros->Connecting()) {
110      animation_connecting_.Stop();
111      SetIcon(SkBitmap());
112      SetText(l10n_util::GetString(IDS_NETWORK_SELECTION_NONE));
113    }
114  } else {
115    animation_connecting_.Stop();
116    SetIcon(SkBitmap());
117    SetText(l10n_util::GetString(IDS_STATUSBAR_NO_NETWORKS_MESSAGE));
118  }
119
120  SchedulePaint();
121}
122
123}  // namespace chromeos
124