Repository navigation
Expand file tree
/
Copy pathIonicDeploy.java
More file actions
668 lines (558 loc) · 22 KB
/
Copy pathIonicDeploy.java
File metadata and controls
668 lines (558 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package com.ionic.deploy;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
class JsonHttpResponse {
String message;
Boolean error;
JSONObject json;
}
public class IonicDeploy extends CordovaPlugin {
String server = "https://apps.ionic.io";
Context myContext = null;
String app_id = null;
boolean debug = true;
SharedPreferences prefs = null;
CordovaWebView v = null;
String version_label = null;
boolean ignore_deploy = false;
JSONObject last_update;
public static final String NO_DEPLOY_LABEL = "NO_DEPLOY_LABEL";
public static final String NO_DEPLOY_AVAILABLE = "NO_DEPLOY_AVAILABLE";
public static final String NOTHING_TO_IGNORE = "NOTHING_TO_IGNORE";
public static final int VERSION_AHEAD = 1;
public static final int VERSION_MATCH = 0;
public static final int VERSION_BEHIND = -1;
/**
* Sets the context of the Command. This can then be used to do things like
* get file paths associated with the Activity.
*
* @param cordova The context of the main Activity.
* @param webView The CordovaWebView Cordova is running in.
*/
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
this.myContext = this.cordova.getActivity().getApplicationContext();
this.prefs = getPreferences();
this.v = webView;
this.version_label = prefs.getString("ionicdeploy_version_label", IonicDeploy.NO_DEPLOY_LABEL);
this.initVersionChecks();
}
private String getUUID() {
return this.prefs.getString("uuid", IonicDeploy.NO_DEPLOY_AVAILABLE);
}
private String getUUID(String defaultUUID) {
return this.prefs.getString("uuid", defaultUUID);
}
private PackageInfo getAppPackageInfo() throws NameNotFoundException {
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0);
return packageInfo;
}
private void initVersionChecks() {
String ionicdeploy_version_label = IonicDeploy.NO_DEPLOY_LABEL;
String uuid = this.getUUID();
try {
ionicdeploy_version_label = this.constructVersionLabel(this.getAppPackageInfo(), uuid);
} catch (NameNotFoundException e) {
logMessage("INIT", "Could not get package info");
}
if(!ionicdeploy_version_label.equals(IonicDeploy.NO_DEPLOY_LABEL)) {
if(this.debug) {
logMessage("INIT", "Version Label 1: " + this.version_label);
logMessage("INIT", "Version Label 2: " + ionicdeploy_version_label);
}
if(!this.version_label.equals(ionicdeploy_version_label)) {
this.ignore_deploy = true;
this.updateVersionLabel(uuid);
this.prefs.edit().remove("uuid").apply();
}
}
}
private String constructVersionLabel(PackageInfo packageInfo, String uuid) {
String version = packageInfo.versionName;
String timestamp = String.valueOf(packageInfo.lastUpdateTime);
return version + ":" + timestamp + ":" + uuid;
}
private String[] deconstructVersionLabel(String label) {
return label.split(":");
}
public Object onMessage(String id, Object data) {
boolean is_nothing = "file:///".equals(String.valueOf(data));
boolean is_index = "file:///android_asset/www/index.html".equals(String.valueOf(data));
boolean is_original = (is_nothing || is_index) ? true : false;
if("onPageStarted".equals(id) && is_original) {
final String uuid = this.getUUID();
if(!IonicDeploy.NO_DEPLOY_AVAILABLE.equals(uuid)) {
logMessage("LOAD", "Init Deploy Version");
this.redirect(uuid, false);
}
}
return null;
}
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback id used when calling back into JavaScript.
* @return True if the action was valid, false if not.
*/
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
this.app_id = args.getString(0);
this.prefs = getPreferences();
initApp(args.getString(0));
final SharedPreferences prefs = this.prefs;
if (action.equals("initialize")) {
this.server = args.getString(1);
return true;
} else if (action.equals("check")) {
logMessage("CHECK", "Checking for updates");
final String channel_tag = args.getString(1);
cordova.getThreadPool().execute(new Runnable() {
public void run() {
checkForUpdates(callbackContext, channel_tag);
}
});
return true;
} else if (action.equals("download")) {
logMessage("DOWNLOAD", "Downloading updates");
cordova.getThreadPool().execute(new Runnable() {
public void run() {
downloadUpdate(callbackContext);
}
});
return true;
} else if (action.equals("extract")) {
logMessage("EXTRACT", "Extracting update");
final String uuid = this.getUUID("");
cordova.getThreadPool().execute(new Runnable() {
public void run() {
unzip("www.zip", uuid, callbackContext);
}
});
return true;
} else if (action.equals("redirect")) {
final String uuid = this.getUUID("");
this.redirect(uuid, true);
return true;
} else if (action.equals("info")) {
this.info(callbackContext);
return true;
} else {
return false;
}
}
private void info(CallbackContext callbackContext) {
JSONObject json = new JSONObject();
try {
json.put("deploy_uuid", this.getUUID());
json.put("binary_version", this.deconstructVersionLabel(this.version_label)[0]);
} catch (JSONException e) {
callbackContext.error("Unable to gather deploy info: " + e.toString());
}
callbackContext.success(json);
}
private void initApp(String app_id) {
this.app_id = app_id;
SharedPreferences prefs = this.prefs;
prefs.edit().putString("app_id", this.app_id).apply();
// Used for keeping track of the order versions were downloaded
int version_count = prefs.getInt("version_count", 0);
prefs.edit().putInt("version_count", version_count).apply();
}
private void checkForUpdates(CallbackContext callbackContext, final String channel_tag) {
this.last_update = null;
String ignore_version = this.prefs.getString("ionicdeploy_version_ignore", "");
String deployed_version = this.prefs.getString("uuid", "");
String loaded_version = this.prefs.getString("loaded_uuid", "");
JsonHttpResponse response = postDeviceDetails(deployed_version, channel_tag);
try {
if (response.json != null) {
Boolean compatible = Boolean.valueOf(response.json.getString("compatible_binary"));
Boolean updatesAvailable = Boolean.valueOf(response.json.getString("update_available"));
if(!compatible) {
logMessage("CHECK", "Refusing update due to incompatible binary version");
} else if(updatesAvailable) {
try {
JSONObject update = response.json.getJSONObject("update");
String update_uuid = update.getString("uuid");
if(!update_uuid.equals(ignore_version) && !update_uuid.equals(loaded_version)) {
prefs.edit().putString("upstream_uuid", update_uuid).apply();
this.last_update = update;
} else {
updatesAvailable = new Boolean(false);
}
} catch (JSONException e) {
callbackContext.error("Update information is not available");
}
}
if(updatesAvailable && compatible) {
callbackContext.success("true");
} else {
callbackContext.success("false");
}
} else {
logMessage("CHECK", "Unable to check for updates.");
callbackContext.success("false");
}
} catch (JSONException e) {
logMessage("CHECK", e.toString());
callbackContext.error("Error checking for updates.");
}
}
private void downloadUpdate(CallbackContext callbackContext) {
String upstream_uuid = this.prefs.getString("upstream_uuid", "");
if (upstream_uuid != "" && this.hasVersion(upstream_uuid)) {
// Set the current version to the upstream uuid
prefs.edit().putString("uuid", upstream_uuid).apply();
callbackContext.success("false");
} else {
try {
String url = this.last_update.getString("url");
final DownloadTask downloadTask = new DownloadTask(this.myContext, callbackContext);
downloadTask.execute(url);
} catch (JSONException e) {
logMessage("DOWNLOAD", e.toString());
callbackContext.error("Error fetching download");
}
}
}
/**
* Get a list of versions that have been downloaded
*
* @return
*/
private Set<String> getMyVersions() {
SharedPreferences prefs = this.prefs;
return prefs.getStringSet("my_versions", new HashSet<String>());
}
/**
* Check to see if we already have the version to be downloaded
*
* @param uuid
* @return
*/
private boolean hasVersion(String uuid) {
Set<String> versions = this.getMyVersions();
logMessage("HASVER", "Checking " + uuid + "...");
for (String version : versions) {
String[] version_string = version.split("\\|");
logMessage("HASVER", version_string[0] + " == " + uuid);
if (version_string[0].equals(uuid)) {
logMessage("HASVER", "Yes");
return true;
}
}
logMessage("HASVER", "No");
return false;
}
/**
* Save a new version string to our list of versions
*
* @param uuid
*/
private void saveVersion(String uuid) {
SharedPreferences prefs = this.prefs;
Integer version_count = prefs.getInt("version_count", 0) + 1;
prefs.edit().putInt("version_count", version_count).apply();
uuid = uuid + "|" + version_count.toString();
Set<String> versions = this.getMyVersions();
versions.add(uuid);
prefs.edit().putStringSet("my_versions", versions).apply();
this.cleanupVersions();
}
private void cleanupVersions() {
// Let's keep 5 versions around for now
SharedPreferences prefs = this.prefs;
int version_count = prefs.getInt("version_count", 0);
Set<String> versions = this.getMyVersions();
if (version_count > 3) {
int threshold = version_count - 3;
for (Iterator<String> i = versions.iterator(); i.hasNext();) {
String version = i.next();
String[] version_string = version.split("\\|");
logMessage("VERSION", version);
int version_number = Integer.parseInt(version_string[1]);
if (version_number < threshold) {
logMessage("REMOVING", version);
i.remove();
removeVersion(version_string[0]);
}
}
Integer version_c = versions.size();
logMessage("VERSIONCOUNT", version_c.toString());
prefs.edit().putStringSet("my_versions", versions).apply();
}
}
/**
* Ugly, lazy bit of code to whack old version directories...
*
* @param uuid
*/
private void removeVersion(String uuid) {
File versionDir = this.myContext.getDir(uuid, Context.MODE_PRIVATE);
if (versionDir.exists()) {
String deleteCmd = "rm -r " + versionDir.getAbsolutePath();
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) {
logMessage("REMOVE", "Failed to remove " + uuid + ". Error: " + e.getMessage());
}
}
}
private JsonHttpResponse postDeviceDetails(String uuid, final String channel_tag) {
String endpoint = "/api/v1/apps/" + this.app_id + "/updates/check/";
JsonHttpResponse response = new JsonHttpResponse();
JSONObject json = new JSONObject();
try {
json.put("device_app_version", this.deconstructVersionLabel(this.version_label)[0]);
json.put("device_deploy_uuid", uuid);
json.put("device_platform", "android");
json.put("channel_tag", channel_tag);
String params = json.toString();
byte[] postData = params.getBytes("UTF-8");
int postDataLength = postData.length;
URL url = new URL(this.server + endpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write( postData );
InputStream in = new BufferedInputStream(conn.getInputStream());
String result = readStream(in);
JSONObject jsonResponse = new JSONObject(result);
response.json = jsonResponse;
} catch (JSONException e) {
response.error = true;
} catch (MalformedURLException e) {
response.error = true;
} catch (IOException e) {
response.error = true;
}
return response;
}
private SharedPreferences getPreferences() {
// Request shared preferences for this app id
SharedPreferences prefs = this.myContext.getSharedPreferences("com.ionic.deploy.preferences", Context.MODE_PRIVATE);
return prefs;
}
private String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
private void logMessage(String tag, String message) {
if (this.debug == true) {
Log.i("IONIC.DEPLOY." + tag, message);
}
}
private void updateVersionLabel(String ignore_version) {
try {
String ionicdeploy_version_label = this.constructVersionLabel(this.getAppPackageInfo(), this.getUUID());
this.prefs.edit().putString("ionicdeploy_version_label", ionicdeploy_version_label).apply();
this.version_label = prefs.getString("ionicdeploy_version_label", IonicDeploy.NO_DEPLOY_LABEL);
this.prefs.edit().putString("ionicdeploy_version_ignore", ignore_version).apply();
} catch (NameNotFoundException e) {
logMessage("LABEL", "Could not get package info");
}
}
/**
* Extract the downloaded archive
*
* @param zip
* @param location
*/
private void unzip(String zip, String location, CallbackContext callbackContext) {
SharedPreferences prefs = getPreferences();
String upstream_uuid = prefs.getString("upstream_uuid", "");
logMessage("UNZIP", upstream_uuid);
this.ignore_deploy = false;
this.updateVersionLabel(IonicDeploy.NOTHING_TO_IGNORE);
if (upstream_uuid != "" && this.hasVersion(upstream_uuid)) {
callbackContext.success("done"); // we have already extracted this version
return;
}
try {
FileInputStream inputStream = this.myContext.openFileInput(zip);
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry zipEntry = null;
// Get the full path to the internal storage
String filesDir = this.myContext.getFilesDir().toString();
// Make the version directory in internal storage
File versionDir = this.myContext.getDir(location, Context.MODE_PRIVATE);
logMessage("UNZIP_DIR", versionDir.getAbsolutePath().toString());
// Figure out how many entries are in the zip so we can calculate extraction progress
ZipFile zipFile = new ZipFile(this.myContext.getFileStreamPath(zip).getAbsolutePath().toString());
float entries = new Float(zipFile.size());
logMessage("ENTRIES", "Total: " + (int) entries);
float extracted = 0.0f;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
File newFile = new File(versionDir + "/" + zipEntry.getName());
newFile.getParentFile().mkdirs();
byte[] buffer = new byte[2048];
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
BufferedOutputStream outputBuffer = new BufferedOutputStream(fileOutputStream, buffer.length);
int bits;
while((bits = zipInputStream.read(buffer, 0, buffer.length)) != -1) {
outputBuffer.write(buffer, 0, bits);
}
zipInputStream.closeEntry();
outputBuffer.flush();
outputBuffer.close();
extracted += 1;
float progress = (extracted / entries) * new Float("100.0f");
logMessage("EXTRACT", "Progress: " + (int) progress + "%");
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, (int) progress);
progressResult.setKeepCallback(true);
callbackContext.sendPluginResult(progressResult);
}
zipInputStream.close();
} catch(Exception e) {
//TODO Handle problems..
logMessage("UNZIP_STEP", "Exception: " + e.getMessage());
}
// Save the version we just downloaded as a version on hand
saveVersion(upstream_uuid);
String wwwFile = this.myContext.getFileStreamPath(zip).getAbsolutePath().toString();
if (this.myContext.getFileStreamPath(zip).exists()) {
String deleteCmd = "rm -r " + wwwFile;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
logMessage("REMOVE", "Removed www.zip");
} catch (IOException e) {
logMessage("REMOVE", "Failed to remove " + wwwFile + ". Error: " + e.getMessage());
}
}
callbackContext.success("done");
}
private void redirect(final String uuid, final boolean recreatePlugins) {
String ignore = this.prefs.getString("ionicdeploy_version_ignore", IonicDeploy.NOTHING_TO_IGNORE);
if (!uuid.equals("") && !this.ignore_deploy && !uuid.equals(ignore)) {
prefs.edit().putString("uuid", uuid).apply();
final File versionDir = this.myContext.getDir(uuid, Context.MODE_PRIVATE);
final String deploy_url = versionDir.toURI() + "index.html";
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
logMessage("REDIRECT", "Loading deploy version: " + uuid);
prefs.edit().putString("loaded_uuid", uuid).apply();
webView.loadUrlIntoView(deploy_url, recreatePlugins);
}
});
}
}
private class DownloadTask extends AsyncTask<String, Integer, String> {
private Context myContext;
private CallbackContext callbackContext;
public DownloadTask(Context context, CallbackContext callbackContext) {
this.myContext = context;
this.callbackContext = callbackContext;
}
@Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
FileOutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
float fileLength = new Float(connection.getContentLength());
logMessage("DOWNLOAD", "File size: " + fileLength);
// download the file
input = connection.getInputStream();
output = this.myContext.openFileOutput("www.zip", Context.MODE_PRIVATE);
byte data[] = new byte[4096];
float total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
// Send the current download progress to a callback
if (fileLength > 0) {
float progress = (total / fileLength) * new Float("100.0f");
logMessage("DOWNLOAD", "Progress: " + (int) progress + "%");
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, (int) progress);
progressResult.setKeepCallback(true);
callbackContext.sendPluginResult(progressResult);
}
}
} catch (Exception e) {
callbackContext.error("Something failed with the download...");
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
// Request shared preferences for this app id
SharedPreferences prefs = getPreferences();
// Set the saved uuid to the most recently acquired upstream_uuid
String uuid = prefs.getString("upstream_uuid", "");
prefs.edit().putString("uuid", uuid).apply();
callbackContext.success("true");
return null;
}
}
}