lwb
2021-03-02 c4d0022c42c437aaabd24985ae5ee2fd862d28f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.secondworld.sdk.utils;
 
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
 
import com.secondworld.sdk.GameAppProxy;
 
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Locale;
import java.util.UUID;
 
public class DeviceUtil {
 
    public static final String FAKE_MAC = "02:00:00:00:00:00";
 
 
 
    public static long getVersionCode() {
        long appVersionCode = 0;
        try {
            PackageInfo packageInfo = GameAppProxy.app.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(GameAppProxy.app.getPackageName(), 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            LogUtil.e("getAppVersionCode", e);
        }
        return appVersionCode;
    }
 
 
    public static String getVersionName() {
        String appVersionName = "";
        try {
            PackageInfo packageInfo = GameAppProxy.app.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(GameAppProxy.app.getPackageName(), 0);
            appVersionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            LogUtil.e("getAppVersionName", e);
        }
        return appVersionName;
    }
 
    /**
     * 获取当前设备总运行内存
     * 单位为 byte
     *
     * @return
     */
    public static long getTotalRAMSize() {
        ActivityManager manager = (ActivityManager) GameAppProxy.app.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        manager.getMemoryInfo(info);
        return info.totalMem;
    }
 
    public static String getAndroidId() {
        String ANDROID_ID = Settings.System.getString(GameAppProxy.app.getContentResolver(), Settings.System.ANDROID_ID);
        if (ANDROID_ID == null || ANDROID_ID.equals("9774d56d682e549c"))
            return "";
        return ANDROID_ID;
    }
 
    /**
     * 唯一标识
     *
     * @return
     */
    public static String getUniqueID() {
        String uniqueID = SPUtils.get().getString(SPUtils.UNIQUE_ID, "");
        if (!uniqueID.trim().isEmpty())
            return uniqueID;
        uniqueID = getAndroidId() + getLocalMac();
        //自己生成的uid
        if (uniqueID.trim().isEmpty())
            uniqueID = UUID.randomUUID().toString();
 
        uniqueID = Util.md5(uniqueID).toUpperCase();
 
        SPUtils.get()
                .edit()
                .putString(SPUtils.UNIQUE_ID, uniqueID.trim())
                .apply();
 
        return uniqueID;
    }
 
    /**
     * mac 地址
     *
     * @return
     */
    public static String getLocalMac() {
        String mac = SPUtils.get().getString(SPUtils.MAC, "");
        if (!mac.trim().isEmpty())
            return mac;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                String interfaceName = "wlan0";
                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                NetworkInterface intf = null;
                while (interfaces.hasMoreElements()) {
                    intf = interfaces.nextElement();
                    if (!intf.getName().equalsIgnoreCase(interfaceName))
                        continue;
                    byte[] addresses = intf.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder buf = new StringBuilder();
                        for (byte aMac : addresses) {
                            buf.append(String.format("%02X:", aMac));
                        }
                        if (buf.length() > 0) {
                            buf.deleteCharAt(buf.length() - 1);
                        }
                        mac = buf.toString();
                    }
                    break;
                }
            } else {
                WifiManager wifi = (WifiManager) GameAppProxy.app.getSystemService(Context.WIFI_SERVICE);
                if (wifi != null) {
                    WifiInfo wifiInfo = wifi.getConnectionInfo();
                    if (wifiInfo != null) {
                        mac = wifiInfo.getMacAddress();
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
 
        mac = mac.toUpperCase(Locale.ENGLISH);
 
        if (mac.equals(FAKE_MAC))
            mac = "";
 
        SPUtils.get()
                .edit()
                .putString(SPUtils.MAC, mac.trim())
                .apply();
 
        return mac;
    }
 
 
}