Skip to content

Commit

Permalink
feat: add getId method for version by device control
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Apr 6, 2022
1 parent 109ebc2 commit b733873
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Do not password encrypt this file, or it will fail to unpack.

* [`download(...)`](#download)
* [`set(...)`](#set)
* [`getId()`](#getid)
* [`delete(...)`](#delete)
* [`list()`](#list)
* [`reset(...)`](#reset)
Expand Down Expand Up @@ -170,6 +171,19 @@ Set version as current version, set will return an error if there are is no inde
--------------------


### getId()

```typescript
getId() => Promise<{ id: string; }>
```

Get unique ID used to identify device into auto update server

**Returns:** <code>Promise&lt;{ id: string; }&gt;</code>

--------------------


### delete(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ interface Callback {
}

public class CapacitorUpdater {
private final CapacitorUpdaterPlugin plugin;
private String TAG = "Capacitor-updater";
public String statsUrl = "";
public String appId = "";
private String versionBuild = "";
public String deviceID = "";

private final CapacitorUpdaterPlugin plugin;
private String versionBuild = "";
private String TAG = "Capacitor-updater";
private Context context;
private String basePathHot = "versions";
private SharedPreferences prefs;
Expand Down Expand Up @@ -279,7 +279,7 @@ public Boolean set(String version, String versionName) {
return false;
}

public void getLatest(String url, String channel, Callback callback) {
public void getLatest(String url, Callback callback) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
Expand All @@ -300,7 +300,6 @@ public void onErrorResponse(VolleyError error) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("cap_channel", channel);
params.put("cap_device_id", this.deviceID);
params.put("cap_app_id", this.appId);
params.put("cap_version_build", this.versionBuild);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ 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 autoUpdateUrlChannel = "";
private Boolean autoUpdate = false;
private Boolean disableAutoUpdateUnderNative = false;
private Boolean disableAutoUpdateToMajor = false;
Expand All @@ -50,7 +49,6 @@ public void load() {
implementation.appId = config.getString("appId", "");
implementation.statsUrl = getConfig().getString("statsUrl", statsUrlDefault);
this.autoUpdateUrl = getConfig().getString("autoUpdateUrl", autoUpdateUrlDefault);
this.autoUpdateUrlChannel = getConfig().getString("autoUpdateUrlChannel", "");
this.autoUpdate = getConfig().getBoolean("autoUpdate", false);
if (!autoUpdate || this.autoUpdateUrl.equals("")) return;
disableAutoUpdateUnderNative = getConfig().getBoolean("disableAutoUpdateUnderNative", false);
Expand Down Expand Up @@ -85,11 +83,12 @@ public void notifyDownload(int percent) {
notifyListeners("download", ret);
}

// @PluginMethod
// public void setChannel(PluginCall call) {
// autoUpdateUrlChannel = call.getString("channel", autoUpdateUrlChannel);
// call.resolve();
// }
@PluginMethod
public void getId(PluginCall call) {
JSObject ret = new JSObject();
ret.put("id", implementation.deviceID);
call.resolve(ret);
}

@PluginMethod
public void download(PluginCall call) {
Expand Down Expand Up @@ -239,7 +238,7 @@ public void onActivityStarted(@NonNull Activity activity) {
new Thread(new Runnable(){
@Override
public void run() {
implementation.getLatest(autoUpdateUrl autoUpdateUrlChannel, (res) -> {
implementation.getLatest(autoUpdateUrl, (res) -> {
try {
String currentVersion = implementation.getVersionName();
String newVersion = (String) res.get("version");
Expand Down
5 changes: 2 additions & 3 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ extension Bundle {

public var statsUrl = ""
public var appId = ""
public var deviceID = UIDevice.current.identifierForVendor?.uuidString ?? ""
public var notifyDownload: (Int) -> Void = { _ in }
private var versionBuild = Bundle.main.buildVersionNumber ?? ""
private var deviceID = UIDevice.current.identifierForVendor?.uuidString ?? ""
private var lastPathHot = ""
private var lastPathPersist = ""
private let basePathHot = "versions"
Expand Down Expand Up @@ -95,11 +95,10 @@ extension Bundle {
deleteFolder(source: destUnZip)
}

@objc public func getLatest(url: URL, channel: String) -> AppVersion? {
@objc public func getLatest(url: URL) -> AppVersion? {
let semaphore = DispatchSemaphore(value: 0)
let latest = AppVersion()
let headers = [
"cap_channel": channel,
"cap_device_id": self.deviceID,
"cap_app_id": self.appId,
"cap_version_build": self.versionBuild,
Expand Down
2 changes: 1 addition & 1 deletion ios/Plugin/CapacitorUpdaterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
CAP_PLUGIN_METHOD(versionName, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(notifyAppReady, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(delayUpdate, CAPPluginReturnPromise);
// CAP_PLUGIN_METHOD(setChannel, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getId, CAPPluginReturnPromise);
)
11 changes: 4 additions & 7 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
static let statsUrlDefault = "https://capgo.app/api/stats"
private var autoUpdateUrl = ""
private var autoUpdate = false
private var autoUpdateUrlChannel = ""
private var statsUrl = ""
private var disableAutoUpdateUnderNative = false;
private var disableAutoUpdateToMajor = false;
private var resetWhenUpdate = false;

override public func load() {
autoUpdateUrl = getConfigValue("autoUpdateUrl") as? String ?? autoUpdateUrlDefault
autoUpdateUrlChannel = getConfigValue("autoUpdateUrlChannel") as? String ?? ""
autoUpdate = getConfigValue("autoUpdate") as? Bool ?? false
implementation.appId = Bundle.main.bundleIdentifier ?? ""
implementation.notifyDownload = notifyDownload
Expand Down Expand Up @@ -64,10 +62,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
self.notifyListeners("download", data: ["percent": percent])
}

// @objc func setChannel(_ call: CAPPluginCall) {
// autoUpdateUrlChannel = call.getString("channel") ?? autoUpdateUrlChannel
// call.resolve()
// }
@objc func getId(_ call: CAPPluginCall) {
call.resolve(["id": implementation.deviceID])
}

@objc func download(_ call: CAPPluginCall) {
let url = URL(string: call.getString("url") ?? "")
Expand Down Expand Up @@ -202,7 +199,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
DispatchQueue.global(qos: .background).async {
print("✨ Capacitor-updater: Check for update in the server")
let url = URL(string: self.autoUpdateUrl)!
let res = self.implementation.getLatest(url: url, channel: self.autoUpdateUrlChannel)
let res = self.implementation.getLatest(url: url)
if (res == nil) {
return
}
Expand Down
5 changes: 5 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export interface CapacitorUpdaterPlugin {
*/
set(options: { version: string, versionName?: string }): Promise<void>;
/**
* Get unique ID used to identify device into auto update server
* @returns {Promise<{ id: string }>} an Promise with id for this device
*/
getId(): Promise<{ id: string }>;
/**
* Delete version in storage
* @returns {Promise<void>} an empty Promise when the version is delete, otherwise throw an error
* @param version The version name to delete
Expand Down
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class CapacitorUpdaterWeb
async set(options: { version: string, versionName?: string }): Promise<void> {
console.log('Cannot set version in web', options);
}
async getId(): Promise<{ id: string }> {
console.log('Cannot get ID in web');
return { id: 'default'};
}
async delete(options: { version: string }): Promise<void> {
console.log('Cannot delete version in web', options);
}
Expand Down

0 comments on commit b733873

Please sign in to comment.