From dea2fa8e1300dc8a023681a49e7a4c56c506219d Mon Sep 17 00:00:00 2001 From: kooritea Date: Mon, 10 May 2021 17:19:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A2=91=E7=B9=81=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E9=80=A0=E6=88=90?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BD=8E=E4=B8=8B=E9=97=AE=E9=A2=98=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 +- .../com/kooritea/fcmfix/MainActivity.java | 9 +++- .../kooritea/fcmfix/xposed/BroadcastFix.java | 38 ++++++++++++-- .../xposed/MiuiLocalNotificationFix.java | 44 +++++++++++++--- .../fcmfix/xposed/ReconnectManagerFix.java | 37 +++----------- .../kooritea/fcmfix/xposed/XposedModule.java | 50 ++++++++++++++++++- 6 files changed, 136 insertions(+), 46 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5f789e0..433b197 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "com.kooritea.fcmfix" minSdkVersion 29 targetSdkVersion 29 - versionCode 9 - versionName "0.1.2" + versionCode 10 + versionName "0.2.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/kooritea/fcmfix/MainActivity.java b/app/src/main/java/com/kooritea/fcmfix/MainActivity.java index 6b6b849..9f84a68 100644 --- a/app/src/main/java/com/kooritea/fcmfix/MainActivity.java +++ b/app/src/main/java/com/kooritea/fcmfix/MainActivity.java @@ -7,6 +7,7 @@ import android.content.ContentProvider; import android.content.ContentResolver; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.content.pm.PackageInfo; @@ -172,12 +173,16 @@ public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) private void addAppInAllowList(String packageName){ this.allowList.add(packageName); - this.sharedPreferencesEditor.putStringSet("allowList",this.allowList); - this.sharedPreferencesEditor.commit(); + this.updateAllowList(); } private void deleteAppInAllowList(String packageName){ this.allowList.remove(packageName); + this.updateAllowList(); + } + + private void updateAllowList(){ this.sharedPreferencesEditor.putStringSet("allowList",this.allowList); this.sharedPreferencesEditor.commit(); + this.sendBroadcast(new Intent("com.kooritea.fcmfix.update.config")); } } \ No newline at end of file diff --git a/app/src/main/java/com/kooritea/fcmfix/xposed/BroadcastFix.java b/app/src/main/java/com/kooritea/fcmfix/xposed/BroadcastFix.java index 5e1c0c8..a5f1af9 100644 --- a/app/src/main/java/com/kooritea/fcmfix/xposed/BroadcastFix.java +++ b/app/src/main/java/com/kooritea/fcmfix/xposed/BroadcastFix.java @@ -1,8 +1,11 @@ package com.kooritea.fcmfix.xposed; import android.app.AndroidAppHelper; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; +import android.content.IntentFilter; import android.os.Build; import com.kooritea.fcmfix.util.ContentProviderHelper; @@ -19,14 +22,19 @@ public class BroadcastFix extends XposedModule { - protected ContentProviderHelper contentProviderHelper; + private Set allowList = null; public BroadcastFix(XC_LoadPackage.LoadPackageParam loadPackageParam) { super(loadPackageParam); - contentProviderHelper = new ContentProviderHelper(AndroidAppHelper.currentApplication().getApplicationContext(),"content://com.kooritea.fcmfix.provider/config"); this.startHook(); } + @Override + protected void onCanReadConfig() throws Exception { + this.onUpdateConfig(); + this.initUpdateConfigReceiver(); + } + protected void startHook(){ Class clazz = XposedHelpers.findClass("com.android.server.am.ActivityManagerService",loadPackageParam.classLoader); final Method[] declareMethods = clazz.getDeclaredMethods(); @@ -74,9 +82,11 @@ protected void beforeHookedMethod(MethodHookParam methodHookParam) throws Throwa } private boolean targetIsAllow(String packageName){ - if (!packageName.equals("com.tencent.mm")) { - Set allowList = this.contentProviderHelper.getStringSet("allowList"); - for (String item : allowList) { + if(this.allowList == null){ + this.checkUserDeviceUnlock(AndroidAppHelper.currentApplication().getApplicationContext()); + } + if(this.allowList != null){ + for (String item : this.allowList) { if (item.equals(packageName)) { return true; } @@ -84,4 +94,22 @@ private boolean targetIsAllow(String packageName){ } return false; } + + private void onUpdateConfig(){ + ContentProviderHelper contentProviderHelper = new ContentProviderHelper(AndroidAppHelper.currentApplication().getApplicationContext(),"content://com.kooritea.fcmfix.provider/config"); + this.allowList = contentProviderHelper.getStringSet("allowList"); + } + + private void initUpdateConfigReceiver(){ + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction("com.kooritea.fcmfix.update.config"); + AndroidAppHelper.currentApplication().getApplicationContext().registerReceiver(new BroadcastReceiver() { + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if ("com.kooritea.fcmfix.update.config".equals(action)) { + onUpdateConfig(); + } + } + }, intentFilter); + } } diff --git a/app/src/main/java/com/kooritea/fcmfix/xposed/MiuiLocalNotificationFix.java b/app/src/main/java/com/kooritea/fcmfix/xposed/MiuiLocalNotificationFix.java index b3310ba..7d94712 100644 --- a/app/src/main/java/com/kooritea/fcmfix/xposed/MiuiLocalNotificationFix.java +++ b/app/src/main/java/com/kooritea/fcmfix/xposed/MiuiLocalNotificationFix.java @@ -1,12 +1,17 @@ package com.kooritea.fcmfix.xposed; import android.app.AndroidAppHelper; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Build; +import android.util.ArraySet; import com.kooritea.fcmfix.util.ContentProviderHelper; import java.lang.reflect.Method; +import java.util.HashSet; import java.util.Set; import de.robv.android.xposed.XC_MethodHook; @@ -16,14 +21,19 @@ public class MiuiLocalNotificationFix extends XposedModule { - protected ContentProviderHelper contentProviderHelper; + private Set allowList = null; public MiuiLocalNotificationFix(XC_LoadPackage.LoadPackageParam loadPackageParam) { super(loadPackageParam); - contentProviderHelper = new ContentProviderHelper(AndroidAppHelper.currentApplication().getApplicationContext(),"content://com.kooritea.fcmfix.provider/config"); this.startHook(); } + @Override + protected void onCanReadConfig() throws Exception { + this.onUpdateConfig(); + this.initUpdateConfigReceiver(); + } + protected void startHook(){ Class clazz = XposedHelpers.findClass("com.android.server.notification.NotificationManagerServiceInjector",loadPackageParam.classLoader); final Method[] declareMethods = clazz.getDeclaredMethods(); @@ -50,12 +60,34 @@ protected void afterHookedMethod(MethodHookParam methodHookParam) throws Throwab } private boolean targetIsAllow(String packageName){ - Set allowList = this.contentProviderHelper.getStringSet("allowList"); - for (String item : allowList) { - if (item.equals(packageName)) { - return true; + if(this.allowList == null){ + this.checkUserDeviceUnlock(AndroidAppHelper.currentApplication().getApplicationContext()); + } + if(this.allowList != null){ + for (String item : this.allowList) { + if (item.equals(packageName)) { + return true; + } } } return false; } + + private void onUpdateConfig(){ + ContentProviderHelper contentProviderHelper = new ContentProviderHelper(AndroidAppHelper.currentApplication().getApplicationContext(),"content://com.kooritea.fcmfix.provider/config"); + this.allowList = contentProviderHelper.getStringSet("allowList"); + } + + private void initUpdateConfigReceiver(){ + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction("com.kooritea.fcmfix.update.config"); + AndroidAppHelper.currentApplication().getApplicationContext().registerReceiver(new BroadcastReceiver() { + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if ("com.kooritea.fcmfix.update.config".equals(action)) { + onUpdateConfig(); + } + } + }, intentFilter); + } } diff --git a/app/src/main/java/com/kooritea/fcmfix/xposed/ReconnectManagerFix.java b/app/src/main/java/com/kooritea/fcmfix/xposed/ReconnectManagerFix.java index 5c2358f..e3fe93a 100644 --- a/app/src/main/java/com/kooritea/fcmfix/xposed/ReconnectManagerFix.java +++ b/app/src/main/java/com/kooritea/fcmfix/xposed/ReconnectManagerFix.java @@ -59,6 +59,11 @@ public ReconnectManagerFix(XC_LoadPackage.LoadPackageParam loadPackageParam) { this.startHookGcmServiceStart(); } + @Override + protected void onCanReadConfig() throws Exception { + this.startHook(); + } + private void startHookGcmServiceStart() { this.GcmChimeraService = XposedHelpers.findClass("com.google.android.gms.gcm.GcmChimeraService", loadPackageParam.classLoader); try{ @@ -76,18 +81,7 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.kooritea.fcmfix.log"); AndroidAppHelper.currentApplication().getApplicationContext().registerReceiver(logBroadcastReceive, intentFilter); - if (AndroidAppHelper.currentApplication().getApplicationContext().getSystemService(UserManager.class).isUserUnlocked()) { - try { - startHook(); - printLog("ReconnectManagerFixStartHook 完成"); - } catch (Exception e) { - printLog("ReconnectManagerFixStartHook 初始化失败: " + e.getMessage()); - } - } else { - IntentFilter userUnlockIntentFilter = new IntentFilter(); - userUnlockIntentFilter.addAction(Intent.ACTION_USER_UNLOCKED); - AndroidAppHelper.currentApplication().getApplicationContext().registerReceiver(unlockBroadcastReceive, userUnlockIntentFilter); - } + checkUserDeviceUnlock(AndroidAppHelper.currentApplication().getApplicationContext()); } }); XposedHelpers.findAndHookMethod(this.GcmChimeraService, "onDestroy", new XC_MethodHook() { @@ -101,7 +95,7 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable } } - private void startHook() throws Exception { + protected void startHook() throws Exception { Context context = AndroidAppHelper.currentApplication().getApplicationContext(); final SharedPreferences sharedPreferences = context.getSharedPreferences("fcmfix_config", Context.MODE_PRIVATE); String versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; @@ -121,7 +115,7 @@ private void startHook() throws Exception { return; } if (!sharedPreferences.getBoolean("enable", false)) { - this.printLog("ReconnectManagerFix配置已关闭"); + this.printLog("ReconnectManagerFix配置文件enable标识为false,退出"); return; } if (!sharedPreferences.getString("gms_version", "").equals(versionName)) { @@ -209,19 +203,4 @@ public void onReceive(Context context, Intent intent) { } }; - private BroadcastReceiver unlockBroadcastReceive = new BroadcastReceiver() { - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - if (Intent.ACTION_USER_UNLOCKED.equals(action)) { - printLog("User Device Unlock Broadcast"); - try { - startHook(); - printLog("ReconnectManagerFixStartHook 完成"); - AndroidAppHelper.currentApplication().getApplicationContext().unregisterReceiver(unlockBroadcastReceive); - } catch (Exception e) { - printLog("ReconnectManagerFixStartHook 初始化失败: " + e.getMessage()); - } - } - } - }; } diff --git a/app/src/main/java/com/kooritea/fcmfix/xposed/XposedModule.java b/app/src/main/java/com/kooritea/fcmfix/xposed/XposedModule.java index 41beef5..31e0382 100644 --- a/app/src/main/java/com/kooritea/fcmfix/xposed/XposedModule.java +++ b/app/src/main/java/com/kooritea/fcmfix/xposed/XposedModule.java @@ -1,7 +1,11 @@ package com.kooritea.fcmfix.xposed; import android.app.AndroidAppHelper; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.os.UserManager; import android.util.Log; import com.kooritea.fcmfix.util.ContentProviderHelper; @@ -9,7 +13,7 @@ import de.robv.android.xposed.XposedBridge; import de.robv.android.xposed.callbacks.XC_LoadPackage; -public class XposedModule { +public abstract class XposedModule { protected XC_LoadPackage.LoadPackageParam loadPackageParam; @@ -17,6 +21,9 @@ protected XposedModule(final XC_LoadPackage.LoadPackageParam loadPackageParam){ this.loadPackageParam = loadPackageParam; } + protected abstract void onCanReadConfig() throws Exception; + private boolean isRegisterUnlockBroadcastReceive = false; + protected void printLog(String text){ XposedBridge.log("[fcmfix] "+ text); Intent log = new Intent("com.kooritea.fcmfix.log"); @@ -24,8 +31,47 @@ protected void printLog(String text){ try{ AndroidAppHelper.currentApplication().getApplicationContext().sendBroadcast(log); }catch (Exception e){ - e.printStackTrace();; + // 没有作用域 +// e.printStackTrace();; + } + } + + /** + * 多次调用也仅调用一次onCanReadConfig + * @param context + */ + protected void checkUserDeviceUnlock(Context context){ + if(!isRegisterUnlockBroadcastReceive){ + if (context.getSystemService(UserManager.class).isUserUnlocked()) { + try { + this.onCanReadConfig(); + printLog("startHook"); + } catch (Exception e) { + printLog("读取配置文件初始化失败: " + e.getMessage()); + } + } else { + isRegisterUnlockBroadcastReceive = true; + IntentFilter userUnlockIntentFilter = new IntentFilter(); + userUnlockIntentFilter.addAction(Intent.ACTION_USER_UNLOCKED); + AndroidAppHelper.currentApplication().getApplicationContext().registerReceiver(unlockBroadcastReceive, userUnlockIntentFilter); + } } } + + private BroadcastReceiver unlockBroadcastReceive = new BroadcastReceiver() { + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (Intent.ACTION_USER_UNLOCKED.equals(action)) { + printLog("User Device Unlock Broadcast"); + try { + onCanReadConfig(); + AndroidAppHelper.currentApplication().getApplicationContext().unregisterReceiver(unlockBroadcastReceive); + isRegisterUnlockBroadcastReceive = false; + } catch (Exception e) { + printLog("读取配置文件初始化失败: " + e.getMessage()); + } + } + } + }; }