Skip to content

Commit

Permalink
opt: 优化工具
Browse files Browse the repository at this point in the history
  • Loading branch information
HChenX committed Dec 15, 2024
1 parent d80d704 commit 124b407
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 84 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ plugins {
id 'maven-publish'
}

def defVersion = 'v.1.1.0'
int defVersionCode = 2024121301
def defVersion = 'v.1.1.1'
int defVersionCode = 2024121500

group = 'com.github.HChenX'
version = defVersion
Expand Down
61 changes: 58 additions & 3 deletions app/src/main/java/com/hchen/hooktool/BaseHC.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@

import static com.hchen.hooktool.log.XposedLog.logE;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;

import com.hchen.hooktool.hook.IHook;
import com.hchen.hooktool.tool.ChainTool;
import com.hchen.hooktool.tool.CoreTool;
import com.hchen.hooktool.tool.PrefsTool;
import com.hchen.hooktool.tool.additional.ResInjectTool;
import com.hchen.hooktool.tool.itool.IAsyncPrefs;
import com.hchen.hooktool.tool.itool.IPrefsApply;

import java.util.ArrayList;
import java.util.List;

import de.robv.android.xposed.IXposedHookZygoteInit;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

Expand All @@ -40,6 +45,8 @@
*/
public abstract class BaseHC extends CoreTool {
public String TAG = getClass().getSimpleName(); // 快捷获取类的简单名称作为 TAG, 为了效果建议配置相应的混淆规则。
private static final List<BaseHC> mIApplications = new ArrayList<>();
private static boolean isFirstHookApplication = true;
private static final ChainTool mChainTool = new ChainTool();
public static XC_LoadPackage.LoadPackageParam lpparam; // onZygote 状态下为 null。
public static ClassLoader classLoader;
Expand All @@ -49,14 +56,14 @@ public abstract class BaseHC extends CoreTool {
* <p>
* Tip: 作为覆写使用,请勿直接调用!
*/
public abstract void init();
protected abstract void init();

/**
* 带 classLoader 的初始化。
* <p>
* Tip: 作为覆写使用,请勿直接调用!
*/
public void init(ClassLoader classLoader) {
protected void init(ClassLoader classLoader) {
}

/**
Expand All @@ -68,7 +75,19 @@ public void init(ClassLoader classLoader) {
* <p>
* Tip: 作为覆写使用,请勿直接调用!
*/
public void initZygote(IXposedHookZygoteInit.StartupParam startupParam) {
protected void initZygote(IXposedHookZygoteInit.StartupParam startupParam) {
}

/**
* Application Context 创建之前调用。
* */
protected void onApplicationBefore(Context context) {
}

/**
* Application Context 创建之后调用。
* */
protected void onApplicationAfter(Context context) {
}

// 请在 handleLoadPackage 阶段调用。
Expand All @@ -89,6 +108,13 @@ final public void onClassLoader(ClassLoader classLoader) {
}
}

final public BaseHC onApplicationCreate() {
if (!mIApplications.contains(this))
mIApplications.add(this);
initApplicationHook();
return this;
}

// 请在 initZygote 阶段调用。
final public void onZygote() {
try {
Expand Down Expand Up @@ -176,4 +202,33 @@ public static void setDensityReplacement(String pkg, String type, String name, f
public static void setObjectReplacement(String pkg, String type, String name, Object replacementResValue) {
ResInjectTool.setObjectReplacement(pkg, type, name, replacementResValue);
}

// ------------ Application Hook --------------
private static void initApplicationHook() {
if (!isFirstHookApplication) return;
hookMethod(Application.class, "attach", Context.class, new IHook() {
@Override
public void before() {
mIApplications.forEach(iApplication -> {
try {
iApplication.onApplicationBefore((Context) getArgs(0));
} catch (Throwable e) {
logE("Application", "Failed to call iApplication: " + iApplication, e);
}
});
}

@Override
public void after() {
mIApplications.forEach(iApplication -> {
try {
iApplication.onApplicationAfter((Context) getArgs(0));
} catch (Throwable e) {
logE("Application", "Failed to call iApplication: " + iApplication, e);
}
});
}
});
isFirstHookApplication = false;
}
}
47 changes: 0 additions & 47 deletions app/src/main/java/com/hchen/hooktool/HCEntrance.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@
*/
package com.hchen.hooktool;

import static com.hchen.hooktool.log.XposedLog.logE;

import android.app.Application;
import android.content.Context;

import com.hchen.hooktool.hook.IHook;
import com.hchen.hooktool.tool.CoreTool;
import com.hchen.hooktool.tool.itool.IApplication;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;

import de.robv.android.xposed.IXposedHookLoadPackage;
Expand Down Expand Up @@ -58,13 +50,6 @@ public abstract class HCEntrance implements IXposedHookLoadPackage, IXposedHookZ
public void onInitZygote(StartupParam startupParam) throws Throwable {
}

/**
* Hook 应用 Application 类的 attach 方法,并获取 Context。
*/
public HashMap<String, IApplication[]> onApplication() {
return null;
}

/**
* 忽略的包名。
* <p>
Expand All @@ -86,10 +71,6 @@ public final void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) thr
initHCState();
}

HashMap<String, IApplication[]> iApplications = onApplication();
if (iApplications != null)
applicationHook(iApplications.get(lpparam.packageName));

onLoadPackage(lpparam);
}

Expand All @@ -115,32 +96,4 @@ private void initHCState() {
bridgeTag = "Unknown";
CoreTool.setStaticField(HCState.class, "mFramework", bridgeTag);
}

private void applicationHook(IApplication[] iApplications) {
if (iApplications == null) return;

CoreTool.hookMethod(Application.class, "attach", Context.class, new IHook() {
@Override
public void before() {
Arrays.stream(iApplications).forEach(iApplication -> {
try {
iApplication.before((Context) getArgs(0));
} catch (Throwable e) {
logE("Application", e);
}
});
}

@Override
public void after() {
Arrays.stream(iApplications).forEach(iApplication -> {
try {
iApplication.after((Context) getArgs(0));
} catch (Throwable e) {
logE("Application", e);
}
});
}
});
}
}
32 changes: 0 additions & 32 deletions app/src/main/java/com/hchen/hooktool/tool/itool/IApplication.java

This file was deleted.

0 comments on commit 124b407

Please sign in to comment.