@capawesome/capacitor-battery 0.0.1

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.
Files changed (46) hide show
  1. package/CapawesomeCapacitorBattery.podspec +17 -0
  2. package/LICENSE +21 -0
  3. package/Package.swift +28 -0
  4. package/README.md +366 -0
  5. package/android/build.gradle +58 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/Battery.java +163 -0
  8. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/BatteryPlugin.java +156 -0
  9. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/CustomException.java +20 -0
  10. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/CustomExceptions.java +9 -0
  11. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/events/BatteryLevelChangeEvent.java +22 -0
  12. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/events/BatteryStateChangeEvent.java +23 -0
  13. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/events/LowPowerModeChangeEvent.java +22 -0
  14. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/results/GetBatteryLevelResult.java +22 -0
  15. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/results/GetBatteryStateResult.java +23 -0
  16. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/results/IsLowPowerModeEnabledResult.java +22 -0
  17. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/interfaces/Callback.java +5 -0
  18. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/interfaces/NonEmptyResultCallback.java +7 -0
  19. package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/interfaces/Result.java +7 -0
  20. package/android/src/main/res/.gitkeep +0 -0
  21. package/dist/docs.json +393 -0
  22. package/dist/esm/definitions.d.ts +166 -0
  23. package/dist/esm/definitions.js +2 -0
  24. package/dist/esm/definitions.js.map +1 -0
  25. package/dist/esm/index.d.ts +4 -0
  26. package/dist/esm/index.js +7 -0
  27. package/dist/esm/index.js.map +1 -0
  28. package/dist/esm/web.d.ts +13 -0
  29. package/dist/esm/web.js +58 -0
  30. package/dist/esm/web.js.map +1 -0
  31. package/dist/plugin.cjs.js +72 -0
  32. package/dist/plugin.cjs.js.map +1 -0
  33. package/dist/plugin.js +75 -0
  34. package/dist/plugin.js.map +1 -0
  35. package/ios/Plugin/Battery.swift +116 -0
  36. package/ios/Plugin/BatteryPlugin.swift +105 -0
  37. package/ios/Plugin/Classes/Events/BatteryLevelChangeEvent.swift +16 -0
  38. package/ios/Plugin/Classes/Events/BatteryStateChangeEvent.swift +16 -0
  39. package/ios/Plugin/Classes/Events/LowPowerModeChangeEvent.swift +16 -0
  40. package/ios/Plugin/Classes/Results/GetBatteryLevelResult.swift +16 -0
  41. package/ios/Plugin/Classes/Results/GetBatteryStateResult.swift +16 -0
  42. package/ios/Plugin/Classes/Results/IsLowPowerModeEnabledResult.swift +16 -0
  43. package/ios/Plugin/Enums/CustomError.swift +14 -0
  44. package/ios/Plugin/Info.plist +24 -0
  45. package/ios/Plugin/Protocols/Result.swift +5 -0
  46. package/package.json +91 -0
