Skip to content

Commit a11599a

Browse files
authored
thermal: add thermal headroom (cpu) and battery temperature monitoring in Android (#27)
* thermal: add thermal headroom (cpu) and battery temperature monitoring * reformatting * android defines * GetThermalHeadroom() interval * suppress JNI overhead * remove unnecessary methods * DeleteJNIArgArray * move java thermal impls to another module * add notification of latest battery temperature on receiver registration
1 parent 3cc70ae commit a11599a

35 files changed

Lines changed: 588 additions & 20 deletions

File tree

Android/.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ android {
2929

3030
dependencies {
3131
implementation project(':MobileSupportStorage.androidlib')
32+
implementation project(':MobileSupportThermal.androidlib')
3233

3334
implementation 'androidx.appcompat:appcompat:1.3.0'
3435
implementation 'com.google.android.material:material:1.4.0'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jp.co.cyberagent.unitysupport.app;
2+
3+
import android.os.Build;
4+
5+
import java.util.function.Consumer;
6+
7+
public class BatteryTemperatureReceiver implements jp.co.cyberagent.unitysupport.thermal.BatteryTemperatureReceiver {
8+
private Consumer<Integer> onReceive;
9+
10+
@Override
11+
public void onReceiveBatteryTemperature(int level) {
12+
onReceive.accept(new Integer(level));
13+
}
14+
15+
public BatteryTemperatureReceiver(Consumer<Integer> onReceive)
16+
{
17+
18+
this.onReceive = onReceive;
19+
}
20+
}

Android/app/src/main/java/jp/co/cyberagent/unitysupport/app/MainActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import android.os.Bundle;
66
import android.widget.TextView;
77

8-
import jp.co.cyberagent.unitysupport.Storage;
8+
import jp.co.cyberagent.unitysupport.storage.Storage;
9+
import jp.co.cyberagent.unitysupport.thermal.BatteryChangedBroadcastReceiver;
910

1011
public class MainActivity extends AppCompatActivity {
1112

@@ -22,5 +23,12 @@ protected void onCreate(Bundle savedInstanceState) {
2223
long usableSpaceAbove0 = Storage.getInternalUsableSpaceAboveO(getApplicationContext(), -1);
2324
textViewUsableSpaceAboveO.setText(String.valueOf(usableSpaceAbove0));
2425
}
26+
27+
BatteryChangedBroadcastReceiver broadcastReceiver = new BatteryChangedBroadcastReceiver();
28+
broadcastReceiver.registerToContext(this.getApplicationContext());
29+
TextView textViewBatteryTemperature = findViewById(R.id.textViewBatteryTemperatureValue);
30+
broadcastReceiver.addReceiver(new BatteryTemperatureReceiver(level -> {
31+
textViewBatteryTemperature.setText(level.toString());
32+
}));
2533
}
2634
}

Android/app/src/main/res/layout/activity_main.xml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<LinearLayout
3737
android:layout_width="match_parent"
3838
android:layout_height="wrap_content"
39-
android:layout_weight="1"
4039
android:orientation="horizontal">
4140

4241
<TextView
@@ -53,6 +52,29 @@
5352
android:layout_weight="1"
5453
android:text="0" />
5554
</LinearLayout>
55+
56+
<LinearLayout
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
android:layout_weight="1"
60+
android:orientation="horizontal">
61+
62+
<TextView
63+
android:id="@+id/textViewBatteryTemperature"
64+
android:layout_width="wrap_content"
65+
android:layout_height="wrap_content"
66+
android:layout_weight="1"
67+
android:text="BatteryTemperature" />
68+
69+
<TextView
70+
android:id="@+id/textViewBatteryTemperatureValue"
71+
android:layout_width="wrap_content"
72+
android:layout_height="wrap_content"
73+
android:layout_weight="1"
74+
android:text="-" />
75+
76+
</LinearLayout>
77+
5678
</LinearLayout>
5779

5880
</androidx.constraintlayout.widget.ConstraintLayout>

Android/settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ include ':app'
1717

1818
include ':MobileSupportStorage.androidlib'
1919
project(':MobileSupportStorage.androidlib').projectDir = new File(settingsDir, '../Packages/MobileSupportStorage/Runtime/Plugins/Android/MobileSupportStorage.androidlib')
20+
include ':MobileSupportThermal.androidlib'
21+
project(':MobileSupportThermal.androidlib').projectDir = new File(settingsDir, '../Packages/MobileSupportThermal/Runtime/Plugins/Android/MobileSupportThermal.androidlib')

Assets/Scripts/ThermalView.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,43 @@
22
// Copyright 2023 CyberAgent, Inc.
33
// --------------------------------------------------------------
44

5+
using System.Collections;
6+
using System.Diagnostics;
57
using MobileSupport;
68
using UnityEngine;
9+
using Debug = UnityEngine.Debug;
710

811
public class ThermalView : MonoBehaviour
912
{
1013
private void Start()
1114
{
15+
#if UNITY_ANDROID
16+
Thermal.OnBatteryTemperatureChanged += value => Debug.Log($"Battery Temperature: {value}");
17+
#endif
18+
1219
#if UNITY_ANDROID || UNITY_IOS
1320
Thermal.OnThermalStatusChanged += status => Debug.Log($"Thermal Status: {status}");
1421
Thermal.StartMonitoring();
1522
#endif
23+
24+
#if UNITY_ANDROID
25+
StartCoroutine(GetTemperaturesLooped());
26+
#endif
27+
}
28+
29+
private IEnumerator GetTemperaturesLooped()
30+
{
31+
Stopwatch sw = new();
32+
33+
while (this)
34+
{
35+
yield return new WaitForSeconds(0.5f);
36+
37+
sw.Restart();
38+
Thermal.GetThermalHeadroom(0, out var headroom, out var resultForecastSeconds, out var isLatestValue);
39+
var elapsed = (double)sw.ElapsedTicks / Stopwatch.Frequency * 1000;
40+
41+
Debug.Log($"Thermal Headroom: {headroom}, isLatestValue: {isLatestValue}, resultForecastSeconds: {resultForecastSeconds} (obtained in {elapsed} ms)");
42+
}
1643
}
1744
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="jp.co.cyberagent.unitysupport">
3+
package="jp.co.cyberagent.unitysupport.storage">
44

55
</manifest>

Packages/MobileSupportStorage/Runtime/Plugins/Android/MobileSupportStorage.androidlib/src/main/java/jp/co/cyberagent/unitysupport/storage.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/MobileSupportStorage/Runtime/Plugins/Android/MobileSupportStorage.androidlib/src/main/java/jp/co/cyberagent/unitysupport/Storage.java renamed to Packages/MobileSupportStorage/Runtime/Plugins/Android/MobileSupportStorage.androidlib/src/main/java/jp/co/cyberagent/unitysupport/storage/Storage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jp.co.cyberagent.unitysupport;
1+
package jp.co.cyberagent.unitysupport.storage;
22

33
import android.annotation.TargetApi;
44
import android.content.Context;

0 commit comments

Comments
 (0)