cronapp-cordova-plugin-contentsync 4.4.0-RC7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +11 -0
- package/README.md +372 -0
- package/package-lock.json +1545 -0
- package/package.json +58 -0
- package/plugin.xml +93 -0
- package/sample/css/index.css +128 -0
- package/sample/img/logo.png +0 -0
- package/sample/index.html +47 -0
- package/sample/js/index.js +152 -0
- package/spec/helper/cordova.js +83 -0
- package/spec/index.spec.js +334 -0
- package/src/android/Sync.java +1102 -0
- package/src/browser/Sync.js +20 -0
- package/src/ios/ContentSync.h +74 -0
- package/src/ios/ContentSync.m +1002 -0
- package/src/ios/SSZipArchive.h +52 -0
- package/src/ios/SSZipArchive.m +540 -0
- package/src/ios/minizip/crypt.h +131 -0
- package/src/ios/minizip/ioapi.c +239 -0
- package/src/ios/minizip/ioapi.h +201 -0
- package/src/ios/minizip/mztools.c +284 -0
- package/src/ios/minizip/mztools.h +31 -0
- package/src/ios/minizip/unzip.c +2153 -0
- package/src/ios/minizip/unzip.h +437 -0
- package/src/ios/minizip/zip.c +2022 -0
- package/src/ios/minizip/zip.h +362 -0
- package/src/windows/SyncProxy.js +279 -0
- package/src/windows/ZipWinProj/PGZipInflate.cs +94 -0
- package/src/windows/ZipWinProj/Properties/AssemblyInfo.cs +30 -0
- package/src/windows/ZipWinProj/ZipWinProj.csproj +57 -0
- package/src/wp8/Sync.cs +746 -0
- package/src/wp8/Unzip.cs +481 -0
- package/tests/anyfile.txt +1 -0
- package/tests/archives/www1.zip +0 -0
- package/tests/archives/www2.zip +0 -0
- package/tests/package.json +11 -0
- package/tests/plugin.xml +22 -0
- package/tests/scripts/start-server.sh +2 -0
- package/tests/scripts/stop-server.sh +1 -0
- package/tests/tests.js +255 -0
- package/www/index.js +285 -0
|
@@ -0,0 +1,1102 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
you may not use this file except in compliance with the License.
|
|
4
|
+
You may obtain a copy of the License at
|
|
5
|
+
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
|
|
8
|
+
Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
See the License for the specific language governing permissions and
|
|
12
|
+
limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
package com.adobe.phonegap.contentsync;
|
|
16
|
+
|
|
17
|
+
import java.io.BufferedInputStream;
|
|
18
|
+
import java.io.Closeable;
|
|
19
|
+
import java.io.File;
|
|
20
|
+
import java.io.FileInputStream;
|
|
21
|
+
import java.io.FileOutputStream;
|
|
22
|
+
import java.io.FilterInputStream;
|
|
23
|
+
import java.io.IOException;
|
|
24
|
+
import java.io.InputStream;
|
|
25
|
+
import java.io.OutputStream;
|
|
26
|
+
import java.lang.reflect.InvocationTargetException;
|
|
27
|
+
import java.lang.reflect.Method;
|
|
28
|
+
import java.net.HttpURLConnection;
|
|
29
|
+
import java.net.URLConnection;
|
|
30
|
+
import java.util.HashMap;
|
|
31
|
+
import java.util.Iterator;
|
|
32
|
+
import java.util.zip.GZIPInputStream;
|
|
33
|
+
import java.util.zip.Inflater;
|
|
34
|
+
import java.util.zip.ZipEntry;
|
|
35
|
+
import java.util.zip.ZipFile;
|
|
36
|
+
import java.util.zip.ZipInputStream;
|
|
37
|
+
|
|
38
|
+
import org.apache.cordova.CordovaPlugin;
|
|
39
|
+
import org.apache.cordova.CallbackContext;
|
|
40
|
+
import org.apache.cordova.CordovaResourceApi;
|
|
41
|
+
import org.apache.cordova.CordovaResourceApi.OpenForReadResult;
|
|
42
|
+
import org.apache.cordova.PluginResult;
|
|
43
|
+
import org.json.JSONArray;
|
|
44
|
+
import org.json.JSONException;
|
|
45
|
+
import org.json.JSONObject;
|
|
46
|
+
|
|
47
|
+
import android.app.Activity;
|
|
48
|
+
import android.content.SharedPreferences;
|
|
49
|
+
import android.content.pm.PackageManager;
|
|
50
|
+
import android.net.Uri;
|
|
51
|
+
import android.os.Environment;
|
|
52
|
+
import android.os.StatFs;
|
|
53
|
+
import android.util.Log;
|
|
54
|
+
import android.util.Patterns;
|
|
55
|
+
import android.webkit.CookieManager;
|
|
56
|
+
|
|
57
|
+
public class Sync extends CordovaPlugin {
|
|
58
|
+
private static final int STATUS_STOPPED = 0;
|
|
59
|
+
private static final int STATUS_DOWNLOADING = 1;
|
|
60
|
+
private static final int STATUS_EXTRACTING = 2;
|
|
61
|
+
private static final int STATUS_COMPLETE = 3;
|
|
62
|
+
|
|
63
|
+
public static final int INVALID_URL_ERROR = 1;
|
|
64
|
+
public static final int CONNECTION_ERROR = 2;
|
|
65
|
+
public static final int UNZIP_ERROR = 3;
|
|
66
|
+
|
|
67
|
+
private static final String PROP_LOCAL_PATH = "localPath";
|
|
68
|
+
private static final String PROP_STATUS = "status";
|
|
69
|
+
private static final String PROP_PROGRESS = "progress";
|
|
70
|
+
private static final String PROP_LOADED = "loaded";
|
|
71
|
+
private static final String PROP_TOTAL = "total";
|
|
72
|
+
private static final String PROP_CACHED = "cached";
|
|
73
|
+
// Type
|
|
74
|
+
private static final String TYPE_REPLACE = "replace";
|
|
75
|
+
private static final String TYPE_MERGE = "merge";
|
|
76
|
+
private static final String TYPE_LOCAL = "local";
|
|
77
|
+
|
|
78
|
+
private static final String LOG_TAG = "ContentSync";
|
|
79
|
+
public static final String PREVIOUS_VERSION = "PREVIOUS_VERSION";
|
|
80
|
+
|
|
81
|
+
private static HashMap<String, ProgressEvent> activeRequests = new HashMap<String, ProgressEvent>();
|
|
82
|
+
private static final int MAX_BUFFER_SIZE = 16 * 1024;
|
|
83
|
+
|
|
84
|
+
@Override
|
|
85
|
+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
|
86
|
+
if (action.equals("sync")) {
|
|
87
|
+
sync(args, callbackContext);
|
|
88
|
+
return true;
|
|
89
|
+
} else if (action.equals("download")) {
|
|
90
|
+
final String source = args.getString(0);
|
|
91
|
+
// Production
|
|
92
|
+
String outputDirectory = cordova.getActivity().getCacheDir().getAbsolutePath();
|
|
93
|
+
// Testing
|
|
94
|
+
//String outputDirectory = cordova.getActivity().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
|
|
95
|
+
String filename = source.substring(source.lastIndexOf("/")+1, source.length());
|
|
96
|
+
final File target = new File(outputDirectory, filename);
|
|
97
|
+
// @TODO we need these
|
|
98
|
+
final JSONObject headers = new JSONObject();
|
|
99
|
+
final CallbackContext finalContext = callbackContext;
|
|
100
|
+
cordova.getThreadPool().execute(new Runnable() {
|
|
101
|
+
public void run() {
|
|
102
|
+
if (download(source, target, headers, createProgressEvent("download"), finalContext)) {
|
|
103
|
+
JSONObject retval = new JSONObject();
|
|
104
|
+
try {
|
|
105
|
+
retval.put("archiveURL", target.getAbsolutePath());
|
|
106
|
+
} catch (JSONException e) {
|
|
107
|
+
// never happens
|
|
108
|
+
}
|
|
109
|
+
finalContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, retval));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return true;
|
|
114
|
+
} else if (action.equals("unzip")) {
|
|
115
|
+
String tempPath = args.getString(0);
|
|
116
|
+
if (tempPath.startsWith("file://")) {
|
|
117
|
+
tempPath = tempPath.substring(7);
|
|
118
|
+
}
|
|
119
|
+
final File source = new File(tempPath);
|
|
120
|
+
final String target = args.getString(1);
|
|
121
|
+
final CallbackContext finalContext = callbackContext;
|
|
122
|
+
cordova.getThreadPool().execute(new Runnable() {
|
|
123
|
+
public void run() {
|
|
124
|
+
unzipSync(source, target, createProgressEvent("unzip"), finalContext);
|
|
125
|
+
finalContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return true;
|
|
129
|
+
} else if (action.equals("cancel")) {
|
|
130
|
+
ProgressEvent progress = activeRequests.get(args.getString(0));
|
|
131
|
+
if (progress != null) {
|
|
132
|
+
progress.setAborted(true);
|
|
133
|
+
}
|
|
134
|
+
} else if (action.equals("loadUrl")) {
|
|
135
|
+
final String url = args.getString(0);
|
|
136
|
+
final CallbackContext finalContext = callbackContext;
|
|
137
|
+
cordova.getActivity().runOnUiThread(new Runnable() {
|
|
138
|
+
public void run() {
|
|
139
|
+
if (!loadIonicServerBasePath(url)) {
|
|
140
|
+
webView.loadUrlIntoView(url, true);
|
|
141
|
+
}
|
|
142
|
+
finalContext.success();
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private boolean loadIonicServerBasePath(String url) {
|
|
151
|
+
if (url == null || !url.startsWith("file://")) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
String path = Uri.parse(url).getPath();
|
|
157
|
+
File file = new File(path);
|
|
158
|
+
if (!file.exists()) {
|
|
159
|
+
File parent = file.getParentFile();
|
|
160
|
+
File grandParent = parent != null ? parent.getParentFile() : null;
|
|
161
|
+
File fallbackRootIndex = grandParent != null ? new File(grandParent, "index.html") : null;
|
|
162
|
+
File fallbackWwwIndex = parent != null ? new File(parent, "www/index.html") : null;
|
|
163
|
+
|
|
164
|
+
if (fallbackRootIndex != null && fallbackRootIndex.exists()) {
|
|
165
|
+
file = fallbackRootIndex;
|
|
166
|
+
} else if (fallbackWwwIndex != null && fallbackWwwIndex.exists()) {
|
|
167
|
+
file = fallbackWwwIndex;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
String basePath = file.isFile() ? file.getParent() : path;
|
|
171
|
+
Object engine = webView.getClass().getMethod("getEngine").invoke(webView);
|
|
172
|
+
Method setServerBasePath = engine.getClass().getMethod("setServerBasePath", String.class);
|
|
173
|
+
|
|
174
|
+
cordova.getActivity()
|
|
175
|
+
.getApplicationContext()
|
|
176
|
+
.getSharedPreferences("WebViewSettings", Activity.MODE_PRIVATE)
|
|
177
|
+
.edit()
|
|
178
|
+
.putString("serverBasePath", basePath)
|
|
179
|
+
.apply();
|
|
180
|
+
|
|
181
|
+
setServerBasePath.invoke(engine, basePath);
|
|
182
|
+
return true;
|
|
183
|
+
} catch (Exception e) {
|
|
184
|
+
Log.e(LOG_TAG, "Failed to load Ionic server base path", e);
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Adds an interface method to an InputStream to return the number of bytes
|
|
191
|
+
* read from the raw stream. This is used to track total progress against
|
|
192
|
+
* the HTTP Content-Length header value from the server.
|
|
193
|
+
*/
|
|
194
|
+
private static abstract class TrackingInputStream extends FilterInputStream {
|
|
195
|
+
public TrackingInputStream(final InputStream in) {
|
|
196
|
+
super(in);
|
|
197
|
+
}
|
|
198
|
+
public abstract long getTotalRawBytesRead();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private static class ExposedGZIPInputStream extends GZIPInputStream {
|
|
202
|
+
public ExposedGZIPInputStream(final InputStream in) throws IOException {
|
|
203
|
+
super(in);
|
|
204
|
+
}
|
|
205
|
+
public Inflater getInflater() {
|
|
206
|
+
return inf;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Provides raw bytes-read tracking for a GZIP input stream. Reports the
|
|
212
|
+
* total number of compressed bytes read from the input, rather than the
|
|
213
|
+
* number of uncompressed bytes.
|
|
214
|
+
*/
|
|
215
|
+
private static class TrackingGZIPInputStream extends TrackingInputStream {
|
|
216
|
+
private ExposedGZIPInputStream gzin;
|
|
217
|
+
public TrackingGZIPInputStream(final ExposedGZIPInputStream gzin) throws IOException {
|
|
218
|
+
super(gzin);
|
|
219
|
+
this.gzin = gzin;
|
|
220
|
+
}
|
|
221
|
+
public long getTotalRawBytesRead() {
|
|
222
|
+
return gzin.getInflater().getBytesRead();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Provides simple total-bytes-read tracking for an existing InputStream
|
|
228
|
+
*/
|
|
229
|
+
private static class SimpleTrackingInputStream extends TrackingInputStream {
|
|
230
|
+
private long bytesRead = 0;
|
|
231
|
+
public SimpleTrackingInputStream(InputStream stream) {
|
|
232
|
+
super(stream);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private int updateBytesRead(int newBytesRead) {
|
|
236
|
+
if (newBytesRead != -1) {
|
|
237
|
+
bytesRead += newBytesRead;
|
|
238
|
+
}
|
|
239
|
+
return newBytesRead;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
@Override
|
|
243
|
+
public int read() throws IOException {
|
|
244
|
+
return updateBytesRead(super.read());
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Note: FilterInputStream delegates read(byte[] bytes) to the below method,
|
|
248
|
+
// so we don't override it or else double count (CB-5631).
|
|
249
|
+
@Override
|
|
250
|
+
public int read(byte[] bytes, int offset, int count) throws IOException {
|
|
251
|
+
return updateBytesRead(super.read(bytes, offset, count));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
public long getTotalRawBytesRead() {
|
|
255
|
+
return bytesRead;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private boolean download(final String source, final File file, final JSONObject headers, final ProgressEvent progress, final CallbackContext callbackContext) {
|
|
260
|
+
Log.d(LOG_TAG, "download " + source);
|
|
261
|
+
|
|
262
|
+
if (!Patterns.WEB_URL.matcher(source).matches()) {
|
|
263
|
+
sendErrorMessage("Invalid URL", INVALID_URL_ERROR, callbackContext);
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
final CordovaResourceApi resourceApi = webView.getResourceApi();
|
|
268
|
+
final Uri sourceUri = resourceApi.remapUri(Uri.parse(source));
|
|
269
|
+
|
|
270
|
+
int uriType = CordovaResourceApi.getUriType(sourceUri);
|
|
271
|
+
final boolean useHttps = uriType == CordovaResourceApi.URI_TYPE_HTTPS;
|
|
272
|
+
final boolean isLocalTransfer = !useHttps && uriType != CordovaResourceApi.URI_TYPE_HTTP;
|
|
273
|
+
|
|
274
|
+
synchronized (progress) {
|
|
275
|
+
if (progress.isAborted()) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
HttpURLConnection connection = null;
|
|
280
|
+
PluginResult result = null;
|
|
281
|
+
TrackingInputStream inputStream = null;
|
|
282
|
+
boolean cached = false;
|
|
283
|
+
boolean retval = true;
|
|
284
|
+
|
|
285
|
+
OutputStream outputStream = null;
|
|
286
|
+
try {
|
|
287
|
+
OpenForReadResult readResult = null;
|
|
288
|
+
final Uri targetUri = resourceApi.remapUri(Uri.fromFile(file));
|
|
289
|
+
|
|
290
|
+
progress.setTargetFile(file);
|
|
291
|
+
progress.setStatus(STATUS_DOWNLOADING);
|
|
292
|
+
|
|
293
|
+
Log.d(LOG_TAG, "Download file: " + sourceUri);
|
|
294
|
+
Log.d(LOG_TAG, "Target file: " + file);
|
|
295
|
+
Log.d(LOG_TAG, "size = " + file.length());
|
|
296
|
+
|
|
297
|
+
if (isLocalTransfer) {
|
|
298
|
+
readResult = resourceApi.openForRead(sourceUri);
|
|
299
|
+
if (readResult.length != -1) {
|
|
300
|
+
progress.setTotal(readResult.length);
|
|
301
|
+
}
|
|
302
|
+
inputStream = new SimpleTrackingInputStream(readResult.inputStream);
|
|
303
|
+
} else {
|
|
304
|
+
// connect to server
|
|
305
|
+
// Open a HTTP connection to the URL based on protocol
|
|
306
|
+
connection = resourceApi.createHttpConnection(sourceUri);
|
|
307
|
+
connection.setRequestMethod("GET");
|
|
308
|
+
|
|
309
|
+
// TODO: Make OkHttp use this CookieManager by default.
|
|
310
|
+
String cookie = getCookies(sourceUri.toString());
|
|
311
|
+
|
|
312
|
+
if(cookie != null)
|
|
313
|
+
{
|
|
314
|
+
connection.setRequestProperty("cookie", cookie);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// This must be explicitly set for gzip progress tracking to work.
|
|
318
|
+
connection.setRequestProperty("Accept-Encoding", "gzip");
|
|
319
|
+
|
|
320
|
+
// Handle the other headers
|
|
321
|
+
if (headers != null) {
|
|
322
|
+
addHeadersToRequest(connection, headers);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
connection.connect();
|
|
326
|
+
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
|
327
|
+
cached = true;
|
|
328
|
+
connection.disconnect();
|
|
329
|
+
sendErrorMessage("Resource not modified: " + source, CONNECTION_ERROR, callbackContext, connection.getResponseCode());
|
|
330
|
+
retval = false;
|
|
331
|
+
return retval;
|
|
332
|
+
} else {
|
|
333
|
+
if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip")) {
|
|
334
|
+
// Only trust content-length header if we understand
|
|
335
|
+
// the encoding -- identity or gzip
|
|
336
|
+
int connectionLength = connection.getContentLength();
|
|
337
|
+
if (connectionLength != -1) {
|
|
338
|
+
if (connectionLength > getFreeSpace()) {
|
|
339
|
+
cached = true;
|
|
340
|
+
connection.disconnect();
|
|
341
|
+
sendErrorMessage("Not enough free space to download", CONNECTION_ERROR, callbackContext, connection.getResponseCode());
|
|
342
|
+
retval = false;
|
|
343
|
+
return retval;
|
|
344
|
+
} else {
|
|
345
|
+
progress.setTotal(connectionLength);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
inputStream = getInputStream(connection);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (!cached) {
|
|
354
|
+
try {
|
|
355
|
+
synchronized (progress) {
|
|
356
|
+
if (progress.isAborted()) {
|
|
357
|
+
retval = false;
|
|
358
|
+
return retval;
|
|
359
|
+
}
|
|
360
|
+
//progress.connection = connection;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// write bytes to file
|
|
364
|
+
byte[] buffer = new byte[MAX_BUFFER_SIZE];
|
|
365
|
+
int bytesRead = 0;
|
|
366
|
+
outputStream = resourceApi.openOutputStream(targetUri);
|
|
367
|
+
while ((bytesRead = inputStream.read(buffer)) > 0) {
|
|
368
|
+
synchronized (progress) {
|
|
369
|
+
if (progress.isAborted()) {
|
|
370
|
+
retval = false;
|
|
371
|
+
return retval;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
Log.d(LOG_TAG, "bytes read = " + bytesRead);
|
|
375
|
+
outputStream.write(buffer, 0, bytesRead);
|
|
376
|
+
// Send a progress event.
|
|
377
|
+
progress.setLoaded(inputStream.getTotalRawBytesRead());
|
|
378
|
+
|
|
379
|
+
updateProgress(callbackContext, progress);
|
|
380
|
+
}
|
|
381
|
+
} finally {
|
|
382
|
+
synchronized (progress) {
|
|
383
|
+
//progress.connection = null;
|
|
384
|
+
}
|
|
385
|
+
safeClose(inputStream);
|
|
386
|
+
safeClose(outputStream);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
} catch (Throwable e) {
|
|
391
|
+
try {
|
|
392
|
+
retval = false;
|
|
393
|
+
sendErrorMessage(e.getLocalizedMessage(), CONNECTION_ERROR, callbackContext, connection.getResponseCode());
|
|
394
|
+
} catch (IOException ioe) {
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return retval;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
private void sendErrorMessage(String message, int type, CallbackContext callbackContext) {
|
|
402
|
+
sendErrorMessage(message, type, callbackContext, -1);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
private void sendErrorMessage(String message, int type, CallbackContext callbackContext, int httpResponseCode) {
|
|
406
|
+
Log.e(LOG_TAG, message);
|
|
407
|
+
JSONObject error = new JSONObject();
|
|
408
|
+
try {
|
|
409
|
+
error.put("type", type);
|
|
410
|
+
error.put("responseCode", httpResponseCode);
|
|
411
|
+
} catch (JSONException e) {
|
|
412
|
+
}
|
|
413
|
+
callbackContext.error(error);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
private long getFreeSpace() {
|
|
417
|
+
File path = Environment.getDataDirectory();
|
|
418
|
+
StatFs stat = new StatFs(path.getPath());
|
|
419
|
+
long blockSize = stat.getBlockSize();
|
|
420
|
+
long availableBlocks = stat.getAvailableBlocks();
|
|
421
|
+
return availableBlocks * blockSize;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
private ProgressEvent createProgressEvent(String id) {
|
|
425
|
+
ProgressEvent progress = new ProgressEvent();
|
|
426
|
+
synchronized (activeRequests) {
|
|
427
|
+
activeRequests.put(id, progress);
|
|
428
|
+
}
|
|
429
|
+
return progress;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private void sync(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
|
433
|
+
// get args
|
|
434
|
+
final String src = args.getString(0);
|
|
435
|
+
final String id = args.getString(1);
|
|
436
|
+
final JSONObject headers;
|
|
437
|
+
if (args.optJSONObject(3) != null) {
|
|
438
|
+
headers = args.optJSONObject(3);
|
|
439
|
+
} else {
|
|
440
|
+
headers = new JSONObject();
|
|
441
|
+
}
|
|
442
|
+
final boolean copyCordovaAssets;
|
|
443
|
+
final boolean copyRootApp = args.getBoolean(5);
|
|
444
|
+
if (copyRootApp) {
|
|
445
|
+
copyCordovaAssets = true;
|
|
446
|
+
} else {
|
|
447
|
+
copyCordovaAssets = args.getBoolean(4);
|
|
448
|
+
}
|
|
449
|
+
final String manifestFile = args.getString(8);
|
|
450
|
+
Log.d(LOG_TAG, "sync called with id = " + id + " and src = " + src + "!");
|
|
451
|
+
|
|
452
|
+
final ProgressEvent progress = createProgressEvent(id);
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* need to clear cache or Android won't pick up on the replaced
|
|
456
|
+
* content
|
|
457
|
+
*/
|
|
458
|
+
cordova.getActivity().runOnUiThread(new Runnable() {
|
|
459
|
+
public void run() {
|
|
460
|
+
webView.clearCache(true);
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
cordova.getThreadPool().execute(new Runnable() {
|
|
465
|
+
public void run() {
|
|
466
|
+
synchronized (progress) {
|
|
467
|
+
if (progress.isAborted()) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
String outputDirectory = getOutputDirectory(id);
|
|
473
|
+
|
|
474
|
+
// Check to see if we should just return the cached version
|
|
475
|
+
String type = args.optString(2, TYPE_REPLACE);
|
|
476
|
+
Log.d(LOG_TAG, "type = " + type);
|
|
477
|
+
File dir = new File(outputDirectory);
|
|
478
|
+
Log.d(LOG_TAG, "dir = " + dir.exists());
|
|
479
|
+
|
|
480
|
+
if (type.equals(TYPE_LOCAL) && hasAppBeenUpdated()) {
|
|
481
|
+
savePrefs();
|
|
482
|
+
|
|
483
|
+
if ("null".equals(src) && (copyRootApp || copyCordovaAssets)) {
|
|
484
|
+
if (copyRootApp) {
|
|
485
|
+
Log.d(LOG_TAG, "doing copy root app");
|
|
486
|
+
copyRootApp(outputDirectory, manifestFile);
|
|
487
|
+
}
|
|
488
|
+
if (copyCordovaAssets) {
|
|
489
|
+
Log.d(LOG_TAG, "doing copy cordova app");
|
|
490
|
+
copyCordovaAssets(outputDirectory);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
} else {
|
|
494
|
+
type = TYPE_REPLACE;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (!dir.exists()) {
|
|
499
|
+
dir.mkdirs();
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (!type.equals(TYPE_LOCAL)) {
|
|
503
|
+
// download file
|
|
504
|
+
if (download(src, createDownloadFileLocation(id), headers, progress, callbackContext)) {
|
|
505
|
+
// update progress with zip file
|
|
506
|
+
File targetFile = progress.getTargetFile();
|
|
507
|
+
Log.d(LOG_TAG, "downloaded = " + targetFile.getAbsolutePath());
|
|
508
|
+
|
|
509
|
+
// Backup existing directory
|
|
510
|
+
File backup = backupExistingDirectory(outputDirectory, type, dir);
|
|
511
|
+
|
|
512
|
+
// @TODO: Do we do this even when type is local?
|
|
513
|
+
if (copyRootApp) {
|
|
514
|
+
copyRootApp(outputDirectory, manifestFile);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
boolean win = false;
|
|
518
|
+
if (isZipFile(targetFile)) {
|
|
519
|
+
win = unzipSync(targetFile, outputDirectory, progress, callbackContext);
|
|
520
|
+
} else {
|
|
521
|
+
// copy file to ID
|
|
522
|
+
win = targetFile.renameTo(new File(outputDirectory));
|
|
523
|
+
progress.setLoaded(1);
|
|
524
|
+
progress.setTotal(1);
|
|
525
|
+
progress.setStatus(STATUS_EXTRACTING);
|
|
526
|
+
progress.updatePercentage();
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// delete temp file
|
|
530
|
+
targetFile.delete();
|
|
531
|
+
|
|
532
|
+
if (copyCordovaAssets) {
|
|
533
|
+
copyCordovaAssets(outputDirectory);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (win) {
|
|
537
|
+
// success, remove backup
|
|
538
|
+
removeFolder(backup);
|
|
539
|
+
} else {
|
|
540
|
+
// failure, revert backup
|
|
541
|
+
removeFolder(dir);
|
|
542
|
+
backup.renameTo(dir);
|
|
543
|
+
}
|
|
544
|
+
} else {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// complete
|
|
550
|
+
synchronized (activeRequests) {
|
|
551
|
+
activeRequests.remove(id);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// Send last progress event
|
|
555
|
+
progress.setStatus(STATUS_COMPLETE);
|
|
556
|
+
updateProgress(callbackContext, progress);
|
|
557
|
+
|
|
558
|
+
// Send completion message
|
|
559
|
+
try {
|
|
560
|
+
JSONObject result = new JSONObject();
|
|
561
|
+
result.put(PROP_LOCAL_PATH, outputDirectory);
|
|
562
|
+
|
|
563
|
+
if (dir.list() != null) {
|
|
564
|
+
Log.d(LOG_TAG, "size of output dir = " + dir.list().length);
|
|
565
|
+
}
|
|
566
|
+
boolean cached = false;
|
|
567
|
+
if (type.equals(TYPE_LOCAL) && dir.exists() && dir.isDirectory() && dir.list() != null && dir.list().length > 0) {
|
|
568
|
+
Log.d(LOG_TAG, "we have a dir with some files in it.");
|
|
569
|
+
cached = true;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
result.put(PROP_CACHED, cached);
|
|
573
|
+
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
|
|
574
|
+
} catch (JSONException e) {
|
|
575
|
+
// never happens
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
private void savePrefs() {
|
|
582
|
+
SharedPreferences.Editor editor = cordova.getActivity().getSharedPreferences(cordova.getActivity().getPackageName(), 0).edit();
|
|
583
|
+
editor.putInt(PREVIOUS_VERSION, getCurrentAppVersion());
|
|
584
|
+
editor.commit();
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
private boolean hasAppBeenUpdated() {
|
|
588
|
+
Activity activity = cordova.getActivity();
|
|
589
|
+
|
|
590
|
+
int currentAppVersion = -1;
|
|
591
|
+
|
|
592
|
+
SharedPreferences settings = activity.getSharedPreferences(activity.getPackageName(), 0);
|
|
593
|
+
int previousAppVersion = settings.getInt(PREVIOUS_VERSION, -1);
|
|
594
|
+
|
|
595
|
+
currentAppVersion = getCurrentAppVersion();
|
|
596
|
+
|
|
597
|
+
Log.d(LOG_TAG, "current = " + currentAppVersion);
|
|
598
|
+
Log.d(LOG_TAG, "previous = " + previousAppVersion);
|
|
599
|
+
|
|
600
|
+
return currentAppVersion > previousAppVersion ? true : false;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
private int getCurrentAppVersion() {
|
|
604
|
+
Activity activity = cordova.getActivity();
|
|
605
|
+
int currentAppVersion = -1;
|
|
606
|
+
try {
|
|
607
|
+
currentAppVersion = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).versionCode;
|
|
608
|
+
} catch (PackageManager.NameNotFoundException e) {
|
|
609
|
+
// ignore
|
|
610
|
+
}
|
|
611
|
+
return currentAppVersion;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
private boolean isZipFile(File targetFile) {
|
|
615
|
+
boolean success = true;
|
|
616
|
+
try {
|
|
617
|
+
ZipFile zip = new ZipFile(targetFile);
|
|
618
|
+
Log.d(LOG_TAG, "seems like a zip file");
|
|
619
|
+
} catch (IOException e) {
|
|
620
|
+
Log.d(LOG_TAG, "not a zip file");
|
|
621
|
+
success = false;
|
|
622
|
+
}
|
|
623
|
+
return success;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
private String getOutputDirectory(final String id) {
|
|
627
|
+
// Production
|
|
628
|
+
String outputDirectory = cordova.getActivity().getFilesDir().getAbsolutePath();
|
|
629
|
+
// Testing
|
|
630
|
+
//String outputDirectory = cordova.getActivity().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
|
|
631
|
+
outputDirectory += outputDirectory.endsWith(File.separator) ? "" : File.separator;
|
|
632
|
+
outputDirectory += id;
|
|
633
|
+
Log.d(LOG_TAG, "output dir = " + outputDirectory);
|
|
634
|
+
|
|
635
|
+
return outputDirectory;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
private File createDownloadFileLocation(final String id) {
|
|
640
|
+
File file = null;
|
|
641
|
+
try {
|
|
642
|
+
String tempId = (id.lastIndexOf("/") > -1) ? id.substring(id.lastIndexOf("/")+1, id.length()) : id;
|
|
643
|
+
file = File.createTempFile(("cdv_" + tempId), ".tmp", cordova.getActivity().getCacheDir());
|
|
644
|
+
} catch (IOException e1) {
|
|
645
|
+
Log.e(LOG_TAG, e1.getLocalizedMessage(), e1);
|
|
646
|
+
}
|
|
647
|
+
return file;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
private File backupExistingDirectory(String outputDirectory, String type, File dir) {
|
|
651
|
+
File backup = new File(outputDirectory + ".bak");
|
|
652
|
+
if (dir.exists()) {
|
|
653
|
+
if (type.equals(TYPE_MERGE)) {
|
|
654
|
+
try {
|
|
655
|
+
copyFolder(dir, backup);
|
|
656
|
+
} catch (IOException e) {
|
|
657
|
+
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
|
|
658
|
+
}
|
|
659
|
+
} else {
|
|
660
|
+
dir.renameTo(backup);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
return backup;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
private void copyRootApp(String outputDirectory, String manifestFile) {
|
|
667
|
+
boolean wwwExists = (new File(outputDirectory, "www")).exists();
|
|
668
|
+
boolean copied = false;
|
|
669
|
+
if (manifestFile != null && !"".equals(manifestFile)) {
|
|
670
|
+
Log.d(LOG_TAG, "Manifest copy");
|
|
671
|
+
try {
|
|
672
|
+
copyRootAppByManifest(outputDirectory, manifestFile, wwwExists);
|
|
673
|
+
copied = true;
|
|
674
|
+
} catch (Exception e) {
|
|
675
|
+
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
if (!copied) {
|
|
679
|
+
Log.d(LOG_TAG, "Long copy");
|
|
680
|
+
try {
|
|
681
|
+
copyAssetFileOrDir(outputDirectory, "www", wwwExists);
|
|
682
|
+
} catch (IOException e) {
|
|
683
|
+
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
private void copyRootAppByManifest(String outputDirectory, String manifestFile, boolean wwwExists) throws IOException, JSONException {
|
|
689
|
+
File fp = new File(outputDirectory);
|
|
690
|
+
if (!fp.exists()) {
|
|
691
|
+
fp.mkdirs();
|
|
692
|
+
}
|
|
693
|
+
InputStream is = cordova.getActivity().getAssets().open("www/" + manifestFile);
|
|
694
|
+
int size = is.available();
|
|
695
|
+
byte[] buffer = new byte[size];
|
|
696
|
+
is.read(buffer);
|
|
697
|
+
is.close();
|
|
698
|
+
JSONObject obj = new JSONObject(new String(buffer, "UTF-8"));
|
|
699
|
+
JSONArray files = obj.getJSONArray("files");
|
|
700
|
+
for (int i=0; i<files.length(); i++) {
|
|
701
|
+
Log.d(LOG_TAG, "file = " + files.getString(i));
|
|
702
|
+
copyAssetFile(outputDirectory, "www/" + files.getString(i), wwwExists);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
private void copyCordovaAssets(String outputDirectory) {
|
|
707
|
+
try {
|
|
708
|
+
boolean wwwExists = (new File(outputDirectory, "www")).exists();
|
|
709
|
+
|
|
710
|
+
// cordova.js
|
|
711
|
+
this.copyAssetFile(outputDirectory, "www/cordova.js", wwwExists);
|
|
712
|
+
|
|
713
|
+
// cordova_plugins.js
|
|
714
|
+
this.copyAssetFile(outputDirectory, "www/cordova_plugins.js", wwwExists);
|
|
715
|
+
|
|
716
|
+
// plugins folder
|
|
717
|
+
this.copyAssetFileOrDir(outputDirectory, "www/plugins", wwwExists);
|
|
718
|
+
} catch(IOException e) {
|
|
719
|
+
Log.e(LOG_TAG, "Failed to copy asset file", e);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
private void copyAssetFileOrDir(String outputDirectory, String path, boolean wwwExists) throws IOException {
|
|
724
|
+
if (path.contains(".")) {
|
|
725
|
+
try {
|
|
726
|
+
this.copyAssetFile(outputDirectory, path, wwwExists);
|
|
727
|
+
} catch (IOException e) {
|
|
728
|
+
copyAssetDir(outputDirectory, path, wwwExists);
|
|
729
|
+
}
|
|
730
|
+
} else {
|
|
731
|
+
copyAssetDir(outputDirectory, path, wwwExists);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
private void copyAssetDir(String outputDirectory, String path, boolean wwwExists) throws IOException {
|
|
736
|
+
String assets[] = cordova.getActivity().getAssets().list(path);
|
|
737
|
+
if (assets.length != 0) {
|
|
738
|
+
for (String file : assets) {
|
|
739
|
+
copyAssetFileOrDir(outputDirectory, path + File.separator + file, wwwExists);
|
|
740
|
+
}
|
|
741
|
+
} else {
|
|
742
|
+
this.copyAssetFile(outputDirectory, path, wwwExists);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
private static TrackingInputStream getInputStream(URLConnection conn) throws IOException {
|
|
747
|
+
String encoding = conn.getContentEncoding();
|
|
748
|
+
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
|
|
749
|
+
return new TrackingGZIPInputStream(new ExposedGZIPInputStream(conn.getInputStream()));
|
|
750
|
+
}
|
|
751
|
+
return new SimpleTrackingInputStream(conn.getInputStream());
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
private static void safeClose(Closeable stream) {
|
|
755
|
+
if (stream != null) {
|
|
756
|
+
try {
|
|
757
|
+
stream.close();
|
|
758
|
+
} catch (IOException e) {
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
private String getCookies(final String target) {
|
|
764
|
+
boolean gotCookie = false;
|
|
765
|
+
String cookie = null;
|
|
766
|
+
Class webViewClass = webView.getClass();
|
|
767
|
+
try {
|
|
768
|
+
Method gcmMethod = webViewClass.getMethod("getCookieManager");
|
|
769
|
+
Class iccmClass = gcmMethod.getReturnType();
|
|
770
|
+
Method gcMethod = iccmClass.getMethod("getCookie", String.class);
|
|
771
|
+
|
|
772
|
+
cookie = (String)gcMethod.invoke(
|
|
773
|
+
iccmClass.cast(
|
|
774
|
+
gcmMethod.invoke(webView)
|
|
775
|
+
), target);
|
|
776
|
+
|
|
777
|
+
gotCookie = true;
|
|
778
|
+
} catch (NoSuchMethodException e) {
|
|
779
|
+
} catch (IllegalAccessException e) {
|
|
780
|
+
} catch (InvocationTargetException e) {
|
|
781
|
+
} catch (ClassCastException e) {
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
if (!gotCookie) {
|
|
785
|
+
cookie = CookieManager.getInstance().getCookie(target);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
return cookie;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
private static void addHeadersToRequest(URLConnection connection, JSONObject headers) {
|
|
792
|
+
try {
|
|
793
|
+
for (Iterator<?> iter = headers.keys(); iter.hasNext(); ) {
|
|
794
|
+
String headerKey = iter.next().toString();
|
|
795
|
+
JSONArray headerValues = headers.optJSONArray(headerKey);
|
|
796
|
+
if (headerValues == null) {
|
|
797
|
+
headerValues = new JSONArray();
|
|
798
|
+
headerValues.put(headers.getString(headerKey));
|
|
799
|
+
}
|
|
800
|
+
connection.setRequestProperty(headerKey, headerValues.getString(0));
|
|
801
|
+
for (int i = 1; i < headerValues.length(); ++i) {
|
|
802
|
+
connection.addRequestProperty(headerKey, headerValues.getString(i));
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
} catch (JSONException e1) {
|
|
806
|
+
// No headers to be manipulated!
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/* Unzip code */
|
|
811
|
+
|
|
812
|
+
// Can't use DataInputStream because it has the wrong endian-ness.
|
|
813
|
+
private static int readInt(InputStream is) throws IOException {
|
|
814
|
+
int a = is.read();
|
|
815
|
+
int b = is.read();
|
|
816
|
+
int c = is.read();
|
|
817
|
+
int d = is.read();
|
|
818
|
+
return a | b << 8 | c << 16 | d << 24;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
private boolean unzipSync(File targetFile, String outputDirectory, ProgressEvent progress, CallbackContext callbackContext) {
|
|
822
|
+
Log.d(LOG_TAG, "unzipSync called");
|
|
823
|
+
Log.d(LOG_TAG, "zip = " + targetFile.getAbsolutePath());
|
|
824
|
+
InputStream inputStream = null;
|
|
825
|
+
ZipFile zip = null;
|
|
826
|
+
boolean anyEntries = false;
|
|
827
|
+
try {
|
|
828
|
+
synchronized (progress) {
|
|
829
|
+
if (progress.isAborted()) {
|
|
830
|
+
return false;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
zip = new ZipFile(targetFile);
|
|
835
|
+
|
|
836
|
+
// Since Cordova 3.3.0 and release of File plugins, files are accessed via cdvfile://
|
|
837
|
+
// Accept a path or a URI for the source zip.
|
|
838
|
+
Uri zipUri = getUriForArg(targetFile.getAbsolutePath());
|
|
839
|
+
Uri outputUri = getUriForArg(outputDirectory);
|
|
840
|
+
|
|
841
|
+
CordovaResourceApi resourceApi = webView.getResourceApi();
|
|
842
|
+
|
|
843
|
+
File tempFile = resourceApi.mapUriToFile(zipUri);
|
|
844
|
+
if (tempFile == null || !tempFile.exists()) {
|
|
845
|
+
sendErrorMessage("Zip file does not exist", UNZIP_ERROR, callbackContext);
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
File outputDir = resourceApi.mapUriToFile(outputUri);
|
|
849
|
+
outputDirectory = outputDir.getAbsolutePath();
|
|
850
|
+
outputDirectory += outputDirectory.endsWith(File.separator) ? "" : File.separator;
|
|
851
|
+
if (outputDir == null || (!outputDir.exists() && !outputDir.mkdirs())){
|
|
852
|
+
sendErrorMessage("Could not create output directory", UNZIP_ERROR, callbackContext);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
OpenForReadResult zipFile = resourceApi.openForRead(zipUri);
|
|
856
|
+
progress.setStatus(STATUS_EXTRACTING);
|
|
857
|
+
progress.setLoaded(0);
|
|
858
|
+
progress.setTotal(zip.size());
|
|
859
|
+
Log.d(LOG_TAG, "zip file len = " + zip.size());
|
|
860
|
+
|
|
861
|
+
inputStream = new BufferedInputStream(zipFile.inputStream);
|
|
862
|
+
inputStream.mark(10);
|
|
863
|
+
int magic = readInt(inputStream);
|
|
864
|
+
|
|
865
|
+
if (magic != 875721283) { // CRX identifier
|
|
866
|
+
inputStream.reset();
|
|
867
|
+
} else {
|
|
868
|
+
// CRX files contain a header. This header consists of:
|
|
869
|
+
// * 4 bytes of magic number
|
|
870
|
+
// * 4 bytes of CRX format version,
|
|
871
|
+
// * 4 bytes of public key length
|
|
872
|
+
// * 4 bytes of signature length
|
|
873
|
+
// * the public key
|
|
874
|
+
// * the signature
|
|
875
|
+
// and then the ordinary zip data follows. We skip over the header before creating the ZipInputStream.
|
|
876
|
+
readInt(inputStream); // version == 2.
|
|
877
|
+
int pubkeyLength = readInt(inputStream);
|
|
878
|
+
int signatureLength = readInt(inputStream);
|
|
879
|
+
|
|
880
|
+
inputStream.skip(pubkeyLength + signatureLength);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// The inputstream is now pointing at the start of the actual zip file content.
|
|
884
|
+
ZipInputStream zis = new ZipInputStream(inputStream);
|
|
885
|
+
inputStream = zis;
|
|
886
|
+
|
|
887
|
+
ZipEntry ze;
|
|
888
|
+
byte[] buffer = new byte[32 * 1024];
|
|
889
|
+
|
|
890
|
+
while ((ze = zis.getNextEntry()) != null) {
|
|
891
|
+
synchronized (progress) {
|
|
892
|
+
if (progress.isAborted()) {
|
|
893
|
+
return false;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
anyEntries = true;
|
|
898
|
+
String compressedName = ze.getName();
|
|
899
|
+
|
|
900
|
+
if (ze.getSize() > getFreeSpace()) {
|
|
901
|
+
return false;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
if (ze.isDirectory()) {
|
|
905
|
+
File dir = new File(outputDirectory + compressedName);
|
|
906
|
+
dir.mkdirs();
|
|
907
|
+
} else {
|
|
908
|
+
File file = new File(outputDirectory + compressedName);
|
|
909
|
+
file.getParentFile().mkdirs();
|
|
910
|
+
if(file.exists() || file.createNewFile()){
|
|
911
|
+
Log.w(LOG_TAG, "extracting: " + file.getPath());
|
|
912
|
+
FileOutputStream fout = new FileOutputStream(file);
|
|
913
|
+
int count;
|
|
914
|
+
while ((count = zis.read(buffer)) != -1)
|
|
915
|
+
{
|
|
916
|
+
fout.write(buffer, 0, count);
|
|
917
|
+
}
|
|
918
|
+
fout.close();
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
}
|
|
922
|
+
progress.addLoaded(1);
|
|
923
|
+
updateProgress(callbackContext, progress);
|
|
924
|
+
zis.closeEntry();
|
|
925
|
+
}
|
|
926
|
+
} catch (Exception e) {
|
|
927
|
+
String errorMessage = "An error occurred while unzipping.";
|
|
928
|
+
sendErrorMessage(errorMessage, UNZIP_ERROR, callbackContext);
|
|
929
|
+
Log.e(LOG_TAG, errorMessage, e);
|
|
930
|
+
} finally {
|
|
931
|
+
if (inputStream != null) {
|
|
932
|
+
try {
|
|
933
|
+
inputStream.close();
|
|
934
|
+
} catch (IOException e) {
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
if (zip != null) {
|
|
938
|
+
try {
|
|
939
|
+
zip.close();
|
|
940
|
+
} catch (IOException e) {
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
if (anyEntries)
|
|
946
|
+
return true;
|
|
947
|
+
else
|
|
948
|
+
return false;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
private void updateProgress(CallbackContext callbackContext, ProgressEvent progress) {
|
|
952
|
+
try {
|
|
953
|
+
if (progress.getLoaded() != progress.getTotal() || progress.getStatus() == STATUS_COMPLETE) {
|
|
954
|
+
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject());
|
|
955
|
+
pluginResult.setKeepCallback(true);
|
|
956
|
+
callbackContext.sendPluginResult(pluginResult);
|
|
957
|
+
}
|
|
958
|
+
} catch (JSONException e) {
|
|
959
|
+
// never happens
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
private Uri getUriForArg(String arg) {
|
|
964
|
+
Uri tmpTarget = Uri.parse(arg);
|
|
965
|
+
return webView.getResourceApi().remapUri(
|
|
966
|
+
tmpTarget.getScheme() != null ? tmpTarget : Uri.fromFile(new File(arg)));
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
private static class ProgressEvent {
|
|
970
|
+
private long loaded;
|
|
971
|
+
private long total;
|
|
972
|
+
private double percentage;
|
|
973
|
+
private int status;
|
|
974
|
+
private boolean aborted;
|
|
975
|
+
private File targetFile;
|
|
976
|
+
public ProgressEvent() {
|
|
977
|
+
this.status = STATUS_STOPPED;
|
|
978
|
+
}
|
|
979
|
+
public long getLoaded() {
|
|
980
|
+
return loaded;
|
|
981
|
+
}
|
|
982
|
+
public void setLoaded(long loaded) {
|
|
983
|
+
this.loaded = loaded;
|
|
984
|
+
updatePercentage();
|
|
985
|
+
}
|
|
986
|
+
public void addLoaded(long add) {
|
|
987
|
+
this.loaded += add;
|
|
988
|
+
updatePercentage();
|
|
989
|
+
}
|
|
990
|
+
public long getTotal() {
|
|
991
|
+
return total;
|
|
992
|
+
}
|
|
993
|
+
public void setTotal(long total) {
|
|
994
|
+
this.total = total;
|
|
995
|
+
updatePercentage();
|
|
996
|
+
}
|
|
997
|
+
public int getStatus() {
|
|
998
|
+
return status;
|
|
999
|
+
}
|
|
1000
|
+
public void setStatus(int status) {
|
|
1001
|
+
this.status = status;
|
|
1002
|
+
}
|
|
1003
|
+
public boolean isAborted() {
|
|
1004
|
+
return aborted;
|
|
1005
|
+
}
|
|
1006
|
+
public void setAborted(boolean aborted) {
|
|
1007
|
+
this.aborted = aborted;
|
|
1008
|
+
}
|
|
1009
|
+
public File getTargetFile() {
|
|
1010
|
+
return targetFile;
|
|
1011
|
+
}
|
|
1012
|
+
public void setTargetFile(File targetFile) {
|
|
1013
|
+
this.targetFile = targetFile;
|
|
1014
|
+
}
|
|
1015
|
+
public JSONObject toJSONObject() throws JSONException {
|
|
1016
|
+
JSONObject jsonProgress = new JSONObject();
|
|
1017
|
+
jsonProgress.put(PROP_PROGRESS, this.percentage);
|
|
1018
|
+
jsonProgress.put(PROP_STATUS, this.getStatus());
|
|
1019
|
+
jsonProgress.put(PROP_LOADED, this.getLoaded());
|
|
1020
|
+
jsonProgress.put(PROP_TOTAL, this.getTotal());
|
|
1021
|
+
return jsonProgress;
|
|
1022
|
+
|
|
1023
|
+
}
|
|
1024
|
+
private void updatePercentage() {
|
|
1025
|
+
double loaded = this.getLoaded();
|
|
1026
|
+
double total = this.getTotal();
|
|
1027
|
+
this.percentage = Math.floor((loaded / total * 100) / 2);
|
|
1028
|
+
if (this.getStatus() == STATUS_EXTRACTING) {
|
|
1029
|
+
this.percentage += 50;
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
private void copyFolder(File src, File dest) throws IOException{
|
|
1035
|
+
if(src.isDirectory()) {
|
|
1036
|
+
if(!dest.exists()){
|
|
1037
|
+
dest.mkdir();
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
//list all the directory contents
|
|
1041
|
+
String files[] = src.list();
|
|
1042
|
+
|
|
1043
|
+
for (String file : files) {
|
|
1044
|
+
//recursive copy
|
|
1045
|
+
copyFolder(new File(src, file), new File(dest, file));
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
} else {
|
|
1049
|
+
//if file, then copy it
|
|
1050
|
+
copyFile(new FileInputStream(src), new FileOutputStream(dest));
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
private void copyAssetFile(String outputDirectory, String filename, boolean wwwExists) throws IOException {
|
|
1055
|
+
String targetFile = filename;
|
|
1056
|
+
if (!wwwExists) {
|
|
1057
|
+
if (targetFile.startsWith("www/")) {
|
|
1058
|
+
targetFile = targetFile.substring(4,targetFile.length());
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
int lastIndex = targetFile.lastIndexOf("/");
|
|
1063
|
+
if (lastIndex > 0) {
|
|
1064
|
+
File targetDir = new File(outputDirectory + "/" + targetFile.substring(0, lastIndex));
|
|
1065
|
+
if (!targetDir.exists()) {
|
|
1066
|
+
targetDir.mkdirs();
|
|
1067
|
+
}
|
|
1068
|
+
} else {
|
|
1069
|
+
File targetDir = new File(outputDirectory);
|
|
1070
|
+
if (!targetDir.exists()) {
|
|
1071
|
+
targetDir.mkdirs();
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
copyFile(cordova.getActivity().getAssets().open(filename), new FileOutputStream(new File(outputDirectory, targetFile)));
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
private void copyFile(InputStream in, OutputStream out) throws IOException {
|
|
1079
|
+
byte[] buffer = new byte[4096];
|
|
1080
|
+
|
|
1081
|
+
int length;
|
|
1082
|
+
//copy the file content in bytes
|
|
1083
|
+
while ((length = in.read(buffer)) > 0){
|
|
1084
|
+
out.write(buffer, 0, length);
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
in.close();
|
|
1088
|
+
out.close();
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
private void removeFolder(File directory) {
|
|
1092
|
+
if (directory.exists() && directory.isDirectory()) {
|
|
1093
|
+
File[] files = directory.listFiles();
|
|
1094
|
+
if (files != null) {
|
|
1095
|
+
for (File file : files) {
|
|
1096
|
+
removeFolder(file);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
directory.delete();
|
|
1101
|
+
}
|
|
1102
|
+
}
|