Selaa lähdekoodia

修改获取设备id方式

weizhengliang 2 vuotta sitten
vanhempi
commit
5c3b6e913b

+ 1 - 0
home/src/main/AndroidManifest.xml

@@ -3,6 +3,7 @@
     xmlns:tools="http://schemas.android.com/tools"
     package="com.wdkl.ncs.android.component.home">
 
+    <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
     <uses-permission android:name="android.permission.WRITE_CALL_LOG" />
     <uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
     <uses-permission android:name="android.permission.BLUETOOTH"/>

+ 8 - 4
home/src/main/code/com/wdkl/ncs/android/component/home/activity/WatchHome2Activity.kt

@@ -124,8 +124,7 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
         initCountDownTimer()
 
         //网络强度监听
-        teleManager =
-            (applicationContext.getSystemService(Context.TELEPHONY_SERVICE)) as TelephonyManager
+        teleManager = (applicationContext.getSystemService(Context.TELEPHONY_SERVICE)) as TelephonyManager
         netType = NetHelper.getInstance().getNetworkState(applicationContext)
         Log.i(TAG, "网络类型:" + netType)
         /*
@@ -172,8 +171,13 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
     }
 
     private fun permissionGranted() {
-        Constants.imei = Util.getIMEI(this)
-        Log.i(TAG, "IMEI " + Util.getIMEI(this))
+        if ("Redmi".equals(Build.BRAND)) {
+            Constants.imei = DeviceIdUtils.getDeviceId(activity)
+        } else {
+            Constants.imei = Util.getIMEI(this)
+        }
+
+        Log.i(TAG, "IMEI " + Constants.imei)
         Log.i(TAG, "mac " + Constants.mac)
         tv_feedback_device_info.text = Constants.imei
 

+ 14 - 0
home/src/main/code/com/wdkl/ncs/android/component/home/util/AppUtils.java

@@ -12,6 +12,7 @@ import com.wdkl.ncs.android.component.home.activity.WatchHome2Activity;
 import com.wdkl.ncs.android.lib.base.BaseApplication;
 import com.wdkl.ncs.keepbackground.work.DaemonEnv;
 
+import java.lang.reflect.Method;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -73,4 +74,17 @@ public class AppUtils {
         //查找字符串中是否有符合的子字符串
         return matcher.find();
     }
+
+    static public String getprop(String key, String defaultValue) {
+        String value = defaultValue;
+        try {
+            Class<?> c = Class.forName("android.os.SystemProperties");
+            Method get = c.getMethod("get", String.class, String.class);
+            value = (String) (get.invoke(c, key, "unknown"));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return value;
+    }
 }

+ 213 - 0
home/src/main/code/com/wdkl/ncs/android/component/home/util/DeviceIdUtils.java

@@ -0,0 +1,213 @@
+package com.wdkl.ncs.android.component.home.util;
+
+import android.annotation.SuppressLint;
+import android.content.ContentResolver;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Environment;
+import android.provider.MediaStore;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.util.Log;
+
+import com.wdkl.ncs.keepbackground.utils.RandomUtil;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.util.Date;
+import java.util.UUID;
+
+/**
+ * jason
+ */
+public final class DeviceIdUtils {
+    private static final String TAG = DeviceIdUtils.class.getSimpleName();
+
+    private static final String TEMP_DIR = "system_config";
+    private static final String TEMP_FILE_NAME = "system_file";
+    private static final String TEMP_FILE_NAME_MIME_TYPE = "application/octet-stream";
+    private static final String SP_NAME = "device_info";
+    private static final String SP_KEY_DEVICE_ID = "device_id";
+
+    public static String getDeviceId(Context context) {
+        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
+        String deviceId = sharedPreferences.getString(SP_KEY_DEVICE_ID, null);
+        if (!TextUtils.isEmpty(deviceId)) {
+            return deviceId;
+        }
+        deviceId = getIMEI(context);
+        if (TextUtils.isEmpty(deviceId)) {
+            deviceId = createUUID(context);
+        }
+        sharedPreferences.edit()
+                .putString(SP_KEY_DEVICE_ID, deviceId)
+                .apply();
+        return deviceId;
+    }
+
+    private static String createUUID(Context context) {
+        //String uuid = UUID.randomUUID().toString().replace("-", "");
+        String uuid = RandomUtil.getRandomNumbersAndLetters(16);
+
+        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
+            Uri externalContentUri = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
+            ContentResolver contentResolver = context.getContentResolver();
+            String[] projection = new String[]{
+                    MediaStore.Downloads._ID
+            };
+            String selection = MediaStore.Downloads.TITLE + "=?";
+            String[] args = new String[]{
+                    TEMP_FILE_NAME
+            };
+            Cursor query = contentResolver.query(externalContentUri, projection, selection, args, null);
+            if (query != null && query.moveToFirst()) {
+                Uri uri = ContentUris.withAppendedId(externalContentUri, query.getLong(0));
+                query.close();
+
+                InputStream inputStream = null;
+                BufferedReader bufferedReader = null;
+                try {
+                    inputStream = contentResolver.openInputStream(uri);
+                    if (inputStream != null) {
+                        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
+                        uuid = bufferedReader.readLine();
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                } finally {
+                    if (bufferedReader != null) {
+                        try {
+                            bufferedReader.close();
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                    if (inputStream != null) {
+                        try {
+                            inputStream.close();
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }
+            } else {
+                ContentValues contentValues = new ContentValues();
+                contentValues.put(MediaStore.Downloads.TITLE, TEMP_FILE_NAME);
+                contentValues.put(MediaStore.Downloads.MIME_TYPE, TEMP_FILE_NAME_MIME_TYPE);
+                contentValues.put(MediaStore.Downloads.DISPLAY_NAME, TEMP_FILE_NAME);
+                contentValues.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + File.separator + TEMP_DIR);
+
+                Uri insert = contentResolver.insert(externalContentUri, contentValues);
+                if (insert != null) {
+                    OutputStream outputStream = null;
+                    try {
+                        outputStream = contentResolver.openOutputStream(insert);
+                        if (outputStream == null) {
+                            return uuid;
+                        }
+                        outputStream.write(uuid.getBytes());
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    } finally {
+                        if (outputStream != null) {
+                            try {
+                                outputStream.close();
+                            } catch (IOException e) {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                }
+            }
+        } else {
+            File externalDownloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
+            File applicationFileDir = new File(externalDownloadsDir, TEMP_DIR);
+            if (!applicationFileDir.exists()) {
+                if (!applicationFileDir.mkdirs()) {
+                    Log.e(TAG, "文件夹创建失败: " + applicationFileDir.getPath());
+                }
+            }
+            File file = new File(applicationFileDir, TEMP_FILE_NAME);
+            if (!file.exists()) {
+                FileWriter fileWriter = null;
+                try {
+                    if (file.createNewFile()) {
+                        fileWriter = new FileWriter(file, false);
+                        fileWriter.write(uuid);
+                    } else {
+                        Log.e(TAG, "文件创建失败:" + file.getPath());
+                    }
+                } catch (IOException e) {
+                    Log.e(TAG, "文件创建失败:" + file.getPath());
+                    e.printStackTrace();
+                } finally {
+                    if (fileWriter != null) {
+                        try {
+                            fileWriter.close();
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }
+            } else {
+                FileReader fileReader = null;
+                BufferedReader bufferedReader = null;
+                try {
+                    fileReader = new FileReader(file);
+                    bufferedReader = new BufferedReader(fileReader);
+                    uuid = bufferedReader.readLine();
+
+                    bufferedReader.close();
+                    fileReader.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                } finally {
+                    if (bufferedReader != null) {
+                        try {
+                            bufferedReader.close();
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
+
+                    if (fileReader != null) {
+                        try {
+                            fileReader.close();
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }
+            }
+        }
+
+        return uuid;
+    }
+
+    private static String getIMEI(Context context) {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+            return null;
+        }
+        try {
+            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
+            if (telephonyManager == null) {
+                return null;
+            }
+            @SuppressLint({"MissingPermission", "HardwareIds"}) String imei = telephonyManager.getDeviceId();
+            return imei;
+        } catch (Exception e) {
+            return null;
+        }
+    }
+}