package com.demo.lib.common.util.device; import android.content.Context; import android.content.SharedPreferences; import com.google.gson.Gson; import com.demo.lib.common.DeviceUuidFactory; import com.demo.lib.common.entity.ExpireTimeValue; import com.demo.lib.common.util.common.StringUtils; import static android.content.Context.MODE_PRIVATE; public class DeviceInfoUtil { enum DeviceInfoType { deviceId("device_id_"), imei("imei_"), androidId("android_id_"), mac("mac_"), utdid("utdid_"), systemModel("system_model_"), systemVersion("system_version_"); private String key; public String getKey() { return key; } private DeviceInfoType(String key) { this.key = key; } } private static String getDeviceInfo(Context context, DeviceInfoType deviceInfoType) { SharedPreferences deviceInfo = context.getSharedPreferences("deviceInfo", MODE_PRIVATE); String data = deviceInfo.getString(deviceInfoType.getKey(), ""); ExpireTimeValue value = null; if (!StringUtils.isEmpty(data)) { value = new Gson().fromJson(data, ExpireTimeValue.class); if (value.getExpireTime() < System.currentTimeMillis()) { value = null; } } if (value == null) { String v = ""; if (deviceInfoType == DeviceInfoType.imei) { v = MobileUtil.getIMEI(context); } else if (deviceInfoType == DeviceInfoType.utdid) { v = com.ta.utdid2.device.UTDevice.getUtdid(context); } else if (deviceInfoType == DeviceInfoType.systemModel) { v = MobileUtil.getSystemModel(); } else if (deviceInfoType == DeviceInfoType.systemVersion) { v = MobileUtil.getSystemVersion(); } else if (deviceInfoType == DeviceInfoType.deviceId) { v = new DeviceUuidFactory(context).getDeviceUuid() + ""; } value = new ExpireTimeValue(v, System.currentTimeMillis() + 1000 * 60 * 10); SharedPreferences.Editor editor = deviceInfo.edit(); editor.putString(deviceInfoType.getKey(), new Gson().toJson(value)); editor.commit(); } if (value == null) return ""; else return value.getValue(); } public static String getIMEI(Context context) { return getDeviceInfo(context, DeviceInfoType.imei); } public static String getAndroidId(Context context) { return getDeviceInfo(context, DeviceInfoType.androidId); } public static String getUtdid(Context context) { return getDeviceInfo(context, DeviceInfoType.utdid); } public static String getSystemModel(Context context) { return getDeviceInfo(context, DeviceInfoType.systemModel); } public static String getSystemVersion(Context context) { return getDeviceInfo(context, DeviceInfoType.systemVersion); } public static String getDeviceId(Context context) { return getDeviceInfo(context, DeviceInfoType.deviceId); } }