Skip to content

Commit

Permalink
feat: add pluginVersion send to server
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Apr 9, 2022
1 parent 3148d98 commit 6453efb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .cz.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "cz_conventional_commits"
tag_format = "$major.$minor.$patch$prerelease"
version = "2.3.3"
version_files = [
"package.json:version",
"CapacitorUpdater.swift",
"CapacitorUpdater.java",
"package.json:version",
".cz.toml"
]
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class CapacitorUpdater {
public String statsUrl = "";
public String appId = "";
public String deviceID = "";
private String pluginVersion = "2.3.3";


private FilenameFilter filter = new FilenameFilter() {
@Override
Expand Down Expand Up @@ -295,6 +297,7 @@ public void getLatest(String url, Callback callback) {
String deviceID = this.deviceID;
String appId = this.appId;
String versionBuild = this.versionBuild;
String pluginVersion = this.pluginVersion;
String versionName = getVersionName().equals("") ? "builtin" : getVersionName();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
Expand All @@ -321,6 +324,7 @@ public Map<String, String> getHeaders() throws AuthFailureError {
params.put("cap_app_id", appId);
params.put("cap_version_build", versionBuild);
params.put("cap_version_name", versionName);
params.put("cap_plugin_version", pluginVersion);
return params;
}
};
Expand All @@ -329,7 +333,7 @@ public Map<String, String> getHeaders() throws AuthFailureError {
}

public String getLastPathHot() {
return prefs.getString("lastPathHot", "");
return prefs.getString("lastPathHot", "public");
}

public String getVersionName() {
Expand Down Expand Up @@ -357,6 +361,7 @@ public void sendStats(String action, String version) {
json.put("version_name", version);
json.put("device_id", this.deviceID);
json.put("version_build", this.versionBuild);
json.put("plugin_version", this.pluginVersion);
json.put("app_id", this.appId);
jsonString = json.toString();
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
private static final String autoUpdateUrlDefault = "https://capgo.app/api/auto_update";
private static final String statsUrlDefault = "https://capgo.app/api/stats";
private String autoUpdateUrl = "";
private String currentVersionNative = "";
private Version currentVersionNative;
private Boolean autoUpdate = false;
private Boolean resetWhenUpdate = true;

Expand All @@ -48,9 +48,14 @@ public void load() {
this.editor = prefs.edit();
try {
implementation = new CapacitorUpdater(this.getContext(), this);
PackageInfo pInfo = this.getContext().getPackageManager().getPackageInfo(this.getContext().getPackageName(), 0);
currentVersionNative = new Version(pInfo.versionName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return;
} catch (Exception ex) {
Log.e(TAG, "Error get currentVersionNative", ex);
return;
}
CapConfig config = CapConfig.loadDefault(getActivity());
implementation.appId = config.getString("appId", "");
Expand All @@ -61,8 +66,6 @@ public void load() {
if (resetWhenUpdate) {
Version LatestVersionNative = new Version(prefs.getString("LatestVersionNative", ""));
try {
PackageInfo pInfo = this.getContext().getPackageManager().getPackageInfo(this.getContext().getPackageName(), 0);
Version currentVersionNative = new Version(pInfo.versionName);
if (!LatestVersionNative.equals("") && currentVersionNative.getMajor() > LatestVersionNative.getMajor()) {
this._reset(false);
editor.putString("LatestVersionAutoUpdate", "");
Expand All @@ -85,13 +88,6 @@ public void load() {
if (!autoUpdate || this.autoUpdateUrl.equals("")) return;
Application application = (Application) this.getContext().getApplicationContext();
application.registerActivityLifecycleCallbacks(this);
try {
PackageInfo pInfo = this.getContext().getPackageManager().getPackageInfo(this.getContext().getPackageName(), 0);
currentVersionNative = pInfo.versionName;
} catch (Exception ex) {
Log.e(TAG, "Error get currentVersionNative", ex);
return;
}
onActivityStarted(getActivity());
}

Expand Down Expand Up @@ -321,7 +317,6 @@ public void onActivityStopped(@NonNull Activity activity) {
String tmpCurVersion = implementation.getLastPathHot();
String curVersion = tmpCurVersion.substring(tmpCurVersion.lastIndexOf('/') +1);
String curVersionName = implementation.getVersionName();
Log.i(TAG, "Next version: " + nextVersionName + ", past version: " + (pastVersionName.equals("") ? "builtin" : pastVersionName));
if (!nextVersion.equals("") && !nextVersionName.equals("")) {
Boolean res = implementation.set(nextVersion, nextVersionName);
if (res && this._reload()) {
Expand Down
3 changes: 3 additions & 0 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extension Bundle {
public var appId = ""
public var deviceID = UIDevice.current.identifierForVendor?.uuidString ?? ""
public var notifyDownload: (Int) -> Void = { _ in }
public var pluginVersion = "2.3.3"
private var versionBuild = Bundle.main.buildVersionNumber ?? ""
private var lastPathHot = ""
private var lastPathPersist = ""
Expand Down Expand Up @@ -111,6 +112,7 @@ extension Bundle {
"cap_device_id": self.deviceID,
"cap_app_id": self.appId,
"cap_version_build": self.versionBuild,
"cap_plugin_version": self.pluginVersion,
"cap_version_name": UserDefaults.standard.string(forKey: "versionName") ?? "builtin"
]
let request = AF.request(url, headers: headers)
Expand Down Expand Up @@ -251,6 +253,7 @@ extension Bundle {
"device_id": self.deviceID,
"version_name": version,
"version_build": self.versionBuild,
"plugin_version": self.pluginVersion,
"app_id": self.appId
]

Expand Down
10 changes: 6 additions & 4 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
static let autoUpdateUrlDefault = "https://capgo.app/api/auto_update"
static let statsUrlDefault = "https://capgo.app/api/stats"
private var autoUpdateUrl = ""
private var currentVersionNative: Version = "0.0.0"
private var autoUpdate = false
private var statsUrl = ""
private var resetWhenUpdate = true;

override public func load() {
do {
currentVersionNative = try Version(Bundle.main.buildVersionNumber ?? "0.0.0")
} catch {
print("✨ Capacitor-updater: Cannot get version native \(currentVersionNative)")
}
autoUpdateUrl = getConfigValue("autoUpdateUrl") as? String ?? CapacitorUpdaterPlugin.autoUpdateUrlDefault
autoUpdate = getConfigValue("autoUpdate") as? Bool ?? false
implementation.appId = Bundle.main.bundleIdentifier ?? ""
Expand All @@ -28,9 +34,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
implementation.statsUrl = getConfigValue("statsUrl") as? String ?? CapacitorUpdaterPlugin.statsUrlDefault
if (resetWhenUpdate) {
var LatestVersionNative: Version = "0.0.0"
var currentVersionNative: Version = "0.0.0"
do {
currentVersionNative = try Version(Bundle.main.buildVersionNumber ?? "0.0.0")
LatestVersionNative = try Version(UserDefaults.standard.string(forKey: "LatestVersionNative") ?? "0.0.0")
} catch {
print("✨ Capacitor-updater: Cannot get version native \(currentVersionNative)")
Expand Down Expand Up @@ -171,7 +175,6 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
@objc func current(_ call: CAPPluginCall) {
let pathHot = implementation.getLastPathHot()
let current = pathHot.count >= 10 ? pathHot.suffix(10) : "builtin"
let currentVersionNative = Bundle.main.buildVersionNumber ?? "0.0.0"
call.resolve([
"current": current,
"currentNative": currentVersionNative
Expand Down Expand Up @@ -248,7 +251,6 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
let notifyAppReady = UserDefaults.standard.bool(forKey: "notifyAppReady")
let curVersion = implementation.getLastPathPersist().components(separatedBy: "/").last!
let curVersionName = implementation.getVersionName()
print("✨ Capacitor-updater: Next version: \(nextVersionName), past version: \(pastVersionName == "" ? "builtin" : pastVersionName)");
if (nextVersion != "" && nextVersionName != "") {
let res = implementation.set(version: nextVersion, versionName: nextVersionName)
if (res && self._reload()) {
Expand Down

0 comments on commit 6453efb

Please sign in to comment.