From 38bf44604c12f4e0df0a94b440440274ffa90234 Mon Sep 17 00:00:00 2001 From: weifengzz Date: Thu, 21 Mar 2019 20:20:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9MIUIUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wonday/aliyun/push/AliyunPushModule.java | 6 ++-- .../org/wonday/aliyun/push/MIUIUtils.java | 21 +++++------- .../wonday/aliyun/push/SystemProperty.java | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 android/src/main/java/org/wonday/aliyun/push/SystemProperty.java diff --git a/android/src/main/java/org/wonday/aliyun/push/AliyunPushModule.java b/android/src/main/java/org/wonday/aliyun/push/AliyunPushModule.java index fea06bb..c9c64e1 100644 --- a/android/src/main/java/org/wonday/aliyun/push/AliyunPushModule.java +++ b/android/src/main/java/org/wonday/aliyun/push/AliyunPushModule.java @@ -91,7 +91,7 @@ public void getDeviceId(final Promise promise) { @ReactMethod public void setApplicationIconBadgeNumber(int badgeNumber, final Promise promise) { - if (MIUIUtils.isMIUI()) { //小米特殊处理 + if (MIUIUtils.isMIUI(getReactApplicationContext())) { //小米特殊处理 FLog.d(ReactConstants.TAG, "setApplicationIconBadgeNumber for xiaomi"); if (badgeNumber==0) { @@ -260,7 +260,7 @@ public void onHostResume() { public void onHostPause() { //小米特殊处理, 处于后台时更新角标, 否则会被系统清除,看不到 - if (MIUIUtils.isMIUI()) { + if (MIUIUtils.isMIUI(getReactApplicationContext())) { FLog.d(ReactConstants.TAG, "onHostPause:setBadgeNumber for xiaomi"); MIUIUtils.setBadgeNumber(this.context, getCurrentActivity().getClass(), badgeNumber); } @@ -271,7 +271,7 @@ public void onHostPause() { public void onHostDestroy() { //小米特殊处理, 处于后台时更新角标, 否则会被系统清除,看不到 - if (MIUIUtils.isMIUI()) { + if (MIUIUtils.isMIUI(getReactApplicationContext())) { FLog.d(ReactConstants.TAG, "onHostDestroy:setBadgeNumber for xiaomi"); MIUIUtils.setBadgeNumber(this.context, getCurrentActivity().getClass(), badgeNumber); } diff --git a/android/src/main/java/org/wonday/aliyun/push/MIUIUtils.java b/android/src/main/java/org/wonday/aliyun/push/MIUIUtils.java index 94a8544..5c3cfb8 100644 --- a/android/src/main/java/org/wonday/aliyun/push/MIUIUtils.java +++ b/android/src/main/java/org/wonday/aliyun/push/MIUIUtils.java @@ -47,28 +47,23 @@ public class MIUIUtils { private static int notificationId = (int)(System.currentTimeMillis()/1000); - public static boolean isMIUI() { + public static boolean isMIUI(Context context) { if(hasChecked) { return isMIUI; } - - Properties prop= new Properties(); - try { - prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop"))); - } catch (IOException e) + SystemProperty sp = new SystemProperty(context); + if (sp.getOrThrow(KEY_MIUI_VERSION_CODE) != null || sp.getOrThrow(KEY_MIUI_VERSION_NAME) != null || sp.getOrThrow(KEY_MIUI_INTERNAL_STORAGE) != null) { + hasChecked = true; + isMIUI = true; + } + } catch (Exception e) { e.printStackTrace(); return false; } - - isMIUI= prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null - || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null - || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null; - - hasChecked = true; - + return isMIUI; } diff --git a/android/src/main/java/org/wonday/aliyun/push/SystemProperty.java b/android/src/main/java/org/wonday/aliyun/push/SystemProperty.java new file mode 100644 index 0000000..42d6059 --- /dev/null +++ b/android/src/main/java/org/wonday/aliyun/push/SystemProperty.java @@ -0,0 +1,34 @@ +package org.wonday.aliyun.push; + +import android.content.Context; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class SystemProperty { + private final Context mContext; + + public SystemProperty(Context mContext) { + this.mContext = mContext; + } + + public String getOrThrow(String key) throws Exception { + try { + ClassLoader classLoader = mContext.getClassLoader(); + Class SystemProperties = classLoader.loadClass("android.os.SystemProperties"); + Method methodGet = SystemProperties.getMethod("get", String.class); + return (String) methodGet.invoke(SystemProperties, key); + } catch (Exception e) { + throw new Exception(e); + } + } + + public String get(String key) { + try { + return getOrThrow(key); + } catch (Exception e) { + return null; + } + } + +}