switched to using ActivityResultContracts
This commit is contained in:
parent
958644b290
commit
c53f537b63
1 changed files with 19 additions and 17 deletions
|
|
@ -1,11 +1,11 @@
|
||||||
package org.fossify.messages.activities
|
package org.fossify.messages.activities
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.content.ActivityNotFoundException
|
import android.content.ActivityNotFoundException
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import org.fossify.commons.activities.BaseSimpleActivity
|
import org.fossify.commons.activities.BaseSimpleActivity
|
||||||
import org.fossify.commons.dialogs.ExportBlockedNumbersDialog
|
import org.fossify.commons.dialogs.ExportBlockedNumbersDialog
|
||||||
import org.fossify.commons.dialogs.FilePickerDialog
|
import org.fossify.commons.dialogs.FilePickerDialog
|
||||||
|
|
@ -26,11 +26,6 @@ import java.io.OutputStream
|
||||||
|
|
||||||
class ManageBlockedKeywordsActivity : BaseSimpleActivity(), RefreshRecyclerViewListener {
|
class ManageBlockedKeywordsActivity : BaseSimpleActivity(), RefreshRecyclerViewListener {
|
||||||
|
|
||||||
private companion object {
|
|
||||||
private const val PICK_IMPORT_SOURCE_INTENT = 11
|
|
||||||
private const val PICK_EXPORT_FILE_INTENT = 21
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getAppIconIDs() = intent.getIntegerArrayListExtra(APP_ICON_IDS) ?: ArrayList()
|
override fun getAppIconIDs() = intent.getIntegerArrayListExtra(APP_ICON_IDS) ?: ArrayList()
|
||||||
|
|
||||||
override fun getAppLauncherName() = intent.getStringExtra(APP_LAUNCHER_NAME) ?: ""
|
override fun getAppLauncherName() = intent.getStringExtra(APP_LAUNCHER_NAME) ?: ""
|
||||||
|
|
@ -90,17 +85,24 @@ class ManageBlockedKeywordsActivity : BaseSimpleActivity(), RefreshRecyclerViewL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
|
private val exportActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { uri ->
|
||||||
super.onActivityResult(requestCode, resultCode, resultData)
|
try {
|
||||||
when {
|
val outputStream = uri?.let { contentResolver.openOutputStream(it) }
|
||||||
requestCode == PICK_IMPORT_SOURCE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null -> {
|
if (outputStream != null) {
|
||||||
tryImportBlockedKeywordsFromFile(resultData.data!!)
|
|
||||||
}
|
|
||||||
|
|
||||||
requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null -> {
|
|
||||||
val outputStream = contentResolver.openOutputStream(resultData.data!!)
|
|
||||||
exportBlockedKeywordsTo(outputStream)
|
exportBlockedKeywordsTo(outputStream)
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
showErrorToast(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val importActivityResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||||
|
try {
|
||||||
|
if (uri != null) {
|
||||||
|
tryImportBlockedKeywordsFromFile(uri)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
showErrorToast(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,7 +114,7 @@ class ManageBlockedKeywordsActivity : BaseSimpleActivity(), RefreshRecyclerViewL
|
||||||
type = "text/plain"
|
type = "text/plain"
|
||||||
|
|
||||||
try {
|
try {
|
||||||
startActivityForResult(this, PICK_IMPORT_SOURCE_INTENT)
|
importActivityResultLauncher.launch(type)
|
||||||
} catch (e: ActivityNotFoundException) {
|
} catch (e: ActivityNotFoundException) {
|
||||||
toast(org.fossify.commons.R.string.system_service_disabled, Toast.LENGTH_LONG)
|
toast(org.fossify.commons.R.string.system_service_disabled, Toast.LENGTH_LONG)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|
@ -198,7 +200,7 @@ class ManageBlockedKeywordsActivity : BaseSimpleActivity(), RefreshRecyclerViewL
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
startActivityForResult(this, PICK_EXPORT_FILE_INTENT)
|
exportActivityResultLauncher.launch(file.name)
|
||||||
} catch (e: ActivityNotFoundException) {
|
} catch (e: ActivityNotFoundException) {
|
||||||
toast(org.fossify.commons.R.string.system_service_disabled, Toast.LENGTH_LONG)
|
toast(org.fossify.commons.R.string.system_service_disabled, Toast.LENGTH_LONG)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue