Skip to content

Commit

Permalink
feat: add disableAutoUpdateUnderNative and disableAutoUpdateToMajor c…
Browse files Browse the repository at this point in the history
…apability
  • Loading branch information
riderx committed Mar 26, 2022
1 parent e666056 commit 7e0d1d2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CapacitorUpdater.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ Pod::Spec.new do |s|
s.dependency 'Capacitor'
s.dependency 'SSZipArchive'
s.dependency 'Alamofire'
s.dependency 'VersionCompare'
s.swift_version = '5.1'
end
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies {
implementation project(':capacitor-android')
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
implementation 'com.android.volley:volley:1.2.1'
implementation 'io.github.g00fy2:versioncompare:1.5.0'
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.util.Log;

Expand All @@ -16,6 +17,7 @@
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import io.github.g00fy2.versioncompare.Version;

import org.json.JSONException;

Expand All @@ -29,6 +31,9 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private String autoUpdateUrl = null;
private Boolean disableAutoUpdateUnderNative = false;
private Boolean disableAutoUpdateToMajor = false;


@Override
public void load() {
Expand All @@ -41,6 +46,8 @@ public void load() {
implementation.statsUrl = getConfig().getString("statsUrl", "https://capgo.app/api/stats");
this.autoUpdateUrl = getConfig().getString("autoUpdateUrl");
if (this.autoUpdateUrl == null || this.autoUpdateUrl.equals("")) return;
disableAutoUpdateUnderNative = getConfig().getBoolean("disableAutoUpdateUnderNative", false);
disableAutoUpdateToMajor = getConfig().getBoolean("disableAutoUpdateBreaking", false);
Application application = (Application) this.getContext().getApplicationContext();
application.registerActivityLifecycleCallbacks(this);
onActivityStarted(getActivity());
Expand Down Expand Up @@ -185,8 +192,18 @@ public void cancelDelay(PluginCall call) {

@Override
public void onActivityStarted(@NonNull Activity activity) {
// disableRevert disableBreaking
String currentVersionNative = "";
try {
PackageInfo pInfo = this.getContext().getPackageManager().getPackageInfo(this.getContext().getPackageName(), 0);
currentVersionNative = pInfo.versionName;
} catch (Exception ex) {
Log.e(TAG, "Error get stats", ex);
return;
}
Log.i(TAG, "Check for update in the server");
if (autoUpdateUrl == null || autoUpdateUrl.equals("")) return;
String finalCurrentVersionNative = currentVersionNative;
new Thread(new Runnable(){
@Override
public void run() {
Expand All @@ -195,7 +212,13 @@ public void run() {
String currentVersion = implementation.getVersionName();
String newVersion = (String) res.get("version");
String failingVersion = prefs.getString("failingVersion", "");
if (!newVersion.equals(currentVersion) && !newVersion.equals(failingVersion)) {
if (disableAutoUpdateUnderNative && new Version(newVersion).isHigherThan(finalCurrentVersionNative)) {
Log.i(TAG, "Cannot download revert, " + newVersion + " is lest than native version " + finalCurrentVersionNative);
}
else if (disableAutoUpdateToMajor && new Version(newVersion).getMajor() > new Version(currentVersion).getMajor()) {
Log.i(TAG, "Cannot download Major, " + newVersion + " is Breaking change from " + currentVersion);
}
else if (!newVersion.equals("") && !newVersion.equals(currentVersion) && !newVersion.equals(failingVersion)) {
new Thread(new Runnable(){
@Override
public void run() {
Expand Down
16 changes: 14 additions & 2 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import Capacitor
import VersionCompare

/**
* Please read the Capacitor iOS Plugin Development Guide
Expand All @@ -10,6 +11,8 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
private var implementation = CapacitorUpdater()
private var autoUpdateUrl = ""
private var statsUrl = ""
private var disableAutoUpdateUnderNative = false;
private var disableAutoUpdateToMajor = false;

override public func load() {
autoUpdateUrl = getConfigValue("autoUpdateUrl") as? String ?? ""
Expand All @@ -21,6 +24,8 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}
implementation.statsUrl = getConfigValue("statsUrl") as? String ?? "https://capgo.app/api/stats"
if (autoUpdateUrl == "") { return }
disableAutoUpdateUnderNative = getConfigValue("disableAutoUpdateUnderNative") as? Bool ?? false
disableAutoUpdateToMajor = getConfigValue("disableAutoUpdateBreaking") as? Bool ?? false
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
nc.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
Expand Down Expand Up @@ -173,8 +178,15 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}
let currentVersion = self.implementation.getVersionName()
let failingVersion = UserDefaults.standard.string(forKey: "failingVersion") ?? ""
let newVersion = res?.version ?? ""
if (newVersion != "" && newVersion != currentVersion && newVersion != failingVersion) {
let newVersion: Version = res?.version ?? ""
let currentVersionNative: Version = Bundle.main.buildVersionNumber
if (newVersion < currentVersionNative) {
print("✨ Capacitor-updater: Cannot download revert, \(newVersion) is lest than native version \(currentVersionNative)")
}
else if () {
print("✨ Capacitor-updater: Cannot download Major, \(newVersion) is Breaking change from \(currentVersion)")
}
else if (newVersion != "" && newVersion != currentVersion && newVersion != failingVersion) {
let dlOp = self.implementation.download(url: downloadUrl)
if let dl = dlOp {
print("✨ Capacitor-updater: New version: \(newVersion) found. Current is \(currentVersion == "" ? "builtin" : currentVersion), next backgrounding will trigger update")
Expand Down

0 comments on commit 7e0d1d2

Please sign in to comment.