270 lines
10 KiB
Java
270 lines
10 KiB
Java
package com.gh.common.util;
|
|
|
|
import android.app.ActivityManager;
|
|
import android.content.Context;
|
|
import android.net.ConnectivityManager;
|
|
import android.net.NetworkInfo;
|
|
import android.net.wifi.WifiManager;
|
|
import android.os.Build;
|
|
import android.provider.Settings;
|
|
import android.telephony.TelephonyManager;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import com.gh.gamecenter.kuaichuan.WifiMgr;
|
|
import com.lightgame.utils.Util_System_Phone_State;
|
|
import com.tencent.stat.StatConfig;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.io.LineNumberReader;
|
|
import java.io.Reader;
|
|
import java.net.Inet4Address;
|
|
import java.net.InetAddress;
|
|
import java.net.NetworkInterface;
|
|
import java.net.SocketException;
|
|
import java.util.Enumeration;
|
|
|
|
import static android.os.Build.MANUFACTURER;
|
|
import static android.os.Build.MODEL;
|
|
|
|
/**
|
|
* Created by khy on 2/08/17.
|
|
*/
|
|
|
|
public class DeviceUtils {
|
|
|
|
public static JSONObject getLoginDevice(Context context) throws JSONException { // device数据
|
|
context = context.getApplicationContext();
|
|
JSONObject object = new JSONObject();
|
|
object.put("os", "Android");
|
|
object.put("imei", Util_System_Phone_State.getDeviceId(context));
|
|
object.put("mac", getMac(context));
|
|
object.put("model", MODEL);
|
|
object.put("manufacturer", MANUFACTURER);
|
|
object.put("android_id", Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID));
|
|
object.put("android_sdk", Build.VERSION.SDK_INT);
|
|
object.put("android_version", android.os.Build.VERSION.RELEASE);
|
|
object.put("ip", getIPAddress(context));
|
|
object.put("network", getNetwork(context));
|
|
return object;
|
|
}
|
|
|
|
|
|
public static JSONObject getUserDevice(Context context) { // 判断新老用户device数据
|
|
JSONObject object = new JSONObject();
|
|
try {
|
|
object.put("IMEI", Util_System_Phone_State.getDeviceId(context));
|
|
object.put("ANDROID_ID", Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID));
|
|
object.put("MAC", getMac(context));
|
|
object.put("MTA_ID", StatConfig.getMid(context));
|
|
object.put("MANUFACTURER", MANUFACTURER);
|
|
object.put("MODEL", MODEL);
|
|
object.put("ANDROID_SDK", Build.VERSION.SDK_INT);
|
|
object.put("ANDROID_VERSION", android.os.Build.VERSION.RELEASE);
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return object;
|
|
}
|
|
|
|
public static String getMac(Context context) {
|
|
String str = "";
|
|
String macSerial = "";
|
|
try {
|
|
Process pp = Runtime.getRuntime().exec(
|
|
"cat /sys/class/net/wlan0/address ");
|
|
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
|
|
LineNumberReader input = new LineNumberReader(ir);
|
|
|
|
while (null != str) {
|
|
str = input.readLine();
|
|
if (str != null) {
|
|
macSerial = str.trim();// 去空格
|
|
break;
|
|
}
|
|
}
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
if ("".equals(macSerial)) {
|
|
try {
|
|
return loadFileAsString("/sys/class/net/eth0/address")
|
|
.toUpperCase().substring(0, 17);
|
|
} catch (FileNotFoundException e) {
|
|
// do nothing
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
if (TextUtils.isEmpty(macSerial)) { // 备用方案
|
|
macSerial = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getMacAddress();
|
|
}
|
|
return macSerial;
|
|
}
|
|
|
|
private static String loadFileAsString(String fileName) throws Exception {
|
|
FileReader reader = new FileReader(fileName);
|
|
String text = loadReaderAsString(reader);
|
|
reader.close();
|
|
return text;
|
|
}
|
|
|
|
private static String loadReaderAsString(Reader reader) throws Exception {
|
|
StringBuilder builder = new StringBuilder();
|
|
char[] buffer = new char[4096];
|
|
int readLength = reader.read(buffer);
|
|
while (readLength >= 0) {
|
|
builder.append(buffer, 0, readLength);
|
|
readLength = reader.read(buffer);
|
|
}
|
|
return builder.toString();
|
|
}
|
|
|
|
public static String getUserAgent() {
|
|
String userAgent = "";
|
|
userAgent = System.getProperty("http.agent");
|
|
StringBuffer sb = new StringBuffer();
|
|
for (int i = 0, length = userAgent.length(); i < length; i++) {
|
|
char c = userAgent.charAt(i);
|
|
if (c <= '\u001f' || c >= '\u007f') {
|
|
sb.append(String.format("\\u%04x", (int) c));
|
|
} else {
|
|
sb.append(c);
|
|
}
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
public static String getIPAddress(Context context) {
|
|
NetworkInfo info = ((ConnectivityManager) context
|
|
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
|
|
if (info != null && info.isConnected()) {
|
|
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
|
|
try {
|
|
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
|
|
NetworkInterface intf = en.nextElement();
|
|
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
|
|
InetAddress inetAddress = enumIpAddr.nextElement();
|
|
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
|
|
return inetAddress.getHostAddress();
|
|
}
|
|
}
|
|
}
|
|
} catch (SocketException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
|
|
return WifiMgr.getInstance(context).getCurrentIpAddress();
|
|
}
|
|
} else {
|
|
//当前无网络连接,请在设置中打开网络
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String getNetwork(Context context) {
|
|
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
if (connManager == null) return null;
|
|
NetworkInfo info = connManager.getActiveNetworkInfo();
|
|
if (info != null && info.isConnected()) {
|
|
int typeMobile = info.getType();
|
|
if (typeMobile == ConnectivityManager.TYPE_WIFI) {
|
|
return "WIFI";
|
|
} else if (typeMobile == ConnectivityManager.TYPE_MOBILE) {
|
|
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
if (telephonyManager != null) {
|
|
int networkType = telephonyManager.getNetworkType();
|
|
String status;
|
|
switch (networkType) {
|
|
case TelephonyManager.NETWORK_TYPE_GPRS:
|
|
case TelephonyManager.NETWORK_TYPE_EDGE:
|
|
case TelephonyManager.NETWORK_TYPE_CDMA:
|
|
case TelephonyManager.NETWORK_TYPE_1xRTT:
|
|
case TelephonyManager.NETWORK_TYPE_IDEN:
|
|
status = "2G";
|
|
break;
|
|
case TelephonyManager.NETWORK_TYPE_UMTS:
|
|
case TelephonyManager.NETWORK_TYPE_EVDO_0:
|
|
case TelephonyManager.NETWORK_TYPE_EVDO_A:
|
|
case TelephonyManager.NETWORK_TYPE_HSDPA:
|
|
case TelephonyManager.NETWORK_TYPE_HSUPA:
|
|
case TelephonyManager.NETWORK_TYPE_HSPA:
|
|
case TelephonyManager.NETWORK_TYPE_EVDO_B:
|
|
case TelephonyManager.NETWORK_TYPE_EHRPD:
|
|
case TelephonyManager.NETWORK_TYPE_HSPAP:
|
|
status = "3G";
|
|
break;
|
|
case TelephonyManager.NETWORK_TYPE_LTE:
|
|
status = "4G";
|
|
break;
|
|
default:
|
|
status = "未知";
|
|
break;
|
|
}
|
|
return status;
|
|
}
|
|
}
|
|
}
|
|
|
|
return "无网络";
|
|
}
|
|
|
|
|
|
// get sim
|
|
public static String getSim(Context context) {
|
|
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
String imsi = tm.getSubscriberId();
|
|
if (imsi == null) {
|
|
return "";
|
|
}
|
|
if (imsi.startsWith("46000") || imsi.startsWith("46002") || imsi.startsWith("46007")) {
|
|
return "中国移动";
|
|
}
|
|
if (imsi.startsWith("46001")) {
|
|
return "中国联通";
|
|
}
|
|
if (imsi.startsWith("46003")) {
|
|
return "中国电信";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
// ping domain
|
|
public static String ping(String domain) {
|
|
try {
|
|
Process process = Runtime.getRuntime().exec("ping -c 5 " + domain);
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
String line;
|
|
StringBuilder builder = new StringBuilder();
|
|
while ((line = reader.readLine()) != null) {
|
|
builder.append(line);
|
|
builder.append("\n");
|
|
}
|
|
return builder.toString();
|
|
} catch (IOException e) {
|
|
return Log.getStackTraceString(e);
|
|
}
|
|
}
|
|
|
|
public static long getTotalRamSizeOfDevice(Context context) {
|
|
ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
|
|
if (actManager != null) {
|
|
actManager.getMemoryInfo(memInfo);
|
|
}
|
|
return memInfo.totalMem / (1024 * 1024);
|
|
}
|
|
|
|
}
|