From 3f5279856dadf788395d1d57bfc9301b1aedafbc Mon Sep 17 00:00:00 2001 From: Nullptr Date: Tue, 7 Sep 2021 08:12:22 +0800 Subject: [PATCH] Move duplicated methods into shared library --- .../lspatch/loader/LSPApplication.java | 4 +- .../lspatch/loader/util/FileUtils.java | 47 +------------------ .../appstub/LSPAppComponentFactoryStub.java | 2 +- share/build.gradle | 4 +- .../org/lsposed/lspatch/share}/FileUtils.java | 3 +- 5 files changed, 7 insertions(+), 53 deletions(-) rename {appstub/src/main/java/org/lsposed/lspatch/util => share/src/main/java/org/lsposed/lspatch/share}/FileUtils.java (97%) diff --git a/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java b/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java index e9d4f61..2d7d1b4 100644 --- a/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java +++ b/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java @@ -187,7 +187,7 @@ public class LSPApplication extends ApplicationServiceClient { if (!Files.exists(Paths.get(cacheApkPath))) { Log.i(TAG, "extract module apk: " + packageName); - FileUtils.deleteFolderIfExists(Paths.get(modulePath)); + org.lsposed.lspatch.share.FileUtils.deleteFolderIfExists(Paths.get(modulePath)); Files.createDirectories(Paths.get(modulePath)); try (var is = context.getAssets().open("modules/" + name)) { Files.copy(is, Paths.get(cacheApkPath)); @@ -355,7 +355,7 @@ public class LSPApplication extends ApplicationServiceClient { } }); } catch (Throwable e) { - Log.e(TAG, "hookApplicationStub"); + Log.e(TAG, "hookApplicationStub", e); } } diff --git a/app/src/main/java/org/lsposed/lspatch/loader/util/FileUtils.java b/app/src/main/java/org/lsposed/lspatch/loader/util/FileUtils.java index b438215..1b40415 100644 --- a/app/src/main/java/org/lsposed/lspatch/loader/util/FileUtils.java +++ b/app/src/main/java/org/lsposed/lspatch/loader/util/FileUtils.java @@ -2,16 +2,7 @@ package org.lsposed.lspatch.loader.util; import android.content.Context; -import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -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 { @@ -20,45 +11,9 @@ public class FileUtils { throw new IllegalStateException("context null"); } try (InputStream is = context.getAssets().open(assetsFileName)) { - return readTextFromInputStream(is); + return org.lsposed.lspatch.share.FileUtils.readTextFromInputStream(is); } catch (Throwable ignored) { } return null; } - - public static String readTextFromInputStream(InputStream is) { - try (InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8); BufferedReader bufferedReader = new BufferedReader(reader)) { - StringBuilder builder = new StringBuilder(); - String str; - while ((str = bufferedReader.readLine()) != null) { - builder.append(str); - } - return builder.toString(); - } catch (Throwable ignored) { - } - return null; - } - - 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; - } - } - }); - } } 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 d7e9be2..a1256f5 100644 --- a/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPAppComponentFactoryStub.java +++ b/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPAppComponentFactoryStub.java @@ -11,7 +11,7 @@ import android.content.Intent; import android.content.pm.ApplicationInfo; import android.util.Log; -import org.lsposed.lspatch.util.FileUtils; +import org.lsposed.lspatch.share.FileUtils; import java.io.InputStream; import java.nio.file.Files; diff --git a/share/build.gradle b/share/build.gradle index 71b5f2f..6d66b73 100644 --- a/share/build.gradle +++ b/share/build.gradle @@ -2,6 +2,6 @@ plugins { id 'java-library' } java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 } \ No newline at end of file diff --git a/appstub/src/main/java/org/lsposed/lspatch/util/FileUtils.java b/share/src/main/java/org/lsposed/lspatch/share/FileUtils.java similarity index 97% rename from appstub/src/main/java/org/lsposed/lspatch/util/FileUtils.java rename to share/src/main/java/org/lsposed/lspatch/share/FileUtils.java index 0c05879..5d14ba4 100644 --- a/appstub/src/main/java/org/lsposed/lspatch/util/FileUtils.java +++ b/share/src/main/java/org/lsposed/lspatch/share/FileUtils.java @@ -1,4 +1,4 @@ -package org.lsposed.lspatch.util; +package org.lsposed.lspatch.share; import java.io.BufferedReader; import java.io.IOException; @@ -12,7 +12,6 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class FileUtils { - public static String readTextFromInputStream(InputStream is) { try (InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8); BufferedReader bufferedReader = new BufferedReader(reader)) { StringBuilder builder = new StringBuilder();