This repository has been archived by the owner on Oct 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 371
/
Temperature.java
executable file
·205 lines (173 loc) · 6.16 KB
/
Temperature.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (C) 2015-2016 Willi Ye <williye97@gmail.com>
*
* This file is part of Kernel Adiutor.
*
* Kernel Adiutor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Kernel Adiutor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kernel Adiutor. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.grarak.kerneladiutor.utils.kernel.cpu;
import android.content.Context;
import com.grarak.kerneladiutor.utils.Log;
import com.grarak.kerneladiutor.R;
import com.grarak.kerneladiutor.utils.Device;
import com.grarak.kerneladiutor.utils.Utils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
/**
* Created by willi on 21.04.16.
*/
public class Temperature {
private static Temperature sInstance;
public static Temperature getInstance(Context context) {
if (sInstance == null) {
sInstance = new Temperature(context);
}
return sInstance;
}
private static final HashMap<String, Integer> sCPUTemps = new HashMap<>();
private static final String THERMAL_ZONE0 = "/sys/class/thermal/thermal_zone0/temp";
static {
sCPUTemps.put("/sys/devices/platform/omap/omap_temp_sensor.0/temperature", 1000);
sCPUTemps.put("/proc/mtktscpu/mtktscpu_temperature", 1000);
}
private TempJson TEMP_JSON;
private String CPU_NODE;
private int CPU_OFFSET;
private String GPU_NODE;
private int GPU_OFFSET;
private Temperature(Context context) {
TEMP_JSON = new TempJson(context);
if (!TEMP_JSON.supported()) {
TEMP_JSON = null;
}
}
public String getGPU(Context context) {
double temp = getGPUTemp();
boolean useFahrenheit = Utils.useFahrenheit(context);
if (useFahrenheit) temp = Utils.celsiusToFahrenheit(temp);
return Utils.roundTo2Decimals(temp) + context.getString(useFahrenheit ? R.string.fahrenheit
: R.string.celsius);
}
private double getGPUTemp() {
return (double) Utils.strToInt(Utils.readFile(GPU_NODE)) / GPU_OFFSET;
}
public boolean hasGPU() {
if (TEMP_JSON != null && TEMP_JSON.getGPU() != null) {
GPU_NODE = TEMP_JSON.getGPU();
if (Utils.existFile(GPU_NODE)) {
GPU_OFFSET = TEMP_JSON.getGPUOffset();
if (GPU_OFFSET != 1 && Utils.readFile(GPU_NODE).length() == 2) {
GPU_OFFSET = 1;
}
return true;
}
GPU_NODE = null;
}
return false;
}
public String getCPU(Context context) {
double temp = getCPUTemp();
boolean useFahrenheit = Utils.useFahrenheit(context);
if (useFahrenheit) temp = Utils.celsiusToFahrenheit(temp);
return Utils.roundTo2Decimals(temp) + context.getString(useFahrenheit ? R.string.fahrenheit
: R.string.celsius);
}
private double getCPUTemp() {
return (double) Utils.strToInt(Utils.readFile(CPU_NODE)) / CPU_OFFSET;
}
public boolean hasCPU() {
if (TEMP_JSON != null && TEMP_JSON.getCPU() != null) {
CPU_NODE = TEMP_JSON.getCPU();
if (Utils.existFile(CPU_NODE)) {
CPU_OFFSET = TEMP_JSON.getCPUOffset();
if (CPU_OFFSET != 1 && Utils.readFile(CPU_NODE).length() == 2) {
CPU_OFFSET = 1;
}
return true;
}
CPU_NODE = null;
}
if (CPU_NODE != null) return true;
for (String node : sCPUTemps.keySet()) {
if (Utils.existFile(node)) {
CPU_NODE = node;
CPU_OFFSET = sCPUTemps.get(CPU_NODE);
return true;
}
}
if (CPU_NODE == null && Utils.existFile(THERMAL_ZONE0)) {
CPU_NODE = THERMAL_ZONE0;
CPU_OFFSET = 1000;
}
if (CPU_NODE != null && Utils.readFile(CPU_NODE).length() == 2) {
CPU_OFFSET = 1;
}
return CPU_NODE != null;
}
public boolean supported() {
return hasCPU() || hasGPU();
}
private class TempJson {
private final String TAG = TempJson.class.getSimpleName();
private JSONObject mDeviceJson;
private TempJson(Context context) {
try {
JSONArray tempArray = new JSONArray(Utils.readAssetFile(context, "temp.json"));
for (int i = 0; i < tempArray.length(); i++) {
JSONObject device = tempArray.getJSONObject(i);
if (Device.getBoard().equalsIgnoreCase(device.getString("board"))) {
mDeviceJson = device;
break;
}
}
} catch (JSONException ignored) {
Log.e(TAG, "Can't read temp.json");
}
}
private int getGPUOffset() {
try {
return mDeviceJson.getInt("gpu-offset");
} catch (JSONException ignored) {
return 1;
}
}
private int getCPUOffset() {
try {
return mDeviceJson.getInt("cpu-offset");
} catch (JSONException ignored) {
return 1;
}
}
public String getGPU() {
try {
return mDeviceJson.getString("gpu");
} catch (JSONException ignored) {
return null;
}
}
public String getCPU() {
try {
return mDeviceJson.getString("cpu");
} catch (JSONException ignored) {
return null;
}
}
public boolean supported() {
return mDeviceJson != null;
}
}
}