diff --git a/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPAppComponentFactoryStub.java b/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPAppComponentFactoryStub.java
index d416d00..d7e9be2 100644
--- a/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPAppComponentFactoryStub.java
+++ b/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPAppComponentFactoryStub.java
@@ -13,8 +13,6 @@ import android.util.Log;
import org.lsposed.lspatch.util.FileUtils;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
@@ -39,24 +37,19 @@ public class LSPAppComponentFactoryStub extends AppComponentFactory {
* This method will be called at instantiateClassLoader by createOrUpdateClassLoaderLocked
**/
private void initOriginalAppComponentFactory(ApplicationInfo aInfo) {
- final String cacheApkPath = aInfo.dataDir + "/cache/origin_apk.bin";
+ final String originPath = aInfo.dataDir + "/cache/lspatch/origin/";
final String originalAppComponentFactoryClass = FileUtils.readTextFromInputStream(baseClassLoader.getResourceAsStream(ORIGINAL_APP_COMPONENT_FACTORY_ASSET_PATH));
try {
- File cacheApk = new File(cacheApkPath);
- if (cacheApk.exists()) {
- try (ZipFile sourceFile = new ZipFile(aInfo.sourceDir);
- InputStream is = new FileInputStream(cacheApk)) {
- var sourceCrc = sourceFile.getEntry(ORIGINAL_APK_ASSET_PATH).getCrc();
- var cacheCrc = FileUtils.calculateCrc(is);
- if (sourceCrc != cacheCrc) {
- Log.i(TAG, "Application updated, extract original apk again");
- cacheApk.delete();
- }
- }
+ String cacheApkPath;
+ try (ZipFile sourceFile = new ZipFile(aInfo.sourceDir)) {
+ cacheApkPath = originPath + sourceFile.getEntry(ORIGINAL_APK_ASSET_PATH).getCrc();
}
- if (!cacheApk.exists()) {
+ if (!Files.exists(Paths.get(cacheApkPath))) {
+ Log.i(TAG, "extract original apk");
+ FileUtils.deleteFolderIfExists(Paths.get(originPath));
+ Files.createDirectories(Paths.get(originPath));
try (InputStream is = baseClassLoader.getResourceAsStream(ORIGINAL_APK_ASSET_PATH)) {
Files.copy(is, Paths.get(cacheApkPath));
}
@@ -68,11 +61,11 @@ public class LSPAppComponentFactoryStub extends AppComponentFactory {
originalAppComponentFactory = (AppComponentFactory) appClassLoader.loadClass(originalAppComponentFactoryClass).newInstance();
} catch (ClassNotFoundException | NullPointerException ignored) {
if (originalAppComponentFactoryClass != null && !originalAppComponentFactoryClass.isEmpty())
- Log.w(TAG, "Original AppComponentFactory not found");
+ Log.w(TAG, "original AppComponentFactory not found");
originalAppComponentFactory = new AppComponentFactory();
}
- Log.d(TAG, "Instantiate original AppComponentFactory: " + originalAppComponentFactory);
+ Log.d(TAG, "instantiate original AppComponentFactory: " + originalAppComponentFactory);
} catch (Throwable e) {
Log.e(TAG, "initOriginalAppComponentFactory", e);
}
diff --git a/appstub/src/main/java/org/lsposed/lspatch/util/FileUtils.java b/appstub/src/main/java/org/lsposed/lspatch/util/FileUtils.java
index 99e43ab..0c05879 100644
--- a/appstub/src/main/java/org/lsposed/lspatch/util/FileUtils.java
+++ b/appstub/src/main/java/org/lsposed/lspatch/util/FileUtils.java
@@ -5,7 +5,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
-import java.util.zip.CRC32;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
public class FileUtils {
@@ -22,13 +26,26 @@ public class FileUtils {
return null;
}
- public static long calculateCrc(InputStream is) throws IOException {
- CRC32 crcMaker = new CRC32();
- byte[] buffer = new byte[65536];
- int bytesRead;
- while((bytesRead = is.read(buffer)) != -1) {
- crcMaker.update(buffer, 0, bytesRead);
- }
- return crcMaker.getValue();
+ public static void deleteFolderIfExists(Path target) throws IOException {
+ if (Files.notExists(target)) return;
+ Files.walkFileTree(target, new SimpleFileVisitor<>() {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
+ throws IOException {
+ Files.delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir, IOException e)
+ throws IOException {
+ if (e == null) {
+ Files.delete(dir);
+ return FileVisitResult.CONTINUE;
+ } else {
+ throw e;
+ }
+ }
+ });
}
}
\ No newline at end of file