Skip to content

Commit

Permalink
新增 PackagesUtils & BitmapUtils 类,删除冗余功能,新增observeCall方法
Browse files Browse the repository at this point in the history
  • Loading branch information
HChenX committed Jul 19, 2024
1 parent 8728a24 commit 5358973
Show file tree
Hide file tree
Showing 19 changed files with 500 additions and 202 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencyResolutionManagement {

```groovy
dependencies {
implementation 'com.github.HChenX:HookTool:v.0.9.8'
implementation 'com.github.HChenX:HookTool:v.0.9.9'
}
```

Expand All @@ -59,7 +59,7 @@ public void initZygote(IXposedHookZygoteInit.StartupParam startupParam) {

@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) {
HCInit.initOther(/* 你模块的包名 */, /* tag */, /* 日志等级 */); // 必须,tip:建议放在第一位
HCInit.initBasicData(/* 你模块的包名 */, /* tag */, /* 日志等级 */); // 必须,tip:建议放在第一位
HCInit.initLoadPackageParam(lpparam); // 必须
}
```
Expand All @@ -70,7 +70,7 @@ public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) {
public static class MainActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
HCInit.initOther(/* 你模块的包名 */, /* tag */, /* 日志等级 */); // 必须
HCInit.initBasicData(/* 你模块的包名 */, /* tag */, /* 日志等级 */); // 必须
}
}
```
Expand Down Expand Up @@ -202,7 +202,6 @@ public class MainTest extends BaseHC {
public void before() throws Throwable {
// hook 方法所属的类
Class<?> c = mClass;

Context context = thisObject();
String string = first();
second(1);
Expand All @@ -211,7 +210,6 @@ public class MainTest extends BaseHC {
setThisField("demo", 1);
callThisMethod("method");
getThisField("test");

// 非静态本类外
Object o = null;
setField(o, "demo", 1);
Expand All @@ -223,9 +221,13 @@ public class MainTest extends BaseHC {
callStaticMethod("com.demo.Main", "callStatic", new Object[]{thisObject(), second()});
int i = getStaticField("com.demo.Main", "field");
setStaticField("com.demo.Main", "test", true);

// 你可调用此方法,使得挂钩自己失效
removeSelf();
// 观察调用
observeCall();
// 获取堆栈
getStackTrace();
}
};
}
Expand Down Expand Up @@ -360,8 +362,8 @@ public class MainTest extends BaseHC {
s = prefs("myPrefs").getString("test", "1"); // 可指定读取文件名
Context context = null;
// nativePrefs() 即可切换为原生模式,配置会保存到寄生应用的私有目录,读取也会从寄生应用私有目录读取。
nativePrefs().prefs(context).editor().putString("test", "1").commit();
nativePrefs().prefs(context).editor().putString("test", "1").commit();

// 如果不方便获取 context 可用使用此方法,异步获取寄生应用 context,再设置。
asynPrefs(new PrefsTool.IAsynPrefs() {
@Override
Expand All @@ -383,7 +385,7 @@ public class MainTest {
// 读取,写入同理。
Context context = null;
prefs(context).editor().putString("test", "1").commit();
prefs(context,"myPrefs").editor().putString("test", "1").commit();
prefs(context, "myPrefs").editor().putString("test", "1").commit();
}
}

Expand All @@ -409,6 +411,14 @@ public class MainTest {

----

- PackagesUtils 类:
- 快速获取软件包信息!

----

- BitmapUtils 类:
- Drawable 转 Bitmap。

- 其他更多精彩正在加载···

# 💕工具使用者
Expand Down
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.0.9.8'
int defVersionCode = 2024071600
def defVersion = 'v.0.9.9'
int defVersionCode = 2024071900

group = 'com.github.HChenX'
version = defVersion
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/hchen/hooktool/BaseHC.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ final public IPrefs prefs(Context context, String prefsName) {
return prefs.prefs(context, prefsName);
}

final public void asyncPrefs(PrefsTool.IAsyncPrefs asynPrefs) {
prefs.asyncPrefs(asynPrefs);
final public void asyncPrefs(PrefsTool.IAsyncPrefs asyncPrefs) {
prefs.asyncPrefs(asyncPrefs);
}

final public PrefsTool nativePrefs() {
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/com/hchen/hooktool/HCInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,24 @@ public static void initStartupParam(IXposedHookZygoteInit.StartupParam startupPa
/**
* 务必设置!
*/
public static void initOther(String modulePackageName, String tag, @Duration int level) {
public static void initBasicData(String modulePackageName, String tag, @Duration int level) {
setTag(tag); /* 设置 TAG */
ToolData.mInitLogLevel = level; /* 设置日志等级 */
ToolData.modulePackageName = modulePackageName; /* 设置模块包名 */
}

/**
* 是否允许使用系统的 classloader,一般不需要开启。
*/
public static void canUseSystemClassLoader(boolean use) {
canUseSystemClassLoader = use; /* 允许使用系统 classloader */
}

public static void logFilter(boolean use, String[] filter) {
ToolData.useFieldObserver = use; /* 使用全局日志过滤 */
ToolData.filter = filter; /* 过滤规则 */
}

public static void filedObserver(boolean use) {
ToolData.useFieldObserver = use; /* 使用字段设置观察 */
/**
* 是否自动对每个被 hook 的方法,在其被调用时打印日志。
*/
public static void autoObserveCall(boolean auto) {
ToolData.autoObserveCall = auto;
}
// ---------- END!----------

Expand All @@ -108,7 +109,6 @@ private static void setTag(String tag) {
ToolData.spareTag = tag;
}


protected static XC_LoadPackage.LoadPackageParam getLoadPackageParam() {
if (lpparam != null) return lpparam;
throw new RuntimeException(ToolData.mInitTag + "[E]: failed to obtain LoadPackageParam, it is null!");
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/hchen/hooktool/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

/**
* 测试和示例类
*
* @hide
*/
public class MainTest extends BaseHC {

Expand Down Expand Up @@ -98,7 +100,6 @@ public void before() throws Throwable {
setThisField("demo", 1);
callThisMethod("method");
getThisField("test");

// 非静态本类外
Object o = null;
setField(o, "demo", 1);
Expand All @@ -113,6 +114,10 @@ public void before() throws Throwable {

// 移除自身
removeSelf();
// 观察调用
observeCall();
// 获取堆栈
getStackTrace();
}
};
}
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/com/hchen/hooktool/data/AppData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of HookTool.
* HookTool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* Copyright (C) 2023-2024 HookTool Contributions
*/
package com.hchen.hooktool.data;

import android.graphics.Bitmap;

/**
* App 数据类
*/
public class AppData {
public int user = 0; /* user id*/
public int uid = -1; /* uid */
public Bitmap icon; /* 图标 */
public String label; /* 应用名 */
public String packageName; /* 包名 */
public String activityName; /* 活动名 */
public String versionName; /* 版本名 */
public String versionCode; /* 版本号 */
public boolean isSystemApp; /* 是否为系统应用 */
public boolean enabled; /* 是否启用 */
}
1 change: 1 addition & 0 deletions app/src/main/java/com/hchen/hooktool/data/ChainData.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

/**
* 链式调用数据
* @hide
*/
public class ChainData {
public ArrayList<Member> members = new ArrayList<>(); /*目标成员组*/
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/hchen/hooktool/data/StateEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

/**
* 状态枚举
* @hide
*/
public enum StateEnum {
NONE, /*未 hook*/
Expand Down
29 changes: 10 additions & 19 deletions app/src/main/java/com/hchen/hooktool/tool/ActionTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.hchen.hooktool.callback.IAction;
import com.hchen.hooktool.data.ChainData;
import com.hchen.hooktool.data.StateEnum;
import com.hchen.hooktool.utils.LogExpand;
import com.hchen.hooktool.utils.ToolData;

import java.lang.reflect.Member;
Expand All @@ -38,6 +37,8 @@

/**
* Hook 执行
*
* @hide
*/
public class ActionTool {
private final ToolData data;
Expand Down Expand Up @@ -78,7 +79,7 @@ protected void doAction(ChainTool chain) {
"type: [" + data.mType + "]. member is null, will skip hook!");
continue;
}
XposedBridge.hookMethod(m, createHook(m, data.iAction));
XposedBridge.hookMethod(m, createHook(data.iAction));
logD(this.data.getTAG(), "success to hook: " + m);
}
data.stateEnum = StateEnum.HOOKED;
Expand All @@ -99,12 +100,14 @@ protected void doAction(ChainTool chain) {
}
}

protected Action createHook(Member member, IAction iAction) {
protected Action createHook(IAction iAction) {
iAction.putUtils(data);
return new Action(member, data.getTAG()) {
return new Action(data.getTAG()) {
@Override
protected void before(MethodHookParam param) throws Throwable {
iAction.putMethodHookParam(param);
if (ToolData.autoObserveCall)
iAction.observeCall();
iAction.before();
}

Expand All @@ -122,9 +125,7 @@ void putThis(XC_MethodHook xcMethodHook) {
}

protected abstract static class Action extends XC_MethodHook {
private String TAG = null;
private LogExpand logExpand = null;
private boolean useLogExpand = false;
private final String TAG;

protected void before(MethodHookParam param) throws Throwable {
}
Expand All @@ -134,32 +135,22 @@ protected void after(MethodHookParam param) throws Throwable {

abstract void putThis(XC_MethodHook xcMethodHook);

public Action(Member member, String tag) {
public Action(String tag) {
super();
TAG = tag;
putThis(this);
this.useLogExpand = ToolData.useLogExpand;
if (useLogExpand) this.logExpand = new LogExpand(member, TAG);
}

public Action(Member member, String tag, int priority) {
public Action(String tag, int priority) {
super(priority);
TAG = tag;
putThis(this);
this.useLogExpand = ToolData.useLogExpand;
if (useLogExpand) this.logExpand = new LogExpand(member, TAG);
}

@Override
protected void beforeHookedMethod(MethodHookParam param) {
try {
before(param);
if (useLogExpand) {
if (logExpand != null) {
logExpand.setParam(param);
logExpand.detailedLogs();
}
}
} catch (Throwable e) {
logE(TAG + ":" + "before", e);
}
Expand Down
Loading

0 comments on commit 5358973

Please sign in to comment.