Skip to content

Commit

Permalink
feat: make auto update revert if fail to load
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Feb 3, 2022
1 parent b19be14 commit 08d7811
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface Callback {
}

public class CapacitorUpdater {
String TAG = "CapacitorUpdater";
String TAG = "Capacitor-updater";
private Context context;
private String basePathHot = "versions";
private SharedPreferences prefs;
Expand Down Expand Up @@ -193,12 +193,11 @@ public ArrayList<String> list() {

public Boolean delete(String version) throws IOException {
File destHot = new File(this.context.getFilesDir() + "/" + basePathHot + "/" + version);
Log.i(TAG, "delete File : " + destHot.getPath());
if (destHot.exists()) {
deleteDirectory(destHot);
return true;
}
Log.i(TAG, "Directory not removed.");
Log.i(TAG, "Directory not removed: " + destHot.getPath());
return false;
}

Expand Down Expand Up @@ -242,7 +241,7 @@ public void onErrorResponse(VolleyError error) {
public String getLastPathHot() {
return prefs.getString("lastPathHot", "");
}

public String getVersionName() {
return prefs.getString("versionName", "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.ArrayList;

@CapacitorPlugin(name = "CapacitorUpdater")
public class CapacitorUpdaterPlugin extends Plugin implements Application.ActivityLifecycleCallbacks {
public class CapacitorUpdaterPlugin extends Plugin implements Application.ActivityLifecycleCallbacks {
String TAG = "Capacitor-updater";
private CapacitorUpdater implementation;
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void run() {

private boolean _reload() {
String pathHot = implementation.getLastPathHot();
Log.i("CapacitorUpdater", "getLastPathHot : " + pathHot);
Log.i(TAG, "getLastPathHot : " + pathHot);
this.bridge.setServerBasePath(pathHot);
return true;
}
Expand All @@ -83,7 +84,7 @@ public void set(PluginCall call) {
call.reject("Update failed, version don't exist");
} else {
String pathHot = implementation.getLastPathHot();
Log.i("CapacitorUpdater", "getLastPathHot : " + pathHot);
Log.i(TAG, "getLastPathHot : " + pathHot);
this.bridge.setServerBasePath(pathHot);
call.resolve();
}
Expand Down Expand Up @@ -114,11 +115,13 @@ public void list(PluginCall call) {
call.resolve(ret);
}

private boolean _reset() {
implementation.reset();
return this._reload();
}
@PluginMethod
public void reset(PluginCall call) {
implementation.reset();
String pathHot = implementation.getLastPathHot();
this.bridge.setServerAssetPath(pathHot);
this._reset();
call.resolve();
}

Expand All @@ -138,22 +141,31 @@ public void versionName(PluginCall call) {
ret.put("versionName", name);
call.resolve(ret);
}
@PluginMethod
public void notifyAppReady(PluginCall call) {
editor.putBoolean("notifyAppReady", true);
editor.commit();
call.resolve();
}

@Override
public void onActivityStarted(@NonNull Activity activity) {
Log.i("CapacitorUpdater", "on foreground");
Log.i(TAG, "Check for update in the server");
if (autoUpdateUrl == null || autoUpdateUrl.equals("")) return;
implementation.getLatest(autoUpdateUrl, (res) -> {
try {
String name = implementation.getVersionName();
String currentVersion = implementation.getVersionName();
String newVersion = (String) res.get("version");
if (!newVersion.equals(name)) {
String failingVersion = prefs.getString("failingVersion", "");
Log.i(TAG, "currentVersion " + currentVersion + ", newVersion " + newVersion + ", failingVersion " + failingVersion + ".");
if (!newVersion.equals(currentVersion) && !newVersion.equals(failingVersion)) {
new Thread(new Runnable(){
@Override
public void run() {
// Do network action in this function
try {
String dl = implementation.download((String) res.get("url"));
Log.i(TAG, "New version: " + newVersion + " found. Current is " + (currentVersion == "" ? "builtin" : currentVersion) + ", next backgrounding will trigger update.");
editor.putString("nextVersion", dl);
editor.putString("nextVersionName", (String) res.get("version"));
editor.commit();
Expand All @@ -172,20 +184,76 @@ public void run() {

@Override
public void onActivityStopped(@NonNull Activity activity) {
Log.i("CapacitorUpdater", "on background");
String pathHot = implementation.getLastPathHot();
Log.i(TAG, "Check for waiting update");
String nextVersion = prefs.getString("nextVersion", "");
String nextVersionName = prefs.getString("nextVersionName", "");
if (nextVersion.equals("") || nextVersionName.equals("")) return;
Log.i("CapacitorUpdater", "set: " + nextVersion + " " + nextVersionName);
Boolean res = implementation.set(nextVersion, nextVersionName);
if (res) {
if (this._reload()) {
Log.i("CapacitorUpdater", "Auto update to VersionName: " + nextVersionName + ", Version: " + nextVersion);
String pastVersion = prefs.getString("pastVersion", "");
String pastVersionName = prefs.getString("pastVersionName", "");
Boolean notifyAppReady = prefs.getBoolean("notifyAppReady", false);
String tmpCurVersion = implementation.getLastPathHot();
String curVersion = tmpCurVersion.substring(tmpCurVersion.lastIndexOf('/') +1);
String curVersionName = implementation.getVersionName();

Log.i(TAG, "next version: " + nextVersionName + ", past version: " + pastVersionName);
if (!nextVersion.equals("") && !nextVersionName.equals("")) {
Boolean res = implementation.set(nextVersion, nextVersionName);
if (res) {
if (this._reload()) {
Log.i(TAG, "Auto update to version: " + nextVersionName);
}
editor.putString("nextVersion", "");
editor.putString("nextVersionName", "");
editor.putString("pastVersion", curVersion);
editor.putString("pastVersionName", curVersionName);
editor.putBoolean("notifyAppReady", false);
editor.commit();
}
editor.putString("nextVersion", "");
editor.putString("nextVersionName", "");
} else if (!notifyAppReady && !pathHot.equals("public")) {
Log.i(TAG, "notifyAppReady never trigger");
Log.i(TAG, "Version: " + curVersionName + ", is considered broken");
Log.i(TAG, "Will downgraded to " + pastVersionName + " for next start");
Log.i(TAG, "Don't forget to trigger 'notifyAppReady()' in js code to validate a version.");
if (!pastVersion.equals("") && !pastVersionName.equals("")) {
Boolean res = implementation.set(pastVersion, pastVersionName);
if (res) {
if (this._reload()) {
Log.i(TAG, "Revert update to version: " + pastVersionName);
}
editor.putString("pastVersion", "");
editor.putString("pastVersionName", "");
editor.commit();
}
} else {
if (this._reset()) {
Log.i(TAG, "Auto reset done");
}
}
editor.putString("failingVersion", curVersionName);
editor.commit();
try {
Boolean res = implementation.delete(curVersion);
if (res) {
Log.i(TAG, "Delete failing version: " + curVersionName);
}
} catch (IOException e) {
e.printStackTrace();
}
} else if (!pastVersion.equals("")) {
Log.i(TAG, "Validated version: " + curVersionName);
try {
Boolean res = implementation.delete(pastVersion);
if (res) {
Log.i(TAG, "Delete past version: " + pastVersionName);
}
} catch (IOException e) {
e.printStackTrace();
}
editor.putString("pastVersion", "");
editor.putString("pastVersionName", "");
editor.commit();
}

}

@Override
Expand Down
22 changes: 13 additions & 9 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class AppVersion: NSObject {
do {
try FileManager.default.createDirectory(atPath: source.path, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Cannot createDirectory " + source.path)
print("✨ Capacitor-updater: Cannot createDirectory " + source.path)
}
}
}
Expand All @@ -52,7 +52,7 @@ public class AppVersion: NSObject {
try FileManager.default.removeItem(atPath: source.path)
try FileManager.default.removeItem(atPath: dest.path)
} catch {
print("File not removed.")
print("✨ Capacitor-updater: File not removed.")
}
}

Expand All @@ -66,7 +66,7 @@ public class AppVersion: NSObject {
try FileManager.default.moveItem(at: source, to: dest)
}
} catch {
print("File not moved.")
print("✨ Capacitor-updater: File not moved.")
}
}

Expand All @@ -81,7 +81,7 @@ public class AppVersion: NSObject {
moveFolder(source: destUnZip, dest: destHot)
deleteFolder(source: destUnZip, dest: destZip)
} else {
print("File not created.")
print("✨ Capacitor-updater: File not created.")
}
}

Expand All @@ -96,7 +96,7 @@ public class AppVersion: NSObject {
moveFolder(source: destUnZip, dest: destPersist)
deleteFolder(source: destUnZip, dest: destZip)
} else {
print("File Persist not created.")
print("✨ Capacitor-updater: File Persist not created.")
}
}

Expand All @@ -115,7 +115,7 @@ public class AppVersion: NSObject {
}

} else {
print("Error get Latest", url, r.error ?? "unknow")
print("✨ Capacitor-updater: Error get Latest", url, r.error ?? "unknow")
}
return nil
}
Expand All @@ -129,7 +129,7 @@ public class AppVersion: NSObject {
saveDownloadedPersist(content: r.content, version: version)
return version
} else {
print("Error downloading zip file", r.error ?? "unknow")
print("✨ Capacitor-updater: Error downloading zip file", r.error ?? "unknow")
}
return nil
}
Expand All @@ -140,7 +140,7 @@ public class AppVersion: NSObject {
let files = try FileManager.default.contentsOfDirectory(atPath: dest.path)
return files
} catch {
print("NO version available" + dest.path)
print("✨ Capacitor-updater: No version available" + dest.path)
return []
}
}
Expand All @@ -150,9 +150,13 @@ public class AppVersion: NSObject {
let destPersist = documentsUrl.appendingPathComponent(basePathPersist).appendingPathComponent(version)
do {
try FileManager.default.removeItem(atPath: destHot.path)
} catch {
print("✨ Capacitor-updater: Hot Folder " + destHot.path + ", not removed.")
}
do {
try FileManager.default.removeItem(atPath: destPersist.path)
} catch {
print("File not removed.")
print("✨ Capacitor-updater: Folder " + destPersist.path + ", not removed.")
return false
}
return true
Expand Down
8 changes: 4 additions & 4 deletions ios/Plugin/CapacitorUpdaterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
CAP_PLUGIN(CapacitorUpdaterPlugin, "CapacitorUpdater",
CAP_PLUGIN_METHOD(download, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(set, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(set, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(list, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(delete, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(load, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(reset, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(delete, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(reset, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(current, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(reload, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(versionName, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(notifyAppReady, CAPPluginReturnPromise);
)
Loading

0 comments on commit 08d7811

Please sign in to comment.