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 globalStrings = null; | |
bshe
2014/09/24 17:07:05
define all globals and functions in WallpaperUtil
Ran
2014/09/24 22:01:35
Acknowledged.
Ran
2014/09/26 17:34:53
Done.
| |
6 function enableSync(callback) { | |
7 if (globalStrings) { | |
8 if (globalStrings.isSyncCustomWallpaper) | |
9 callback(); | |
10 } | |
11 else { | |
12 chrome.wallpaperPrivate.getStrings(function(strings) { | |
13 globalStrings = strings; | |
14 if (globalStrings.isSyncCustomWallpaper) | |
15 callback(); | |
16 }); | |
17 } | |
18 } | |
19 | |
20 function errorHandler(e) { | |
21 console.error(e); | |
22 } | |
23 | |
24 function writeFile(fileEntry, picData, writeCallback) { | |
25 fileEntry.createWriter(function(fileWriter) { | |
26 var blob = new Blob([new Int8Array(picData)], | |
27 {'type' : 'image\/jpeg'}); | |
bshe
2014/09/24 17:07:05
Did you try png wallpapers? We also support png fi
Ran
2014/09/24 22:19:22
Yes, I tried png wallpapers and sync works as well
Ran
2014/09/26 17:34:54
Done.
| |
28 fileWriter.write(blob); | |
29 if (writeCallback) writeCallback(); | |
30 },errorHandler); | |
31 } | |
32 | |
33 function writeWallpaperToSync(wallpaperFilename, picData, writeCallback) { | |
34 var writeWallpaperToSyncCallback = function(fs) { | |
35 if (!fs) return; | |
36 fs.root.getFile(wallpaperFilename, | |
37 {create: false}, | |
38 function() { writeCallback();}, //already exists | |
39 function(e) { //not exists, create | |
40 fs.root.getFile(wallpaperFilename, | |
41 {create: true}, | |
42 function(fe) { | |
43 writeFile(fe, picData, writeCallback); | |
44 }, | |
45 errorHandler); | |
46 }); | |
47 }; | |
48 chrome.syncFileSystem.requestFileSystem(writeWallpaperToSyncCallback); | |
49 } | |
50 | |
51 function storePictureToLocal(picData, loadDir, picName) { | |
52 if (!picData) return; //TODO | |
53 var getDirSuccess = function(dirEntry) { | |
54 dirEntry.getFile(picName, | |
55 {create: false}, | |
56 function() {}, //already exists | |
57 function(e) { //not exists, create | |
58 dirEntry.getFile(picName, | |
59 {create: true}, | |
60 function(fe) { | |
61 writeFile(fe, picData); | |
62 }, | |
63 errorHandler); | |
64 }); | |
65 }; | |
66 window.webkitRequestFileSystem(window.PERSISTENT, 1024 * 1024 * 100, | |
67 function(fs) { | |
68 fs.root.getDirectory(loadDir, {create: true}, | |
69 getDirSuccess, errorHandler); | |
70 }, | |
71 errorHandler); | |
72 } | |
73 | |
74 var setWallpaperFromSyncLastCallFinished = true; | |
75 function setWallpaperByName(wallpaperFilename, wallpaperLayout, successCall) { | |
76 if (!setWallpaperFromSyncLastCallFinished) return; | |
77 setWallpaperFromSyncLastCallFinished = false; | |
78 var setWallpaperFromSyncCallback = function(fs) { | |
79 if (!fs) { | |
80 errorHandler('fs is null'); | |
81 return; | |
82 } | |
83 if (!wallpaperFilename) { | |
84 errorHandler('wallpaperFilename is null'); | |
85 return; | |
86 } | |
87 if (!wallpaperLayout) | |
88 wallpaperLayout = 'CENTER_CROPPED'; | |
89 fs.root.getFile(wallpaperFilename, {create: false}, function(fileEntry) { | |
90 fileEntry.file(function(file) { | |
91 var reader = new FileReader(); | |
92 reader.onloadend = function() { | |
93 chrome.wallpaperPrivate.setCustomWallpaper( | |
94 reader.result, | |
95 wallpaperLayout, | |
96 true, | |
97 wallpaperFilename, | |
98 function(thumbnailData) { | |
99 if (!thumbnailData) { | |
100 errorHandler('thumbnailData is null!'); | |
101 //setWallpaperFromSyncCallback(); | |
102 } | |
103 else { | |
104 storePictureToLocal(reader.result, | |
105 'original', | |
106 wallpaperFilename); | |
107 storePictureToLocal(thumbnailData, | |
108 'thumbnail', | |
109 wallpaperFilename); | |
110 if (successCall) | |
111 successCall(); | |
112 setWallpaperFromSyncLastCallFinished = true; | |
113 } | |
114 } | |
115 ); | |
116 }; | |
117 reader.readAsArrayBuffer(file); | |
118 },errorHandler); | |
119 },function(e) { //fail to read file, expected due to download delay | |
120 setWallpaperFromSyncLastCallFinished = true; | |
121 }); | |
122 }; | |
123 chrome.syncFileSystem.requestFileSystem(setWallpaperFromSyncCallback); | |
bshe
2014/09/24 17:07:05
We can probably save fs to a local variable so tha
Ran
2014/09/26 17:34:53
Done.
| |
124 } | |
125 | |
5 var WallpaperUtil = {}; | 126 var WallpaperUtil = {}; |
6 | 127 |
7 /** | 128 /** |
8 * Saves value to local storage that associates with key. | 129 * Saves value to local storage that associates with key. |
9 * @param {string} key The key that associates with value. | 130 * @param {string} key The key that associates with value. |
10 * @param {string} value The value to save to local storage. | 131 * @param {string} value The value to save to local storage. |
11 * @param {boolen} sync True if the value is saved to sync storage. | 132 * @param {boolen} sync True if the value is saved to sync storage. |
12 * @param {function=} opt_callback The callback on success, or on failure. | 133 * @param {function=} opt_callback The callback on success, or on failure. |
13 */ | 134 */ |
14 WallpaperUtil.saveToStorage = function(key, value, sync, opt_callback) { | 135 WallpaperUtil.saveToStorage = function(key, value, sync, opt_callback) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url, | 218 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url, |
98 onSuccess); | 219 onSuccess); |
99 self.saveWallpaperInfo(url, layout, | 220 self.saveWallpaperInfo(url, layout, |
100 Constants.WallpaperSourceEnum.Online); | 221 Constants.WallpaperSourceEnum.Online); |
101 } else { | 222 } else { |
102 onFailure(); | 223 onFailure(); |
103 } | 224 } |
104 }, onFailure); | 225 }, onFailure); |
105 }); | 226 }); |
106 }; | 227 }; |
OLD | NEW |