@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
@@ -0,0 +1,156 @@
1
+ package io.capawesome.capacitorjs.plugins.battery;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.Logger;
5
+ import com.getcapacitor.Plugin;
6
+ import com.getcapacitor.PluginCall;
7
+ import com.getcapacitor.PluginMethod;
8
+ import com.getcapacitor.annotation.CapacitorPlugin;
9
+ import io.capawesome.capacitorjs.plugins.battery.classes.events.BatteryLevelChangeEvent;
10
+ import io.capawesome.capacitorjs.plugins.battery.classes.events.BatteryStateChangeEvent;
11
+ import io.capawesome.capacitorjs.plugins.battery.classes.events.LowPowerModeChangeEvent;
12
+ import io.capawesome.capacitorjs.plugins.battery.classes.results.GetBatteryLevelResult;
13
+ import io.capawesome.capacitorjs.plugins.battery.classes.results.GetBatteryStateResult;
14
+ import io.capawesome.capacitorjs.plugins.battery.classes.results.IsLowPowerModeEnabledResult;
15
+ import io.capawesome.capacitorjs.plugins.battery.interfaces.NonEmptyResultCallback;
16
+ import io.capawesome.capacitorjs.plugins.battery.interfaces.Result;
17
+
18
+ @CapacitorPlugin(name = "Battery")
19
+ public class BatteryPlugin extends Plugin {
20
+
21
+ public static final String EVENT_BATTERY_LEVEL_CHANGE = "batteryLevelChange";
22
+
23
+ public static final String EVENT_BATTERY_STATE_CHANGE = "batteryStateChange";
24
+
25
+ public static final String EVENT_LOW_POWER_MODE_CHANGE = "lowPowerModeChange";
26
+
27
+ public static final String TAG = "BatteryPlugin";
28
+
29
+ private static final String ERROR_UNKNOWN_ERROR = "An unknown error occurred.";
30
+
31
+ private Battery implementation;
32
+
33
+ @Override
34
+ public void load() {
35
+ implementation = new Battery(this);
36
+ }
37
+
38
+ @Override
39
+ @PluginMethod(returnType = PluginMethod.RETURN_NONE)
40
+ public void addListener(PluginCall call) {
41
+ super.addListener(call);
42
+ implementation.startObserving();
43
+ }
44
+
45
+ @PluginMethod
46
+ public void getBatteryLevel(PluginCall call) {
47
+ try {
48
+ NonEmptyResultCallback<GetBatteryLevelResult> callback = new NonEmptyResultCallback<>() {
49
+ @Override
50
+ public void success(@NonNull GetBatteryLevelResult result) {
51
+ resolveCall(call, result);
52
+ }
53
+
54
+ @Override
55
+ public void error(@NonNull Exception exception) {
56
+ rejectCall(call, exception);
57
+ }
58
+ };
59
+
60
+ implementation.getBatteryLevel(callback);
61
+ } catch (Exception exception) {
62
+ rejectCall(call, exception);
63
+ }
64
+ }
65
+
66
+ @PluginMethod
67
+ public void getBatteryState(PluginCall call) {
68
+ try {
69
+ NonEmptyResultCallback<GetBatteryStateResult> callback = new NonEmptyResultCallback<>() {
70
+ @Override
71
+ public void success(@NonNull GetBatteryStateResult result) {
72
+ resolveCall(call, result);
73
+ }
74
+
75
+ @Override
76
+ public void error(@NonNull Exception exception) {
77
+ rejectCall(call, exception);
78
+ }
79
+ };
80
+
81
+ implementation.getBatteryState(callback);
82
+ } catch (Exception exception) {
83
+ rejectCall(call, exception);
84
+ }
85
+ }
86
+
87
+ @PluginMethod
88
+ public void isLowPowerModeEnabled(PluginCall call) {
89
+ try {
90
+ NonEmptyResultCallback<IsLowPowerModeEnabledResult> callback = new NonEmptyResultCallback<>() {
91
+ @Override
92
+ public void success(@NonNull IsLowPowerModeEnabledResult result) {
93
+ resolveCall(call, result);
94
+ }
95
+
96
+ @Override
97
+ public void error(@NonNull Exception exception) {
98
+ rejectCall(call, exception);
99
+ }
100
+ };
101
+
102
+ implementation.isLowPowerModeEnabled(callback);
103
+ } catch (Exception exception) {
104
+ rejectCall(call, exception);
105
+ }
106
+ }
107
+
108
+ public void notifyBatteryLevelChangeListeners(@NonNull BatteryLevelChangeEvent event) {
109
+ notifyListeners(EVENT_BATTERY_LEVEL_CHANGE, event.toJSObject());
110
+ }
111
+
112
+ public void notifyBatteryStateChangeListeners(@NonNull BatteryStateChangeEvent event) {
113
+ notifyListeners(EVENT_BATTERY_STATE_CHANGE, event.toJSObject());
114
+ }
115
+
116
+ public void notifyLowPowerModeChangeListeners(@NonNull LowPowerModeChangeEvent event) {
117
+ notifyListeners(EVENT_LOW_POWER_MODE_CHANGE, event.toJSObject());
118
+ }
119
+
120
+ @Override
121
+ @PluginMethod
122
+ public void removeAllListeners(PluginCall call) {
123
+ super.removeAllListeners(call);
124
+ implementation.stopObserving();
125
+ }
126
+
127
+ @Override
128
+ @PluginMethod(returnType = PluginMethod.RETURN_NONE)
129
+ public void removeListener(PluginCall call) {
130
+ super.removeListener(call);
131
+ if (!hasAnyListeners()) {
132
+ implementation.stopObserving();
133
+ }
134
+ }
135
+
136
+ private boolean hasAnyListeners() {
137
+ return (
138
+ hasListeners(EVENT_BATTERY_LEVEL_CHANGE) ||
139
+ hasListeners(EVENT_BATTERY_STATE_CHANGE) ||
140
+ hasListeners(EVENT_LOW_POWER_MODE_CHANGE)
141
+ );
142
+ }
143
+
144
+ private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
145
+ String message = exception.getMessage();
146
+ if (message == null) {
147
+ message = ERROR_UNKNOWN_ERROR;
148
+ }
149
+ Logger.error(TAG, message, exception);
150
+ call.reject(message);
151
+ }
152
+
153
+ private void resolveCall(@NonNull PluginCall call, @NonNull Result result) {
154
+ call.resolve(result.toJSObject());
155
+ }
156
+ }
@@ -0,0 +1,20 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.classes;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+
6
+ public class CustomException extends Exception {
7
+
8
+ @Nullable
9
+ private final String code;
10
+
11
+ public CustomException(@Nullable String code, @NonNull String message) {
12
+ super(message);
13
+ this.code = code;
14
+ }
15
+
16
+ @Nullable
17
+ public String getCode() {
18
+ return code;
19
+ }
20
+ }
@@ -0,0 +1,9 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.classes;
2
+
3
+ public class CustomExceptions {
4
+
5
+ public static final CustomException BATTERY_LEVEL_UNAVAILABLE = new CustomException(
6
+ null,
7
+ "The battery level is currently unavailable."
8
+ );
9
+ }
@@ -0,0 +1,22 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.classes.events;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.JSObject;
5
+ import io.capawesome.capacitorjs.plugins.battery.interfaces.Result;
6
+
7
+ public class BatteryLevelChangeEvent implements Result {
8
+
9
+ private final float level;
10
+
11
+ public BatteryLevelChangeEvent(float level) {
12
+ this.level = level;
13
+ }
14
+
15
+ @Override
16
+ @NonNull
17
+ public JSObject toJSObject() {
18
+ JSObject result = new JSObject();
19
+ result.put("level", level);
20
+ return result;
21
+ }
22
+ }
@@ -0,0 +1,23 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.classes.events;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.JSObject;
5
+ import io.capawesome.capacitorjs.plugins.battery.interfaces.Result;
6
+
7
+ public class BatteryStateChangeEvent implements Result {
8
+
9
+ @NonNull
10
+ private final String state;
11
+
12
+ public BatteryStateChangeEvent(@NonNull String state) {
13
+ this.state = state;
14
+ }
15
+
16
+ @Override
17
+ @NonNull
18
+ public JSObject toJSObject() {
19
+ JSObject result = new JSObject();
20
+ result.put("state", state);
21
+ return result;
22
+ }
23
+ }
@@ -0,0 +1,22 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.classes.events;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.JSObject;
5
+ import io.capawesome.capacitorjs.plugins.battery.interfaces.Result;
6
+
7
+ public class LowPowerModeChangeEvent implements Result {
8
+
9
+ private final boolean enabled;
10
+
11
+ public LowPowerModeChangeEvent(boolean enabled) {
12
+ this.enabled = enabled;
13
+ }
14
+
15
+ @Override
16
+ @NonNull
17
+ public JSObject toJSObject() {
18
+ JSObject result = new JSObject();
19
+ result.put("enabled", enabled);
20
+ return result;
21
+ }
22
+ }
@@ -0,0 +1,22 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.classes.results;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.JSObject;
5
+ import io.capawesome.capacitorjs.plugins.battery.interfaces.Result;
6
+
7
+ public class GetBatteryLevelResult implements Result {
8
+
9
+ private final float level;
10
+
11
+ public GetBatteryLevelResult(float level) {
12
+ this.level = level;
13
+ }
14
+
15
+ @Override
16
+ @NonNull
17
+ public JSObject toJSObject() {
18
+ JSObject result = new JSObject();
19
+ result.put("level", level);
20
+ return result;
21
+ }
22
+ }
@@ -0,0 +1,23 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.classes.results;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.JSObject;
5
+ import io.capawesome.capacitorjs.plugins.battery.interfaces.Result;
6
+
7
+ public class GetBatteryStateResult implements Result {
8
+
9
+ @NonNull
10
+ private final String state;
11
+
12
+ public GetBatteryStateResult(@NonNull String state) {
13
+ this.state = state;
14
+ }
15
+
16
+ @Override
17
+ @NonNull
18
+ public JSObject toJSObject() {
19
+ JSObject result = new JSObject();
20
+ result.put("state", state);
21
+ return result;
22
+ }
23
+ }
@@ -0,0 +1,22 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.classes.results;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.JSObject;
5
+ import io.capawesome.capacitorjs.plugins.battery.interfaces.Result;
6
+
7
+ public class IsLowPowerModeEnabledResult implements Result {
8
+
9
+ private final boolean enabled;
10
+
11
+ public IsLowPowerModeEnabledResult(boolean enabled) {
12
+ this.enabled = enabled;
13
+ }
14
+
15
+ @Override
16
+ @NonNull
17
+ public JSObject toJSObject() {
18
+ JSObject result = new JSObject();
19
+ result.put("enabled", enabled);
20
+ return result;
21
+ }
22
+ }
@@ -0,0 +1,5 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.interfaces;
2
+
3
+ public interface Callback {
4
+ void error(Exception exception);
5
+ }
@@ -0,0 +1,7 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.interfaces;
2
+
3
+ import androidx.annotation.NonNull;
4
+
5
+ public interface NonEmptyResultCallback<T extends Result> extends Callback {
6
+ void success(@NonNull T result);
7
+ }
@@ -0,0 +1,7 @@
1
+ package io.capawesome.capacitorjs.plugins.battery.interfaces;
2
+
3
+ import com.getcapacitor.JSObject;
4
+
5
+ public interface Result {
6
+ JSObject toJSObject();
7
+ }
File without changes