Skip to content

Commit

Permalink
feat: 增加 sp,dp,px 转换 & 支持通过 enabled 控制是否启用 & 支持通过 onThrowable 监听崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
HChenX committed Jan 21, 2025
1 parent aab9f34 commit 13c82d7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.7'
int defVersionCode = 2025011300
def defVersion = 'v.1.1.8'
int defVersionCode = 2025012100

group = 'com.github.HChenX'
version = defVersion
Expand Down
27 changes: 24 additions & 3 deletions app/src/main/java/com/hchen/hooktool/BaseHC.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public abstract class BaseHC extends CoreTool {
public static XC_LoadPackage.LoadPackageParam lpparam; // onZygote 状态下为 null。
public static ClassLoader classLoader;

/**
* 是否启用
*/
protected boolean enabled() {
return true;
}

/**
* handleLoadPackage 阶段。
* <p>
Expand Down Expand Up @@ -91,26 +98,38 @@ protected void onApplicationBefore(Context context) {
protected void onApplicationAfter(Context context) {
}

/**
* 发生崩溃时回调,不要在这里继续执行 hook 逻辑。
*/
protected void onThrowable(Throwable throwable) {
}

// 请在 handleLoadPackage 阶段调用。
final public void onLoadPackage() {
try {
init();
if (enabled())
init();
} catch (Throwable e) {
onThrowable(e);
logE(TAG, "Waring! will stop hook process!!", e);
}
}

// 请传入自定义的 classLoader。
final public void onClassLoader(ClassLoader classLoader) {
try {
init(classLoader);
if (enabled())
init(classLoader);
} catch (Throwable e) {
onThrowable(e);
logE(TAG, "Waring! will stop hook process!!", e);
}
}

// Hook Application
final public BaseHC onApplicationCreate() {
if (!enabled()) return this;

if (!mIApplications.contains(this))
mIApplications.add(this);
initApplicationHook();
Expand All @@ -120,8 +139,10 @@ final public BaseHC onApplicationCreate() {
// 请在 initZygote 阶段调用。
final public void onZygote() {
try {
initZygote(HCData.getStartupParam());
if (enabled())
initZygote(HCData.getStartupParam());
} catch (Throwable e) {
onThrowable(e);
logE(TAG, "Waring! will stop hook process!!", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,31 @@ public static boolean isMiuiInternational() {
* 是否是神色模式。
*/
public static boolean isDarkMode(Resources resources) {
return (resources.getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
return (resources.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}

public static int px2dp(Context context, float pxValue) {
// 获取屏幕密度(每英寸多少个像素点)
float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

public static int px2sp(Context context, float pxValue) {
// 获取字体的缩放密度
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}

public static int dp2px(Context context, float dpValue) {
// 获取屏幕密度
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

public static int sp2px(Context context, float spValue) {
// 获取字体的缩放密度
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}

/**
Expand Down

0 comments on commit 13c82d7

Please sign in to comment.