package com.secondworld.universalsdk; 
 | 
  
 | 
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.PermissionChecker; 
 | 
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 (PermissionChecker.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(); 
 | 
    } 
 | 
} 
 |