mirror of
https://github.com/areteruhiro/LIME-beta-hiro.git
synced 2025-02-05 21:11:39 +09:00
既読者機能の差し戻し
This commit is contained in:
parent
aaa7f8e455
commit
7c6a915f43
@ -65,6 +65,8 @@ public class ReadChecker implements IHook {
|
||||
private SQLiteDatabase db4 = null;
|
||||
private boolean shouldHookOnCreate = false;
|
||||
private String currentGroupId = null;
|
||||
|
||||
private static final int MAX_RETRY_COUNT = 3; // 最大リトライ回数
|
||||
@Override
|
||||
public void hook(LimeOptions limeOptions, XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
|
||||
if (!limeOptions.ReadChecker.checked) return;
|
||||
@ -146,13 +148,13 @@ public class ReadChecker implements IHook {
|
||||
|
||||
if (shouldHookOnCreate && currentGroupId != null) {
|
||||
|
||||
Activity activity = (Activity) param.thisObject;
|
||||
Activity activity = (Activity) param.thisObject;
|
||||
|
||||
|
||||
addButton(activity, moduleContext);
|
||||
}
|
||||
addButton(activity, moduleContext);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -189,6 +191,7 @@ public class ReadChecker implements IHook {
|
||||
cursor.close();
|
||||
return noGroup;
|
||||
}
|
||||
|
||||
private void addButton(Activity activity, Context moduleContext) {
|
||||
// ファイルパスを取得
|
||||
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "LimeBackup");
|
||||
@ -345,7 +348,9 @@ public class ReadChecker implements IHook {
|
||||
if (limeDatabase == null) {
|
||||
return;
|
||||
}
|
||||
// SQLクエリの初期化
|
||||
String query;
|
||||
// group_id が null のレコードを探し、chat_history から chat_id を取得して更新する
|
||||
Cursor nullGroupIdCursor = limeDatabase.rawQuery("SELECT server_id, user_name FROM read_message WHERE group_id = 'null'", null);
|
||||
while (nullGroupIdCursor.moveToNext()) {
|
||||
String serverId = nullGroupIdCursor.getString(0);
|
||||
@ -353,7 +358,10 @@ public class ReadChecker implements IHook {
|
||||
String chatId = queryDatabase(db3, "SELECT chat_id FROM chat_history WHERE server_id=?", serverId);
|
||||
|
||||
if (chatId != null && !"null".equals(chatId)) {
|
||||
// group_id を更新
|
||||
limeDatabase.execSQL("UPDATE read_message SET group_id=? WHERE server_id=?", new String[]{chatId, serverId});
|
||||
|
||||
// 同じ group_id を持つ他の server_id を検索
|
||||
Cursor sameGroupIdCursor = limeDatabase.rawQuery(
|
||||
"SELECT server_id, user_name FROM read_message WHERE group_id=? AND server_id != ?",
|
||||
new String[]{chatId, serverId}
|
||||
@ -362,9 +370,9 @@ public class ReadChecker implements IHook {
|
||||
while (sameGroupIdCursor.moveToNext()) {
|
||||
String otherServerId = sameGroupIdCursor.getString(0);
|
||||
String otherUserName = sameGroupIdCursor.getString(1);
|
||||
|
||||
if (!userName.equals(otherUserName)) {
|
||||
|
||||
if (!userName.equals(otherUserName)) {
|
||||
// 新しいレコードを作成
|
||||
limeDatabase.execSQL(
|
||||
"INSERT INTO read_message (server_id, group_id, user_name) VALUES (?, ?, ?)",
|
||||
new String[]{otherServerId, chatId, userName}
|
||||
@ -377,8 +385,10 @@ public class ReadChecker implements IHook {
|
||||
nullGroupIdCursor.close();
|
||||
|
||||
if (limeOptions.MySendMessage.checked) {
|
||||
query = "SELECT server_id, content, created_time FROM read_message WHERE group_id=? AND Send_User = 'null' ORDER BY created_time ASC";
|
||||
// Send_User が (null) のメッセージのみを取得するクエリ
|
||||
query = "SELECT server_id, content, created_time FROM read_message WHERE group_id=? AND Send_User = 'null' ORDER BY created_time ASC";
|
||||
} else {
|
||||
// 通常のクエリ
|
||||
query = "SELECT server_id, content, created_time FROM read_message WHERE group_id=? ORDER BY created_time ASC";
|
||||
}
|
||||
|
||||
@ -390,6 +400,8 @@ public class ReadChecker implements IHook {
|
||||
String serverId = cursor.getString(0);
|
||||
String content = cursor.getString(1);
|
||||
String timeFormatted = cursor.getString(2);
|
||||
|
||||
// created_time が "null" の場合、chat_history テーブルから取得してフォーマットする
|
||||
if ("null".equals(timeFormatted)) {
|
||||
String timeEpochStr = queryDatabase(db3, "SELECT created_time FROM chat_history WHERE server_id=?", serverId);
|
||||
if (timeEpochStr != null && !"null".equals(timeEpochStr)) {
|
||||
@ -466,32 +478,43 @@ public class ReadChecker implements IHook {
|
||||
|
||||
private List<String> getuser_namesForServerId(String serverId, SQLiteDatabase db3) {
|
||||
if (limeDatabase == null) {
|
||||
//("limeDatabaseがnullです。");
|
||||
//("limeDatabaseがnullです。");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// ID カラムで昇順ソートするクエリ
|
||||
String query = "SELECT user_name, ID, Sent_User FROM read_message WHERE server_id=? ORDER BY CAST(ID AS INTEGER) ASC";
|
||||
Cursor cursor = limeDatabase.rawQuery(query, new String[]{serverId});
|
||||
List<String> userNames = new ArrayList<>();
|
||||
Set<String> uniqueUserNames = new HashSet<>();
|
||||
Set<String> uniqueUserNames = new HashSet<>(); // 重複排除用の Set
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
String userNameStr = cursor.getString(0);
|
||||
int id = cursor.getInt(1);
|
||||
String SentUser = cursor.getString(2);
|
||||
int id = cursor.getInt(1); // ID を取得
|
||||
String SentUser = cursor.getString(2); // Sent_User を取得
|
||||
|
||||
if (userNameStr != null) {
|
||||
// //("取得したuser_name: " + userNameStr);
|
||||
// //("取得したuser_name: " + userNameStr);
|
||||
//XposedBridge.log("取得したSent_User: " + SentUser);
|
||||
|
||||
// user_name の値をトリミングして "null" かどうかを確認
|
||||
String trimmedUserName = userNameStr.trim();
|
||||
if (trimmedUserName.startsWith("-")) {
|
||||
// "-ユーザー名 [時間]" の形式からユーザー名部分を抽出
|
||||
int bracketIndex = trimmedUserName.indexOf('[');
|
||||
if (bracketIndex != -1) {
|
||||
String userNamePart = trimmedUserName.substring(1, bracketIndex).trim();
|
||||
////("抽出したユーザー名部分: " + userNamePart);
|
||||
|
||||
if (userNamePart.equals("null")) {
|
||||
if (SentUser != null) {
|
||||
// SentUser を mid として使用し、contacts テーブルからユーザー名を再取得
|
||||
if (SentUser != null) {
|
||||
String newUserName = queryDatabase(db4, "SELECT profile_name FROM contacts WHERE mid=?", SentUser);
|
||||
//XposedBridge.log("再取得したnewUserName: " + newUserName);
|
||||
|
||||
if (newUserName != null && !newUserName.equals("null")) {
|
||||
userNameStr = "-" + newUserName + " [" + trimmedUserName.substring(bracketIndex + 1);
|
||||
// 新しいユーザー名を反映
|
||||
userNameStr = "-" + newUserName + " [" + trimmedUserName.substring(bracketIndex + 1);
|
||||
//XposedBridge.log("更新後のuser_name: " + userNameStr);
|
||||
}
|
||||
} else {
|
||||
@ -509,7 +532,7 @@ public class ReadChecker implements IHook {
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
////("最終的なuserNames: " + userNames);
|
||||
////("最終的なuserNames: " + userNames);
|
||||
return userNames;
|
||||
}
|
||||
|
||||
@ -573,7 +596,7 @@ public class ReadChecker implements IHook {
|
||||
moduleContext = appContext.createPackageContext(
|
||||
"io.github.hiro.lime", Context.CONTEXT_IGNORE_SECURITY);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
//("Failed to create package context: " + e.getMessage());
|
||||
//("Failed to create package context: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -619,6 +642,8 @@ public class ReadChecker implements IHook {
|
||||
if (serverId == null || SentUser == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// データベースが利用可能になるまで待機して再試行
|
||||
String SendUser = queryDatabaseWithRetry(db3, "SELECT from_mid FROM chat_history WHERE server_id=?", serverId);
|
||||
if (SendUser == null) {
|
||||
SendUser = "null";
|
||||
@ -672,31 +697,37 @@ public class ReadChecker implements IHook {
|
||||
String finalContent = (content != null && !content.isEmpty() && !content.equals("null"))
|
||||
? content
|
||||
: (!mediaDescription.isEmpty() ? mediaDescription : "No content:" + serverId);
|
||||
String currentTime = getCurrentTime();
|
||||
|
||||
//("セーブメゾットに渡したよ" + serverId + ", Sent_User: " + SentUser);
|
||||
saveData(SendUser, groupId, serverId, SentUser, groupName, finalContent, user_name, timeFormatted, context,currentTime);
|
||||
|
||||
saveData(SendUser, groupId, serverId, SentUser, groupName, finalContent, user_name, timeFormatted, context);
|
||||
|
||||
} catch (Resources.NotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InterruptedException e) {
|
||||
// 割り込みが発生した場合の処理
|
||||
Thread.currentThread().interrupt(); // 割り込み状態を再設定
|
||||
// System.out.println("Database operation was interrupted");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String queryDatabaseWithRetry(SQLiteDatabase db, String query, String... params) {
|
||||
private String queryDatabaseWithRetry(SQLiteDatabase db, String query, String... params) throws InterruptedException {
|
||||
final int RETRY_DELAY_MS = 100;
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
// 割り込みが発生したかどうかをチェック
|
||||
if (Thread.currentThread().isInterrupted()) {
|
||||
throw new InterruptedException("Thread was interrupted");
|
||||
}
|
||||
|
||||
return queryDatabase(db, query, params);
|
||||
} catch (SQLiteDatabaseLockedException e) {
|
||||
|
||||
try {
|
||||
Thread.sleep(RETRY_DELAY_MS);
|
||||
} catch (InterruptedException ie) {
|
||||
// 割り込みが発生した場合、スレッドの割り込み状態を再設定し、例外をスロー
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Thread interrupted while waiting for database", ie);
|
||||
throw ie;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -705,14 +736,15 @@ public class ReadChecker implements IHook {
|
||||
|
||||
private String formatMessageTime(String timeEpochStr) {
|
||||
if (timeEpochStr == null || timeEpochStr.trim().isEmpty()) {
|
||||
return "null";
|
||||
return "null"; // null または空文字列の場合 "null" を返す
|
||||
}
|
||||
|
||||
try {
|
||||
long timeEpoch = Long.parseLong(timeEpochStr); // 数値に変換
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
|
||||
return sdf.format(new Date(timeEpoch));
|
||||
return sdf.format(new Date(timeEpoch)); // フォーマットして返す
|
||||
} catch (NumberFormatException e) {
|
||||
// 数値として不正な形式の場合
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
@ -728,7 +760,7 @@ public class ReadChecker implements IHook {
|
||||
private String extractServerId(String paramValue, Context context) {
|
||||
Pattern pattern = Pattern.compile("param3:([0-9]+)");
|
||||
Matcher matcher = pattern.matcher(paramValue);
|
||||
//(paramValue);
|
||||
//(paramValue);
|
||||
if (matcher.find()) {
|
||||
return matcher.group(1);
|
||||
|
||||
@ -742,7 +774,7 @@ public class ReadChecker implements IHook {
|
||||
|
||||
private String queryDatabase(SQLiteDatabase db, String query, String... selectionArgs) {
|
||||
if (db == null) {
|
||||
//("Database is not initialized.");
|
||||
//("Database is not initialized.");
|
||||
return null;
|
||||
}
|
||||
Cursor cursor = db.rawQuery(query, selectionArgs);
|
||||
@ -781,11 +813,11 @@ public class ReadChecker implements IHook {
|
||||
"created_time TEXT" +
|
||||
");";
|
||||
limeDatabase.execSQL(createGroupTable);
|
||||
//("Database initialized and read_message table created with ID column.");
|
||||
//("Database initialized and read_message table created with ID column.");
|
||||
}
|
||||
|
||||
|
||||
private void saveData(String SendUser, String groupId, String serverId, String SentUser, String groupName, String content, String user_name, String timeFormatted, Context context,String currentTime) {
|
||||
private void saveData(String SendUser, String groupId, String serverId, String SentUser, String groupName, String content, String user_name, String timeFormatted, Context context) {
|
||||
// nullの場合に"null"を代入
|
||||
SendUser = (SendUser == null) ? "null" : SendUser;
|
||||
groupId = (groupId == null) ? "null" : groupId;
|
||||
@ -796,7 +828,7 @@ public class ReadChecker implements IHook {
|
||||
user_name = (user_name == null) ? "null" : user_name;
|
||||
timeFormatted = (timeFormatted == null) ? "null" : timeFormatted;
|
||||
|
||||
//("セーブメゾットまで処理されたよ: serverId=" + serverId + ", Sent_User=" + SentUser);
|
||||
//("セーブメゾットまで処理されたよ: serverId=" + serverId + ", Sent_User=" + SentUser);
|
||||
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
@ -807,7 +839,7 @@ public class ReadChecker implements IHook {
|
||||
if (cursor.moveToFirst()) {
|
||||
int count = cursor.getInt(0);
|
||||
String existingUserName = cursor.getString(1);
|
||||
|
||||
String currentTime = getCurrentTime();
|
||||
|
||||
if (count > 0) {
|
||||
// 既存のレコードがある場合
|
||||
@ -817,17 +849,17 @@ public class ReadChecker implements IHook {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("user_name", updatedUserName);
|
||||
limeDatabase.update("read_message", values, "server_id=? AND Sent_User=?", new String[]{serverId, SentUser});
|
||||
//("User name updated for server_id: " + serverId + ", Sent_User: " + SentUser);
|
||||
//("User name updated for server_id: " + serverId + ", Sent_User: " + SentUser);
|
||||
}
|
||||
} else {
|
||||
// 新しいレコードを挿入する
|
||||
//("insertNewRecordにデータを渡したよ");
|
||||
//("insertNewRecordにデータを渡したよ");
|
||||
insertNewRecord(SendUser, groupId, serverId, SentUser, groupName, content, "-" + user_name + " [" + currentTime + "]", timeFormatted);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 例外が発生した場合にログを出力
|
||||
//("saveDataでエラーが発生しました: " + e.getMessage());
|
||||
//("saveDataでエラーが発生しました: " + e.getMessage());
|
||||
} finally {
|
||||
// カーソルを閉じる
|
||||
if (cursor != null) {
|
||||
@ -843,8 +875,8 @@ public class ReadChecker implements IHook {
|
||||
}
|
||||
|
||||
private void insertNewRecord(String SendUser, String groupId, String serverId, String SentUser, String groupName, String content, String user_name, String timeFormatted) {
|
||||
//("Attempting to insert new record: server_id=" + serverId + ", Sent_User=" + SentUser);
|
||||
//("Inserting values: groupId=" + groupId + ", serverId=" + serverId + ", SentUser=" + SentUser + ", SendUser=" + SendUser + ", groupName=" + groupName + ", content=" + content + ", user_name=" + user_name + ", timeFormatted=" + timeFormatted);
|
||||
//("Attempting to insert new record: server_id=" + serverId + ", Sent_User=" + SentUser);
|
||||
//("Inserting values: groupId=" + groupId + ", serverId=" + serverId + ", SentUser=" + SentUser + ", SendUser=" + SendUser + ", groupName=" + groupName + ", content=" + content + ", user_name=" + user_name + ", timeFormatted=" + timeFormatted);
|
||||
|
||||
// 新しいレコードを挿入
|
||||
insertRecord(SendUser, groupId, serverId, SentUser, groupName, content, user_name, timeFormatted);
|
||||
@ -869,11 +901,11 @@ public class ReadChecker implements IHook {
|
||||
if (!otherSentUser.equals(SentUser)) {
|
||||
// user_name のみ変更して新しいレコードを挿入
|
||||
insertRecord(otherSendUser, groupId, otherServerId, SentUser, otherGroupName, otherContent, user_name, othertimeFormatted);
|
||||
//("Copied record inserted: server_id=" + otherServerId + ", Sent_User=" + SentUser);
|
||||
//("Copied record inserted: server_id=" + otherServerId + ", Sent_User=" + SentUser);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//("Error during copying records: " + e.getMessage());
|
||||
//("Error during copying records: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
@ -899,9 +931,9 @@ public class ReadChecker implements IHook {
|
||||
limeDatabase.beginTransaction();
|
||||
limeDatabase.execSQL(insertQuery, new Object[]{groupId, serverId, SentUser, sendUserValue, groupName, content, user_name, timeFormatted});
|
||||
limeDatabase.setTransactionSuccessful();
|
||||
//("New record inserted successfully: server_id=" + serverId + ", Sent_User=" + SentUser);
|
||||
//("New record inserted successfully: server_id=" + serverId + ", Sent_User=" + SentUser);
|
||||
} catch (Exception e) {
|
||||
//("Error inserting new record: " + e.getMessage());
|
||||
//("Error inserting new record: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
limeDatabase.endTransaction();
|
||||
@ -914,4 +946,4 @@ public class ReadChecker implements IHook {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user