Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 * This Polymer element shows media controls for a route that is currently cast | 6 * This Polymer element shows media controls for a route that is currently cast |
| 7 * to a device. | 7 * to a device. |
| 8 * @implements {RouteControlsInterface} | 8 * @implements {RouteControlsInterface} |
| 9 */ | 9 */ |
| 10 Polymer({ | 10 Polymer({ |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 }, | 78 }, |
| 79 | 79 |
| 80 /** | 80 /** |
| 81 * The status of the media route shown. | 81 * The status of the media route shown. |
| 82 * @type {!media_router.RouteStatus} | 82 * @type {!media_router.RouteStatus} |
| 83 */ | 83 */ |
| 84 routeStatus: { | 84 routeStatus: { |
| 85 type: Object, | 85 type: Object, |
| 86 observer: 'onRouteStatusChange_', | 86 observer: 'onRouteStatusChange_', |
| 87 value: new media_router.RouteStatus( | 87 value: new media_router.RouteStatus( |
| 88 '', '', false, false, false, false, false, false, 0, 0, 0), | 88 '', '', false, false, false, false, 0, false, 0, 0, 0), |
|
imcheng
2017/06/20 23:00:00
nit: Can we have a default constructor for RouteSt
takumif
2017/06/20 23:56:31
Added default argument values to the ctor paramete
| |
| 89 }, | |
| 90 | |
| 91 /** | |
| 92 * The ID of the timer currently set to increment the current time of the | |
| 93 * media. Is 0 when the current time is not being incremented. | |
|
imcheng
2017/06/20 23:00:00
, or 0 if the current time is not being incremente
takumif
2017/06/20 23:56:31
Done.
| |
| 94 * @private {number} | |
| 95 */ | |
| 96 timeIncrementsTimeoutId_: { | |
| 97 type: Number, | |
| 98 value: 0, | |
| 89 }, | 99 }, |
| 90 }, | 100 }, |
| 91 | 101 |
| 92 behaviors: [ | 102 behaviors: [ |
| 93 I18nBehavior, | 103 I18nBehavior, |
| 94 ], | 104 ], |
| 95 | 105 |
| 96 /** | 106 /** |
| 97 * Called by Polymer when the element loads. Registers the element to be | 107 * Called by Polymer when the element loads. Registers the element to be |
| 98 * notified of route status updates. | 108 * notified of route status updates. |
| 99 */ | 109 */ |
| 100 ready: function() { | 110 ready: function() { |
| 101 media_router.ui.setRouteControls( | 111 media_router.ui.setRouteControls( |
| 102 /** @type {RouteControlsInterface} */ (this)); | 112 /** @type {RouteControlsInterface} */ (this)); |
| 103 }, | 113 }, |
| 104 | 114 |
| 105 /** | 115 /** |
| 116 * Current time can be incremented if the media is playing, and either the | |
| 117 * duration is 0 or current time is less than the duration. | |
| 118 * @return {boolean} | |
| 119 * @private | |
| 120 */ | |
| 121 canIncrementCurrentTime_: function() { | |
| 122 return this.routeStatus.playState === media_router.PlayState.PLAYING && | |
| 123 (this.routeStatus.duration === 0 || | |
| 124 this.routeStatus.currentTime < this.routeStatus.duration); | |
| 125 }, | |
| 126 | |
| 127 /** | |
| 106 * Converts a number representing an interval of seconds to a string with | 128 * Converts a number representing an interval of seconds to a string with |
| 107 * HH:MM:SS format. | 129 * HH:MM:SS format. |
| 108 * @param {number} timeInSec Must be non-negative. Intervals longer than 100 | 130 * @param {number} timeInSec Must be non-negative. Intervals longer than 100 |
| 109 * hours get truncated silently. | 131 * hours get truncated silently. |
| 110 * @return {string} | 132 * @return {string} |
| 111 * | |
| 112 * @private | 133 * @private |
| 113 */ | 134 */ |
| 114 getFormattedTime_: function(timeInSec) { | 135 getFormattedTime_: function(timeInSec) { |
| 115 if (timeInSec < 0) { | 136 if (timeInSec < 0) { |
| 116 return ''; | 137 return ''; |
| 117 } | 138 } |
| 118 var hours = Math.floor(timeInSec / 3600); | 139 var hours = Math.floor(timeInSec / 3600); |
| 119 var minutes = Math.floor(timeInSec / 60) % 60; | 140 var minutes = Math.floor(timeInSec / 60) % 60; |
| 120 var seconds = Math.floor(timeInSec) % 60; | 141 var seconds = Math.floor(timeInSec) % 60; |
| 121 return ('0' + hours).substr(-2) + ':' + ('0' + minutes).substr(-2) + ':' + | 142 return ('0' + hours).substr(-2) + ':' + ('0' + minutes).substr(-2) + ':' + |
| 122 ('0' + seconds).substr(-2); | 143 ('0' + seconds).substr(-2); |
| 123 }, | 144 }, |
| 124 | 145 |
| 125 /** | 146 /** |
| 126 * @param {!media_router.RouteStatus} routeStatus | 147 * @param {!media_router.RouteStatus} routeStatus |
| 127 * @return {string} The value for the icon attribute of the mute/unmute | 148 * @return {string} The value for the icon attribute of the mute/unmute |
| 128 * button. | 149 * button. |
| 129 * | |
| 130 * @private | 150 * @private |
| 131 */ | 151 */ |
| 132 getMuteUnmuteIcon_: function(routeStatus) { | 152 getMuteUnmuteIcon_: function(routeStatus) { |
| 133 return routeStatus.isMuted ? 'av:volume-off' : 'av:volume-up'; | 153 return routeStatus.isMuted ? 'av:volume-off' : 'av:volume-up'; |
| 134 }, | 154 }, |
| 135 | 155 |
| 136 /** | 156 /** |
| 137 * @param {!media_router.RouteStatus} routeStatus | 157 * @param {!media_router.RouteStatus} routeStatus |
| 138 * @return {string} Localized title for the mute/unmute button. | 158 * @return {string} Localized title for the mute/unmute button. |
| 139 * | |
| 140 * @private | 159 * @private |
| 141 */ | 160 */ |
| 142 getMuteUnmuteTitle_: function(routeStatus) { | 161 getMuteUnmuteTitle_: function(routeStatus) { |
| 143 return routeStatus.isMuted ? this.i18n('unmuteTitle') : | 162 return routeStatus.isMuted ? this.i18n('unmuteTitle') : |
| 144 this.i18n('muteTitle'); | 163 this.i18n('muteTitle'); |
| 145 }, | 164 }, |
| 146 | 165 |
| 147 /** | 166 /** |
| 148 * @param {!media_router.RouteStatus} routeStatus | 167 * @param {!media_router.RouteStatus} routeStatus |
| 149 * @return {string}The value for the icon attribute of the play/pause button. | 168 * @return {string}The value for the icon attribute of the play/pause button. |
| 150 * | |
| 151 * @private | 169 * @private |
| 152 */ | 170 */ |
| 153 getPlayPauseIcon_: function(routeStatus) { | 171 getPlayPauseIcon_: function(routeStatus) { |
| 154 return routeStatus.isPaused ? 'av:play-arrow' : 'av:pause'; | 172 return routeStatus.playState === media_router.PlayState.PAUSED ? |
| 173 'av:play-arrow' : | |
| 174 'av:pause'; | |
| 155 }, | 175 }, |
| 156 | 176 |
| 157 /** | 177 /** |
| 158 * @param {!media_router.RouteStatus} routeStatus | 178 * @param {!media_router.RouteStatus} routeStatus |
| 159 * @return {string} Localized title for the play/pause button. | 179 * @return {string} Localized title for the play/pause button. |
| 160 * | |
| 161 * @private | 180 * @private |
| 162 */ | 181 */ |
| 163 getPlayPauseTitle_: function(routeStatus) { | 182 getPlayPauseTitle_: function(routeStatus) { |
| 164 return routeStatus.isPaused ? this.i18n('playTitle') : | 183 return routeStatus.playState === media_router.PlayState.PAUSED ? |
| 165 this.i18n('pauseTitle'); | 184 this.i18n('playTitle') : |
| 185 this.i18n('pauseTitle'); | |
| 186 }, | |
| 187 | |
| 188 /** | |
| 189 * Checks whether the media is still playing, and if so, sends a media status | |
| 190 * update incrementing the current time and schedules another call for a | |
| 191 * second later. | |
| 192 * @private | |
| 193 */ | |
| 194 maybeIncrementCurrentTime_: function() { | |
| 195 if (this.canIncrementCurrentTime_()) { | |
| 196 this.routeStatus.currentTime++; | |
| 197 this.displayedCurrentTime_ = this.routeStatus.currentTime; | |
| 198 if (this.routeStatus.duration === 0 || | |
| 199 this.routeStatus.currentTime < this.routeStatus.duration) { | |
| 200 this.timeIncrementsTimeoutId_ = | |
| 201 setTimeout(() => this.maybeIncrementCurrentTime_(), 1000); | |
| 202 } | |
| 203 } else { | |
| 204 this.timeIncrementsTimeoutId_ = 0; | |
| 205 } | |
| 166 }, | 206 }, |
| 167 | 207 |
| 168 /** | 208 /** |
| 169 * Called when the user toggles the mute status of the media. Sends a mute or | 209 * Called when the user toggles the mute status of the media. Sends a mute or |
| 170 * unmute command to the browser. | 210 * unmute command to the browser. |
| 171 * | |
| 172 * @private | 211 * @private |
| 173 */ | 212 */ |
| 174 onMuteUnmute_: function() { | 213 onMuteUnmute_: function() { |
| 175 media_router.browserApi.setCurrentMediaMute(!this.routeStatus.isMuted); | 214 media_router.browserApi.setCurrentMediaMute(!this.routeStatus.isMuted); |
| 176 }, | 215 }, |
| 177 | 216 |
| 178 /** | 217 /** |
| 179 * Called when the user toggles between playing and pausing the media. Sends a | 218 * Called when the user toggles between playing and pausing the media. Sends a |
| 180 * play or pause command to the browser. | 219 * play or pause command to the browser. |
| 181 * | |
| 182 * @private | 220 * @private |
| 183 */ | 221 */ |
| 184 onPlayPause_: function() { | 222 onPlayPause_: function() { |
| 185 if (this.routeStatus.isPaused) { | 223 if (this.routeStatus.playState === media_router.PlayState.PAUSED) { |
| 186 media_router.browserApi.playCurrentMedia(); | 224 media_router.browserApi.playCurrentMedia(); |
| 187 } else { | 225 } else { |
| 188 media_router.browserApi.pauseCurrentMedia(); | 226 media_router.browserApi.pauseCurrentMedia(); |
| 189 } | 227 } |
| 190 }, | 228 }, |
| 191 | 229 |
| 192 /** | 230 /** |
| 193 * Updates seek and volume bars if the user is not currently dragging on | 231 * Updates seek and volume bars if the user is not currently dragging on |
| 194 * them. | 232 * them. |
| 195 * @param {!media_router.RouteStatus} newRouteStatus | 233 * @param {!media_router.RouteStatus} newRouteStatus |
| 196 * | |
| 197 * @private | 234 * @private |
| 198 */ | 235 */ |
| 199 onRouteStatusChange_: function(newRouteStatus) { | 236 onRouteStatusChange_: function(newRouteStatus) { |
| 200 if (!this.isSeeking_) { | 237 if (!this.isSeeking_) { |
| 201 this.displayedCurrentTime_ = newRouteStatus.currentTime; | 238 this.displayedCurrentTime_ = newRouteStatus.currentTime; |
| 202 } | 239 } |
| 203 if (!this.isVolumeChanging_) { | 240 if (!this.isVolumeChanging_) { |
| 204 this.displayedVolume_ = newRouteStatus.volume; | 241 this.displayedVolume_ = newRouteStatus.volume; |
| 205 } | 242 } |
| 206 if (newRouteStatus.description !== '') { | 243 if (newRouteStatus.description !== '') { |
| 207 this.displayedDescription_ = newRouteStatus.description; | 244 this.displayedDescription_ = newRouteStatus.description; |
| 208 } | 245 } |
| 209 if (!this.initialLoadTime_) { | 246 if (!this.initialLoadTime_) { |
| 210 this.initialLoadTime_ = Date.now(); | 247 this.initialLoadTime_ = Date.now(); |
| 211 media_router.browserApi.reportWebUIRouteControllerLoaded( | 248 media_router.browserApi.reportWebUIRouteControllerLoaded( |
| 212 this.initialLoadTime_ - this.routeDetailsOpenTime); | 249 this.initialLoadTime_ - this.routeDetailsOpenTime); |
| 213 } | 250 } |
| 251 if (!this.timeIncrementsTimeoutId_) { | |
|
imcheng
2017/06/20 23:00:00
Do we need to check this.canIncrementCurrentTime_
takumif
2017/06/20 23:56:31
We could, but it's not necessary, since maybeIncre
| |
| 252 this.timeIncrementsTimeoutId_ = | |
| 253 setTimeout(() => this.maybeIncrementCurrentTime_(), 1000); | |
| 254 } | |
| 214 }, | 255 }, |
| 215 | 256 |
| 216 /** | 257 /** |
| 217 * Called when the route is updated. Updates the description shown if it has | 258 * Called when the route is updated. Updates the description shown if it has |
| 218 * not been provided by status updates. | 259 * not been provided by status updates. |
| 219 * @param {!media_router.Route} route | 260 * @param {?media_router.Route} route |
| 220 */ | 261 */ |
| 221 onRouteUpdated: function(route) { | 262 onRouteUpdated: function(route) { |
| 222 if (this.routeStatus.description === '') { | 263 if (!route) { |
| 264 this.stopIncrementingCurrentTime_(); | |
| 265 } | |
| 266 if (route && this.routeStatus.description === '') { | |
| 223 this.displayedDescription_ = | 267 this.displayedDescription_ = |
| 224 loadTimeData.getStringF('castingActivityStatus', route.description); | 268 loadTimeData.getStringF('castingActivityStatus', route.description); |
| 225 } | 269 } |
| 226 }, | 270 }, |
| 227 | 271 |
| 228 /** | 272 /** |
| 229 * Called when the user clicks on or stops dragging the seek bar. | 273 * Called when the user clicks on or stops dragging the seek bar. |
| 230 * @param {!Event} e | 274 * @param {!Event} e |
| 231 * | |
| 232 * @private | 275 * @private |
| 233 */ | 276 */ |
| 234 onSeekComplete_: function(e) { | 277 onSeekComplete_: function(e) { |
| 278 this.stopIncrementingCurrentTime_(); | |
| 235 this.isSeeking_ = false; | 279 this.isSeeking_ = false; |
| 236 this.displayedCurrentTime_ = e.target.value; | 280 this.displayedCurrentTime_ = e.target.value; |
| 237 media_router.browserApi.seekCurrentMedia(this.displayedCurrentTime_); | 281 media_router.browserApi.seekCurrentMedia(this.displayedCurrentTime_); |
| 238 }, | 282 }, |
| 239 | 283 |
| 240 /** | 284 /** |
| 241 * Called when the user starts dragging the seek bar. | 285 * Called when the user starts dragging the seek bar. |
| 242 * @param {!Event} e | 286 * @param {!Event} e |
| 243 * | |
| 244 * @private | 287 * @private |
| 245 */ | 288 */ |
| 246 onSeekStart_: function(e) { | 289 onSeekStart_: function(e) { |
| 247 this.isSeeking_ = true; | 290 this.isSeeking_ = true; |
| 248 var target = /** @type {{immediateValue: number}} */ (e.target); | 291 var target = /** @type {{immediateValue: number}} */ (e.target); |
| 249 this.displayedCurrentTime_ = target.immediateValue; | 292 this.displayedCurrentTime_ = target.immediateValue; |
| 250 }, | 293 }, |
| 251 | 294 |
| 252 /** | 295 /** |
| 253 * Called when the user clicks on or stops dragging the volume bar. | 296 * Called when the user clicks on or stops dragging the volume bar. |
| 254 * @param {!Event} e | 297 * @param {!Event} e |
| 255 * | |
| 256 * @private | 298 * @private |
| 257 */ | 299 */ |
| 258 onVolumeChangeComplete_: function(e) { | 300 onVolumeChangeComplete_: function(e) { |
| 259 this.isVolumeChanging_ = false; | 301 this.isVolumeChanging_ = false; |
| 260 this.volumeSliderValue_ = e.target.value; | 302 this.volumeSliderValue_ = e.target.value; |
| 261 media_router.browserApi.setCurrentMediaVolume(this.volumeSliderValue_); | 303 media_router.browserApi.setCurrentMediaVolume(this.volumeSliderValue_); |
| 262 }, | 304 }, |
| 263 | 305 |
| 264 /** | 306 /** |
| 265 * Called when the user starts dragging the volume bar. | 307 * Called when the user starts dragging the volume bar. |
| 266 * @param {!Event} e | 308 * @param {!Event} e |
| 267 * | |
| 268 * @private | 309 * @private |
| 269 */ | 310 */ |
| 270 onVolumeChangeStart_: function(e) { | 311 onVolumeChangeStart_: function(e) { |
| 271 this.isVolumeChanging_ = true; | 312 this.isVolumeChanging_ = true; |
| 272 var target = /** @type {{immediateValue: number}} */ (e.target); | 313 var target = /** @type {{immediateValue: number}} */ (e.target); |
| 273 this.volumeSliderValue_ = target.immediateValue; | 314 this.volumeSliderValue_ = target.immediateValue; |
| 274 }, | 315 }, |
| 275 | 316 |
| 276 /** | 317 /** |
| 277 * Resets the route controls. Called when the route details view is closed. | 318 * Resets the route controls. Called when the route details view is closed. |
| 278 */ | 319 */ |
| 279 reset: function() { | 320 reset: function() { |
| 280 this.routeStatus = new media_router.RouteStatus( | 321 this.routeStatus = new media_router.RouteStatus( |
| 281 '', '', false, false, false, false, false, false, 0, 0, 0); | 322 '', '', false, false, false, false, false, false, 0, 0, 0); |
| 282 media_router.ui.setRouteControls(null); | 323 media_router.ui.setRouteControls(null); |
| 283 }, | 324 }, |
| 325 | |
| 326 /** | |
| 327 * If it is currently incrementing the current time shown, then stops doing | |
| 328 * so. | |
| 329 * @private | |
| 330 */ | |
| 331 stopIncrementingCurrentTime_: function() { | |
| 332 if (this.timeIncrementsTimeoutId_) { | |
| 333 clearTimeout(this.timeIncrementsTimeoutId_); | |
| 334 this.timeIncrementsTimeoutId_ = 0; | |
| 335 } | |
| 336 } | |
| 284 }); | 337 }); |
| OLD | NEW |