From 4d378e819c685fd34c021d150a2fd9d5f19e66a5 Mon Sep 17 00:00:00 2001 From: merkost Date: Tue, 18 Jul 2023 16:23:04 +1000 Subject: [PATCH] ImportResult extracted to a separate class and MainActivity cleared --- .../smsmessenger/activities/MainActivity.kt | 119 ------------------ .../smsmessenger/helpers/MessagesExporter.kt | 68 ---------- .../smsmessenger/models/ImportResult.kt | 5 + app/src/main/res/menu/menu_main.xml | 10 -- 4 files changed, 5 insertions(+), 197 deletions(-) delete mode 100644 app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ImportResult.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt index 1204d748..07f056c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt @@ -3,19 +3,15 @@ package com.simplemobiletools.smsmessenger.activities import android.annotation.SuppressLint import android.app.Activity import android.app.role.RoleManager -import android.content.ActivityNotFoundException import android.content.Intent import android.content.pm.ShortcutInfo import android.content.pm.ShortcutManager import android.graphics.drawable.Icon import android.graphics.drawable.LayerDrawable -import android.net.Uri import android.os.Bundle import android.provider.Telephony import android.text.TextUtils -import android.widget.Toast import androidx.coordinatorlayout.widget.CoordinatorLayout -import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* @@ -25,8 +21,6 @@ import com.simplemobiletools.smsmessenger.BuildConfig import com.simplemobiletools.smsmessenger.R import com.simplemobiletools.smsmessenger.adapters.ConversationsAdapter import com.simplemobiletools.smsmessenger.adapters.SearchResultsAdapter -import com.simplemobiletools.smsmessenger.dialogs.ExportMessagesDialog -import com.simplemobiletools.smsmessenger.dialogs.ImportMessagesDialog import com.simplemobiletools.smsmessenger.extensions.* import com.simplemobiletools.smsmessenger.helpers.* import com.simplemobiletools.smsmessenger.models.Conversation @@ -37,19 +31,14 @@ import kotlinx.android.synthetic.main.activity_main.* import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode -import java.io.FileOutputStream -import java.io.OutputStream class MainActivity : SimpleActivity() { private val MAKE_DEFAULT_APP_REQUEST = 1 - private val PICK_IMPORT_SOURCE_INTENT = 11 - private val PICK_EXPORT_FILE_INTENT = 21 private var storedTextColor = 0 private var storedFontSize = 0 private var lastSearchedText = "" private var bus: EventBus? = null - private val smsExporter by lazy { MessagesExporter(this) } private var wasProtectionHandled = false @SuppressLint("InlinedApi") @@ -174,8 +163,6 @@ class MainActivity : SimpleActivity() { main_menu.getToolbar().setOnMenuItemClickListener { menuItem -> when (menuItem.itemId) { - R.id.import_messages -> tryImportMessages() - R.id.export_messages -> tryToExportMessages() R.id.more_apps_from_us -> launchMoreAppsFromUsIntent() R.id.settings -> launchSettings() R.id.about -> launchAbout() @@ -199,11 +186,6 @@ class MainActivity : SimpleActivity() { } else { finish() } - } else if (requestCode == PICK_IMPORT_SOURCE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) { - tryImportMessagesFromFile(resultData.data!!) - } else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) { - val outputStream = contentResolver.openOutputStream(resultData.data!!) - exportMessagesTo(outputStream) } } @@ -578,107 +560,6 @@ class MainActivity : SimpleActivity() { startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true) } - private fun tryToExportMessages() { - if (isQPlus()) { - ExportMessagesDialog(this, config.lastExportPath, true) { file -> - Intent(Intent.ACTION_CREATE_DOCUMENT).apply { - type = EXPORT_MIME_TYPE - putExtra(Intent.EXTRA_TITLE, file.name) - addCategory(Intent.CATEGORY_OPENABLE) - - try { - startActivityForResult(this, PICK_EXPORT_FILE_INTENT) - } catch (e: ActivityNotFoundException) { - toast(R.string.system_service_disabled, Toast.LENGTH_LONG) - } catch (e: Exception) { - showErrorToast(e) - } - } - } - } else { - handlePermission(PERMISSION_WRITE_STORAGE) { - if (it) { - ExportMessagesDialog(this, config.lastExportPath, false) { file -> - getFileOutputStream(file.toFileDirItem(this), true) { outStream -> - exportMessagesTo(outStream) - } - } - } - } - } - } - - private fun exportMessagesTo(outputStream: OutputStream?) { - toast(R.string.exporting) - ensureBackgroundThread { - smsExporter.exportMessages(outputStream) { - val toastId = when (it) { - MessagesExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful - else -> R.string.exporting_failed - } - - toast(toastId) - } - } - } - - private fun tryImportMessages() { - if (isQPlus()) { - Intent(Intent.ACTION_GET_CONTENT).apply { - addCategory(Intent.CATEGORY_OPENABLE) - type = EXPORT_MIME_TYPE - - try { - startActivityForResult(this, PICK_IMPORT_SOURCE_INTENT) - } catch (e: ActivityNotFoundException) { - toast(R.string.system_service_disabled, Toast.LENGTH_LONG) - } catch (e: Exception) { - showErrorToast(e) - } - } - } else { - handlePermission(PERMISSION_READ_STORAGE) { - if (it) { - importEvents() - } - } - } - } - - private fun importEvents() { - FilePickerDialog(this) { - showImportEventsDialog(it) - } - } - - private fun showImportEventsDialog(path: String) { - ImportMessagesDialog(this, path) - } - - private fun tryImportMessagesFromFile(uri: Uri) { - when (uri.scheme) { - "file" -> showImportEventsDialog(uri.path!!) - "content" -> { - val tempFile = getTempFile("messages", "backup.json") - if (tempFile == null) { - toast(R.string.unknown_error_occurred) - return - } - - try { - val inputStream = contentResolver.openInputStream(uri) - val out = FileOutputStream(tempFile) - inputStream!!.copyTo(out) - showImportEventsDialog(tempFile.absolutePath) - } catch (e: Exception) { - showErrorToast(e) - } - } - - else -> toast(R.string.invalid_file_format) - } - } - @Subscribe(threadMode = ThreadMode.MAIN) fun refreshMessages(event: Events.RefreshMessages) { initMessenger() diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt deleted file mode 100644 index 49c2d4b6..00000000 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt +++ /dev/null @@ -1,68 +0,0 @@ -package com.simplemobiletools.smsmessenger.helpers - -import android.content.Context -import com.google.gson.Gson -import com.google.gson.stream.JsonWriter -import com.simplemobiletools.commons.helpers.ensureBackgroundThread -import com.simplemobiletools.smsmessenger.extensions.config -import com.simplemobiletools.smsmessenger.extensions.getConversationIds -import java.io.OutputStream - -class MessagesExporter(private val context: Context) { - enum class ExportResult { - EXPORT_FAIL, EXPORT_OK - } - - private val config = context.config - private val messageReader = MessagesReader(context) - private val gson = Gson() - - fun exportMessages(outputStream: OutputStream?, onProgress: (total: Int, current: Int) -> Unit = { _, _ -> }, callback: (result: ExportResult) -> Unit) { - ensureBackgroundThread { - if (outputStream == null) { - callback.invoke(ExportResult.EXPORT_FAIL) - return@ensureBackgroundThread - } - val writer = JsonWriter(outputStream.bufferedWriter()) - writer.use { - try { - var written = 0 - writer.beginArray() - val conversationIds = context.getConversationIds() - val totalMessages = messageReader.getMessagesCount() - for (threadId in conversationIds) { - writer.beginObject() - if (config.exportSms && messageReader.getSmsCount() > 0) { - writer.name("sms") - writer.beginArray() - messageReader.forEachSms(threadId) { - writer.jsonValue(gson.toJson(it)) - written++ - onProgress.invoke(totalMessages, written) - } - writer.endArray() - } - - if (config.exportMms && messageReader.getMmsCount() > 0) { - writer.name("mms") - writer.beginArray() - messageReader.forEachMms(threadId) { - writer.jsonValue(gson.toJson(it)) - written++ - onProgress.invoke(totalMessages, written) - } - - writer.endArray() - } - - writer.endObject() - } - writer.endArray() - callback.invoke(ExportResult.EXPORT_OK) - } catch (e: Exception) { - callback.invoke(ExportResult.EXPORT_FAIL) - } - } - } - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ImportResult.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ImportResult.kt new file mode 100644 index 00000000..c4151d15 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ImportResult.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.smsmessenger.models + +enum class ImportResult { + IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW +} diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index d60f5871..0c9486f7 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -3,16 +3,6 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:ignore="AppCompatResource,AlwaysShowAction"> - -