OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** | 5 /** |
6 * @fileoverview Polymer element for rendering network icons based on ONC | 6 * @fileoverview Polymer element for rendering network icons based on ONC |
7 * state properties. | 7 * state properties. |
8 */ | 8 */ |
9 | 9 |
10 Polymer({ | 10 Polymer({ |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 /** | 22 /** |
23 * If true, the icon is part of a list of networks and may be displayed | 23 * If true, the icon is part of a list of networks and may be displayed |
24 * differently, e.g. the disconnected image will never be shown for | 24 * differently, e.g. the disconnected image will never be shown for |
25 * list items. | 25 * list items. |
26 */ | 26 */ |
27 isListItem: { | 27 isListItem: { |
28 type: Boolean, | 28 type: Boolean, |
29 value: false, | 29 value: false, |
30 }, | 30 }, |
| 31 |
| 32 /** |
| 33 * Animation frame, updated while the icon is in a connecting state. |
| 34 * Exposed to the DOM to trigger getIcon_ calls when it changes. |
| 35 * @private |
| 36 */ |
| 37 animationFrame_: { |
| 38 type: Number, |
| 39 value: 0, |
| 40 }, |
| 41 }, |
| 42 |
| 43 /** @private {?number} */ |
| 44 animationId_: null, |
| 45 |
| 46 /** @override */ |
| 47 detached: function() { |
| 48 this.resetAnimation_(); |
31 }, | 49 }, |
32 | 50 |
33 /** | 51 /** |
34 * @return {string} The name of the svg icon image to show. | 52 * @return {string} The name of the svg icon image to show. |
35 * @private | 53 * @private |
36 */ | 54 */ |
37 getIcon_: function() { | 55 getIcon_: function() { |
38 if (!this.networkState) | 56 var networkState = this.networkState; |
| 57 if (!networkState) |
39 return ''; | 58 return ''; |
40 let showDisconnected = | 59 var type = networkState.Type; |
41 !this.isListItem && (!this.networkState.ConnectionState || | 60 if (type == CrOnc.Type.ETHERNET) |
42 this.networkState.ConnectionState == | 61 return 'network:settings-ethernet'; |
43 CrOnc.ConnectionState.NOT_CONNECTED); | 62 if (type == CrOnc.Type.VPN) |
| 63 return 'network:vpn-key'; |
44 | 64 |
45 switch (this.networkState.Type) { | 65 var connectionState = networkState.ConnectionState; |
46 case CrOnc.Type.ETHERNET: { | 66 var showDisconnected = !this.isListItem && |
47 return 'network:settings-ethernet'; | 67 (!connectionState || |
48 } | 68 connectionState == CrOnc.ConnectionState.NOT_CONNECTED); |
49 case CrOnc.Type.VPN: { | 69 var index = 0; |
50 return 'network:vpn-key'; | 70 if (connectionState == CrOnc.ConnectionState.CONNECTING) { |
51 } | 71 index = this.getAnimationIndex_(); |
52 case CrOnc.Type.CELLULAR: { | 72 } else { |
53 let strength = | 73 this.resetAnimation_(); |
54 showDisconnected ? 0 : CrOnc.getSignalStrength(this.networkState); | 74 if (!showDisconnected) |
55 let index = this.strengthToIndex_(strength); | 75 index = this.strengthToIndex_(CrOnc.getSignalStrength(networkState)); |
56 return 'network:signal-cellular-' + index.toString(10) + '-bar'; | |
57 } | |
58 case CrOnc.Type.WI_FI: | |
59 case CrOnc.Type.WI_MAX: { | |
60 if (showDisconnected) | |
61 return 'network:signal-wifi-off'; | |
62 let strength = CrOnc.getSignalStrength(this.networkState); | |
63 let index = this.strengthToIndex_(strength); | |
64 return 'network:signal-wifi-' + index.toString(10) + '-bar'; | |
65 } | |
66 default: | |
67 assertNotReached(); | |
68 } | 76 } |
| 77 |
| 78 if (type == CrOnc.Type.CELLULAR) { |
| 79 return 'network:signal-cellular-' + index.toString(10) + '-bar'; |
| 80 } else if (type == CrOnc.Type.WI_FI || type == CrOnc.Type.WI_MAX) { |
| 81 if (showDisconnected) |
| 82 return 'network:signal-wifi-off'; |
| 83 return 'network:signal-wifi-' + index.toString(10) + '-bar'; |
| 84 } |
| 85 assertNotReached(); |
69 return ''; | 86 return ''; |
70 }, | 87 }, |
71 | 88 |
72 /** | 89 /** |
73 * @param {number} strength The signal strength from [0 - 100]. | 90 * @param {number} strength The signal strength from [0 - 100]. |
74 * @return {number} An index from 0-4 corresponding to |strength|. | 91 * @return {number} An index from 0-4 corresponding to |strength|. |
75 * @private | 92 * @private |
76 */ | 93 */ |
77 strengthToIndex_: function(strength) { | 94 strengthToIndex_: function(strength) { |
78 if (strength == 0) | 95 if (strength == 0) |
79 return 0; | 96 return 0; |
80 return Math.min(Math.trunc((strength - 1) / 25) + 1, 4); | 97 return Math.min(Math.trunc((strength - 1) / 25) + 1, 4); |
81 }, | 98 }, |
82 | 99 |
83 /** | 100 /** |
| 101 * Returns an index from 0-4 for animating a connecting wireless icon. |
| 102 * The index will cycle from 0 -> 4 then from 4 -> 0. |
| 103 * @return {number} |
| 104 * @private |
| 105 */ |
| 106 getAnimationIndex_() { |
| 107 if (this.animationId_ == null) { |
| 108 /** const */ var INTERVAL_MS = 750 / 4; // Matches network_icon.cc. |
| 109 this.animationFrame_ = 0; |
| 110 this.animationId_ = window.setInterval(function() { |
| 111 this.animationFrame_ = |
| 112 (this.animationFrame_ >= 9) ? 0 : this.animationFrame_ + 1; |
| 113 }.bind(this), INTERVAL_MS); |
| 114 } |
| 115 if (this.animationFrame_ > 4) |
| 116 return 9 - this.animationFrame_; |
| 117 return this.animationFrame_; |
| 118 }, |
| 119 |
| 120 /** @private */ |
| 121 resetAnimation_() { |
| 122 this.animationFrame_ = 0; |
| 123 if (this.animationId_ != null) { |
| 124 window.clearInterval(this.animationId_); |
| 125 this.animationId_ = null; |
| 126 } |
| 127 }, |
| 128 |
| 129 /** |
84 * @return {boolean} | 130 * @return {boolean} |
85 * @private | 131 * @private |
86 */ | 132 */ |
87 showTechnology_: function() { | 133 showTechnology_: function() { |
88 return this.getTechnology_() != ''; | 134 return this.getTechnology_() != ''; |
89 }, | 135 }, |
90 | 136 |
91 /** | 137 /** |
92 * @return {string} | 138 * @return {string} |
93 * @private | 139 * @private |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 let networkState = this.networkState; | 192 let networkState = this.networkState; |
147 if (!this.networkState) | 193 if (!this.networkState) |
148 return false; | 194 return false; |
149 if (networkState.Type == CrOnc.Type.WI_FI && networkState.WiFi) { | 195 if (networkState.Type == CrOnc.Type.WI_FI && networkState.WiFi) { |
150 let security = networkState.WiFi.Security; | 196 let security = networkState.WiFi.Security; |
151 return !!security && security != 'None'; | 197 return !!security && security != 'None'; |
152 } | 198 } |
153 return false; | 199 return false; |
154 }, | 200 }, |
155 }); | 201 }); |
OLD | NEW |