summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/status/network_menu_icon.cc
blob: 404493f1be03f8b0b265efb9b69e0a7c50592c54 (plain)
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
// 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 "chrome/browser/chromeos/status/network_menu_icon.h"

#include <algorithm>
#include <cmath>
#include <map>
#include <utility>

#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/accessibility/accessibility_util.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "grit/ash_resources.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_source.h"
#include "ui/gfx/size_conversions.h"

using std::max;
using std::min;

namespace chromeos {

namespace {

// Amount to fade icons while connecting.
const double kConnectingImageAlpha = 0.5;

// Animation cycle length.
const int kThrobDurationMs = 750;

// Images for strength bars for wired networks.
const int kNumBarsImages = 5;
gfx::ImageSkia* kBarsImagesAnimatingDark[kNumBarsImages - 1];
gfx::ImageSkia* kBarsImagesAnimatingLight[kNumBarsImages - 1];

// Imagaes for strength arcs for wireless networks.
const int kNumArcsImages = 5;
gfx::ImageSkia* kArcsImagesAnimatingDark[kNumArcsImages - 1];
gfx::ImageSkia* kArcsImagesAnimatingLight[kNumArcsImages - 1];

// Badge offsets. The right and bottom offsets are computed based on the size
// of the network icon and the badge in order to accomodate multiple icon
// resolutions (ie. standard and high DPI).
const int kBadgeLeftX = 0;
const int kBadgeTopY = 0;

int StrengthIndex(int strength, int count) {
  if (strength == 0) {
    return 0;
  } else {
    // Return an index in the range [1, count].
    const float findex = (static_cast<float>(strength) / 100.0f) *
        nextafter(static_cast<float>(count), 0);
    int index = 1 + static_cast<int>(findex);
    index = max(min(index, count), 1);
    return index;
  }
}

int WifiStrengthIndex(const WifiNetwork* wifi) {
  return StrengthIndex(wifi->strength(), kNumArcsImages - 1);
}

int WimaxStrengthIndex(const WimaxNetwork* wimax) {
  return StrengthIndex(wimax->strength(), kNumBarsImages - 1);
}

int CellularStrengthIndex(const CellularNetwork* cellular) {
  return StrengthIndex(cellular->strength(), kNumBarsImages - 1);
}

const gfx::ImageSkia* BadgeForNetworkTechnology(
    const CellularNetwork* cellular,
    NetworkMenuIcon::ResourceColorTheme color) {
  const int kUnknownBadgeType = -1;
  int id = kUnknownBadgeType;
  switch (cellular->network_technology()) {
    case NETWORK_TECHNOLOGY_EVDO:
      id = (color == NetworkMenuIcon::COLOR_DARK) ?
          IDR_AURA_UBER_TRAY_NETWORK_3G_DARK :
          IDR_AURA_UBER_TRAY_NETWORK_3G_LIGHT;
      break;
    case NETWORK_TECHNOLOGY_1XRTT:
      id = IDR_AURA_UBER_TRAY_NETWORK_1X;
      break;
    case NETWORK_TECHNOLOGY_GPRS:
      id = IDR_AURA_UBER_TRAY_NETWORK_GPRS;
      break;
    case NETWORK_TECHNOLOGY_EDGE:
      id = (color == NetworkMenuIcon::COLOR_DARK) ?
          IDR_AURA_UBER_TRAY_NETWORK_EDGE_DARK :
          IDR_AURA_UBER_TRAY_NETWORK_EDGE_LIGHT;
      break;
    case NETWORK_TECHNOLOGY_UMTS:
      id =  (color == NetworkMenuIcon::COLOR_DARK) ?
          IDR_AURA_UBER_TRAY_NETWORK_3G_DARK :
          IDR_AURA_UBER_TRAY_NETWORK_3G_LIGHT;
      break;
    case NETWORK_TECHNOLOGY_HSPA:
      id = IDR_AURA_UBER_TRAY_NETWORK_HSPA;
      break;
    case NETWORK_TECHNOLOGY_HSPA_PLUS:
      id = IDR_AURA_UBER_TRAY_NETWORK_HSPA_PLUS;
      break;
    case NETWORK_TECHNOLOGY_LTE:
      id = IDR_AURA_UBER_TRAY_NETWORK_LTE;
      break;
    case NETWORK_TECHNOLOGY_LTE_ADVANCED:
      id = IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED;
      break;
    case NETWORK_TECHNOLOGY_GSM:
      id = IDR_AURA_UBER_TRAY_NETWORK_GPRS;
      break;
    case NETWORK_TECHNOLOGY_UNKNOWN:
      break;
  }
  if (id == kUnknownBadgeType)
    return NULL;
  else
    return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id);
}

const SkBitmap GetEmptyBitmap(const gfx::Size pixel_size) {
  typedef std::pair<int, int> SizeKey;
  typedef std::map<SizeKey, SkBitmap> SizeBitmapMap;
  static SizeBitmapMap* empty_bitmaps_ = new SizeBitmapMap;

  SizeKey key(pixel_size.width(), pixel_size.height());

  SizeBitmapMap::iterator iter = empty_bitmaps_->find(key);
  if (iter != empty_bitmaps_->end())
    return iter->second;

  SkBitmap empty;
  empty.setConfig(SkBitmap::kARGB_8888_Config, key.first, key.second);
  empty.allocPixels();
  empty.eraseARGB(0, 0, 0, 0);
  (*empty_bitmaps_)[key] = empty;
  return empty;
}

class EmptyImageSource: public gfx::ImageSkiaSource {
 public:
  explicit EmptyImageSource(const gfx::Size& size)
      : size_(size) {
  }

