Runt
2024-09-28 1464250d6b481fb9274cc571ecbe031b71ab457c
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
package com.runt.open.mvi.utils;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
 
 
import com.github.gzuliyujiang.oaid.DeviceID;
import com.github.gzuliyujiang.oaid.DeviceIdentifier;
 
import org.json.JSONObject;
 
import java.lang.reflect.Method;
import java.util.HashSet;
 
/**
 * copy from: http://docs.aiduoyou.com/web/#/100/1495
 */
public class DeviceIdUtils {
 
 
    public static String getDeviceId(Context context, int slotId) {
        try {
            //实例化TelephonyManager对象
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Method method = telephonyManager.getClass().getMethod("getDeviceId", int.class);
            return (String) method.invoke(telephonyManager, slotId);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return "";
    }
 
    public static String getImei(Context context, int slotId) {
        try {
            //实例化TelephonyManager对象
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Method method = telephonyManager.getClass().getMethod("getImei", int.class);
            return (String) method.invoke(telephonyManager, slotId);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return "";
    }
 
    @SuppressLint("MissingPermission")
    public static String getDeviceId(Context context) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return tm.getDeviceId();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return "";
    }
    public static String getImei(Context context) {
        try {
            //实例化TelephonyManager对象
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Method method = telephonyManager.getClass().getMethod("getImei");
            return (String) method.invoke(telephonyManager);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return "";
    }
 
    public static String getAndroidId(Context context) {
        try {
            return Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return "";
    }
 
 
    public static JSONObject getDeviceIds(Context context) {
        try {
            String imei1 = getDeviceId(context, 0);
            String imei2 = getDeviceId(context, 1);
            String imei3 = getImei(context, 0);
            String imei4 = getImei(context, 1);
            String imei5 = getDeviceId(context);
            String imei6 = getImei(context);
            HashSet<String> hashSet = new HashSet();
            if (!TextUtils.isEmpty(imei1)) {
                hashSet.add(imei1);
            }
            if (!TextUtils.isEmpty(imei2)) {
                hashSet.add(imei2);
            }
            if (!TextUtils.isEmpty(imei3)) {
                hashSet.add(imei3);
            }
            if (!TextUtils.isEmpty(imei4)) {
                hashSet.add(imei4);
            }
            if (!TextUtils.isEmpty(imei5)) {
                hashSet.add(imei5);
            }
            if (!TextUtils.isEmpty(imei6)) {
                hashSet.add(imei6);
            }
            JSONObject jsonObject = new JSONObject();
            int i = 0;
            for (String value : hashSet) {
                i ++;
                jsonObject.put(String.valueOf(i), value);
            }
 
            String androidId = getAndroidId(context);
            if (!TextUtils.isEmpty(androidId)) {
                jsonObject.put("6", androidId);
            }
 
            DeviceID.supportedOAID(context);
            // 获取OAID/AAID,同步调用
            String OAID = DeviceIdentifier.getOAID(context);
            jsonObject.put("7", OAID);
            // 2022-03-15 暂时不管oaid
//            String OAID = SPManager.getValue(SPManager.OAID, "");
//            String OAID = PrefUtil.get(PrefUtil.OAID);
//            if (!TextUtils.isEmpty(OAID)) {
//                jsonObject.put("7", OAID);
//            }
 
            return jsonObject;
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return null;
    }
 
 
 
}