refine code

This commit is contained in:
327135569 2021-04-11 16:15:29 +08:00
parent f97f52dc8c
commit 49e6d32fc5
9 changed files with 23 additions and 152 deletions

View File

@ -1,19 +1,15 @@
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
android { android {
compileSdkVersion 30 compileSdkVersion rootProject.ext.androidCompileSdkVersion
defaultConfig { defaultConfig {
applicationId "com.storm.wind.xposed" applicationId "org.lsposed.lspatch"
minSdkVersion 27 minSdkVersion rootProject.ext.androidMinSdkVersion
targetSdkVersion 28 targetSdkVersion rootProject.ext.androidTargetSdkVersion
versionCode version_code as Integer versionCode rootProject.ext.verCode
versionName version_name versionName rootProject.ext.verName
multiDexEnabled false multiDexEnabled false
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
} }
buildTypes { buildTypes {
debug { debug {
@ -65,6 +61,5 @@ android {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation("androidx.core:core:1.3.2")
implementation project(path: ':lspcore') implementation project(path: ':lspcore')
} }

View File

@ -1,41 +1,23 @@
package com.storm.wind.xposed; package com.storm.wind.xposed;
import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import de.robv.android.xposed.XC_MethodHook; import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers; import de.robv.android.xposed.XposedHelpers;
//import android.support.v4.app.ActivityCompat;
public class MainActivity extends Activity { public class MainActivity extends Activity {
private static final int REQUEST_PERMISSION_CODE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
}
}
XposedHelpers.findAndHookMethod(this.getClass(), "checkXposed", new XC_MethodHook() { XposedHelpers.findAndHookMethod(this.getClass(), "checkXposed", new XC_MethodHook() {
@Override @Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable { protected void beforeHookedMethod(MethodHookParam param) throws Throwable {

View File

@ -182,6 +182,7 @@ public class MMPLoader {
boolean configFileExist = configFileExist(); boolean configFileExist = configFileExist();
// todo: Android 11
for (PackageInfo pkg : pm.getInstalledPackages(PackageManager.GET_META_DATA)) { for (PackageInfo pkg : pm.getInstalledPackages(PackageManager.GET_META_DATA)) {
ApplicationInfo app = pkg.applicationInfo; ApplicationInfo app = pkg.applicationInfo;
if (!app.enabled) { if (!app.enabled) {
@ -228,7 +229,7 @@ public class MMPLoader {
return modulePathList; return modulePathList;
} }
// sd卡中加载指定文件以加载指定的xposed module // sd 卡中加载指定文件以加载指定的 xposed module
private static List<String> loadPackageNameListFromFile(boolean loadActivedPackages) { private static List<String> loadPackageNameListFromFile(boolean loadActivedPackages) {
File moduleFile = new File(DIR_BASE, XPOSED_MODULE_FILE_PATH); File moduleFile = new File(DIR_BASE, XPOSED_MODULE_FILE_PATH);
if (!moduleFile.exists()) { if (!moduleFile.exists()) {
@ -236,11 +237,8 @@ public class MMPLoader {
} }
List<String> modulePackageList = new ArrayList<>(); List<String> modulePackageList = new ArrayList<>();
FileInputStream fileInputStream = null; try (FileInputStream fileInputStream = new FileInputStream(moduleFile);
BufferedReader bufferedReader = null; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream))) {
try {
fileInputStream = new FileInputStream(moduleFile);
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String modulePackageName; String modulePackageName;
while ((modulePackageName = bufferedReader.readLine()) != null) { while ((modulePackageName = bufferedReader.readLine()) != null) {
modulePackageName = modulePackageName.trim(); modulePackageName = modulePackageName.trim();
@ -263,10 +261,6 @@ public class MMPLoader {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
finally {
closeStream(fileInputStream);
closeStream(bufferedReader);
}
return modulePackageList; return modulePackageList;
} }
@ -282,12 +276,8 @@ public class MMPLoader {
throw new IllegalStateException("create " + XPOSED_MODULE_FILE_PATH + " err"); throw new IllegalStateException("create " + XPOSED_MODULE_FILE_PATH + " err");
} }
} }
FileOutputStream outputStream = null; try (FileOutputStream outputStream = new FileOutputStream(moduleFile, true);
BufferedWriter writer = null; BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream))) {
try {
outputStream = new FileOutputStream(moduleFile, true);
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
for (Pair<String, String> packageInfo : packageNameList) { for (Pair<String, String> packageInfo : packageNameList) {
String packageName = packageInfo.first; String packageName = packageInfo.first;
String appName = packageInfo.second; String appName = packageInfo.second;
@ -300,10 +290,6 @@ public class MMPLoader {
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
finally {
closeStream(outputStream);
closeStream(writer);
}
} }
private static boolean configFileExist() { private static boolean configFileExist() {

View File

@ -21,15 +21,15 @@ public class FileUtils {
public static boolean isSdcardPermissionGranted(Context context) { public static boolean isSdcardPermissionGranted(Context context) {
int pid = android.os.Process.myPid(); int pid = android.os.Process.myPid();
int uid = Process.myUid(); int uid = Process.myUid();
return context.checkPermission(PERMISSIONS_STORAGE[0], pid, uid) == PackageManager.PERMISSION_GRANTED && context.checkPermission(PERMISSIONS_STORAGE[1], pid, uid) == PackageManager.PERMISSION_GRANTED; return context.checkPermission(PERMISSIONS_STORAGE[0], pid, uid) == PackageManager.PERMISSION_GRANTED && context.checkPermission(PERMISSIONS_STORAGE[1], pid,
uid) == PackageManager.PERMISSION_GRANTED;
} }
public static String readTextFromAssets(Context context, String assetsFileName) { public static String readTextFromAssets(Context context, String assetsFileName) {
if (context == null) { if (context == null) {
throw new IllegalStateException("context null"); throw new IllegalStateException("context null");
} }
try { try (InputStream is = context.getAssets().open(assetsFileName)) {
InputStream is = context.getAssets().open(assetsFileName);
return readTextFromInputStream(is); return readTextFromInputStream(is);
} }
catch (Exception e) { catch (Exception e) {

View File

@ -1,93 +0,0 @@
package com.wind.xposed.entry.util;
import static android.os.Build.VERSION.SDK_INT;
import android.os.Build;
import android.util.Log;
import java.lang.reflect.Method;
/**
* @author Windysha
*/
public class ReflectionApiCheck {
private static final String TAG = ReflectionApiCheck.class.getSimpleName();
private static final int ERROR_EXEMPT_FAILED = -21;
private static Object sVmRuntime;
private static Method setHiddenApiExemptions;
static {
if (SDK_INT >= Build.VERSION_CODES.P) {
try {
Method forName = Class.class.getDeclaredMethod("forName", String.class);
Method getDeclaredMethod = Class.class.getDeclaredMethod("getDeclaredMethod", String.class, Class[].class);
Class<?> vmRuntimeClass = (Class<?>) forName.invoke(null, "dalvik.system.VMRuntime");
Method getRuntime = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "getRuntime", null);
if (getRuntime == null) {
throw new IllegalStateException("getRuntime method null");
}
setHiddenApiExemptions = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "setHiddenApiExemptions", new Class[]{String[].class});
sVmRuntime = getRuntime.invoke(null);
}
catch (Throwable e) {
Log.e(TAG, "reflect bootstrap failed:", e);
}
}
}
public static int unseal() {
if (SDK_INT < 28) {
// Below Android P, ignore
return 0;
}
// try exempt API first.
if (exemptAll()) {
return 0;
}
else {
return ERROR_EXEMPT_FAILED;
}
}
/**
* make the method exempted from hidden API check.
*
* @param method the method signature prefix.
* @return true if success.
*/
public static boolean exempt(String method) {
return exempt(new String[]{method});
}
/**
* make specific methods exempted from hidden API check.
*
* @param methods the method signature prefix, such as "Ldalvik/system", "Landroid" or even "L"
* @return true if success
*/
public static boolean exempt(String... methods) {
if (sVmRuntime == null || setHiddenApiExemptions == null) {
return false;
}
try {
setHiddenApiExemptions.invoke(sVmRuntime, new Object[]{methods});
return true;
}
catch (Throwable e) {
return false;
}
}
/**
* Make all hidden API exempted.
*
* @return true if success.
*/
public static boolean exemptAll() {
return exempt(new String[]{"L"});
}
}

View File

@ -10,7 +10,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class XpatchUtils { public class XpatchUtils {
final static String TAG = "MMP" + XpatchUtils.class.getSimpleName(); final static String TAG = "LSP" + XpatchUtils.class.getSimpleName();
public static Context createAppContext() { public static Context createAppContext() {
try { try {

View File

@ -7,8 +7,8 @@
<TextView <TextView
android:id="@+id/msg" android:id="@+id/msg"
android:textSize="50sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Hello World!" /> android:text="Hello World!" />
</RelativeLayout> </RelativeLayout>

View File

@ -9,20 +9,21 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:7.0.0-alpha14' classpath 'com.android.tools.build:gradle:7.0.0-alpha14'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
} }
} }
// sync from https://github.com/LSPosed/LSPosed/blob/master/build.gradle.kts
ext { ext {
androidCompileSdkVersion = 30 androidCompileSdkVersion = 30
androidCompileNdkVersion = "22.1.7171670" androidCompileNdkVersion = "22.1.7171670"
androidBuildToolsVersion = "30.0.3" androidBuildToolsVersion = "30.0.3"
androidMinSdkVersion = 27 androidMinSdkVersion = 27
androidTargetSdkVersion = 28 androidTargetSdkVersion = 30
verCode = 1 verCode = 1
verName = "mmpatch" verName = "lspatch"
apiCode = 93 apiCode = 93
defaultManagerPackageName = "org.github.mmpatch" defaultManagerPackageName = "org.lsposed.lspatch"
androidSourceCompatibility = JavaVersion.VERSION_11 androidSourceCompatibility = JavaVersion.VERSION_11
androidTargetCompatibility = JavaVersion.VERSION_11 androidTargetCompatibility = JavaVersion.VERSION_11
zipPathMagiskReleasePath = project(":lspcore").projectDir.path + "/build/tmp/release/magisk/" zipPathMagiskReleasePath = project(":lspcore").projectDir.path + "/build/tmp/release/magisk/"

View File

@ -1,5 +1,5 @@
include ':app' include ':app'
rootProject.name='MMPLoader' rootProject.name='LSPatch'
include ':lspcore' include ':lspcore'
project(':lspcore').projectDir = new File('mmp/core') project(':lspcore').projectDir = new File('mmp/core')
include ':hiddenapi-stubs' include ':hiddenapi-stubs'