forked from shatyuka/Zhiliao
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb3de68
commit d04737e
Showing
5 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
app/src/main/java/com/shatyuka/zhiliao/hooks/CustomTabFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.shatyuka.zhiliao.hooks; | ||
|
||
import com.shatyuka.zhiliao.Helper; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import de.robv.android.xposed.XC_MethodHook; | ||
import de.robv.android.xposed.XposedBridge; | ||
|
||
|
||
public class CustomTabFilter implements IHook { | ||
|
||
static Class<?> mainPageFragment; | ||
|
||
static Field customTabInfoList; | ||
|
||
static Field tabType; | ||
|
||
@Override | ||
public String getName() { | ||
return "自定义首页顶栏Tab"; | ||
} | ||
|
||
@Override | ||
public void init(ClassLoader classLoader) throws Throwable { | ||
mainPageFragment = classLoader.loadClass("com.zhihu.android.app.feed.explore.view.MainPageFragment"); | ||
|
||
customTabInfoList = Arrays.stream(mainPageFragment.getDeclaredFields()) | ||
.filter(field -> field.getType() == List.class).findFirst().get(); | ||
customTabInfoList.setAccessible(true); | ||
|
||
tabType = classLoader.loadClass("com.zhihu.android.api.model.CustomTabInfo").getDeclaredField("tab_type"); | ||
tabType.setAccessible(true); | ||
} | ||
|
||
@Override | ||
public void hook() throws Throwable { | ||
if (Helper.prefs.getBoolean("switch_mainswitch", false)) { | ||
XposedBridge.hookAllMethods(mainPageFragment, "onCreate", new XC_MethodHook() { | ||
@Override | ||
protected void afterHookedMethod(MethodHookParam param) throws Throwable { | ||
|
||
List<Object> tabList = (List<Object>) customTabInfoList.get(param.thisObject); | ||
|
||
String tabFilter = Helper.prefs.getString("edit_tabfilter", ""); | ||
if (tabFilter.isEmpty()) { | ||
Helper.prefs.edit().putString("edit_tabfilter", encodePattern(tabList)).apply(); | ||
} else { | ||
List<Object> postProcessTabList = postProcessTabList(tabList, decodePattern(tabFilter)); | ||
customTabInfoList.set(param.thisObject, postProcessTabList); | ||
} | ||
} | ||
|
||
}); | ||
} | ||
} | ||
|
||
private String encodePattern(List<Object> tabList) { | ||
if (tabList == null || tabList.isEmpty()) { | ||
return null; | ||
} | ||
return tabList.stream().map(o -> { | ||
try { | ||
return (String) tabType.get(o); | ||
} catch (IllegalAccessException e) { | ||
return null; | ||
} | ||
}).filter(s -> s != null && !s.isEmpty()) | ||
.collect(Collectors.joining("|")); | ||
} | ||
|
||
private List<String> decodePattern(String pattern) { | ||
return Arrays.stream(pattern.split("\\|")) | ||
.map(String::trim) | ||
.filter(s -> !s.isEmpty()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<Object> postProcessTabList(List<Object> tabList, List<String> typeFilterList) { | ||
if (tabList == null || tabList.isEmpty()) { | ||
return tabList; | ||
} | ||
|
||
Map<String, Object> typeTabMap = tabList.stream() | ||
.collect(Collectors.toMap(tab -> { | ||
try { | ||
return (String) tabType.get(tab); | ||
} catch (IllegalAccessException ignore) { | ||
return null; | ||
} | ||
}, tab -> tab)); | ||
|
||
return typeFilterList.stream() | ||
.map(typeTabMap::get) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#808080" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M10.83,8H21V6H8.83L10.83,8zM15.83,13H18v-2h-4.17L15.83,13zM14,16.83V18h-4v-2h3.17l-3,-3H6v-2h2.17l-3,-3H3V6h0.17L1.39,4.22l1.41,-1.41l18.38,18.38l-1.41,1.41L14,16.83z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters