1
0
mirror of https://github.com/areteruhiro/LIME-beta-hiro.git synced 2025-02-05 21:11:39 +09:00

SharedPreferencesではなくファイルに設定状態を保存するように

LsPatchに対応
This commit is contained in:
areteruhiro 2024-10-09 00:59:16 +09:00 committed by GitHub
parent 5d8b75c224
commit fe2e8021f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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.RelativeLayout;
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;
@ -26,18 +30,21 @@ public class KeepUnread implements IHook {
if (limeOptions.removeKeepUnread.checked) return;
XposedBridge.hookAllConstructors(
loadPackageParam.classLoader.loadClass("jp.naver.line.android.common.view.listview.PopupListView"),
new XC_MethodHook() {
@Override
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);
@ -66,11 +73,10 @@ public class KeepUnread implements IHook {
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 +85,6 @@ public class KeepUnread implements IHook {
}
}
);
XposedHelpers.findAndHookMethod(
loadPackageParam.classLoader.loadClass(Constants.MARK_AS_READ_HOOK.className),
Constants.MARK_AS_READ_HOOK.methodName,
@ -93,4 +98,30 @@ public class KeepUnread implements IHook {
}
);
}
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;
}
}
}