1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/SysInfo.java.in Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 +#filter substitution 1.5 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +package org.mozilla.gecko; 1.11 + 1.12 +import org.mozilla.gecko.util.HardwareUtils; 1.13 + 1.14 +import android.os.StrictMode; 1.15 +import android.util.Log; 1.16 + 1.17 +import java.io.File; 1.18 +import java.io.FileFilter; 1.19 + 1.20 +import java.util.regex.Pattern; 1.21 + 1.22 +/** 1.23 + * A collection of system info values, broadly mirroring a subset of 1.24 + * nsSystemInfo. See also the constants in AppConstants, which reflect 1.25 + * much of nsIXULAppInfo. 1.26 + */ 1.27 +public final class SysInfo { 1.28 + private static final String LOG_TAG = "GeckoSysInfo"; 1.29 + 1.30 + // We don't mind an instant of possible duplicate work, we only wish to 1.31 + // avoid inconsistency, so we don't bother with synchronization for 1.32 + // these. 1.33 + private static volatile int cpuCount = -1; 1.34 + 1.35 + /** 1.36 + * Get the number of cores on the device. 1.37 + * 1.38 + * We can't use a nice tidy API call, because they're all wrong: 1.39 + * 1.40 + * <http://stackoverflow.com/questions/7962155/how-can-you-detect-a-dual-core- 1.41 + * cpu-on-an-android-device-from-code> 1.42 + * 1.43 + * This method is based on that code. 1.44 + * 1.45 + * @return the number of CPU cores, or 1 if the number could not be 1.46 + * determined. 1.47 + */ 1.48 + public static int getCPUCount() { 1.49 + if (cpuCount > 0) { 1.50 + return cpuCount; 1.51 + } 1.52 + 1.53 + if (android.os.Build.VERSION.SDK_INT < 9) { 1.54 + return readCPUCount(); 1.55 + } 1.56 + 1.57 + // Avoid a strict mode warning... but only on devices that have StrictMode. 1.58 + StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); 1.59 + try { 1.60 + return readCPUCount(); 1.61 + } finally { 1.62 + StrictMode.setThreadPolicy(savedPolicy); 1.63 + } 1.64 + } 1.65 + 1.66 + private static int readCPUCount() { 1.67 + class CpuFilter implements FileFilter { 1.68 + @Override 1.69 + public boolean accept(File pathname) { 1.70 + return Pattern.matches("cpu[0-9]+", pathname.getName()); 1.71 + } 1.72 + } 1.73 + try { 1.74 + final File dir = new File("/sys/devices/system/cpu/"); 1.75 + return cpuCount = dir.listFiles(new CpuFilter()).length; 1.76 + } catch (Exception e) { 1.77 + Log.w(LOG_TAG, "Assuming 1 CPU; got exception.", e); 1.78 + return cpuCount = 1; 1.79 + } 1.80 + } 1.81 + 1.82 + /** 1.83 + * Wraps HardwareUtils so callers don't need to know about it. 1.84 + */ 1.85 + public static int getMemSize() { 1.86 + return HardwareUtils.getMemSize(); 1.87 + } 1.88 + 1.89 + /** 1.90 + * @return the SDK version supported by this device, such as '16'. 1.91 + */ 1.92 + public static int getVersion() { 1.93 + return android.os.Build.VERSION.SDK_INT; 1.94 + } 1.95 + 1.96 + /** 1.97 + * @return the release version string, such as "4.1.2". 1.98 + */ 1.99 + public static String getReleaseVersion() { 1.100 + return android.os.Build.VERSION.RELEASE; 1.101 + } 1.102 + 1.103 + /** 1.104 + * @return the kernel version string, such as "3.4.10-geb45596". 1.105 + */ 1.106 + public static String getKernelVersion() { 1.107 + return System.getProperty("os.version", ""); 1.108 + } 1.109 + 1.110 + /** 1.111 + * @return the device manufacturer, such as "HTC". 1.112 + */ 1.113 + public static String getManufacturer() { 1.114 + return android.os.Build.MANUFACTURER; 1.115 + } 1.116 + 1.117 + /** 1.118 + * @return the device name, such as "HTC One". 1.119 + */ 1.120 + public static String getDevice() { 1.121 + // No, not android.os.Build.DEVICE. 1.122 + return android.os.Build.MODEL; 1.123 + } 1.124 + 1.125 + /** 1.126 + * @return the Android "hardware" identifier, such as "m7". 1.127 + */ 1.128 + public static String getHardware() { 1.129 + return android.os.Build.HARDWARE; 1.130 + } 1.131 + 1.132 + /** 1.133 + * @return the system OS name. Hardcoded to "Android". 1.134 + */ 1.135 + public static String getName() { 1.136 + // We deliberately differ from PR_SI_SYSNAME, which is "Linux". 1.137 + return "Android"; 1.138 + } 1.139 + 1.140 + /** 1.141 + * @return the architecture string, excluding ABI. 1.142 + */ 1.143 + public static String getArch() { 1.144 + return "@CPU_ARCH@"; // "arm" 1.145 + } 1.146 + 1.147 + /** 1.148 + * @return the Android architecture string, including ABI. 1.149 + */ 1.150 + public static String getArchABI() { 1.151 + // Android likes to include the ABI, too ("armeabiv7"), so we 1.152 + // differ to add value. 1.153 + return android.os.Build.CPU_ABI; 1.154 + } 1.155 + 1.156 + /** 1.157 + * @return the default system locale, such as "en-US" 1.158 + */ 1.159 + public static String getLocale() { 1.160 + return java.util.Locale.getDefault().toString().replace('_', '-'); 1.161 + } 1.162 +}