michael@0: #filter substitution michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.util.HardwareUtils; michael@0: michael@0: import android.os.StrictMode; michael@0: import android.util.Log; michael@0: michael@0: import java.io.File; michael@0: import java.io.FileFilter; michael@0: michael@0: import java.util.regex.Pattern; michael@0: michael@0: /** michael@0: * A collection of system info values, broadly mirroring a subset of michael@0: * nsSystemInfo. See also the constants in AppConstants, which reflect michael@0: * much of nsIXULAppInfo. michael@0: */ michael@0: public final class SysInfo { michael@0: private static final String LOG_TAG = "GeckoSysInfo"; michael@0: michael@0: // We don't mind an instant of possible duplicate work, we only wish to michael@0: // avoid inconsistency, so we don't bother with synchronization for michael@0: // these. michael@0: private static volatile int cpuCount = -1; michael@0: michael@0: /** michael@0: * Get the number of cores on the device. michael@0: * michael@0: * We can't use a nice tidy API call, because they're all wrong: michael@0: * michael@0: * michael@0: * michael@0: * This method is based on that code. michael@0: * michael@0: * @return the number of CPU cores, or 1 if the number could not be michael@0: * determined. michael@0: */ michael@0: public static int getCPUCount() { michael@0: if (cpuCount > 0) { michael@0: return cpuCount; michael@0: } michael@0: michael@0: if (android.os.Build.VERSION.SDK_INT < 9) { michael@0: return readCPUCount(); michael@0: } michael@0: michael@0: // Avoid a strict mode warning... but only on devices that have StrictMode. michael@0: StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); michael@0: try { michael@0: return readCPUCount(); michael@0: } finally { michael@0: StrictMode.setThreadPolicy(savedPolicy); michael@0: } michael@0: } michael@0: michael@0: private static int readCPUCount() { michael@0: class CpuFilter implements FileFilter { michael@0: @Override michael@0: public boolean accept(File pathname) { michael@0: return Pattern.matches("cpu[0-9]+", pathname.getName()); michael@0: } michael@0: } michael@0: try { michael@0: final File dir = new File("/sys/devices/system/cpu/"); michael@0: return cpuCount = dir.listFiles(new CpuFilter()).length; michael@0: } catch (Exception e) { michael@0: Log.w(LOG_TAG, "Assuming 1 CPU; got exception.", e); michael@0: return cpuCount = 1; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Wraps HardwareUtils so callers don't need to know about it. michael@0: */ michael@0: public static int getMemSize() { michael@0: return HardwareUtils.getMemSize(); michael@0: } michael@0: michael@0: /** michael@0: * @return the SDK version supported by this device, such as '16'. michael@0: */ michael@0: public static int getVersion() { michael@0: return android.os.Build.VERSION.SDK_INT; michael@0: } michael@0: michael@0: /** michael@0: * @return the release version string, such as "4.1.2". michael@0: */ michael@0: public static String getReleaseVersion() { michael@0: return android.os.Build.VERSION.RELEASE; michael@0: } michael@0: michael@0: /** michael@0: * @return the kernel version string, such as "3.4.10-geb45596". michael@0: */ michael@0: public static String getKernelVersion() { michael@0: return System.getProperty("os.version", ""); michael@0: } michael@0: michael@0: /** michael@0: * @return the device manufacturer, such as "HTC". michael@0: */ michael@0: public static String getManufacturer() { michael@0: return android.os.Build.MANUFACTURER; michael@0: } michael@0: michael@0: /** michael@0: * @return the device name, such as "HTC One". michael@0: */ michael@0: public static String getDevice() { michael@0: // No, not android.os.Build.DEVICE. michael@0: return android.os.Build.MODEL; michael@0: } michael@0: michael@0: /** michael@0: * @return the Android "hardware" identifier, such as "m7". michael@0: */ michael@0: public static String getHardware() { michael@0: return android.os.Build.HARDWARE; michael@0: } michael@0: michael@0: /** michael@0: * @return the system OS name. Hardcoded to "Android". michael@0: */ michael@0: public static String getName() { michael@0: // We deliberately differ from PR_SI_SYSNAME, which is "Linux". michael@0: return "Android"; michael@0: } michael@0: michael@0: /** michael@0: * @return the architecture string, excluding ABI. michael@0: */ michael@0: public static String getArch() { michael@0: return "@CPU_ARCH@"; // "arm" michael@0: } michael@0: michael@0: /** michael@0: * @return the Android architecture string, including ABI. michael@0: */ michael@0: public static String getArchABI() { michael@0: // Android likes to include the ABI, too ("armeabiv7"), so we michael@0: // differ to add value. michael@0: return android.os.Build.CPU_ABI; michael@0: } michael@0: michael@0: /** michael@0: * @return the default system locale, such as "en-US" michael@0: */ michael@0: public static String getLocale() { michael@0: return java.util.Locale.getDefault().toString().replace('_', '-'); michael@0: } michael@0: }