@capgo/capacitor-light-sensor 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/CapgoCapacitorLightSensor.podspec +17 -0
- package/LICENSE +373 -0
- package/Package.swift +28 -0
- package/README.md +317 -0
- package/android/build.gradle +60 -0
- package/android/gradle.properties +2 -0
- package/android/proguard-rules.pro +21 -0
- package/android/settings.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/app/capgo/capacitor/lightsensor/CapgoLightSensorPlugin.kt +133 -0
- package/dist/docs.json +423 -0
- package/dist/esm/definitions.d.ts +191 -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 +13 -0
- package/dist/esm/web.js +29 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +43 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +46 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/CapgoLightSensorPlugin/CapgoLightSensorPlugin.swift +46 -0
- package/ios/Tests/CapgoLightSensorPluginTests/CapgoLightSensorPluginTests.swift +20 -0
- package/package.json +92 -0
package/README.md
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# @capgo/capacitor-light-sensor
|
|
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_light_sensor"> ➡️ Get Instant updates for your App with Capgo</a></h2>
|
|
6
|
+
<h2><a href="https://capgo.app/consulting/?ref=plugin_light_sensor"> Missing a feature? We'll build the plugin for you 💪</a></h2>
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
Capacitor plugin for accessing the device's ambient light sensor.
|
|
10
|
+
|
|
11
|
+
## Why Capacitor Light Sensor?
|
|
12
|
+
|
|
13
|
+
- **Real Ambient Light Data**: Get accurate light level readings in lux
|
|
14
|
+
- **Efficient Sensor Access**: Uses native Android sensor APIs for optimal performance
|
|
15
|
+
- **Configurable Update Intervals**: Control how often you receive sensor updates
|
|
16
|
+
- **Battery Conscious**: Start and stop the sensor as needed to conserve battery
|
|
17
|
+
- **TypeScript Support**: Full type definitions for a great developer experience
|
|
18
|
+
|
|
19
|
+
## Platform Support
|
|
20
|
+
|
|
21
|
+
| Platform | Support |
|
|
22
|
+
|----------|---------|
|
|
23
|
+
| Android | ✅ Full support via TYPE_LIGHT sensor |
|
|
24
|
+
| iOS | ❌ Not available (no public API) |
|
|
25
|
+
| Web | ❌ Not available |
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install @capgo/capacitor-light-sensor
|
|
31
|
+
npx cap sync
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Requirements
|
|
35
|
+
|
|
36
|
+
### Android
|
|
37
|
+
- Minimum SDK: 24 (Android 7.0)
|
|
38
|
+
- Target SDK: 36
|
|
39
|
+
- Device must have a light sensor (most Android phones do)
|
|
40
|
+
|
|
41
|
+
### High Sampling Rate (Android 12+)
|
|
42
|
+
For update intervals below 200ms on Android 12 and above, add this permission to your `AndroidManifest.xml`:
|
|
43
|
+
|
|
44
|
+
```xml
|
|
45
|
+
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { LightSensor } from '@capgo/capacitor-light-sensor';
|
|
52
|
+
|
|
53
|
+
// Check if sensor is available
|
|
54
|
+
const { available } = await LightSensor.isAvailable();
|
|
55
|
+
|
|
56
|
+
if (available) {
|
|
57
|
+
// Start listening with 500ms update interval
|
|
58
|
+
await LightSensor.start({ updateInterval: 500 });
|
|
59
|
+
|
|
60
|
+
// Add listener for sensor data
|
|
61
|
+
const handle = await LightSensor.addListener('lightSensorChange', (data) => {
|
|
62
|
+
console.log(`Light level: ${data.illuminance} lux`);
|
|
63
|
+
console.log(`Timestamp: ${data.timestamp}`);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Later, stop the sensor
|
|
67
|
+
await LightSensor.stop();
|
|
68
|
+
await handle.remove();
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Light Level Reference
|
|
73
|
+
|
|
74
|
+
| Lux Value | Condition |
|
|
75
|
+
|-----------|-----------|
|
|
76
|
+
| 0.0001 | Moonless, overcast night |
|
|
77
|
+
| 0.27-1 | Full moon on a clear night |
|
|
78
|
+
| 3.4 | Dark limit of civil twilight |
|
|
79
|
+
| 50 | Family living room |
|
|
80
|
+
| 80 | Office hallway |
|
|
81
|
+
| 100 | Very dark overcast day |
|
|
82
|
+
| 400 | Sunrise/sunset on clear day |
|
|
83
|
+
| 1,000 | Overcast day |
|
|
84
|
+
| 10,000-25,000 | Full daylight (indirect) |
|
|
85
|
+
| 32,000-100,000 | Direct sunlight |
|
|
86
|
+
|
|
87
|
+
## API
|
|
88
|
+
|
|
89
|
+
<docgen-index>
|
|
90
|
+
|
|
91
|
+
* [`isAvailable()`](#isavailable)
|
|
92
|
+
* [`start(...)`](#start)
|
|
93
|
+
* [`stop()`](#stop)
|
|
94
|
+
* [`addListener('lightSensorChange', ...)`](#addlistenerlightsensorchange-)
|
|
95
|
+
* [`removeAllListeners()`](#removealllisteners)
|
|
96
|
+
* [`checkPermissions()`](#checkpermissions)
|
|
97
|
+
* [`requestPermissions()`](#requestpermissions)
|
|
98
|
+
* [`getPluginVersion()`](#getpluginversion)
|
|
99
|
+
* [Interfaces](#interfaces)
|
|
100
|
+
* [Type Aliases](#type-aliases)
|
|
101
|
+
|
|
102
|
+
</docgen-index>
|
|
103
|
+
|
|
104
|
+
<docgen-api>
|
|
105
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
106
|
+
|
|
107
|
+
Capacitor plugin for accessing the device's ambient light sensor.
|
|
108
|
+
|
|
109
|
+
### isAvailable()
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
isAvailable() => Promise<IsAvailableResult>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Check if the light sensor is available on the current device.
|
|
116
|
+
You should always check sensor availability before attempting to use it.
|
|
117
|
+
|
|
118
|
+
**Returns:** <code>Promise<<a href="#isavailableresult">IsAvailableResult</a>></code>
|
|
119
|
+
|
|
120
|
+
**Since:** 0.0.1
|
|
121
|
+
|
|
122
|
+
--------------------
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
### start(...)
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
start(options?: StartOptions | undefined) => Promise<void>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Start listening to light sensor updates.
|
|
132
|
+
This will begin sensor measurements at the specified interval.
|
|
133
|
+
Use `addListener` to receive the sensor data.
|
|
134
|
+
|
|
135
|
+
| Param | Type | Description |
|
|
136
|
+
| ------------- | ----------------------------------------------------- | -------------------------------------- |
|
|
137
|
+
| **`options`** | <code><a href="#startoptions">StartOptions</a></code> | - Configuration options for the sensor |
|
|
138
|
+
|
|
139
|
+
**Since:** 0.0.1
|
|
140
|
+
|
|
141
|
+
--------------------
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
### stop()
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
stop() => Promise<void>
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Stop listening to light sensor updates.
|
|
151
|
+
This will stop the sensor and conserve battery.
|
|
152
|
+
|
|
153
|
+
**Since:** 0.0.1
|
|
154
|
+
|
|
155
|
+
--------------------
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
### addListener('lightSensorChange', ...)
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
addListener(eventName: 'lightSensorChange', listenerFunc: LightSensorCallback) => Promise<PluginListenerHandle>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Add a listener for light sensor change events.
|
|
165
|
+
The listener will be called whenever new sensor data is available.
|
|
166
|
+
|
|
167
|
+
| Param | Type | Description |
|
|
168
|
+
| ------------------ | ------------------------------------------------------------------- | ------------------------------------------ |
|
|
169
|
+
| **`eventName`** | <code>'lightSensorChange'</code> | - Must be 'lightSensorChange' |
|
|
170
|
+
| **`listenerFunc`** | <code><a href="#lightsensorcallback">LightSensorCallback</a></code> | - Callback function to receive sensor data |
|
|
171
|
+
|
|
172
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
173
|
+
|
|
174
|
+
**Since:** 0.0.1
|
|
175
|
+
|
|
176
|
+
--------------------
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
### removeAllListeners()
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
removeAllListeners() => Promise<void>
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Remove all listeners for light sensor events.
|
|
186
|
+
|
|
187
|
+
**Since:** 0.0.1
|
|
188
|
+
|
|
189
|
+
--------------------
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
### checkPermissions()
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
checkPermissions() => Promise<PermissionStatus>
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Check the current permission status for high sampling rate sensors.
|
|
199
|
+
On Android 12+, the HIGH_SAMPLING_RATE_SENSORS permission is required
|
|
200
|
+
for sensor update intervals below 200ms.
|
|
201
|
+
|
|
202
|
+
**Returns:** <code>Promise<<a href="#permissionstatus">PermissionStatus</a>></code>
|
|
203
|
+
|
|
204
|
+
**Since:** 0.0.1
|
|
205
|
+
|
|
206
|
+
--------------------
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
### requestPermissions()
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
requestPermissions() => Promise<PermissionStatus>
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Request permission for high sampling rate sensors.
|
|
216
|
+
On Android 12+, this requests the HIGH_SAMPLING_RATE_SENSORS permission.
|
|
217
|
+
|
|
218
|
+
**Returns:** <code>Promise<<a href="#permissionstatus">PermissionStatus</a>></code>
|
|
219
|
+
|
|
220
|
+
**Since:** 0.0.1
|
|
221
|
+
|
|
222
|
+
--------------------
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
### getPluginVersion()
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
getPluginVersion() => Promise<VersionResult>
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Get the current version of the plugin.
|
|
232
|
+
|
|
233
|
+
**Returns:** <code>Promise<<a href="#versionresult">VersionResult</a>></code>
|
|
234
|
+
|
|
235
|
+
**Since:** 0.0.1
|
|
236
|
+
|
|
237
|
+
--------------------
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
### Interfaces
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
#### IsAvailableResult
|
|
244
|
+
|
|
245
|
+
Result indicating whether the sensor is available.
|
|
246
|
+
|
|
247
|
+
| Prop | Type | Description | Since |
|
|
248
|
+
| --------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------- | ----- |
|
|
249
|
+
| **`available`** | <code>boolean</code> | Whether the light sensor is available on this device. Always false on iOS as the light sensor API is not available. | 0.0.1 |
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
#### StartOptions
|
|
253
|
+
|
|
254
|
+
Options for starting the light sensor listener.
|
|
255
|
+
|
|
256
|
+
| Prop | Type | Description | Default | Since |
|
|
257
|
+
| -------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------- | ----- |
|
|
258
|
+
| **`updateInterval`** | <code>number</code> | The desired interval between sensor updates in milliseconds. On Android 12+, there's a minimum interval of 200ms unless the app has the HIGH_SAMPLING_RATE_SENSORS permission. | <code>200</code> | 0.0.1 |
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
#### PluginListenerHandle
|
|
262
|
+
|
|
263
|
+
| Prop | Type |
|
|
264
|
+
| ------------ | ----------------------------------------- |
|
|
265
|
+
| **`remove`** | <code>() => Promise<void></code> |
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
#### LightSensorMeasurement
|
|
269
|
+
|
|
270
|
+
A single light sensor measurement.
|
|
271
|
+
|
|
272
|
+
| Prop | Type | Description | Since |
|
|
273
|
+
| ----------------- | ------------------- | ---------------------------------------------------- | ----- |
|
|
274
|
+
| **`illuminance`** | <code>number</code> | Ambient light level in lux (lx). | 0.0.1 |
|
|
275
|
+
| **`timestamp`** | <code>number</code> | Timestamp of the measurement in seconds since epoch. | 0.0.1 |
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
#### PermissionStatus
|
|
279
|
+
|
|
280
|
+
Result of a permission request or check.
|
|
281
|
+
|
|
282
|
+
| Prop | Type | Description | Since |
|
|
283
|
+
| ---------------------- | ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
|
|
284
|
+
| **`highSamplingRate`** | <code>'prompt' \| 'prompt-with-rationale' \| 'granted' \| 'denied'</code> | Whether the high sampling rate sensor permission is granted. On Android 12+, this permission is required for update intervals below 200ms. | 0.0.1 |
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
#### VersionResult
|
|
288
|
+
|
|
289
|
+
Plugin version information.
|
|
290
|
+
|
|
291
|
+
| Prop | Type | Description | Since |
|
|
292
|
+
| ------------- | ------------------- | ---------------------------------- | ----- |
|
|
293
|
+
| **`version`** | <code>string</code> | The current version of the plugin. | 0.0.1 |
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
### Type Aliases
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
#### LightSensorCallback
|
|
300
|
+
|
|
301
|
+
Callback function for light sensor updates.
|
|
302
|
+
|
|
303
|
+
<code>(measurement: <a href="#lightsensormeasurement">LightSensorMeasurement</a>): void</code>
|
|
304
|
+
|
|
305
|
+
</docgen-api>
|
|
306
|
+
|
|
307
|
+
## Contributing
|
|
308
|
+
|
|
309
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for details on how to contribute to this plugin.
|
|
310
|
+
|
|
311
|
+
## License
|
|
312
|
+
|
|
313
|
+
[MPL-2.0](./LICENSE)
|
|
314
|
+
|
|
315
|
+
## Credits
|
|
316
|
+
|
|
317
|
+
This SDK has been inspired by [Expo light sensor](https://docs.expo.dev/versions/latest/sdk/light-sensor/)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
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
|
+
ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '2.1.21'
|
|
10
|
+
repositories {
|
|
11
|
+
google()
|
|
12
|
+
mavenCentral()
|
|
13
|
+
}
|
|
14
|
+
dependencies {
|
|
15
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
16
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
apply plugin: 'com.android.library'
|
|
21
|
+
apply plugin: 'kotlin-android'
|
|
22
|
+
|
|
23
|
+
android {
|
|
24
|
+
namespace "app.capgo.capacitor.lightsensor"
|
|
25
|
+
compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
26
|
+
defaultConfig {
|
|
27
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
28
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
29
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
30
|
+
}
|
|
31
|
+
buildTypes {
|
|
32
|
+
release {
|
|
33
|
+
minifyEnabled false
|
|
34
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
lintOptions {
|
|
38
|
+
abortOnError false
|
|
39
|
+
}
|
|
40
|
+
compileOptions {
|
|
41
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
42
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
43
|
+
}
|
|
44
|
+
kotlinOptions {
|
|
45
|
+
jvmTarget = '21'
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
repositories {
|
|
50
|
+
google()
|
|
51
|
+
mavenCentral()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
dependencies {
|
|
55
|
+
implementation project(':capacitor-android')
|
|
56
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
57
|
+
testImplementation "junit:junit:$junitVersion"
|
|
58
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
59
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
60
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Add project specific ProGuard rules here.
|
|
2
|
+
# You can control the set of applied configuration files using the
|
|
3
|
+
# proguardFiles setting in build.gradle.
|
|
4
|
+
#
|
|
5
|
+
# For more details, see
|
|
6
|
+
# http://developer.android.com/guide/developing/tools/proguard.html
|
|
7
|
+
|
|
8
|
+
# If your project uses WebView with JS, uncomment the following
|
|
9
|
+
# and specify the fully qualified class name to the JavaScript interface
|
|
10
|
+
# class:
|
|
11
|
+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
|
12
|
+
# public *;
|
|
13
|
+
#}
|
|
14
|
+
|
|
15
|
+
# Uncomment this to preserve the line number information for
|
|
16
|
+
# debugging stack traces.
|
|
17
|
+
#-keepattributes SourceFile,LineNumberTable
|
|
18
|
+
|
|
19
|
+
# If you keep the line number information, uncomment this to
|
|
20
|
+
# hide the original source file name.
|
|
21
|
+
#-renamesourcefileattribute SourceFile
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
package app.capgo.capacitor.lightsensor
|
|
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 android.os.Build
|
|
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 = "CapgoLightSensor")
|
|
16
|
+
class CapgoLightSensorPlugin : Plugin(), SensorEventListener {
|
|
17
|
+
|
|
18
|
+
companion object {
|
|
19
|
+
private const val PLUGIN_VERSION = "8.0.0"
|
|
20
|
+
private const val EVENT_LIGHT_SENSOR_CHANGE = "lightSensorChange"
|
|
21
|
+
private const val DEFAULT_UPDATE_INTERVAL = 200000 // 200ms in microseconds
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private var sensorManager: SensorManager? = null
|
|
25
|
+
private var lightSensor: Sensor? = null
|
|
26
|
+
private var isListening = false
|
|
27
|
+
private var updateInterval = DEFAULT_UPDATE_INTERVAL
|
|
28
|
+
|
|
29
|
+
override fun load() {
|
|
30
|
+
sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
|
31
|
+
lightSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_LIGHT)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@PluginMethod
|
|
35
|
+
fun isAvailable(call: PluginCall) {
|
|
36
|
+
val ret = JSObject()
|
|
37
|
+
ret.put("available", lightSensor != null)
|
|
38
|
+
call.resolve(ret)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@PluginMethod
|
|
42
|
+
fun start(call: PluginCall) {
|
|
43
|
+
if (lightSensor == null) {
|
|
44
|
+
call.reject("Light sensor is not available on this device")
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (isListening) {
|
|
49
|
+
call.resolve()
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
val intervalMs = call.getInt("updateInterval", 200) ?: 200
|
|
54
|
+
updateInterval = intervalMs * 1000 // Convert to microseconds
|
|
55
|
+
|
|
56
|
+
val registered = sensorManager?.registerListener(
|
|
57
|
+
this,
|
|
58
|
+
lightSensor,
|
|
59
|
+
updateInterval
|
|
60
|
+
) ?: false
|
|
61
|
+
|
|
62
|
+
if (registered) {
|
|
63
|
+
isListening = true
|
|
64
|
+
call.resolve()
|
|
65
|
+
} else {
|
|
66
|
+
call.reject("Failed to register light sensor listener")
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@PluginMethod
|
|
71
|
+
fun stop(call: PluginCall) {
|
|
72
|
+
if (isListening) {
|
|
73
|
+
sensorManager?.unregisterListener(this)
|
|
74
|
+
isListening = false
|
|
75
|
+
}
|
|
76
|
+
call.resolve()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@PluginMethod
|
|
80
|
+
override fun checkPermissions(call: PluginCall) {
|
|
81
|
+
val ret = JSObject()
|
|
82
|
+
// HIGH_SAMPLING_RATE_SENSORS is a normal permission, granted at install time
|
|
83
|
+
// if declared in manifest. For now, we assume it's granted.
|
|
84
|
+
ret.put("highSamplingRate", "granted")
|
|
85
|
+
call.resolve(ret)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@PluginMethod
|
|
89
|
+
override fun requestPermissions(call: PluginCall) {
|
|
90
|
+
// HIGH_SAMPLING_RATE_SENSORS is a normal permission, automatically granted
|
|
91
|
+
// if declared in the manifest. No runtime request needed.
|
|
92
|
+
val ret = JSObject()
|
|
93
|
+
ret.put("highSamplingRate", "granted")
|
|
94
|
+
call.resolve(ret)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@PluginMethod
|
|
98
|
+
fun getPluginVersion(call: PluginCall) {
|
|
99
|
+
val ret = JSObject()
|
|
100
|
+
ret.put("version", PLUGIN_VERSION)
|
|
101
|
+
call.resolve(ret)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
override fun onSensorChanged(event: SensorEvent?) {
|
|
105
|
+
if (event?.sensor?.type == Sensor.TYPE_LIGHT) {
|
|
106
|
+
val illuminance = event.values[0]
|
|
107
|
+
val timestamp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
|
108
|
+
// Convert nanoseconds to seconds
|
|
109
|
+
event.timestamp / 1_000_000_000.0
|
|
110
|
+
} else {
|
|
111
|
+
System.currentTimeMillis() / 1000.0
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
val data = JSObject()
|
|
115
|
+
data.put("illuminance", illuminance.toDouble())
|
|
116
|
+
data.put("timestamp", timestamp)
|
|
117
|
+
|
|
118
|
+
notifyListeners(EVENT_LIGHT_SENSOR_CHANGE, data)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
|
|
123
|
+
// Not used for light sensor
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
override fun handleOnDestroy() {
|
|
127
|
+
if (isListening) {
|
|
128
|
+
sensorManager?.unregisterListener(this)
|
|
129
|
+
isListening = false
|
|
130
|
+
}
|
|
131
|
+
super.handleOnDestroy()
|
|
132
|
+
}
|
|
133
|
+
}
|