1
0
mirror of https://github.com/areteruhiro/LIME-beta-hiro.git synced 2025-02-11 07:51:37 +09:00

Update KeepUnread.java

KeepUnreadボンタンの状態保存方法に伴う、クラッシュのため、従来の保存方法に変更
This commit is contained in:
areteruhiro 2024-11-05 16:35:24 +09:00 committed by GitHub
parent ccf2a72fd7
commit 5eb87fa34c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ import android.app.AndroidAppHelper;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
@ -12,17 +13,17 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import io.github.hiro.lime.LimeOptions;
import io.github.hiro.lime.R;
import de.robv.android.xposed.XSharedPreferences;
public class KeepUnread implements IHook {
static boolean keepUnread = false;
private static final String PREFS_NAME = "KeepUnreadPrefs";
private static final String KEY_KEEP_UNREAD = "keep_unread";
@Override
public void hook(LimeOptions limeOptions, XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
@ -38,10 +39,7 @@ public class KeepUnread implements IHook {
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
View rootView = (View) param.getResult();
Context appContext = rootView.getContext();
XSharedPreferences prefs = new XSharedPreferences("io.github.hiro.lime", PREFS_NAME);
keepUnread = prefs.getBoolean(KEY_KEEP_UNREAD, false);
Context moduleContext = AndroidAppHelper.currentApplication().createPackageContext(
"io.github.hiro.lime", Context.CONTEXT_IGNORE_SECURITY);
@ -50,21 +48,22 @@ public class KeepUnread implements IHook {
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
keepUnread = readStateFromFile(appContext);
ImageView imageView = new ImageView(appContext);
updateSwitchImage(imageView, keepUnread, moduleContext);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageParams.setMargins(50, 0, 0, 0);
imageParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); // 垂直中央に配置
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); // 水平中央に配置
imageParams.setMargins(50, 0, 0, 0); // 水平に50ピクセル右に移動必要に応じて調整
imageView.setOnClickListener(v -> {
keepUnread = !keepUnread;
updateSwitchImage(imageView, keepUnread, moduleContext);
prefs.edit().putBoolean(KEY_KEEP_UNREAD, keepUnread).apply();
saveStateToFile(appContext, keepUnread);
});
layout.addView(imageView, imageParams);
if (rootView instanceof ViewGroup) {
@ -79,16 +78,17 @@ public class KeepUnread implements IHook {
}
private void updateSwitchImage(ImageView imageView, boolean isOn, Context moduleContext) {
String imageName = isOn ? "switch_on" : "switch_off";
int imageResource = moduleContext.getResources().getIdentifier(imageName, "drawable", "io.github.hiro.lime");
if (imageResource != 0) {
Drawable drawable = moduleContext.getResources().getDrawable(imageResource, null);
if (drawable != null) {
drawable = scaleDrawable(drawable, 86, 86);
imageView.setImageDrawable(drawable);
String imageName = isOn ? "switch_on" : "switch_off";
int imageResource = moduleContext.getResources().getIdentifier(imageName, "drawable", "io.github.hiro.lime");
if (imageResource != 0) {
Drawable drawable = moduleContext.getResources().getDrawable(imageResource, null);
if (drawable != null) {
drawable = scaleDrawable(drawable, 86, 86);
imageView.setImageDrawable(drawable);
}
}
}
}
private Drawable scaleDrawable(Drawable drawable, int width, int height) {
@ -96,6 +96,28 @@ public class KeepUnread implements IHook {
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
return new BitmapDrawable(scaledBitmap);
}
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;
}
}
}
);
@ -106,7 +128,7 @@ public class KeepUnread implements IHook {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
if (keepUnread) {
param.setResult(null);
param.setResult(null);
}
}
}