@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.
- package/CapawesomeCapacitorBattery.podspec +17 -0
- package/LICENSE +21 -0
- package/Package.swift +28 -0
- package/README.md +366 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/Battery.java +163 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/BatteryPlugin.java +156 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/CustomException.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/CustomExceptions.java +9 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/events/BatteryLevelChangeEvent.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/events/BatteryStateChangeEvent.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/events/LowPowerModeChangeEvent.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/results/GetBatteryLevelResult.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/results/GetBatteryStateResult.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/classes/results/IsLowPowerModeEnabledResult.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/battery/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +393 -0
- package/dist/esm/definitions.d.ts +166 -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 +58 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +72 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +75 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Battery.swift +116 -0
- package/ios/Plugin/BatteryPlugin.swift +105 -0
- package/ios/Plugin/Classes/Events/BatteryLevelChangeEvent.swift +16 -0
- package/ios/Plugin/Classes/Events/BatteryStateChangeEvent.swift +16 -0
- package/ios/Plugin/Classes/Events/LowPowerModeChangeEvent.swift +16 -0
- package/ios/Plugin/Classes/Results/GetBatteryLevelResult.swift +16 -0
- package/ios/Plugin/Classes/Results/GetBatteryStateResult.swift +16 -0
- package/ios/Plugin/Classes/Results/IsLowPowerModeEnabledResult.swift +16 -0
- package/ios/Plugin/Enums/CustomError.swift +14 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Protocols/Result.swift +5 -0
- package/package.json +91 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@capacitor/core');
|
|
4
|
+
|
|
5
|
+
const Battery = core.registerPlugin('Battery', {
|
|
6
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.BatteryWeb()),
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
class BatteryWeb extends core.WebPlugin {
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
void this.initializeBatteryManager();
|
|
13
|
+
}
|
|
14
|
+
async getBatteryLevel() {
|
|
15
|
+
const batteryManager = await this.getBatteryManager();
|
|
16
|
+
return { level: batteryManager.level };
|
|
17
|
+
}
|
|
18
|
+
async getBatteryState() {
|
|
19
|
+
const batteryManager = await this.getBatteryManager();
|
|
20
|
+
return { state: this.mapBatteryState(batteryManager) };
|
|
21
|
+
}
|
|
22
|
+
async isLowPowerModeEnabled() {
|
|
23
|
+
throw this.unimplemented('Not implemented on web.');
|
|
24
|
+
}
|
|
25
|
+
async getBatteryManager() {
|
|
26
|
+
const getBattery = navigator.getBattery;
|
|
27
|
+
if (!getBattery) {
|
|
28
|
+
throw this.unimplemented(BatteryWeb.errorNotSupported);
|
|
29
|
+
}
|
|
30
|
+
if (!this.batteryManagerPromise) {
|
|
31
|
+
this.batteryManagerPromise = getBattery.call(navigator);
|
|
32
|
+
}
|
|
33
|
+
return this.batteryManagerPromise;
|
|
34
|
+
}
|
|
35
|
+
async initializeBatteryManager() {
|
|
36
|
+
let batteryManager;
|
|
37
|
+
try {
|
|
38
|
+
batteryManager = await this.getBatteryManager();
|
|
39
|
+
}
|
|
40
|
+
catch (_a) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
batteryManager.addEventListener('levelchange', () => {
|
|
44
|
+
this.notifyListeners('batteryLevelChange', {
|
|
45
|
+
level: batteryManager.level,
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
batteryManager.addEventListener('chargingchange', () => {
|
|
49
|
+
this.notifyListeners('batteryStateChange', {
|
|
50
|
+
state: this.mapBatteryState(batteryManager),
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
mapBatteryState(batteryManager) {
|
|
55
|
+
if (batteryManager.level >= 1) {
|
|
56
|
+
return 'full';
|
|
57
|
+
}
|
|
58
|
+
if (batteryManager.charging) {
|
|
59
|
+
return 'charging';
|
|
60
|
+
}
|
|
61
|
+
return 'unplugged';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
BatteryWeb.errorNotSupported = 'The Battery Status API is not supported in this browser.';
|
|
65
|
+
|
|
66
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
67
|
+
__proto__: null,
|
|
68
|
+
BatteryWeb: BatteryWeb
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
exports.Battery = Battery;
|
|
72
|
+
//# sourceMappingURL=plugin.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Battery = registerPlugin('Battery', {\n web: () => import('./web').then(m => new m.BatteryWeb()),\n});\nexport * from './definitions';\nexport { Battery };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class BatteryWeb extends WebPlugin {\n constructor() {\n super();\n void this.initializeBatteryManager();\n }\n async getBatteryLevel() {\n const batteryManager = await this.getBatteryManager();\n return { level: batteryManager.level };\n }\n async getBatteryState() {\n const batteryManager = await this.getBatteryManager();\n return { state: this.mapBatteryState(batteryManager) };\n }\n async isLowPowerModeEnabled() {\n throw this.unimplemented('Not implemented on web.');\n }\n async getBatteryManager() {\n const getBattery = navigator.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 async initializeBatteryManager() {\n let batteryManager;\n try {\n batteryManager = await this.getBatteryManager();\n }\n catch (_a) {\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 mapBatteryState(batteryManager) {\n if (batteryManager.level >= 1) {\n return 'full';\n }\n if (batteryManager.charging) {\n return 'charging';\n }\n return 'unplugged';\n }\n}\nBatteryWeb.errorNotSupported = 'The Battery Status API is not supported in this browser.';\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,OAAO,GAAGA,mBAAc,CAAC,SAAS,EAAE;AAC1C,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,UAAU,EAAE,CAAC;AAC5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CAAC,gBAAgB,CAAC,aAAa,EAAE,MAAM;AAC7D,YAAY,IAAI,CAAC,eAAe,CAAC,oBAAoB,EAAE;AACvD,gBAAgB,KAAK,EAAE,cAAc,CAAC,KAAK;AAC3C,aAAa,CAAC;AACd,QAAQ,CAAC,CAAC;AACV,QAAQ,cAAc,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,MAAM;AAChE,YAAY,IAAI,CAAC,eAAe,CAAC,oBAAoB,EAAE;AACvD,gBAAgB,KAAK,EAAE,IAAI,CAAC,eAAe,CAAC,cAAc,CAAC;AAC3D,aAAa,CAAC;AACd,QAAQ,CAAC,CAAC;AACV,IAAI;AACJ,IAAI,eAAe,CAAC,cAAc,EAAE;AACpC,QAAQ,IAAI,cAAc,CAAC,KAAK,IAAI,CAAC,EAAE;AACvC,YAAY,OAAO,MAAM;AACzB,QAAQ;AACR,QAAQ,IAAI,cAAc,CAAC,QAAQ,EAAE;AACrC,YAAY,OAAO,UAAU;AAC7B,QAAQ;AACR,QAAQ,OAAO,WAAW;AAC1B,IAAI;AACJ;AACA,UAAU,CAAC,iBAAiB,GAAG,0DAA0D;;;;;;;;;"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
var capacitorBattery = (function (exports, core) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const Battery = core.registerPlugin('Battery', {
|
|
5
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.BatteryWeb()),
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
class BatteryWeb extends core.WebPlugin {
|
|
9
|
+
constructor() {
|
|
10
|
+
super();
|
|
11
|
+
void this.initializeBatteryManager();
|
|
12
|
+
}
|
|
13
|
+
async getBatteryLevel() {
|
|
14
|
+
const batteryManager = await this.getBatteryManager();
|
|
15
|
+
return { level: batteryManager.level };
|
|
16
|
+
}
|
|
17
|
+
async getBatteryState() {
|
|
18
|
+
const batteryManager = await this.getBatteryManager();
|
|
19
|
+
return { state: this.mapBatteryState(batteryManager) };
|
|
20
|
+
}
|
|
21
|
+
async isLowPowerModeEnabled() {
|
|
22
|
+
throw this.unimplemented('Not implemented on web.');
|
|
23
|
+
}
|
|
24
|
+
async getBatteryManager() {
|
|
25
|
+
const getBattery = navigator.getBattery;
|
|
26
|
+
if (!getBattery) {
|
|
27
|
+
throw this.unimplemented(BatteryWeb.errorNotSupported);
|
|
28
|
+
}
|
|
29
|
+
if (!this.batteryManagerPromise) {
|
|
30
|
+
this.batteryManagerPromise = getBattery.call(navigator);
|
|
31
|
+
}
|
|
32
|
+
return this.batteryManagerPromise;
|
|
33
|
+
}
|
|
34
|
+
async initializeBatteryManager() {
|
|
35
|
+
let batteryManager;
|
|
36
|
+
try {
|
|
37
|
+
batteryManager = await this.getBatteryManager();
|
|
38
|
+
}
|
|
39
|
+
catch (_a) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
batteryManager.addEventListener('levelchange', () => {
|
|
43
|
+
this.notifyListeners('batteryLevelChange', {
|
|
44
|
+
level: batteryManager.level,
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
batteryManager.addEventListener('chargingchange', () => {
|
|
48
|
+
this.notifyListeners('batteryStateChange', {
|
|
49
|
+
state: this.mapBatteryState(batteryManager),
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
mapBatteryState(batteryManager) {
|
|
54
|
+
if (batteryManager.level >= 1) {
|
|
55
|
+
return 'full';
|
|
56
|
+
}
|
|
57
|
+
if (batteryManager.charging) {
|
|
58
|
+
return 'charging';
|
|
59
|
+
}
|
|
60
|
+
return 'unplugged';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
BatteryWeb.errorNotSupported = 'The Battery Status API is not supported in this browser.';
|
|
64
|
+
|
|
65
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
66
|
+
__proto__: null,
|
|
67
|
+
BatteryWeb: BatteryWeb
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
exports.Battery = Battery;
|
|
71
|
+
|
|
72
|
+
return exports;
|
|
73
|
+
|
|
74
|
+
})({}, capacitorExports);
|
|
75
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Battery = registerPlugin('Battery', {\n web: () => import('./web').then(m => new m.BatteryWeb()),\n});\nexport * from './definitions';\nexport { Battery };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class BatteryWeb extends WebPlugin {\n constructor() {\n super();\n void this.initializeBatteryManager();\n }\n async getBatteryLevel() {\n const batteryManager = await this.getBatteryManager();\n return { level: batteryManager.level };\n }\n async getBatteryState() {\n const batteryManager = await this.getBatteryManager();\n return { state: this.mapBatteryState(batteryManager) };\n }\n async isLowPowerModeEnabled() {\n throw this.unimplemented('Not implemented on web.');\n }\n async getBatteryManager() {\n const getBattery = navigator.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 async initializeBatteryManager() {\n let batteryManager;\n try {\n batteryManager = await this.getBatteryManager();\n }\n catch (_a) {\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 mapBatteryState(batteryManager) {\n if (batteryManager.level >= 1) {\n return 'full';\n }\n if (batteryManager.charging) {\n return 'charging';\n }\n return 'unplugged';\n }\n}\nBatteryWeb.errorNotSupported = 'The Battery Status API is not supported in this browser.';\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,OAAO,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Z,YAAY,cAAc,GAAG,MAAM,IAAI,CAAC,iBAAiB,EAAE;IAC3D,QAAQ;IACR,QAAQ,OAAO,EAAE,EAAE;IACnB,YAAY;IACZ,QAAQ;IACR,QAAQ,cAAc,CAAC,gBAAgB,CAAC,aAAa,EAAE,MAAM;IAC7D,YAAY,IAAI,CAAC,eAAe,CAAC,oBAAoB,EAAE;IACvD,gBAAgB,KAAK,EAAE,cAAc,CAAC,KAAK;IAC3C,aAAa,CAAC;IACd,QAAQ,CAAC,CAAC;IACV,QAAQ,cAAc,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,MAAM;IAChE,YAAY,IAAI,CAAC,eAAe,CAAC,oBAAoB,EAAE;IACvD,gBAAgB,KAAK,EAAE,IAAI,CAAC,eAAe,CAAC,cAAc,CAAC;IAC3D,aAAa,CAAC;IACd,QAAQ,CAAC,CAAC;IACV,IAAI;IACJ,IAAI,eAAe,CAAC,cAAc,EAAE;IACpC,QAAQ,IAAI,cAAc,CAAC,KAAK,IAAI,CAAC,EAAE;IACvC,YAAY,OAAO,MAAM;IACzB,QAAQ;IACR,QAAQ,IAAI,cAAc,CAAC,QAAQ,EAAE;IACrC,YAAY,OAAO,UAAU;IAC7B,QAAQ;IACR,QAAQ,OAAO,WAAW;IAC1B,IAAI;IACJ;IACA,UAAU,CAAC,iBAAiB,GAAG,0DAA0D;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
@objc public class Battery: NSObject {
|
|
5
|
+
private let plugin: BatteryPlugin
|
|
6
|
+
|
|
7
|
+
private var isObserving = false
|
|
8
|
+
|
|
9
|
+
init(plugin: BatteryPlugin) {
|
|
10
|
+
self.plugin = plugin
|
|
11
|
+
super.init()
|
|
12
|
+
DispatchQueue.main.async {
|
|
13
|
+
UIDevice.current.isBatteryMonitoringEnabled = true
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
deinit {
|
|
18
|
+
NotificationCenter.default.removeObserver(self)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@objc public func getBatteryLevel(completion: @escaping (GetBatteryLevelResult?, Error?) -> Void) {
|
|
22
|
+
DispatchQueue.main.async {
|
|
23
|
+
UIDevice.current.isBatteryMonitoringEnabled = true
|
|
24
|
+
let level = UIDevice.current.batteryLevel
|
|
25
|
+
if level < 0 {
|
|
26
|
+
completion(nil, CustomError.batteryLevelUnavailable)
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
completion(GetBatteryLevelResult(level: level), nil)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@objc public func getBatteryState(completion: @escaping (GetBatteryStateResult?, Error?) -> Void) {
|
|
34
|
+
DispatchQueue.main.async {
|
|
35
|
+
UIDevice.current.isBatteryMonitoringEnabled = true
|
|
36
|
+
let state = self.mapBatteryState(UIDevice.current.batteryState)
|
|
37
|
+
completion(GetBatteryStateResult(state: state), nil)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@objc public func isLowPowerModeEnabled(completion: @escaping (IsLowPowerModeEnabledResult?, Error?) -> Void) {
|
|
42
|
+
let enabled = ProcessInfo.processInfo.isLowPowerModeEnabled
|
|
43
|
+
completion(IsLowPowerModeEnabledResult(enabled: enabled), nil)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
func startObserving() {
|
|
47
|
+
DispatchQueue.main.async { [weak self] in
|
|
48
|
+
guard let self = self, !self.isObserving else {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
self.isObserving = true
|
|
52
|
+
UIDevice.current.isBatteryMonitoringEnabled = true
|
|
53
|
+
NotificationCenter.default.addObserver(
|
|
54
|
+
self,
|
|
55
|
+
selector: #selector(self.handleBatteryLevelDidChange),
|
|
56
|
+
name: UIDevice.batteryLevelDidChangeNotification,
|
|
57
|
+
object: nil
|
|
58
|
+
)
|
|
59
|
+
NotificationCenter.default.addObserver(
|
|
60
|
+
self,
|
|
61
|
+
selector: #selector(self.handleBatteryStateDidChange),
|
|
62
|
+
name: UIDevice.batteryStateDidChangeNotification,
|
|
63
|
+
object: nil
|
|
64
|
+
)
|
|
65
|
+
NotificationCenter.default.addObserver(
|
|
66
|
+
self,
|
|
67
|
+
selector: #selector(self.handlePowerStateDidChange),
|
|
68
|
+
name: Notification.Name.NSProcessInfoPowerStateDidChange,
|
|
69
|
+
object: nil
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
func stopObserving() {
|
|
75
|
+
DispatchQueue.main.async { [weak self] in
|
|
76
|
+
guard let self = self, self.isObserving else {
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
self.isObserving = false
|
|
80
|
+
NotificationCenter.default.removeObserver(self, name: UIDevice.batteryLevelDidChangeNotification, object: nil)
|
|
81
|
+
NotificationCenter.default.removeObserver(self, name: UIDevice.batteryStateDidChangeNotification, object: nil)
|
|
82
|
+
NotificationCenter.default.removeObserver(self, name: Notification.Name.NSProcessInfoPowerStateDidChange, object: nil)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@objc private func handleBatteryLevelDidChange() {
|
|
87
|
+
let level = UIDevice.current.batteryLevel
|
|
88
|
+
guard level >= 0 else {
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
plugin.notifyBatteryLevelChangeListeners(BatteryLevelChangeEvent(level: level))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@objc private func handleBatteryStateDidChange() {
|
|
95
|
+
let state = mapBatteryState(UIDevice.current.batteryState)
|
|
96
|
+
plugin.notifyBatteryStateChangeListeners(BatteryStateChangeEvent(state: state))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@objc private func handlePowerStateDidChange() {
|
|
100
|
+
let enabled = ProcessInfo.processInfo.isLowPowerModeEnabled
|
|
101
|
+
plugin.notifyLowPowerModeChangeListeners(LowPowerModeChangeEvent(enabled: enabled))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private func mapBatteryState(_ state: UIDevice.BatteryState) -> String {
|
|
105
|
+
switch state {
|
|
106
|
+
case .charging:
|
|
107
|
+
return "charging"
|
|
108
|
+
case .full:
|
|
109
|
+
return "full"
|
|
110
|
+
case .unplugged:
|
|
111
|
+
return "unplugged"
|
|
112
|
+
default:
|
|
113
|
+
return "unknown"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc(BatteryPlugin)
|
|
5
|
+
public class BatteryPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
6
|
+
public let identifier = "BatteryPlugin"
|
|
7
|
+
public let jsName = "Battery"
|
|
8
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
9
|
+
CAPPluginMethod(name: "getBatteryLevel", returnType: CAPPluginReturnPromise),
|
|
10
|
+
CAPPluginMethod(name: "getBatteryState", returnType: CAPPluginReturnPromise),
|
|
11
|
+
CAPPluginMethod(name: "isLowPowerModeEnabled", returnType: CAPPluginReturnPromise)
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
public static let eventBatteryLevelChange = "batteryLevelChange"
|
|
15
|
+
|
|
16
|
+
public static let eventBatteryStateChange = "batteryStateChange"
|
|
17
|
+
|
|
18
|
+
public static let eventLowPowerModeChange = "lowPowerModeChange"
|
|
19
|
+
|
|
20
|
+
public static let tag = "BatteryPlugin"
|
|
21
|
+
|
|
22
|
+
private var implementation: Battery?
|
|
23
|
+
|
|
24
|
+
override public func load() {
|
|
25
|
+
self.implementation = Battery(plugin: self)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@objc override public func addListener(_ call: CAPPluginCall) {
|
|
29
|
+
super.addListener(call)
|
|
30
|
+
implementation?.startObserving()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@objc func getBatteryLevel(_ call: CAPPluginCall) {
|
|
34
|
+
implementation?.getBatteryLevel { result, error in
|
|
35
|
+
if let error = error {
|
|
36
|
+
self.rejectCall(call, error)
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
self.resolveCall(call, result)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@objc func getBatteryState(_ call: CAPPluginCall) {
|
|
44
|
+
implementation?.getBatteryState { result, error in
|
|
45
|
+
if let error = error {
|
|
46
|
+
self.rejectCall(call, error)
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
self.resolveCall(call, result)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@objc func isLowPowerModeEnabled(_ call: CAPPluginCall) {
|
|
54
|
+
implementation?.isLowPowerModeEnabled { result, error in
|
|
55
|
+
if let error = error {
|
|
56
|
+
self.rejectCall(call, error)
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
self.resolveCall(call, result)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public func notifyBatteryLevelChangeListeners(_ event: BatteryLevelChangeEvent) {
|
|
64
|
+
self.notifyListeners(Self.eventBatteryLevelChange, data: event.toJSObject() as? [String: Any])
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public func notifyBatteryStateChangeListeners(_ event: BatteryStateChangeEvent) {
|
|
68
|
+
self.notifyListeners(Self.eventBatteryStateChange, data: event.toJSObject() as? [String: Any])
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public func notifyLowPowerModeChangeListeners(_ event: LowPowerModeChangeEvent) {
|
|
72
|
+
self.notifyListeners(Self.eventLowPowerModeChange, data: event.toJSObject() as? [String: Any])
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@objc override public func removeAllListeners(_ call: CAPPluginCall) {
|
|
76
|
+
super.removeAllListeners(call)
|
|
77
|
+
implementation?.stopObserving()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@objc override public func removeListener(_ call: CAPPluginCall) {
|
|
81
|
+
super.removeListener(call)
|
|
82
|
+
if !hasAnyListeners() {
|
|
83
|
+
implementation?.stopObserving()
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private func hasAnyListeners() -> Bool {
|
|
88
|
+
return hasListeners(Self.eventBatteryLevelChange)
|
|
89
|
+
|| hasListeners(Self.eventBatteryStateChange)
|
|
90
|
+
|| hasListeners(Self.eventLowPowerModeChange)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private func rejectCall(_ call: CAPPluginCall, _ error: Error) {
|
|
94
|
+
CAPLog.print("[", BatteryPlugin.tag, "] ", error)
|
|
95
|
+
call.reject(error.localizedDescription)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private func resolveCall(_ call: CAPPluginCall, _ result: Result?) {
|
|
99
|
+
if let result = result?.toJSObject() as? JSObject {
|
|
100
|
+
call.resolve(result)
|
|
101
|
+
} else {
|
|
102
|
+
call.resolve()
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class BatteryLevelChangeEvent: NSObject, Result {
|
|
5
|
+
let level: Float
|
|
6
|
+
|
|
7
|
+
init(level: Float) {
|
|
8
|
+
self.level = level
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["level"] = level
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class BatteryStateChangeEvent: NSObject, Result {
|
|
5
|
+
let state: String
|
|
6
|
+
|
|
7
|
+
init(state: String) {
|
|
8
|
+
self.state = state
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["state"] = state
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class LowPowerModeChangeEvent: NSObject, Result {
|
|
5
|
+
let enabled: Bool
|
|
6
|
+
|
|
7
|
+
init(enabled: Bool) {
|
|
8
|
+
self.enabled = enabled
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["enabled"] = enabled
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class GetBatteryLevelResult: NSObject, Result {
|
|
5
|
+
let level: Float
|
|
6
|
+
|
|
7
|
+
init(level: Float) {
|
|
8
|
+
self.level = level
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["level"] = level
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class GetBatteryStateResult: NSObject, Result {
|
|
5
|
+
let state: String
|
|
6
|
+
|
|
7
|
+
init(state: String) {
|
|
8
|
+
self.state = state
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["state"] = state
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class IsLowPowerModeEnabledResult: NSObject, Result {
|
|
5
|
+
let enabled: Bool
|
|
6
|
+
|
|
7
|
+
init(enabled: Bool) {
|
|
8
|
+
self.enabled = enabled
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["enabled"] = enabled
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
public enum CustomError: Error {
|
|
4
|
+
case batteryLevelUnavailable
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
extension CustomError: LocalizedError {
|
|
8
|
+
public var errorDescription: String? {
|
|
9
|
+
switch self {
|
|
10
|
+
case .batteryLevelUnavailable:
|
|
11
|
+
return NSLocalizedString("The battery level is currently unavailable.", comment: "batteryLevelUnavailable")
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>CFBundleDevelopmentRegion</key>
|
|
6
|
+
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
|
7
|
+
<key>CFBundleExecutable</key>
|
|
8
|
+
<string>$(EXECUTABLE_NAME)</string>
|
|
9
|
+
<key>CFBundleIdentifier</key>
|
|
10
|
+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
|
11
|
+
<key>CFBundleInfoDictionaryVersion</key>
|
|
12
|
+
<string>6.0</string>
|
|
13
|
+
<key>CFBundleName</key>
|
|
14
|
+
<string>$(PRODUCT_NAME)</string>
|
|
15
|
+
<key>CFBundlePackageType</key>
|
|
16
|
+
<string>FMWK</string>
|
|
17
|
+
<key>CFBundleShortVersionString</key>
|
|
18
|
+
<string>1.0</string>
|
|
19
|
+
<key>CFBundleVersion</key>
|
|
20
|
+
<string>$(CURRENT_PROJECT_VERSION)</string>
|
|
21
|
+
<key>NSPrincipalClass</key>
|
|
22
|
+
<string></string>
|
|
23
|
+
</dict>
|
|
24
|
+
</plist>
|
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capawesome/capacitor-battery",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Capacitor plugin to access battery information.",
|
|
5
|
+
"main": "dist/plugin.cjs.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"types": "dist/esm/index.d.ts",
|
|
8
|
+
"unpkg": "dist/plugin.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"android/src/main/",
|
|
11
|
+
"android/build.gradle",
|
|
12
|
+
"dist/",
|
|
13
|
+
"ios/Plugin/",
|
|
14
|
+
"CapawesomeCapacitorBattery.podspec",
|
|
15
|
+
"Package.swift"
|
|
16
|
+
],
|
|
17
|
+
"author": "Robin Genz <mail@robingenz.dev>",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/capawesome-team/capacitor-plugins.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/capawesome-team/capacitor-plugins/issues"
|
|
25
|
+
},
|
|
26
|
+
"funding": [
|
|
27
|
+
{
|
|
28
|
+
"type": "github",
|
|
29
|
+
"url": "https://github.com/sponsors/capawesome-team/"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"type": "opencollective",
|
|
33
|
+
"url": "https://opencollective.com/capawesome"
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
"homepage": "https://capawesome.io/docs/plugins/battery/",
|
|
37
|
+
"keywords": [
|
|
38
|
+
"capacitor",
|
|
39
|
+
"plugin",
|
|
40
|
+
"native"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
|
|
44
|
+
"verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",
|
|
45
|
+
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
|
46
|
+
"verify:web": "npm run build",
|
|
47
|
+
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
|
|
48
|
+
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
|
|
49
|
+
"eslint": "eslint . --ext ts",
|
|
50
|
+
"prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
|
|
51
|
+
"swiftlint": "node-swiftlint",
|
|
52
|
+
"docgen": "docgen --api BatteryPlugin --output-readme README.md --output-json dist/docs.json",
|
|
53
|
+
"build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
|
|
54
|
+
"clean": "rimraf ./dist",
|
|
55
|
+
"watch": "tsc --watch",
|
|
56
|
+
"ios:pod:install": "cd ios && pod install --repo-update && cd ..",
|
|
57
|
+
"ios:spm:install": "cd ios && swift package resolve && cd ..",
|
|
58
|
+
"prepublishOnly": "npm run build"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@capacitor/android": "8.0.0",
|
|
62
|
+
"@capacitor/cli": "8.0.0",
|
|
63
|
+
"@capacitor/core": "8.0.0",
|
|
64
|
+
"@capacitor/docgen": "0.3.1",
|
|
65
|
+
"@capacitor/ios": "8.0.0",
|
|
66
|
+
"@ionic/eslint-config": "0.4.0",
|
|
67
|
+
"eslint": "8.57.0",
|
|
68
|
+
"prettier-plugin-java": "2.6.7",
|
|
69
|
+
"rimraf": "6.1.2",
|
|
70
|
+
"rollup": "4.53.3",
|
|
71
|
+
"swiftlint": "2.0.0",
|
|
72
|
+
"typescript": "5.9.3"
|
|
73
|
+
},
|
|
74
|
+
"peerDependencies": {
|
|
75
|
+
"@capacitor/core": ">=8.0.0"
|
|
76
|
+
},
|
|
77
|
+
"eslintConfig": {
|
|
78
|
+
"extends": "@ionic/eslint-config/recommended"
|
|
79
|
+
},
|
|
80
|
+
"capacitor": {
|
|
81
|
+
"ios": {
|
|
82
|
+
"src": "ios"
|
|
83
|
+
},
|
|
84
|
+
"android": {
|
|
85
|
+
"src": "android"
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"publishConfig": {
|
|
89
|
+
"access": "public"
|
|
90
|
+
}
|
|
91
|
+
}
|