diff --git a/app/src/main/java/io/github/chipppppppppp/lime/hooks/KeepUnread.java b/app/src/main/java/io/github/chipppppppppp/lime/hooks/KeepUnread.java index 1c094ef3..c5b540ed 100644 --- a/app/src/main/java/io/github/chipppppppppp/lime/hooks/KeepUnread.java +++ b/app/src/main/java/io/github/chipppppppppp/lime/hooks/KeepUnread.java @@ -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; @@ -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; @@ -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); @@ -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); @@ -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); @@ -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, @@ -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; + } + } }