ImportResult extracted to a separate class and MainActivity cleared
This commit is contained in:
parent
30b100b62f
commit
4d378e819c
|
@ -3,19 +3,15 @@ package com.simplemobiletools.smsmessenger.activities
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.role.RoleManager
|
import android.app.role.RoleManager
|
||||||
import android.content.ActivityNotFoundException
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ShortcutInfo
|
import android.content.pm.ShortcutInfo
|
||||||
import android.content.pm.ShortcutManager
|
import android.content.pm.ShortcutManager
|
||||||
import android.graphics.drawable.Icon
|
import android.graphics.drawable.Icon
|
||||||
import android.graphics.drawable.LayerDrawable
|
import android.graphics.drawable.LayerDrawable
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||||
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
|
||||||
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
|
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.*
|
||||||
import com.simplemobiletools.commons.helpers.*
|
import com.simplemobiletools.commons.helpers.*
|
||||||
|
@ -25,8 +21,6 @@ import com.simplemobiletools.smsmessenger.BuildConfig
|
||||||
import com.simplemobiletools.smsmessenger.R
|
import com.simplemobiletools.smsmessenger.R
|
||||||
import com.simplemobiletools.smsmessenger.adapters.ConversationsAdapter
|
import com.simplemobiletools.smsmessenger.adapters.ConversationsAdapter
|
||||||
import com.simplemobiletools.smsmessenger.adapters.SearchResultsAdapter
|
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.extensions.*
|
||||||
import com.simplemobiletools.smsmessenger.helpers.*
|
import com.simplemobiletools.smsmessenger.helpers.*
|
||||||
import com.simplemobiletools.smsmessenger.models.Conversation
|
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.EventBus
|
||||||
import org.greenrobot.eventbus.Subscribe
|
import org.greenrobot.eventbus.Subscribe
|
||||||
import org.greenrobot.eventbus.ThreadMode
|
import org.greenrobot.eventbus.ThreadMode
|
||||||
import java.io.FileOutputStream
|
|
||||||
import java.io.OutputStream
|
|
||||||
|
|
||||||
class MainActivity : SimpleActivity() {
|
class MainActivity : SimpleActivity() {
|
||||||
private val MAKE_DEFAULT_APP_REQUEST = 1
|
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 storedTextColor = 0
|
||||||
private var storedFontSize = 0
|
private var storedFontSize = 0
|
||||||
private var lastSearchedText = ""
|
private var lastSearchedText = ""
|
||||||
private var bus: EventBus? = null
|
private var bus: EventBus? = null
|
||||||
private val smsExporter by lazy { MessagesExporter(this) }
|
|
||||||
private var wasProtectionHandled = false
|
private var wasProtectionHandled = false
|
||||||
|
|
||||||
@SuppressLint("InlinedApi")
|
@SuppressLint("InlinedApi")
|
||||||
|
@ -174,8 +163,6 @@ class MainActivity : SimpleActivity() {
|
||||||
|
|
||||||
main_menu.getToolbar().setOnMenuItemClickListener { menuItem ->
|
main_menu.getToolbar().setOnMenuItemClickListener { menuItem ->
|
||||||
when (menuItem.itemId) {
|
when (menuItem.itemId) {
|
||||||
R.id.import_messages -> tryImportMessages()
|
|
||||||
R.id.export_messages -> tryToExportMessages()
|
|
||||||
R.id.more_apps_from_us -> launchMoreAppsFromUsIntent()
|
R.id.more_apps_from_us -> launchMoreAppsFromUsIntent()
|
||||||
R.id.settings -> launchSettings()
|
R.id.settings -> launchSettings()
|
||||||
R.id.about -> launchAbout()
|
R.id.about -> launchAbout()
|
||||||
|
@ -199,11 +186,6 @@ class MainActivity : SimpleActivity() {
|
||||||
} else {
|
} else {
|
||||||
finish()
|
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)
|
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)
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
fun refreshMessages(event: Events.RefreshMessages) {
|
fun refreshMessages(event: Events.RefreshMessages) {
|
||||||
initMessenger()
|
initMessenger()
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.simplemobiletools.smsmessenger.models
|
||||||
|
|
||||||
|
enum class ImportResult {
|
||||||
|
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW
|
||||||
|
}
|
|
@ -3,16 +3,6 @@
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
tools:ignore="AppCompatResource,AlwaysShowAction">
|
tools:ignore="AppCompatResource,AlwaysShowAction">
|
||||||
<item
|
|
||||||
android:id="@+id/import_messages"
|
|
||||||
android:showAsAction="never"
|
|
||||||
android:title="@string/import_messages"
|
|
||||||
app:showAsAction="never" />
|
|
||||||
<item
|
|
||||||
android:id="@+id/export_messages"
|
|
||||||
android:showAsAction="never"
|
|
||||||
android:title="@string/export_messages"
|
|
||||||
app:showAsAction="never" />
|
|
||||||
<item
|
<item
|
||||||
android:id="@+id/settings"
|
android:id="@+id/settings"
|
||||||
android:icon="@drawable/ic_settings_cog_vector"
|
android:icon="@drawable/ic_settings_cog_vector"
|
||||||
|
|
Loading…
Reference in New Issue