  virtual gfx::ImageSkiaRep GetImageForScale(
      ui::ScaleFactor scale_factor) OVERRIDE {
    gfx::Size pixel_size = gfx::ToFlooredSize(
        gfx::ScaleSize(size_, ui::GetScaleFactorScale(scale_factor)));
    SkBitmap empty_bitmap = GetEmptyBitmap(pixel_size);
    return gfx::ImageSkiaRep(empty_bitmap, scale_factor);
  }
 private:
  const gfx::Size size_;

  DISALLOW_COPY_AND_ASSIGN(EmptyImageSource);
};

// This defines how we assemble a network icon.
class NetworkIconImageSource : public gfx::ImageSkiaSource {
 public:
  NetworkIconImageSource(const gfx::ImageSkia& icon,
                         const gfx::ImageSkia* top_left_badge,
                         const gfx::ImageSkia* top_right_badge,
                         const gfx::ImageSkia* bottom_left_badge,
                         const gfx::ImageSkia* bottom_right_badge)
      : icon_(icon),
        top_left_badge_(top_left_badge),
        top_right_badge_(top_right_badge),
        bottom_left_badge_(bottom_left_badge),
        bottom_right_badge_(bottom_right_badge) {
  }
  virtual ~NetworkIconImageSource() {}

  virtual gfx::ImageSkiaRep GetImageForScale(
      ui::ScaleFactor scale_factor) OVERRIDE {
    gfx::ImageSkiaRep icon_rep = icon_.GetRepresentation(scale_factor);
    if (icon_rep.is_null())
      return gfx::ImageSkiaRep();
    gfx::Canvas canvas(icon_rep, false);
    if (top_left_badge_)
      canvas.DrawImageInt(*top_left_badge_, kBadgeLeftX, kBadgeTopY);
    if (top_right_badge_)
      canvas.DrawImageInt(*top_right_badge_,
                          icon_.width() - top_right_badge_->width(),
                          kBadgeTopY);
    if (bottom_left_badge_) {
      canvas.DrawImageInt(*bottom_left_badge_,
                          kBadgeLeftX,
                          icon_.height() - bottom_left_badge_->height());
    }
    if (bottom_right_badge_) {
      canvas.DrawImageInt(*bottom_right_badge_,
                          icon_.width() - bottom_right_badge_->width(),
                          icon_.height() - bottom_right_badge_->height());
    }
    return canvas.ExtractImageRep();
  }

 private:
  const gfx::ImageSkia icon_;
  const gfx::ImageSkia *top_left_badge_;
  const gfx::ImageSkia *top_right_badge_;
  const gfx::ImageSkia *bottom_left_badge_;
  const gfx::ImageSkia *bottom_right_badge_;

  DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource);
};

gfx::ImageSkia GetEmptyImage(const gfx::Size& size) {
  return gfx::ImageSkia(new EmptyImageSource(size), size);
}

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// NetworkIcon

// Sets up and generates an ImageSkia for a Network icon.
class NetworkIcon {
 public:
  // Default constructor is used by the status bar icon (NetworkMenuIcon).
  explicit NetworkIcon(NetworkMenuIcon::ResourceColorTheme color);

  // Service path constructor for cached network service icons.
  NetworkIcon(const std::string& service_path,
              NetworkMenuIcon::ResourceColorTheme color);

  ~NetworkIcon();

  // Resets the icon state.
  void ClearIconAndBadges();

  // Resets the saved state to force an update.
  void SetDirty();

  // Updates |vpn_connected_|, returns true if it changed.
  bool SetOrClearVpnConnected(const Network* network);

  // Determines whether or not the associated network might be dirty and if so
  // updates and generates the icon. Does nothing if network no longer exists.
  void Update();

  // Sets up the base icon image.
  void SetIcon(const Network* network);

  // Sets up the various badges:
  // top_left: cellular roaming
  // top_right: libcros warning
  // bottom_left: VPN
  // bottom_right: disconnected / secure / technology / warning
  void SetBadges(const Network* network);

  // Clears any previous state then sets the base icon and badges.
  void UpdateIcon(const Network* network);

  // Generates the image. Call after setting the icon and badges.
  void GenerateImage();

  const gfx::ImageSkia GetImage() const { return image_; }

  bool ShouldShowInTray() const;

  void set_type(ConnectionType type) { type_ = type; }
  void set_state(ConnectionState state) { state_ = state; }
  void set_icon(const gfx::ImageSkia& icon) { icon_ = icon; }
  void set_top_left_badge(const gfx::ImageSkia* badge) {
    top_left_badge_ = badge;
  }
  void set_top_right_badge(const gfx::ImageSkia* badge) {
    top_right_badge_ = badge;
  }
  void set_bottom_left_badge(const gfx::ImageSkia* badge) {
    bottom_left_badge_ = badge;
  }
  void set_bottom_right_badge(const gfx::ImageSkia* badge) {
    bottom_right_badge_ = badge;
  }

 private:
  // Updates strength_index_ for wifi or cellular networks.
  // Returns true if |strength_index_| changed.
  bool UpdateWirelessStrengthIndex(const Network* network);

  // Updates the local state for cellular networks.
  bool UpdateCellularState(const Network* network);

  std::string service_path_;
  ConnectionType type_;
  ConnectionState state_;
  NetworkMenuIcon::ResourceColorTheme resource_color_theme_;
  int strength_index_;
  gfx::ImageSkia image_;
  gfx::ImageSkia icon_;
  const gfx::ImageSkia* top_left_badge_;
  const gfx::ImageSkia* top_right_badge_;
  const gfx::ImageSkia* bottom_left_badge_;
  const gfx::ImageSkia* bottom_right_badge_;
  bool is_status_bar_;
  const Network* connected_network_;  // weak pointer; used for VPN icons.
  bool vpn_connected_;
  NetworkRoamingState roaming_state_;

  DISALLOW_COPY_AND_ASSIGN(NetworkIcon);
};

////////////////////////////////////////////////////////////////////////////////
// NetworkIcon

NetworkIcon::NetworkIcon(NetworkMenuIcon::ResourceColorTheme color)
    : type_(TYPE_UNKNOWN),
      state_(STATE_UNKNOWN),
      resource_color_theme_(color),
      strength_index_(-1),
      top_left_badge_(NULL),
      top_right_badge_(NULL),
      bottom_left_badge_(NULL),
      bottom_right_badge_(NULL),
      is_status_bar_(true),
      connected_network_(NULL),
      vpn_connected_(false),
      roaming_state_(ROAMING_STATE_UNKNOWN) {
}

NetworkIcon::NetworkIcon(const std::string& service_path,
                         NetworkMenuIcon::ResourceColorTheme color)
    : service_path_(service_path),
      type_(TYPE_UNKNOWN),
      state_(STATE_UNKNOWN),
      resource_color_theme_(color),
      strength_index_(-1),
      top_left_badge_(NULL),
      top_right_badge_(NULL),
      bottom_left_badge_(NULL),
      bottom_right_badge_(NULL),
      is_status_bar_(false),
      connected_network_(NULL),
      vpn_connected_(false),
      roaming_state_(ROAMING_STATE_UNKNOWN) {
}

NetworkIcon::~NetworkIcon() {
}

void NetworkIcon::ClearIconAndBadges() {
  icon_ = gfx::ImageSkia();
  top_left_badge_ = NULL;
  top_right_badge_ = NULL;
  bottom_left_badge_ = NULL;
  bottom_right_badge_ = NULL;
}

void NetworkIcon::SetDirty() {
  state_ = STATE_UNKNOWN;
  strength_index_ = -1;
}

bool NetworkIcon::SetOrClearVpnConnected(const Network* network) {
  if (network->type() == TYPE_VPN)
    return false;  // Never show the VPN badge for a VPN network.
  chromeos::NetworkLibrary* cros =
      chromeos::CrosLibrary::Get()->GetNetworkLibrary();
  bool vpn_connected = (network->connected() &&
                        cros->virtual_network() &&
                        cros->virtual_network()->connected());
  if (vpn_connected_ != vpn_connected) {
    vpn_connected_ = vpn_connected;
    return true;
  }
  return false;
}

void NetworkIcon::Update() {
  chromeos::NetworkLibrary* cros =
      chromeos::CrosLibrary::Get()->GetNetworkLibrary();
  // First look for a visible network.
  const Network* network = cros->FindNetworkByPath(service_path_);
  if (!network) {
    // If not a visible network, check for a remembered network.
    network = cros->FindRememberedNetworkByPath(service_path_);
    if (!network) {
      LOG(WARNING) << "Unable to find network:" << service_path_;
      return;
    }
  }

  // Determine whether or not we need to update the icon.
  bool dirty = image_.isNull();

  // If the network state has changed, the icon needs updating.
  if (state_ != network->state()) {
    state_ = network->state();
    dirty = true;
  }

  type_ = network->type();

  if (type_ == TYPE_WIFI || type_ == TYPE_WIMAX || type_ == TYPE_CELLULAR) {
    if (UpdateWirelessStrengthIndex(network))
      dirty = true;
  }

  if (type_ == TYPE_CELLULAR) {
    if (UpdateCellularState(network))
      dirty = true;
  }

  if (type_ == TYPE_VPN) {
    // For VPN, check to see if the connected network has changed.
    if (cros->connected_network() != connected_network_) {
      connected_network_ = cros->connected_network();
      dirty = true;
    }
  }

  if (dirty) {
    // Set the icon and badges based on the network.
    UpdateIcon(network);
    // Generate the image from the icon.
    GenerateImage();
  }
}

void NetworkIcon::SetIcon(const Network* network) {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();

  set_type(network->type());
  set_state(network->state());

  switch (type_) {
    case TYPE_ETHERNET: {
      icon_ = *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED);
      break;
    }
    case TYPE_WIFI: {
      const WifiNetwork* wifi = static_cast<const WifiNetwork*>(network);
      if (strength_index_ == -1)
        strength_index_ = WifiStrengthIndex(wifi);
      icon_ = NetworkMenuIcon::GetImage(
          NetworkMenuIcon::ARCS, strength_index_, resource_color_theme_);
      break;
    }
    case TYPE_WIMAX: {
      const WimaxNetwork* wimax = static_cast<const WimaxNetwork*>(network);
      if (strength_index_ == -1)
        strength_index_ =  WimaxStrengthIndex(wimax);
      icon_ = NetworkMenuIcon::GetImage(
          NetworkMenuIcon::BARS, strength_index_, resource_color_theme_);
      break;
    }
    case TYPE_CELLULAR: {
      const CellularNetwork* cellular =
          static_cast<const CellularNetwork*>(network);
      if (strength_index_ == -1)
        strength_index_ = CellularStrengthIndex(cellular);
      icon_ = NetworkMenuIcon::GetImage(
          NetworkMenuIcon::BARS, strength_index_, resource_color_theme_);
      break;
    }
    case TYPE_VPN: {
      icon_ = *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN);
      break;
    }
    default: {
      LOG(WARNING) << "Request for icon for unsupported type: " << type_;
      icon_ = *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED);
      break;
    }
  }
}

void NetworkIcon::SetBadges(const Network* network) {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  chromeos::NetworkLibrary* cros =
      chromeos::CrosLibrary::Get()->GetNetworkLibrary();

  bool use_dark_icons = resource_color_theme_ == NetworkMenuIcon::COLOR_DARK;
  switch (network->type()) {
    case TYPE_ETHERNET:
      break;
    case TYPE_WIFI: {
      const WifiNetwork* wifi = static_cast<const WifiNetwork*>(network);
      if (wifi->encrypted() && use_dark_icons) {
        bottom_right_badge_ = rb.GetImageSkiaNamed(
            IDR_AURA_UBER_TRAY_NETWORK_SECURE_DARK);
      }
      break;
    }
    case TYPE_WIMAX: {
      top_left_badge_ = rb.GetImageSkiaNamed(
          use_dark_icons ?
          IDR_AURA_UBER_TRAY_NETWORK_4G_DARK :
          IDR_AURA_UBER_TRAY_NETWORK_4G_LIGHT);
      break;
    }
    case TYPE_CELLULAR: {
      const CellularNetwork* cellular =
            static_cast<const CellularNetwork*>(network);
      if (cellular->roaming_state() == ROAMING_STATE_ROAMING &&
          !cros->IsCellularAlwaysInRoaming()) {
        // For cellular that always in roaming don't show roaming badge.
        bottom_right_badge_ = rb.GetImageSkiaNamed(use_dark_icons ?
            IDR_AURA_UBER_TRAY_NETWORK_ROAMING_DARK :
            IDR_AURA_UBER_TRAY_NETWORK_ROAMING_LIGHT);
      }
      if (!cellular->connecting()) {
        top_left_badge_ = BadgeForNetworkTechnology(cellular,
                                                    resource_color_theme_);
      }
      break;
    }
    default:
      break;
  }
  if (vpn_connected_ && network->type() != TYPE_VPN) {
    bottom_left_badge_ = rb.GetImageSkiaNamed(
        IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE);
  }
}

void NetworkIcon::UpdateIcon(const Network* network) {
  ClearIconAndBadges();
  SetIcon(network);
  SetBadges(network);
}

void NetworkIcon::GenerateImage() {
  if (icon_.isNull())
    return;

  image_ = NetworkMenuIcon::GenerateImageFromComponents(icon_, top_left_badge_,
      top_right_badge_, bottom_left_badge_, bottom_right_badge_);
}

bool NetworkIcon::ShouldShowInTray() const {
  if (type_ != TYPE_ETHERNET)
    return true;
  if (!Network::IsConnectedState(state_))
    return true;
  NetworkLibrary* crosnet = CrosLibrary::Get()->GetNetworkLibrary();
  if (crosnet->virtual_network())
    return true;
  return false;
}

bool NetworkIcon::UpdateWirelessStrengthIndex(const Network* network) {
  bool dirty = false;
  ConnectionType type = network->type();
  int index = 0;
  if (type == TYPE_WIFI) {
    index = WifiStrengthIndex(static_cast<const WifiNetwork*>(network));
  } else if (type == TYPE_WIMAX) {
    index = WimaxStrengthIndex(static_cast<const WimaxNetwork*>(network));
  } else if (type == TYPE_CELLULAR) {
    index = CellularStrengthIndex(static_cast<const CellularNetwork*>(network));
  }
  if (index != strength_index_) {
    strength_index_ = index;
    dirty = true;
  }
  return dirty;
}

bool NetworkIcon::UpdateCellularState(const Network* network) {
  if (network->type() != TYPE_CELLULAR)
    return false;
  bool dirty = false;
  const CellularNetwork* cellular =
    static_cast<const CellularNetwork*>(network);
  const gfx::ImageSkia* technology_badge = BadgeForNetworkTechnology(
      cellular, resource_color_theme_);
  if (technology_badge != top_left_badge_) {
    dirty = true;
  }
  if (cellular->roaming_state() != roaming_state_) {
    roaming_state_ = cellular->roaming_state();
    dirty = true;
  }
  return dirty;
}

////////////////////////////////////////////////////////////////////////////////
// NetworkMenuIcon

NetworkMenuIcon::NetworkMenuIcon(Delegate* delegate, Mode mode)
    : mode_(mode),
      delegate_(delegate),
      resource_color_theme_(COLOR_DARK),
      ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)),
      connecting_network_(NULL) {
  // Set up the connection animation throbber.
  animation_connecting_.SetThrobDuration(kThrobDurationMs);
  animation_connecting_.SetTweenType(ui::Tween::LINEAR);

  // Initialize the icon.
  icon_.reset(new NetworkIcon(resource_color_theme_));
}

NetworkMenuIcon::~NetworkMenuIcon() {
}

// Public methods:

void NetworkMenuIcon::SetResourceColorTheme(ResourceColorTheme color) {
  if (color == resource_color_theme_)
    return;

  resource_color_theme_ = color;
  icon_.reset(new NetworkIcon(resource_color_theme_));
}

bool NetworkMenuIcon::ShouldShowIconInTray() {
  if (!icon_.get())
    return false;
  return icon_->ShouldShowInTray();
}

const gfx::ImageSkia NetworkMenuIcon::GetIconAndText(string16* text) {
  SetIconAndText();
  if (text)
    *text = text_;
  icon_->GenerateImage();
  return icon_->GetImage();
}

const gfx::ImageSkia NetworkMenuIcon::GetVpnIconAndText(string16* text) {
  SetVpnIconAndText();
  if (text)
    *text = text_;
  icon_->GenerateImage();
  return icon_->GetImage();
}

void NetworkMenuIcon::AnimationProgressed(const ui::Animation* animation) {
  if (animation == &animation_connecting_ && delegate_) {
    // Only update the connecting network from here.
    if (GetConnectingNetwork() == connecting_network_)
      delegate_->NetworkMenuIconChanged();
  }
}

// Private methods:

// In menu mode, returns any connecting network.
// In dropdown mode, only returns connecting network if not connected.
const Network* NetworkMenuIcon::GetConnectingNetwork() {
  NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
  if ((mode_ == MENU_MODE) ||
      (mode_ == DROPDOWN_MODE && !cros->connected_network())) {
    const Network* connecting_network = cros->connecting_network();
    // Only show connecting icon for wireless networks.
    if (connecting_network && connecting_network->type() != TYPE_ETHERNET) {
      return connecting_network;
    }
  }
  return NULL;
}

double NetworkMenuIcon::GetAnimation() {
  if (!animation_connecting_.is_animating()) {
    animation_connecting_.Reset();
    animation_connecting_.StartThrobbing(-1 /*throb indefinitely*/);
    return 0;
  }
  return animation_connecting_.GetCurrentValue();
}

// TODO(stevenjb): move below SetIconAndText.
void NetworkMenuIcon::SetConnectingIconAndText() {
  int image_count;
  ImageType image_type;
  gfx::ImageSkia** images;

  icon_->set_type(connecting_network_->type());
  icon_->set_state(connecting_network_->state());

  if (connecting_network_->type() == TYPE_WIFI) {
    image_count = kNumArcsImages - 1;
    image_type = ARCS;
    images = resource_color_theme_ == COLOR_DARK ? kArcsImagesAnimatingDark :
                                                   kArcsImagesAnimatingLight;
  } else {
    image_count = kNumBarsImages - 1;
    image_type = BARS;
    images = resource_color_theme_ == COLOR_DARK ? kBarsImagesAnimatingDark :
                                                   kBarsImagesAnimatingLight;
  }
  int index = GetAnimation() * nextafter(static_cast<float>(image_count), 0);
  index = std::max(std::min(index, image_count - 1), 0);

  // Lazily cache images.
  if (!images[index]) {
    gfx::ImageSkia source =
        GetImage(image_type, index + 1, resource_color_theme_);
    images[index] =
        new gfx::ImageSkia(NetworkMenuIcon::GenerateConnectingImage(source));
  }
  icon_->set_icon(*images[index]);
  icon_->SetBadges(connecting_network_);
  if (mode_ == MENU_MODE) {
    text_ = l10n_util::GetStringFUTF16(
        IDS_STATUSBAR_NETWORK_CONNECTING_TOOLTIP,
        UTF8ToUTF16(connecting_network_->name()));
  } else {
    text_ = UTF8ToUTF16(connecting_network_->name());
  }
}