package/dist/docs.json ADDED
@@ -0,0 +1,393 @@
1
+ {
2
+ "api": {
3
+ "name": "BatteryPlugin",
4
+ "slug": "batteryplugin",
5
+ "docs": "",
6
+ "tags": [],
7
+ "methods": [
8
+ {
9
+ "name": "getBatteryLevel",
10
+ "signature": "() => Promise<GetBatteryLevelResult>",
11
+ "parameters": [],
12
+ "returns": "Promise<GetBatteryLevelResult>",
13
+ "tags": [
14
+ {
15
+ "name": "since",
16
+ "text": "0.1.0"
17
+ }
18
+ ],
19
+ "docs": "Get the current battery level of the device.\n\nOn the web, this is only supported in browsers that implement the\n[Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)\n(Chromium-based browsers).\n\nOnly available on Android, iOS and Web.",
20
+ "complexTypes": [
21
+ "GetBatteryLevelResult"
22
+ ],
23
+ "slug": "getbatterylevel"
24
+ },
25
+ {
26
+ "name": "getBatteryState",
27
+ "signature": "() => Promise<GetBatteryStateResult>",
28
+ "parameters": [],
29
+ "returns": "Promise<GetBatteryStateResult>",
30
+ "tags": [
31
+ {
32
+ "name": "since",
33
+ "text": "0.1.0"
34
+ }
35
+ ],
36
+ "docs": "Get the current battery state of the device.\n\nOn the web, this is only supported in browsers that implement the\n[Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)\n(Chromium-based browsers).\n\nOnly available on Android, iOS and Web.",
37
+ "complexTypes": [
38
+ "GetBatteryStateResult"
39
+ ],
40
+ "slug": "getbatterystate"
41
+ },
42
+ {
43
+ "name": "isLowPowerModeEnabled",
44
+ "signature": "() => Promise<IsLowPowerModeEnabledResult>",
45
+ "parameters": [],
46
+ "returns": "Promise<IsLowPowerModeEnabledResult>",
47
+ "tags": [
48
+ {
49
+ "name": "since",
50
+ "text": "0.1.0"
51
+ }
52
+ ],
53
+ "docs": "Get whether the low power mode is currently enabled.\n\nOn Android, this refers to the power saver mode. On iOS, this refers to\nthe Low Power Mode.\n\nOnly available on Android and iOS.",
54
+ "complexTypes": [
55
+ "IsLowPowerModeEnabledResult"
56
+ ],
57
+ "slug": "islowpowermodeenabled"
58
+ },
59
+ {
60
+ "name": "addListener",
61
+ "signature": "(eventName: 'batteryLevelChange', listenerFunc: (event: BatteryLevelChangeEvent) => void) => Promise<PluginListenerHandle>",
62
+ "parameters": [
63
+ {
64
+ "name": "eventName",
65
+ "docs": "",
66
+ "type": "'batteryLevelChange'"
67
+ },
68
+ {
69
+ "name": "listenerFunc",
70
+ "docs": "",
71
+ "type": "(event: BatteryLevelChangeEvent) => void"
72
+ }
73
+ ],
74
+ "returns": "Promise<PluginListenerHandle>",
75
+ "tags": [
76
+ {
77
+ "name": "since",
78
+ "text": "0.1.0"
79
+ }
80
+ ],
81
+ "docs": "Listen for changes to the battery level of the device.\n\nThe device is only observed while at least one listener is attached.\n\nOn the web, this is only supported in browsers that implement the\n[Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)\n(Chromium-based browsers).\n\nOnly available on Android, iOS and Web.",
82
+ "complexTypes": [
83
+ "PluginListenerHandle",
84
+ "BatteryLevelChangeEvent"
85
+ ],
86
+ "slug": "addlistenerbatterylevelchange-"
87
+ },
88
+ {
89
+ "name": "addListener",
90
+ "signature": "(eventName: 'batteryStateChange', listenerFunc: (event: BatteryStateChangeEvent) => void) => Promise<PluginListenerHandle>",
91
+ "parameters": [
92
+ {
93
+ "name": "eventName",
94
+ "docs": "",
95
+ "type": "'batteryStateChange'"
96
+ },
97
+ {
98
+ "name": "listenerFunc",
99
+ "docs": "",
100
+ "type": "(event: BatteryStateChangeEvent) => void"
101
+ }
102
+ ],
103
+ "returns": "Promise<PluginListenerHandle>",
104
+ "tags": [
105
+ {
106
+ "name": "since",
107
+ "text": "0.1.0"
108
+ }
109
+ ],
110
+ "docs": "Listen for changes to the battery state of the device.\n\nThe device is only observed while at least one listener is attached.\n\nOn the web, this is only supported in browsers that implement the\n[Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)\n(Chromium-based browsers).\n\nOnly available on Android, iOS and Web.",
111
+ "complexTypes": [
112
+ "PluginListenerHandle",
113
+ "BatteryStateChangeEvent"
114
+ ],
115
+ "slug": "addlistenerbatterystatechange-"
116
+ },
117
+ {
118
+ "name": "addListener",
119
+ "signature": "(eventName: 'lowPowerModeChange', listenerFunc: (event: LowPowerModeChangeEvent) => void) => Promise<PluginListenerHandle>",
120
+ "parameters": [
121
+ {
122
+ "name": "eventName",
123
+ "docs": "",
124
+ "type": "'lowPowerModeChange'"
125
+ },
126
+ {
127
+ "name": "listenerFunc",
128
+ "docs": "",
129
+ "type": "(event: LowPowerModeChangeEvent) => void"
130
+ }
131
+ ],
132
+ "returns": "Promise<PluginListenerHandle>",
133
+ "tags": [
134
+ {
135
+ "name": "since",
136
+ "text": "0.1.0"
137
+ }
138
+ ],
139
+ "docs": "Listen for changes to the low power mode of the device.\n\nThe device is only observed while at least one listener is attached.\n\nOnly available on Android and iOS.",
140
+ "complexTypes": [
141
+ "PluginListenerHandle",
142
+ "LowPowerModeChangeEvent"
143
+ ],
144
+ "slug": "addlistenerlowpowermodechange-"
145
+ },
146
+ {
147
+ "name": "removeAllListeners",
148
+ "signature": "() => Promise<void>",
149
+ "parameters": [],
150
+ "returns": "Promise<void>",
151
+ "tags": [
152
+ {
153
+ "name": "since",
154
+ "text": "0.1.0"
155
+ }
156
+ ],
157
+ "docs": "Remove all listeners for this plugin.",
158
+ "complexTypes": [],
159
+ "slug": "removealllisteners"
160
+ }
161
+ ],
162
+ "properties": []
163
+ },
164
+ "interfaces": [
165
+ {
166
+ "name": "GetBatteryLevelResult",
167
+ "slug": "getbatterylevelresult",
168
+ "docs": "",
169
+ "tags": [
170
+ {
171
+ "text": "0.1.0",
172
+ "name": "since"
173
+ }
174
+ ],
175
+ "methods": [],
176
+ "properties": [
177
+ {
178
+ "name": "level",
179
+ "tags": [
180
+ {
181
+ "text": "0.75",
182
+ "name": "example"
183
+ },
184
+ {
185
+ "text": "0.1.0",
186
+ "name": "since"
187
+ }
188
+ ],
189
+ "docs": "The current battery level of the device as a value between `0.0` and `1.0`.",
190
+ "complexTypes": [],
191
+ "type": "number"
192
+ }
193
+ ]
194
+ },
195
+ {
196
+ "name": "GetBatteryStateResult",
197
+ "slug": "getbatterystateresult",
198
+ "docs": "",
199
+ "tags": [
200
+ {
201
+ "text": "0.1.0",
202
+ "name": "since"
203
+ }
204
+ ],
205
+ "methods": [],
206
+ "properties": [
207
+ {
208
+ "name": "state",
209
+ "tags": [
210
+ {
211
+ "text": "'charging'",
212
+ "name": "example"
213
+ },
214
+ {
215
+ "text": "0.1.0",
216
+ "name": "since"
217
+ }
218
+ ],
219
+ "docs": "The current battery state of the device.",
220
+ "complexTypes": [
221
+ "BatteryState"
222
+ ],
223
+ "type": "BatteryState"
224
+ }
225
+ ]
226
+ },
227
+ {
228
+ "name": "IsLowPowerModeEnabledResult",
229
+ "slug": "islowpowermodeenabledresult",
230
+ "docs": "",
231
+ "tags": [
232
+ {
233
+ "text": "0.1.0",
234
+ "name": "since"
235
+ }
236
+ ],
237
+ "methods": [],
238
+ "properties": [
239
+ {
240
+ "name": "enabled",
241
+ "tags": [
242
+ {
243
+ "text": "true",
244
+ "name": "example"
245
+ },
246
+ {
247
+ "text": "0.1.0",
248
+ "name": "since"
249
+ }
250
+ ],
251
+ "docs": "Whether the low power mode is currently enabled.",
252
+ "complexTypes": [],
253
+ "type": "boolean"
254
+ }
255
+ ]
256
+ },
257
+ {
258
+ "name": "PluginListenerHandle",
259
+ "slug": "pluginlistenerhandle",
260
+ "docs": "",
261
+ "tags": [],
262
+ "methods": [],
263
+ "properties": [
264
+ {
265
+ "name": "remove",
266
+ "tags": [],
267
+ "docs": "",
268
+ "complexTypes": [],
269
+ "type": "() => Promise<void>"
270
+ }
271
+ ]
272
+ },
273
+ {
274
+ "name": "BatteryLevelChangeEvent",
275
+ "slug": "batterylevelchangeevent",
276
+ "docs": "",
277
+ "tags": [
278
+ {
279
+ "text": "0.1.0",
280
+ "name": "since"
281
+ }
282
+ ],
283
+ "methods": [],
284
+ "properties": [
285
+ {
286
+ "name": "level",
287
+ "tags": [
288
+ {
289
+ "text": "0.75",
290
+ "name": "example"
291
+ },
292
+ {
293
+ "text": "0.1.0",
294
+ "name": "since"
295
+ }
296
+ ],
297
+ "docs": "The current battery level of the device as a value between `0.0` and `1.0`.",
298
+ "complexTypes": [],
299
+ "type": "number"
300
+ }
301
+ ]
302
+ },
303
+ {
304
+ "name": "BatteryStateChangeEvent",
305
+ "slug": "batterystatechangeevent",
306
+ "docs": "",
307
+ "tags": [
308
+ {
309
+ "text": "0.1.0",
310
+ "name": "since"
311
+ }
312
+ ],
313
+ "methods": [],
314
+ "properties": [
315
+ {
316
+ "name": "state",
317
+ "tags": [
318
+ {
319
+ "text": "'charging'",
320
+ "name": "example"
321
+ },
322
+ {
323
+ "text": "0.1.0",
324
+ "name": "since"
325
+ }
326
+ ],
327
+ "docs": "The current battery state of the device.",
328
+ "complexTypes": [
329
+ "BatteryState"
330
+ ],
331
+ "type": "BatteryState"
332
+ }
333
+ ]
334
+ },
335
+ {
336
+ "name": "LowPowerModeChangeEvent",
337
+ "slug": "lowpowermodechangeevent",
338
+ "docs": "",
339
+ "tags": [
340
+ {
341
+ "text": "0.1.0",
342
+ "name": "since"
343
+ }
344
+ ],
345
+ "methods": [],
346
+ "properties": [
347
+ {
348
+ "name": "enabled",
349
+ "tags": [
350
+ {
351
+ "text": "true",
352
+ "name": "example"
353
+ },
354
+ {
355
+ "text": "0.1.0",
356
+ "name": "since"
357
+ }
358
+ ],
359
+ "docs": "Whether the low power mode is currently enabled.",
360
+ "complexTypes": [],
361
+ "type": "boolean"
362
+ }
363
+ ]
364
+ }
365
+ ],
366
+ "enums": [],
367
+ "typeAliases": [
368
+ {
369
+ "name": "BatteryState",
370
+ "slug": "batterystate",
371
+ "docs": "The battery state of the device.\n\n- `charging`: The device is plugged into power and the battery is charging.\n- `full`: The device is plugged into power and the battery is fully charged.\n- `unplugged`: The device is not plugged into power and the battery is\n discharging.\n- `unknown`: The battery state could not be determined.",
372
+ "types": [
373
+ {
374
+ "text": "'charging'",
375
+ "complexTypes": []
376
+ },
377
+ {
378
+ "text": "'full'",
379
+ "complexTypes": []
380
+ },
381
+ {
382
+ "text": "'unplugged'",
383
+ "complexTypes": []
384
+ },
385
+ {
386
+ "text": "'unknown'",
387
+ "complexTypes": []
388
+ }
389
+ ]
390
+ }
391
+ ],
392
+ "pluginConfigs": []
393
+ }
@@ -0,0 +1,166 @@
1
+ import type { PluginListenerHandle } from '@capacitor/core';
2
+ export interface BatteryPlugin {
3
+ /**
4
+ * Get the current battery level of the device.
5
+ *
6
+ * On the web, this is only supported in browsers that implement the
7
+ * [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)
8
+ * (Chromium-based browsers).
9
+ *
10
+ * Only available on Android, iOS and Web.
11
+ *
12
+ * @since 0.1.0
13
+ */
14
+ getBatteryLevel(): Promise<GetBatteryLevelResult>;
15
+ /**
16
+ * Get the current battery state of the device.
17
+ *
18
+ * On the web, this is only supported in browsers that implement the
19
+ * [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)
20
+ * (Chromium-based browsers).
21
+ *
22
+ * Only available on Android, iOS and Web.
23
+ *
24
+ * @since 0.1.0
25
+ */
26
+ getBatteryState(): Promise<GetBatteryStateResult>;
27
+ /**
28
+ * Get whether the low power mode is currently enabled.
29
+ *
30
+ * On Android, this refers to the power saver mode. On iOS, this refers to
31
+ * the Low Power Mode.
32
+ *
33
+ * Only available on Android and iOS.
34
+ *
35
+ * @since 0.1.0
36
+ */
37
+ isLowPowerModeEnabled(): Promise<IsLowPowerModeEnabledResult>;
38
+ /**
39
+ * Listen for changes to the battery level of the device.
40
+ *
41
+ * The device is only observed while at least one listener is attached.
42
+ *
43
+ * On the web, this is only supported in browsers that implement the
44
+ * [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)
45
+ * (Chromium-based browsers).
46
+ *
47
+ * Only available on Android, iOS and Web.
48
+ *
49
+ * @since 0.1.0
50
+ */
51
+ addListener(eventName: 'batteryLevelChange', listenerFunc: (event: BatteryLevelChangeEvent) => void): Promise<PluginListenerHandle>;
52
+ /**
53
+ * Listen for changes to the battery state of the device.
54
+ *
55
+ * The device is only observed while at least one listener is attached.
56
+ *
57
+ * On the web, this is only supported in browsers that implement the
58
+ * [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)
59
+ * (Chromium-based browsers).
60
+ *
61
+ * Only available on Android, iOS and Web.
62
+ *
63
+ * @since 0.1.0
64
+ */
65
+ addListener(eventName: 'batteryStateChange', listenerFunc: (event: BatteryStateChangeEvent) => void): Promise<PluginListenerHandle>;
66
+ /**
67
+ * Listen for changes to the low power mode of the device.
68
+ *
69
+ * The device is only observed while at least one listener is attached.
70
+ *
71
+ * Only available on Android and iOS.
72
+ *
73
+ * @since 0.1.0
74
+ */
75
+ addListener(eventName: 'lowPowerModeChange', listenerFunc: (event: LowPowerModeChangeEvent) => void): Promise<PluginListenerHandle>;
76
+ /**
77
+ * Remove all listeners for this plugin.
78
+ *
79
+ * @since 0.1.0
80
+ */
81
+ removeAllListeners(): Promise<void>;
82
+ }
83
+ /**
84
+ * @since 0.1.0
85
+ */
86
+ export interface GetBatteryLevelResult {
87
+ /**
88
+ * The current battery level of the device as a value between `0.0` and `1.0`.
89
+ *
90
+ * @example 0.75
91
+ * @since 0.1.0
92
+ */
93
+ level: number;
94
+ }
95
+ /**
96
+ * @since 0.1.0
97
+ */
98
+ export interface GetBatteryStateResult {
99
+ /**
100
+ * The current battery state of the device.
101
+ *
102
+ * @example 'charging'
103
+ * @since 0.1.0
104
+ */
105
+ state: BatteryState;
106
+ }
107
+ /**
108
+ * @since 0.1.0
109
+ */
110
+ export interface IsLowPowerModeEnabledResult {
111
+ /**
112
+ * Whether the low power mode is currently enabled.
113
+ *
114
+ * @example true
115
+ * @since 0.1.0
116
+ */
117
+ enabled: boolean;
118
+ }
119
+ /**
120
+ * @since 0.1.0
121
+ */
122
+ export interface BatteryLevelChangeEvent {
123
+ /**
124
+ * The current battery level of the device as a value between `0.0` and `1.0`.
125
+ *
126
+ * @example 0.75
127
+ * @since 0.1.0
128
+ */
129
+ level: number;
130
+ }
131
+ /**
132
+ * @since 0.1.0
133
+ */
134
+ export interface BatteryStateChangeEvent {
135
+ /**
136
+ * The current battery state of the device.
137
+ *
138
+ * @example 'charging'
139
+ * @since 0.1.0
140
+ */
141
+ state: BatteryState;
142
+ }
143
+ /**
144
+ * @since 0.1.0
145
+ */
146
+ export interface LowPowerModeChangeEvent {
147
+ /**
148
+ * Whether the low power mode is currently enabled.
149
+ *
150
+ * @example true
151
+ * @since 0.1.0
152
+ */
153
+ enabled: boolean;
154
+ }
155
+ /**
156
+ * The battery state of the device.
157
+ *
158
+ * - `charging`: The device is plugged into power and the battery is charging.
159
+ * - `full`: The device is plugged into power and the battery is fully charged.
160
+ * - `unplugged`: The device is not plugged into power and the battery is
161
+ * discharging.
162
+ * - `unknown`: The battery state could not be determined.
163
+ *
164
+ * @since 0.1.0
165
+ */
166
+ export type BatteryState = 'charging' | 'full' | 'unplugged' | 'unknown';
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=definitions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PluginListenerHandle } from '@capacitor/core';\n\nexport interface BatteryPlugin {\n /**\n * Get the current battery level of the device.\n *\n * On the web, this is only supported in browsers that implement the\n * [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)\n * (Chromium-based browsers).\n *\n * Only available on Android, iOS and Web.\n *\n * @since 0.1.0\n */\n getBatteryLevel(): Promise<GetBatteryLevelResult>;\n /**\n * Get the current battery state of the device.\n *\n * On the web, this is only supported in browsers that implement the\n * [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)\n * (Chromium-based browsers).\n *\n * Only available on Android, iOS and Web.\n *\n * @since 0.1.0\n */\n getBatteryState(): Promise<GetBatteryStateResult>;\n /**\n * Get whether the low power mode is currently enabled.\n *\n * On Android, this refers to the power saver mode. On iOS, this refers to\n * the Low Power Mode.\n *\n * Only available on Android and iOS.\n *\n * @since 0.1.0\n */\n isLowPowerModeEnabled(): Promise<IsLowPowerModeEnabledResult>;\n /**\n * Listen for changes to the battery level of the device.\n *\n * The device is only observed while at least one listener is attached.\n *\n * On the web, this is only supported in browsers that implement the\n * [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)\n * (Chromium-based browsers).\n *\n * Only available on Android, iOS and Web.\n *\n * @since 0.1.0\n */\n addListener(\n eventName: 'batteryLevelChange',\n listenerFunc: (event: BatteryLevelChangeEvent) => void,\n ): Promise<PluginListenerHandle>;\n /**\n * Listen for changes to the battery state of the device.\n *\n * The device is only observed while at least one listener is attached.\n *\n * On the web, this is only supported in browsers that implement the\n * [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)\n * (Chromium-based browsers).\n *\n * Only available on Android, iOS and Web.\n *\n * @since 0.1.0\n */\n addListener(\n eventName: 'batteryStateChange',\n listenerFunc: (event: BatteryStateChangeEvent) => void,\n ): Promise<PluginListenerHandle>;\n /**\n * Listen for changes to the low power mode of the device.\n *\n * The device is only observed while at least one listener is attached.\n *\n * Only available on Android and iOS.\n *\n * @since 0.1.0\n */\n addListener(\n eventName: 'lowPowerModeChange',\n listenerFunc: (event: LowPowerModeChangeEvent) => void,\n ): Promise<PluginListenerHandle>;\n /**\n * Remove all listeners for this plugin.\n *\n * @since 0.1.0\n */\n removeAllListeners(): Promise<void>;\n}\n\n/**\n * @since 0.1.0\n */\nexport interface GetBatteryLevelResult {\n /**\n * The current battery level of the device as a value between `0.0` and `1.0`.\n *\n * @example 0.75\n * @since 0.1.0\n */\n level: number;\n}\n\n/**\n * @since 0.1.0\n */\nexport interface GetBatteryStateResult {\n /**\n * The current battery state of the device.\n *\n * @example 'charging'\n * @since 0.1.0\n */\n state: BatteryState;\n}\n\n/**\n * @since 0.1.0\n */\nexport interface IsLowPowerModeEnabledResult {\n /**\n * Whether the low power mode is currently enabled.\n *\n * @example true\n * @since 0.1.0\n */\n enabled: boolean;\n}\n\n/**\n * @since 0.1.0\n */\nexport interface BatteryLevelChangeEvent {\n /**\n * The current battery level of the device as a value between `0.0` and `1.0`.\n *\n * @example 0.75\n * @since 0.1.0\n */\n level: number;\n}\n\n/**\n * @since 0.1.0\n */\nexport interface BatteryStateChangeEvent {\n /**\n * The current battery state of the device.\n *\n * @example 'charging'\n * @since 0.1.0\n */\n state: BatteryState;\n}\n\n/**\n * @since 0.1.0\n */\nexport interface LowPowerModeChangeEvent {\n /**\n * Whether the low power mode is currently enabled.\n *\n * @example true\n * @since 0.1.0\n */\n enabled: boolean;\n}\n\n/**\n * The battery state of the device.\n *\n * - `charging`: The device is plugged into power and the battery is charging.\n * - `full`: The device is plugged into power and the battery is fully charged.\n * - `unplugged`: The device is not plugged into power and the battery is\n * discharging.\n * - `unknown`: The battery state could not be determined.\n *\n * @since 0.1.0\n */\nexport type BatteryState = 'charging' | 'full' | 'unplugged' | 'unknown';\n"]}
@@ -0,0 +1,4 @@
1
+ import type { BatteryPlugin } from './definitions';
2
+ declare const Battery: BatteryPlugin;
3
+ export * from './definitions';
4
+ export { Battery };
@@ -0,0 +1,7 @@
1
+ import { registerPlugin } from '@capacitor/core';
2
+ const Battery = registerPlugin('Battery', {
3
+ web: () => import('./web').then(m => new m.BatteryWeb()),
4
+ });
5
+ export * from './definitions';
6
+ export { Battery };
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,OAAO,GAAG,cAAc,CAAgB,SAAS,EAAE;IACvD,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,CAAC;CACzD,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { BatteryPlugin } from './definitions';\n\nconst Battery = registerPlugin<BatteryPlugin>('Battery', {\n web: () => import('./web').then(m => new m.BatteryWeb()),\n});\n\nexport * from './definitions';\nexport { Battery };\n"]}
@@ -0,0 +1,13 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import type { BatteryPlugin, GetBatteryLevelResult, GetBatteryStateResult, IsLowPowerModeEnabledResult } from './definitions';
3
+ export declare class BatteryWeb extends WebPlugin implements BatteryPlugin {
4
+ private static readonly errorNotSupported;
5
+ private batteryManagerPromise;
6
+ constructor();
7
+ getBatteryLevel(): Promise<GetBatteryLevelResult>;
8
+ getBatteryState(): Promise<GetBatteryStateResult>;
9
+ isLowPowerModeEnabled(): Promise<IsLowPowerModeEnabledResult>;
10
+ private getBatteryManager;
11
+ private initializeBatteryManager;
12
+ private mapBatteryState;
13
+ }
@@ -0,0 +1,58 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ export class BatteryWeb extends WebPlugin {
3
+ constructor() {
4
+ super();
5
+ void this.initializeBatteryManager();
6
+ }
7
+ async getBatteryLevel() {
8
+ const batteryManager = await this.getBatteryManager();
9
+ return { level: batteryManager.level };
10
+ }
11
+ async getBatteryState() {
12
+ const batteryManager = await this.getBatteryManager();
13
+ return { state: this.mapBatteryState(batteryManager) };
14
+ }
15
+ async isLowPowerModeEnabled() {
16
+ throw this.unimplemented('Not implemented on web.');
17
+ }
18
+ async getBatteryManager() {
19
+ const getBattery = navigator.getBattery;
20
+ if (!getBattery) {
21
+ throw this.unimplemented(BatteryWeb.errorNotSupported);
22
+ }
23
+ if (!this.batteryManagerPromise) {
24
+ this.batteryManagerPromise = getBattery.call(navigator);
25
+ }
26
+ return this.batteryManagerPromise;
27
+ }
28
+ async initializeBatteryManager() {
29
+ let batteryManager;
30
+ try {
31
+ batteryManager = await this.getBatteryManager();
32
+ }
33
+ catch (_a) {
34
+ return;
35
+ }
36
+ batteryManager.addEventListener('levelchange', () => {
37
+ this.notifyListeners('batteryLevelChange', {
38
+ level: batteryManager.level,
39
+ });
40
+ });
41
+ batteryManager.addEventListener('chargingchange', () => {
42
+ this.notifyListeners('batteryStateChange', {
43
+ state: this.mapBatteryState(batteryManager),
44
+ });
45
+ });
46
+ }
47
+ mapBatteryState(batteryManager) {
48
+ if (batteryManager.level >= 1) {
49
+ return 'full';
50
+ }
51
+ if (batteryManager.charging) {
52
+ return 'charging';
53
+ }
54
+ return 'unplugged';
55
+ }
56
+ }
57
+ BatteryWeb.errorNotSupported = 'The Battery Status API is not supported in this browser.';
58
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAmB5C,MAAM,OAAO,UAAW,SAAQ,SAAS;IAMvC;QACE,KAAK,EAAE,CAAC;QACR,KAAK,IAAI,CAAC,wBAAwB,EAAE,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,eAAe;QACnB,MAAM,cAAc,GAAG,MAAM,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACtD,OAAO,EAAE,KAAK,EAAE,cAAc,CAAC,KAAK,EAAE,CAAC;IACzC,CAAC;IAED,KAAK,CAAC,eAAe;QACnB,MAAM,cAAc,GAAG,MAAM,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACtD,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,eAAe,CAAC,cAAc,CAAC,EAAE,CAAC;IACzD,CAAC;IAED,KAAK,CAAC,qBAAqB;QACzB,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC,CAAC;IACtD,CAAC;IAEO,KAAK,CAAC,iBAAiB;QAC7B,MAAM,UAAU,GAAI,SAAkC,CAAC,UAAU,CAAC;QAClE,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,iBAAiB,CAAC,CAAC;QACzD,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAChC,IAAI,CAAC,qBAAqB,GAAG,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC1D,CAAC;QACD,OAAO,IAAI,CAAC,qBAAqB,CAAC;IACpC,CAAC;IAEO,KAAK,CAAC,wBAAwB;QACpC,IAAI,cAA8B,CAAC;QACnC,IAAI,CAAC;YACH,cAAc,GAAG,MAAM,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAClD,CAAC;QAAC,WAAM,CAAC;YACP,OAAO;QACT,CAAC;QACD,cAAc,CAAC,gBAAgB,CAAC,aAAa,EAAE,GAAG,EAAE;YAClD,IAAI,CAAC,eAAe,CAAC,oBAAoB,EAAE;gBACzC,KAAK,EAAE,cAAc,CAAC,KAAK;aAC5B,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,cAAc,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,GAAG,EAAE;YACrD,IAAI,CAAC,eAAe,CAAC,oBAAoB,EAAE;gBACzC,KAAK,EAAE,IAAI,CAAC,eAAe,CAAC,cAAc,CAAC;aAC5C,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,eAAe,CAAC,cAA8B;QACpD,IAAI,cAAc,CAAC,KAAK,IAAI,CAAC,EAAE,CAAC;YAC9B,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,IAAI,cAAc,CAAC,QAAQ,EAAE,CAAC;YAC5B,OAAO,UAAU,CAAC;QACpB,CAAC;QACD,OAAO,WAAW,CAAC;IACrB,CAAC;;AA9DuB,4BAAiB,GACvC,0DAA0D,CAAC","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type {\n BatteryPlugin,\n BatteryState,\n GetBatteryLevelResult,\n GetBatteryStateResult,\n IsLowPowerModeEnabledResult,\n} from './definitions';\n\ninterface BatteryManager extends EventTarget {\n readonly charging: boolean;\n readonly level: number;\n}\n\ninterface NavigatorWithBattery extends Navigator {\n getBattery?: () => Promise<BatteryManager>;\n}\n\nexport class BatteryWeb extends WebPlugin implements BatteryPlugin {\n private static readonly errorNotSupported =\n 'The Battery Status API is not supported in this browser.';\n\n private batteryManagerPromise: Promise<BatteryManager> | undefined;\n\n constructor() {\n super();\n void this.initializeBatteryManager();\n }\n\n async getBatteryLevel(): Promise<GetBatteryLevelResult> {\n const batteryManager = await this.getBatteryManager();\n return { level: batteryManager.level };\n }\n\n async getBatteryState(): Promise<GetBatteryStateResult> {\n const batteryManager = await this.getBatteryManager();\n return { state: this.mapBatteryState(batteryManager) };\n }\n\n async isLowPowerModeEnabled(): Promise<IsLowPowerModeEnabledResult> {\n throw this.unimplemented('Not implemented on web.');\n }\n\n private async getBatteryManager(): Promise<BatteryManager> {\n const getBattery = (navigator as NavigatorWithBattery).getBattery;\n if (!getBattery) {\n throw this.unimplemented(BatteryWeb.errorNotSupported);\n }\n if (!this.batteryManagerPromise) {\n this.batteryManagerPromise = getBattery.call(navigator);\n }\n return this.batteryManagerPromise;\n }\n\n private async initializeBatteryManager(): Promise<void> {\n let batteryManager: BatteryManager;\n try {\n batteryManager = await this.getBatteryManager();\n } catch {\n return;\n }\n batteryManager.addEventListener('levelchange', () => {\n this.notifyListeners('batteryLevelChange', {\n level: batteryManager.level,\n });\n });\n batteryManager.addEventListener('chargingchange', () => {\n this.notifyListeners('batteryStateChange', {\n state: this.mapBatteryState(batteryManager),\n });\n });\n }\n\n private mapBatteryState(batteryManager: BatteryManager): BatteryState {\n if (batteryManager.level >= 1) {\n return 'full';\n }\n if (batteryManager.charging) {\n return 'charging';\n }\n return 'unplugged';\n }\n}\n"]}