Skip to content

Commit

Permalink
优化: prefs 监听
Browse files Browse the repository at this point in the history
  • Loading branch information
HChenX committed Jun 11, 2024
1 parent 47661c5 commit 6eb3a0f
Show file tree
Hide file tree
Showing 15 changed files with 557 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class DexKit {
private static String callTAG;
private static Gson gson;
private static final HashMap<String, Boolean> touchMap = new HashMap<>();
private static final HashMap<String, ArrayList<DexKitData>> cacheMap = new HashMap<>();
public static final HashMap<String, ArrayList<DexKitData>> cacheMap = new HashMap<>();
private List<AnnotatedElement> elementList = new ArrayList<>();
private static final String DEXKIT_PATH = "/cache/dexkit/";
private String hostDir = null;
Expand Down Expand Up @@ -312,77 +312,6 @@ private static List<AnnotatedElement> run(@NonNull String tag, IDexKit iDexKit,
}
}

private static ArrayList<DexKitData> getCacheMapOrReadFile(String dexFile) {
ArrayList<DexKitData> cacheData = cacheMap.get(dexFile);
if (cacheData == null) {
Type type = new TypeToken<ArrayList<DexKitData>>() {
}.getType();
ArrayList<DexKitData> dataList = gson.fromJson(FileUtils.read(dexFile), type);
cacheMap.put(dexFile, dataList);
cacheData = dataList;
}
return cacheData;
}

private static boolean touchIfNeed(String dexFile) {
Boolean isTouch = touchMap.get(dexFile);
if (!Boolean.TRUE.equals(isTouch)) {
if (!FileUtils.exists(dexFile)) {
if (FileUtils.touch(dexFile)) {
if (FileUtils.write(dexFile, new JSONArray().toString()))
touchMap.put(dexFile, true);
else touchMap.put(dexFile, true);
} else {
XposedLogUtils.logE(callTAG, "failed to create file!");
touchMap.put(dexFile, false);
return false;
}
}
}
return true;
}

@NonNull
public static String getFile(String fileName) {
return dexKit.dexPath + fileName;
}

/**
* 删除指定路径缓存内指定标签的数据。
*/
public static boolean removeData(@NotNull String tag, @NotNull String filePath) {
try {
ArrayList<DexKitData> dataList = getCacheMapOrReadFile(filePath);
dataList.removeIf(dexKitData -> tag.equals(dexKitData.tag));
FileUtils.write(filePath, gson.toJson(dataList));
return true;
} catch (Throwable e) {
XposedLogUtils.logE(callTAG, e);
}
return false;
}

/**
* 删除指定路径缓存内指定标签的数据。
*/
public static boolean removeData(@NotNull String tag) {
return removeData(tag, getFile(callTAG));
}

private static String getCallName(StackTraceElement[] stackTrace) {
for (StackTraceElement stackTraceElement : stackTrace) {
String callName = stackTraceElement.getClassName();
if (callName.contains("com.sevtinge.hyperceiler.module.hook.")) {
String last = callName.substring(callName.lastIndexOf('.') + 1);
if (last.contains("$")) {
continue;
}
return last;
}
}
throw new RuntimeException("Invalid call stack");
}

