张波

更新代码

... ... @@ -56,7 +56,7 @@ uploadArchives {
}
pom.project {
artifactId 'wdkit'
version '1.0.0'
version '1.0.1'
groupId 'com.wd'
packaging 'aar'
}
... ...
package com.wd.foundation.wdkit;
public class Teat {
}
/*
* Copyright (c) Wondertek Technologies Co., Ltd. 2019-2020. All rights reserved.
*/
package com.wd.foundation.wdkit.system;
import android.content.Context;
import android.text.TextUtils;
import com.wd.foundation.wdkitcore.tools.AppContext;
/**
* 设备工具<BR>
*
* @author zhangbo
* @version [V1.0.0, 2020/7/23]
* @since V1.0.0
*/
public final class DeviceIdUtil {
private static String DEVICE_IMEI = "";
private DeviceIdUtil() {
}
/**
* 获取设备id
*
* @return
*/
public static String getDeviceId() {
Context context = AppContext.getContext();
if (TextUtils.isEmpty(DEVICE_IMEI)) {
if (context != null) {
DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory();
DEVICE_IMEI = deviceUuidFactory.getUniqueId(context);
}
}
return DEVICE_IMEI;
}
}
... ...
package com.wd.foundation.wdkit.system;
import java.util.UUID;
import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.text.TextUtils;
class DeviceUuidFactory {
private static final String PREFS_FILE = "dev_id.xml";
private static final String PREFS_DEVICE_ID = "dev_id";
private String uniqueId; // 唯一Id
public DeviceUuidFactory() {
}
public synchronized String getUniqueId(Context context) {
if (!TextUtils.isEmpty(uniqueId)) {
return uniqueId;
}
uniqueId = getIdFromLocal(context);
if (TextUtils.isEmpty(uniqueId)) {
uniqueId = getAndroidId(context);
if (TextUtils.isEmpty(uniqueId)) {
uniqueId = UUID.randomUUID().toString();
}
final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);
prefs.edit().putString(PREFS_DEVICE_ID, uniqueId).apply();
}
return uniqueId;
}
private synchronized String getIdFromLocal(Context context) {
final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);
String id = prefs.getString(PREFS_DEVICE_ID, null);
if (!TextUtils.isEmpty(id)) {
return id;
}
return null;
}
private synchronized String getAndroidId(Context context) {
String generateId = null;
try {
final String androidId =
Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
if (TextUtils.isEmpty(androidId) || "9774d56d682e549c".equals(androidId)) {
generateId = getId(context);
} else {
UUID uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
generateId = uuid.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return generateId;
}
private synchronized String getId(Context context) {
return Installation.id(context);
}
}
... ...
package com.wd.foundation.wdkit.system;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.UUID;
import android.content.Context;
/**
* @author lvjinhui
* @description 设备ID
*/
class Installation {
private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";
public synchronized static String id(Context context) {
if (sID == null) {
File installation = new File(context.getFilesDir(), INSTALLATION);
try {
if (!installation.exists()) {
writeInstallationFile(installation);
}
sID = readInstallationFile(installation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return sID;
}
private static String readInstallationFile(File installation) throws IOException {
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
if (null != f) {
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(bytes);
}
private static void writeInstallationFile(File installation) throws IOException {
FileOutputStream out = new FileOutputStream(installation);
String id = UUID.randomUUID().toString();
out.write(id.getBytes());
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
... ...
/*
* Copyright (c) Wondertek Technologies Co., Ltd. 2019-2020. All rights reserved.
*/
package com.wd.foundation.wdkit.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;
import androidx.annotation.RequiresApi;
import com.wd.foundation.wdkit.system.DeviceIdUtil;
import com.wd.foundation.wdkitcore.tools.AppContext;
/**
* 设备相关工具类<BR>
*
* @author wangnaiwen
* @version [V1.0.0.0, 2020/4/7]
* @since V1.0.0.0
*/
public class DeviceUtils {
private DeviceUtils() {
}
/**
* 获取设备屏幕宽度.
*
* @return the int
*/
public static int fastGetScreenWidth() {
return getDeviceWidth();
}
/**
* 获取设备屏幕高度.
*
* @return the int
*/
public static int fastGetScreenHeight() {
return getDeviceHeight();
}
/**
* 获取设备屏幕宽度.
*
* @return 屏幕宽度
*/
public static int getDeviceWidth() {
Context context = AppContext.getContext();
return Math.min(context.getResources().getDisplayMetrics().widthPixels,
context.getResources().getDisplayMetrics().heightPixels);
}
/**
* 获取设备屏幕高度.
*
* @return 屏幕高度
*/
public static int getDeviceHeight() {
Context context = AppContext.getContext();
return Math.max(context.getResources().getDisplayMetrics().widthPixels,
context.getResources().getDisplayMetrics().heightPixels);
}
public static int getScreenHeight() {
Context context = AppContext.getContext();
Display display = ((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
return display.getHeight();
}
/**
* 获取版本名称
*/
public static String getVersionName() {
try {
Context context = AppContext.getContext();
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取版本号
*/
public static int getVersionCode(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取当前手机系统版本号
*
* @return 系统版本号
*/
public static String getSystemVersion() {
return Build.VERSION.RELEASE;
}
/**
* 获取机型
*/
public static String getPhoneModel() {
try {
/**
* 手机品牌
*/
String brand = Build.BRAND;
/**
* 手机型号
*/
String model = Build.MODEL;
return brand + " " + model;
} catch (Exception exception) {
exception.printStackTrace();
}
return "";
}
/**
* 兼容API获取屏幕信息
*
* @param context
* @return
*/
public static Display getDisplay(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return getDisplayApiR(context);
} else {
return getDisplayApiL(context);
}
}
private static Display getDisplayApiL(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay();
}
@RequiresApi(api = Build.VERSION_CODES.R)
private static Display getDisplayApiR(Context context) {
return context.getDisplay();
}
public static boolean isStrangePhone() {
boolean strangePhone = "mx5".equalsIgnoreCase(Build.DEVICE) || "Redmi Note2".equalsIgnoreCase(Build.DEVICE)
|| "Z00A_1".equalsIgnoreCase(Build.DEVICE) || "hwH60-L02".equalsIgnoreCase(Build.DEVICE)
|| "hermes".equalsIgnoreCase(Build.DEVICE)
|| ("V4".equalsIgnoreCase(Build.DEVICE) && "Meitu".equalsIgnoreCase(Build.MANUFACTURER))
|| ("m1metal".equalsIgnoreCase(Build.DEVICE) && "Meizu".equalsIgnoreCase(Build.MANUFACTURER));
return strangePhone;
}
/**
* 获取设备id
*
* @return
*/
public static String getDeviceId() {
// 获取设备id,业务可以不用这个,自己生成id。
return DeviceIdUtil.getDeviceId();
}
}
... ...