@capgo/capacitor-accelerometer 7.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.
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = 'CapgoCapacitorAccelerometer'
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.license = package['license']
10
+ s.homepage = package['repository']['url']
11
+ s.author = package['author']
12
+ s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
13
+ s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
14
+ s.ios.deployment_target = '14.0'
15
+ s.dependency 'Capacitor'
16
+ s.swift_version = '5.1'
17
+ end
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Capgo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/Package.swift ADDED
@@ -0,0 +1,28 @@
1
+ // swift-tools-version: 5.9
2
+ import PackageDescription
3
+
4
+ let package = Package(
5
+ name: "CapgoCapacitorAccelerometer",
6
+ platforms: [.iOS(.v14)],
7
+ products: [
8
+ .library(
9
+ name: "CapgoCapacitorAccelerometer",
10
+ targets: ["CapacitorAccelerometerPlugin"])
11
+ ],
12
+ dependencies: [
13
+ .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.0.0")
14
+ ],
15
+ targets: [
16
+ .target(
17
+ name: "CapacitorAccelerometerPlugin",
18
+ dependencies: [
19
+ .product(name: "Capacitor", package: "capacitor-swift-pm"),
20
+ .product(name: "Cordova", package: "capacitor-swift-pm")
21
+ ],
22
+ path: "ios/Sources/CapacitorAccelerometerPlugin"),
23
+ .testTarget(
24
+ name: "CapacitorAccelerometerPluginTests",
25
+ dependencies: ["CapacitorAccelerometerPlugin"],
26
+ path: "ios/Tests/CapacitorAccelerometerPluginTests")
27
+ ]
28
+ )
package/README.md ADDED
@@ -0,0 +1,243 @@
1
+ # @capgo/capacitor-accelerometer
2
+ <a href="https://capgo.app/"><img src='https://raw.githubusercontent.com/Cap-go/capgo/main/assets/capgo_banner.png' alt='Capgo - Instant updates for capacitor'/></a>
3
+
4
+ <div align="center">
5
+ <h2><a href="https://capgo.app/?ref=plugin"> ➡️ Get Instant updates for your App with Capgo</a></h2>
6
+ <h2><a href="https://capgo.app/consulting/?ref=plugin"> Missing a feature? We’ll build the plugin for you 💪</a></h2>
7
+ </div>
8
+
9
+
10
+ Access raw accelerometer measurements across iOS, Android, and the Web.
11
+
12
+ WIP: the plugin is not yet ready for production
13
+
14
+ ## Documentation
15
+
16
+ The most complete doc is available here: https://capgo.app/docs/plugins/accelerometer/
17
+
18
+ ## Install
19
+
20
+ ```bash
21
+ npm install @capgo/capacitor-accelerometer
22
+ npx cap sync
23
+ ```
24
+
25
+ ## API
26
+
27
+ <docgen-index>
28
+
29
+ * [`getMeasurement()`](#getmeasurement)
30
+ * [`isAvailable()`](#isavailable)
31
+ * [`startMeasurementUpdates()`](#startmeasurementupdates)
32
+ * [`stopMeasurementUpdates()`](#stopmeasurementupdates)
33
+ * [`checkPermissions()`](#checkpermissions)
34
+ * [`requestPermissions()`](#requestpermissions)
35
+ * [`addListener('measurement', ...)`](#addlistenermeasurement-)
36
+ * [`removeAllListeners()`](#removealllisteners)
37
+ * [Interfaces](#interfaces)
38
+ * [Type Aliases](#type-aliases)
39
+
40
+ </docgen-index>
41
+
42
+ <docgen-api>
43
+ <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
44
+
45
+ Capacitor plugin contract for working with the device accelerometer.
46
+
47
+ ### getMeasurement()
48
+
49
+ ```typescript
50
+ getMeasurement() => Promise<GetMeasurementResult>
51
+ ```
52
+
53
+ Get the most recent accelerometer sample that was recorded by the native layer.
54
+
55
+ **Returns:** <code>Promise&lt;<a href="#measurement">Measurement</a>&gt;</code>
56
+
57
+ **Since:** 1.0.0
58
+
59
+ --------------------
60
+
61
+
62
+ ### isAvailable()
63
+
64
+ ```typescript
65
+ isAvailable() => Promise<IsAvailableResult>
66
+ ```
67
+
68
+ Check if the current device includes an accelerometer sensor.
69
+
70
+ **Returns:** <code>Promise&lt;<a href="#isavailableresult">IsAvailableResult</a>&gt;</code>
71
+
72
+ **Since:** 1.0.0
73
+
74
+ --------------------
75
+
76
+
77
+ ### startMeasurementUpdates()
78
+
79
+ ```typescript
80
+ startMeasurementUpdates() => Promise<void>
81
+ ```
82
+
83
+ Begin streaming accelerometer updates to the JavaScript layer.
84
+
85
+ Call {@link addListener} with the `measurement` event to receive the updates.
86
+
87
+ **Since:** 1.0.0
88
+
89
+ --------------------
90
+
91
+
92
+ ### stopMeasurementUpdates()
93
+
94
+ ```typescript
95
+ stopMeasurementUpdates() => Promise<void>
96
+ ```
97
+
98
+ Stop streaming accelerometer updates started via {@link startMeasurementUpdates}.
99
+
100
+ **Since:** 1.0.0
101
+
102
+ --------------------
103
+
104
+
105
+ ### checkPermissions()
106
+
107
+ ```typescript
108
+ checkPermissions() => Promise<PermissionStatus>
109
+ ```
110
+
111
+ Return the current permission state for accessing motion data.
112
+
113
+ On platforms without explicit permissions this resolves to `granted`.
114
+
115
+ **Returns:** <code>Promise&lt;<a href="#permissionstatus">PermissionStatus</a>&gt;</code>
116
+
117
+ **Since:** 1.0.0
118
+
119
+ --------------------
120
+
121
+
122
+ ### requestPermissions()
123
+
124
+ ```typescript
125
+ requestPermissions() => Promise<PermissionStatus>
126
+ ```
127
+
128
+ Request permission to access motion data if supported by the platform.
129
+
130
+ **Returns:** <code>Promise&lt;<a href="#permissionstatus">PermissionStatus</a>&gt;</code>
131
+
132
+ **Since:** 1.0.0
133
+
134
+ --------------------
135
+
136
+
137
+ ### addListener('measurement', ...)
138
+
139
+ ```typescript
140
+ addListener(eventName: 'measurement', listenerFunc: (event: MeasurementEvent) => void) => Promise<PluginListenerHandle>
141
+ ```
142
+
143
+ Listen for measurement updates.
144
+
145
+ | Param | Type | Description |
146
+ | ------------------ | ----------------------------------------------------------------------- | ------------------------------------------ |
147
+ | **`eventName`** | <code>'measurement'</code> | Only the `measurement` event is supported. |
148
+ | **`listenerFunc`** | <code>(event: <a href="#measurement">Measurement</a>) =&gt; void</code> | Callback invoked with each measurement. |
149
+
150
+ **Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
151
+
152
+ **Since:** 1.0.0
153
+
154
+ --------------------
155
+
156
+
157
+ ### removeAllListeners()
158
+
159
+ ```typescript
160
+ removeAllListeners() => Promise<void>
161
+ ```
162
+
163
+ Remove all listeners that have been registered on the plugin.
164
+
165
+ **Since:** 1.0.0
166
+
167
+ --------------------
168
+
169
+
170
+ ### Interfaces
171
+
172
+
173
+ #### Measurement
174
+
175
+ The x, y and z axis acceleration values reported by the device motion sensors.
176
+
177
+ | Prop | Type | Description | Since |
178
+ | ------- | ------------------- | -------------------------------------- | ----- |
179
+ | **`x`** | <code>number</code> | The acceleration on the x-axis in G's. | 1.0.0 |
180
+ | **`y`** | <code>number</code> | The acceleration on the y-axis in G's. | 1.0.0 |
181
+ | **`z`** | <code>number</code> | The acceleration on the z-axis in G's. | 1.0.0 |
182
+
183
+
184
+ #### IsAvailableResult
185
+
186
+ Result returned by {@link CapacitorAccelerometerPlugin.isAvailable}.
187
+
188
+ | Prop | Type | Description | Since |
189
+ | ----------------- | -------------------- | ----------------------------------------------------------- | ----- |
190
+ | **`isAvailable`** | <code>boolean</code> | Whether an accelerometer sensor is available on the device. | 1.0.0 |
191
+
192
+
193
+ #### PermissionStatus
194
+
195
+ Permission information returned by {@link CapacitorAccelerometerPlugin.checkPermissions}
196
+ and {@link CapacitorAccelerometerPlugin.requestPermissions}.
197
+
198
+ | Prop | Type | Description | Since |
199
+ | ------------------- | ------------------------------------------------------------------------------------- | ----------------------------------------------------------------------- | ----- |
200
+ | **`accelerometer`** | <code><a href="#accelerometerpermissionstate">AccelerometerPermissionState</a></code> | The permission state for accessing motion data on the current platform. | 1.0.0 |
201
+
202
+
203
+ #### PluginListenerHandle
204
+
205
+ | Prop | Type |
206
+ | ------------ | ----------------------------------------- |
207
+ | **`remove`** | <code>() =&gt; Promise&lt;void&gt;</code> |
208
+
209
+
210
+ ### Type Aliases
211
+
212
+
213
+ #### GetMeasurementResult
214
+
215
+ Alias for the most recent measurement.
216
+
217
+ <code><a href="#measurement">Measurement</a></code>
218
+
219
+
220
+ #### AccelerometerPermissionState
221
+
222
+ Permission state union including `limited` for platforms that can throttle motion access.
223
+
224
+ <code><a href="#permissionstate">PermissionState</a> | 'limited'</code>
225
+
226
+
227
+ #### PermissionState
228
+
229
+ <code>'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'</code>
230
+
231
+
232
+ #### MeasurementEvent
233
+
234
+ Event payload emitted when {@link CapacitorAccelerometerPlugin.startMeasurementUpdates}
235
+ is active.
236
+
237
+ <code><a href="#measurement">Measurement</a></code>
238
+
239
+ </docgen-api>
240
+
241
+ ### Credit
242
+
243
+ This plugin was inspired from: https://github.com/kesha-antonov/react-native-background-downloader
@@ -0,0 +1,57 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
6
+ }
7
+
8
+ buildscript {
9
+ repositories {
10
+ google()
11
+ mavenCentral()
12
+ }
13
+ dependencies {
14
+ classpath 'com.android.tools.build:gradle:8.7.2'
15
+ }
16
+ }
17
+
18
+ apply plugin: 'com.android.library'
19
+
20
+ android {
21
+ namespace "app.capgo.accelerometer"
22
+ compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
23
+ defaultConfig {
24
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
25
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35
26
+ versionCode 1
27
+ versionName "1.0"
28
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
29
+ }
30
+ buildTypes {
31
+ release {
32
+ minifyEnabled false
33
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34
+ }
35
+ }
36
+ lintOptions {
37
+ abortOnError false
38
+ }
39
+ compileOptions {
40
+ sourceCompatibility JavaVersion.VERSION_21
41
+ targetCompatibility JavaVersion.VERSION_21
42
+ }
43
+ }
44
+
45
+ repositories {
46
+ google()
47
+ mavenCentral()
48
+ }
49
+
50
+ dependencies {
51
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
52
+ implementation project(':capacitor-android')
53
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
54
+ testImplementation "junit:junit:$junitVersion"
55
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
56
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
57
+ }
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -0,0 +1,160 @@
1
+ package app.capgo.accelerometer;
2
+
3
+ import android.content.Context;
4
+ import android.hardware.Sensor;
5
+ import android.hardware.SensorEvent;
6
+ import android.hardware.SensorEventListener;
7
+ import android.hardware.SensorManager;
8
+ import androidx.annotation.Nullable;
9
+ import com.getcapacitor.JSObject;
10
+ import com.getcapacitor.Plugin;
11
+ import com.getcapacitor.PluginCall;
12
+ import com.getcapacitor.PluginMethod;
13
+ import com.getcapacitor.annotation.CapacitorPlugin;
14
+
15
+ @CapacitorPlugin(name = "CapacitorAccelerometer")
16
+ public class CapacitorAccelerometerPlugin extends Plugin implements SensorEventListener {
17
+
18
+ private static final String PERMISSION_GRANTED = "granted";
19
+ private static final String PERMISSION_DENIED = "denied";
20
+
21
+ @Nullable
22
+ private SensorManager sensorManager;
23
+
24
+ @Nullable
25
+ private Sensor accelerometer;
26
+
27
+ private final JSObject lastMeasurement = new JSObject();
28
+
29
+ private boolean updatesActive = false;
30
+
31
+ @Override
32
+ public void load() {
33
+ Context context = getContext();
34
+ sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
35
+ if (sensorManager != null) {
36
+ accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
37
+ }
38
+ lastMeasurement.put("x", 0);
39
+ lastMeasurement.put("y", 0);
40
+ lastMeasurement.put("z", 0);
41
+ }
42
+
43
+ @PluginMethod
44
+ public void getMeasurement(PluginCall call) {
45
+ if (accelerometer == null) {
46
+ call.reject("Accelerometer sensor not available on this device.");
47
+ return;
48
+ }
49
+ call.resolve(snapshotMeasurement());
50
+ }
51
+
52
+ @PluginMethod
53
+ public void isAvailable(PluginCall call) {
54
+ JSObject result = new JSObject();
55
+ result.put("isAvailable", accelerometer != null);
56
+ call.resolve(result);
57
+ }
58
+
59
+ @PluginMethod
60
+ public void startMeasurementUpdates(PluginCall call) {
61
+ if (accelerometer == null) {
62
+ call.reject("Accelerometer sensor not available on this device.");
63
+ return;
64
+ }
65
+ if (updatesActive) {
66
+ call.resolve();
67
+ return;
68
+ }
69
+ if (sensorManager != null && sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME)) {
70
+ updatesActive = true;
71
+ call.resolve();
72
+ } else {
73
+ call.reject("Failed to register accelerometer listener.");
74
+ }
75
+ }
76
+
77
+ @PluginMethod
78
+ public void stopMeasurementUpdates(PluginCall call) {
79
+ if (sensorManager != null && updatesActive) {
80
+ sensorManager.unregisterListener(this);
81
+ updatesActive = false;
82
+ }
83
+ call.resolve();
84
+ }
85
+
86
+ @PluginMethod
87
+ public void checkPermissions(PluginCall call) {
88
+ JSObject result = new JSObject();
89
+ result.put("accelerometer", accelerometer != null ? PERMISSION_GRANTED : PERMISSION_DENIED);
90
+ call.resolve(result);
91
+ }
92
+
93
+ @PluginMethod
94
+ public void requestPermissions(PluginCall call) {
95
+ // No explicit runtime permission is required for accelerometer access.
96
+ checkPermissions(call);
97
+ }
98
+
99
+ @PluginMethod
100
+ public void removeAllListeners(PluginCall call) {
101
+ super.removeAllListeners(call);
102
+ }
103
+
104
+ @Override
105
+ public void handleOnPause() {
106
+ if (sensorManager != null && updatesActive) {
107
+ sensorManager.unregisterListener(this);
108
+ }
109
+ super.handleOnPause();
110
+ }
111
+
112
+ @Override
113
+ public void handleOnResume() {
114
+ super.handleOnResume();
115
+ if (updatesActive && sensorManager != null && accelerometer != null) {
116
+ sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
117
+ }
118
+ }
119
+
120
+ @Override
121
+ public void handleOnDestroy() {
122
+ if (sensorManager != null) {
123
+ sensorManager.unregisterListener(this);
124
+ }
125
+ super.handleOnDestroy();
126
+ }
127
+
128
+ @Override
129
+ public void onSensorChanged(SensorEvent event) {
130
+ if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
131
+ return;
132
+ }
133
+ double x = event.values[0] / SensorManager.GRAVITY_EARTH;
134
+ double y = event.values[1] / SensorManager.GRAVITY_EARTH;
135
+ double z = event.values[2] / SensorManager.GRAVITY_EARTH;
136
+
137
+ lastMeasurement.put("x", x);
138
+ lastMeasurement.put("y", y);
139
+ lastMeasurement.put("z", z);
140
+
141
+ JSObject payload = new JSObject();
142
+ payload.put("x", x);
143
+ payload.put("y", y);
144
+ payload.put("z", z);
145
+ notifyListeners("measurement", payload);
146
+ }
147
+
148
+ @Override
149
+ public void onAccuracyChanged(Sensor sensor, int accuracy) {
150
+ // No-op
151
+ }
152
+
153
+ private JSObject snapshotMeasurement() {
154
+ JSObject copy = new JSObject();
155
+ copy.put("x", lastMeasurement.optDouble("x", 0));
156
+ copy.put("y", lastMeasurement.optDouble("y", 0));
157
+ copy.put("z", lastMeasurement.optDouble("z", 0));
158
+ return copy;
159
+ }
160
+ }
File without changes