// 获取结果并写入缓存
private static List<AnnotatedElement> getElementsAndWriteCache(String tag, IDexKit iDexKit, IDexKitList iDexKitList,
DexKitBridge dexKitBridge, ArrayList<DexKitData> dadaList, String dexFile,
Expand Down Expand Up @@ -502,6 +431,78 @@ private static List<AnnotatedElement> getFileCache(String tag, IDexKit iDexKit,
return getElement;
}

private static ArrayList<DexKitData> getCacheMapOrReadFile(String dexFile) {
ArrayList<DexKitData> cacheData = cacheMap.get(dexFile);
if (cacheData == null) {
Type type = new TypeToken<ArrayList<DexKitData>>() {
}.getType();
ArrayList<DexKitData> dataList = gson.fromJson(FileUtils.read(dexFile), type);
cacheMap.put(dexFile, dataList);
cacheData = dataList;
}
return cacheData;
}

private static boolean touchIfNeed(String dexFile) {
Boolean isTouch = touchMap.get(dexFile);
if (!Boolean.TRUE.equals(isTouch)) {
if (!FileUtils.exists(dexFile)) {
if (FileUtils.touch(dexFile)) {
if (FileUtils.write(dexFile, new JSONArray().toString()))
touchMap.put(dexFile, true);
else touchMap.put(dexFile, true);
} else {
XposedLogUtils.logE(callTAG, "failed to create file!");
touchMap.put(dexFile, false);
return false;
}
}
}
return true;
}

@NonNull
public static String getFile(String fileName) {
return dexKit.dexPath + fileName;
}

/**
* 删除指定路径缓存内指定标签的数据。
*/
public static boolean removeData(@NotNull String tag, @NotNull String filePath) {
try {
ArrayList<DexKitData> dataList = getCacheMapOrReadFile(filePath);
dataList.removeIf(dexKitData -> tag.equals(dexKitData.tag));
FileUtils.write(filePath, gson.toJson(dataList));
return true;
} catch (Throwable e) {
XposedLogUtils.logE(callTAG, e);
}
return false;
}

/**
* 删除指定路径缓存内指定标签的数据。
*/
public static boolean removeData(@NotNull String tag) {
return removeData(tag, getFile(callTAG));
}

private static String getCallName(StackTraceElement[] stackTrace) {
for (StackTraceElement stackTraceElement : stackTrace) {
String callName = stackTraceElement.getClassName();
if (callName.contains("com.sevtinge.hyperceiler.module.hook.")) {
String last = callName.substring(callName.lastIndexOf('.') + 1);
if (last.contains("$")) {
continue;
}
return last;
}
}
throw new RuntimeException("Invalid call stack");
}


@NotNull
private static Class<?> getClass(@Nullable String name, ClassLoader classLoader) {
if (name == null) throwRuntime("str is null, cant get class!!");
Expand All @@ -511,7 +512,7 @@ private static Class<?> getClass(@Nullable String name, ClassLoader classLoader)
name = name.replace("[", "");
int i = old.length() - name.length();
if (i != 0) {
c = switch (name.charAt(0)) {
c = switch (name.charAt(0)) {
case 'Z' -> boolean.class;
case 'B' -> byte.class;
case 'C' -> char.class;
Expand All @@ -521,13 +522,13 @@ private static Class<?> getClass(@Nullable String name, ClassLoader classLoader)
case 'F' -> float.class;
case 'D' -> double.class;
default -> null;
};
if (c != null) {
return Array.newInstance(c, new int[i]).getClass();
} else {
name = name.replace(";", "").substring(1);
return Array.newInstance(classLoader.loadClass(name), new int[i]).getClass();
}
};
if (c != null) {
return Array.newInstance(c, new int[i]).getClass();
} else {
name = name.replace(";", "").substring(1);
return Array.newInstance(classLoader.loadClass(name), new int[i]).getClass();
}
}
return switch (name.trim()) {
case "int" -> int.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* This file is part of HyperCeiler.
* This file is part of HyperCeiler.
* HyperCeiler is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License.
* HyperCeiler is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* Copyright (C) 2023-2024 HyperCeiler Contributions
*/
* Copyright (C) 2023-2024 HyperCeiler Contributions
*/
package com.sevtinge.hyperceiler.module.hook.home;

import android.app.Activity;
Expand All @@ -30,6 +30,7 @@
import com.sevtinge.hyperceiler.module.base.BaseHook;
import com.sevtinge.hyperceiler.module.hook.GlobalActions;
import com.sevtinge.hyperceiler.utils.log.AndroidLogUtils;
import com.sevtinge.hyperceiler.utils.prefs.PrefType;
import com.sevtinge.hyperceiler.utils.prefs.PrefsChangeObserver;
import com.sevtinge.hyperceiler.utils.prefs.PrefsUtils;

Expand All @@ -48,11 +49,13 @@ public void init() {
findAndHookMethod(mWorkspace, "onVerticalGesture", int.class, MotionEvent.class, new MethodHook() {
@Override
protected void before(MethodHookParam param) {
if ((boolean) XposedHelpers.callMethod(param.thisObject, "isInNormalEditingMode")) return;
if ((boolean) XposedHelpers.callMethod(param.thisObject, "isInNormalEditingMode"))
return;
String key = null;
Context helperContext = ((ViewGroup) param.thisObject).getContext();
int numOfFingers = 1;
if (param.args[1] != null) numOfFingers = ((MotionEvent) param.args[1]).getPointerCount();
if (param.args[1] != null)
numOfFingers = ((MotionEvent) param.args[1]).getPointerCount();
if ((int) param.args[0] == 11) {
if (numOfFingers == 1)
key = "prefs_key_home_gesture_down_swipe";
Expand All @@ -76,18 +79,16 @@ protected void after(final MethodHookParam param) {
Handler mHandler = (Handler) XposedHelpers.getObjectField(act, "mHandler");
new PrefsChangeObserver(act, mHandler) {
@Override
public void onChange(Uri uri) {
public void onChange(PrefType type, Uri uri, String name, Object def) {
try {
String type = uri.getPathSegments().get(1);
String key = uri.getPathSegments().get(2);
if (key.contains("prefs_key_home_gesture_down_swipe"))
if (name.contains("prefs_key_home_gesture_down_swipe"))
switch (type) {
case "string" ->
mPrefsMap.put(key, PrefsUtils.getSharedStringPrefs(act, key, ""));
case "integer" ->
mPrefsMap.put(key, PrefsUtils.getSharedIntPrefs(act, key, 1));
case "boolean" ->
mPrefsMap.put(key, PrefsUtils.getSharedBoolPrefs(act, key, false));
case PrefType.String ->
mPrefsMap.put(name, PrefsUtils.getSharedStringPrefs(act, name, ""));
case PrefType.Integer ->
mPrefsMap.put(name, PrefsUtils.getSharedIntPrefs(act, name, 1));
case PrefType.Boolean ->
mPrefsMap.put(name, PrefsUtils.getSharedBoolPrefs(act, name, false));
}
} catch (Throwable t) {
AndroidLogUtils.logD(TAG, "setAction", t);
Expand All @@ -100,7 +101,8 @@ public void onChange(Uri uri) {
findAndHookMethodSilently("com.miui.home.launcher.uioverrides.StatusBarSwipeController", "canInterceptTouch", MotionEvent.class, new MethodHook() {
@Override
protected void before(final MethodHookParam param) {
if (mPrefsMap.getInt("home_gesture_down_swipe_action", 0) > 0) param.setResult(false);
if (mPrefsMap.getInt("home_gesture_down_swipe_action", 0) > 0)
param.setResult(false);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* This file is part of HyperCeiler.
* This file is part of HyperCeiler.
* HyperCeiler is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License.
* HyperCeiler is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* Copyright (C) 2023-2024 HyperCeiler Contributions
*/
* Copyright (C) 2023-2024 HyperCeiler Contributions
*/
package com.sevtinge.hyperceiler.module.hook.home.drawer;

import static com.sevtinge.hyperceiler.utils.devicesdk.DisplayUtils.dp2px;
Expand All @@ -35,7 +35,7 @@

public class AllAppsContainerViewSuperBlur extends BaseTool {
private boolean isBlur;

private View view;
@Override
public void doHook() {
classTool.findClass("allApp", "com.miui.home.launcher.allapps.BaseAllAppsContainerView")
Expand All @@ -46,7 +46,7 @@ public void after(ParamTool param) {
ViewSwitcher mCategoryContainer = param.getField("mCategoryContainer");
RelativeLayout appsView = (RelativeLayout) mCategoryContainer.getParent();
FrameLayout frameLayout = new FrameLayout(mCategoryContainer.getContext());
View view = new View(mCategoryContainer.getContext());
view = new View(mCategoryContainer.getContext());
frameLayout.addView(view);
view.getLayoutParams().height = FrameLayout.LayoutParams.MATCH_PARENT;
view.getLayoutParams().width = FrameLayout.LayoutParams.MATCH_PARENT;
Expand Down
Loading

0 comments on commit 6eb3a0f

Please sign in to comment.