Commit 6760ab4d by 严久程

取mac地址

parent 905ef045
......@@ -72,6 +72,7 @@ internal class BaiduActionDataBean private constructor(builder: Builder) {
internal var androidId: String? = ""
internal var convertParam: String? = ""
internal var uid : String? = ""
internal var mac : String? = ""
internal var channel : String? = ""
fun pkname(pkname: String = ""): Builder {
......@@ -143,6 +144,10 @@ internal class BaiduActionDataBean private constructor(builder: Builder) {
this.uid = uid
return this
}
fun mac(mac : String? = "") : Builder {
this.mac = mac
return this
}
fun channel(channel : String? = "") : Builder {
this.channel = channel
......
......@@ -6,19 +6,33 @@ import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
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.tbruyelle.rxpermissions2.RxPermissions;
import com.yidianling.common.tools.RxTool;
import com.ydl.ydlcommon.BuildConfig;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import static com.umeng.socialize.utils.ContextUtil.getPackageName;
public class DeviceTool {
......@@ -223,4 +237,298 @@ public class DeviceTool {
return NETWORN_NONE;
}
public static String getMac(Context context) {
String strMac = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
strMac = getLocalMacAddressFromWifiInfo(context);
return strMac;
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
strMac = getMacAddress(context);
return strMac;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (!TextUtils.isEmpty(getMacAddress())) {
strMac = getMacAddress();
return strMac;
} else if (!TextUtils.isEmpty(getMachineHardwareAddress())) {
strMac = getMachineHardwareAddress();
return strMac;
} else {
strMac = getLocalMacAddressFromBusybox();
return strMac;
}
}
return "02:00:00:00:00:00";
}
public static String getLocalMacAddressFromWifiInfo(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo winfo = wifi.getConnectionInfo();
String mac = winfo.getMacAddress();
return mac;
}
public static String getMacAddress(Context context) {
// 如果是6.0以下,直接通过wifimanager获取
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
String macAddress0 = getMacAddress0(context);
if (!TextUtils.isEmpty(macAddress0)) {
return macAddress0;
}
}
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);
for (; null != str; ) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (Exception ex) {
Log.e("----->" + "NetInfoManager", "getMacAddress:" + ex.toString());
}
if (macSerial == null || "".equals(macSerial)) {
try {
return loadFileAsString("/sys/class/net/eth0/address")
.toUpperCase().substring(0, 17);
} catch (Exception e) {
e.printStackTrace();
Log.e("----->" + "NetInfoManager",
"getMacAddress:" + e.toString());
}
}
return macSerial;
}
private static String getMacAddress0(Context context) {
if (isAccessWifiStateAuthorized(context)) {
WifiManager wifiMgr = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = null;
try {
wifiInfo = wifiMgr.getConnectionInfo();
return wifiInfo.getMacAddress();
} catch (Exception e) {
Log.e("----->" + "NetInfoManager",
"getMacAddress0:" + e.toString());
}
}
return "";
}
/**
* Check whether accessing wifi state is permitted
*
* @param context
* @return
*/
private static boolean isAccessWifiStateAuthorized(Context context) {
if (PackageManager.PERMISSION_GRANTED == context
.checkCallingOrSelfPermission("android.permission.ACCESS_WIFI_STATE")) {
Log.e("----->" + "NetInfoManager", "isAccessWifiStateAuthorized:"
+ "access wifi state is enabled");
return true;
} else
return false;
}
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();
}
/**
* 根据IP地址获取MAC地址
*
* @return
*/
public static String getMacAddress() {
String strMacAddr = null;
try {
// 获得IpD地址
InetAddress ip = getLocalInetAddress();
byte[] b = NetworkInterface.getByInetAddress(ip)
.getHardwareAddress();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; i++) {
if (i != 0) {
buffer.append(':');
}
String str = Integer.toHexString(b[i] & 0xFF);
buffer.append(str.length() == 1 ? 0 + str : str);
}
strMacAddr = buffer.toString().toUpperCase();
} catch (Exception e) {
}
return strMacAddr;
}
/**
* 获取移动设备本地IP
*
* @return
*/
private static InetAddress getLocalInetAddress() {
InetAddress ip = null;
try {
// 列举
Enumeration<NetworkInterface> en_netInterface = NetworkInterface
.getNetworkInterfaces();
while (en_netInterface.hasMoreElements()) {// 是否还有元素
NetworkInterface ni = (NetworkInterface) en_netInterface
.nextElement();// 得到下一个元素
Enumeration<InetAddress> en_ip = ni.getInetAddresses();// 得到一个ip地址的列举
while (en_ip.hasMoreElements()) {
ip = en_ip.nextElement();
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1)
break;
else
ip = null;
}
if (ip != null) {
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
}
/**
* 获取本地IP
*
* @return
*/
private static String getLocalIpAddress() {
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
public static String getMachineHardwareAddress() {
Enumeration<NetworkInterface> interfaces = null;
try {
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
String hardWareAddress = null;
NetworkInterface iF = null;
if (interfaces == null) {
return null;
}
while (interfaces.hasMoreElements()) {
iF = interfaces.nextElement();
try {
hardWareAddress = bytesToString(iF.getHardwareAddress());
if (hardWareAddress != null)
break;
} catch (SocketException e) {
e.printStackTrace();
}
}
return hardWareAddress;
}
/***
* byte转为String
*
* @param bytes
* @return
*/
private static String bytesToString(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
StringBuilder buf = new StringBuilder();
for (byte b : bytes) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString();
}
public static String getLocalMacAddressFromBusybox() {
String result = "";
String Mac = "";
result = callCmd("busybox ifconfig", "HWaddr");
// 如果返回的result == null,则说明网络不可取
if (result == null) {
return "网络异常";
}
// 对该行数据进行解析
// 例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67
if (result.length() > 0 && result.contains("HWaddr") == true) {
Mac = result.substring(result.indexOf("HWaddr") + 6,
result.length() - 1);
result = Mac;
}
return result;
}
private static String callCmd(String cmd, String filter) {
String result = "";
String line = "";
try {
Process proc = Runtime.getRuntime().exec(cmd);
InputStreamReader is = new InputStreamReader(proc.getInputStream());
BufferedReader br = new BufferedReader(is);
while ((line = br.readLine()) != null
&& line.contains(filter) == false) {
result += line;
}
result = line;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
......@@ -48,16 +48,22 @@ class ActionCountUtils {
count(eventName, *sign1, urlStr = "", apiStr = "")
}
fun countUid(eventName: String,uid: String, vararg sign1: String) {
count(eventName, *sign1, urlStr = "", apiStr = "",uid = uid)
fun countUid(eventName: String, uid: String, vararg sign1: String) {
count(eventName, *sign1, urlStr = "", apiStr = "", uid = uid)
}
fun count(eventName: String, vararg sign1: String, urlStr: String = "", apiStr: String = "",uid: String = "") {
fun count(
eventName: String,
vararg sign1: String,
urlStr: String = "",
apiStr: String = "",
uid: String = ""
) {
var partId = ""
var position = ""
var userId = uid
if (TextUtils.isEmpty(uid)){
userId = ModularServiceManager.getPlatformUserService()?.getUser()?.userId?: ""
if (TextUtils.isEmpty(uid)) {
userId = ModularServiceManager.getPlatformUserService()?.getUser()?.userId ?: ""
}
if (eventName.contains("|")) {
val splitList = eventName.split("|")
......@@ -73,11 +79,25 @@ class ActionCountUtils {
/**
* 行为数据埋点调用的方法
*/
fun count(uid: String?, partId: String, position: String, url: String, api: String, sign1: String) {
fun count(
uid: String?,
partId: String,
position: String,
url: String,
api: String,
sign1: String
) {
count(uid, partId, position, url, api, signs = *arrayOf(sign1))
}
fun count(uid: String?, partId: String, position: String, url: String, api: String, vararg signs: String) {
fun count(
uid: String?,
partId: String,
position: String,
url: String,
api: String,
vararg signs: String
) {
try {
......@@ -140,17 +160,25 @@ class ActionCountUtils {
/**
* 百度检测数据埋点调用的方法
*/
fun baiduCount( partId: String, position: String, convertType: Int) {
fun baiduCount(partId: String, position: String, convertType: Int) {
val appFrom = BaseApp.instance.getGlobalConfig().appFrom
if (YDLConstants.FROM_YDL != appFrom){
if (YDLConstants.FROM_YDL != appFrom) {
return
}
val userId= ModularServiceManager.getPlatformUserService()?.getUser()?.userId?: ""
baiduCount(userId,partId,position,"","",convertType)
val userId = ModularServiceManager.getPlatformUserService()?.getUser()?.userId ?: ""
baiduCount(userId, partId, position, "", "", convertType)
}
fun baiduCount(uid: String?, partId: String, position: String, url: String, api: String, convertType: Int) {
var appId = "ydl-app-android-"+ (if (BaseApp.instance.getGlobalConfig().appFrom== YDLConstants.FROM_XLZX)"xlzx" else "user")
fun baiduCount(
uid: String?,
partId: String,
position: String,
url: String,
api: String,
convertType: Int
) {
var appId =
"ydl-app-android-" + (if (BaseApp.instance.getGlobalConfig().appFrom == YDLConstants.FROM_XLZX) "xlzx" else "user")
try {
val actionDataParams = ActionDataBean.Builder()
......@@ -185,6 +213,16 @@ class ActionCountUtils {
baiduActionDataParams.network(DeviceTool.getNetworkState(RxTool.getContext()))
baiduActionDataParams.convertParam("")
baiduActionDataParams.uid(uid)
try {
var mac = DeviceTool.getMac(BaseApp.getApp())
if (!TextUtils.isEmpty(mac)) {
mac = mac.replace(":", "")
}
com.ydl.ydlcommon.utils.LogUtil.d("MAC=${mac}")
baiduActionDataParams.mac(mac)
} catch (e: Exception) {
}
baiduActionDataParams.channel(YdlCommonOut.getChannelName())
val gson = Gson().toJson(baiduActionDataParams)
Log.e("Action", "---------$gson")
......@@ -289,7 +327,8 @@ class ActionCountUtils {
*/
private fun isWifi(): Boolean {
try {
var connectivityManager: ConnectivityManager = RxTool.getContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
var connectivityManager: ConnectivityManager =
RxTool.getContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
var info: NetworkInfo = connectivityManager.activeNetworkInfo
if (info != null && info.type == ConnectivityManager.TYPE_WIFI) {
return true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment