-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathSystemUtils.java
More file actions
237 lines (199 loc) · 7.51 KB
/
SystemUtils.java
File metadata and controls
237 lines (199 loc) · 7.51 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package org.anddev.andengine.util;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Scanner;
import java.util.regex.MatchResult;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 15:50:31 - 14.07.2010
*/
public class SystemUtils {
// ===========================================================
// Constants
// ===========================================================
private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n";
private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n";
private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n";
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
private SystemUtils() {}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
public static int getPackageVersionCode(final Context pContext) {
return SystemUtils.getPackageInfo(pContext).versionCode;
}
public static String getPackageVersionName(final Context pContext) {
return SystemUtils.getPackageInfo(pContext).versionName;
}
private static PackageInfo getPackageInfo(final Context pContext) {
try {
return pContext.getPackageManager().getPackageInfo(pContext.getPackageName(), 0);
} catch (final NameNotFoundException e) {
Debug.e(e);
return null;
}
}
public static boolean hasSystemFeature(final Context pContext, final String pFeature) {
try {
final Method PackageManager_hasSystemFeatures = PackageManager.class.getMethod("hasSystemFeature", new Class[] { String.class });
return (PackageManager_hasSystemFeatures == null) ? false : (Boolean) PackageManager_hasSystemFeatures.invoke(pContext.getPackageManager(), pFeature);
} catch (final Throwable t) {
return false;
}
}
/**
* @param pBuildVersionCode taken from {@link Build.VERSION_CODES}.
*/
public static boolean isAndroidVersionOrHigher(final int pBuildVersionCode) {
return Integer.parseInt(Build.VERSION.SDK) >= pBuildVersionCode;
}
public static float getCPUBogoMips() throws SystemUtilsException {
final MatchResult matchResult = SystemUtils.matchSystemFile("/proc/cpuinfo", BOGOMIPS_PATTERN, 1000);
try {
if(matchResult.groupCount() > 0) {
return Float.parseFloat(matchResult.group(1));
} else {
throw new SystemUtilsException();
}
} catch (final NumberFormatException e) {
throw new SystemUtilsException(e);
}
}
/**
* @return in kiloBytes.
* @throws SystemUtilsException
*/
public static int getMemoryTotal() throws SystemUtilsException {
final MatchResult matchResult = SystemUtils.matchSystemFile("/proc/meminfo", MEMTOTAL_PATTERN, 1000);
try {
if(matchResult.groupCount() > 0) {
return Integer.parseInt(matchResult.group(1));
} else {
throw new SystemUtilsException();
}
} catch (final NumberFormatException e) {
throw new SystemUtilsException(e);
}
}
/**
* @return in kiloBytes.
* @throws SystemUtilsException
*/
public static int getMemoryFree() throws SystemUtilsException {
final MatchResult matchResult = SystemUtils.matchSystemFile("/proc/meminfo", MEMFREE_PATTERN, 1000);
try {
if(matchResult.groupCount() > 0) {
return Integer.parseInt(matchResult.group(1));
} else {
throw new SystemUtilsException();
}
} catch (final NumberFormatException e) {
throw new SystemUtilsException(e);
}
}
/**
* @return in kiloHertz.
* @throws SystemUtilsException
*/
public static int getCPUFrequencyCurrent() throws SystemUtilsException {
return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
}
/**
* @return in kiloHertz.
* @throws SystemUtilsException
*/
public static int getCPUFrequencyMin() throws SystemUtilsException {
return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq");
}
/**
* @return in kiloHertz.
* @throws SystemUtilsException
*/
public static int getCPUFrequencyMax() throws SystemUtilsException {
return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
}
/**
* @return in kiloHertz.
* @throws SystemUtilsException
*/
public static int getCPUFrequencyMinScaling() throws SystemUtilsException {
return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq");
}
/**
* @return in kiloHertz.
* @throws SystemUtilsException
*/
public static int getCPUFrequencyMaxScaling() throws SystemUtilsException {
return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
}
private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws SystemUtilsException {
InputStream in = null;
try {
final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();
in = process.getInputStream();
final Scanner scanner = new Scanner(in);
final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null;
if(matchFound) {
return scanner.match();
} else {
throw new SystemUtilsException();
}
} catch (final IOException e) {
throw new SystemUtilsException(e);
} finally {
StreamUtils.close(in);
}
}
private static int readSystemFileAsInt(final String pSystemFile) throws SystemUtilsException {
InputStream in = null;
try {
final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();
in = process.getInputStream();
final String content = StreamUtils.readFully(in);
return Integer.parseInt(content);
} catch (final IOException e) {
throw new SystemUtilsException(e);
} catch (final NumberFormatException e) {
throw new SystemUtilsException(e);
} finally {
StreamUtils.close(in);
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public static class SystemUtilsException extends Exception {
// ===========================================================
// Constants
// ===========================================================
private static final long serialVersionUID = -7256483361095147596L;
// ===========================================================
// Methods
// ===========================================================
public SystemUtilsException() {
}
public SystemUtilsException(final Throwable pThrowable) {
super(pThrowable);
}
}
}