package com.wasu.widgets.tools; import android.app.ActivityManager; import android.content.Context; import android.graphics.Point; import android.os.Build; import android.os.Environment; import android.os.StatFs; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.WindowManager; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.util.List; public class DeviceUtils { private final static String TAG = "DeviceUtils"; private final static String kCpuInfoMaxFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; public static int getMaxCpuFreq() { int result = 0; FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(kCpuInfoMaxFreqFilePath); br = new BufferedReader(fr); String text = br.readLine(); result = Integer.parseInt(text.trim()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null) try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (br != null) try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } /** * 获取进程name * @param cxt * @param pid * @return */ public static String getProcessName(Context cxt, int pid) { ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE); List runningApps = am.getRunningAppProcesses(); if (runningApps == null) { return null; } for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) { if (procInfo.pid == pid) { return procInfo.processName; } } return null; } private final static String kCpuInfoMinFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"; /* 获取CPU最小频率(单位KHZ) */ public static int getMinCpuFreq() { int result = 0; FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(kCpuInfoMinFreqFilePath); br = new BufferedReader(fr); String text = br.readLine(); result = Integer.parseInt(text.trim()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null) try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (br != null) try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } private final static String kCpuInfoCurFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"; /* 实时获取CPU当前频率(单位KHZ) */ public static int getCurCpuFreq() { int result = 0; FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(kCpuInfoCurFreqFilePath); br = new BufferedReader(fr); String text = br.readLine(); result = Integer.parseInt(text.trim()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null) try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (br != null) try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } public static String getDns() { String dns = ""; try { Process process = Runtime.getRuntime().exec("getprop net.dns1"); InputStream stdin = process.getInputStream(); byte[] re = new byte[128]; while (stdin.read(re) != -1) { dns = dns + new String(re); } dns = dns.trim(); } catch (Exception e) { e.printStackTrace(); } return dns; } public static String getCurCpuFreqAsString() { String result = ""; ProcessBuilder cmd; try { String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq" }; cmd = new ProcessBuilder(args); Process process = cmd.start(); InputStream in = process.getInputStream(); byte[] re = new byte[24]; while (in.read(re) != -1) { result = result + new String(re); } in.close(); } catch (IOException ex) { ex.printStackTrace(); } return result.trim(); } public static String getMinCpuFreqAsString() { String result = ""; ProcessBuilder cmd; try { String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" }; cmd = new ProcessBuilder(args); Process process = cmd.start(); InputStream in = process.getInputStream(); byte[] re = new byte[24]; while (in.read(re) != -1) { result = result + new String(re); } in.close(); } catch (IOException ex) { ex.printStackTrace(); } return result.trim(); } /* 获取CPU名字 */ public static String getCpuName() { FileReader fr = null; BufferedReader br = null; try { fr = new FileReader("/proc/cpuinfo"); br = new BufferedReader(fr); String text = br.readLine(); String[] array = text.split(":\\s+", 2); for (int i = 0; i < array.length; i++) { } return array[1]; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null) try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (br != null) try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } // 获取内存大小 public static long getTotalMemory() { long mTotal = 0; String path = "/proc/meminfo"; String content = null; BufferedReader br = null; try { br = new BufferedReader(new FileReader(path), 8); String line; if ((line = br.readLine()) != null) { content = line; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } int begin = content.indexOf(':'); int end = content.indexOf('k'); if(begin >= 0 && begin < end) { content = content.substring(begin + 1, end).trim(); mTotal = Integer.parseInt(content); } return mTotal; } // 获取Rom信息 public static long[] getRomMemroy() { long[] romInfo = new long[2]; // Total rom memory romInfo[0] = getTotalInternalMemorySize(); // Available rom memory File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); romInfo[1] = blockSize * availableBlocks; getVersion(); return romInfo; } public static long getTotalInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } // 获取SD卡大小 public static long[] getSDCardMemory() { long[] sdCardInfo = new long[2]; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { File sdcardDir = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(sdcardDir.getPath()); long bSize = sf.getBlockSize(); long bCount = sf.getBlockCount(); long availBlocks = sf.getAvailableBlocks(); sdCardInfo[0] = bSize * bCount;// 总大小 sdCardInfo[1] = bSize * availBlocks;// 可用大小 } return sdCardInfo; } public static String[] getVersion() { String[] version = { "null", "null", "null", "null" }; String str1 = "/proc/version"; String str2; String[] arrayOfString; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader( localFileReader, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); version[0] = arrayOfString[2];// KernelVersion localBufferedReader.close(); } catch (IOException e) { } version[1] = Build.VERSION.RELEASE;// firmware version version[2] = Build.MODEL;// model version[3] = Build.DISPLAY;// system version return version; } public static int getCpuCount() { int cpuCount = Runtime.getRuntime().availableProcessors(); return cpuCount; } public static float getPixelFromDip(Context context, float dpi) { DisplayMetrics mDisplayMetrics = context.getResources() .getDisplayMetrics(); return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpi, mDisplayMetrics); } public static Point getScreenSize(Context context) { WindowManager windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); Point outSize = new Point(); windowManager.getDefaultDisplay().getSize(outSize); return outSize; } public static String getDeviceRatio(Context context) { Point outSize = getScreenSize(context); return outSize.x + "*" + outSize.y; } public static float Default_W = 1280.0f; public static float Default_H = 720.0f; public static String[] screenAdapter(String[] s, Context context) { Point size = DeviceUtils.getScreenSize(context); int newWidth = (int) (Integer.parseInt(s[0]) * size.x / Default_W); int newHeight = (int) (Integer.parseInt(s[1]) * size.y / Default_H); String[] res = new String[] { "" + newWidth, "" + newHeight }; return res; } public static float getAdapterWidth(Context context, int width) { float res = width; Point size = DeviceUtils.getScreenSize(context); res = width * (size.x / Default_W); return res; } public static float getAdapterHeight(Context context, int height) { float res = height; Point size = DeviceUtils.getScreenSize(context); res = height * size.y / Default_H; return res; } }