Skip to content

Commit

Permalink
修复频繁读取配置文件造成性能低下问题 #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kooritea committed May 10, 2021
1 parent 0742ff3 commit dea2fa8
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 46 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/kooritea/fcmfix/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
}
38 changes: 33 additions & 5 deletions app/src/main/java/com/kooritea/fcmfix/xposed/BroadcastFix.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,14 +22,19 @@

public class BroadcastFix extends XposedModule {

protected ContentProviderHelper contentProviderHelper;
private Set<String> 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();
Expand Down Expand Up @@ -74,14 +82,34 @@ protected void beforeHookedMethod(MethodHookParam methodHookParam) throws Throwa
}

private boolean targetIsAllow(String packageName){
if (!packageName.equals("com.tencent.mm")) {
Set<String> 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;
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,14 +21,19 @@

public class MiuiLocalNotificationFix extends XposedModule {

protected ContentProviderHelper contentProviderHelper;
private Set<String> 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();
Expand All @@ -50,12 +60,34 @@ protected void afterHookedMethod(MethodHookParam methodHookParam) throws Throwab
}

private boolean targetIsAllow(String packageName){
Set<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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());
}
}
}
};
}
50 changes: 48 additions & 2 deletions app/src/main/java/com/kooritea/fcmfix/xposed/XposedModule.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,77 @@
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;

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;

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");
log.putExtra("text",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());
}
}
}
};
}

0 comments on commit dea2fa8

Please sign in to comment.