mobile/android/base/SysInfo.java.in

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial