diff --git a/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt index 9fff3db354739212f4dbe88364bd9dac58b5d957..efa51ad6586daf5f238f330df0cbf904cebbd9e5 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt @@ -22,6 +22,7 @@ import android.content.Context import android.os.Environment import android.provider.Telephony import androidx.core.content.contentValuesOf +import com.moez.QKSMS.util.FileUtils import com.moez.QKSMS.model.BackupFile import com.moez.QKSMS.model.Message import com.moez.QKSMS.util.Preferences @@ -53,7 +54,8 @@ class BackupRepositoryImpl @Inject constructor( ) : BackupRepository { companion object { - private val BACKUP_DIRECTORY = Environment.getExternalStorageDirectory().toString() + "/QKSMS/Backups" + private val OLD_BACKUP_DIRECTORY = Environment.getExternalStorageDirectory().toString() + "/QKSMS/Backups" + private val BACKUP_DIRECTORY = Environment.getExternalStorageDirectory().toString() + "/Message/Backups" } data class Backup( @@ -151,7 +153,10 @@ class BackupRepositoryImpl @Inject constructor( override fun getBackupProgress(): Observable = backupProgress - override fun getBackups(): Observable> = QkFileObserver(BACKUP_DIRECTORY).observable + override fun getBackups(): Observable> { + FileUtils.moveDir(File(OLD_BACKUP_DIRECTORY), File(BACKUP_DIRECTORY)) + + return QkFileObserver(BACKUP_DIRECTORY).observable .map { File(BACKUP_DIRECTORY).listFiles() ?: arrayOf() } .subscribeOn(Schedulers.io()) .observeOn(Schedulers.computation()) @@ -170,6 +175,7 @@ class BackupRepositoryImpl @Inject constructor( } } .map { files -> files.sortedByDescending { file -> file.date } } + } override fun performRestore(filePath: String) { // If a backupFile or restore is already running, don't do anything diff --git a/data/src/main/java/com/moez/QKSMS/repository/MessageRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/MessageRepositoryImpl.kt index 7cdddf1d24b34a88da5ebb12c1a96bca12509ad9..7fbd3d5364060f12e5a3919f9c59aa7ddd0635cc 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/MessageRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/MessageRepositoryImpl.kt @@ -46,6 +46,7 @@ import com.moez.QKSMS.compat.TelephonyCompat import com.moez.QKSMS.extensions.anyOf import com.moez.QKSMS.manager.ActiveConversationManager import com.moez.QKSMS.manager.KeyManager +import com.moez.QKSMS.util.FileUtils import com.moez.QKSMS.model.Attachment import com.moez.QKSMS.model.Conversation import com.moez.QKSMS.model.Message @@ -83,7 +84,14 @@ class MessageRepositoryImpl @Inject constructor( private val syncRepository: SyncRepository ) : MessageRepository { + companion object { + private val OLD_MEDIA_DIRECTORY = Environment.getExternalStorageDirectory().toString() + "/QKSMS/Media" + private val MEDIA_DIRECTORY = Environment.getExternalStorageDirectory().toString() + "/Message/Media" + } + override fun getMessages(threadId: Long, query: String): RealmResults { + FileUtils.moveDir(File(OLD_MEDIA_DIRECTORY), File(MEDIA_DIRECTORY)) + return Realm.getDefaultInstance() .where(Message::class.java) .equalTo("threadId", threadId) @@ -172,7 +180,8 @@ class MessageRepositoryImpl @Inject constructor( val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(part.type) ?: return null val date = part.messages?.first()?.date - val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Media").apply { mkdirs() } + val dir = File(MEDIA_DIRECTORY).apply { mkdirs() } + val fileName = part.name?.takeIf { name -> name.endsWith(extension) } ?: "${part.type.split("/").last()}_$date.$extension" var file: File diff --git a/data/src/main/java/com/moez/QKSMS/util/FileUtils.kt b/data/src/main/java/com/moez/QKSMS/util/FileUtils.kt new file mode 100644 index 0000000000000000000000000000000000000000..2ac317f3ddf23c716088ce12138f75cb64f5e6dc --- /dev/null +++ b/data/src/main/java/com/moez/QKSMS/util/FileUtils.kt @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2022 MURENA SAS + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.moez.QKSMS.util + +import java.io.File + +object FileUtils { + + fun moveDir(oldDir: File, newDir: File) { + if (oldDir.exists() && oldDir.isDirectory) { + newDir.apply { mkdirs() } + oldDir.copyRecursively(newDir) + oldDir.deleteRecursively() + } + } + +} diff --git a/presentation/src/main/java/com/moez/QKSMS/common/util/FileLoggingTree.kt b/presentation/src/main/java/com/moez/QKSMS/common/util/FileLoggingTree.kt index aa1dd63221d203cb57ae66f56de7518d2d446fc6..8dbdcc2dabc1b1e28b53ba0a6ea1847f0fc14c46 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/util/FileLoggingTree.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/util/FileLoggingTree.kt @@ -20,6 +20,7 @@ package com.moez.QKSMS.common.util import android.os.Environment import android.util.Log +import com.moez.QKSMS.util.FileUtils import com.moez.QKSMS.util.Preferences import io.reactivex.schedulers.Schedulers import timber.log.Timber @@ -59,7 +60,9 @@ class FileLoggingTree @Inject constructor(private val prefs: Preferences) : Timb synchronized(fileLock) { try { // Create the directory - val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Logs").apply { mkdirs() } + val dir = File(Environment.getExternalStorageDirectory(), "Message/Logs").apply { mkdirs() } + val oldDir = File(Environment.getExternalStorageDirectory(), "QKSMS/Logs") + FileUtils.moveDir(oldDir, dir) // Create the file val file = File(dir, "${SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(System.currentTimeMillis())}.log") diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index abd36c21322fbc8df8ac6d31e697d89da95bb89a..7464dd0d5c621b5836ffaf7fda6ef8a4927cae9d 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -168,7 +168,7 @@ Stop restore Messages that have already been restored will remain on your device Backups - /MESSAGE/Backups + /Message/Backups No backups found No messages @@ -279,8 +279,8 @@ Blocked conversations Blocking Manager - MESSAGE - Built-in blocking functionality in MESSAGE + Message + Built-in blocking functionality in Message Call Blocker - Incoming/Outgoing Block spam messages, numbers & unknown calls with blacklist & Schedule Call Control