1// Copyright (c) 2011 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 "base/utf_string_conversions.h"
8#include "chrome/browser/chromeos/cros/cros_library.h"
9#include "chrome/browser/chromeos/options/network_config_view.h"
10#include "chrome/browser/chromeos/status/status_area_host.h"
11#include "grit/generated_resources.h"
12#include "grit/theme_resources.h"
13#include "ui/base/l10n/l10n_util.h"
14#include "ui/base/resource/resource_bundle.h"
15#include "ui/gfx/canvas_skia.h"
16#include "views/window/window.h"
17
18namespace chromeos {
19
20////////////////////////////////////////////////////////////////////////////////
21// NetworkDropdownButton
22
23// static
24const int NetworkDropdownButton::kThrobDuration = 750;
25
26NetworkDropdownButton::NetworkDropdownButton(bool browser_mode,
27                                             gfx::NativeWindow parent_window)
28    : DropDownButton(NULL,
29                     UTF16ToWide(l10n_util::GetStringUTF16(
30                         IDS_STATUSBAR_NO_NETWORKS_MESSAGE)),
31                     this,
32                     true),
33      browser_mode_(browser_mode),
34      ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)),
35      parent_window_(parent_window) {
36  animation_connecting_.SetThrobDuration(kThrobDuration);
37  animation_connecting_.SetTweenType(ui::Tween::LINEAR);
38  CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this);
39  // The initial state will be updated on Refresh.
40  // See network_selection_view.cc.
41}
42
43NetworkDropdownButton::~NetworkDropdownButton() {
44  CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this);
45}
46
47////////////////////////////////////////////////////////////////////////////////
48// NetworkMenuButton, ui::AnimationDelegate implementation:
49
50void NetworkDropdownButton::AnimationProgressed(
51    const ui::Animation* animation) {
52  if (animation == &animation_connecting_) {
53    SetIcon(*IconForNetworkConnecting(animation_connecting_.GetCurrentValue(),
54                                      true));
55    SchedulePaint();
56  } else {
57    MenuButton::AnimationProgressed(animation);
58  }
59}
60
61void NetworkDropdownButton::Refresh() {
62  OnNetworkManagerChanged(CrosLibrary::Get()->GetNetworkLibrary());
63}
64
65////////////////////////////////////////////////////////////////////////////////
66// NetworkDropdownButton, NetworkLibrary::NetworkManagerObserver implementation:
67
68void NetworkDropdownButton::OnNetworkManagerChanged(NetworkLibrary* cros) {
69  // Show network that we will actually use. It could be another network than
70  // user selected. For example user selected WiFi network but we have Ethernet
71  // connection and Chrome OS device will actually use Ethernet.
72
73  // This gets called on initialization, so any changes should be reflected
74  // in CrosMock::SetNetworkLibraryStatusAreaExpectations().
75
76  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
77  if (CrosLibrary::Get()->EnsureLoaded()) {
78    // Always show the active network, if any
79    const Network* active_network = cros->active_network();
80    if (active_network != NULL) {
81      animation_connecting_.Stop();
82      if (active_network->type() == TYPE_ETHERNET) {
83        SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED));
84        SetText(UTF16ToWide(
85            l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET)));
86      } else if (active_network->type() == TYPE_WIFI) {
87        const WifiNetwork* wifi =
88            static_cast<const WifiNetwork*>(active_network);
89        SetIcon(*IconForNetworkStrength(wifi, true));
90        SetText(UTF8ToWide(wifi->name()));
91      } else if (active_network->type() == TYPE_CELLULAR) {
92        const CellularNetwork* cellular =
93            static_cast<const CellularNetwork*>(active_network);
94        SetIcon(*IconForNetworkStrength(cellular, true));
95        SetText(UTF8ToWide(cellular->name()));
96      } else {
97        NOTREACHED();
98      }
99    } else if (cros->wifi_connecting() || cros->cellular_connecting()) {
100      if (!animation_connecting_.is_animating()) {
101        animation_connecting_.Reset();
102        animation_connecting_.StartThrobbing(-1);
103        SetIcon(*IconForNetworkConnecting(0, true));
104      }
105      if (cros->wifi_connecting())
106        SetText(UTF8ToWide(cros->wifi_network()->name()));
107      else if (cros->cellular_connecting())
108        SetText(UTF8ToWide(cros->cellular_network()->name()));
109    }
110
111    if (!cros->Connected() && !cros->Connecting()) {
112      animation_connecting_.Stop();
113      SetIcon(SkBitmap());
114      SetText(UTF16ToWide(
115          l10n_util::GetStringUTF16(IDS_NETWORK_SELECTION_NONE)));
116    }
117  } else {
118    animation_connecting_.Stop();
119    SetIcon(SkBitmap());
120    SetText(UTF16ToWide(
121        l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_NETWORKS_MESSAGE)));
122  }
123
124  SchedulePaint();
125  UpdateMenu();
126}
127
128}  // namespace chromeos
129