406 lines
14 KiB
Java
406 lines
14 KiB
Java
package com.android.systemss.presenter;
|
||
|
||
import android.app.ActivityManager;
|
||
import android.app.usage.UsageStats;
|
||
import android.app.usage.UsageStatsManager;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.pm.ResolveInfo;
|
||
import android.os.Build;
|
||
import android.os.Environment;
|
||
import android.os.StatFs;
|
||
|
||
import java.io.BufferedReader;
|
||
import java.io.IOException;
|
||
import java.io.InputStreamReader;
|
||
import java.net.NetworkInterface;
|
||
import java.net.SocketException;
|
||
import java.text.DecimalFormat;
|
||
import java.util.List;
|
||
import java.util.SortedMap;
|
||
import java.util.TreeMap;
|
||
import java.util.UUID;
|
||
|
||
public class ArmlogicDeviceUtil {
|
||
|
||
public static String getDeviceId() {
|
||
// String uuid =null;
|
||
// TelephonyManager TelephonyMgr = (TelephonyManager) activity.getSystemService(TELEPHONY_SERVICE);
|
||
// if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED) {
|
||
// PermissionChecker.getInstance().requestReadPhoneState(activity);
|
||
// return uuid;
|
||
// }
|
||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||
// /* LogUtils.e("$$$$$$$$------Build.getSerial()="+Build.getSerial());
|
||
// LogUtils.e("$$$$$$$$------TelephonyMgr.getSimSerialNumber()="+TelephonyMgr.getSimSerialNumber());
|
||
// LogUtils.e("$$$$$$$$------TelephonyMgr.getImei()="+TelephonyMgr.getImei());
|
||
// LogUtils.e("$$$$$$$$------TelephonyMgr.getSubscriberId()="+TelephonyMgr.getSubscriberId());
|
||
// LogUtils.e("$$$$$$$$------TelephonyMgr.getLine1Number()="+TelephonyMgr.getLine1Number());
|
||
// LogUtils.e("$$$$$$$$------TelephonyMgr.getDeviceId()="+TelephonyMgr.getDeviceId());
|
||
// LogUtils.e("$$$$$$$$------UUID="+UUID.randomUUID().toString());*/
|
||
// uuid = Build.getSerial();//序列号
|
||
// if(isEmpty(uuid)){//IMEI:国际移动设备识别码
|
||
// uuid = TelephonyMgr.getImei();
|
||
// }
|
||
// if(isEmpty(uuid)){//IMEI:国际移动设备识别码
|
||
// uuid = TelephonyMgr.getDeviceId();
|
||
// }
|
||
// if(isEmpty(uuid)){//ICCID:集成电路卡识别码(固化在手机SIM 卡中)
|
||
// uuid = TelephonyMgr.getSimSerialNumber();
|
||
// }
|
||
// if(isEmpty(uuid)){//IMSI:国际移动用户识别码(sim卡运营商信息)
|
||
// uuid = TelephonyMgr.getSubscriberId();
|
||
// }
|
||
// if(isEmpty(uuid)){
|
||
// uuid = TelephonyMgr.getLine1Number();
|
||
// }
|
||
// //UUID.randomUUID().toString();
|
||
// }else {
|
||
// Class<?> c = null;
|
||
// try {
|
||
// c = Class.forName("android.os.SystemProperties");
|
||
// Method get = c.getMethod("get", String.class);
|
||
// uuid = (String) get.invoke(c, "ro.serialno");
|
||
// } catch (ClassNotFoundException e) {
|
||
// e.printStackTrace();
|
||
// } catch (NoSuchMethodException e) {
|
||
// e.printStackTrace();
|
||
// } catch (IllegalAccessException e) {
|
||
// e.printStackTrace();
|
||
// } catch (InvocationTargetException e) {
|
||
// e.printStackTrace();
|
||
// }
|
||
// }
|
||
// if (isEmpty(uuid)){
|
||
// UUID.randomUUID().toString();
|
||
// }
|
||
return "";
|
||
}
|
||
|
||
// public static String getSerialNumber(){
|
||
// String serial = null;
|
||
// try {
|
||
// if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1){//7.11+
|
||
// serial = Build.getSerial();
|
||
// LogUtils.e("===========7.11+=======================");
|
||
// }else{
|
||
// Class<?> c;
|
||
// c = Class.forName("android.os.SystemProperties");
|
||
// Method get = c.getMethod("get", String.class);
|
||
// serial = (String) get.invoke(c, "ro.serialno");
|
||
// LogUtils.e("===========7.11-======================");
|
||
// }
|
||
// } catch (ClassNotFoundException e) {
|
||
// // TODO Auto-generated catch block
|
||
// e.printStackTrace();
|
||
// } catch (NoSuchMethodException e) {
|
||
// // TODO Auto-generated catch block
|
||
// e.printStackTrace();
|
||
// } catch (SecurityException e) {
|
||
// // TODO Auto-generated catch block
|
||
// e.printStackTrace();
|
||
// } catch (IllegalAccessException e) {
|
||
// // TODO Auto-generated catch block
|
||
// e.printStackTrace();
|
||
// } catch (IllegalArgumentException e) {
|
||
// // TODO Auto-generated catch block
|
||
// e.printStackTrace();
|
||
// } catch (InvocationTargetException e) {
|
||
// // TODO Auto-generated catch block
|
||
// e.printStackTrace();
|
||
// }
|
||
// if (serial == null){
|
||
// //serial = "000000";
|
||
// }
|
||
// return serial;
|
||
// }
|
||
|
||
|
||
public static String getSerialNum(Context context) {
|
||
String serial = null;
|
||
// try {
|
||
// Class<?> c = Class.forName("android.os.SystemProperties");
|
||
// Method get = c.getMethod("get", String.class);
|
||
// serial = (String) get.invoke(c, "ro.serialno");
|
||
// } catch (Exception e) {
|
||
// }
|
||
|
||
|
||
// if (serial == null || serial.isEmpty() || serial.length() < 5) {
|
||
// serial = getWifiMacAddress(context);
|
||
// if (serial == null || serial.isEmpty()) serial = getBtMacAddress(context);
|
||
// if (serial == null || serial.isEmpty()) serial = "serial No. error ";
|
||
// }
|
||
// return serial;
|
||
return "BBBBBBBB";
|
||
}
|
||
|
||
public static boolean isDeviceExist(Context context, String mac) {
|
||
String deviceId = getEthernetMac();
|
||
if (deviceId.equals(mac)) {
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
public static String getEthernetMac() {
|
||
String ethernetMac = null;
|
||
try {
|
||
NetworkInterface NIC = NetworkInterface.getByName("eth0");
|
||
byte[] buf = NIC.getHardwareAddress();
|
||
ethernetMac = byteHexString(buf);
|
||
} catch (SocketException e) {
|
||
e.printStackTrace();
|
||
}
|
||
// return "EC:2E:CC:81:0D:62"; //for test
|
||
return ethernetMac;
|
||
}
|
||
|
||
|
||
/*
|
||
* 字节数组转16进制字符串
|
||
*/
|
||
public static String byteHexString(byte[] array) {
|
||
StringBuilder builder = new StringBuilder();
|
||
|
||
for(int i=0;i<array.length;i++){
|
||
String hex = Integer.toHexString(array[i] & 0xFF);
|
||
if (hex.length() == 1) {
|
||
hex = '0' + hex;
|
||
}
|
||
if(i<array.length-1){
|
||
builder.append(hex+":");
|
||
}else {
|
||
builder.append(hex);
|
||
}
|
||
|
||
}
|
||
|
||
return builder.toString().toUpperCase();
|
||
}
|
||
|
||
|
||
public static String getBrand(){
|
||
return Build.BRAND;
|
||
}
|
||
|
||
public static String getBuild(){
|
||
|
||
// return Build.FINGERPRINT;
|
||
return Build.DISPLAY; //47版本后改为display
|
||
}
|
||
|
||
public static String getDevice(){
|
||
|
||
return Build.DEVICE;
|
||
}
|
||
public static String getCpu() {
|
||
return getCPUinfo();
|
||
}
|
||
// public static String getDevicecpuId(Context context){
|
||
//
|
||
// String cpuInfo = SharedPreferencesUtil.getSharePrefrencesString(context,SharedPreferencesUtil.DEVICE_CPUID);
|
||
// if(cpuInfo==null||"".equals(cpuInfo)){
|
||
// cpuInfo = getCpu();
|
||
// }
|
||
//
|
||
// if(cpuInfo==null||"".equals(cpuInfo)){
|
||
// cpuInfo = createUUID();
|
||
// SharedPreferencesUtil.setSharePrefrencesString(context,SharedPreferencesUtil.DEVICE_CPUID,cpuInfo);
|
||
// }
|
||
// LogUtils.loge("CPUID==>"+cpuInfo);
|
||
// return cpuInfo;
|
||
// }
|
||
|
||
public static String getDdr(){
|
||
return "";
|
||
}
|
||
|
||
public static String getModel(){
|
||
return Build.MODEL;
|
||
}
|
||
|
||
public static String getPlatform(){
|
||
return "amlogic";
|
||
}
|
||
|
||
public static String getSystemVersion(){
|
||
return Build.VERSION.RELEASE;
|
||
}
|
||
|
||
private static String getCPUinfo() {
|
||
String cpuAddress = "";
|
||
String line = null;
|
||
String cmd = "cat /proc/cpuinfo";
|
||
try {
|
||
Process p = Runtime.getRuntime().exec(cmd);
|
||
// BufferedReader ie = new BufferedReader(new InputStreamReader(p.getErrorStream()));
|
||
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||
// String error = null;
|
||
// while ((error = ie.readLine()) != null && !error.equals("null")) {
|
||
// Log.d("===========", "========error="+error);
|
||
// }
|
||
while ((line = in.readLine()) != null && !line.equals("null")) {
|
||
if (line.contains("Serial")) {
|
||
String[] SerialStr = line.split(":");
|
||
if (SerialStr.length == 2) {
|
||
String mSerial = SerialStr[1];
|
||
cpuAddress = mSerial.trim();
|
||
return cpuAddress;
|
||
}
|
||
}
|
||
}
|
||
} catch (IOException ioe) {
|
||
ioe.printStackTrace();
|
||
}
|
||
return cpuAddress;
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取系统ram大小
|
||
* @return
|
||
*/
|
||
public static String getSysteTotalMemorySize(Context context){
|
||
//获得ActivityManager服务的对象
|
||
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
|
||
//获得MemoryInfo对象
|
||
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo() ;
|
||
//获得系统可用内存,保存在MemoryInfo对象上
|
||
mActivityManager.getMemoryInfo(memoryInfo) ;
|
||
long memSize = memoryInfo.totalMem ;
|
||
//字符类型转换
|
||
String availMemStr = formateFileSize(memSize,true);
|
||
return availMemStr ;
|
||
}
|
||
|
||
|
||
// /**
|
||
// * 获取系统rom大小
|
||
// * @return
|
||
// */
|
||
// @RequiresApi(api = Build.VERSION_CODES.O)
|
||
// public static String getSysteTotalStorageSize(Context context){
|
||
// //获得ActivityManager服务的对象
|
||
// String availMemStr = null;
|
||
// try {
|
||
// StorageStatsManager storageManager = (StorageStatsManager)context.getSystemService(Context.STORAGE_STATS_SERVICE);
|
||
// //字符类型转换
|
||
// availMemStr = formateFileSize( storageManager.getTotalBytes(StorageManager.UUID_DEFAULT),true);
|
||
// } catch (IOException e) {
|
||
// e.printStackTrace();
|
||
// }
|
||
// return availMemStr ;
|
||
// }
|
||
|
||
|
||
/**
|
||
* 获取系统rom大小
|
||
* @return
|
||
*/
|
||
public static String getSysteTotalStorageSize(Context context){
|
||
//获得ActivityManager服务的对象
|
||
String availMemStr = null;
|
||
try {
|
||
// final StorageManager storageManager = (StorageManager)context.getSystemService(Context.STORAGE_SERVICE);
|
||
// storageManager.getStorageVolumes()
|
||
|
||
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());
|
||
long totalSize = statFs.getTotalBytes();
|
||
//字符类型转换
|
||
availMemStr = formateFileSize( totalSize,true);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
return availMemStr ;
|
||
}
|
||
|
||
|
||
// public static String getSysteTotalStorageSize(Context context) {
|
||
// String dir = "/proc/meminfo";
|
||
// try {
|
||
// FileReader fr = new FileReader(dir);
|
||
// BufferedReader br = new BufferedReader(fr, 2048);
|
||
// String memoryLine = br.readLine();
|
||
// String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
|
||
// br.close();
|
||
// return formateFileSize(Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024l,true);
|
||
// } catch (Exception e) {
|
||
// e.printStackTrace();
|
||
// }
|
||
// return formateFileSize(0, true);
|
||
// }
|
||
|
||
public static String formateFileSize(long size, boolean isInteger) {
|
||
DecimalFormat df = isInteger ? new DecimalFormat("#0") : new DecimalFormat("#0.#");
|
||
String fileSizeString = "0M";
|
||
if (size < 1024 && size > 0) {
|
||
fileSizeString = df.format((double) size) + "B";
|
||
} else if (size < 1024 * 1024) {
|
||
fileSizeString = df.format((double) size / 1024) + "K";
|
||
} else if (size < 1024 * 1024 * 1024) {
|
||
fileSizeString = df.format((double) size / (1024 * 1024)) + "M";
|
||
} else {
|
||
fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "G";
|
||
}
|
||
return fileSizeString;
|
||
}
|
||
|
||
|
||
/***
|
||
* 获取本机默认的launcher
|
||
* @return
|
||
*/
|
||
public static String getDefaultLauncherPackageName(Context context) {
|
||
final UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
|
||
long time = System.currentTimeMillis();
|
||
List<UsageStats> appList = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*1000, time);
|
||
|
||
if (appList != null && appList.size() > 0) {
|
||
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
|
||
for (UsageStats usageStats : appList) {
|
||
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
|
||
}
|
||
if (mySortedMap != null && !mySortedMap.isEmpty()) {
|
||
return mySortedMap.get(mySortedMap.lastKey()).getPackageName();
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
public static String getLauncherPackageName(Context context) {
|
||
final Intent intent = new Intent(Intent.ACTION_MAIN);
|
||
intent.addCategory(Intent.CATEGORY_HOME);
|
||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||
|
||
final List<ResolveInfo> resList = context.getPackageManager().queryIntentActivities(intent, 0);
|
||
String tmp="";
|
||
for (ResolveInfo res:resList) {
|
||
if (res.activityInfo == null) {
|
||
continue;
|
||
}
|
||
if("com.android.settings".equals(res.activityInfo.packageName)){
|
||
continue;
|
||
}
|
||
|
||
if("com.android.tv.settings".equals(res.activityInfo.packageName)){
|
||
continue;
|
||
}
|
||
|
||
tmp+=res.activityInfo.packageName+",";
|
||
}
|
||
return "".equals(tmp)?null:tmp;
|
||
}
|
||
|
||
|
||
private static String createUUID(){
|
||
String timems =System.currentTimeMillis()+"";
|
||
String tmp = timems+"-"+ UUID.randomUUID().toString();
|
||
return tmp.replace("-","").substring(0,31);
|
||
}
|
||
}
|