From 6bbdd935d12d1f2534e2098140be15f7c52ec28b Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 17 Mar 2020 19:18:51 +0100 Subject: [PATCH] add handling for importing settings with scoped storage --- .../pro/activities/SettingsActivity.kt | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt index 8db505a4a..ed8bc09ee 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.calendar.pro.activities +import android.app.Activity import android.app.TimePickerDialog import android.content.Intent import android.content.res.Resources @@ -20,10 +21,12 @@ import com.simplemobiletools.commons.models.RadioItem import kotlinx.android.synthetic.main.activity_settings.* import org.joda.time.DateTime import java.io.File +import java.io.InputStream import java.util.* class SettingsActivity : SimpleActivity() { private val GET_RINGTONE_URI = 1 + private val PICK_IMPORT_SOURCE_INTENT = 2 lateinit var res: Resources private var mStoredPrimaryColor = 0 @@ -105,6 +108,9 @@ class SettingsActivity : SimpleActivity() { if (requestCode == GET_RINGTONE_URI && resultCode == RESULT_OK && resultData != null) { val newAlarmSound = storeNewYourAlarmSound(resultData) updateReminderSound(newAlarmSound) + } else if (requestCode == PICK_IMPORT_SOURCE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) { + val inputStream = contentResolver.openInputStream(resultData.data!!) + parseFile(inputStream) } } @@ -703,14 +709,18 @@ class SettingsActivity : SimpleActivity() { private fun setupImportSettings() { settings_import_holder.setOnClickListener { - handlePermission(PERMISSION_READ_STORAGE) { - if (it) { - FilePickerDialog(this) { - ensureBackgroundThread { - try { - parseFile(it) - } catch (e: Exception) { - showErrorToast(e) + if (isQPlus()) { + Intent(Intent.ACTION_GET_CONTENT).apply { + addCategory(Intent.CATEGORY_OPENABLE) + type = "text/plain" + startActivityForResult(this, PICK_IMPORT_SOURCE_INTENT) + } + } else { + handlePermission(PERMISSION_READ_STORAGE) { + if (it) { + FilePickerDialog(this) { + ensureBackgroundThread { + parseFile(File(it).inputStream()) } } } @@ -719,8 +729,12 @@ class SettingsActivity : SimpleActivity() { } } - private fun parseFile(path: String) { - val inputStream = File(path).inputStream() + private fun parseFile(inputStream: InputStream?) { + if (inputStream == null) { + toast(R.string.unknown_error_occurred) + return + } + var importedItems = 0 val configValues = LinkedHashMap() inputStream.bufferedReader().use {