1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/network/tray_network.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_item_more.h"
#include "ash/system/tray/tray_item_view.h"
#include "ash/system/tray/tray_views.h"
#include "base/utf_string_conversions.h"
#include "grit/ash_strings.h"
#include "grit/ui_resources.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/aura/window.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/skia_util.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_delegate.h"
#include "ui/views/widget/widget.h"
namespace {
// Height of the list of networks in the popup.
const int kNetworkListHeight = 203;
// Create a label with the font size and color used in the network info bubble.
views::Label* CreateInfoBubbleLabel(const string16& text) {
const SkColor text_color = SkColorSetARGB(127, 0, 0, 0);
views::Label* label = new views::Label(text);
label->SetFont(label->font().DeriveFont(-1));
label->SetEnabledColor(text_color);
return label;
}
// Create a row of labels for the network info bubble.
views::View* CreateInfoBubbleLine(const string16& text_label,
const std::string& text_string) {
views::View* view = new views::View;
view->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 1));
view->AddChildView(CreateInfoBubbleLabel(text_label));
view->AddChildView(CreateInfoBubbleLabel(UTF8ToUTF16(": ")));
view->AddChildView(CreateInfoBubbleLabel(UTF8ToUTF16(text_string)));
return view;
}
// A bubble that cannot be activated.
class NonActivatableSettingsBubble : public views::BubbleDelegateView {
public:
NonActivatableSettingsBubble(views::View* anchor, views::View* content)
: views::BubbleDelegateView(anchor, views::BubbleBorder::TOP_RIGHT) {
set_use_focusless(true);
set_parent_window(ash::Shell::GetInstance()->GetContainer(
ash::internal::kShellWindowId_SettingBubbleContainer));
SetLayoutManager(new views::FillLayout());
AddChildView(content);
}
virtual ~NonActivatableSettingsBubble() {}
virtual bool CanActivate() const OVERRIDE {
return false;
}
private:
DISALLOW_COPY_AND_ASSIGN(NonActivatableSettingsBubble);
};
} // namespace
namespace ash {
namespace internal {
namespace tray {
enum ColorTheme {
LIGHT,
DARK,
};
class NetworkTrayView : public TrayItemView {
public:
NetworkTrayView(ColorTheme size, bool tray_icon)
: color_theme_(size), tray_icon_(tray_icon) {
SetLayoutManager(new views::FillLayout());
image_view_ = color_theme_ == DARK ?
new FixedSizedImageView(0, kTrayPopupItemHeight) :
new views::ImageView;
AddChildView(image_view_);
NetworkIconInfo info;
Shell::GetInstance()->tray_delegate()->
GetMostRelevantNetworkIcon(&info, false);
Update(info);
}
virtual ~NetworkTrayView() {}
void Update(const NetworkIconInfo& info) {
image_view_->SetImage(info.image);
if (tray_icon_)
SetVisible(info.tray_icon_visible);
SchedulePaint();
}
private:
views::ImageView* image_view_;
ColorTheme color_theme_;
bool tray_icon_;
DISALLOW_COPY_AND_ASSIGN(NetworkTrayView);
};
class NetworkDefaultView : public TrayItemMore {
public:
explicit NetworkDefaultView(SystemTrayItem* owner)
: TrayItemMore(owner) {
Update();
}
virtual ~NetworkDefaultView() {}
void Update() {
NetworkIconInfo info;
Shell::GetInstance()->tray_delegate()->
GetMostRelevantNetworkIcon(&info, true);
SetImage(&info.image);
SetLabel(info.description);
SetAccessibleName(info.description);
}
private:
DISALLOW_COPY_AND_ASSIGN(NetworkDefaultView);
};
class NetworkDetailedView : public views::View,
public views::ButtonListener,
public ViewClickListener {
public:
explicit NetworkDetailedView(user::LoginStatus login)
: login_(login),
header_(NULL),
header_text_(NULL),
header_buttons_(NULL),
airplane_(NULL),
info_icon_(NULL),
button_wifi_(NULL),
button_cellular_(NULL),
view_mobile_account_(NULL),
setup_mobile_account_(NULL),
other_wifi_(NULL),
other_mobile_(NULL),
settings_(NULL),
proxy_settings_(NULL),
info_bubble_(NULL) {
SetLayoutManager(new views::BoxLayout(
views::BoxLayout::kVertical, 0, 0, 0));
set_background(views::Background::CreateSolidBackground(kBackgroundColor));
SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
delegate->RequestNetworkScan();
Update();
}
virtual ~NetworkDetailedView() {
if (info_bubble_)
info_bubble_->GetWidget()->CloseNow();
}
void Update() {
RemoveAllChildViews(true);
header_ = NULL;
header_text_ = NULL;
header_buttons_ = NULL;
airplane_ = NULL;
info_icon_ = NULL;
button_wifi_ = NULL;
button_cellular_ = NULL;
view_mobile_account_ = NULL;
setup_mobile_account_ = NULL;
other_wifi_ = NULL;
other_mobile_ = NULL;
settings_ = NULL;
proxy_settings_ = NULL;
AppendHeaderEntry();
AppendHeaderButtons();
AppendNetworkEntries();
if (login_ != user::LOGGED_IN_LOCKED)
AppendNetworkExtra();
Layout();
}
private:
void AppendHeaderEntry() {
header_ = new views::View;
header_->SetLayoutManager(new
views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
header_text_ = CreateDetailedHeaderEntry(IDS_ASH_STATUS_TRAY_NETWORK, this);
header_->AddChildView(header_text_);
AddChildView(header_);
}
void AppendHeaderButtons() {
SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
header_buttons_ = new views::View;
header_buttons_->SetLayoutManager(new
views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
button_wifi_ = new TrayPopupHeaderButton(this,
IDR_AURA_UBER_TRAY_WIFI_ENABLED,
IDR_AURA_UBER_TRAY_WIFI_DISABLED);
button_wifi_->SetToggled(!delegate->GetWifiEnabled());
header_buttons_->AddChildView(button_wifi_);
if (delegate->GetCellularAvailable()) {
button_cellular_ = new TrayPopupHeaderButton(this,
IDR_AURA_UBER_TRAY_CELLULAR_ENABLED,
IDR_AURA_UBER_TRAY_CELLULAR_DISABLED);
button_cellular_->SetToggled(!delegate->GetCellularEnabled());
header_buttons_->AddChildView(button_cellular_);
}
info_icon_ = new TrayPopupHeaderButton(this,
IDR_AURA_UBER_TRAY_NETWORK_INFO,
IDR_AURA_UBER_TRAY_NETWORK_INFO);
header_buttons_->AddChildView(info_icon_);
header_->AddChildView(header_buttons_);
}
void AppendNetworkEntries() {
SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
std::vector<NetworkIconInfo> list;
delegate->GetAvailableNetworks(&list);
FixedSizedScrollView* scroller = new FixedSizedScrollView;
views::View* networks = new views::View;
networks->SetLayoutManager(new views::BoxLayout(
views::BoxLayout::kVertical, 0, 0, 1));
network_map_.clear();
for (size_t i = 0; i < list.size(); i++) {
HoverHighlightView* container = new HoverHighlightView(this);
container->set_fixed_height(kTrayPopupItemHeight);
container->AddIconAndLabel(list[i].image,
list[i].description.empty() ? list[i].name : list[i].description,
list[i].highlight ? gfx::Font::BOLD : gfx::Font::NORMAL);
networks->AddChildView(container);
container->set_border(views::Border::CreateEmptyBorder(0,
kTrayPopupDetailsIconWidth, 0, 0));
network_map_[container] = list[i].service_path;
}
if (login_ != user::LOGGED_IN_NONE) {
std::string carrier_id, topup_url, setup_url;
if (delegate->GetCellularCarrierInfo(&carrier_id,
&topup_url,
&setup_url)) {
if (carrier_id != carrier_id_) {
carrier_id_ = carrier_id;
if (!topup_url.empty())
topup_url_ = topup_url;
}
if (!setup_url.empty())
setup_url_ = setup_url;
if (!topup_url_.empty()) {
HoverHighlightView* container = new HoverHighlightView(this);
container->set_fixed_height(kTrayPopupItemHeight);
container->AddLabel(ui::ResourceBundle::GetSharedInstance().
GetLocalizedString(IDS_ASH_STATUS_TRAY_MOBILE_VIEW_ACCOUNT),
gfx::Font::NORMAL);
AddChildView(container);
view_mobile_account_ = container;
}
if (!setup_url_.empty()) {
HoverHighlightView* container = new HoverHighlightView(this);
container->set_fixed_height(kTrayPopupItemHeight);
container->AddLabel(ui::ResourceBundle::GetSharedInstance().
GetLocalizedString(IDS_ASH_STATUS_TRAY_SETUP_MOBILE),
gfx::Font::NORMAL);
AddChildView(container);
setup_mobile_account_ = container;
}
}
}
scroller->set_border(views::Border::CreateSolidSidedBorder(1, 0, 1, 0,
SkColorSetARGB(25, 0, 0, 0)));
scroller->set_fixed_size(
gfx::Size(networks->GetPreferredSize().width() +
scroller->GetScrollBarWidth(),
kNetworkListHeight));
scroller->SetContentsView(networks);
AddChildView(scroller);
}
void AppendNetworkExtra() {
ash::SystemTrayDelegate* delegate =
ash::Shell::GetInstance()->tray_delegate();
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
TrayPopupTextButtonContainer* bottom_row =
new TrayPopupTextButtonContainer;
other_wifi_ = new TrayPopupTextButton(this,
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_OTHER_WIFI));
other_wifi_->SetEnabled(delegate->GetWifiEnabled());
bottom_row->AddTextButton(other_wifi_);
if (delegate->GetCellularAvailable()) {
if (delegate->GetCellularScanSupported()) {
other_mobile_ = new TrayPopupTextButton(this,
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_OTHER_MOBILE));
other_mobile_->SetEnabled(delegate->GetCellularEnabled());
bottom_row->AddTextButton(other_mobile_);
}
}
CreateSettingsEntry();
bottom_row->AddTextButton(settings_ ? settings_ : proxy_settings_);
AddChildView(bottom_row);
}
void AppendAirplaneModeEntry() {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
HoverHighlightView* container = new HoverHighlightView(this);
container->set_fixed_height(kTrayPopupItemHeight);
container->AddIconAndLabel(
*rb.GetImageNamed(IDR_AURA_UBER_TRAY_NETWORK_AIRPLANE).ToSkBitmap(),
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_AIRPLANE_MODE),
gfx::Font::NORMAL);
AddChildView(container);
airplane_ = container;
}
// Adds a settings entry when logged in, and an entry for changing proxy
// settings otherwise.
void CreateSettingsEntry() {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
if (login_ != user::LOGGED_IN_NONE) {
// Settings, only if logged in.
settings_ = new TrayPopupTextButton(this,
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_NETWORK_SETTINGS));
} else {
proxy_settings_ = new TrayPopupTextButton(this,
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_NETWORK_PROXY_SETTINGS));
}
}
views::View* CreateNetworkInfoView() {
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
std::string ip_address, ethernet_address, wifi_address;
Shell::GetInstance()->tray_delegate()->GetNetworkAddresses(&ip_address,
ðernet_address, &wifi_address);
views::View* container = new views::View;
container->SetLayoutManager(new
views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1));
container->set_border(views::Border::CreateEmptyBorder(0, 5, 0, 5));
// GetNetworkAddresses returns empty strings if no information is available.
if (!ip_address.empty()) {
container->AddChildView(CreateInfoBubbleLine(bundle.GetLocalizedString(
IDS_ASH_STATUS_TRAY_IP), ip_address));
}
if (!ethernet_address.empty()) {
container->AddChildView(CreateInfoBubbleLine(bundle.GetLocalizedString(
IDS_ASH_STATUS_TRAY_ETHERNET), ethernet_address));
}
if (!wifi_address.empty()) {
container->AddChildView(CreateInfoBubbleLine(bundle.GetLocalizedString(
IDS_ASH_STATUS_TRAY_WIFI), wifi_address));
}
// Avoid an empty bubble in the unlikely event that there is no network
// information at all.
if (!container->has_children()) {
container->AddChildView(CreateInfoBubbleLabel(bundle.GetLocalizedString(
IDS_ASH_STATUS_TRAY_NO_NETWORKS)));
}
return container;
}
void ToggleInfoBubble() {
if (ResetInfoBubble())
return;
info_bubble_ = new NonActivatableSettingsBubble(
info_icon_, CreateNetworkInfoView());
views::BubbleDelegateView::CreateBubble(info_bubble_);
info_bubble_->Show();
}
// Returns whether an existing info-bubble was closed.
bool ResetInfoBubble() {
if (!info_bubble_)
return false;
info_bubble_->GetWidget()->Close();
info_bubble_ = NULL;
return true;
}
// Overridden from views::View.
virtual void Layout() OVERRIDE {
views::View::Layout();
// Align the network info view and icon.
gfx::Rect header_bounds = header_->bounds();
gfx::Size buttons_size = header_buttons_->size();
header_buttons_->SetBounds(
header_->width() - buttons_size.width(), 0,
buttons_size.width(), header_->height());
header_text_->SetBounds(0, 0,
header_->width() - buttons_size.width(), header_->height());
}
// Overridden from ButtonListener.
virtual void ButtonPressed(views::Button* sender,
const views::Event& event) OVERRIDE {
ash::SystemTrayDelegate* delegate =
ash::Shell::GetInstance()->tray_delegate();
if (sender == info_icon_) {
ToggleInfoBubble();
return;
}
// If the info bubble was visible, close it when some other item is clicked
// on.
ResetInfoBubble();
if (sender == button_wifi_)
delegate->ToggleWifi();
else if (sender == button_cellular_)
delegate->ToggleCellular();
else if (sender == settings_)
delegate->ShowNetworkSettings();
else if (sender == proxy_settings_)
delegate->ChangeProxySettings();
else if (sender == other_mobile_)
delegate->ShowOtherCellular();
else if (sender == other_wifi_)
delegate->ShowOtherWifi();
else
NOTREACHED();
}
// Overridden from ViewClickListener.
virtual void ClickedOn(views::View* sender) OVERRIDE {
ash::SystemTrayDelegate* delegate =
ash::Shell::GetInstance()->tray_delegate();
// If the info bubble was visible, close it when some other item is clicked
// on.
ResetInfoBubble();
if (sender == header_text_)
Shell::GetInstance()->tray()->ShowDefaultView();
if (login_ == user::LOGGED_IN_LOCKED)
return;
if (sender == view_mobile_account_) {
delegate->ShowCellularURL(topup_url_);
} else if (sender == setup_mobile_account_) {
delegate->ShowCellularURL(setup_url_);
} else if (sender == airplane_) {
delegate->ToggleAirplaneMode();
} else {
std::map<views::View*, std::string>::iterator find;
find = network_map_.find(sender);
if (find != network_map_.end()) {
std::string network_id = find->second;
delegate->ConnectToNetwork(network_id);
}
}
}
std::string carrier_id_;
std::string topup_url_;
std::string setup_url_;
user::LoginStatus login_;
std::map<views::View*, std::string> network_map_;
views::View* header_;
views::View* header_text_;
views::View* header_buttons_;
views::View* airplane_;
views::ImageButton* info_icon_;
views::ToggleImageButton* button_wifi_;
views::ToggleImageButton* button_cellular_;
views::View* view_mobile_account_;
views::View* setup_mobile_account_;
TrayPopupTextButton* other_wifi_;
TrayPopupTextButton* other_mobile_;
TrayPopupTextButton* settings_;
TrayPopupTextButton* proxy_settings_;
views::BubbleDelegateView* info_bubble_;
DISALLOW_COPY_AND_ASSIGN(NetworkDetailedView);
};
} // namespace tray
TrayNetwork::TrayNetwork()
: tray_(NULL),
default_(NULL),
detailed_(NULL) {
}
TrayNetwork::~TrayNetwork() {
}
views::View* TrayNetwork::CreateTrayView(user::LoginStatus status) {
CHECK(tray_ == NULL);
tray_ = new tray::NetworkTrayView(tray::LIGHT, true /*tray_icon*/);
return tray_;
}
views::View* TrayNetwork::CreateDefaultView(user::LoginStatus status) {
CHECK(default_ == NULL);
default_ = new tray::NetworkDefaultView(this);
return default_;
}
views::View* TrayNetwork::CreateDetailedView(user::LoginStatus status) {
CHECK(detailed_ == NULL);
detailed_ = new tray::NetworkDetailedView(status);
return detailed_;
}
void TrayNetwork::DestroyTrayView() {
tray_ = NULL;
}
void TrayNetwork::DestroyDefaultView() {
default_ = NULL;
}
void TrayNetwork::DestroyDetailedView() {
detailed_ = NULL;
}
void TrayNetwork::UpdateAfterLoginStatusChange(user::LoginStatus status) {
}
void TrayNetwork::OnNetworkRefresh(const NetworkIconInfo& info) {
if (tray_)
tray_->Update(info);
if (default_)
default_->Update();
if (detailed_)
detailed_->Update();
}
} // namespace internal
} // namespace ash
|