parent
35f384c19f
commit
fa92195933
@ -1,195 +0,0 @@ |
||||
package io.legado.app.ui.config |
||||
|
||||
import android.app.Activity.RESULT_OK |
||||
import android.content.Intent |
||||
import android.net.Uri |
||||
import androidx.documentfile.provider.DocumentFile |
||||
import androidx.fragment.app.Fragment |
||||
import io.legado.app.R |
||||
import io.legado.app.constant.PreferKey |
||||
import io.legado.app.help.AppConfig |
||||
import io.legado.app.help.coroutine.Coroutine |
||||
import io.legado.app.help.storage.Backup |
||||
import io.legado.app.help.storage.BookWebDav |
||||
import io.legado.app.help.storage.ImportOldData |
||||
import io.legado.app.help.storage.Restore |
||||
import io.legado.app.lib.permission.Permissions |
||||
import io.legado.app.lib.permission.PermissionsCompat |
||||
import io.legado.app.ui.filepicker.FilePicker |
||||
import io.legado.app.utils.getPrefString |
||||
import io.legado.app.utils.isContentScheme |
||||
import io.legado.app.utils.longToast |
||||
import io.legado.app.utils.toastOnUi |
||||
import kotlinx.coroutines.Dispatchers.Main |
||||
import splitties.init.appCtx |
||||
|
||||
object BackupRestoreUi { |
||||
private const val selectFolderRequestCode = 21 |
||||
private const val backupSelectRequestCode = 22 |
||||
private const val restoreSelectRequestCode = 33 |
||||
private const val oldDataRequestCode = 11 |
||||
|
||||
fun backup(fragment: Fragment) { |
||||
val backupPath = AppConfig.backupPath |
||||
if (backupPath.isNullOrEmpty()) { |
||||
selectBackupFolder(fragment, backupSelectRequestCode) |
||||
} else { |
||||
if (backupPath.isContentScheme()) { |
||||
val uri = Uri.parse(backupPath) |
||||
val doc = DocumentFile.fromTreeUri(fragment.requireContext(), uri) |
||||
if (doc?.canWrite() == true) { |
||||
Coroutine.async { |
||||
Backup.backup(fragment.requireContext(), backupPath) |
||||
}.onSuccess { |
||||
fragment.toastOnUi(R.string.backup_success) |
||||
} |
||||
} else { |
||||
selectBackupFolder(fragment, backupSelectRequestCode) |
||||
} |
||||
} else { |
||||
backupUsePermission(fragment, backupPath) |
||||
} |
||||
} |
||||
} |
||||
|
||||
private fun backupUsePermission( |
||||
fragment: Fragment, |
||||
path: String |
||||
) { |
||||
PermissionsCompat.Builder(fragment) |
||||
.addPermissions(*Permissions.Group.STORAGE) |
||||
.rationale(R.string.tip_perm_request_storage) |
||||
.onGranted { |
||||
Coroutine.async { |
||||
AppConfig.backupPath = path |
||||
Backup.backup(fragment.requireContext(), path) |
||||
}.onSuccess { |
||||
fragment.toastOnUi(R.string.backup_success) |
||||
} |
||||
} |
||||
.request() |
||||
} |
||||
|
||||
fun selectBackupFolder(fragment: Fragment, requestCode: Int = selectFolderRequestCode) { |
||||
FilePicker.selectFolder(fragment, requestCode) |
||||
} |
||||
|
||||
fun restore(fragment: Fragment) { |
||||
Coroutine.async(context = Main) { |
||||
BookWebDav.showRestoreDialog(fragment.requireContext()) |
||||
}.onError { |
||||
fragment.longToast("WebDavError:${it.localizedMessage}\n将从本地备份恢复。") |
||||
val backupPath = fragment.getPrefString(PreferKey.backupPath) |
||||
if (backupPath?.isNotEmpty() == true) { |
||||
if (backupPath.isContentScheme()) { |
||||
val uri = Uri.parse(backupPath) |
||||
val doc = DocumentFile.fromTreeUri(fragment.requireContext(), uri) |
||||
if (doc?.canWrite() == true) { |
||||
Restore.restore(fragment.requireContext(), backupPath) |
||||
} else { |
||||
selectBackupFolder(fragment, restoreSelectRequestCode) |
||||
} |
||||
} else { |
||||
restoreUsePermission(fragment, backupPath) |
||||
} |
||||
} else { |
||||
selectBackupFolder(fragment, restoreSelectRequestCode) |
||||
} |
||||
} |
||||
} |
||||
|
||||
fun restoreByFolder(fragment: Fragment) { |
||||
selectBackupFolder(fragment, restoreSelectRequestCode) |
||||
} |
||||
|
||||
private fun restoreUsePermission(fragment: Fragment, path: String) { |
||||
PermissionsCompat.Builder(fragment) |
||||
.addPermissions(*Permissions.Group.STORAGE) |
||||
.rationale(R.string.tip_perm_request_storage) |
||||
.onGranted { |
||||
Coroutine.async { |
||||
AppConfig.backupPath = path |
||||
Restore.restoreDatabase(path) |
||||
Restore.restoreConfig(path) |
||||
} |
||||
} |
||||
.request() |
||||
} |
||||
|
||||
fun importOldData(fragment: Fragment) { |
||||
FilePicker.selectFolder(fragment, oldDataRequestCode) |
||||
} |
||||
|
||||
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
||||
when (requestCode) { |
||||
backupSelectRequestCode -> if (resultCode == RESULT_OK) { |
||||
data?.data?.let { uri -> |
||||
if (uri.isContentScheme()) { |
||||
appCtx.contentResolver.takePersistableUriPermission( |
||||
uri, |
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION |
||||
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
||||
) |
||||
AppConfig.backupPath = uri.toString() |
||||
Coroutine.async { |
||||
Backup.backup(appCtx, uri.toString()) |
||||
}.onSuccess { |
||||
appCtx.toastOnUi(R.string.backup_success) |
||||
} |
||||
} else { |
||||
uri.path?.let { path -> |
||||
AppConfig.backupPath = path |
||||
Coroutine.async { |
||||
Backup.backup(appCtx, path) |
||||
}.onSuccess { |
||||
appCtx.toastOnUi(R.string.backup_success) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
restoreSelectRequestCode -> if (resultCode == RESULT_OK) { |
||||
data?.data?.let { uri -> |
||||
if (uri.isContentScheme()) { |
||||
appCtx.contentResolver.takePersistableUriPermission( |
||||
uri, |
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION |
||||
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
||||
) |
||||
AppConfig.backupPath = uri.toString() |
||||
Coroutine.async { |
||||
Restore.restore(appCtx, uri.toString()) |
||||
} |
||||
} else { |
||||
uri.path?.let { path -> |
||||
AppConfig.backupPath = path |
||||
Coroutine.async { |
||||
Restore.restore(appCtx, path) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
selectFolderRequestCode -> if (resultCode == RESULT_OK) { |
||||
data?.data?.let { uri -> |
||||
if (uri.isContentScheme()) { |
||||
appCtx.contentResolver.takePersistableUriPermission( |
||||
uri, |
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION |
||||
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
||||
) |
||||
AppConfig.backupPath = uri.toString() |
||||
} else { |
||||
AppConfig.backupPath = uri.path |
||||
} |
||||
} |
||||
} |
||||
oldDataRequestCode -> if (resultCode == RESULT_OK) { |
||||
data?.data?.let { uri -> |
||||
ImportOldData.importUri(appCtx, uri) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,284 +1,43 @@ |
||||
package io.legado.app.ui.filepicker |
||||
|
||||
import android.app.Activity.RESULT_OK |
||||
import android.content.Context |
||||
import android.content.Intent |
||||
import android.os.Build |
||||
import androidx.appcompat.app.AppCompatActivity |
||||
import androidx.fragment.app.Fragment |
||||
import io.legado.app.R |
||||
import io.legado.app.lib.dialogs.alert |
||||
import io.legado.app.lib.permission.Permissions |
||||
import io.legado.app.lib.permission.PermissionsCompat |
||||
import android.net.Uri |
||||
import androidx.activity.result.contract.ActivityResultContract |
||||
|
||||
@Suppress("unused") |
||||
object FilePicker { |
||||
class FilePicker : ActivityResultContract<FilePickerParam, Uri?>() { |
||||
|
||||
fun selectFolder( |
||||
activity: AppCompatActivity, |
||||
requestCode: Int, |
||||
title: String = activity.getString(R.string.select_folder), |
||||
otherActions: List<String>? = null, |
||||
otherFun: ((action: String) -> Unit)? = null |
||||
) { |
||||
val selectList = arrayListOf(activity.getString(R.string.sys_folder_picker)) |
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
||||
selectList.add(activity.getString(R.string.app_folder_picker)) |
||||
} |
||||
otherActions?.let { |
||||
selectList.addAll(otherActions) |
||||
} |
||||
activity.alert(title = title) { |
||||
items(selectList) { _, index -> |
||||
when (index) { |
||||
0 -> { |
||||
kotlin.runCatching { |
||||
val intent = createSelectDirIntent() |
||||
activity.startActivityForResult(intent, requestCode) |
||||
}.onFailure { |
||||
checkPermissions(activity) { |
||||
FilePickerDialog.show( |
||||
activity.supportFragmentManager, |
||||
requestCode, |
||||
mode = FilePickerDialog.DIRECTORY |
||||
) |
||||
} |
||||
} |
||||
} |
||||
else -> { |
||||
val selectText = selectList[index] |
||||
if (selectText == activity.getString(R.string.app_folder_picker)) { |
||||
checkPermissions(activity) { |
||||
FilePickerDialog.show( |
||||
activity.supportFragmentManager, |
||||
requestCode, |
||||
mode = FilePickerDialog.DIRECTORY |
||||
) |
||||
} |
||||
} else { |
||||
otherFun?.invoke(selectText) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
}.show() |
||||
} |
||||
|
||||
fun selectFolder( |
||||
fragment: Fragment, |
||||
requestCode: Int, |
||||
title: String = fragment.getString(R.string.select_folder), |
||||
otherActions: List<String>? = null, |
||||
otherFun: ((action: String) -> Unit)? = null |
||||
) { |
||||
val selectList = arrayListOf(fragment.getString(R.string.sys_folder_picker)) |
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
||||
selectList.add(fragment.getString(R.string.app_folder_picker)) |
||||
} |
||||
otherActions?.let { |
||||
selectList.addAll(otherActions) |
||||
} |
||||
fragment.alert(title = title) { |
||||
items(selectList) { _, index -> |
||||
when (index) { |
||||
0 -> { |
||||
kotlin.runCatching { |
||||
val intent = createSelectDirIntent() |
||||
fragment.startActivityForResult(intent, requestCode) |
||||
}.onFailure { |
||||
checkPermissions(fragment) { |
||||
FilePickerDialog.show( |
||||
fragment.childFragmentManager, |
||||
requestCode, |
||||
mode = FilePickerDialog.DIRECTORY |
||||
) |
||||
} |
||||
} |
||||
} |
||||
else -> { |
||||
val selectText = selectList[index] |
||||
if (selectText == fragment.getString(R.string.app_folder_picker)) { |
||||
checkPermissions(fragment) { |
||||
FilePickerDialog.show( |
||||
fragment.childFragmentManager, |
||||
requestCode, |
||||
mode = FilePickerDialog.DIRECTORY |
||||
) |
||||
} |
||||
} else { |
||||
otherFun?.invoke(selectText) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
}.show() |
||||
} |
||||
|
||||
fun selectFile( |
||||
activity: AppCompatActivity, |
||||
requestCode: Int, |
||||
title: String = activity.getString(R.string.select_file), |
||||
allowExtensions: Array<String> = arrayOf(), |
||||
otherActions: List<String>? = null, |
||||
otherFun: ((action: String) -> Unit)? = null |
||||
) { |
||||
val selectList = arrayListOf(activity.getString(R.string.sys_file_picker)) |
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
||||
selectList.add(activity.getString(R.string.app_file_picker)) |
||||
} |
||||
otherActions?.let { |
||||
selectList.addAll(otherActions) |
||||
} |
||||
activity.alert(title = title) { |
||||
items(selectList) { _, index -> |
||||
when (index) { |
||||
0 -> { |
||||
kotlin.runCatching { |
||||
val intent = createSelectFileIntent() |
||||
intent.putExtra( |
||||
Intent.EXTRA_MIME_TYPES, |
||||
typesOfExtensions(allowExtensions) |
||||
) |
||||
activity.startActivityForResult(intent, requestCode) |
||||
}.onFailure { |
||||
checkPermissions(activity) { |
||||
FilePickerDialog.show( |
||||
activity.supportFragmentManager, |
||||
requestCode, |
||||
mode = FilePickerDialog.FILE, |
||||
allowExtensions = allowExtensions |
||||
) |
||||
} |
||||
} |
||||
} |
||||
else -> { |
||||
val selectText = selectList[index] |
||||
if (selectText == activity.getString(R.string.app_file_picker)) { |
||||
checkPermissions(activity) { |
||||
FilePickerDialog.show( |
||||
activity.supportFragmentManager, |
||||
requestCode, |
||||
mode = FilePickerDialog.FILE, |
||||
allowExtensions = allowExtensions |
||||
) |
||||
} |
||||
} else { |
||||
otherFun?.invoke(selectText) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
}.show() |
||||
companion object { |
||||
const val DIRECTORY = 0 |
||||
const val FILE = 1 |
||||
} |
||||
|
||||
fun selectFile( |
||||
fragment: Fragment, |
||||
requestCode: Int, |
||||
title: String = fragment.getString(R.string.select_file), |
||||
allowExtensions: Array<String> = arrayOf(), |
||||
otherActions: List<String>? = null, |
||||
otherFun: ((action: String) -> Unit)? = null |
||||
) { |
||||
val selectList = arrayListOf(fragment.getString(R.string.sys_file_picker)) |
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
||||
selectList.add(fragment.getString(R.string.app_file_picker)) |
||||
override fun createIntent(context: Context, input: FilePickerParam?): Intent { |
||||
val intent = Intent(context, FilePickerActivity::class.java) |
||||
input?.let { |
||||
intent.putExtra("mode", it.mode) |
||||
intent.putExtra("title", it.title) |
||||
intent.putExtra("allowExtensions", it.allowExtensions) |
||||
intent.putExtra("otherActions", it.otherActions) |
||||
} |
||||
otherActions?.let { |
||||
selectList.addAll(otherActions) |
||||
} |
||||
fragment.alert(title = title) { |
||||
items(selectList) { _, index -> |
||||
when (index) { |
||||
0 -> { |
||||
kotlin.runCatching { |
||||
val intent = createSelectFileIntent() |
||||
intent.putExtra( |
||||
Intent.EXTRA_MIME_TYPES, |
||||
typesOfExtensions(allowExtensions) |
||||
) |
||||
fragment.startActivityForResult(intent, requestCode) |
||||
}.onFailure { |
||||
checkPermissions(fragment) { |
||||
FilePickerDialog.show( |
||||
fragment.childFragmentManager, |
||||
requestCode, |
||||
mode = FilePickerDialog.FILE, |
||||
allowExtensions = allowExtensions |
||||
) |
||||
} |
||||
} |
||||
} |
||||
else -> { |
||||
val selectText = selectList[index] |
||||
if (selectText == fragment.getString(R.string.app_file_picker)) { |
||||
checkPermissions(fragment) { |
||||
FilePickerDialog.show( |
||||
fragment.childFragmentManager, |
||||
requestCode, |
||||
mode = FilePickerDialog.FILE, |
||||
allowExtensions = allowExtensions |
||||
) |
||||
} |
||||
} else { |
||||
otherFun?.invoke(selectText) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
}.show() |
||||
} |
||||
|
||||
private fun createSelectFileIntent(): Intent { |
||||
val intent = Intent(Intent.ACTION_GET_CONTENT) |
||||
intent.addCategory(Intent.CATEGORY_OPENABLE) |
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) |
||||
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
||||
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) |
||||
intent.addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) |
||||
intent.type = "*/*" |
||||
return intent |
||||
} |
||||
|
||||
private fun createSelectDirIntent(): Intent { |
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) |
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) |
||||
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
||||
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) |
||||
intent.addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) |
||||
return intent |
||||
} |
||||
|
||||
private fun checkPermissions(fragment: Fragment, success: (() -> Unit)? = null) { |
||||
PermissionsCompat.Builder(fragment) |
||||
.addPermissions(*Permissions.Group.STORAGE) |
||||
.rationale(R.string.tip_perm_request_storage) |
||||
.onGranted { |
||||
success?.invoke() |
||||
} |
||||
.request() |
||||
override fun parseResult(resultCode: Int, intent: Intent?): Uri? { |
||||
if (resultCode == RESULT_OK) { |
||||
return intent?.data |
||||
} |
||||
return null |
||||
} |
||||
|
||||
private fun checkPermissions(activity: AppCompatActivity, success: (() -> Unit)? = null) { |
||||
PermissionsCompat.Builder(activity) |
||||
.addPermissions(*Permissions.Group.STORAGE) |
||||
.rationale(R.string.tip_perm_request_storage) |
||||
.onGranted { |
||||
success?.invoke() |
||||
} |
||||
.request() |
||||
} |
||||
} |
||||
|
||||
private fun typesOfExtensions(allowExtensions: Array<String>): Array<String> { |
||||
val types = hashSetOf<String>() |
||||
if (allowExtensions.isNullOrEmpty()) { |
||||
types.add("*/*") |
||||
} else { |
||||
allowExtensions.forEach { |
||||
when (it) { |
||||
"*" -> types.add("*/*") |
||||
"txt", "xml" -> types.add("text/*") |
||||
else -> types.add("application/$it") |
||||
} |
||||
} |
||||
} |
||||
return types.toTypedArray() |
||||
} |
||||
} |
||||
@Suppress("ArrayInDataClass") |
||||
data class FilePickerParam( |
||||
var mode: Int = 0, |
||||
var title: String? = null, |
||||
var allowExtensions: Array<String> = arrayOf(), |
||||
var otherActions: Array<String>? = null, |
||||
) |
||||
|
@ -0,0 +1,145 @@ |
||||
package io.legado.app.ui.filepicker |
||||
|
||||
import android.content.Context |
||||
import android.content.Intent |
||||
import android.net.Uri |
||||
import android.os.Build |
||||
import android.os.Bundle |
||||
import androidx.activity.result.contract.ActivityResultContract |
||||
import androidx.activity.result.contract.ActivityResultContracts |
||||
import androidx.annotation.CallSuper |
||||
import io.legado.app.R |
||||
import io.legado.app.base.BaseActivity |
||||
import io.legado.app.constant.Theme |
||||
import io.legado.app.databinding.ActivityTranslucenceBinding |
||||
import io.legado.app.lib.dialogs.alert |
||||
import io.legado.app.lib.permission.Permissions |
||||
import io.legado.app.lib.permission.PermissionsCompat |
||||
import io.legado.app.utils.isContentScheme |
||||
import java.io.File |
||||
|
||||
class FilePickerActivity : |
||||
BaseActivity<ActivityTranslucenceBinding>( |
||||
theme = Theme.Transparent |
||||
), FilePickerDialog.CallBack { |
||||
|
||||
private val selectDocTree = |
||||
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { |
||||
onResult(Intent().setData(it)) |
||||
} |
||||
|
||||
private val selectDoc = registerForActivityResult(GetContent()) { |
||||
onResult(Intent().setData(it)) |
||||
} |
||||
|
||||
override fun getViewBinding(): ActivityTranslucenceBinding { |
||||
return ActivityTranslucenceBinding.inflate(layoutInflater) |
||||
} |
||||
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) { |
||||
val mode = intent.getIntExtra("mode", 0) |
||||
val allowExtensions = intent.getStringArrayExtra("allowExtensions") |
||||
val selectList = if (mode == FilePicker.DIRECTORY) { |
||||
arrayListOf(getString(R.string.sys_folder_picker)) |
||||
} else { |
||||
arrayListOf(getString(R.string.sys_file_picker)) |
||||
} |
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
||||
selectList.add(getString(R.string.app_folder_picker)) |
||||
} |
||||
intent.getStringArrayListExtra("otherActions")?.let { |
||||
selectList.addAll(it) |
||||
} |
||||
val title = intent.getStringExtra("title") ?: let { |
||||
if (mode == FilePicker.DIRECTORY) { |
||||
return@let getString(R.string.select_folder) |
||||
} else { |
||||
return@let getString(R.string.select_file) |
||||
} |
||||
} |
||||
alert(title) { |
||||
items(selectList) { _, index -> |
||||
when (index) { |
||||
0 -> if (mode == FilePicker.DIRECTORY) { |
||||
selectDocTree.launch(null) |
||||
} else { |
||||
selectDoc.launch(typesOfExtensions(allowExtensions)) |
||||
} |
||||
2 -> if (mode == FilePicker.DIRECTORY) { |
||||
checkPermissions { |
||||
FilePickerDialog.show( |
||||
supportFragmentManager, |
||||
mode = FilePicker.DIRECTORY |
||||
) |
||||
} |
||||
} else { |
||||
checkPermissions { |
||||
FilePickerDialog.show( |
||||
supportFragmentManager, |
||||
mode = FilePicker.FILE, |
||||
allowExtensions = allowExtensions |
||||
) |
||||
} |
||||
} |
||||
else -> { |
||||
val path = selectList[index] |
||||
val uri = if (path.isContentScheme()) { |
||||
Uri.fromFile(File(path)) |
||||
} else { |
||||
Uri.parse(path) |
||||
} |
||||
onResult(Intent().setData(uri)) |
||||
} |
||||
} |
||||
} |
||||
onCancelled { |
||||
finish() |
||||
} |
||||
}.show() |
||||
} |
||||
|
||||
private fun checkPermissions(success: (() -> Unit)? = null) { |
||||
PermissionsCompat.Builder(this) |
||||
.addPermissions(*Permissions.Group.STORAGE) |
||||
.rationale(R.string.tip_perm_request_storage) |
||||
.onGranted { |
||||
success?.invoke() |
||||
} |
||||
.request() |
||||
} |
||||
|
||||
private fun typesOfExtensions(allowExtensions: Array<String>?): Array<String> { |
||||
val types = hashSetOf<String>() |
||||
if (allowExtensions.isNullOrEmpty()) { |
||||
types.add("*/*") |
||||
} else { |
||||
allowExtensions.forEach { |
||||
when (it) { |
||||
"*" -> types.add("*/*") |
||||
"txt", "xml" -> types.add("text/*") |
||||
else -> types.add("application/$it") |
||||
} |
||||
} |
||||
} |
||||
return types.toTypedArray() |
||||
} |
||||
|
||||
override fun onResult(data: Intent) { |
||||
setResult(RESULT_OK, data) |
||||
finish() |
||||
} |
||||
|
||||
class GetContent : ActivityResultContract<Array<String>, Uri>() { |
||||
@CallSuper |
||||
override fun createIntent(context: Context, input: Array<String>): Intent { |
||||
return Intent(Intent.ACTION_GET_CONTENT) |
||||
.addCategory(Intent.CATEGORY_OPENABLE) |
||||
.putExtra(Intent.EXTRA_MIME_TYPES, input) |
||||
.setType("*/*") |
||||
} |
||||
|
||||
override fun parseResult(resultCode: Int, intent: Intent?): Uri? { |
||||
return if (intent == null || resultCode != RESULT_OK) null else intent.data |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,23 @@ |
||||
package io.legado.app.ui.qrcode |
||||
|
||||
import android.app.Activity.RESULT_OK |
||||
import android.content.Context |
||||
import android.content.Intent |
||||
import androidx.activity.result.contract.ActivityResultContract |
||||
|
||||
class QrCodeResult : ActivityResultContract<Unit, String?>() { |
||||
|
||||
override fun createIntent(context: Context, input: Unit?): Intent { |
||||
return Intent(context, QrCodeActivity::class.java) |
||||
} |
||||
|
||||
override fun parseResult(resultCode: Int, intent: Intent?): String? { |
||||
if (resultCode == RESULT_OK) { |
||||
intent?.getStringExtra("result")?.let { |
||||
return it |
||||
} |
||||
} |
||||
return null |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue