Skip to content

Commit

Permalink
fix: name folder to id
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Jun 29, 2022
1 parent d5c300e commit fe789a3
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 211 deletions.
36 changes: 18 additions & 18 deletions android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BundleInfo {
public static final String DOWNLOADED_BUILTIN = "1970-01-01T00:00:00.000Z";

private final String downloaded;
private final String folder;
private final String id;
private final String version;
private final BundleStatus status;

Expand All @@ -28,25 +28,25 @@ public class BundleInfo {
}

public BundleInfo(final BundleInfo source) {
this(source.folder, source.version, source.status, source.downloaded);
this(source.id, source.version, source.status, source.downloaded);
}

public BundleInfo(final String folder, final String version, final BundleStatus status, final Date downloaded) {
this(folder, version, status, sdf.format(downloaded));
public BundleInfo(final String id, final String version, final BundleStatus status, final Date downloaded) {
this(id, version, status, sdf.format(downloaded));
}

public BundleInfo(final String folder, final String version, final BundleStatus status, final String downloaded) {
public BundleInfo(final String id, final String version, final BundleStatus status, final String downloaded) {
this.downloaded = downloaded.trim();
this.folder = folder;
this.id = id;
this.version = version;
this.status = status;
}

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

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

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

public BundleInfo setFolder(String folder) {
return new BundleInfo(folder, this.version, this.status, this.downloaded);
public BundleInfo setId(String id) {
return new BundleInfo(id, this.version, this.status, this.downloaded);
}

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

public BundleInfo setVersionName(String version) {
return new BundleInfo(this.folder, version, this.status, this.downloaded);
return new BundleInfo(this.id, version, this.status, this.downloaded);
}

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

public BundleInfo setStatus(BundleStatus status) {
return new BundleInfo(this.folder, this.version, status, this.downloaded);
return new BundleInfo(this.id, this.version, status, this.downloaded);
}

public static BundleInfo fromJSON(final JSObject json) throws JSONException {
Expand All @@ -94,7 +94,7 @@ public static BundleInfo fromJSON(final JSObject json) throws JSONException {
public static BundleInfo fromJSON(final String jsonString) throws JSONException {
JSONObject json = new JSONObject(new JSONTokener(jsonString));
return new BundleInfo(
json.has("folder") ? json.getString("folder") : "",
json.has("id") ? json.getString("id") : "",
json.has("version") ? json.getString("version") : BundleInfo.VERSION_UNKNOWN,
json.has("status") ? BundleStatus.fromString(json.getString("status")) : BundleStatus.PENDING,
json.has("downloaded") ? json.getString("downloaded") : ""
Expand All @@ -103,7 +103,7 @@ public static BundleInfo fromJSON(final String jsonString) throws JSONException

public JSObject toJSON() {
final JSObject result = new JSObject();
result.put("folder", this.getFolder());
result.put("id", this.getId());
result.put("version", this.getVersionName());
result.put("downloaded", this.getDownloaded());
result.put("status", this.getStatus());
Expand All @@ -115,7 +115,7 @@ public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof BundleInfo)) return false;
final BundleInfo that = (BundleInfo) o;
return this.getFolder().equals(that.getFolder());
return this.getId().equals(that.getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ private boolean bundleExists(final File bundle) {
return new File(bundle.getPath(), "/index.html").exists();
}

public Boolean set(final BundleInfo version) {
return this.set(version.getFolder());
public Boolean set(final BundleInfo bundle) {
return this.set(bundle.getId());
}

public Boolean set(final String folder) {
Expand All @@ -301,17 +301,17 @@ public Boolean set(final String folder) {
return false;
}

public void commit(final BundleInfo version) {
this.setBundleStatus(version.getVersionName(), BundleStatus.SUCCESS);
this.setFallbackVersion(version);
public void commit(final BundleInfo bundle) {
this.setBundleStatus(bundle.getVersionName(), BundleStatus.SUCCESS);
this.setFallbackVersion(bundle);
}

public void reset() {
this.reset(false);
}

public void rollback(final BundleInfo version) {
this.setBundleStatus(version.getVersionName(), BundleStatus.ERROR);
public void rollback(final BundleInfo bundle) {
this.setBundleStatus(bundle.getVersionName(), BundleStatus.ERROR);
}

public void reset(final boolean internal) {
Expand All @@ -330,7 +330,7 @@ public void getLatest(final String url, final Callback callback) {
final String versionCode = this.versionCode;
final String versionOs = this.versionOs;
final String pluginVersion = CapacitorUpdater.pluginVersion;
final String version = this.getCurrentBundle().getFolder();
final String version = this.getCurrentBundle().getId();
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
Expand Down Expand Up @@ -365,7 +365,7 @@ public Map<String, String> getHeaders() throws AuthFailureError {
this.requestQueue.add(stringRequest);
}

public void sendStats(final String action, final BundleInfo version) {
public void sendStats(final String action, final BundleInfo bundle) {
String statsUrl = this.statsUrl;
if (statsUrl == null || "".equals(statsUrl) || statsUrl.length() == 0) { return; }
final URL url;
Expand All @@ -375,7 +375,7 @@ public void sendStats(final String action, final BundleInfo version) {
url = new URL(statsUrl);
json.put("platform", "android");
json.put("action", action);
json.put("version_name", version.getVersionName());
json.put("version_name", bundle.getVersionName());
json.put("device_id", this.deviceID);
json.put("version_build", this.versionBuild);
json.put("version_code", this.versionCode);
Expand Down Expand Up @@ -406,7 +406,7 @@ public void run() {
if (responseCode != 200) {
Log.e(TAG, "Stats error responseCode: " + responseCode);
} else {
Log.i(TAG, "Stats send for \"" + action + "\", version " + version);
Log.i(TAG, "Stats send for \"" + action + "\", version " + bundle.getVersionName());
}
} catch (final Exception ex) {
Log.e(TAG, "Error post stats", ex);
Expand Down Expand Up @@ -441,10 +441,10 @@ public BundleInfo getBundleInfo(String folder) {
return result;
}

public BundleInfo getBundleInfoByName(final String version) {
public BundleInfo getBundleInfoByName(final String folder) {
final List<BundleInfo> installed = this.list();
for(final BundleInfo i : installed) {
if(i.getFolder().equals(version)) {
if(i.getId().equals(folder)) {
return i;
}
}
Expand All @@ -465,7 +465,7 @@ private void saveBundleInfo(final String folder, final BundleInfo info) {
Log.d(TAG, "Removing info for folder [" + folder + "]");
this.editor.remove(folder + INFO_SUFFIX);
} else {
final BundleInfo update = info.setFolder(folder);
final BundleInfo update = info.setId(folder);
Log.d(TAG, "Storing info for folder [" + folder + "] " + update.toString());
this.editor.putString(folder + INFO_SUFFIX, update.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ private void cleanupObsoleteVersions() {
Log.i(CapacitorUpdater.TAG, "New native major version detected: " + this.currentVersionNative);
this.implementation.reset(true);
final List<BundleInfo> installed = this.implementation.list();
for (final BundleInfo version: installed) {
for (final BundleInfo bundle: installed) {
try {
Log.i(CapacitorUpdater.TAG, "Deleting obsolete version: " + version);
this.implementation.delete(version.getFolder());
Log.i(CapacitorUpdater.TAG, "Deleting obsolete bundle: " + bundle.getId());
this.implementation.delete(bundle.getId());
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Failed to delete: " + version, e);
Log.e(CapacitorUpdater.TAG, "Failed to delete: " + bundle.getId(), e);
}
}
}
Expand Down Expand Up @@ -280,10 +280,10 @@ public void list(final PluginCall call) {
final List<BundleInfo> res = this.implementation.list();
final JSObject ret = new JSObject();
final JSArray values = new JSArray();
for (final BundleInfo version : res) {
values.put(version.toJSON());
for (final BundleInfo bundle : res) {
values.put(bundle.toJSON());
}
ret.put("versions", values);
ret.put("bundles", values);
call.resolve(ret);
}
catch(final Exception e) {
Expand Down Expand Up @@ -340,8 +340,8 @@ public void current(final PluginCall call) {
public void notifyAppReady(final PluginCall call) {
try {
Log.i(CapacitorUpdater.TAG, "Current bundle loaded successfully. ['notifyAppReady()' was called]");
final BundleInfo version = this.implementation.getCurrentBundle();
this.implementation.commit(version);
final BundleInfo bundle = this.implementation.getCurrentBundle();
this.implementation.commit(bundle);
call.resolve();
}
catch(final Exception e) {
Expand Down Expand Up @@ -428,7 +428,7 @@ public void run() {
final BundleInfo 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.getId().equals(latestVersionName)) {

final BundleInfo latest = CapacitorUpdaterPlugin.this.implementation.getBundleInfoByName(latestVersionName);
if(latest != null) {
Expand All @@ -438,7 +438,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.getFolder());
CapacitorUpdaterPlugin.this.implementation.setNextVersion(latest.getId());
return;
}
}
Expand All @@ -448,12 +448,12 @@ public void run() {
@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.");
Log.i(CapacitorUpdater.TAG, "New bundle: " + latestVersionName + " found. Current is: " + current.getVersionName() + ". Update will occur next time app moves to background.");

final String url = (String) res.get("url");
final BundleInfo next = CapacitorUpdaterPlugin.this.implementation.download(url, latestVersionName);

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

final JSObject updateAvailable = new JSObject();
updateAvailable.put("version", next.toJSON());
Expand Down Expand Up @@ -499,14 +499,14 @@ public void onActivityStopped(@NonNull final Activity activity) {
Log.d(CapacitorUpdater.TAG, "Fallback version is: " + fallback);
Log.d(CapacitorUpdater.TAG, "Current version is: " + current);

if (next != null && !next.isErrorStatus() && (next.getFolder() != current.getFolder())) {
if (next != null && !next.isErrorStatus() && (next.getId() != current.getId())) {
// There is a next version waiting for activation
Log.d(CapacitorUpdater.TAG, "Next version is: " + next);
Log.d(CapacitorUpdater.TAG, "Next version is: " + next.getVersionName());
if (this.implementation.set(next) && this._reload()) {
Log.i(CapacitorUpdater.TAG, "Updated to version: " + next);
Log.i(CapacitorUpdater.TAG, "Updated to version: " + next.getVersionName());
this.implementation.setNextVersion(null);
} else {
Log.e(CapacitorUpdater.TAG, "Update to version: " + next + " Failed!");
Log.e(CapacitorUpdater.TAG, "Update to version: " + next.getVersionName() + " Failed!");
}
} else if (!success) {
// There is a no next version, and the current version has failed
Expand All @@ -527,9 +527,9 @@ public void onActivityStopped(@NonNull final Activity activity) {
if (!fallback.isBuiltin() && !fallback.equals(current)) {
final Boolean res = this.implementation.set(fallback);
if (res && this._reload()) {
Log.i(CapacitorUpdater.TAG, "Revert to version: " + fallback);
Log.i(CapacitorUpdater.TAG, "Revert to version: " + fallback.getVersionName());
} else {
Log.e(CapacitorUpdater.TAG, "Revert to version: " + fallback + " Failed!");
Log.e(CapacitorUpdater.TAG, "Revert to version: " + fallback.getVersionName() + " Failed!");
}
} else {
if (this._reset(false)) {
Expand All @@ -538,14 +538,14 @@ public void onActivityStopped(@NonNull final Activity activity) {
}

if (this.autoDeleteFailed) {
Log.i(CapacitorUpdater.TAG, "Deleting failing version: " + current);
Log.i(CapacitorUpdater.TAG, "Deleting failing version: " + current.getVersionName());
try {
final Boolean res = this.implementation.delete(current.getFolder());
final Boolean res = this.implementation.delete(current.getId());
if (res) {
Log.i(CapacitorUpdater.TAG, "Failed version deleted: " + current);
Log.i(CapacitorUpdater.TAG, "Failed version deleted: " + current.getVersionName());
}
} catch (final IOException e) {
Log.e(CapacitorUpdater.TAG, "Failed to delete failed version: " + current, e);
Log.e(CapacitorUpdater.TAG, "Failed to delete failed version: " + current.getVersionName(), e);
}
}
} else {
Expand All @@ -561,10 +561,10 @@ public void onActivityStopped(@NonNull final Activity activity) {
try {
final Boolean res = this.implementation.delete(fallback.getVersionName());
if (res) {
Log.i(CapacitorUpdater.TAG, "Deleted previous version: " + fallback);
Log.i(CapacitorUpdater.TAG, "Deleted previous version: " + fallback.getVersionName());
}
} catch (final IOException e) {
Log.e(CapacitorUpdater.TAG, "Failed to delete previous version: " + fallback, e);
Log.e(CapacitorUpdater.TAG, "Failed to delete previous version: " + fallback.getVersionName(), e);
}
}
}
Expand All @@ -588,11 +588,11 @@ public void run() {
}

if(BundleStatus.SUCCESS != current.getStatus()) {
Log.e(CapacitorUpdater.TAG, "notifyAppReady was not called, roll back current version: " + current);
Log.e(CapacitorUpdater.TAG, "notifyAppReady was not called, roll back current bundle: " + current.getId());
CapacitorUpdaterPlugin.this.implementation.rollback(current);
CapacitorUpdaterPlugin.this._reset(true);
} else {
Log.i(CapacitorUpdater.TAG, "notifyAppReady was called. This is fine: " + current);
Log.i(CapacitorUpdater.TAG, "notifyAppReady was called. This is fine: " + current.getId());
}

CapacitorUpdaterPlugin.this.appReadyCheck = null;
Expand Down

0 comments on commit fe789a3

Please sign in to comment.