Add dialog to import messages

This commit is contained in:
Paul Akhamiogu 2021-09-10 00:31:04 +01:00
parent 2a5f2bfc03
commit b74a511a5e
6 changed files with 147 additions and 3 deletions

View File

@ -8,10 +8,12 @@ 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.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.models.FAQItem
@ -19,12 +21,14 @@ 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.dialogs.ExportMessagesDialog 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.EXPORT_MIME_TYPE import com.simplemobiletools.smsmessenger.helpers.EXPORT_MIME_TYPE
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.THREAD_TITLE import com.simplemobiletools.smsmessenger.helpers.THREAD_TITLE
import com.simplemobiletools.smsmessenger.models.Conversation import com.simplemobiletools.smsmessenger.models.Conversation
import com.simplemobiletools.smsmessenger.models.Events import com.simplemobiletools.smsmessenger.models.Events
import java.io.FileOutputStream
import java.io.OutputStream import java.io.OutputStream
import java.util.ArrayList import java.util.ArrayList
import java.util.Arrays import java.util.Arrays
@ -115,7 +119,7 @@ class MainActivity : SimpleActivity() {
R.id.search -> launchSearch() R.id.search -> launchSearch()
R.id.settings -> launchSettings() R.id.settings -> launchSettings()
R.id.export_messages -> tryToExportMessages() R.id.export_messages -> tryToExportMessages()
R.id.import_messages -> launchSettings() R.id.import_messages -> tryImportMessages()
R.id.about -> launchAbout() R.id.about -> launchAbout()
else -> return super.onOptionsItemSelected(item) else -> return super.onOptionsItemSelected(item)
} }
@ -131,7 +135,7 @@ class MainActivity : SimpleActivity() {
finish() finish()
} }
} else if (requestCode == PICK_IMPORT_SOURCE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) { } 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) { } else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
val outputStream = contentResolver.openOutputStream(resultData.data!!) val outputStream = contentResolver.openOutputStream(resultData.data!!)
exportMessagesTo(outputStream) exportMessagesTo(outputStream)
@ -346,7 +350,7 @@ class MainActivity : SimpleActivity() {
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) { if (it) {
ExportMessagesDialog(this, config.lastExportPath, false) { file -> ExportMessagesDialog(this, config.lastExportPath, false) { file ->
getFileOutputStream(file.toFileDirItem(this), true) { outStream-> getFileOutputStream(file.toFileDirItem(this), true) { outStream ->
exportMessagesTo(outStream) exportMessagesTo(outStream)
} }
} }
@ -361,6 +365,61 @@ class MainActivity : SimpleActivity() {
} }
} }
private fun tryImportMessages() {
if (isQPlus()) {
Intent(Intent.ACTION_GET_CONTENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = EXPORT_MIME_TYPE
startActivityForResult(this, PICK_IMPORT_SOURCE_INTENT)
}
} else {
handlePermission(PERMISSION_READ_STORAGE) {
if (it) {
importEvents()
}
}
}
}
private fun importEvents() {
FilePickerDialog(this) {
showImportEventsDialog(it)
}
}
private fun showImportEventsDialog(path: String) {
ImportMessagesDialog(this, path) { refresh ->
if (refresh) {
runOnUiThread {
// refresh
}
}
}
}
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()

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.smsmessenger.dialogs
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.extensions.config
import kotlinx.android.synthetic.main.dialog_import_messages.view.import_mms_checkbox
import kotlinx.android.synthetic.main.dialog_import_messages.view.import_sms_checkbox
class ImportMessagesDialog(
private val activity: SimpleActivity,
private val path: String,
private val callback: (refresh: Boolean) -> Unit,
) {
private val config = activity.config
init {
val view = (activity.layoutInflater.inflate(R.layout.dialog_import_messages, null) as ViewGroup).apply {
import_sms_checkbox.isChecked = config.importSms
import_mms_checkbox.isChecked = config.importMms
}
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.import_messages) {
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
dismiss()
callback.invoke(true)
}
}
}
}
}

View File

@ -38,4 +38,12 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getBoolean(EXPORT_MMS, true) get() = prefs.getBoolean(EXPORT_MMS, true)
set(exportMms) = prefs.edit().putBoolean(EXPORT_MMS, exportMms).apply() set(exportMms) = prefs.edit().putBoolean(EXPORT_MMS, exportMms).apply()
var importSms: Boolean
get() = prefs.getBoolean(IMPORT_SMS, true)
set(importSms) = prefs.edit().putBoolean(IMPORT_SMS, importSms).apply()
var importMms: Boolean
get() = prefs.getBoolean(IMPORT_MMS, true)
set(importMms) = prefs.edit().putBoolean(IMPORT_MMS, importMms).apply()
} }

View File

@ -20,6 +20,8 @@ const val EXPORT_SMS = "export_sms"
const val EXPORT_MMS = "export_mms" const val EXPORT_MMS = "export_mms"
const val EXPORT_MIME_TYPE = "application/json" const val EXPORT_MIME_TYPE = "application/json"
const val EXPORT_FILE_EXT = ".json" const val EXPORT_FILE_EXT = ".json"
const val IMPORT_SMS = "import_sms"
const val IMPORT_MMS = "import_mms"
private const val PATH = "com.simplemobiletools.smsmessenger.action." private const val PATH = "com.simplemobiletools.smsmessenger.action."
const val MARK_AS_READ = PATH + "mark_as_read" const val MARK_AS_READ = PATH + "mark_as_read"

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/import_messages_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/import_messages_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/import_sms_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:paddingTop="@dimen/small_margin"
android:paddingBottom="@dimen/small_margin"
android:text="@string/import_sms" />
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/import_mms_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:paddingTop="@dimen/small_margin"
android:paddingBottom="@dimen/small_margin"
android:text="@string/import_mms" />
</LinearLayout>
</ScrollView>

View File

@ -54,6 +54,8 @@
<string name="export_sms">Export SMS</string> <string name="export_sms">Export SMS</string>
<string name="export_mms">Export MMS</string> <string name="export_mms">Export MMS</string>
<string name="import_messages">Import messages</string> <string name="import_messages">Import messages</string>
<string name="import_sms">Import SMS</string>
<string name="import_mms">Import MMS</string>
<!-- FAQ --> <!-- FAQ -->
<string name="faq_1_title">Why does the app require access to the internet?</string> <string name="faq_1_title">Why does the app require access to the internet?</string>