Move duplicated methods into shared library

This commit is contained in:
Nullptr 2021-09-07 08:12:22 +08:00
parent efd701b7e0
commit 3f5279856d
5 changed files with 7 additions and 53 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}
});
}
}

View File

@ -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;

View File

@ -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
}

View File

@ -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();