OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 var WALLPAPER_PICKER_WIDTH = 574; | 5 var WALLPAPER_PICKER_WIDTH = 574; |
6 var WALLPAPER_PICKER_HEIGHT = 420; | 6 var WALLPAPER_PICKER_HEIGHT = 420; |
7 | 7 |
8 var wallpaperPickerWindow; | 8 var wallpaperPickerWindow; |
9 | 9 |
10 var surpriseWallpaper = null; | 10 var surpriseWallpaper = null; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 }; | 108 }; |
109 | 109 |
110 /** | 110 /** |
111 * Sets a new random wallpaper if one has not already been set today. | 111 * Sets a new random wallpaper if one has not already been set today. |
112 * @private | 112 * @private |
113 */ | 113 */ |
114 SurpriseWallpaper.prototype.updateRandomWallpaper_ = function() { | 114 SurpriseWallpaper.prototype.updateRandomWallpaper_ = function() { |
115 var self = this; | 115 var self = this; |
116 Constants.WallpaperSyncStorage.get( | 116 Constants.WallpaperSyncStorage.get( |
117 Constants.AccessLastSurpriseWallpaperChangedDate, function(items) { | 117 Constants.AccessLastSurpriseWallpaperChangedDate, function(items) { |
118 var dateString = new Date().toDateString(); | 118 var today = new Date().toDateString(); |
119 // At most one random wallpaper per day. | 119 self.setRandomWallpaper_(today); |
120 if (items[Constants.AccessLastSurpriseWallpaperChangedDate] != dateString) { | |
121 self.setRandomWallpaper_(dateString); | |
122 } | |
123 }); | 120 }); |
124 }; | 121 }; |
125 | 122 |
126 /** | 123 /** |
127 * Sets wallpaper to one of the wallpapers displayed in wallpaper picker. If | 124 * Sets wallpaper to one of the wallpapers displayed in wallpaper picker. If |
128 * the wallpaper download fails, retry one hour later. Wallpapers that are | 125 * the wallpaper download fails, retry one hour later. Wallpapers that are |
129 * disabled for surprise me are excluded. | 126 * disabled for surprise me are excluded. |
130 * @param {string} dateString String representation of current local date. | 127 * @param {string} dateString String representation of current local date. |
131 * @private | 128 * @private |
132 */ | 129 */ |
(...skipping 10 matching lines...) Expand all Loading... | |
143 element.available_for_surprise_me == undefined; | 140 element.available_for_surprise_me == undefined; |
144 }); | 141 }); |
145 var index = Math.floor(Math.random() * filtered.length); | 142 var index = Math.floor(Math.random() * filtered.length); |
146 var wallpaper = filtered[index]; | 143 var wallpaper = filtered[index]; |
147 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix; | 144 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix; |
148 var onSuccess = function() { | 145 var onSuccess = function() { |
149 WallpaperUtil.saveToStorage( | 146 WallpaperUtil.saveToStorage( |
150 Constants.AccessLastSurpriseWallpaperChangedDate, | 147 Constants.AccessLastSurpriseWallpaperChangedDate, |
151 dateString, | 148 dateString, |
152 true); | 149 true); |
150 WallpaperUtil.saveWallpaperInfo(wallpaperURL, wallpaper.default_layout, | |
151 Constants.WallpaperSourceEnum.Online); | |
bshe
2015/03/04 19:45:39
nit: 4 spaces indention for new line.
xdai1
2015/03/04 21:32:39
Done.
| |
153 }; | 152 }; |
154 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout, | 153 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout, |
155 onSuccess, self.retryLater_.bind(self)); | 154 onSuccess, self.retryLater_.bind(self)); |
156 } | 155 } |
157 }); | 156 }); |
158 }; | 157 }; |
159 | 158 |
160 /** | 159 /** |
161 * Sets wallpaper to the wallpaper specified by item from rss. If downloading | 160 * Sets wallpaper to the wallpaper specified by item from rss. If downloading |
162 * the wallpaper fails, retry one hour later. | 161 * the wallpaper fails, retry one hour later. |
(...skipping 24 matching lines...) Expand all Loading... | |
187 * Disables the wallpaper surprise me feature. Clear all alarms and states. | 186 * Disables the wallpaper surprise me feature. Clear all alarms and states. |
188 */ | 187 */ |
189 SurpriseWallpaper.prototype.disable = function() { | 188 SurpriseWallpaper.prototype.disable = function() { |
190 chrome.alarms.clearAll(); | 189 chrome.alarms.clearAll(); |
191 // Makes last changed date invalid. | 190 // Makes last changed date invalid. |
192 WallpaperUtil.saveToStorage(Constants.AccessLastSurpriseWallpaperChangedDate, | 191 WallpaperUtil.saveToStorage(Constants.AccessLastSurpriseWallpaperChangedDate, |
193 '', true); | 192 '', true); |
194 }; | 193 }; |
195 | 194 |
196 /** | 195 /** |
197 * Changes current wallpaper and sets up an alarm to schedule next change around | 196 * Changes current wallpaper once a day and sets up an alarm to schedule next |
198 * midnight. | 197 * change around midnight. If the surprise wallpaper was already set toady, |
198 * then don't change it any more. | |
199 */ | 199 */ |
200 SurpriseWallpaper.prototype.next = function() { | 200 SurpriseWallpaper.prototype.next = function() { |
201 var nextUpdate = this.nextUpdateTime(new Date()); | 201 var nextUpdate = this.nextUpdateTime(new Date()); |
202 chrome.alarms.create({when: nextUpdate}); | 202 chrome.alarms.create({when: nextUpdate}); |
203 this.tryChangeWallpaper(); | 203 |
204 var self = this; | |
205 Constants.WallpaperSyncStorage.get( | |
bshe
2015/03/04 19:45:39
why do you want to move the check to here? Looks l
xdai1
2015/03/04 21:32:39
Since when surprise me is enabled, we only change
bshe
2015/03/05 14:53:19
Rss wallpaper is not saved to sync storage and we
| |
206 Constants.AccessLastSurpriseWallpaperChangedDate, function(items) { | |
207 var today = new Date().toDateString(); | |
208 if (items[Constants.AccessLastSurpriseWallpaperChangedDate] != today) { | |
209 self.tryChangeWallpaper(); | |
210 } | |
211 }); | |
204 }; | 212 }; |
205 | 213 |
206 /** | 214 /** |
207 * Calculates when the next wallpaper change should be triggered. | 215 * Calculates when the next wallpaper change should be triggered. |
208 * @param {Date} now Current time. | 216 * @param {Date} now Current time. |
209 * @return {number} The time when next wallpaper change should happen. | 217 * @return {number} The time when next wallpaper change should happen. |
210 */ | 218 */ |
211 SurpriseWallpaper.prototype.nextUpdateTime = function(now) { | 219 SurpriseWallpaper.prototype.nextUpdateTime = function(now) { |
212 var nextUpdate = new Date(now.setDate(now.getDate() + 1)).toDateString(); | 220 var nextUpdate = new Date(now.setDate(now.getDate() + 1)).toDateString(); |
213 return new Date(nextUpdate).getTime(); | 221 return new Date(nextUpdate).getTime(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 WallpaperUtil.requestSyncFS(function() {}); | 278 WallpaperUtil.requestSyncFS(function() {}); |
271 if (changes[Constants.AccessSurpriseMeEnabledKey]) { | 279 if (changes[Constants.AccessSurpriseMeEnabledKey]) { |
272 if (changes[Constants.AccessSurpriseMeEnabledKey].newValue) { | 280 if (changes[Constants.AccessSurpriseMeEnabledKey].newValue) { |
273 SurpriseWallpaper.getInstance().next(); | 281 SurpriseWallpaper.getInstance().next(); |
274 } else { | 282 } else { |
275 SurpriseWallpaper.getInstance().disable(); | 283 SurpriseWallpaper.getInstance().disable(); |
276 } | 284 } |
277 } | 285 } |
278 | 286 |
279 if (changes[Constants.AccessSyncWallpaperInfoKey]) { | 287 if (changes[Constants.AccessSyncWallpaperInfoKey]) { |
280 var newValue = changes[Constants.AccessSyncWallpaperInfoKey].newValue; | 288 var syncInfo = changes[Constants.AccessSyncWallpaperInfoKey].newValue; |
281 Constants.WallpaperLocalStorage.get(Constants.AccessLocalWallpaperInfoKey, | 289 |
282 function(items) { | 290 Constants.WallpaperSyncStorage.get(Constants.AccessSurpriseMeEnabledKey, |
283 // Normally, the wallpaper info saved in local storage and sync storage | 291 function(enabledItems) { |
284 // are the same. If the synced value changed by sync service, they may | 292 var syncSurpriseMeEnabled = |
285 // different. In that case, change wallpaper to the one saved in sync | 293 enabledItems[Constants.AccessSurpriseMeEnabledKey]; |
bshe
2015/03/04 19:45:39
nit: 4 spaces indention
xdai1
2015/03/04 21:32:39
Done.
| |
286 // storage and update the local value. | 294 |
287 var localValue = items[Constants.AccessLocalWallpaperInfoKey]; | 295 Constants.WallpaperSyncStorage.get( |
288 if (localValue == undefined || | 296 Constants.AccessLastSurpriseWallpaperChangedDate, function(items) { |
bshe
2015/03/04 19:45:39
ditto
xdai1
2015/03/04 21:32:39
Done.
| |
289 localValue.url != newValue.url || | 297 var syncLastSurpriseMeChangedDate = |
bshe
2015/03/04 19:45:39
nit: function implementation should have 2 space i
xdai1
2015/03/04 21:32:39
I think it already has 2 space indention?
| |
290 localValue.layout != newValue.layout || | 298 items[Constants.AccessLastSurpriseWallpaperChangedDate]; |
291 localValue.source != newValue.source) { | 299 |
292 if (newValue.source == Constants.WallpaperSourceEnum.Online) { | 300 var today = new Date().toDateString(); |
293 // TODO(bshe): Consider schedule an alarm to set online wallpaper | 301 // If SurpriseMe is enabled and surprise wallpaper hasn't been changed |
294 // later when failed. Note that we need to cancel the retry if user | 302 // today, we should not sync the change, instead onAlarm() will be |
295 // set another wallpaper before retry alarm invoked. | 303 // triggered to update a surprise me wallpaper. |
296 WallpaperUtil.setOnlineWallpaper(newValue.url, newValue.layout, | 304 if (!syncSurpriseMeEnabled || (syncSurpriseMeEnabled && |
297 function() {}, function() {}); | 305 syncLastSurpriseMeChangedDate == today)) { |
bshe
2015/03/04 19:45:39
nit: indention is off. Consider this:
if (!syncSur
xdai1
2015/03/04 21:32:39
Done.
| |
298 } else if (newValue.source == Constants.WallpaperSourceEnum.Custom) { | 306 Constants.WallpaperLocalStorage.get( |
299 WallpaperUtil.setCustomWallpaperFromSyncFS(newValue.url, | 307 Constants.AccessLocalWallpaperInfoKey, function(infoItems) { |
300 newValue.layout); | 308 var localInfo = |
301 } else if (newValue.source == Constants.WallpaperSourceEnum.Default) { | 309 infoItems[Constants.AccessLocalWallpaperInfoKey]; |
bshe
2015/03/04 19:45:39
nit: 4 space indention
xdai1
2015/03/04 21:32:39
Done.
| |
302 chrome.wallpaperPrivate.resetWallpaper(); | 310 // Normally, the wallpaper info saved in local storage and sync |
311 // storage are the same. If the synced value changed by sync | |
312 // service, they may different. In that case, change wallpaper | |
313 // to the one saved in sync storage and update the local value. | |
314 if (localInfo == undefined || | |
315 localInfo.url != syncInfo.url || | |
316 localInfo.layout != syncInfo.layout || | |
317 localInfo.source != syncInfo.source) { | |
318 if (syncInfo.source == Constants.WallpaperSourceEnum.Online) { | |
319 // TODO(bshe): Consider schedule an alarm to set online | |
320 // wallpaper later when failed. Note that we need to cancel | |
321 // the retry if user set another wallpaper before retry | |
322 // alarm invoked. | |
323 WallpaperUtil.setOnlineWallpaper(syncInfo.url, | |
324 syncInfo.layout, function() {}, function() {}); | |
325 } else if (syncInfo.source == | |
326 Constants.WallpaperSourceEnum.Custom) { | |
327 WallpaperUtil.setCustomWallpaperFromSyncFS(syncInfo.url, | |
328 syncInfo.layout); | |
329 } else if (syncInfo.source == | |
330 Constants.WallpaperSourceEnum.Default) { | |
331 chrome.wallpaperPrivate.resetWallpaper(); | |
332 } | |
333 WallpaperUtil.saveToStorage( | |
334 Constants.AccessLocalWallpaperInfoKey, syncInfo, false); | |
335 } | |
336 }); | |
303 } | 337 } |
304 WallpaperUtil.saveToStorage(Constants.AccessLocalWallpaperInfoKey, | 338 }); |
305 newValue, false); | |
306 } | |
307 }); | 339 }); |
308 } | 340 } |
309 }); | 341 }); |
310 }); | 342 }); |
311 | 343 |
312 chrome.alarms.onAlarm.addListener(function() { | 344 chrome.alarms.onAlarm.addListener(function() { |
313 SurpriseWallpaper.getInstance().next(); | 345 SurpriseWallpaper.getInstance().next(); |
314 }); | 346 }); |
OLD | NEW |