Skip to content

Commit

Permalink
style: reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
cinit committed Aug 20, 2024
1 parent 0f2b484 commit f28ddad
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
103 changes: 63 additions & 40 deletions app/src/main/java/cc/ioctl/tmoe/hook/core/ProfileActivityRowHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,33 @@ public static final class RowManipulator {
int keyCount = 0;

public int getRowIdForField(String fieldName) {
for (var a: rows) {
if (a.type == RowInfo.TYPE_FIELD && fieldName.equals(a.name)) return a.index;
for (var a : rows) {
if (a.type == RowInfo.TYPE_FIELD && fieldName.equals(a.name)) {
return a.index;
}
}
return -1;
}

public int getRowIdForKey(String key) {
for (var a: rows) {
if (a.type == RowInfo.TYPE_KEY && key.equals(a.name)) return a.index;
for (var a : rows) {
if (a.type == RowInfo.TYPE_KEY && key.equals(a.name)) {
return a.index;
}
}
return -1;
}

public void insertRowAtPosition(String key, int position) {
for (var a: rows) {
if (a.type == RowInfo.TYPE_KEY && key.equals(a.name)) throw new IllegalArgumentException("duplicated row key " + key);
for (var a : rows) {
if (a.type == RowInfo.TYPE_KEY && key.equals(a.name)) {
throw new IllegalArgumentException("duplicated row key " + key);
}
}
for (var a: rows) {
if (a.index >= position) a.index++;
for (var a : rows) {
if (a.index >= position) {
a.index++;
}
}
var row = new RowInfo();
row.type = RowInfo.TYPE_KEY;
Expand All @@ -70,15 +78,16 @@ public void insertRowAtPosition(String key, int position) {

public boolean insertRowAtField(String key, String fieldName) {
var idx = getRowIdForField(fieldName);
if (idx == -1) return false;
if (idx == -1) {
return false;
}
insertRowAtPosition(key, idx);
return true;
}
}

public interface Callback {
/**
*
* @param key
* @param adpater
* @param holder
Expand All @@ -89,6 +98,7 @@ public interface Callback {

/**
* getItemViewType
*
* @param key
* @param adapter
* @param profileActivity
Expand All @@ -97,7 +107,6 @@ public interface Callback {
int getItemViewType(@NonNull String key, @NonNull Object adapter, @NonNull Object profileActivity);

/**
*
* @param key
* @param adapter
* @return true if handled, otherwise false
Expand All @@ -109,7 +118,8 @@ public interface Callback {

private static final List<Callback> sCallbacks = new ArrayList<>();

private ProfileActivityRowHook() {}
private ProfileActivityRowHook() {
}

private boolean mInitialized = false;
private static Field fBaseFragment_arguments = null;
Expand Down Expand Up @@ -188,7 +198,9 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Object holder = param.args[0];
int position = (int) Reflex.invokeVirtual(holder, "getAdapterPosition", int.class);
var rowKey = getRowForKey(fragment, position);
if (rowKey == null) return;
if (rowKey == null) {
return;
}
param.setResult(true);
}
}
Expand All @@ -200,8 +212,10 @@ protected void afterHookedMethod(MethodHookParam param) {
int position = (int) param.args[0];
Object fragment = Reflex.getInstanceObjectOrNull(param.thisObject, "this$0");
var rowKey = getRowForKey(fragment, position);
if (rowKey == null) return;
for (var c: sCallbacks) {
if (rowKey == null) {
return;
}
for (var c : sCallbacks) {
var type = c.getItemViewType(rowKey, param.thisObject, fragment);
if (type != -1) {
param.setResult(type);
Expand All @@ -216,33 +230,36 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
int position = (int) param.args[1];
Object fragment = Reflex.getInstanceObjectOrNull(param.thisObject, "this$0");
var rowKey = getRowForKey(fragment, position);
if (rowKey == null) return;
if (rowKey == null) {
return;
}
var holder = param.args[0];
for (var c: sCallbacks) {
for (var c : sCallbacks) {
if (c.onBindViewHolder(rowKey, holder, param.thisObject, fragment)) {
param.setResult(null);
break;
}
}
}
});
XposedBridge.hookAllMethods(
Initiator.load("org.telegram.ui.ProfileActivity$DiffCallback"), "fillPositions",
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Object fragment = Reflex.getInstanceObjectOrNull(param.thisObject, "this$0");
var args = (Bundle) fBaseFragment_arguments.get(fragment);
if (args == null) return;
var map = (HashMap<Integer, String>) args.getSerializable(PROFILE_ACTIVITY_EXTRA_ROWS);
if (map == null) return;
var i = 1000;
for (var pos: map.keySet()) {
XposedHelpers.callMethod(param.thisObject, "put", ++i, pos, param.args[0]);
}
}
XposedBridge.hookAllMethods(Initiator.load("org.telegram.ui.ProfileActivity$DiffCallback"), "fillPositions", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Object fragment = Reflex.getInstanceObjectOrNull(param.thisObject, "this$0");
var args = (Bundle) fBaseFragment_arguments.get(fragment);
if (args == null) {
return;
}
);
var map = (HashMap<Integer, String>) args.getSerializable(PROFILE_ACTIVITY_EXTRA_ROWS);
if (map == null) {
return;
}
var i = 1000;
for (var pos : map.keySet()) {
XposedHelpers.callMethod(param.thisObject, "put", ++i, pos, param.args[0]);
}
}
});
// we only need to do this once
Field fProfileActivity_rowCount = kProfileActivity.getDeclaredField("rowCount");
fProfileActivity_rowCount.setAccessible(true);
Expand Down Expand Up @@ -290,8 +307,10 @@ protected void beforeHookedMethod(MethodHookParam param) {
Objects.requireNonNull(fragment, "fragment is unexpectedly null");
int position = (int) param.args[1];
var rowKey = getRowForKey(fragment, position);
if (rowKey == null) return;
for (var c: sCallbacks) {
if (rowKey == null) {
return;
}
for (var c : sCallbacks) {
if (c.onItemClicked(rowKey, param.thisObject, fragment)) {
param.setResult(null);
break;
Expand Down Expand Up @@ -334,11 +353,11 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
manipulator.rows.add(row);
}

for (Callback c: sCallbacks) {
for (Callback c : sCallbacks) {
c.onInsertRow(manipulator, param.thisObject);
}

for (var row: manipulator.rows) {
for (var row : manipulator.rows) {
if (row.type == RowInfo.TYPE_FIELD) {
fields.get(row.fIndex).setInt(param.thisObject, row.index);
}
Expand All @@ -352,7 +371,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
fBaseFragment_arguments.set(param.thisObject, args);
}
var map = new HashMap<Integer, String>();
for (var row: manipulator.rows) {
for (var row : manipulator.rows) {
if (row.type == RowInfo.TYPE_KEY) {
map.put(row.index, row.name);
}
Expand All @@ -365,9 +384,13 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
private static @Nullable String getRowForKey(Object profileActivity, int row) {
try {
var args = (Bundle) fBaseFragment_arguments.get(profileActivity);
if (args == null) return null;
if (args == null) {
return null;
}
var map = (HashMap<Integer, String>) args.getSerializable(PROFILE_ACTIVITY_EXTRA_ROWS);
if (map == null) return null;
if (map == null) {
return null;
}
return map.get(row);
} catch (Throwable t) {
Utils.loge(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ private static void presentTMoeSettingsFragment(@NonNull Object parentFragment)

@Override
public boolean onBindViewHolder(@NonNull String key, @NonNull Object holder, @NonNull Object adpater, @NonNull Object profileActivity) {
if (!TMOE_SETTINGS_ROW.equals(key)) return false;
if (!TMOE_SETTINGS_ROW.equals(key)) {
return false;
}
FrameLayout textCell = (FrameLayout) Reflex.getInstanceObjectOrNull(holder, "itemView");
if (textCell != null) {
// color and theme is already set by Telegram, we only need to set the text and icon
Expand Down Expand Up @@ -72,7 +74,9 @@ public boolean onBindViewHolder(@NonNull String key, @NonNull Object holder, @No

@Override
public int getItemViewType(@NonNull String key, @NonNull Object adapter, @NonNull Object profileActivity) {
if (TMOE_SETTINGS_ROW.equals(key)) return 4;
if (TMOE_SETTINGS_ROW.equals(key)) {
return 4;
}
return -1;
}

Expand Down

0 comments on commit f28ddad

Please sign in to comment.