Skip to content

Commit

Permalink
fix(android): autoUpdate properly compares version names during getLa…
Browse files Browse the repository at this point in the history
…test check
  • Loading branch information
lincolnthree committed May 10, 2022
1 parent b0c1181 commit 9e5c37c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -241,8 +242,8 @@ public VersionInfo download(final String url, final String versionName) throws I
return this.getVersionInfo(version);
}

public ArrayList<VersionInfo> list() {
final ArrayList<VersionInfo> res = new ArrayList<>();
public List<VersionInfo> list() {
final List<VersionInfo> res = new ArrayList<>();
final File destHot = new File(this.getDocumentsDir(), bundleDirectory);
Log.i(TAG, "list File : " + destHot.getPath());
if (destHot.exists()) {
Expand Down Expand Up @@ -428,6 +429,16 @@ public VersionInfo getVersionInfo(String version) {
return new VersionInfo(version, status, downloaded, name);
}

public VersionInfo getVersionInfoByName(final String version) {
final List<VersionInfo> installed = this.list();
for(final VersionInfo i : installed) {
if(i.getName().equals(version)) {
return i;
}
}
return null;
}

private void removeVersionInfo(final String version) {
this.setVersionDownloadedTimestamp(version, null);
this.setVersionName(version, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@CapacitorPlugin(name = "CapacitorUpdater")
public class CapacitorUpdaterPlugin extends Plugin implements Application.ActivityLifecycleCallbacks {
Expand Down Expand Up @@ -108,7 +108,7 @@ private void cleanupObsoleteVersions() {
try {
if (!"".equals(previous.getOriginalString()) && this.currentVersionNative.getMajor() > previous.getMajor()) {
this.implementation.reset(true);
final ArrayList<VersionInfo> installed = this.implementation.list();
final List<VersionInfo> installed = this.implementation.list();
for (final VersionInfo version: installed) {
try {
Log.i(CapacitorUpdater.TAG, "Deleting obsolete version: " + version);
Expand Down Expand Up @@ -281,7 +281,7 @@ public void delete(final PluginCall call) {
@PluginMethod
public void list(final PluginCall call) {
try {
final ArrayList<VersionInfo> res = this.implementation.list();
final List<VersionInfo> res = this.implementation.list();
final JSObject ret = new JSObject();
final JSArray values = new JSArray();
for (final VersionInfo version : res) {
Expand Down Expand Up @@ -383,7 +383,7 @@ public void cancelDelay(final PluginCall call) {
}

private Boolean _isAutoUpdateEnabled() {
return !"".equals(CapacitorUpdaterPlugin.this.autoUpdateUrl);
return CapacitorUpdaterPlugin.this.autoUpdate && !"".equals(CapacitorUpdaterPlugin.this.autoUpdateUrl);
}

@PluginMethod
Expand Down Expand Up @@ -430,18 +430,24 @@ public void run() {
return;
}
final VersionInfo current = CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
final String newVersion = (String) res.get("version");
final String latestVersionName = (String) res.get("version");

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

final VersionInfo latest = CapacitorUpdaterPlugin.this.implementation.getVersionInfoByName(latestVersionName);
if(latest != null && latest.isErrorStatus()) {
Log.e(CapacitorUpdater.TAG, "Latest version already exists, and is in error state. Aborting update.");
return;
}

// FIXME: What is failingVersion actually doing? Seems redundant with VersionStatus
final String failingVersion = CapacitorUpdaterPlugin.this.prefs.getString("failingVersion", "");
if (!"".equals(newVersion) && !newVersion.equals(current.getVersion()) && !newVersion.equals(failingVersion)) {
new Thread(new Runnable(){
@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.");

final String url = (String) res.get("url");
final VersionInfo next = CapacitorUpdaterPlugin.this.implementation.download(url, newVersion);
Log.i(CapacitorUpdater.TAG, "New version: " + newVersion + " found. Current is " + (current.getName().equals("") ? "builtin" : current.getName()) + ", next backgrounding will trigger update");
final VersionInfo next = CapacitorUpdaterPlugin.this.implementation.download(url, latestVersionName);

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

Expand Down Expand Up @@ -491,6 +497,9 @@ public void onActivityStopped(@NonNull final Activity activity) {

final Boolean success = current.getStatus() == VersionStatus.SUCCESS;

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

Expand Down

0 comments on commit 9e5c37c

Please sign in to comment.