Add support for .txt files for importing
This commit is contained in:
parent
a6b97698bf
commit
a35edce84a
|
@ -627,7 +627,7 @@ class MainActivity : SimpleActivity() {
|
||||||
Intent(Intent.ACTION_GET_CONTENT).apply {
|
Intent(Intent.ACTION_GET_CONTENT).apply {
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
type = JSON_MIME_TYPE
|
type = JSON_MIME_TYPE
|
||||||
putExtra(Intent.EXTRA_MIME_TYPES, arrayOf(JSON_MIME_TYPE, XML_MIME_TYPE))
|
putExtra(Intent.EXTRA_MIME_TYPES, arrayOf(JSON_MIME_TYPE, XML_MIME_TYPE, TXT_MIME_TYPE))
|
||||||
|
|
||||||
try {
|
try {
|
||||||
startActivityForResult(this, PICK_IMPORT_SOURCE_INTENT)
|
startActivityForResult(this, PICK_IMPORT_SOURCE_INTENT)
|
||||||
|
|
|
@ -31,6 +31,7 @@ const val EXPORT_MMS = "export_mms"
|
||||||
const val JSON_FILE_EXT = ".json"
|
const val JSON_FILE_EXT = ".json"
|
||||||
const val JSON_MIME_TYPE = "application/json"
|
const val JSON_MIME_TYPE = "application/json"
|
||||||
const val XML_MIME_TYPE = "text/xml"
|
const val XML_MIME_TYPE = "text/xml"
|
||||||
|
const val TXT_MIME_TYPE = "text/plain"
|
||||||
const val IMPORT_SMS = "import_sms"
|
const val IMPORT_SMS = "import_sms"
|
||||||
const val IMPORT_MMS = "import_mms"
|
const val IMPORT_MMS = "import_mms"
|
||||||
const val WAS_DB_CLEARED = "was_db_cleared_2"
|
const val WAS_DB_CLEARED = "was_db_cleared_2"
|
||||||
|
|
|
@ -32,13 +32,19 @@ class MessagesImporter(private val context: Context) {
|
||||||
fun importMessages(path: String, onProgress: (total: Int, current: Int) -> Unit = { _, _ -> }, callback: (result: ImportResult) -> Unit) {
|
fun importMessages(path: String, onProgress: (total: Int, current: Int) -> Unit = { _, _ -> }, callback: (result: ImportResult) -> Unit) {
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
try {
|
try {
|
||||||
val inputStream = if (path.contains("/")) {
|
val isXml = if (path.endsWith("txt")) {
|
||||||
File(path).inputStream()
|
// Need to read the first line to determine if it is xml
|
||||||
|
val tempStream = getInputStreamForPath(path)
|
||||||
|
tempStream.bufferedReader().use {
|
||||||
|
it.readLine().startsWith("<?xml")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
context.assets.open(path)
|
path.endsWith("xml")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.endsWith("xml")) {
|
val inputStream = getInputStreamForPath(path)
|
||||||
|
|
||||||
|
if (isXml) {
|
||||||
inputStream.importXml()
|
inputStream.importXml()
|
||||||
} else {
|
} else {
|
||||||
inputStream.importJson()
|
inputStream.importJson()
|
||||||
|
@ -60,6 +66,14 @@ class MessagesImporter(private val context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getInputStreamForPath(path: String): InputStream {
|
||||||
|
return if (path.contains("/")) {
|
||||||
|
File(path).inputStream()
|
||||||
|
} else {
|
||||||
|
context.assets.open(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun InputStream.importJson() {
|
private fun InputStream.importJson() {
|
||||||
bufferedReader().use { reader ->
|
bufferedReader().use { reader ->
|
||||||
val jsonReader = gson.newJsonReader(reader)
|
val jsonReader = gson.newJsonReader(reader)
|
||||||
|
|
Loading…
Reference in New Issue