@capgo/capacitor-device-info 8.0.0
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/CapgoCapacitorDeviceInfo.podspec +17 -0
- package/LICENSE +373 -0
- package/Package.swift +28 -0
- package/README.md +426 -0
- package/android/build.gradle +59 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/app/capgo/deviceinfo/DeviceInfo.java +765 -0
- package/android/src/main/java/app/capgo/deviceinfo/DeviceInfoPlugin.java +195 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +1285 -0
- package/dist/esm/definitions.d.ts +612 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +29 -0
- package/dist/esm/web.js +182 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +196 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +199 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/DeviceInfoPlugin/DeviceInfo.swift +279 -0
- package/ios/Sources/DeviceInfoPlugin/DeviceInfoPlugin.swift +157 -0
- package/ios/Tests/DeviceInfoPluginTests/DeviceInfoTests.swift +22 -0
- package/package.json +95 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
package app.capgo.deviceinfo;
|
|
2
|
+
|
|
3
|
+
import com.getcapacitor.JSObject;
|
|
4
|
+
import com.getcapacitor.Plugin;
|
|
5
|
+
import com.getcapacitor.PluginCall;
|
|
6
|
+
import com.getcapacitor.PluginMethod;
|
|
7
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
8
|
+
import java.util.concurrent.ExecutorService;
|
|
9
|
+
import java.util.concurrent.Executors;
|
|
10
|
+
import java.util.concurrent.ScheduledExecutorService;
|
|
11
|
+
import java.util.concurrent.ScheduledFuture;
|
|
12
|
+
import java.util.concurrent.TimeUnit;
|
|
13
|
+
|
|
14
|
+
@CapacitorPlugin(name = "DeviceInfo")
|
|
15
|
+
public class DeviceInfoPlugin extends Plugin {
|
|
16
|
+
|
|
17
|
+
private static final String EVENT_DEVICE_INFO_UPDATE = "deviceInfoUpdate";
|
|
18
|
+
private static final int DEFAULT_INTERVAL_MS = 1000;
|
|
19
|
+
private static final int MIN_INTERVAL_MS = 250;
|
|
20
|
+
|
|
21
|
+
private final DeviceInfo implementation = new DeviceInfo();
|
|
22
|
+
private final Object monitoringLock = new Object();
|
|
23
|
+
private final ExecutorService readExecutor = Executors.newSingleThreadExecutor();
|
|
24
|
+
|
|
25
|
+
private ScheduledExecutorService executorService;
|
|
26
|
+
private ScheduledFuture<?> monitoringTask;
|
|
27
|
+
private int intervalMs;
|
|
28
|
+
private long startedAt;
|
|
29
|
+
private int samplesEmitted;
|
|
30
|
+
private Integer sampleLimit;
|
|
31
|
+
private Long stopAt;
|
|
32
|
+
|
|
33
|
+
@PluginMethod
|
|
34
|
+
public void getInfo(PluginCall call) {
|
|
35
|
+
readExecutor.execute(() -> resolveCall(call, implementation.getInfo(getContext())));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@PluginMethod
|
|
39
|
+
public void startMonitoring(PluginCall call) {
|
|
40
|
+
int effectiveIntervalMs = Math.max(call.getInt("intervalMs", DEFAULT_INTERVAL_MS), MIN_INTERVAL_MS);
|
|
41
|
+
Integer requestedSampleLimit = call.getInt("sampleCount", null);
|
|
42
|
+
Double requestedDurationMs = call.getDouble("durationMs");
|
|
43
|
+
boolean emitImmediately = call.getBoolean("emitImmediately", true);
|
|
44
|
+
long now = System.currentTimeMillis();
|
|
45
|
+
|
|
46
|
+
synchronized (monitoringLock) {
|
|
47
|
+
stopMonitoringInternal();
|
|
48
|
+
intervalMs = effectiveIntervalMs;
|
|
49
|
+
startedAt = now;
|
|
50
|
+
samplesEmitted = 0;
|
|
51
|
+
sampleLimit = requestedSampleLimit != null && requestedSampleLimit > 0 ? requestedSampleLimit : null;
|
|
52
|
+
stopAt = requestedDurationMs != null && requestedDurationMs > 0 ? now + requestedDurationMs.longValue() : null;
|
|
53
|
+
executorService = Executors.newSingleThreadScheduledExecutor();
|
|
54
|
+
|
|
55
|
+
if (emitImmediately) {
|
|
56
|
+
executorService.execute(this::emitSample);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (isMonitoringLocked()) {
|
|
60
|
+
monitoringTask = executorService.scheduleAtFixedRate(
|
|
61
|
+
this::emitSample,
|
|
62
|
+
effectiveIntervalMs,
|
|
63
|
+
effectiveIntervalMs,
|
|
64
|
+
TimeUnit.MILLISECONDS
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
JSObject ret = new JSObject();
|
|
70
|
+
ret.put("monitoring", isMonitoring());
|
|
71
|
+
ret.put("intervalMs", effectiveIntervalMs);
|
|
72
|
+
ret.put("startedAt", (double) now);
|
|
73
|
+
call.resolve(ret);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@PluginMethod
|
|
77
|
+
public void stopMonitoring(PluginCall call) {
|
|
78
|
+
synchronized (monitoringLock) {
|
|
79
|
+
stopMonitoringInternal();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
JSObject ret = new JSObject();
|
|
83
|
+
ret.put("monitoring", false);
|
|
84
|
+
call.resolve(ret);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@PluginMethod
|
|
88
|
+
public void isMonitoring(PluginCall call) {
|
|
89
|
+
JSObject ret = new JSObject();
|
|
90
|
+
|
|
91
|
+
synchronized (monitoringLock) {
|
|
92
|
+
boolean monitoring = isMonitoringLocked();
|
|
93
|
+
ret.put("monitoring", monitoring);
|
|
94
|
+
if (monitoring) {
|
|
95
|
+
ret.put("intervalMs", intervalMs);
|
|
96
|
+
ret.put("startedAt", (double) startedAt);
|
|
97
|
+
ret.put("samplesEmitted", samplesEmitted);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
call.resolve(ret);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@PluginMethod
|
|
105
|
+
public void removeAllListeners(PluginCall call) {
|
|
106
|
+
super.removeAllListeners(call);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@PluginMethod
|
|
110
|
+
public void getPluginVersion(PluginCall call) {
|
|
111
|
+
JSObject ret = new JSObject();
|
|
112
|
+
ret.put("version", implementation.getPluginVersion());
|
|
113
|
+
call.resolve(ret);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@Override
|
|
117
|
+
protected void handleOnDestroy() {
|
|
118
|
+
synchronized (monitoringLock) {
|
|
119
|
+
stopMonitoringInternal();
|
|
120
|
+
}
|
|
121
|
+
readExecutor.shutdownNow();
|
|
122
|
+
super.handleOnDestroy();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private void resolveCall(PluginCall call, JSObject data) {
|
|
126
|
+
if (getActivity() != null) {
|
|
127
|
+
getActivity().runOnUiThread(() -> call.resolve(data));
|
|
128
|
+
} else {
|
|
129
|
+
call.resolve(data);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private void emitSample() {
|
|
134
|
+
JSObject sample;
|
|
135
|
+
boolean shouldStop;
|
|
136
|
+
|
|
137
|
+
synchronized (monitoringLock) {
|
|
138
|
+
if (!isMonitoringLocked()) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
sample = implementation.getInfo(getContext());
|
|
143
|
+
samplesEmitted += 1;
|
|
144
|
+
sample.put("sequence", samplesEmitted);
|
|
145
|
+
sample.put("startedAt", (double) startedAt);
|
|
146
|
+
sample.put("elapsedMs", System.currentTimeMillis() - startedAt);
|
|
147
|
+
shouldStop = shouldStopMonitoringLocked();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (getActivity() != null) {
|
|
151
|
+
getActivity().runOnUiThread(() -> notifyListeners(EVENT_DEVICE_INFO_UPDATE, sample));
|
|
152
|
+
} else {
|
|
153
|
+
notifyListeners(EVENT_DEVICE_INFO_UPDATE, sample);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (shouldStop) {
|
|
157
|
+
synchronized (monitoringLock) {
|
|
158
|
+
stopMonitoringInternal();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private boolean shouldStopMonitoringLocked() {
|
|
164
|
+
if (sampleLimit != null && samplesEmitted >= sampleLimit) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
return stopAt != null && System.currentTimeMillis() >= stopAt;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private boolean isMonitoring() {
|
|
171
|
+
synchronized (monitoringLock) {
|
|
172
|
+
return isMonitoringLocked();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private boolean isMonitoringLocked() {
|
|
177
|
+
return executorService != null && !executorService.isShutdown() && startedAt > 0;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private void stopMonitoringInternal() {
|
|
181
|
+
if (monitoringTask != null) {
|
|
182
|
+
monitoringTask.cancel(false);
|
|
183
|
+
monitoringTask = null;
|
|
184
|
+
}
|
|
185
|
+
if (executorService != null) {
|
|
186
|
+
executorService.shutdownNow();
|
|
187
|
+
executorService = null;
|
|
188
|
+
}
|
|
189
|
+
intervalMs = 0;
|
|
190
|
+
startedAt = 0;
|
|
191
|
+
samplesEmitted = 0;
|
|
192
|
+
sampleLimit = null;
|
|
193
|
+
stopAt = null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
File without changes
|