Skip to content

Commit

Permalink
LSPatch 環境下で未読スイッチを表示 (#184)
Browse files Browse the repository at this point in the history
Co-authored-by: Syuugo <[email protected]>
  • Loading branch information
areteruhiro and s1204IT authored Oct 8, 2024
1 parent fd52486 commit 6771f66
Showing 1 changed file with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.chipppppppppp.lime.hooks;

import android.content.Context;
import android.content.SharedPreferences;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
Expand All @@ -11,6 +11,10 @@
import android.widget.Switch;
import android.widget.TextView;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
Expand All @@ -32,12 +36,13 @@ public void hook(LimeOptions limeOptions, XC_LoadPackage.LoadPackageParam loadPa
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
ViewGroup viewGroup = (ViewGroup) param.thisObject;
Context context = viewGroup.getContext();

SharedPreferences prefs = context.getSharedPreferences(Constants.MODULE_NAME + "-options", Context.MODE_PRIVATE);
context.getApplicationContext().createPackageContext(Constants.MODULE_NAME, Context.CONTEXT_IGNORE_SECURITY);

Context moduleContext = context.getApplicationContext().createPackageContext(Constants.MODULE_NAME, Context.CONTEXT_IGNORE_SECURITY);
String textKeepUnread = moduleContext.getResources().getString(R.string.switch_keep_unread);

keepUnread = readStateFromFile(context);

RelativeLayout container = new RelativeLayout(context);
RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Expand All @@ -47,7 +52,6 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
background.setShape(GradientDrawable.RECTANGLE);
background.setColor(Color.parseColor("#06C755"));
background.setCornerRadii(new float[]{100, 100, 80, 30, 100, 100, 80, 30});

container.setBackground(background);

TextView label = new TextView(context);
Expand All @@ -66,11 +70,10 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
switchParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
switchParams.setMargins(0, 0, 40, 0);
keepUnread = prefs.getBoolean("keep_unread", false);
switchView.setChecked(keepUnread);
switchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
keepUnread = isChecked;
prefs.edit().putBoolean("keep_unread", isChecked).apply();
saveStateToFile(context, isChecked);
});

container.addView(switchView, switchParams);
Expand All @@ -79,7 +82,6 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
}
}
);

XposedHelpers.findAndHookMethod(
loadPackageParam.classLoader.loadClass(Constants.MARK_AS_READ_HOOK.className),
Constants.MARK_AS_READ_HOOK.methodName,
Expand All @@ -93,4 +95,26 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
}
);
}
private void saveStateToFile(Context context, boolean state) {
String filename = "keep_unread_state.txt";
try (FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE)) {
fos.write((state ? "1" : "0").getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean readStateFromFile(Context context) {
String filename = "keep_unread_state.txt";
try (FileInputStream fis = context.openFileInput(filename)) {
int c;
StringBuilder sb = new StringBuilder();
while ((c = fis.read()) != -1) {
sb.append((char) c);
}
return "1".equals(sb.toString());
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}

0 comments on commit 6771f66

Please sign in to comment.