FEATURE: Import and export blocked keywords

This commit is contained in:
ronniedroid 2024-01-30 15:42:42 +03:00
parent 0058d6c6f9
commit 958644b290
9 changed files with 365 additions and 5 deletions

View file

@ -0,0 +1,29 @@
package org.fossify.messages.helpers
import org.fossify.commons.helpers.ExportResult
import java.io.OutputStream
object BlockedKeywordsExporter {
fun exportBlockedKeywords(
blockedKeywords: ArrayList<String>,
outputStream: OutputStream?,
callback: (result: ExportResult) -> Unit,
) {
if (outputStream == null) {
callback.invoke(ExportResult.EXPORT_FAIL)
return
}
try {
outputStream.bufferedWriter().use { out ->
out.write(blockedKeywords.joinToString(BLOCKED_KEYWORDS_EXPORT_DELIMITER) {
it
})
}
callback.invoke(ExportResult.EXPORT_OK)
} catch (e: Exception) {
callback.invoke(ExportResult.EXPORT_FAIL)
}
}
}

View file

@ -0,0 +1,37 @@
package org.fossify.messages.helpers
import android.app.Activity
import org.fossify.commons.extensions.showErrorToast
import org.fossify.messages.extensions.config
import java.io.File
class BlockedKeywordsImporter(
private val activity: Activity,
) {
enum class ImportResult {
IMPORT_FAIL, IMPORT_OK
}
fun importBlockedKeywords(path: String): ImportResult {
return try {
val inputStream = File(path).inputStream()
val keywords = inputStream.bufferedReader().use {
val content = it.readText().trimEnd().split(BLOCKED_KEYWORDS_EXPORT_DELIMITER)
content
}
if (keywords.isNotEmpty()) {
keywords.forEach { keyword: String ->
activity.config.addBlockedKeyword(keyword)
}
ImportResult.IMPORT_OK
} else {
ImportResult.IMPORT_FAIL
}
} catch (e: Exception) {
activity.showErrorToast(e)
ImportResult.IMPORT_FAIL
}
}
}

View file

@ -2,6 +2,7 @@ package org.fossify.messages.helpers
import android.content.Context
import org.fossify.commons.helpers.BaseConfig
import org.fossify.commons.helpers.LAST_BLOCKED_NUMBERS_EXPORT_PATH
import org.fossify.messages.extensions.getDefaultKeyboardHeight
import org.fossify.messages.models.Conversation
@ -127,4 +128,8 @@ class Config(context: Context) : BaseConfig(context) {
fun removeCustomNotificationsByThreadId(threadId: Long) {
customNotifications = customNotifications.minus(threadId.toString())
}
var lastBlockedKeywordExportPath: String
get() = prefs.getString(LAST_BLOCKED_KEYWORD_EXPORT_PATH, "")!!
set(lastBlockedNumbersExportPath) = prefs.edit().putString(LAST_BLOCKED_KEYWORD_EXPORT_PATH, lastBlockedNumbersExportPath).apply()
}

View file

@ -26,6 +26,9 @@ const val SEND_GROUP_MESSAGE_MMS = "send_group_message_mms"
const val MMS_FILE_SIZE_LIMIT = "mms_file_size_limit"
const val PINNED_CONVERSATIONS = "pinned_conversations"
const val BLOCKED_KEYWORDS = "blocked_keywords"
const val BLOCKED_KEYWORDS_EXPORT_DELIMITER = ","
const val BLOCKED_KEYWORDS_EXPORT_EXTENSION = ".txt"
const val LAST_BLOCKED_KEYWORD_EXPORT_PATH = "last_blocked_keyword_export_path"
const val EXPORT_SMS = "export_sms"
const val EXPORT_MMS = "export_mms"
const val JSON_FILE_EXTENSION = ".json"