From 8ff71af4d2c3aee2136bd9cbf33ecf6d46e187ea Mon Sep 17 00:00:00 2001 From: martindonadieu Date: Tue, 28 Jun 2022 18:19:16 +0100 Subject: [PATCH] fix: build issue android --- .../capacitor_updater/CapacitorUpdater.java | 23 ++-- .../CapacitorUpdaterPlugin.java | 102 +++++++++--------- .../forgr/capacitor_updater/VersionInfo.java | 14 +-- 3 files changed, 69 insertions(+), 70 deletions(-) diff --git a/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java b/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java index 38a64c5c1..efb4fb68c 100644 --- a/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +++ b/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java @@ -58,12 +58,12 @@ public class CapacitorUpdater { public SharedPreferences.Editor editor; public SharedPreferences prefs; - private RequestQueue requestQueue; + public RequestQueue requestQueue; - private File documentsDir; - private String versionBuild = ""; - private String versionCode = ""; - private String versionOs = ""; + public File documentsDir; + public String versionBuild = ""; + public String versionCode = ""; + public String versionOs = ""; public String statsUrl = ""; public String appId = ""; @@ -106,7 +106,7 @@ private File unzip(final String folder, final File zipFile, final String dest) t ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { - final File file = new File(targetDirectory, entry.getFolder()); + final File file = new File(targetDirectory, entry.getName()); final String canonicalPath = file.getCanonicalPath(); final String canonicalDir = (new File(String.valueOf(targetDirectory))).getCanonicalPath(); final File dir = entry.isDirectory() ? file : file.getParentFile(); @@ -221,8 +221,7 @@ private void setCurrentBundle(final File bundle) { public VersionInfo download(final String url, final String versionName) throws IOException { final String folder = this.randomString(10); - VersionInfo info = new VersionInfo(folder, versionName, VersionStatus.DOWNLOADING, new Date(System.currentTimeMillis())); - this.saveVersionInfo(folder, info); + this.saveVersionInfo(folder, new VersionInfo(folder, versionName, VersionStatus.DOWNLOADING, new Date(System.currentTimeMillis()))); this.notifyDownload(folder, 0); final String path = this.randomString(10); final File zipFile = new File(this.documentsDir, path); @@ -248,7 +247,7 @@ public List list() { Log.d(TAG, "list File : " + destHot.getPath()); if (destHot.exists()) { for (final File i : destHot.listFiles()) { - final String folder = i.getFolder(); + final String folder = i.getName(); res.add(this.getVersionInfo(folder)); } } else { @@ -427,14 +426,14 @@ public VersionInfo getVersionInfo(String folder) { Log.d(TAG, "Getting info for [" + folder + "]"); VersionInfo result; if(VersionInfo.VERSION_BUILTIN.equals(folder)) { - result = new VersionInfo(folder, VersionStatus.SUCCESS, (String) null, ""); + result = new VersionInfo(folder, (String) null, VersionStatus.SUCCESS, ""); } else { try { String stored = this.prefs.getString(folder + INFO_SUFFIX, ""); result = VersionInfo.fromJSON(stored); } catch (JSONException e) { Log.e(TAG, "Failed to parse folder info for [" + folder + "] ", e); - result = new VersionInfo(folder, VersionStatus.PENDING, (String) null, ""); + result = new VersionInfo(folder, (String) null, VersionStatus.PENDING, ""); } } @@ -459,7 +458,7 @@ private void removeVersionInfo(final String folder) { private void saveVersionInfo(final String folder, final VersionInfo info) { if(folder == null || (info != null && (info.isBuiltin() || info.isUnknown()))) { Log.d(TAG, "Not saving info for folder: [" + folder + "] " + info); - return; + return; } if(info == null) { diff --git a/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java b/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java index e60f703cf..cd5682e5a 100644 --- a/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +++ b/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java @@ -169,7 +169,7 @@ public void getPluginVersion(final PluginCall call) { public void download(final PluginCall call) { final String url = call.getString("url"); final String versionName = call.getString("versionName"); - if (!url || !versionName) { + if (url == null || versionName == null) { call.reject("missing url or versionName"); return; } @@ -179,7 +179,7 @@ public void download(final PluginCall call) { @Override public void run() { try { - + final VersionInfo downloaded = CapacitorUpdaterPlugin.this.implementation.download(url, versionName); call.resolve(downloaded.toJSON()); } catch (final IOException e) { @@ -406,7 +406,7 @@ private void checkAppReady() { this.appReadyCheck = new Thread(new DeferredNotifyAppReadyCheck()); this.appReadyCheck.start(); } catch (final Exception e) { - Log.e(CapacitorUpdater.TAG, "Failed to start " + DeferredNotifyAppReadyCheck.class.getFolder(), e); + Log.e(CapacitorUpdater.TAG, "Failed to start " + DeferredNotifyAppReadyCheck.class.getName(), e); } } @@ -417,64 +417,64 @@ public void onActivityStarted(@NonNull final Activity activity) { @Override public void run() { - Log.i(CapacitorUpdater.TAG, "Check for update via: " + CapacitorUpdaterPlugin.this.autoUpdateUrl); - CapacitorUpdaterPlugin.this.implementation.getLatest(CapacitorUpdaterPlugin.this.autoUpdateUrl, (res) -> { - try { - if (res.has("message")) { - Log.i(CapacitorUpdater.TAG, "message: " + res.get("message")); - if (res.has("major") && res.getBoolean("major") && res.has("version")) { - final JSObject majorAvailable = new JSObject(); - majorAvailable.put("version", (String) res.get("version")); - CapacitorUpdaterPlugin.this.notifyListeners("majorAvailable", majorAvailable); - } - return; + Log.i(CapacitorUpdater.TAG, "Check for update via: " + CapacitorUpdaterPlugin.this.autoUpdateUrl); + CapacitorUpdaterPlugin.this.implementation.getLatest(CapacitorUpdaterPlugin.this.autoUpdateUrl, (res) -> { + try { + if (res.has("message")) { + Log.i(CapacitorUpdater.TAG, "message: " + res.get("message")); + if (res.has("major") && res.getBoolean("major") && res.has("version")) { + final JSObject majorAvailable = new JSObject(); + majorAvailable.put("version", (String) res.get("version")); + CapacitorUpdaterPlugin.this.notifyListeners("majorAvailable", majorAvailable); } - final VersionInfo current = CapacitorUpdaterPlugin.this.implementation.getCurrentBundle(); - final String latestVersionName = (String) res.get("version"); + return; + } + final VersionInfo current = CapacitorUpdaterPlugin.this.implementation.getCurrentBundle(); + final String latestVersionName = (String) res.get("version"); - if (latestVersionName != null && !"".equals(latestVersionName) && !current.getFolder().equals(latestVersionName)) { + if (latestVersionName != null && !"".equals(latestVersionName) && !current.getFolder().equals(latestVersionName)) { - final VersionInfo latest = CapacitorUpdaterPlugin.this.implementation.getVersionInfoByName(latestVersionName); - if(latest != null) { - if(latest.isErrorStatus()) { - Log.e(CapacitorUpdater.TAG, "Latest version already exists, and is in error state. Aborting update."); - return; - } - if(latest.isDownloaded()){ - Log.e(CapacitorUpdater.TAG, "Latest version already exists and download is NOT required. Update will occur next time app moves to background."); - CapacitorUpdaterPlugin.this.implementation.setNextVersion(latest.getFolder()); - return; - } + final VersionInfo latest = CapacitorUpdaterPlugin.this.implementation.getVersionInfoByName(latestVersionName); + if(latest != null) { + if(latest.isErrorStatus()) { + Log.e(CapacitorUpdater.TAG, "Latest version already exists, and is in error state. Aborting update."); + return; + } + if(latest.isDownloaded()){ + Log.e(CapacitorUpdater.TAG, "Latest version already exists and download is NOT required. Update will occur next time app moves to background."); + CapacitorUpdaterPlugin.this.implementation.setNextVersion(latest.getFolder()); + return; } + } - new Thread(new Runnable(){ - @Override - public void run() { - try { - Log.i(CapacitorUpdater.TAG, "New version: " + latestVersionName + " found. Current is: " + current.getVersionName() + ". Update will occur next time app moves to background."); + new Thread(new Runnable(){ + @Override + public void run() { + try { + Log.i(CapacitorUpdater.TAG, "New version: " + latestVersionName + " found. Current is: " + current.getVersionName() + ". Update will occur next time app moves to background."); - final String url = (String) res.get("url"); - final VersionInfo next = CapacitorUpdaterPlugin.this.implementation.download(url, latestVersionName); + final String url = (String) res.get("url"); + final VersionInfo next = CapacitorUpdaterPlugin.this.implementation.download(url, latestVersionName); - CapacitorUpdaterPlugin.this.implementation.setNextVersion(next.getFolder()); + CapacitorUpdaterPlugin.this.implementation.setNextVersion(next.getFolder()); - final JSObject updateAvailable = new JSObject(); - updateAvailable.put("version", next.toJSON()); - CapacitorUpdaterPlugin.this.notifyListeners("updateAvailable", updateAvailable); - } catch (final Exception e) { - Log.e(CapacitorUpdater.TAG, "error downloading file", e); - } + final JSObject updateAvailable = new JSObject(); + updateAvailable.put("version", next.toJSON()); + CapacitorUpdaterPlugin.this.notifyListeners("updateAvailable", updateAvailable); + } catch (final Exception e) { + Log.e(CapacitorUpdater.TAG, "error downloading file", e); } - }).start(); - } else { - Log.i(CapacitorUpdater.TAG, "No need to update, " + current + " is the latest version."); - } - } catch (final JSONException e) { - Log.e(CapacitorUpdater.TAG, "error parsing JSON", e); + } + }).start(); + } else { + Log.i(CapacitorUpdater.TAG, "No need to update, " + current + " is the latest version."); } - }); - } + } catch (final JSONException e) { + Log.e(CapacitorUpdater.TAG, "error parsing JSON", e); + } + }); + } }).start(); } @@ -601,7 +601,7 @@ public void run() { CapacitorUpdaterPlugin.this.appReadyCheck = null; } catch (final InterruptedException e) { - Log.e(CapacitorUpdater.TAG, DeferredNotifyAppReadyCheck.class.getFolder() + " was interrupted."); + Log.e(CapacitorUpdater.TAG, DeferredNotifyAppReadyCheck.class.getName() + " was interrupted."); } } } diff --git a/android/src/main/java/ee/forgr/capacitor_updater/VersionInfo.java b/android/src/main/java/ee/forgr/capacitor_updater/VersionInfo.java index 150e28e1b..d71bcc9ec 100644 --- a/android/src/main/java/ee/forgr/capacitor_updater/VersionInfo.java +++ b/android/src/main/java/ee/forgr/capacitor_updater/VersionInfo.java @@ -28,7 +28,7 @@ public class VersionInfo { } public VersionInfo(final VersionInfo source) { - this(source.folder, source.version, source.status, source.downloaded); + this(source.folder, source.versionName, source.status, source.downloaded); } public VersionInfo(final String folder, final String version, final VersionStatus status, final Date downloaded) { @@ -38,7 +38,7 @@ public VersionInfo(final String folder, final String version, final VersionStatu public VersionInfo(final String folder, final String version, final VersionStatus status, final String downloaded) { this.downloaded = downloaded.trim(); this.folder = folder; - this.version = version; + this.versionName = version; this.status = status; } @@ -60,7 +60,7 @@ public String getDownloaded() { } public VersionInfo setDownloaded(Date downloaded) { - return new VersionInfo(this.folder, this.version, this.status, downloaded); + return new VersionInfo(this.folder, this.versionName, this.status, downloaded); } public String getFolder() { @@ -68,11 +68,11 @@ public String getFolder() { } public VersionInfo setFolder(String folder) { - return new VersionInfo(folder, this.version, this.status, this.downloaded); + return new VersionInfo(folder, this.versionName, this.status, this.downloaded); } public String getVersionName() { - return this.version == null ? VERSION_BUILTIN : this.version; + return this.versionName == null ? VERSION_BUILTIN : this.versionName; } public VersionInfo setVersionName(String version) { @@ -84,7 +84,7 @@ public VersionStatus getStatus() { } public VersionInfo setStatus(VersionStatus status) { - return new VersionInfo(this.folder, this.version, status, this.downloaded); + return new VersionInfo(this.folder, this.versionName, status, this.downloaded); } public static VersionInfo fromJSON(final JSObject json) throws JSONException { @@ -120,7 +120,7 @@ public boolean equals(final Object o) { @Override public int hashCode() { - return Objects.hash(this.version); + return Objects.hash(this.versionName); } @Override