Showing
6 changed files
with
363 additions
and
5 deletions
| 1 | +/* | ||
| 2 | + * Copyright (c) Wondertek Technologies Co., Ltd. 2019-2020. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.wd.foundation.wdkit.system; | ||
| 6 | + | ||
| 7 | +import android.content.Context; | ||
| 8 | +import android.text.TextUtils; | ||
| 9 | + | ||
| 10 | +import com.wd.foundation.wdkitcore.tools.AppContext; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * 设备工具<BR> | ||
| 14 | + * | ||
| 15 | + * @author zhangbo | ||
| 16 | + * @version [V1.0.0, 2020/7/23] | ||
| 17 | + * @since V1.0.0 | ||
| 18 | + */ | ||
| 19 | +public final class DeviceIdUtil { | ||
| 20 | + private static String DEVICE_IMEI = ""; | ||
| 21 | + | ||
| 22 | + private DeviceIdUtil() { | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * 获取设备id | ||
| 27 | + * | ||
| 28 | + * @return | ||
| 29 | + */ | ||
| 30 | + public static String getDeviceId() { | ||
| 31 | + Context context = AppContext.getContext(); | ||
| 32 | + if (TextUtils.isEmpty(DEVICE_IMEI)) { | ||
| 33 | + if (context != null) { | ||
| 34 | + DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(); | ||
| 35 | + DEVICE_IMEI = deviceUuidFactory.getUniqueId(context); | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + return DEVICE_IMEI; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | +} |
| 1 | + | ||
| 2 | +package com.wd.foundation.wdkit.system; | ||
| 3 | + | ||
| 4 | +import java.util.UUID; | ||
| 5 | + | ||
| 6 | +import android.content.Context; | ||
| 7 | +import android.content.SharedPreferences; | ||
| 8 | +import android.provider.Settings; | ||
| 9 | +import android.text.TextUtils; | ||
| 10 | + | ||
| 11 | +class DeviceUuidFactory { | ||
| 12 | + private static final String PREFS_FILE = "dev_id.xml"; | ||
| 13 | + | ||
| 14 | + private static final String PREFS_DEVICE_ID = "dev_id"; | ||
| 15 | + | ||
| 16 | + private String uniqueId; // 唯一Id | ||
| 17 | + | ||
| 18 | + public DeviceUuidFactory() { | ||
| 19 | + | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + public synchronized String getUniqueId(Context context) { | ||
| 23 | + if (!TextUtils.isEmpty(uniqueId)) { | ||
| 24 | + return uniqueId; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + uniqueId = getIdFromLocal(context); | ||
| 28 | + | ||
| 29 | + if (TextUtils.isEmpty(uniqueId)) { | ||
| 30 | + | ||
| 31 | + uniqueId = getAndroidId(context); | ||
| 32 | + | ||
| 33 | + if (TextUtils.isEmpty(uniqueId)) { | ||
| 34 | + uniqueId = UUID.randomUUID().toString(); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0); | ||
| 38 | + prefs.edit().putString(PREFS_DEVICE_ID, uniqueId).apply(); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + return uniqueId; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + private synchronized String getIdFromLocal(Context context) { | ||
| 45 | + final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0); | ||
| 46 | + String id = prefs.getString(PREFS_DEVICE_ID, null); | ||
| 47 | + if (!TextUtils.isEmpty(id)) { | ||
| 48 | + return id; | ||
| 49 | + } | ||
| 50 | + return null; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + private synchronized String getAndroidId(Context context) { | ||
| 54 | + String generateId = null; | ||
| 55 | + | ||
| 56 | + try { | ||
| 57 | + final String androidId = | ||
| 58 | + Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); | ||
| 59 | + | ||
| 60 | + if (TextUtils.isEmpty(androidId) || "9774d56d682e549c".equals(androidId)) { | ||
| 61 | + generateId = getId(context); | ||
| 62 | + } else { | ||
| 63 | + | ||
| 64 | + UUID uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")); | ||
| 65 | + generateId = uuid.toString(); | ||
| 66 | + } | ||
| 67 | + } catch (Exception e) { | ||
| 68 | + e.printStackTrace(); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + return generateId; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + private synchronized String getId(Context context) { | ||
| 75 | + return Installation.id(context); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | +} |
| 1 | + | ||
| 2 | +package com.wd.foundation.wdkit.system; | ||
| 3 | + | ||
| 4 | +import java.io.File; | ||
| 5 | +import java.io.FileOutputStream; | ||
| 6 | +import java.io.IOException; | ||
| 7 | +import java.io.RandomAccessFile; | ||
| 8 | +import java.util.UUID; | ||
| 9 | + | ||
| 10 | +import android.content.Context; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * @author lvjinhui | ||
| 14 | + * @description 设备ID | ||
| 15 | + */ | ||
| 16 | +class Installation { | ||
| 17 | + private static String sID = null; | ||
| 18 | + | ||
| 19 | + private static final String INSTALLATION = "INSTALLATION"; | ||
| 20 | + | ||
| 21 | + public synchronized static String id(Context context) { | ||
| 22 | + if (sID == null) { | ||
| 23 | + File installation = new File(context.getFilesDir(), INSTALLATION); | ||
| 24 | + try { | ||
| 25 | + if (!installation.exists()) { | ||
| 26 | + writeInstallationFile(installation); | ||
| 27 | + } | ||
| 28 | + sID = readInstallationFile(installation); | ||
| 29 | + } catch (Exception e) { | ||
| 30 | + throw new RuntimeException(e); | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + return sID; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + private static String readInstallationFile(File installation) throws IOException { | ||
| 37 | + RandomAccessFile f = new RandomAccessFile(installation, "r"); | ||
| 38 | + byte[] bytes = new byte[(int) f.length()]; | ||
| 39 | + f.readFully(bytes); | ||
| 40 | + if (null != f) { | ||
| 41 | + try { | ||
| 42 | + f.close(); | ||
| 43 | + } catch (IOException e) { | ||
| 44 | + e.printStackTrace(); | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + return new String(bytes); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + private static void writeInstallationFile(File installation) throws IOException { | ||
| 51 | + FileOutputStream out = new FileOutputStream(installation); | ||
| 52 | + String id = UUID.randomUUID().toString(); | ||
| 53 | + out.write(id.getBytes()); | ||
| 54 | + if (null != out) { | ||
| 55 | + try { | ||
| 56 | + out.close(); | ||
| 57 | + } catch (IOException e) { | ||
| 58 | + e.printStackTrace(); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | +} |
| 1 | +/* | ||
| 2 | + * Copyright (c) Wondertek Technologies Co., Ltd. 2019-2020. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.wd.foundation.wdkit.utils; | ||
| 6 | + | ||
| 7 | +import android.content.Context; | ||
| 8 | +import android.content.pm.PackageInfo; | ||
| 9 | +import android.content.pm.PackageManager; | ||
| 10 | +import android.os.Build; | ||
| 11 | +import android.view.Display; | ||
| 12 | +import android.view.WindowManager; | ||
| 13 | + | ||
| 14 | +import androidx.annotation.RequiresApi; | ||
| 15 | + | ||
| 16 | +import com.wd.foundation.wdkit.system.DeviceIdUtil; | ||
| 17 | +import com.wd.foundation.wdkitcore.tools.AppContext; | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * 设备相关工具类<BR> | ||
| 21 | + * | ||
| 22 | + * @author wangnaiwen | ||
| 23 | + * @version [V1.0.0.0, 2020/4/7] | ||
| 24 | + * @since V1.0.0.0 | ||
| 25 | + */ | ||
| 26 | +public class DeviceUtils { | ||
| 27 | + | ||
| 28 | + private DeviceUtils() { | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * 获取设备屏幕宽度. | ||
| 33 | + * | ||
| 34 | + * @return the int | ||
| 35 | + */ | ||
| 36 | + public static int fastGetScreenWidth() { | ||
| 37 | + return getDeviceWidth(); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * 获取设备屏幕高度. | ||
| 42 | + * | ||
| 43 | + * @return the int | ||
| 44 | + */ | ||
| 45 | + public static int fastGetScreenHeight() { | ||
| 46 | + return getDeviceHeight(); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * 获取设备屏幕宽度. | ||
| 51 | + * | ||
| 52 | + * @return 屏幕宽度 | ||
| 53 | + */ | ||
| 54 | + public static int getDeviceWidth() { | ||
| 55 | + Context context = AppContext.getContext(); | ||
| 56 | + return Math.min(context.getResources().getDisplayMetrics().widthPixels, | ||
| 57 | + context.getResources().getDisplayMetrics().heightPixels); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + /** | ||
| 61 | + * 获取设备屏幕高度. | ||
| 62 | + * | ||
| 63 | + * @return 屏幕高度 | ||
| 64 | + */ | ||
| 65 | + public static int getDeviceHeight() { | ||
| 66 | + Context context = AppContext.getContext(); | ||
| 67 | + return Math.max(context.getResources().getDisplayMetrics().widthPixels, | ||
| 68 | + context.getResources().getDisplayMetrics().heightPixels); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + public static int getScreenHeight() { | ||
| 72 | + Context context = AppContext.getContext(); | ||
| 73 | + Display display = ((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)) | ||
| 74 | + .getDefaultDisplay(); | ||
| 75 | + return display.getHeight(); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * 获取版本名称 | ||
| 80 | + */ | ||
| 81 | + public static String getVersionName() { | ||
| 82 | + try { | ||
| 83 | + Context context = AppContext.getContext(); | ||
| 84 | + PackageManager packageManager = context.getPackageManager(); | ||
| 85 | + PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0); | ||
| 86 | + return packageInfo.versionName; | ||
| 87 | + } catch (PackageManager.NameNotFoundException e) { | ||
| 88 | + e.printStackTrace(); | ||
| 89 | + } | ||
| 90 | + return null; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * 获取版本号 | ||
| 95 | + */ | ||
| 96 | + public static int getVersionCode(Context context) { | ||
| 97 | + try { | ||
| 98 | + PackageManager packageManager = context.getPackageManager(); | ||
| 99 | + PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0); | ||
| 100 | + return packageInfo.versionCode; | ||
| 101 | + } catch (PackageManager.NameNotFoundException e) { | ||
| 102 | + e.printStackTrace(); | ||
| 103 | + } | ||
| 104 | + return 0; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + /** | ||
| 108 | + * 获取当前手机系统版本号 | ||
| 109 | + * | ||
| 110 | + * @return 系统版本号 | ||
| 111 | + */ | ||
| 112 | + | ||
| 113 | + public static String getSystemVersion() { | ||
| 114 | + return Build.VERSION.RELEASE; | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + /** | ||
| 118 | + * 获取机型 | ||
| 119 | + */ | ||
| 120 | + public static String getPhoneModel() { | ||
| 121 | + try { | ||
| 122 | + /** | ||
| 123 | + * 手机品牌 | ||
| 124 | + */ | ||
| 125 | + String brand = Build.BRAND; | ||
| 126 | + /** | ||
| 127 | + * 手机型号 | ||
| 128 | + */ | ||
| 129 | + String model = Build.MODEL; | ||
| 130 | + return brand + " " + model; | ||
| 131 | + } catch (Exception exception) { | ||
| 132 | + exception.printStackTrace(); | ||
| 133 | + } | ||
| 134 | + return ""; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + /** | ||
| 138 | + * 兼容API获取屏幕信息 | ||
| 139 | + * | ||
| 140 | + * @param context | ||
| 141 | + * @return | ||
| 142 | + */ | ||
| 143 | + public static Display getDisplay(Context context) { | ||
| 144 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| 145 | + return getDisplayApiR(context); | ||
| 146 | + } else { | ||
| 147 | + return getDisplayApiL(context); | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + private static Display getDisplayApiL(Context context) { | ||
| 152 | + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | ||
| 153 | + return wm.getDefaultDisplay(); | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + @RequiresApi(api = Build.VERSION_CODES.R) | ||
| 157 | + private static Display getDisplayApiR(Context context) { | ||
| 158 | + return context.getDisplay(); | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + public static boolean isStrangePhone() { | ||
| 162 | + boolean strangePhone = "mx5".equalsIgnoreCase(Build.DEVICE) || "Redmi Note2".equalsIgnoreCase(Build.DEVICE) | ||
| 163 | + || "Z00A_1".equalsIgnoreCase(Build.DEVICE) || "hwH60-L02".equalsIgnoreCase(Build.DEVICE) | ||
| 164 | + || "hermes".equalsIgnoreCase(Build.DEVICE) | ||
| 165 | + || ("V4".equalsIgnoreCase(Build.DEVICE) && "Meitu".equalsIgnoreCase(Build.MANUFACTURER)) | ||
| 166 | + || ("m1metal".equalsIgnoreCase(Build.DEVICE) && "Meizu".equalsIgnoreCase(Build.MANUFACTURER)); | ||
| 167 | + | ||
| 168 | + return strangePhone; | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + /** | ||
| 172 | + * 获取设备id | ||
| 173 | + * | ||
| 174 | + * @return | ||
| 175 | + */ | ||
| 176 | + public static String getDeviceId() { | ||
| 177 | + // 获取设备id,业务可以不用这个,自己生成id。 | ||
| 178 | + return DeviceIdUtil.getDeviceId(); | ||
| 179 | + } | ||
| 180 | +} |
-
Please register or login to post a comment