Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
feat: add wifi only switch for always show original image
Browse files Browse the repository at this point in the history
  • Loading branch information
mkx173 committed Feb 20, 2024
1 parent 9b08786 commit c5a7d36
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private LinearLayout createRootPreference(final Activity activity) {
preferenceLayout.addView(new SwitchButtonHolder(activity, "吧页面起始页面改为最新", "frs_tab", SwitchButtonHolder.TYPE_SWITCH));
preferenceLayout.addView(new SwitchButtonHolder(activity, "消息页面起始页面改为通知", "msg_center_tab", SwitchButtonHolder.TYPE_SWITCH));
preferenceLayout.addView(new SwitchButtonHolder(activity, "自动查看原图", "origin_src", SwitchButtonHolder.TYPE_SWITCH));
preferenceLayout.addView(new SwitchButtonHolder(activity, "自动查看原图仅WiFi下生效", "origin_src_only_wifi", SwitchButtonHolder.TYPE_SWITCH));

preferenceLayout.addView(TSPreferenceHelper.createTextView(isPurgeEnabled ? "奇怪怪" : "其它"));
preferenceLayout.addView(new SwitchButtonHolder(activity, "禁用帖子手势", "forbid_gesture", SwitchButtonHolder.TYPE_SWITCH));
Expand Down
49 changes: 40 additions & 9 deletions app/src/main/java/gm/tieba/tabswitch/hooker/auto/OriginSrc.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import de.robv.android.xposed.XposedHelpers;
import gm.tieba.tabswitch.XposedContext;
import gm.tieba.tabswitch.dao.AcRules;
import gm.tieba.tabswitch.dao.Preferences;
import gm.tieba.tabswitch.hooker.IHooker;
import gm.tieba.tabswitch.hooker.deobfuscation.StringMatcher;

Expand All @@ -28,9 +29,15 @@ public String key() {
return "origin_src";
}

private static boolean isHooked;
private static XC_MethodHook.Unhook picListUnhook;
private static XC_MethodHook.Unhook pbContentUnhook;
private static XC_MethodHook.Unhook mediaUnhook;

private static void doHook() {
if (isHooked) return;
AcRules.findRule(new StringMatcher("pic_amount"), (matcher, clazz, method) ->
XposedHelpers.findAndHookMethod(clazz, sClassLoader, method, JSONObject.class, Boolean.class, new XC_MethodHook() {
picListUnhook = XposedHelpers.findAndHookMethod(clazz, sClassLoader, method, JSONObject.class, Boolean.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(final MethodHookParam param) throws Throwable {
final JSONObject jsonObject = (JSONObject) param.args[0];
Expand All @@ -48,7 +55,7 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
jsonObject.put("pic_list", picList);
}
}));
XposedHelpers.findAndHookMethod("tbclient.PbContent$Builder", sClassLoader, "build", boolean.class, new XC_MethodHook() {
pbContentUnhook = XposedHelpers.findAndHookMethod("tbclient.PbContent$Builder", sClassLoader, "build", boolean.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(final XC_MethodHook.MethodHookParam param) throws Throwable {
XposedHelpers.setObjectField(param.thisObject, "show_original_btn", 0);
Expand All @@ -59,7 +66,7 @@ protected void beforeHookedMethod(final XC_MethodHook.MethodHookParam param) thr
}
}
});
XposedHelpers.findAndHookMethod("tbclient.Media$Builder", sClassLoader, "build", boolean.class, new XC_MethodHook() {
mediaUnhook = XposedHelpers.findAndHookMethod("tbclient.Media$Builder", sClassLoader, "build", boolean.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(final XC_MethodHook.MethodHookParam param) throws Throwable {
XposedHelpers.setObjectField(param.thisObject, "show_original_btn", 0);
Expand All @@ -70,17 +77,39 @@ protected void beforeHookedMethod(final XC_MethodHook.MethodHookParam param) thr
}
}
});
isHooked = true;
}

private static void doUnHook() {
if (!isHooked) return;
if (picListUnhook != null) {
picListUnhook.unhook();
picListUnhook = null;
}
if (pbContentUnhook != null) {
pbContentUnhook.unhook();
pbContentUnhook = null;
}
if (mediaUnhook != null) {
mediaUnhook.unhook();
mediaUnhook = null;
}
isHooked = false;
}

@SuppressLint("MissingPermission")
@Override
public void hook() throws Throwable {
final NetworkCallbackImpl networkCallback = new NetworkCallbackImpl();
final NetworkRequest.Builder builder = new NetworkRequest.Builder();
final NetworkRequest request = builder.build();
final ConnectivityManager connMgr = (ConnectivityManager) getContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if (connMgr != null) connMgr.registerNetworkCallback(request, networkCallback);
if (Preferences.getBoolean("origin_src_only_wifi")) {
final NetworkCallbackImpl networkCallback = new NetworkCallbackImpl();
final NetworkRequest.Builder builder = new NetworkRequest.Builder();
final NetworkRequest request = builder.build();
final ConnectivityManager connMgr = (ConnectivityManager) getContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if (connMgr != null) connMgr.registerNetworkCallback(request, networkCallback);
} else {
doHook();
}
}

private static class NetworkCallbackImpl extends ConnectivityManager.NetworkCallback {
Expand All @@ -93,6 +122,8 @@ public void onCapabilitiesChanged(final Network network, final NetworkCapabiliti
if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
&& networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
doHook();
} else {
doUnHook();
}
}
}
Expand Down

0 comments on commit c5a7d36

Please sign in to comment.