// Sets up the icon and badges for GenerateBitmap().
void NetworkMenuIcon::SetIconAndText() {
  NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
  DCHECK(cros);

  if (cros->wifi_scanning())
    return;  // Don't update icon while scanning

  icon_->ClearIconAndBadges();

  // If we are connecting to a network, display that.
  connecting_network_ = GetConnectingNetwork();
  if (connecting_network_) {
    SetConnectingIconAndText();
    return;
  }

  // If not connecting to a network, show the active or connected network.
  const Network* network;
  if (mode_ == DROPDOWN_MODE && cros->connected_network())
    network = cros->connected_network();
  else
    network = cros->active_nonvirtual_network();
  if (network) {
    SetActiveNetworkIconAndText(network);
    return;
  }

  // Not connecting, so stop animation.
  animation_connecting_.Stop();

  // No connecting, connected, or active network.
  SetDisconnectedIconAndText();
}

void NetworkMenuIcon::SetVpnIconAndText() {
  NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
  DCHECK(cros);

  icon_->ClearIconAndBadges();
  const VirtualNetwork* vpn = cros->virtual_network();
  if (!vpn) {
    NOTREACHED();
    SetDisconnectedIconAndText();
    return;
  }
  if (vpn->connecting()) {
    connecting_network_ = vpn;
    SetConnectingIconAndText();
    return;
  }

  // If not connecting to a network, show the active/connected VPN.
  SetActiveNetworkIconAndText(vpn);
}

void NetworkMenuIcon::SetActiveNetworkIconAndText(const Network* network) {
  NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  bool animating = false;

  // Set icon and badges. Call SetDirty() since network may have changed.
  icon_->SetDirty();
  icon_->SetOrClearVpnConnected(network);
  icon_->UpdateIcon(network);
  // Overlay the VPN badge if connecting to a VPN.
  if (network->type() != TYPE_VPN &&
      cros->virtual_network() && cros->virtual_network()->connecting()) {
    const gfx::ImageSkia* vpn_badge =
        rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE);
    const double animation = GetAnimation();
    animating = true;
    // Even though this is the only place we use vpn_connecting_badge_,
    // it is important that this is a member variable since we set a
    // pointer to it and access that pointer in icon_->GenerateImage().
    vpn_connecting_badge_ = gfx::ImageSkiaOperations::CreateBlendedImage(
        GetEmptyImage(vpn_badge->size()), *vpn_badge, animation);
    icon_->set_bottom_left_badge(&vpn_connecting_badge_);
  }
  if (!animating)
    animation_connecting_.Stop();

  // Set the text to display.
  if (network->type() == TYPE_ETHERNET) {
    if (mode_ == MENU_MODE) {
      text_ = l10n_util::GetStringFUTF16(
          IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP,
          l10n_util::GetStringUTF16(
              IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET));
    } else {
      text_ = l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
    }
  } else {
    if (mode_ == MENU_MODE) {
      text_ = l10n_util::GetStringFUTF16(
          IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP,
          UTF8ToUTF16(network->name()));
    } else {
      text_ = UTF8ToUTF16(network->name());
    }
  }
}

void NetworkMenuIcon::SetDisconnectedIconAndText() {
  icon_->set_icon(GetDisconnectedImage(ARCS, resource_color_theme_));
  if (mode_ == MENU_MODE)
    text_ = l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP);
  else
    text_ = l10n_util::GetStringUTF16(IDS_NETWORK_SELECTION_NONE_SELECTED);
}

////////////////////////////////////////////////////////////////////////////////
// Static functions for generating network icon images:

// This defines how we assemble a network icon.
// Currently we iterate over all the available resolutions in |icon|. This will
// be wrong once we dynamically load image resolutions.
// TODO(pkotwicz): Figure out what to do when a new image resolution becomes
// available.
const gfx::ImageSkia NetworkMenuIcon::GenerateImageFromComponents(
    const gfx::ImageSkia& icon,
    const gfx::ImageSkia* top_left_badge,
    const gfx::ImageSkia* top_right_badge,
    const gfx::ImageSkia* bottom_left_badge,
    const gfx::ImageSkia* bottom_right_badge) {
  return gfx::ImageSkia(new NetworkIconImageSource(icon,
                                                   top_left_badge,
                                                   top_right_badge,
                                                   bottom_left_badge,
                                                   bottom_right_badge),
                   icon.size());
}

// We blend connecting icons with a black image to generate a faded icon.
const gfx::ImageSkia NetworkMenuIcon::GenerateConnectingImage(
    const gfx::ImageSkia& source) {
  return gfx::ImageSkiaOperations::CreateBlendedImage(
      GetEmptyImage(source.size()), source, kConnectingImageAlpha);
}

// Generates and caches an icon image for a network's current state.
const gfx::ImageSkia NetworkMenuIcon::GetImage(const Network* network,
                                               ResourceColorTheme color) {
  DCHECK(network);
  // Maintain a static (global) icon map. Note: Icons are never destroyed;
  // it is assumed that a finite and reasonable number of network icons will be
  // created during a session.

  typedef std::map<std::string, NetworkIcon*> NetworkIconMap;
  static NetworkIconMap* icon_map_dark = NULL;
  static NetworkIconMap* icon_map_light = NULL;
  if (icon_map_dark == NULL)
    icon_map_dark = new NetworkIconMap;
  if (icon_map_light == NULL)
    icon_map_light = new NetworkIconMap;

  NetworkIconMap* icon_map = color == COLOR_DARK ? icon_map_dark :
                                                   icon_map_light;
  // Find or add the icon.
  NetworkIcon* icon;
  NetworkIconMap::iterator iter = icon_map->find(network->service_path());
  if (iter == icon_map->end()) {
    icon = new NetworkIcon(network->service_path(), color);
    icon_map->insert(std::make_pair(network->service_path(), icon));
  } else {
    icon = iter->second;
  }
  // Update and return the icon's image.
  icon->Update();
  return icon->GetImage();
}

const gfx::ImageSkia NetworkMenuIcon::GetImage(ImageType type,
                                               int index,
                                               ResourceColorTheme color) {
  int width, height = 0;
  gfx::ImageSkia* images = NULL;
  if (type == NetworkMenuIcon::ARCS) {
    if (index >= kNumArcsImages)
      return gfx::ImageSkia();
    images = ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
        color == NetworkMenuIcon::COLOR_DARK ?
        IDR_AURA_UBER_TRAY_NETWORK_ARCS_DARK :
        IDR_AURA_UBER_TRAY_NETWORK_ARCS_LIGHT);
    width = images->width();
    height = images->height() / kNumArcsImages;
  } else {
    if (index >= kNumBarsImages)
      return gfx::ImageSkia();

    images = ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
        color == NetworkMenuIcon::COLOR_DARK ?
        IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK :
        IDR_AURA_UBER_TRAY_NETWORK_BARS_LIGHT);
    width = images->width();
    height = images->height() / kNumBarsImages;
  }
  return gfx::ImageSkiaOperations::ExtractSubset(*images,
      gfx::Rect(0, index * height, width, height));
}

const gfx::ImageSkia NetworkMenuIcon::GetDisconnectedImage(
    ImageType type,
    ResourceColorTheme color) {
  return GetImage(type, 0, color);
}

const gfx::ImageSkia NetworkMenuIcon::GetConnectedImage(ImageType type,
      ResourceColorTheme color) {
  return GetImage(type, NumImages(type) - 1, color);
}

gfx::ImageSkia* NetworkMenuIcon::GetVirtualNetworkImage() {
  return ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
      IDR_AURA_UBER_TRAY_NETWORK_VPN);
}

int NetworkMenuIcon::NumImages(ImageType type) {
  return (type == ARCS) ? kNumArcsImages : kNumBarsImages;
}

}  // chromeos