Skip to content

Commit

Permalink
refactor: 显示卡片类别
Browse files Browse the repository at this point in the history
  • Loading branch information
CLOUDERHEM committed Feb 4, 2024
1 parent 2c8a008 commit 35e37b6
Showing 1 changed file with 45 additions and 22 deletions.
67 changes: 45 additions & 22 deletions app/src/main/java/com/shatyuka/zhiliao/hooks/Tag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.shatyuka.zhiliao.hooks;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -33,6 +32,8 @@ public class Tag implements IHook {
static Field SugarHolder_mData;
static Field TemplateRoot_unique;

private final int TAG_ID = 0xABCDEF;

@Override
public String getName() {
return "显示卡片类别";
Expand Down Expand Up @@ -76,42 +77,64 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (viewGroup == null) {
return;
}
TextView title = viewGroup.findViewById(Helper.context.getResources().getIdentifier("title", "id", MainHook.hookPackage));

if (title != null) {
title.setText("   " + title.getText());

Object templateFeed = SugarHolder_mData.get(thisObject);
Object unique = TemplateRoot_unique.get(templateFeed);
String type = (String) Helper.DataUnique_type.get(unique);
TextView title = viewGroup.findViewById(Helper.context.getResources().getIdentifier("title", "id", MainHook.hookPackage));
View author = viewGroup.findViewById(Helper.context.getResources().getIdentifier("author", "id", MainHook.hookPackage));
if (title == null) {
return;
}

RelativeLayout relativeLayout = new RelativeLayout(viewGroup.getContext());
relativeLayout.setY((float) (Helper.scale * 3 + 0.5));
relativeLayout.addView(buildTagView(viewGroup.getContext(), type));
Object templateFeed = SugarHolder_mData.get(thisObject);
Object unique = TemplateRoot_unique.get(templateFeed);
String type = (String) Helper.DataUnique_type.get(unique);

RelativeLayout tagLayout;
TextView tag = viewGroup.findViewById(TAG_ID);
if (tag == null) {
tag = new TextView(viewGroup.getContext());
tag.setId(TAG_ID);

tagLayout = new RelativeLayout(viewGroup.getContext());
tagLayout.addView(tag);
((ViewGroup) title.getParent()).addView(tagLayout);
} else {
tagLayout = (RelativeLayout) tag.getParent();
}

ViewGroup parent = (ViewGroup) title.getParent();
View author = parent.getChildAt(1);
int baseX = ((ViewGroup.MarginLayoutParams) title.getLayoutParams()).leftMargin;
if (baseX != 0) {
((ViewGroup.MarginLayoutParams) author.getLayoutParams()).leftMargin = baseX;
}

ViewGroup.MarginLayoutParams authorLayoutParams = (ViewGroup.MarginLayoutParams) author.getLayoutParams();
// tag对齐
if (authorLayoutParams.leftMargin != 0) {
relativeLayout.setX(authorLayoutParams.leftMargin);
}
postProcessTag(tagLayout, tag, type, baseX, title.getVisibility() == View.VISIBLE);

parent.addView(relativeLayout);
// 有标题
if (title.getVisibility() == View.VISIBLE) {
title.setText("   " + title.getText());
} else {
((ViewGroup.MarginLayoutParams) author.getLayoutParams()).leftMargin = (int) (Helper.scale * 40 + 0.5 + baseX);
}

}
});
}
}

private View buildTagView(Context context, String type) {
TextView tag = new TextView(context);
private void postProcessTag(RelativeLayout relativeLayout, TextView tag, String type, int baseX, boolean hasTitle) {
tag.setTextColor(-1);
tag.setText(getType(type));
tag.setBackground(getBackground(type));

return tag;
if (baseX != 0) {
relativeLayout.setX(baseX);
}

if (hasTitle) {
relativeLayout.setY((float) (Helper.scale * 3 + 0.5));
} else {
relativeLayout.setY(0);
}

}

static String getType(String type) {
Expand Down

0 comments on commit 35e37b6

Please sign in to comment.