Skip to content

Commit

Permalink
add TaskListener
Browse files Browse the repository at this point in the history
  • Loading branch information
huanli233 committed Mar 25, 2024
1 parent a66d8ea commit 8b97ba9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<url>http://github.com/huanli233/MagiskPatcher4J</url>

<properties>
<!--gpg.keyname>CF1C8E37</gpg.keyname-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/huanli233/magiskpatcher/listener/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.huanli233.magiskpatcher.listener;

public enum Task {
UNPACK_BOOT(), CHECK_RAMDISK(), PATCH_RAMDISK(), PATCH_KERNEL(), REPACK_BOOT();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.huanli233.magiskpatcher.listener;

public interface TaskListener {
public void onTaskNext(Task task);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.OutputStream;
import java.security.MessageDigest;
import com.huanli233.magiskpatcher.exception.InitializationFailedException;
import com.huanli233.magiskpatcher.listener.Task;
import com.huanli233.magiskpatcher.listener.TaskListener;
import com.huanli233.magiskpatcher.log.DefaultLogger;
import com.huanli233.magiskpatcher.log.ILogger;
import com.huanli233.magiskpatcher.utils.CmdUtil.ExecResult;
Expand Down Expand Up @@ -51,6 +53,11 @@ public class MagiskPatcher {
*/
private ILogger logger;

/**
* Listener
*/
private TaskListener listener;

/**
* @param fileTempPath magiskboot及其他文件的临时存储路径
* @throws InitializationFailedException 初始化失败
Expand Down Expand Up @@ -129,6 +136,20 @@ public void setArch(Arch arch) {
this.arch = arch;
}

/**
* @return listener
*/
public TaskListener getListener() {
return listener;
}

/**
* @param listener 要设置的 listener
*/
public void setListener(TaskListener listener) {
this.listener = listener;
}

/**
* 执行Magiskboot命令
* @return 命令结果
Expand Down Expand Up @@ -281,6 +302,12 @@ private String grepProp(String key, String file) {
return "";
}

private void updateTask(Task task) {
if (this.listener != null) {
this.listener.onTaskNext(task);
}
}

/**
* 修补boot文件
* @param bootimg 要修补的boot文件
Expand Down Expand Up @@ -317,6 +344,7 @@ public int patch(File bootimg, File result) {

// 解包boot
logger.info("- 解包 boot 文件");
updateTask(Task.UNPACK_BOOT);
rt = execCmd("unpack", bootimg.getAbsolutePath()).getExitCode();
switch (rt) {
case 0:
Expand All @@ -335,6 +363,7 @@ public int patch(File bootimg, File result) {

// check ramdisk
logger.info("- 检查 ramdisk 状态");
updateTask(Task.CHECK_RAMDISK);
int status;
String skipBackup;
if (isFile("ramdisk.cpio")) {
Expand Down Expand Up @@ -377,6 +406,7 @@ public int patch(File bootimg, File result) {
}

logger.info("- 修补 ramdisk");
updateTask(Task.PATCH_RAMDISK);
String skip32 = "#";
String skip64 = "#";
if (isFile("magisk64")) {
Expand Down Expand Up @@ -440,6 +470,7 @@ public int patch(File bootimg, File result) {
}
}

updateTask(Task.PATCH_KERNEL);
if (isFile("kernel")) {
boolean patchedKernel = false;
rt = execCmd("hexpatch", "kernel",
Expand All @@ -460,6 +491,7 @@ public int patch(File bootimg, File result) {
}

logger.info("- 打包 boot 镜像");
updateTask(Task.REPACK_BOOT);
rt = execCmd("repack", bootimg.getAbsolutePath()).getExitCode();
if (rt != 0) {
logger.err("! 无法打包 boot 镜像");
Expand Down

0 comments on commit 8b97ba9

Please sign in to comment.