client_Hale
2018-09-04 fff1f19a381e6ad295cb3e7bd41cce48e74fbeea
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.secondworld.univeralsdk;
 
import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.util.Log;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.util.UUID;
 
/**
 * Created by Administrator on 2018/7/12 0012.
 */
 
public class UniqueID
{
    public static String get(Context context)
    {
        String _id = read(context);
 
        if (_id != null && !_id.equals(""))
        {
            return _id;
        }
 
        StringBuffer _buffer = new StringBuffer();
 
        _id = getDeviceId(context);
        _buffer.append(_id);
 
        _id = getLocalMac(context).replace(":", "");
        _buffer.append(_id);
 
        if (_buffer == null || _buffer.length() <= 0)
        {
            UUID _uuid = UUID.randomUUID();
            _id = _uuid.toString().replace("-", "");
            _buffer.append(_id);
        }
 
        _id = getMD5(_buffer.toString(), false);
 
        if (_id.length() > 0)
        {
            save(_id, context);
        }
        return _id;
    }
 
    private static String read(Context context)
    {
        File _file = new File(context.getExternalFilesDir(""), "/tsw_device_unique_id.txt");
        if (!_file.exists())
        {
            return null;
        }
        StringBuffer _buffer = new StringBuffer();
        try
        {
            FileInputStream _fis = new FileInputStream(_file);
            InputStreamReader _isr = new InputStreamReader(_fis, "UTF-8");
            Reader _in = new BufferedReader(_isr);
            int _index;
            while ((_index = _in.read()) > -1)
            {
                _buffer.append((char) _index);
            }
            _in.close();
            return _buffer.toString();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }
 
    private static void save(String id, Context context)
    {
        File _file = new File(context.getExternalFilesDir(""), "/tsw_device_unique_id.txt");
        try
        {
            Log.i("UniqueIDUtil", "path: " + _file.getAbsolutePath());
            FileOutputStream _fos = new FileOutputStream(_file);
            Writer _writer = new OutputStreamWriter(_fos, "UTF-8");
            _writer.write(id);
            _writer.close();
            _fos.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
 
    public static String getAndroidID(Context context)
    {
        return Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
    }
 
    public static String getDeviceId(Context context)
    {
        SharedPreferences _sp = context.getSharedPreferences(context.getPackageName(),
                                                             Context.MODE_PRIVATE);
        String _deviceId = _sp.getString(StaticDefine.LS_KEY_IMEI, "");
        if (!_deviceId.equals(""))
        {
            return _deviceId;
        }
 
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
 
        if (ContextCompat.checkSelfPermission(context,
                                              Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)
        {
            _deviceId = tm.getDeviceId();
            SharedPreferences.Editor _e = _sp.edit();
            _e.putString(StaticDefine.LS_KEY_IMEI, _deviceId);
            _e.apply();
            return _deviceId;
        }
        return "";
    }
 
    public static String getLocalMac(Context context)
    {
        SharedPreferences _sp = context.getSharedPreferences(context.getPackageName(),
                                                             Context.MODE_PRIVATE);
        String _macAddress = _sp.getString(StaticDefine.LS_KEY_MAC, "");
        if (!_macAddress.equals(""))
        {
            return _macAddress;
        }
        StringBuffer _buffer = new StringBuffer();
        NetworkInterface _networkInterface;
        try
        {
            _networkInterface = NetworkInterface.getByName("eth1");
            if (_networkInterface == null)
            {
                _networkInterface = NetworkInterface.getByName("wlan0");
            }
            if (_networkInterface == null)
            {
                return "";
            }
            byte[] _addresses = _networkInterface.getHardwareAddress();
            for (byte b : _addresses)
            {
                _buffer.append(String.format("%02X:", b));
            }
            if (_buffer.length() > 0)
            {
                _buffer.deleteCharAt(_buffer.length() - 1);
            }
            _macAddress = _buffer.toString();
            SharedPreferences.Editor _e = _sp.edit();
            _e.putString(StaticDefine.LS_KEY_MAC, _macAddress);
            _e.apply();
        } catch (SocketException e)
        {
            e.printStackTrace();
            return "";
        }
        return _macAddress;
    }
 
    public static String getMD5(String message, boolean upperCase)
    {
        String md5str = "";
        try
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
 
            byte[] input = message.getBytes();
 
            byte[] buff = md.digest(input);
 
            md5str = bytesToHex(buff, upperCase);
 
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return md5str;
    }
 
    private static String bytesToHex(byte[] bytes, boolean upperCase)
    {
        StringBuffer md5str = new StringBuffer();
        int digital;
        for (int i = 0; i < bytes.length; i++)
        {
            digital = bytes[i];
 
            if (digital < 0)
            {
                digital += 256;
            }
            if (digital < 16)
            {
                md5str.append("0");
            }
            md5str.append(Integer.toHexString(digital));
        }
        if (upperCase)
        {
            return md5str.toString().toUpperCase();
        }
        return md5str.toString().toLowerCase();
    }
}