|
1 #filter substitution |
|
2 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 package org.mozilla.gecko; |
|
8 |
|
9 import org.mozilla.gecko.util.HardwareUtils; |
|
10 |
|
11 import android.os.StrictMode; |
|
12 import android.util.Log; |
|
13 |
|
14 import java.io.File; |
|
15 import java.io.FileFilter; |
|
16 |
|
17 import java.util.regex.Pattern; |
|
18 |
|
19 /** |
|
20 * A collection of system info values, broadly mirroring a subset of |
|
21 * nsSystemInfo. See also the constants in AppConstants, which reflect |
|
22 * much of nsIXULAppInfo. |
|
23 */ |
|
24 public final class SysInfo { |
|
25 private static final String LOG_TAG = "GeckoSysInfo"; |
|
26 |
|
27 // We don't mind an instant of possible duplicate work, we only wish to |
|
28 // avoid inconsistency, so we don't bother with synchronization for |
|
29 // these. |
|
30 private static volatile int cpuCount = -1; |
|
31 |
|
32 /** |
|
33 * Get the number of cores on the device. |
|
34 * |
|
35 * We can't use a nice tidy API call, because they're all wrong: |
|
36 * |
|
37 * <http://stackoverflow.com/questions/7962155/how-can-you-detect-a-dual-core- |
|
38 * cpu-on-an-android-device-from-code> |
|
39 * |
|
40 * This method is based on that code. |
|
41 * |
|
42 * @return the number of CPU cores, or 1 if the number could not be |
|
43 * determined. |
|
44 */ |
|
45 public static int getCPUCount() { |
|
46 if (cpuCount > 0) { |
|
47 return cpuCount; |
|
48 } |
|
49 |
|
50 if (android.os.Build.VERSION.SDK_INT < 9) { |
|
51 return readCPUCount(); |
|
52 } |
|
53 |
|
54 // Avoid a strict mode warning... but only on devices that have StrictMode. |
|
55 StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
|
56 try { |
|
57 return readCPUCount(); |
|
58 } finally { |
|
59 StrictMode.setThreadPolicy(savedPolicy); |
|
60 } |
|
61 } |
|
62 |
|
63 private static int readCPUCount() { |
|
64 class CpuFilter implements FileFilter { |
|
65 @Override |
|
66 public boolean accept(File pathname) { |
|
67 return Pattern.matches("cpu[0-9]+", pathname.getName()); |
|
68 } |
|
69 } |
|
70 try { |
|
71 final File dir = new File("/sys/devices/system/cpu/"); |
|
72 return cpuCount = dir.listFiles(new CpuFilter()).length; |
|
73 } catch (Exception e) { |
|
74 Log.w(LOG_TAG, "Assuming 1 CPU; got exception.", e); |
|
75 return cpuCount = 1; |
|
76 } |
|
77 } |
|
78 |
|
79 /** |
|
80 * Wraps HardwareUtils so callers don't need to know about it. |
|
81 */ |
|
82 public static int getMemSize() { |
|
83 return HardwareUtils.getMemSize(); |
|
84 } |
|
85 |
|
86 /** |
|
87 * @return the SDK version supported by this device, such as '16'. |
|
88 */ |
|
89 public static int getVersion() { |
|
90 return android.os.Build.VERSION.SDK_INT; |
|
91 } |
|
92 |
|
93 /** |
|
94 * @return the release version string, such as "4.1.2". |
|
95 */ |
|
96 public static String getReleaseVersion() { |
|
97 return android.os.Build.VERSION.RELEASE; |
|
98 } |
|
99 |
|
100 /** |
|
101 * @return the kernel version string, such as "3.4.10-geb45596". |
|
102 */ |
|
103 public static String getKernelVersion() { |
|
104 return System.getProperty("os.version", ""); |
|
105 } |
|
106 |
|
107 /** |
|
108 * @return the device manufacturer, such as "HTC". |
|
109 */ |
|
110 public static String getManufacturer() { |
|
111 return android.os.Build.MANUFACTURER; |
|
112 } |
|
113 |
|
114 /** |
|
115 * @return the device name, such as "HTC One". |
|
116 */ |
|
117 public static String getDevice() { |
|
118 // No, not android.os.Build.DEVICE. |
|
119 return android.os.Build.MODEL; |
|
120 } |
|
121 |
|
122 /** |
|
123 * @return the Android "hardware" identifier, such as "m7". |
|
124 */ |
|
125 public static String getHardware() { |
|
126 return android.os.Build.HARDWARE; |
|
127 } |
|
128 |
|
129 /** |
|
130 * @return the system OS name. Hardcoded to "Android". |
|
131 */ |
|
132 public static String getName() { |
|
133 // We deliberately differ from PR_SI_SYSNAME, which is "Linux". |
|
134 return "Android"; |
|
135 } |
|
136 |
|
137 /** |
|
138 * @return the architecture string, excluding ABI. |
|
139 */ |
|
140 public static String getArch() { |
|
141 return "@CPU_ARCH@"; // "arm" |
|
142 } |
|
143 |
|
144 /** |
|
145 * @return the Android architecture string, including ABI. |
|
146 */ |
|
147 public static String getArchABI() { |
|
148 // Android likes to include the ABI, too ("armeabiv7"), so we |
|
149 // differ to add value. |
|
150 return android.os.Build.CPU_ABI; |
|
151 } |
|
152 |
|
153 /** |
|
154 * @return the default system locale, such as "en-US" |
|
155 */ |
|
156 public static String getLocale() { |
|
157 return java.util.Locale.getDefault().toString().replace('_', '-'); |
|
158 } |
|
159 } |