Skip to content

Commit

Permalink
自動バックアップ機能(24H間隔)←間隔設定はまだできません
Browse files Browse the repository at this point in the history
既読についての機能改善
  • Loading branch information
areteruhiro committed Oct 31, 2024
1 parent cd73043 commit 1634438
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 103 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdk 28
targetSdk 34
versionCode 15
versionName "1.12.2"
versionName "1.12.3"
multiDexEnabled false
proguardFiles += 'proguard-rules.pro'
buildConfigField 'String', 'HOOK_TARGET_VERSION', '"141700420"'
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/io/github/hiro/lime/LimeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public Option(String name, int id, boolean checked) {
public Option ReadChecker = new Option("ReadChecker", R.string.ReadChecker, false);
public Option RemoveNotification = new Option("RemoveNotification", R.string.removeNotification, false);
public Option NaviColor = new Option("NaviColor", R.string.NaviColor, false);
public Option AutomaticBackup = new Option("AutomaticBackup", R.string.AutomaticBackup, false);




public Option[] options = {
Expand Down Expand Up @@ -71,6 +74,7 @@ public Option(String name, int id, boolean checked) {
outputCommunication,
calltone,
NaviColor,
AutomaticBackup

};
}
2 changes: 2 additions & 0 deletions app/src/main/java/io/github/hiro/lime/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import de.robv.android.xposed.callbacks.XC_LayoutInflated;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import io.github.hiro.lime.hooks.AddRegistrationOptions;
import io.github.hiro.lime.hooks.AutomaticBackup;
import io.github.hiro.lime.hooks.BlockTracking;
import io.github.hiro.lime.hooks.CheckHookTargetVersion;
import io.github.hiro.lime.hooks.Constants;
Expand Down Expand Up @@ -79,6 +80,7 @@ public class Main implements IXposedHookLoadPackage, IXposedHookInitPackageResou
new ReadChecker(),
new NaviColor(),
new KeepUnreadLSpatch(),
new AutomaticBackup()

};

Expand Down
108 changes: 108 additions & 0 deletions app/src/main/java/io/github/hiro/lime/hooks/AutomaticBackup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package io.github.hiro.lime.hooks;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import io.github.hiro.lime.LimeOptions;
import io.github.hiro.lime.hooks.IHook;

public class AutomaticBackup implements IHook {

private static final String TAG = "AutomaticBackup";
private ScheduledExecutorService scheduler;
private static final String PREFS_NAME = "BackupPrefs";
public static final String KEY_BACKUP_INTERVAL = "backup_interval_in_millis";
private static final long DAILY_INTERVAL = 24 * 60 * 60 * 1000;

@Override
public void hook(LimeOptions limeOptions, XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
if (!limeOptions.AutomaticBackup.checked) return;

XposedBridge.hookAllMethods(Activity.class, "onCreate", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Activity activity = (Activity) param.thisObject;
Context appContext = activity.getApplicationContext();
if (scheduler == null || scheduler.isShutdown()) {
startScheduledBackup(appContext);
}
}
});
}

private void startScheduledBackup(Context appContext) {
// 1日ごとにバックアップを行う
if (scheduler == null) {
scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> checkAndBackupChatHistory(appContext), 0, DAILY_INTERVAL, TimeUnit.MILLISECONDS);
}
}

private void checkAndBackupChatHistory(Context appContext) {
File backupDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "LimeBackup");
File[] backupFiles = backupDir.listFiles((dir, name) -> name.startsWith("naver_line_backup_"));

long currentTime = System.currentTimeMillis();
long lastBackupTime = 0;

if (backupFiles != null && backupFiles.length > 0) {
File latestBackupFile = backupFiles[0];
for (File file : backupFiles) {
if (file.lastModified() > latestBackupFile.lastModified()) {
latestBackupFile = file;
}
}
lastBackupTime = latestBackupFile.lastModified();
}

// 最後のバックアップから指定された間隔が経過しているか確認
if (currentTime - lastBackupTime >= DAILY_INTERVAL) {
backupChatHistory(appContext);

}
}

private void backupChatHistory(Context appContext) {
File originalDbFile = appContext.getDatabasePath("naver_line");
File backupDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "LimeBackup");
if (!backupDir.exists() && !backupDir.mkdirs()) {
return;
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File backupFileWithTimestamp = new File(backupDir, "naver_line_backup_" + timeStamp + ".db");

try (FileChannel source = new FileInputStream(originalDbFile).getChannel()) {
try (FileChannel destinationWithTimestamp = new FileOutputStream(backupFileWithTimestamp).getChannel()) {
destinationWithTimestamp.transferFrom(source, 0, source.size());
}
showToast(appContext, "自動バックアップが成功しました"); // トーストをUIスレッドで表示

} catch (IOException e) {
showToast(appContext, "自動バックアップ中にエラーが発生しました: " + e.getMessage());
}
}

// トーストをUIスレッドで表示するヘルパーメソッド
private void showToast(final Context context, final String message) {
new android.os.Handler(context.getMainLooper()).post(() ->
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
);
}
}
28 changes: 13 additions & 15 deletions app/src/main/java/io/github/hiro/lime/hooks/EmbedOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static android.content.ContentValues.TAG;

import static io.github.hiro.lime.hooks.AutomaticBackup.KEY_BACKUP_INTERVAL;

import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
Expand All @@ -22,12 +24,15 @@
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
Expand Down Expand Up @@ -227,6 +232,7 @@ public void onClick(View v) {
layout.addView(restorefolderButton);



builder.setPositiveButton(R.string.positive_button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expand Down Expand Up @@ -463,39 +469,31 @@ public void onClick(View view) {
);
}


private void addButton(LinearLayout layout, String buttonText, View.OnClickListener listener) {
Button button = new Button(layout.getContext());
button.setText(buttonText);
button.setOnClickListener(listener);
layout.addView(button);
}

private void backupChatHistory(Context appContext) {
File originalDbFile = appContext.getDatabasePath("naver_line");

File backupDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "LimeBackup");

if (!backupDir.exists()) {
if (!backupDir.mkdirs()) {
Log.e(TAG, "Failed to create backup directory: " + backupDir.getAbsolutePath());
return;
}
}

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String backupFileNameWithTimestamp = "naver_line_backup_" + timeStamp + ".db";
String backupFileNameFixed = "naver_line_backup.db";

File backupFileWithTimestamp = new File(backupDir, backupFileNameWithTimestamp);
File backupFileFixed = new File(backupDir, backupFileNameFixed);

try (FileChannel source = new FileInputStream(originalDbFile).getChannel()) {
try (FileChannel destinationWithTimestamp = new FileOutputStream(backupFileWithTimestamp).getChannel()) {
destinationWithTimestamp.transferFrom(source, 0, source.size());
}
source.position(0);

source.position(0);
try (FileChannel destinationFixed = new FileOutputStream(backupFileFixed).getChannel()) {
destinationFixed.transferFrom(source, 0, source.size());
}
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/io/github/hiro/lime/hooks/KeepUnread.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ private Drawable scaleDrawable(Drawable drawable, int width, int height) {
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)) {
Expand Down
Loading

0 comments on commit 1634438

Please sign in to comment.