Skip to content

Commit

Permalink
fix: add getPluginVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed May 4, 2022
1 parent 0a4dc9f commit bed9ce1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ public class CapacitorUpdater {
static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static SecureRandom rnd = new SecureRandom();
private final String TAG = "Capacitor-updater";
private final String pluginVersion = "3.2.1";
private final Context context;
private final String basePathHot = "versions";
private final SharedPreferences prefs;
private final SharedPreferences.Editor editor;

public String appId = "";
public String deviceID = "";
private String versionBuild = "";
private String versionCode = "";
private String versionOs = "";

public String appId = "";
public String deviceID = "";
public final String pluginVersion = "3.2.1";
public String statsUrl = "";

public CapacitorUpdater (final Context context) throws PackageManager.NameNotFoundException {
Expand Down Expand Up @@ -104,7 +104,7 @@ private File unzip(final File zipFile, final String dest) throws IOException {
final long lengthTotal = zipFile.length();
long lengthRead = bufferSize;
int percent = 0;
this.events.notifyDownload(75);
this.notifyDownload(75);

ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
Expand Down Expand Up @@ -188,13 +188,13 @@ private File downloadFile(final String url, final String dest) throws IOExceptio

int bytesRead = bufferSize;
int percent = 0;
this.events.notifyDownload(10);
this.notifyDownload(10);
while ((length = dis.read(buffer))>0) {
fos.write(buffer, 0, length);
final int newPercent = (int)((bytesRead * 100) / totalLength);
if (totalLength > 1 && newPercent != percent) {
percent = newPercent;
this.events.notifyDownload(this.calcTotalPercent(percent, 10, 70));
this.notifyDownload(this.calcTotalPercent(percent, 10, 70));
}
bytesRead += length;
}
Expand All @@ -216,20 +216,20 @@ private void deleteDirectory(final File file) throws IOException {
}

public String download(final String url) throws IOException {
this.events.notifyDownload(0);
this.notifyDownload(0);
final String path = this.randomString(10);
final File zipFile = new File(this.context.getFilesDir() + "/" + path);
final String folderNameUnZip = this.randomString(10);
final String version = this.randomString(10);
final String folderName = this.basePathHot + "/" + version;
this.events.notifyDownload(5);
this.notifyDownload(5);
final File downloaded = this.downloadFile(url, path);
this.events.notifyDownload(71);
this.notifyDownload(71);
final File unzipped = this.unzip(downloaded, folderNameUnZip);
zipFile.delete();
this.events.notifyDownload(91);
this.notifyDownload(91);
this.flattenAssets(unzipped, folderName);
this.events.notifyDownload(100);
this.notifyDownload(100);
return version;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public void getId(final PluginCall call) {
call.resolve(ret);
}

@PluginMethod
public void getPluginVersion(final PluginCall call) {
final JSObject ret = new JSObject();
ret.put("vrsion", this.implementation.pluginVersion);
call.resolve(ret);
}

@PluginMethod
public void download(final PluginCall call) {
new Thread(new Runnable(){
Expand Down
11 changes: 6 additions & 5 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ extension CustomError: LocalizedError {

@objc public class CapacitorUpdater: NSObject {

public var statsUrl = ""
public var appId = ""
public var deviceID = UIDevice.current.identifierForVendor?.uuidString ?? ""
public var notifyDownload: (Int) -> Void = { _ in }
public var pluginVersion = "3.2.0"
private var versionBuild = Bundle.main.releaseVersionNumber ?? ""
private var versionCode = Bundle.main.buildVersionNumber ?? ""
private var versionOs = ProcessInfo().operatingSystemVersion.getFullVersion()
Expand All @@ -78,6 +73,12 @@ extension CustomError: LocalizedError {
private let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
private let libraryUrl = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!

public var statsUrl = ""
public var appId = ""
public var deviceID = UIDevice.current.identifierForVendor?.uuidString ?? ""
public var notifyDownload: (Int) -> Void = { _ in }
public var pluginVersion = "3.2.0"

private func calcTotalPercent(percent: Int, min: Int, max: Int) -> Int {
return (percent * (max - min)) / 100 + min;
}
Expand Down
4 changes: 4 additions & 0 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
@objc func getId(_ call: CAPPluginCall) {
call.resolve(["id": implementation.deviceID])
}

@objc func getPluginVersion(_ call: CAPPluginCall) {
call.resolve(["version": implementation.pluginVersion])
}

@objc func download(_ call: CAPPluginCall) {
let url = URL(string: call.getString("url") ?? "")
Expand Down

0 comments on commit bed9ce1

Please sign in to comment.