Skip to content

Commit

Permalink
feat: add to download event version
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Jun 24, 2022
1 parent 24c552a commit 2069379
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 157 deletions.
154 changes: 78 additions & 76 deletions android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void load() {
try {
this.implementation = new CapacitorUpdater() {
@Override
public void notifyDownload(final int percent) {
CapacitorUpdaterPlugin.this.notifyDownload(percent);
public void notifyDownload(final String folder, final int percent) {
CapacitorUpdaterPlugin.this.notifyDownload(folder, percent);
}
};
final PackageInfo pInfo = this.getContext().getPackageManager().getPackageInfo(this.getContext().getPackageName(), 0);
Expand Down Expand Up @@ -113,7 +113,7 @@ private void cleanupObsoleteVersions() {
for (final VersionInfo version: installed) {
try {
Log.i(CapacitorUpdater.TAG, "Deleting obsolete version: " + version);
this.implementation.delete(version.getVersion());
this.implementation.delete(version.getFolder());
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Failed to delete: " + version, e);
}
Expand All @@ -129,10 +129,11 @@ private void cleanupObsoleteVersions() {
this.editor.commit();
}

public void notifyDownload(final int percent) {
public void notifyDownload(final String folder, final int percent) {
try {
final JSObject ret = new JSObject();
ret.put("percent", percent);
ret.put("version", this.implementation.getVersionInfo(folder).toJSON());
this.notifyListeners("download", ret);
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Could not notify listeners", e);
Expand Down Expand Up @@ -167,14 +168,18 @@ public void getPluginVersion(final PluginCall call) {
@PluginMethod
public void download(final PluginCall call) {
final String url = call.getString("url");
final String versionName = call.getString("versionName");
if (!url || !versionName) {
call.reject("missing url or versionName");
return;
}
try {
Log.i(CapacitorUpdater.TAG, "Downloading " + url);

new Thread(new Runnable(){
@Override
public void run() {
try {
final String versionName = call.getString("versionName");

final VersionInfo downloaded = CapacitorUpdaterPlugin.this.implementation.download(url, versionName);
call.resolve(downloaded.toJSON());
} catch (final IOException e) {
Expand Down Expand Up @@ -217,58 +222,58 @@ public void reload(final PluginCall call) {

@PluginMethod
public void next(final PluginCall call) {
final String version = call.getString("version");
final String folder = call.getString("folder");
final String versionName = call.getString("versionName", "");

try {
Log.i(CapacitorUpdater.TAG, "Setting next active version " + version);
if (!this.implementation.setNextVersion(version)) {
call.reject("Set next version failed. Version " + version + " does not exist.");
Log.i(CapacitorUpdater.TAG, "Setting next active folder " + folder);
if (!this.implementation.setNextVersion(folder)) {
call.reject("Set next folder failed. Version " + folder + " does not exist.");
} else {
if(!"".equals(versionName)) {
this.implementation.setVersionName(version, versionName);
this.implementation.setVersionName(folder, versionName);
}
call.resolve(this.implementation.getVersionInfo(version).toJSON());
call.resolve(this.implementation.getVersionInfo(folder).toJSON());
}
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Could not set next version " + version, e);
call.reject("Could not set next version " + version, e);
Log.e(CapacitorUpdater.TAG, "Could not set next folder " + folder, e);
call.reject("Could not set next folder " + folder, e);
}
}

@PluginMethod
public void set(final PluginCall call) {
final String version = call.getString("version");
final String folder = call.getString("folder");

try {
Log.i(CapacitorUpdater.TAG, "Setting active bundle " + version);
if (!this.implementation.set(version)) {
Log.i(CapacitorUpdater.TAG, "No such bundle " + version);
call.reject("Update failed, version " + version + " does not exist.");
Log.i(CapacitorUpdater.TAG, "Setting active bundle " + folder);
if (!this.implementation.set(folder)) {
Log.i(CapacitorUpdater.TAG, "No such bundle " + folder);
call.reject("Update failed, folder " + folder + " does not exist.");
} else {
Log.i(CapacitorUpdater.TAG, "Bundle successfully set to" + version);
Log.i(CapacitorUpdater.TAG, "Bundle successfully set to" + folder);
this.reload(call);
}
} catch(final Exception e) {
Log.e(CapacitorUpdater.TAG, "Could not set version " + version, e);
call.reject("Could not set version " + version, e);
Log.e(CapacitorUpdater.TAG, "Could not set folder " + folder, e);
call.reject("Could not set folder " + folder, e);
}
}

@PluginMethod
public void delete(final PluginCall call) {
final String version = call.getString("version");
Log.i(CapacitorUpdater.TAG, "Deleting version: " + version);
final String folder = call.getString("folder");
Log.i(CapacitorUpdater.TAG, "Deleting folder: " + folder);
try {
final Boolean res = this.implementation.delete(version);
final Boolean res = this.implementation.delete(folder);
if (res) {
call.resolve();
} else {
call.reject("Delete failed, version " + version + " does not exist");
call.reject("Delete failed, folder " + folder + " does not exist");
}
} catch(final Exception e) {
Log.e(CapacitorUpdater.TAG, "Could not delete version " + version, e);
call.reject("Could not delete version " + version, e);
Log.e(CapacitorUpdater.TAG, "Could not delete folder " + folder, e);
call.reject("Could not delete folder " + folder, e);
}
}

Expand Down Expand Up @@ -401,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.getName(), e);
Log.e(CapacitorUpdater.TAG, "Failed to start " + DeferredNotifyAppReadyCheck.class.getFolder(), e);
}
}

Expand All @@ -427,7 +432,7 @@ public void run() {
final VersionInfo current = CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
final String latestVersionName = (String) res.get("version");

if (latestVersionName != null && !"".equals(latestVersionName) && !current.getName().equals(latestVersionName)) {
if (latestVersionName != null && !"".equals(latestVersionName) && !current.getFolder().equals(latestVersionName)) {

final VersionInfo latest = CapacitorUpdaterPlugin.this.implementation.getVersionInfoByName(latestVersionName);
if(latest != null) {
Expand All @@ -437,7 +442,7 @@ public void run() {
}
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.getVersion());
CapacitorUpdaterPlugin.this.implementation.setNextVersion(latest.getFolder());
return;
}
}
Expand All @@ -447,15 +452,15 @@ public void run() {
@Override
public void run() {
try {
Log.i(CapacitorUpdater.TAG, "New version: " + latestVersionName + " found. Current is: " + current.getName() + ". Update will occur next time app moves to background.");
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);

CapacitorUpdaterPlugin.this.implementation.setNextVersion(next.getVersion());
CapacitorUpdaterPlugin.this.implementation.setNextVersion(next.getFolder());

final JSObject updateAvailable = new JSObject();
updateAvailable.put("version", next.getVersion());
updateAvailable.put("version", next.toJSON());
CapacitorUpdaterPlugin.this.notifyListeners("updateAvailable", updateAvailable);
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "error downloading file", e);
Expand Down Expand Up @@ -497,10 +502,10 @@ public void onActivityStopped(@NonNull final Activity activity) {

Log.d(CapacitorUpdater.TAG, "Fallback version is: " + fallback);
Log.d(CapacitorUpdater.TAG, "Current version is: " + current);
Log.d(CapacitorUpdater.TAG, "Next version is: " + next);
if (next != null && !next.isErrorStatus() && (next.getVersion() != current.getVersion())) {
// There is a next version waiting for activation

if (next != null && !next.isErrorStatus() && (next.getFolder() != current.getFolder())) {
// There is a next version waiting for activation
Log.d(CapacitorUpdater.TAG, "Next version is: " + next);
if (this.implementation.set(next) && this._reload()) {
Log.i(CapacitorUpdater.TAG, "Updated to version: " + next);
this.implementation.setNextVersion(null);
Expand Down Expand Up @@ -539,7 +544,7 @@ public void onActivityStopped(@NonNull final Activity activity) {
if (this.autoDeleteFailed) {
Log.i(CapacitorUpdater.TAG, "Deleting failing version: " + current);
try {
final Boolean res = this.implementation.delete(current.getVersion());
final Boolean res = this.implementation.delete(current.getFolder());
if (res) {
Log.i(CapacitorUpdater.TAG, "Failed version deleted: " + current);
}
Expand All @@ -558,7 +563,7 @@ public void onActivityStopped(@NonNull final Activity activity) {
if(this.autoDeletePrevious) {
Log.i(CapacitorUpdater.TAG, "Version successfully loaded: " + current);
try {
final Boolean res = this.implementation.delete(fallback.getVersion());
final Boolean res = this.implementation.delete(fallback.getVersionName());
if (res) {
Log.i(CapacitorUpdater.TAG, "Deleted previous version: " + fallback);
}
Expand Down Expand Up @@ -596,7 +601,7 @@ public void run() {

CapacitorUpdaterPlugin.this.appReadyCheck = null;
} catch (final InterruptedException e) {
Log.e(CapacitorUpdater.TAG, DeferredNotifyAppReadyCheck.class.getName() + " was interrupted.");
Log.e(CapacitorUpdater.TAG, DeferredNotifyAppReadyCheck.class.getFolder() + " was interrupted.");
}
}
}
Expand Down
50 changes: 24 additions & 26 deletions android/src/main/java/ee/forgr/capacitor_updater/VersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ public class VersionInfo {
public static final String DOWNLOADED_BUILTIN = "1970-01-01T00:00:00.000Z";

private final String downloaded;
private final String name;
private final String version;
private final String folder;
private final String versionName;
private final VersionStatus status;

static {
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
}

public VersionInfo(final VersionInfo source) {
this(source.version, source.status, source.downloaded, source.name);
this(source.folder, source.version, source.status, source.downloaded);
}

public VersionInfo(final String version, final VersionStatus status, final Date downloaded, final String name) {
this(version, status, sdf.format(downloaded), name);
public VersionInfo(final String folder, final String version, final VersionStatus status, final Date downloaded) {
this(folder, version, status, sdf.format(downloaded));
}

public VersionInfo(final String version, final VersionStatus status, final String downloaded, final String name) {
public VersionInfo(final String folder, final String version, final VersionStatus status, final String downloaded) {
this.downloaded = downloaded.trim();
this.name = name;
this.folder = folder;
this.version = version;
this.status = status;
}

public Boolean isBuiltin() {
return VERSION_BUILTIN.equals(this.getVersion());
return VERSION_BUILTIN.equals(this.getVersionName());
}
public Boolean isUnknown() {
return VERSION_UNKNOWN.equals(this.getVersion());
return VERSION_UNKNOWN.equals(this.getVersionName());
}
public Boolean isErrorStatus() {
return VersionStatus.ERROR == this.status;
Expand All @@ -60,31 +60,31 @@ public String getDownloaded() {
}

public VersionInfo setDownloaded(Date downloaded) {
return new VersionInfo(this.version, this.status, downloaded, this.name);
return new VersionInfo(this.folder, this.version, this.status, downloaded);
}

public String getName() {
return this.isBuiltin() ? VERSION_BUILTIN : this.name;
public String getFolder() {
return this.isBuiltin() ? VERSION_BUILTIN : this.folder;
}

public VersionInfo setName(String name) {
return new VersionInfo(this.version, this.status, this.downloaded, name);
public VersionInfo setFolder(String folder) {
return new VersionInfo(folder, this.version, this.status, this.downloaded);
}

public String getVersion() {
public String getVersionName() {
return this.version == null ? VERSION_BUILTIN : this.version;
}

public VersionInfo setVersion(String version) {
return new VersionInfo(version, this.status, this.downloaded, this.name);
public VersionInfo setVersionName(String version) {
return new VersionInfo(this.folder, version, this.status, this.downloaded);
}

public VersionStatus getStatus() {
return this.isBuiltin() ? VersionStatus.SUCCESS : this.status;
}

public VersionInfo setStatus(VersionStatus status) {
return new VersionInfo(this.version, status, this.downloaded, this.name);
return new VersionInfo(this.folder, this.version, status, this.downloaded);
}

public static VersionInfo fromJSON(final JSObject json) throws JSONException {
Expand All @@ -94,30 +94,28 @@ public static VersionInfo fromJSON(final JSObject json) throws JSONException {
public static VersionInfo fromJSON(final String jsonString) throws JSONException {
JSONObject json = new JSONObject(new JSONTokener(jsonString));
return new VersionInfo(
json.has("version") ? json.getString("version") : VersionInfo.VERSION_UNKNOWN,
json.has("folder") ? json.getString("name") : "",
json.has("versionName") ? json.getString("version") : VersionInfo.VERSION_UNKNOWN,
json.has("status") ? VersionStatus.fromString(json.getString("status")) : VersionStatus.PENDING,
json.has("downloaded") ? json.getString("downloaded") : "",
json.has("name") ? json.getString("name") : ""
json.has("downloaded") ? json.getString("downloaded") : ""
);
}

public JSObject toJSON() {
final JSObject result = new JSObject();
result.put("folder", this.getFolder());
result.put("versionName", this.getVersionName());
result.put("downloaded", this.getDownloaded());
result.put("name", this.getName());
result.put("version", this.getVersion());
result.put("status", this.getStatus());
return result;
}



@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof VersionInfo)) return false;
final VersionInfo that = (VersionInfo) o;
return this.getVersion().equals(that.getVersion());
return this.getFolder().equals(that.getFolder());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
public enum VersionStatus {
SUCCESS("success"),
ERROR("error"),
PENDING("pending");
PENDING("pending"),
DOWNLOADING("donwloading");

public final String label;

Expand Down

0 comments on commit 2069379

Please sign in to comment.