Skip to content

Commit

Permalink
opt: Tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lingqiqi5211 committed Jan 10, 2025
1 parent cdfef14 commit 7b4231f
Show file tree
Hide file tree
Showing 25 changed files with 135 additions and 208 deletions.
4 changes: 2 additions & 2 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-keep class com.sevtinge.hyperceiler.module.skip.SystemFrameworkForCorePatch
-keep class com.sevtinge.hyperceiler.ui.activity.LauncherActivity
-keep class com.sevtinge.hyperceiler.utils.blur.*
-keep class com.sevtinge.hyperceiler.utils.Helpers { boolean isModuleActive; }
-keep class com.sevtinge.hyperceiler.utils.Helpers { int XposedVersion; }
-keep class com.sevtinge.hyperceiler.module.base.tool.AppsTool { boolean isModuleActive; }
-keep class com.sevtinge.hyperceiler.module.base.tool.AppsTool { int XposedVersion; }
-keep class fan.**{ *; }
-keep class androidx.preference.**{ *; }
-keep class org.luckypray.dexkit.**{ *; }
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/sevtinge/hyperceiler/XposedInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
package com.sevtinge.hyperceiler;

import static com.sevtinge.hyperceiler.utils.Helpers.getPackageVersionCode;
import static com.sevtinge.hyperceiler.utils.Helpers.getPackageVersionName;
import static com.sevtinge.hyperceiler.module.base.tool.AppsTool.getPackageVersionCode;
import static com.sevtinge.hyperceiler.module.base.tool.AppsTool.getPackageVersionName;
import static com.sevtinge.hyperceiler.utils.devicesdk.MiDeviceAppUtilsKt.isPad;
import static com.sevtinge.hyperceiler.utils.devicesdk.SystemSDKKt.getAndroidVersion;
import static com.sevtinge.hyperceiler.utils.devicesdk.SystemSDKKt.getHyperOSVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.sevtinge.hyperceiler.module.base.dexkit.DexKit;
import com.sevtinge.hyperceiler.safe.CrashData;
import com.sevtinge.hyperceiler.utils.ContextUtils;
import com.sevtinge.hyperceiler.utils.Helpers;
import com.sevtinge.hyperceiler.module.base.tool.AppsTool;
import com.sevtinge.hyperceiler.utils.api.ProjectApi;
import com.sevtinge.hyperceiler.utils.log.XposedLogUtils;
import com.sevtinge.hyperceiler.utils.prefs.PrefsMap;
Expand Down Expand Up @@ -116,8 +116,8 @@ public void initHook(Object hook, boolean isInit, String versionName, int versio
onCreate(hook);
return;
}
int code = Helpers.getPackageVersionCode(mLoadPackageParam);
String name = Helpers.getPackageVersionName(mLoadPackageParam);
int code = AppsTool.getPackageVersionCode(mLoadPackageParam);
String name = AppsTool.getPackageVersionName(mLoadPackageParam);
if (code == versionCode)
onCreate(hook);
if (name.equals(versionName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
package com.sevtinge.hyperceiler.module.base.dexkit;

import static com.sevtinge.hyperceiler.utils.Helpers.getPackageVersionCode;
import static com.sevtinge.hyperceiler.utils.Helpers.getPackageVersionName;
import static com.sevtinge.hyperceiler.module.base.tool.AppsTool.getPackageVersionCode;
import static com.sevtinge.hyperceiler.module.base.tool.AppsTool.getPackageVersionName;
import static com.sevtinge.hyperceiler.utils.shell.ShellUtils.rootExecCmd;

import android.content.Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
* Copyright (C) 2023-2025 HyperCeiler Contributions
*/
package com.sevtinge.hyperceiler.utils;
package com.sevtinge.hyperceiler.module.base.tool;

import static com.sevtinge.hyperceiler.utils.log.XposedLogUtils.logE;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityOptions;
import android.app.backup.BackupManager;
import android.content.Context;
import android.content.Intent;
Expand All @@ -42,6 +41,7 @@
import android.widget.TextView;

import com.sevtinge.hyperceiler.R;
import com.sevtinge.hyperceiler.utils.DialogHelper;
import com.sevtinge.hyperceiler.utils.api.ProjectApi;
import com.sevtinge.hyperceiler.utils.log.AndroidLogUtils;
import com.sevtinge.hyperceiler.utils.prefs.PrefsUtils;
Expand All @@ -56,17 +56,13 @@
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import fan.appcompat.app.AlertDialog;

public class Helpers {
public class AppsTool {

private static final String TAG = "Helpers";

public static boolean isModuleActive = false;
public static int XposedVersion = 0;

public static final int REQUEST_PERMISSIONS_BACKUP = 1;
public static final int REQUEST_PERMISSIONS_RESTORE = 2;


public static LruCache<String, Bitmap> memoryCache = new LruCache<>((int) (Runtime.getRuntime().maxMemory() / 1024) / 2) {
@Override
protected int sizeOf(String key, Bitmap icon) {
Expand Down Expand Up @@ -98,16 +94,6 @@ public static synchronized Resources getModuleRes(Context context) throws Throwa
return (config == null ? moduleContext.getResources() : moduleContext.createConfigurationContext(config).getResources());
}

public static boolean checkStorageReadable(Activity activity) {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY) || state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
DialogHelper.showDialog(activity, "警告!", "无法访问任何合适的存储空间");
return false;
}
}

public static class MimeType {
public static int IMAGE = 1;
public static int AUDIO = 2;
Expand Down Expand Up @@ -180,7 +166,7 @@ public static void registerFileObserver(Context context) {
FileObserver mFileObserver = new FileObserver(new File(PrefsUtils.getSharedPrefsPath()), FileObserver.CLOSE_WRITE) {
@Override
public void onEvent(int event, String path) {
Helpers.fixPermissionsAsync(context);
AppsTool.fixPermissionsAsync(context);
}
};
mFileObserver.startWatching();
Expand All @@ -201,29 +187,6 @@ private static String getCallerMethod() {
return stackTrace[4].getMethodName();
}

public static ActivityOptions makeFreeformActivityOptions(Context context, String str) {

/*ActivityOptions activityOptions;
try {
activityOptions = ReflectUtils.callStaticObjectMethod(Class.forName("android.util.MiuiMultiWindowUtils"), ActivityOptions.class, "getActivityOptions", new Class[]{Context.class, String.class, Boolean.TYPE, Boolean.TYPE}, context, str, true, false);
} catch (Exception e) {
logD(TAG, "MiuiMultiWindowUtils getActivityOptions error", e);
activityOptions = null;
}
if (activityOptions != null) {
return activityOptions;
}
ActivityOptions makeBasic = ActivityOptions.makeBasic();
ReflectUtils.callObjectMethod("android.app.ActivityOptions", "setLaunchWindowingMode", new Class[]{int.class}, 5);
Rect rect = (Rect) ReflectUtils.callObjectMethod("android.util.MiuiMultiWindowUtils", "getFreeformRect", new Class[]{Context.class}, new Object[]{context});
makeBasic.setLaunchBounds(rect);
return makeBasic;*/
return null;
}

public static void openAppInfo(Context context, String pkg, int user) {
try {
Intent intent = new Intent("miui.intent.action.APP_MANAGER_APPLICATION_DETAIL");
Expand All @@ -249,27 +212,25 @@ public static void openAppInfo(Context context, String pkg, int user) {
public static String getPackageVersionName(XC_LoadPackage.LoadPackageParam lpparam) {
try {
Class<?> parserCls = XposedHelpers.findClass("android.content.pm.PackageParser", lpparam.classLoader);
Object parser = parserCls.newInstance();
Object parser = parserCls.getDeclaredConstructor().newInstance();
File apkPath = new File(lpparam.appInfo.sourceDir);
Object pkg = XposedHelpers.callMethod(parser, "parsePackage", apkPath, 0);
// XposedLogUtils.logI("getPackageVersionName", lpparam.packageName + " versionName is " + versionName);
return (String) XposedHelpers.getObjectField(pkg, "mVersionName");
} catch (Throwable e) {
// XposedLogUtils.logW("getPackageVersionName", e);
logE("getPackageVersionCode", e);
return "null";
}
}

public static int getPackageVersionCode(XC_LoadPackage.LoadPackageParam lpparam) {
try {
Class<?> parserCls = XposedHelpers.findClass("android.content.pm.PackageParser", lpparam.classLoader);
Object parser = parserCls.newInstance();
Object parser = parserCls.getDeclaredConstructor().newInstance();
File apkPath = new File(lpparam.appInfo.sourceDir);
Object pkg = XposedHelpers.callMethod(parser, "parsePackage", apkPath, 0);
// XposedLogUtils.logI("getPackageVersionCode", lpparam.packageName + " versionCode is " + versionCode);
return XposedHelpers.getIntField(pkg, "mVersionCode");
} catch (Throwable e) {
// XposedLogUtils.logW("getPackageVersionCode", e);
logE("getPackageVersionCode", e);
return -1;
}
}
Expand All @@ -279,76 +240,88 @@ public static void checkXposedActivateState(Context context) {
}

public static void doRestart(Context context, String[] packageName, boolean isRestartSystem) {
boolean result = false;
boolean result;
boolean pid = true;

if (isRestartSystem) {
result = ShellInit.getShell().run("reboot").sync().isResult();
} else {
if (packageName != null) {
for (String packageGet : packageName) {
if (packageGet == null) {
continue;
}
// String test = "XX";
// ShellUtils.CommandResult commandResult = ShellUtils.execCommand("{ [[ $(pgrep -f '" + packageGet +
// "' | grep -v $$) != \"\" ]] && { pkill -l 9 -f \"" + packageGet +
// "\"; }; } || { echo \"kill error\"; }", true, true);

boolean getResult =
ShellInit.getShell().add("pid=$(pgrep -f \"" + packageGet + "\" | grep -v $$)")
.add("if [[ $pid == \"\" ]]; then")
.add(" pids=\"\"")
.add(" pid=$(ps -A -o PID,ARGS=CMD | grep \"" + packageGet + "\" | grep -v \"grep\")")
.add(" for i in $pid; do")
.add(" if [[ $(echo $i | grep '[0-9]' 2>/dev/null) != \"\" ]]; then")
.add(" if [[ $pids == \"\" ]]; then")
.add(" pids=$i")
.add(" else")
.add(" pids=\"$pids $i\"")
.add(" fi")
.add(" fi")
.add(" done")
.add("fi")
.add("if [[ $pids != \"\" ]]; then")
.add(" pid=$pids")
.add("fi")
.add("if [[ $pid != \"\" ]]; then")
.add(" for i in $pid; do")
.add(" kill -s 15 $i &>/dev/null")
.add(" done")
.add("else")
.add(" echo \"No Find Pid!\"")
.add("fi").over().sync().isResult();
ArrayList<String> outPut = ShellInit.getShell().getOutPut();
ArrayList<String> error = ShellInit.getShell().getError();

if (getResult) {
if (!outPut.isEmpty()) {
if (outPut.get(0).equals("No Find Pid!")) {
pid = false;
} else {
result = true;
}
} else result = true;
} else
AndroidLogUtils.logE("doRestart: ", "result: " + ShellInit.getShell().getResult() +
" errorMsg: " + error + " package: " + packageGet);
result = handlePackages(packageName);
pid = result;
}

if (!result) {
showAlertDialog(context, isRestartSystem, pid);
}
}

private static boolean handlePackages(String[] packageName) {
if (packageName == null) {
AndroidLogUtils.logE("doRestart: ", "packageName is null");
return false;
}

boolean result = false;
for (String packageGet : packageName) {
if (packageGet == null) continue;

boolean getResult =
ShellInit.getShell().add("pid=$(pgrep -f \"" + packageGet + "\" | grep -v $$)")
.add("if [[ $pid == \"\" ]]; then")
.add(" pids=\"\"")
.add(" pid=$(ps -A -o PID,ARGS=CMD | grep \"" + packageGet + "\" | grep -v \"grep\")")
.add(" for i in $pid; do")
.add(" if [[ $(echo $i | grep '[0-9]' 2>/dev/null) != \"\" ]]; then")
.add(" if [[ $pids == \"\" ]]; then")
.add(" pids=$i")
.add(" else")
.add(" pids=\"$pids $i\"")
.add(" fi")
.add(" fi")
.add(" done")
.add("fi")
.add("if [[ $pids != \"\" ]]; then")
.add(" pid=$pids")
.add("fi")
.add("if [[ $pid != \"\" ]]; then")
.add(" for i in $pid; do")
.add(" kill -s 15 $i &>/dev/null")
.add(" done")
.add("else")
.add(" echo \"No Find Pid!\"")
.add("fi").over().sync().isResult();

ArrayList<String> outPut = ShellInit.getShell().getOutPut();
ArrayList<String> error = ShellInit.getShell().getError();

if (getResult) {
if (!outPut.isEmpty() && outPut.get(0).equals("No Find Pid!")) {
return false;
} else {
result = true;
}
} else {
AndroidLogUtils.logE("doRestart: ", "packageName is null");
AndroidLogUtils.logE("doRestart: ", "result: " + ShellInit.getShell().getResult() + " errorMsg: " + error + " package: " + packageGet);
}
// result = ShellUtils.getResultBoolean("pkill -l 9 -f " + packageName, true);
}
if (!result) {
new AlertDialog.Builder(context)
.setCancelable(false)
.setTitle(R.string.tip)
.setMessage(isRestartSystem ? R.string.reboot_failed :
pid ? R.string.kill_failed : R.string.pid_failed)
.setHapticFeedbackEnabled(true)
.setPositiveButton(android.R.string.ok, null)
.show();
return result;
}

private static void showAlertDialog(Context context, boolean isRestartSystem, boolean pid) {
new AlertDialog.Builder(context)
.setCancelable(false)
.setTitle(R.string.tip)
.setMessage(getErrorMessage(isRestartSystem, pid))
.setHapticFeedbackEnabled(true)
.setPositiveButton(android.R.string.ok, null)
.show();
}

private static int getErrorMessage(boolean isRestartSystem, boolean pid) {
if (isRestartSystem) {
return R.string.reboot_failed;
} else {
return pid ? R.string.kill_failed : R.string.pid_failed;
}
}
}
Loading

0 comments on commit 7b4231f

Please sign in to comment.