Skip to content

Commit

Permalink
fix: build issue android
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Jun 28, 2022
1 parent 8770d23 commit 8ff71af
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -248,7 +247,7 @@ public List<VersionInfo> 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 {
Expand Down Expand Up @@ -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, "");
}
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand All @@ -60,19 +60,19 @@ 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() {
return this.isBuiltin() ? VERSION_BUILTIN : this.folder;
}

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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8ff71af

Please sign in to comment.