@capgo/capacitor-updater 4.41.0 → 4.43.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapgoCapacitorUpdater.podspec +7 -5
- package/Package.swift +40 -0
- package/README.md +1913 -303
- package/android/build.gradle +41 -8
- package/android/proguard-rules.pro +45 -0
- package/android/src/main/AndroidManifest.xml +1 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +88 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +223 -195
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +23 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +2720 -1242
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +1854 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +359 -121
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +28 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +44 -49
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +296 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +215 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +858 -117
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +156 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +45 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +360 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +603 -0
- package/dist/docs.json +3022 -765
- package/dist/esm/definitions.d.ts +1717 -198
- package/dist/esm/definitions.js +103 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/history.d.ts +1 -0
- package/dist/esm/history.js +283 -0
- package/dist/esm/history.js.map +1 -0
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +5 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +43 -42
- package/dist/esm/web.js +122 -37
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +512 -37
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +512 -37
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +87 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BigInt.swift +55 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +177 -0
- package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/BundleStatus.swift +12 -12
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +2020 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +1959 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +313 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +257 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +392 -0
- package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +310 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +274 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +441 -0
- package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +1 -2
- package/package.json +49 -41
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1131
- package/ios/Plugin/BundleInfo.swift +0 -113
- package/ios/Plugin/CapacitorUpdater.swift +0 -850
- package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
- package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
- package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -678
- package/ios/Plugin/CryptoCipher.swift +0 -240
- /package/{LICENCE → LICENSE} +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import androidx.work.BackoffPolicy;
|
|
5
|
+
import androidx.work.Configuration;
|
|
6
|
+
import androidx.work.Constraints;
|
|
7
|
+
import androidx.work.Data;
|
|
8
|
+
import androidx.work.ExistingWorkPolicy;
|
|
9
|
+
import androidx.work.NetworkType;
|
|
10
|
+
import androidx.work.OneTimeWorkRequest;
|
|
11
|
+
import androidx.work.WorkManager;
|
|
12
|
+
import androidx.work.WorkRequest;
|
|
13
|
+
import java.util.concurrent.TimeUnit;
|
|
14
|
+
|
|
15
|
+
public class DownloadWorkerManager {
|
|
16
|
+
|
|
17
|
+
private static Logger logger;
|
|
18
|
+
|
|
19
|
+
public static void setLogger(Logger loggerInstance) {
|
|
20
|
+
logger = loggerInstance;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private static volatile boolean isInitialized = false;
|
|
24
|
+
|
|
25
|
+
private static synchronized void initializeIfNeeded(Context context) {
|
|
26
|
+
if (!isInitialized) {
|
|
27
|
+
try {
|
|
28
|
+
Configuration config = new Configuration.Builder().setMinimumLoggingLevel(android.util.Log.INFO).build();
|
|
29
|
+
WorkManager.initialize(context, config);
|
|
30
|
+
isInitialized = true;
|
|
31
|
+
} catch (IllegalStateException e) {
|
|
32
|
+
// WorkManager was already initialized, ignore
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public static boolean isVersionDownloading(Context context, String version) {
|
|
38
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
39
|
+
try {
|
|
40
|
+
return WorkManager.getInstance(context)
|
|
41
|
+
.getWorkInfosByTag(version)
|
|
42
|
+
.get()
|
|
43
|
+
.stream()
|
|
44
|
+
.anyMatch((workInfo) -> !workInfo.getState().isFinished());
|
|
45
|
+
} catch (Exception e) {
|
|
46
|
+
logger.error("Error checking download status: " + e.getMessage());
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public static void enqueueDownload(
|
|
52
|
+
Context context,
|
|
53
|
+
String url,
|
|
54
|
+
String id,
|
|
55
|
+
String documentsDir,
|
|
56
|
+
String dest,
|
|
57
|
+
String version,
|
|
58
|
+
String sessionKey,
|
|
59
|
+
String checksum,
|
|
60
|
+
String publicKey,
|
|
61
|
+
boolean isManifest,
|
|
62
|
+
boolean isEmulator,
|
|
63
|
+
String appId,
|
|
64
|
+
String pluginVersion,
|
|
65
|
+
boolean isProd,
|
|
66
|
+
String statsUrl,
|
|
67
|
+
String deviceId,
|
|
68
|
+
String versionBuild,
|
|
69
|
+
String versionCode,
|
|
70
|
+
String versionOs,
|
|
71
|
+
String customId,
|
|
72
|
+
String defaultChannel
|
|
73
|
+
) {
|
|
74
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
75
|
+
|
|
76
|
+
// Use unique work name for this bundle to prevent duplicates
|
|
77
|
+
String uniqueWorkName = "bundle_" + id + "_" + version;
|
|
78
|
+
|
|
79
|
+
// Create input data
|
|
80
|
+
Data inputData = new Data.Builder()
|
|
81
|
+
.putString(DownloadService.URL, url)
|
|
82
|
+
.putString(DownloadService.ID, id)
|
|
83
|
+
.putString(DownloadService.DOCDIR, documentsDir)
|
|
84
|
+
.putString(DownloadService.FILEDEST, dest)
|
|
85
|
+
.putString(DownloadService.VERSION, version)
|
|
86
|
+
.putString(DownloadService.SESSIONKEY, sessionKey)
|
|
87
|
+
.putString(DownloadService.CHECKSUM, checksum)
|
|
88
|
+
.putBoolean(DownloadService.IS_MANIFEST, isManifest)
|
|
89
|
+
.putString(DownloadService.PUBLIC_KEY, publicKey)
|
|
90
|
+
.putString(DownloadService.APP_ID, appId)
|
|
91
|
+
.putString(DownloadService.pluginVersion, pluginVersion)
|
|
92
|
+
.putString(DownloadService.STATS_URL, statsUrl)
|
|
93
|
+
.putString(DownloadService.DEVICE_ID, deviceId)
|
|
94
|
+
.putString(DownloadService.VERSION_BUILD, versionBuild)
|
|
95
|
+
.putString(DownloadService.VERSION_CODE, versionCode)
|
|
96
|
+
.putString(DownloadService.VERSION_OS, versionOs)
|
|
97
|
+
.putString(DownloadService.CUSTOM_ID, customId)
|
|
98
|
+
.putString(DownloadService.DEFAULT_CHANNEL, defaultChannel)
|
|
99
|
+
.putBoolean(DownloadService.IS_PROD, isProd)
|
|
100
|
+
.putBoolean(DownloadService.IS_EMULATOR, isEmulator)
|
|
101
|
+
.build();
|
|
102
|
+
|
|
103
|
+
// Create network constraints - be more lenient on emulators
|
|
104
|
+
Constraints.Builder constraintsBuilder = new Constraints.Builder();
|
|
105
|
+
if (isEmulator) {
|
|
106
|
+
logger.info("Emulator detected - using lenient network constraints");
|
|
107
|
+
// On emulators, use NOT_REQUIRED to avoid background network issues
|
|
108
|
+
constraintsBuilder.setRequiredNetworkType(NetworkType.NOT_REQUIRED);
|
|
109
|
+
} else {
|
|
110
|
+
constraintsBuilder.setRequiredNetworkType(NetworkType.CONNECTED);
|
|
111
|
+
}
|
|
112
|
+
Constraints constraints = constraintsBuilder.build();
|
|
113
|
+
|
|
114
|
+
// Create work request with tags for tracking
|
|
115
|
+
OneTimeWorkRequest.Builder workRequestBuilder = new OneTimeWorkRequest.Builder(DownloadService.class)
|
|
116
|
+
.setConstraints(constraints)
|
|
117
|
+
.setInputData(inputData)
|
|
118
|
+
.addTag(id)
|
|
119
|
+
.addTag(version)
|
|
120
|
+
.addTag("capacitor_updater_download");
|
|
121
|
+
|
|
122
|
+
// More aggressive retry policy for emulators
|
|
123
|
+
if (isEmulator) {
|
|
124
|
+
workRequestBuilder.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS);
|
|
125
|
+
} else {
|
|
126
|
+
workRequestBuilder.setBackoffCriteria(BackoffPolicy.LINEAR, WorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
OneTimeWorkRequest workRequest = workRequestBuilder.build();
|
|
130
|
+
|
|
131
|
+
// Use beginUniqueWork to prevent duplicate downloads
|
|
132
|
+
WorkManager.getInstance(context)
|
|
133
|
+
.beginUniqueWork(
|
|
134
|
+
uniqueWorkName,
|
|
135
|
+
ExistingWorkPolicy.KEEP, // Don't start if already running
|
|
136
|
+
workRequest
|
|
137
|
+
)
|
|
138
|
+
.enqueue();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public static void cancelVersionDownload(Context context, String version) {
|
|
142
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
143
|
+
WorkManager.getInstance(context).cancelAllWorkByTag(version);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public static void cancelBundleDownload(Context context, String id, String version) {
|
|
147
|
+
String uniqueWorkName = "bundle_" + id + "_" + version;
|
|
148
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
149
|
+
WorkManager.getInstance(context).cancelUniqueWork(uniqueWorkName);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public static void cancelAllDownloads(Context context) {
|
|
153
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
154
|
+
WorkManager.getInstance(context).cancelAllWorkByTag("capacitor_updater_download");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.content.pm.PackageInfo;
|
|
4
|
+
import android.content.pm.PackageManager;
|
|
5
|
+
import android.os.Build;
|
|
6
|
+
import com.getcapacitor.JSObject;
|
|
7
|
+
import java.util.Map;
|
|
8
|
+
|
|
9
|
+
public class InternalUtils {
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Converts a Map to JSObject for proper bridge serialization.
|
|
13
|
+
*/
|
|
14
|
+
public static JSObject mapToJSObject(Map<String, Object> map) {
|
|
15
|
+
JSObject jsObject = new JSObject();
|
|
16
|
+
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
17
|
+
jsObject.put(entry.getKey(), entry.getValue());
|
|
18
|
+
}
|
|
19
|
+
return jsObject;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static String getPackageName(PackageManager pm, String packageName) {
|
|
23
|
+
try {
|
|
24
|
+
PackageInfo pInfo = getPackageInfoInternal(pm, packageName);
|
|
25
|
+
return (pInfo != null) ? pInfo.packageName : null;
|
|
26
|
+
} catch (PackageManager.NameNotFoundException e) {
|
|
27
|
+
// Exception is handled internally, and null is returned to indicate the package name could not be retrieved
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private static PackageInfo getPackageInfoInternal(PackageManager pm, String packageName) throws PackageManager.NameNotFoundException {
|
|
33
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
34
|
+
return pm.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0));
|
|
35
|
+
} else {
|
|
36
|
+
return getPackageInfoLegacy(pm, packageName, (int) (long) 0);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@SuppressWarnings("deprecation")
|
|
41
|
+
private static PackageInfo getPackageInfoLegacy(PackageManager pm, String packageName, int flags)
|
|
42
|
+
throws PackageManager.NameNotFoundException {
|
|
43
|
+
return pm.getPackageInfo(packageName, flags);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.util.ArrayMap;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
import androidx.annotation.NonNull;
|
|
6
|
+
import androidx.annotation.Nullable;
|
|
7
|
+
import com.getcapacitor.*;
|
|
8
|
+
import java.util.ArrayList;
|
|
9
|
+
import java.util.Arrays;
|
|
10
|
+
import java.util.Date;
|
|
11
|
+
import java.util.Locale;
|
|
12
|
+
import java.util.Map;
|
|
13
|
+
import org.jetbrains.annotations.Contract;
|
|
14
|
+
|
|
15
|
+
public class Logger {
|
|
16
|
+
|
|
17
|
+
private Bridge bridge;
|
|
18
|
+
|
|
19
|
+
public enum LogLevel {
|
|
20
|
+
silent,
|
|
21
|
+
error,
|
|
22
|
+
warn,
|
|
23
|
+
info,
|
|
24
|
+
debug
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static class Options {
|
|
28
|
+
|
|
29
|
+
LogLevel level;
|
|
30
|
+
Map<String, String> labels;
|
|
31
|
+
boolean useSystemLog;
|
|
32
|
+
|
|
33
|
+
Options() {
|
|
34
|
+
level = LogLevel.info;
|
|
35
|
+
labels = null;
|
|
36
|
+
useSystemLog = true; // Default to true for backward compatibility
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
Options(@NonNull Options other) {
|
|
40
|
+
level = other.level;
|
|
41
|
+
labels = other.labels;
|
|
42
|
+
useSystemLog = other.useSystemLog;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
Options(LogLevel level) {
|
|
46
|
+
this.level = level;
|
|
47
|
+
this.labels = null;
|
|
48
|
+
useSystemLog = true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
Options(Map<String, String> labels) {
|
|
52
|
+
this.level = LogLevel.info;
|
|
53
|
+
this.labels = labels;
|
|
54
|
+
useSystemLog = true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Options(LogLevel level, Map<String, String> labels) {
|
|
58
|
+
this.level = level;
|
|
59
|
+
this.labels = labels;
|
|
60
|
+
useSystemLog = true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
Options(LogLevel level, Map<String, String> labels, boolean useSystemLog) {
|
|
64
|
+
this.level = level;
|
|
65
|
+
this.labels = labels;
|
|
66
|
+
this.useSystemLog = useSystemLog;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
Options(boolean useSystemLog) {
|
|
70
|
+
this.level = LogLevel.info;
|
|
71
|
+
this.labels = null;
|
|
72
|
+
this.useSystemLog = useSystemLog;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public LogLevel level;
|
|
77
|
+
private final Map<LogLevel, String> labels = new ArrayMap<>();
|
|
78
|
+
private String tag;
|
|
79
|
+
private final ArrayMap<String, Long> timers = new ArrayMap<>();
|
|
80
|
+
private final String kDefaultTimerLabel = "default";
|
|
81
|
+
private boolean useSystemLog;
|
|
82
|
+
|
|
83
|
+
public void setBridge(Bridge bridge) {
|
|
84
|
+
this.bridge = bridge;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public Logger(String tag) {
|
|
88
|
+
super();
|
|
89
|
+
init(tag, new Options());
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public Logger(String tag, Options options) {
|
|
93
|
+
super();
|
|
94
|
+
init(tag, options);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private void init(String tag, @NonNull Options options) {
|
|
98
|
+
this.level = options.level;
|
|
99
|
+
this.useSystemLog = options.useSystemLog;
|
|
100
|
+
this.labels.putAll(
|
|
101
|
+
Map.of(LogLevel.silent, "", LogLevel.error, "🔴", LogLevel.warn, "🟠", LogLevel.info, "🟢", LogLevel.debug, "\uD83D\uDD0E")
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (options.labels != null) {
|
|
105
|
+
setLabels(options.labels);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
this.tag = tag;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
@Nullable
|
|
112
|
+
public Object getLevelWithName(String name) {
|
|
113
|
+
try {
|
|
114
|
+
return LogLevel.valueOf(name);
|
|
115
|
+
} catch (IllegalArgumentException ex) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public String getLevelName() {
|
|
121
|
+
return level.name();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public void setLevelName(String name) {
|
|
125
|
+
try {
|
|
126
|
+
level = LogLevel.valueOf(name);
|
|
127
|
+
} catch (IllegalArgumentException e) {
|
|
128
|
+
// Ignore it
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public Map<String, String> getLabels() {
|
|
133
|
+
ArrayMap<String, String> result = new ArrayMap<>();
|
|
134
|
+
|
|
135
|
+
for (Map.Entry<LogLevel, String> entry : labels.entrySet()) {
|
|
136
|
+
result.put(entry.getKey().name(), entry.getValue());
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public void setLabels(@NonNull Map<String, String> labels) {
|
|
143
|
+
for (Map.Entry<String, String> entry : labels.entrySet()) {
|
|
144
|
+
Object level = getLevelWithName(entry.getKey());
|
|
145
|
+
|
|
146
|
+
if (level != null) {
|
|
147
|
+
this.labels.put((LogLevel) level, entry.getValue());
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public String getTag() {
|
|
153
|
+
return tag;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public void setTag(@NonNull String tag) {
|
|
157
|
+
if (!tag.isEmpty()) {
|
|
158
|
+
this.tag = tag;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public void error(String message) {
|
|
163
|
+
logWithTagAtLevel(LogLevel.error, "", tag, message);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
public void warn(String message) {
|
|
167
|
+
logWithTagAtLevel(LogLevel.warn, "", tag, message);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public void info(String message) {
|
|
171
|
+
logWithTagAtLevel(LogLevel.info, "", tag, message);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
public void log(String message) {
|
|
175
|
+
info(message);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public void debug(String message) {
|
|
179
|
+
logWithTagAtLevel(LogLevel.debug, "", tag, message);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public void logAtLevel(LogLevel level, String message) {
|
|
183
|
+
logWithTagAtLevel(level, "", tag, message);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public void logAtLevel(String level, String message) {
|
|
187
|
+
logWithTagAtLevel(resolveLevelName(level), "", tag, message);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public void logWithTagAtLevel(LogLevel level, String tag, String message) {
|
|
191
|
+
logWithTagAtLevel(level, "", tag, message);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public void logWithTagAtLevel(String level, String tag, String message) {
|
|
195
|
+
logWithTagAtLevel(resolveLevelName(level), "", tag, message);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private LogLevel resolveLevelName(String name) {
|
|
199
|
+
Object level = getLevelWithName(name);
|
|
200
|
+
|
|
201
|
+
if (level != null) {
|
|
202
|
+
return (LogLevel) level;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return LogLevel.info;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
public void logWithTagAtLevel(LogLevel level, String label, String tag, String message) {
|
|
209
|
+
if (this.level.compareTo(level) < 0) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (label.isEmpty()) {
|
|
214
|
+
label = labels.get(level);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// If the label is ASCII, surround it with []
|
|
218
|
+
String format;
|
|
219
|
+
|
|
220
|
+
if (label != null) {
|
|
221
|
+
format = label.charAt(0) <= 127 ? "[%s]: %s" : "%s %s";
|
|
222
|
+
} else {
|
|
223
|
+
label = "";
|
|
224
|
+
format = "%s%s";
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
String formattedMessage = String.format(format, label, message);
|
|
228
|
+
|
|
229
|
+
if (tag.isEmpty()) {
|
|
230
|
+
tag = this.tag;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Log to Android system log if enabled
|
|
234
|
+
if (useSystemLog) {
|
|
235
|
+
switch (level) {
|
|
236
|
+
case error:
|
|
237
|
+
Log.e(tag, formattedMessage);
|
|
238
|
+
break;
|
|
239
|
+
case warn:
|
|
240
|
+
Log.w(tag, formattedMessage);
|
|
241
|
+
break;
|
|
242
|
+
case info:
|
|
243
|
+
Log.i(tag, formattedMessage);
|
|
244
|
+
break;
|
|
245
|
+
case debug:
|
|
246
|
+
Log.d(tag, formattedMessage);
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Send to JavaScript if webView is available
|
|
252
|
+
if (bridge != null && bridge.getWebView() != null) {
|
|
253
|
+
bridge.eval(
|
|
254
|
+
"console." + level.name() + "(\"[" + tag.replace("\"", "\\\"") + "] " + formattedMessage.replace("\"", "\\\"") + "\")",
|
|
255
|
+
null
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
public void dir(Object value) {
|
|
261
|
+
String message;
|
|
262
|
+
|
|
263
|
+
if (value != null) {
|
|
264
|
+
if (value.getClass().isArray()) {
|
|
265
|
+
Object[] arr = (Object[]) value;
|
|
266
|
+
message = Arrays.deepToString(arr);
|
|
267
|
+
} else {
|
|
268
|
+
try {
|
|
269
|
+
message = value.toString();
|
|
270
|
+
} catch (Exception ex) {
|
|
271
|
+
message = String.format("<%s>", ex.getMessage());
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
} else {
|
|
275
|
+
message = "null";
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
info(message);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
public void time() {
|
|
282
|
+
time(kDefaultTimerLabel);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
public void time(String label) {
|
|
286
|
+
timers.put(label, new Date().getTime());
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
public void timeLog() {
|
|
290
|
+
timeLog(kDefaultTimerLabel);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
public void timeLog(String label) {
|
|
294
|
+
label = resolveTimerLabel(label);
|
|
295
|
+
|
|
296
|
+
if (timers.containsKey(label)) {
|
|
297
|
+
Long start = timers.get(label);
|
|
298
|
+
|
|
299
|
+
// This will always be true
|
|
300
|
+
if (start != null) {
|
|
301
|
+
long now = new Date().getTime();
|
|
302
|
+
long diff = now - start;
|
|
303
|
+
info(String.format(Locale.getDefault(), "%s: %s", label, formatMilliseconds(diff)));
|
|
304
|
+
}
|
|
305
|
+
} else {
|
|
306
|
+
warn(String.format("timer '%s' does not exist", label));
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
public void timeEnd() {
|
|
311
|
+
timeEnd(kDefaultTimerLabel);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
public void timeEnd(String label) {
|
|
315
|
+
label = resolveTimerLabel(label);
|
|
316
|
+
timeLog(label);
|
|
317
|
+
timers.remove(label);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
@Contract(pure = true)
|
|
321
|
+
private String resolveTimerLabel(@NonNull String label) {
|
|
322
|
+
return label.isEmpty() ? kDefaultTimerLabel : label;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
@NonNull
|
|
326
|
+
private String formatMilliseconds(long milliseconds) {
|
|
327
|
+
long seconds = Math.floorDiv(milliseconds, 1000);
|
|
328
|
+
long minutes = Math.floorDiv(seconds, 60);
|
|
329
|
+
long hours = Math.floorDiv(minutes, 60);
|
|
330
|
+
long millis = milliseconds % 1000;
|
|
331
|
+
|
|
332
|
+
if (seconds < 1) {
|
|
333
|
+
return String.format(Locale.getDefault(), "%dms", milliseconds);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (minutes < 1) {
|
|
337
|
+
return String.format(Locale.getDefault(), "%d.%ds", seconds, millis);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
seconds = seconds % 60;
|
|
341
|
+
|
|
342
|
+
if (hours < 1) {
|
|
343
|
+
return String.format(Locale.getDefault(), "%d:%02d.%03d (min:sec.ms)", minutes, seconds, millis);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
minutes = minutes % 60;
|
|
347
|
+
return String.format(Locale.getDefault(), "%d:%02d:%02d (hr:min:sec)", hours, minutes, seconds);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
public void trace() {
|
|
351
|
+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
|
352
|
+
ArrayList<String> stack = new ArrayList<>();
|
|
353
|
+
|
|
354
|
+
for (StackTraceElement element : stackTrace) {
|
|
355
|
+
stack.add(element.toString());
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
info("trace\n" + String.join("\n", stack));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
package ee.forgr.capacitor_updater;
|
|
8
|
+
|
|
9
|
+
import android.hardware.Sensor;
|
|
10
|
+
import android.hardware.SensorEvent;
|
|
11
|
+
import android.hardware.SensorEventListener;
|
|
12
|
+
import android.hardware.SensorManager;
|
|
13
|
+
|
|
14
|
+
public class ShakeDetector implements SensorEventListener {
|
|
15
|
+
|
|
16
|
+
public interface Listener {
|
|
17
|
+
void onShakeDetected();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private static final float SHAKE_THRESHOLD = 12.0f; // Acceleration threshold for shake detection
|
|
21
|
+
private static final int SHAKE_TIMEOUT = 500; // Minimum time between shake events (ms)
|
|
22
|
+
|
|
23
|
+
private Listener listener;
|
|
24
|
+
private SensorManager sensorManager;
|
|
25
|
+
private Sensor accelerometer;
|
|
26
|
+
private long lastShakeTime = 0;
|
|
27
|
+
|
|
28
|
+
public ShakeDetector(Listener listener) {
|
|
29
|
+
this.listener = listener;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public void start(SensorManager sensorManager) {
|
|
33
|
+
this.sensorManager = sensorManager;
|
|
34
|
+
this.accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
|
35
|
+
|
|
36
|
+
if (accelerometer != null) {
|
|
37
|
+
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public void stop() {
|
|
42
|
+
if (sensorManager != null) {
|
|
43
|
+
sensorManager.unregisterListener(this);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Override
|
|
48
|
+
public void onSensorChanged(SensorEvent event) {
|
|
49
|
+
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
|
|
50
|
+
float x = event.values[0];
|
|
51
|
+
float y = event.values[1];
|
|
52
|
+
float z = event.values[2];
|
|
53
|
+
|
|
54
|
+
// Calculate the acceleration magnitude (excluding gravity)
|
|
55
|
+
float acceleration = (float) Math.sqrt(x * x + y * y + z * z) - SensorManager.GRAVITY_EARTH;
|
|
56
|
+
|
|
57
|
+
// Check if acceleration exceeds threshold and enough time has passed
|
|
58
|
+
long currentTime = System.currentTimeMillis();
|
|
59
|
+
if (Math.abs(acceleration) > SHAKE_THRESHOLD && currentTime - lastShakeTime > SHAKE_TIMEOUT) {
|
|
60
|
+
lastShakeTime = currentTime;
|
|
61
|
+
if (listener != null) {
|
|
62
|
+
listener.onShakeDetected();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@Override
|
|
69
|
+
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
|
70
|
+
// Not needed for shake detection
|
|
71
|
+
}
|
|
72
|
+
}
|