@capawesome/capacitor-thermal-state 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/CapawesomeCapacitorThermalState.podspec +17 -0
- package/LICENSE +21 -0
- package/Package.swift +28 -0
- package/README.md +221 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/ThermalState.java +78 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/ThermalStatePlugin.java +99 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/classes/events/ThermalStateChangeEvent.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/classes/results/GetThermalStateResult.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +181 -0
- package/dist/esm/definitions.d.ts +65 -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 +5 -0
- package/dist/esm/web.js +7 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +21 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +24 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Classes/Events/ThermalStateChangeEvent.swift +16 -0
- package/ios/Plugin/Classes/Results/GetThermalStateResult.swift +16 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Protocols/Result.swift +5 -0
- package/ios/Plugin/ThermalState.swift +62 -0
- package/ios/Plugin/ThermalStatePlugin.swift +65 -0
- package/package.json +91 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'CapawesomeCapacitorThermalState'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['repository']['url']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '15.0'
|
|
15
|
+
s.dependency 'Capacitor'
|
|
16
|
+
s.swift_version = '5.1'
|
|
17
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robin Genz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Package.swift
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
|
|
4
|
+
let package = Package(
|
|
5
|
+
name: "CapawesomeCapacitorThermalState",
|
|
6
|
+
platforms: [.iOS(.v15)],
|
|
7
|
+
products: [
|
|
8
|
+
.library(
|
|
9
|
+
name: "CapawesomeCapacitorThermalState",
|
|
10
|
+
targets: ["ThermalStatePlugin"])
|
|
11
|
+
],
|
|
12
|
+
dependencies: [
|
|
13
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0")
|
|
14
|
+
],
|
|
15
|
+
targets: [
|
|
16
|
+
.target(
|
|
17
|
+
name: "ThermalStatePlugin",
|
|
18
|
+
dependencies: [
|
|
19
|
+
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
20
|
+
.product(name: "Cordova", package: "capacitor-swift-pm")
|
|
21
|
+
],
|
|
22
|
+
path: "ios/Plugin"),
|
|
23
|
+
.testTarget(
|
|
24
|
+
name: "ThermalStatePluginTests",
|
|
25
|
+
dependencies: ["ThermalStatePlugin"],
|
|
26
|
+
path: "ios/PluginTests")
|
|
27
|
+
]
|
|
28
|
+
)
|
package/README.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# @capawesome/capacitor-thermal-state
|
|
2
|
+
|
|
3
|
+
Capacitor plugin to read the device thermal state and react before the operating system throttles your app.
|
|
4
|
+
|
|
5
|
+
<div class="capawesome-z29o10a">
|
|
6
|
+
<a href="https://cloud.capawesome.io/" target="_blank">
|
|
7
|
+
<img alt="Deliver Live Updates to your Capacitor app with Capawesome Cloud" src="https://cloud.capawesome.io/assets/banners/cloud-build-and-deploy-capacitor-apps.png?t=1" />
|
|
8
|
+
</a>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🌡️ **Thermal state**: Read the current thermal state of the device.
|
|
14
|
+
- 🔔 **Change events**: Get notified whenever the thermal state changes.
|
|
15
|
+
- 📦 **CocoaPods & SPM**: Supports CocoaPods and Swift Package Manager for iOS.
|
|
16
|
+
- 🔁 **Up-to-date**: Always supports the latest Capacitor version.
|
|
17
|
+
|
|
18
|
+
Missing a feature? Just [open an issue](https://github.com/capawesome-team/capacitor-plugins/issues) and we'll take a look!
|
|
19
|
+
|
|
20
|
+
## Newsletter
|
|
21
|
+
|
|
22
|
+
Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our [Capawesome Newsletter](https://cloud.capawesome.io/newsletter/).
|
|
23
|
+
|
|
24
|
+
## Compatibility
|
|
25
|
+
|
|
26
|
+
| Plugin Version | Capacitor Version | Status |
|
|
27
|
+
| -------------- | ----------------- | -------------- |
|
|
28
|
+
| 0.x.x | >=8.x.x | Active support |
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
You can use our **AI-Assisted Setup** to install the plugin.
|
|
33
|
+
Add the [Capawesome Skills](https://github.com/capawesome-team/skills) to your AI tool using the following command:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npx skills add capawesome-team/skills --skill capacitor-plugins
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Then use the following prompt:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome/capacitor-thermal-state` plugin in my project.
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
If you prefer **Manual Setup**, install the plugin by running the following commands and follow the platform-specific instructions below:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm install @capawesome/capacitor-thermal-state
|
|
49
|
+
npx cap sync
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
This plugin is available on **Android** and **iOS**. On the web, all methods reject as unimplemented.
|
|
53
|
+
|
|
54
|
+
### Android
|
|
55
|
+
|
|
56
|
+
Reading the thermal state requires **Android 10** (API level 29) or newer. On older versions, `getThermalState(...)` rejects as unavailable and the `thermalStateChange` event is never emitted.
|
|
57
|
+
|
|
58
|
+
The Android thermal status values are mapped to the four common thermal states as follows:
|
|
59
|
+
|
|
60
|
+
| Android Thermal Status | Thermal State |
|
|
61
|
+
| --------------------------------------------------------------------------------------- | ------------- |
|
|
62
|
+
| `THERMAL_STATUS_NONE` | `nominal` |
|
|
63
|
+
| `THERMAL_STATUS_LIGHT` | `fair` |
|
|
64
|
+
| `THERMAL_STATUS_MODERATE` | `serious` |
|
|
65
|
+
| `THERMAL_STATUS_SEVERE` / `THERMAL_STATUS_CRITICAL` / `THERMAL_STATUS_EMERGENCY` / `THERMAL_STATUS_SHUTDOWN` | `critical` |
|
|
66
|
+
|
|
67
|
+
## Configuration
|
|
68
|
+
|
|
69
|
+
No configuration required for this plugin.
|
|
70
|
+
|
|
71
|
+
## Usage
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { ThermalState } from '@capawesome/capacitor-thermal-state';
|
|
75
|
+
|
|
76
|
+
const getThermalState = async () => {
|
|
77
|
+
const { state } = await ThermalState.getThermalState();
|
|
78
|
+
return state;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const addThermalStateChangeListener = async () => {
|
|
82
|
+
await ThermalState.addListener('thermalStateChange', event => {
|
|
83
|
+
console.log(event.state);
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const removeAllListeners = async () => {
|
|
88
|
+
await ThermalState.removeAllListeners();
|
|
89
|
+
};
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## API
|
|
93
|
+
|
|
94
|
+
<docgen-index>
|
|
95
|
+
|
|
96
|
+
* [`getThermalState()`](#getthermalstate)
|
|
97
|
+
* [`addListener('thermalStateChange', ...)`](#addlistenerthermalstatechange-)
|
|
98
|
+
* [`removeAllListeners()`](#removealllisteners)
|
|
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
|
+
### getThermalState()
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
getThermalState() => Promise<GetThermalStateResult>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Get the current thermal state of the device.
|
|
114
|
+
|
|
115
|
+
Only available on Android (API level 29+) and iOS.
|
|
116
|
+
|
|
117
|
+
**Returns:** <code>Promise<<a href="#getthermalstateresult">GetThermalStateResult</a>></code>
|
|
118
|
+
|
|
119
|
+
**Since:** 0.1.0
|
|
120
|
+
|
|
121
|
+
--------------------
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
### addListener('thermalStateChange', ...)
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
addListener(eventName: 'thermalStateChange', listenerFunc: (event: ThermalStateChangeEvent) => void) => Promise<PluginListenerHandle>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Listen for changes to the thermal state of the device.
|
|
131
|
+
|
|
132
|
+
The device is only observed while at least one listener is attached.
|
|
133
|
+
|
|
134
|
+
Only available on Android (API level 29+) and iOS.
|
|
135
|
+
|
|
136
|
+
| Param | Type |
|
|
137
|
+
| ------------------ | ----------------------------------------------------------------------------------------------- |
|
|
138
|
+
| **`eventName`** | <code>'thermalStateChange'</code> |
|
|
139
|
+
| **`listenerFunc`** | <code>(event: <a href="#thermalstatechangeevent">ThermalStateChangeEvent</a>) => void</code> |
|
|
140
|
+
|
|
141
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
142
|
+
|
|
143
|
+
**Since:** 0.1.0
|
|
144
|
+
|
|
145
|
+
--------------------
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
### removeAllListeners()
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
removeAllListeners() => Promise<void>
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Remove all listeners for this plugin.
|
|
155
|
+
|
|
156
|
+
**Since:** 0.1.0
|
|
157
|
+
|
|
158
|
+
--------------------
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
### Interfaces
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
#### GetThermalStateResult
|
|
165
|
+
|
|
166
|
+
| Prop | Type | Description | Since |
|
|
167
|
+
| ----------- | --------------------------------------------------------------- | ---------------------------------------- | ----- |
|
|
168
|
+
| **`state`** | <code><a href="#thermalstatevalue">ThermalStateValue</a></code> | The current thermal state of the device. | 0.1.0 |
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
#### PluginListenerHandle
|
|
172
|
+
|
|
173
|
+
| Prop | Type |
|
|
174
|
+
| ------------ | ----------------------------------------- |
|
|
175
|
+
| **`remove`** | <code>() => Promise<void></code> |
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
#### ThermalStateChangeEvent
|
|
179
|
+
|
|
180
|
+
| Prop | Type | Description | Since |
|
|
181
|
+
| ----------- | --------------------------------------------------------------- | ---------------------------------------- | ----- |
|
|
182
|
+
| **`state`** | <code><a href="#thermalstatevalue">ThermalStateValue</a></code> | The current thermal state of the device. | 0.1.0 |
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
### Type Aliases
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
#### ThermalStateValue
|
|
189
|
+
|
|
190
|
+
The thermal state of the device.
|
|
191
|
+
|
|
192
|
+
- `critical`: The thermal state is significantly impacting performance.
|
|
193
|
+
Reduce the workload as much as possible.
|
|
194
|
+
- `fair`: The thermal state is slightly elevated. Consider reducing
|
|
195
|
+
non-essential work.
|
|
196
|
+
- `nominal`: The thermal state is within normal limits. No action is needed.
|
|
197
|
+
- `serious`: The thermal state is high. Reduce the workload to help the
|
|
198
|
+
device cool down.
|
|
199
|
+
|
|
200
|
+
<code>'critical' | 'fair' | 'nominal' | 'serious'</code>
|
|
201
|
+
|
|
202
|
+
</docgen-api>
|
|
203
|
+
|
|
204
|
+
## Handling the Thermal State
|
|
205
|
+
|
|
206
|
+
Use the thermal state to progressively reduce your app's workload before the operating system throttles it. The following table lists suggested reactions for each state:
|
|
207
|
+
|
|
208
|
+
| State | Suggested App Reaction |
|
|
209
|
+
| ---------- | -------------------------------------------------------------------------------------- |
|
|
210
|
+
| `nominal` | No action needed. Run at full quality. |
|
|
211
|
+
| `fair` | Reduce non-essential work, e.g. lower the prefetching rate. |
|
|
212
|
+
| `serious` | Reduce the frame rate, pause prefetching, and defer background or ML work. |
|
|
213
|
+
| `critical` | Reduce the workload as much as possible to help the device cool down. |
|
|
214
|
+
|
|
215
|
+
## Changelog
|
|
216
|
+
|
|
217
|
+
See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/thermal-state/CHANGELOG.md).
|
|
218
|
+
|
|
219
|
+
## License
|
|
220
|
+
|
|
221
|
+
See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/thermal-state/LICENSE).
|
|
@@ -0,0 +1,58 @@
|
|
|
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.3.0'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
dependencies {
|
|
14
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
apply plugin: 'com.android.library'
|
|
19
|
+
|
|
20
|
+
android {
|
|
21
|
+
namespace = "io.capawesome.capacitorjs.plugins.thermalstate"
|
|
22
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
25
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.0"
|
|
28
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
29
|
+
}
|
|
30
|
+
buildTypes {
|
|
31
|
+
release {
|
|
32
|
+
minifyEnabled false
|
|
33
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
lintOptions {
|
|
37
|
+
abortOnError = false
|
|
38
|
+
}
|
|
39
|
+
compileOptions {
|
|
40
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
repositories {
|
|
46
|
+
google()
|
|
47
|
+
mavenCentral()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
dependencies {
|
|
52
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
53
|
+
implementation project(':capacitor-android')
|
|
54
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
55
|
+
testImplementation "junit:junit:$junitVersion"
|
|
56
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
57
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
58
|
+
}
|
package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/ThermalState.java
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.thermalstate;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.os.Build;
|
|
5
|
+
import android.os.PowerManager;
|
|
6
|
+
import androidx.annotation.NonNull;
|
|
7
|
+
import androidx.annotation.RequiresApi;
|
|
8
|
+
import androidx.core.content.ContextCompat;
|
|
9
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.classes.events.ThermalStateChangeEvent;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.classes.results.GetThermalStateResult;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.interfaces.NonEmptyResultCallback;
|
|
12
|
+
import java.util.concurrent.Executor;
|
|
13
|
+
|
|
14
|
+
public class ThermalState {
|
|
15
|
+
|
|
16
|
+
@NonNull
|
|
17
|
+
private final ThermalStatePlugin plugin;
|
|
18
|
+
|
|
19
|
+
private boolean isObserving = false;
|
|
20
|
+
|
|
21
|
+
@NonNull
|
|
22
|
+
private final PowerManager powerManager;
|
|
23
|
+
|
|
24
|
+
private PowerManager.OnThermalStatusChangedListener thermalStatusChangedListener;
|
|
25
|
+
|
|
26
|
+
public ThermalState(@NonNull ThermalStatePlugin plugin) {
|
|
27
|
+
this.plugin = plugin;
|
|
28
|
+
this.powerManager = (PowerManager) plugin.getContext().getSystemService(Context.POWER_SERVICE);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@RequiresApi(api = Build.VERSION_CODES.Q)
|
|
32
|
+
public void getThermalState(@NonNull NonEmptyResultCallback<GetThermalStateResult> callback) {
|
|
33
|
+
String state = mapThermalStatus(powerManager.getCurrentThermalStatus());
|
|
34
|
+
callback.success(new GetThermalStateResult(state));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public void startObserving() {
|
|
38
|
+
if (isObserving || Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
Executor executor = ContextCompat.getMainExecutor(plugin.getContext());
|
|
42
|
+
thermalStatusChangedListener = this::handleThermalStatusChanged;
|
|
43
|
+
powerManager.addThermalStatusListener(executor, thermalStatusChangedListener);
|
|
44
|
+
isObserving = true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public void stopObserving() {
|
|
48
|
+
if (!isObserving) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (thermalStatusChangedListener != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
52
|
+
powerManager.removeThermalStatusListener(thermalStatusChangedListener);
|
|
53
|
+
thermalStatusChangedListener = null;
|
|
54
|
+
}
|
|
55
|
+
isObserving = false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private void handleThermalStatusChanged(int status) {
|
|
59
|
+
plugin.notifyThermalStateChangeListeners(new ThermalStateChangeEvent(mapThermalStatus(status)));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@NonNull
|
|
63
|
+
private String mapThermalStatus(int status) {
|
|
64
|
+
switch (status) {
|
|
65
|
+
case PowerManager.THERMAL_STATUS_SEVERE:
|
|
66
|
+
case PowerManager.THERMAL_STATUS_CRITICAL:
|
|
67
|
+
case PowerManager.THERMAL_STATUS_EMERGENCY:
|
|
68
|
+
case PowerManager.THERMAL_STATUS_SHUTDOWN:
|
|
69
|
+
return "critical";
|
|
70
|
+
case PowerManager.THERMAL_STATUS_LIGHT:
|
|
71
|
+
return "fair";
|
|
72
|
+
case PowerManager.THERMAL_STATUS_MODERATE:
|
|
73
|
+
return "serious";
|
|
74
|
+
default:
|
|
75
|
+
return "nominal";
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
package/android/src/main/java/io/capawesome/capacitorjs/plugins/thermalstate/ThermalStatePlugin.java
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.thermalstate;
|
|
2
|
+
|
|
3
|
+
import android.os.Build;
|
|
4
|
+
import androidx.annotation.NonNull;
|
|
5
|
+
import com.getcapacitor.Logger;
|
|
6
|
+
import com.getcapacitor.Plugin;
|
|
7
|
+
import com.getcapacitor.PluginCall;
|
|
8
|
+
import com.getcapacitor.PluginMethod;
|
|
9
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.classes.events.ThermalStateChangeEvent;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.classes.results.GetThermalStateResult;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.interfaces.NonEmptyResultCallback;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.interfaces.Result;
|
|
14
|
+
|
|
15
|
+
@CapacitorPlugin(name = "ThermalState")
|
|
16
|
+
public class ThermalStatePlugin extends Plugin {
|
|
17
|
+
|
|
18
|
+
public static final String EVENT_THERMAL_STATE_CHANGE = "thermalStateChange";
|
|
19
|
+
|
|
20
|
+
public static final String TAG = "ThermalStatePlugin";
|
|
21
|
+
|
|
22
|
+
private static final String ERROR_UNKNOWN_ERROR = "An unknown error occurred.";
|
|
23
|
+
|
|
24
|
+
private ThermalState implementation;
|
|
25
|
+
|
|
26
|
+
@Override
|
|
27
|
+
public void load() {
|
|
28
|
+
implementation = new ThermalState(this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@Override
|
|
32
|
+
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
|
|
33
|
+
public void addListener(PluginCall call) {
|
|
34
|
+
super.addListener(call);
|
|
35
|
+
implementation.startObserving();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@PluginMethod
|
|
39
|
+
public void getThermalState(PluginCall call) {
|
|
40
|
+
try {
|
|
41
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
|
42
|
+
rejectCallAsUnavailable(call);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
NonEmptyResultCallback<GetThermalStateResult> callback = new NonEmptyResultCallback<>() {
|
|
46
|
+
@Override
|
|
47
|
+
public void success(@NonNull GetThermalStateResult result) {
|
|
48
|
+
resolveCall(call, result);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@Override
|
|
52
|
+
public void error(@NonNull Exception exception) {
|
|
53
|
+
rejectCall(call, exception);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
implementation.getThermalState(callback);
|
|
58
|
+
} catch (Exception exception) {
|
|
59
|
+
rejectCall(call, exception);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public void notifyThermalStateChangeListeners(@NonNull ThermalStateChangeEvent event) {
|
|
64
|
+
notifyListeners(EVENT_THERMAL_STATE_CHANGE, event.toJSObject());
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@Override
|
|
68
|
+
@PluginMethod
|
|
69
|
+
public void removeAllListeners(PluginCall call) {
|
|
70
|
+
super.removeAllListeners(call);
|
|
71
|
+
implementation.stopObserving();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@Override
|
|
75
|
+
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
|
|
76
|
+
public void removeListener(PluginCall call) {
|
|
77
|
+
super.removeListener(call);
|
|
78
|
+
if (!hasListeners(EVENT_THERMAL_STATE_CHANGE)) {
|
|
79
|
+
implementation.stopObserving();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
|
|
84
|
+
String message = exception.getMessage();
|
|
85
|
+
if (message == null) {
|
|
86
|
+
message = ERROR_UNKNOWN_ERROR;
|
|
87
|
+
}
|
|
88
|
+
Logger.error(TAG, message, exception);
|
|
89
|
+
call.reject(message);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private void rejectCallAsUnavailable(@NonNull PluginCall call) {
|
|
93
|
+
call.unavailable("This method is not available on this platform.");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private void resolveCall(@NonNull PluginCall call, @NonNull Result result) {
|
|
97
|
+
call.resolve(result.toJSObject());
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.thermalstate.classes.events;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.interfaces.Result;
|
|
6
|
+
|
|
7
|
+
public class ThermalStateChangeEvent implements Result {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String state;
|
|
11
|
+
|
|
12
|
+
public ThermalStateChangeEvent(@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,23 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.thermalstate.classes.results;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.thermalstate.interfaces.Result;
|
|
6
|
+
|
|
7
|
+
public class GetThermalStateResult implements Result {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String state;
|
|
11
|
+
|
|
12
|
+
public GetThermalStateResult(@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
|
+
}
|
|
File without changes
|
package/dist/docs.json
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
{
|
|
2
|
+
"api": {
|
|
3
|
+
"name": "ThermalStatePlugin",
|
|
4
|
+
"slug": "thermalstateplugin",
|
|
5
|
+
"docs": "",
|
|
6
|
+
"tags": [],
|
|
7
|
+
"methods": [
|
|
8
|
+
{
|
|
9
|
+
"name": "getThermalState",
|
|
10
|
+
"signature": "() => Promise<GetThermalStateResult>",
|
|
11
|
+
"parameters": [],
|
|
12
|
+
"returns": "Promise<GetThermalStateResult>",
|
|
13
|
+
"tags": [
|
|
14
|
+
{
|
|
15
|
+
"name": "since",
|
|
16
|
+
"text": "0.1.0"
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
"docs": "Get the current thermal state of the device.\n\nOnly available on Android (API level 29+) and iOS.",
|
|
20
|
+
"complexTypes": [
|
|
21
|
+
"GetThermalStateResult"
|
|
22
|
+
],
|
|
23
|
+
"slug": "getthermalstate"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"name": "addListener",
|
|
27
|
+
"signature": "(eventName: 'thermalStateChange', listenerFunc: (event: ThermalStateChangeEvent) => void) => Promise<PluginListenerHandle>",
|
|
28
|
+
"parameters": [
|
|
29
|
+
{
|
|
30
|
+
"name": "eventName",
|
|
31
|
+
"docs": "",
|
|
32
|
+
"type": "'thermalStateChange'"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"name": "listenerFunc",
|
|
36
|
+
"docs": "",
|
|
37
|
+
"type": "(event: ThermalStateChangeEvent) => void"
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
"returns": "Promise<PluginListenerHandle>",
|
|
41
|
+
"tags": [
|
|
42
|
+
{
|
|
43
|
+
"name": "since",
|
|
44
|
+
"text": "0.1.0"
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
"docs": "Listen for changes to the thermal state of the device.\n\nThe device is only observed while at least one listener is attached.\n\nOnly available on Android (API level 29+) and iOS.",
|
|
48
|
+
"complexTypes": [
|
|
49
|
+
"PluginListenerHandle",
|
|
50
|
+
"ThermalStateChangeEvent"
|
|
51
|
+
],
|
|
52
|
+
"slug": "addlistenerthermalstatechange-"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "removeAllListeners",
|
|
56
|
+
"signature": "() => Promise<void>",
|
|
57
|
+
"parameters": [],
|
|
58
|
+
"returns": "Promise<void>",
|
|
59
|
+
"tags": [
|
|
60
|
+
{
|
|
61
|
+
"name": "since",
|
|
62
|
+
"text": "0.1.0"
|
|
63
|
+
}
|
|
64
|
+
],
|
|
65
|
+
"docs": "Remove all listeners for this plugin.",
|
|
66
|
+
"complexTypes": [],
|
|
67
|
+
"slug": "removealllisteners"
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
"properties": []
|
|
71
|
+
},
|
|
72
|
+
"interfaces": [
|
|
73
|
+
{
|
|
74
|
+
"name": "GetThermalStateResult",
|
|
75
|
+
"slug": "getthermalstateresult",
|
|
76
|
+
"docs": "",
|
|
77
|
+
"tags": [
|
|
78
|
+
{
|
|
79
|
+
"text": "0.1.0",
|
|
80
|
+
"name": "since"
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
"methods": [],
|
|
84
|
+
"properties": [
|
|
85
|
+
{
|
|
86
|
+
"name": "state",
|
|
87
|
+
"tags": [
|
|
88
|
+
{
|
|
89
|
+
"text": "'nominal'",
|
|
90
|
+
"name": "example"
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
"text": "0.1.0",
|
|
94
|
+
"name": "since"
|
|
95
|
+
}
|
|
96
|
+
],
|
|
97
|
+
"docs": "The current thermal state of the device.",
|
|
98
|
+
"complexTypes": [
|
|
99
|
+
"ThermalStateValue"
|
|
100
|
+
],
|
|
101
|
+
"type": "ThermalStateValue"
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"name": "PluginListenerHandle",
|
|
107
|
+
"slug": "pluginlistenerhandle",
|
|
108
|
+
"docs": "",
|
|
109
|
+
"tags": [],
|
|
110
|
+
"methods": [],
|
|
111
|
+
"properties": [
|
|
112
|
+
{
|
|
113
|
+
"name": "remove",
|
|
114
|
+
"tags": [],
|
|
115
|
+
"docs": "",
|
|
116
|
+
"complexTypes": [],
|
|
117
|
+
"type": "() => Promise<void>"
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"name": "ThermalStateChangeEvent",
|
|
123
|
+
"slug": "thermalstatechangeevent",
|
|
124
|
+
"docs": "",
|
|
125
|
+
"tags": [
|
|
126
|
+
{
|
|
127
|
+
"text": "0.1.0",
|
|
128
|
+
"name": "since"
|
|
129
|
+
}
|
|
130
|
+
],
|
|
131
|
+
"methods": [],
|
|
132
|
+
"properties": [
|
|
133
|
+
{
|
|
134
|
+
"name": "state",
|
|
135
|
+
"tags": [
|
|
136
|
+
{
|
|
137
|
+
"text": "'nominal'",
|
|
138
|
+
"name": "example"
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
"text": "0.1.0",
|
|
142
|
+
"name": "since"
|
|
143
|
+
}
|
|
144
|
+
],
|
|
145
|
+
"docs": "The current thermal state of the device.",
|
|
146
|
+
"complexTypes": [
|
|
147
|
+
"ThermalStateValue"
|
|
148
|
+
],
|
|
149
|
+
"type": "ThermalStateValue"
|
|
150
|
+
}
|
|
151
|
+
]
|
|
152
|
+
}
|
|
153
|
+
],
|
|
154
|
+
"enums": [],
|
|
155
|
+
"typeAliases": [
|
|
156
|
+
{
|
|
157
|
+
"name": "ThermalStateValue",
|
|
158
|
+
"slug": "thermalstatevalue",
|
|
159
|
+
"docs": "The thermal state of the device.\n\n- `critical`: The thermal state is significantly impacting performance.\n Reduce the workload as much as possible.\n- `fair`: The thermal state is slightly elevated. Consider reducing\n non-essential work.\n- `nominal`: The thermal state is within normal limits. No action is needed.\n- `serious`: The thermal state is high. Reduce the workload to help the\n device cool down.",
|
|
160
|
+
"types": [
|
|
161
|
+
{
|
|
162
|
+
"text": "'critical'",
|
|
163
|
+
"complexTypes": []
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
"text": "'fair'",
|
|
167
|
+
"complexTypes": []
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
"text": "'nominal'",
|
|
171
|
+
"complexTypes": []
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
"text": "'serious'",
|
|
175
|
+
"complexTypes": []
|
|
176
|
+
}
|
|
177
|
+
]
|
|
178
|
+
}
|
|
179
|
+
],
|
|
180
|
+
"pluginConfigs": []
|
|
181
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { PluginListenerHandle } from '@capacitor/core';
|
|
2
|
+
export interface ThermalStatePlugin {
|
|
3
|
+
/**
|
|
4
|
+
* Get the current thermal state of the device.
|
|
5
|
+
*
|
|
6
|
+
* Only available on Android (API level 29+) and iOS.
|
|
7
|
+
*
|
|
8
|
+
* @since 0.1.0
|
|
9
|
+
*/
|
|
10
|
+
getThermalState(): Promise<GetThermalStateResult>;
|
|
11
|
+
/**
|
|
12
|
+
* Listen for changes to the thermal state of the device.
|
|
13
|
+
*
|
|
14
|
+
* The device is only observed while at least one listener is attached.
|
|
15
|
+
*
|
|
16
|
+
* Only available on Android (API level 29+) and iOS.
|
|
17
|
+
*
|
|
18
|
+
* @since 0.1.0
|
|
19
|
+
*/
|
|
20
|
+
addListener(eventName: 'thermalStateChange', listenerFunc: (event: ThermalStateChangeEvent) => void): Promise<PluginListenerHandle>;
|
|
21
|
+
/**
|
|
22
|
+
* Remove all listeners for this plugin.
|
|
23
|
+
*
|
|
24
|
+
* @since 0.1.0
|
|
25
|
+
*/
|
|
26
|
+
removeAllListeners(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @since 0.1.0
|
|
30
|
+
*/
|
|
31
|
+
export interface GetThermalStateResult {
|
|
32
|
+
/**
|
|
33
|
+
* The current thermal state of the device.
|
|
34
|
+
*
|
|
35
|
+
* @example 'nominal'
|
|
36
|
+
* @since 0.1.0
|
|
37
|
+
*/
|
|
38
|
+
state: ThermalStateValue;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @since 0.1.0
|
|
42
|
+
*/
|
|
43
|
+
export interface ThermalStateChangeEvent {
|
|
44
|
+
/**
|
|
45
|
+
* The current thermal state of the device.
|
|
46
|
+
*
|
|
47
|
+
* @example 'nominal'
|
|
48
|
+
* @since 0.1.0
|
|
49
|
+
*/
|
|
50
|
+
state: ThermalStateValue;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The thermal state of the device.
|
|
54
|
+
*
|
|
55
|
+
* - `critical`: The thermal state is significantly impacting performance.
|
|
56
|
+
* Reduce the workload as much as possible.
|
|
57
|
+
* - `fair`: The thermal state is slightly elevated. Consider reducing
|
|
58
|
+
* non-essential work.
|
|
59
|
+
* - `nominal`: The thermal state is within normal limits. No action is needed.
|
|
60
|
+
* - `serious`: The thermal state is high. Reduce the workload to help the
|
|
61
|
+
* device cool down.
|
|
62
|
+
*
|
|
63
|
+
* @since 0.1.0
|
|
64
|
+
*/
|
|
65
|
+
export type ThermalStateValue = 'critical' | 'fair' | 'nominal' | 'serious';
|
|
@@ -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 ThermalStatePlugin {\n /**\n * Get the current thermal state of the device.\n *\n * Only available on Android (API level 29+) and iOS.\n *\n * @since 0.1.0\n */\n getThermalState(): Promise<GetThermalStateResult>;\n /**\n * Listen for changes to the thermal state of the device.\n *\n * The device is only observed while at least one listener is attached.\n *\n * Only available on Android (API level 29+) and iOS.\n *\n * @since 0.1.0\n */\n addListener(\n eventName: 'thermalStateChange',\n listenerFunc: (event: ThermalStateChangeEvent) => 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 GetThermalStateResult {\n /**\n * The current thermal state of the device.\n *\n * @example 'nominal'\n * @since 0.1.0\n */\n state: ThermalStateValue;\n}\n\n/**\n * @since 0.1.0\n */\nexport interface ThermalStateChangeEvent {\n /**\n * The current thermal state of the device.\n *\n * @example 'nominal'\n * @since 0.1.0\n */\n state: ThermalStateValue;\n}\n\n/**\n * The thermal state of the device.\n *\n * - `critical`: The thermal state is significantly impacting performance.\n * Reduce the workload as much as possible.\n * - `fair`: The thermal state is slightly elevated. Consider reducing\n * non-essential work.\n * - `nominal`: The thermal state is within normal limits. No action is needed.\n * - `serious`: The thermal state is high. Reduce the workload to help the\n * device cool down.\n *\n * @since 0.1.0\n */\nexport type ThermalStateValue = 'critical' | 'fair' | 'nominal' | 'serious';\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { registerPlugin } from '@capacitor/core';
|
|
2
|
+
const ThermalState = registerPlugin('ThermalState', {
|
|
3
|
+
web: () => import('./web').then(m => new m.ThermalStateWeb()),
|
|
4
|
+
});
|
|
5
|
+
export * from './definitions';
|
|
6
|
+
export { ThermalState };
|
|
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,YAAY,GAAG,cAAc,CAAqB,cAAc,EAAE;IACtE,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,eAAe,EAAE,CAAC;CAC9D,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { ThermalStatePlugin } from './definitions';\n\nconst ThermalState = registerPlugin<ThermalStatePlugin>('ThermalState', {\n web: () => import('./web').then(m => new m.ThermalStateWeb()),\n});\n\nexport * from './definitions';\nexport { ThermalState };\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
import type { GetThermalStateResult, ThermalStatePlugin } from './definitions';
|
|
3
|
+
export declare class ThermalStateWeb extends WebPlugin implements ThermalStatePlugin {
|
|
4
|
+
getThermalState(): Promise<GetThermalStateResult>;
|
|
5
|
+
}
|
package/dist/esm/web.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,OAAO,eAAgB,SAAQ,SAAS;IAC5C,KAAK,CAAC,eAAe;QACnB,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC,CAAC;IACtD,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type { GetThermalStateResult, ThermalStatePlugin } from './definitions';\n\nexport class ThermalStateWeb extends WebPlugin implements ThermalStatePlugin {\n async getThermalState(): Promise<GetThermalStateResult> {\n throw this.unimplemented('Not implemented on web.');\n }\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@capacitor/core');
|
|
4
|
+
|
|
5
|
+
const ThermalState = core.registerPlugin('ThermalState', {
|
|
6
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.ThermalStateWeb()),
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
class ThermalStateWeb extends core.WebPlugin {
|
|
10
|
+
async getThermalState() {
|
|
11
|
+
throw this.unimplemented('Not implemented on web.');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
16
|
+
__proto__: null,
|
|
17
|
+
ThermalStateWeb: ThermalStateWeb
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
exports.ThermalState = ThermalState;
|
|
21
|
+
//# 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 ThermalState = registerPlugin('ThermalState', {\n web: () => import('./web').then(m => new m.ThermalStateWeb()),\n});\nexport * from './definitions';\nexport { ThermalState };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class ThermalStateWeb extends WebPlugin {\n async getThermalState() {\n throw this.unimplemented('Not implemented on web.');\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,YAAY,GAAGA,mBAAc,CAAC,cAAc,EAAE;AACpD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,eAAe,EAAE,CAAC;AACjE,CAAC;;ACFM,MAAM,eAAe,SAASC,cAAS,CAAC;AAC/C,IAAI,MAAM,eAAe,GAAG;AAC5B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC;AAC3D,IAAI;AACJ;;;;;;;;;"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var capacitorThermalState = (function (exports, core) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const ThermalState = core.registerPlugin('ThermalState', {
|
|
5
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.ThermalStateWeb()),
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
class ThermalStateWeb extends core.WebPlugin {
|
|
9
|
+
async getThermalState() {
|
|
10
|
+
throw this.unimplemented('Not implemented on web.');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
15
|
+
__proto__: null,
|
|
16
|
+
ThermalStateWeb: ThermalStateWeb
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
exports.ThermalState = ThermalState;
|
|
20
|
+
|
|
21
|
+
return exports;
|
|
22
|
+
|
|
23
|
+
})({}, capacitorExports);
|
|
24
|
+
//# 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 ThermalState = registerPlugin('ThermalState', {\n web: () => import('./web').then(m => new m.ThermalStateWeb()),\n});\nexport * from './definitions';\nexport { ThermalState };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class ThermalStateWeb extends WebPlugin {\n async getThermalState() {\n throw this.unimplemented('Not implemented on web.');\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,YAAY,GAAGA,mBAAc,CAAC,cAAc,EAAE;IACpD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,eAAe,EAAE,CAAC;IACjE,CAAC;;ICFM,MAAM,eAAe,SAASC,cAAS,CAAC;IAC/C,IAAI,MAAM,eAAe,GAAG;IAC5B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC;IAC3D,IAAI;IACJ;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class ThermalStateChangeEvent: 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 GetThermalStateResult: 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,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>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
@objc public class ThermalState: NSObject {
|
|
4
|
+
private let plugin: ThermalStatePlugin
|
|
5
|
+
|
|
6
|
+
private var isObserving = false
|
|
7
|
+
|
|
8
|
+
init(plugin: ThermalStatePlugin) {
|
|
9
|
+
self.plugin = plugin
|
|
10
|
+
super.init()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
deinit {
|
|
14
|
+
NotificationCenter.default.removeObserver(self)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@objc public func getThermalState(completion: @escaping (GetThermalStateResult?, Error?) -> Void) {
|
|
18
|
+
let state = mapThermalState(ProcessInfo.processInfo.thermalState)
|
|
19
|
+
completion(GetThermalStateResult(state: state), nil)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func startObserving() {
|
|
23
|
+
guard !isObserving else {
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
isObserving = true
|
|
27
|
+
NotificationCenter.default.addObserver(
|
|
28
|
+
self,
|
|
29
|
+
selector: #selector(handleThermalStateDidChange),
|
|
30
|
+
name: ProcessInfo.thermalStateDidChangeNotification,
|
|
31
|
+
object: nil
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
func stopObserving() {
|
|
36
|
+
guard isObserving else {
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
isObserving = false
|
|
40
|
+
NotificationCenter.default.removeObserver(self, name: ProcessInfo.thermalStateDidChangeNotification, object: nil)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@objc private func handleThermalStateDidChange() {
|
|
44
|
+
let state = mapThermalState(ProcessInfo.processInfo.thermalState)
|
|
45
|
+
plugin.notifyThermalStateChangeListeners(ThermalStateChangeEvent(state: state))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private func mapThermalState(_ state: ProcessInfo.ThermalState) -> String {
|
|
49
|
+
switch state {
|
|
50
|
+
case .critical:
|
|
51
|
+
return "critical"
|
|
52
|
+
case .fair:
|
|
53
|
+
return "fair"
|
|
54
|
+
case .nominal:
|
|
55
|
+
return "nominal"
|
|
56
|
+
case .serious:
|
|
57
|
+
return "serious"
|
|
58
|
+
@unknown default:
|
|
59
|
+
return "nominal"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc(ThermalStatePlugin)
|
|
5
|
+
public class ThermalStatePlugin: CAPPlugin, CAPBridgedPlugin {
|
|
6
|
+
public let identifier = "ThermalStatePlugin"
|
|
7
|
+
public let jsName = "ThermalState"
|
|
8
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
9
|
+
CAPPluginMethod(name: "getThermalState", returnType: CAPPluginReturnPromise)
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
public static let eventThermalStateChange = "thermalStateChange"
|
|
13
|
+
|
|
14
|
+
public static let tag = "ThermalStatePlugin"
|
|
15
|
+
|
|
16
|
+
private var implementation: ThermalState?
|
|
17
|
+
|
|
18
|
+
override public func load() {
|
|
19
|
+
self.implementation = ThermalState(plugin: self)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@objc override public func addListener(_ call: CAPPluginCall) {
|
|
23
|
+
super.addListener(call)
|
|
24
|
+
implementation?.startObserving()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@objc func getThermalState(_ call: CAPPluginCall) {
|
|
28
|
+
implementation?.getThermalState { result, error in
|
|
29
|
+
if let error = error {
|
|
30
|
+
self.rejectCall(call, error)
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
self.resolveCall(call, result)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public func notifyThermalStateChangeListeners(_ event: ThermalStateChangeEvent) {
|
|
38
|
+
self.notifyListeners(Self.eventThermalStateChange, data: event.toJSObject() as? [String: Any])
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@objc override public func removeAllListeners(_ call: CAPPluginCall) {
|
|
42
|
+
super.removeAllListeners(call)
|
|
43
|
+
implementation?.stopObserving()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@objc override public func removeListener(_ call: CAPPluginCall) {
|
|
47
|
+
super.removeListener(call)
|
|
48
|
+
if !hasListeners(Self.eventThermalStateChange) {
|
|
49
|
+
implementation?.stopObserving()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private func rejectCall(_ call: CAPPluginCall, _ error: Error) {
|
|
54
|
+
CAPLog.print("[", ThermalStatePlugin.tag, "] ", error)
|
|
55
|
+
call.reject(error.localizedDescription)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private func resolveCall(_ call: CAPPluginCall, _ result: Result?) {
|
|
59
|
+
if let result = result?.toJSObject() as? JSObject {
|
|
60
|
+
call.resolve(result)
|
|
61
|
+
} else {
|
|
62
|
+
call.resolve()
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capawesome/capacitor-thermal-state",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Capacitor plugin to read the device thermal state.",
|
|
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
|
+
"CapawesomeCapacitorThermalState.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/thermal-state/",
|
|
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 ThermalStatePlugin --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
|
+
}
|