|
|
@ -6,24 +6,29 @@ import android.os.Build |
|
|
|
import android.os.Bundle |
|
|
|
import android.os.Bundle |
|
|
|
import android.webkit.MimeTypeMap |
|
|
|
import android.webkit.MimeTypeMap |
|
|
|
import androidx.activity.result.contract.ActivityResultContracts |
|
|
|
import androidx.activity.result.contract.ActivityResultContracts |
|
|
|
|
|
|
|
import androidx.activity.viewModels |
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.base.BaseActivity |
|
|
|
import io.legado.app.base.VMBaseActivity |
|
|
|
import io.legado.app.constant.Theme |
|
|
|
import io.legado.app.constant.Theme |
|
|
|
import io.legado.app.databinding.ActivityTranslucenceBinding |
|
|
|
import io.legado.app.databinding.ActivityTranslucenceBinding |
|
|
|
|
|
|
|
import io.legado.app.help.IntentDataHelp |
|
|
|
import io.legado.app.lib.dialogs.SelectItem |
|
|
|
import io.legado.app.lib.dialogs.SelectItem |
|
|
|
import io.legado.app.lib.dialogs.alert |
|
|
|
import io.legado.app.lib.dialogs.alert |
|
|
|
import io.legado.app.lib.permission.Permissions |
|
|
|
import io.legado.app.lib.permission.Permissions |
|
|
|
import io.legado.app.lib.permission.PermissionsCompat |
|
|
|
import io.legado.app.lib.permission.PermissionsCompat |
|
|
|
import io.legado.app.utils.isContentScheme |
|
|
|
import io.legado.app.utils.isContentScheme |
|
|
|
|
|
|
|
import io.legado.app.utils.toastOnUi |
|
|
|
import io.legado.app.utils.viewbindingdelegate.viewBinding |
|
|
|
import io.legado.app.utils.viewbindingdelegate.viewBinding |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
class HandleFileActivity : |
|
|
|
class HandleFileActivity : |
|
|
|
BaseActivity<ActivityTranslucenceBinding>( |
|
|
|
VMBaseActivity<ActivityTranslucenceBinding, HandleFileViewModel>( |
|
|
|
theme = Theme.Transparent |
|
|
|
theme = Theme.Transparent |
|
|
|
), FilePickerDialog.CallBack { |
|
|
|
), FilePickerDialog.CallBack { |
|
|
|
|
|
|
|
|
|
|
|
override val binding by viewBinding(ActivityTranslucenceBinding::inflate) |
|
|
|
override val binding by viewBinding(ActivityTranslucenceBinding::inflate) |
|
|
|
|
|
|
|
override val viewModel by viewModels<HandleFileViewModel>() |
|
|
|
|
|
|
|
private var mode = 0 |
|
|
|
|
|
|
|
|
|
|
|
private val selectDocTree = |
|
|
|
private val selectDocTree = |
|
|
|
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { |
|
|
|
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { |
|
|
@ -45,27 +50,20 @@ class HandleFileActivity : |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) { |
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) { |
|
|
|
val mode = intent.getIntExtra("mode", 0) |
|
|
|
mode = intent.getIntExtra("mode", 0) |
|
|
|
|
|
|
|
viewModel.errorLiveData.observe(this) { |
|
|
|
|
|
|
|
toastOnUi(it) |
|
|
|
|
|
|
|
finish() |
|
|
|
|
|
|
|
} |
|
|
|
val allowExtensions = intent.getStringArrayExtra("allowExtensions") |
|
|
|
val allowExtensions = intent.getStringArrayExtra("allowExtensions") |
|
|
|
val selectList = when (mode) { |
|
|
|
val selectList = when (mode) { |
|
|
|
HandleFileContract.DIR -> arrayListOf( |
|
|
|
HandleFileContract.DIR -> getDirActions() |
|
|
|
SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR) |
|
|
|
HandleFileContract.FILE -> getFileActions() |
|
|
|
).apply { |
|
|
|
HandleFileContract.EXPORT -> arrayListOf( |
|
|
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
|
|
|
SelectItem(getString(R.string.upload_url), 111) |
|
|
|
add(SelectItem(getString(R.string.app_folder_picker), 10)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
HandleFileContract.FILE -> arrayListOf( |
|
|
|
|
|
|
|
SelectItem(getString(R.string.sys_file_picker), HandleFileContract.FILE) |
|
|
|
|
|
|
|
).apply { |
|
|
|
).apply { |
|
|
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
|
|
|
addAll(getDirActions()) |
|
|
|
add(SelectItem(getString(R.string.app_folder_picker), 11)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
HandleFileContract.EXPORT -> arrayListOf( |
|
|
|
|
|
|
|
SelectItem(getString(R.string.upload_url), 111), |
|
|
|
|
|
|
|
SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
else -> arrayListOf() |
|
|
|
else -> arrayListOf() |
|
|
|
} |
|
|
|
} |
|
|
|
intent.getParcelableArrayListExtra<SelectItem>("otherActions")?.let { |
|
|
|
intent.getParcelableArrayListExtra<SelectItem>("otherActions")?.let { |
|
|
@ -99,6 +97,12 @@ class HandleFileActivity : |
|
|
|
allowExtensions = allowExtensions |
|
|
|
allowExtensions = allowExtensions |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
111 -> getFileData()?.let { |
|
|
|
|
|
|
|
viewModel.upload(it.first, it.second) { url -> |
|
|
|
|
|
|
|
val uri = Uri.parse(url) |
|
|
|
|
|
|
|
onResult(Intent().setData(uri)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else -> { |
|
|
|
else -> { |
|
|
|
val path = item.title |
|
|
|
val path = item.title |
|
|
|
val uri = if (path.isContentScheme()) { |
|
|
|
val uri = if (path.isContentScheme()) { |
|
|
@ -116,6 +120,39 @@ class HandleFileActivity : |
|
|
|
}.show() |
|
|
|
}.show() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun getFileData(): Pair<String, ByteArray>? { |
|
|
|
|
|
|
|
val fileName = intent.getStringExtra("fileName") |
|
|
|
|
|
|
|
val file = intent.getStringExtra("fileKey")?.let { |
|
|
|
|
|
|
|
IntentDataHelp.getData<ByteArray>(it) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (fileName != null && file != null) { |
|
|
|
|
|
|
|
return Pair(fileName, file) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun getDirActions(): ArrayList<SelectItem> { |
|
|
|
|
|
|
|
return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
|
|
|
|
|
|
|
arrayListOf( |
|
|
|
|
|
|
|
SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR), |
|
|
|
|
|
|
|
SelectItem(getString(R.string.app_folder_picker), 10) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
arrayListOf(SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun getFileActions(): ArrayList<SelectItem> { |
|
|
|
|
|
|
|
return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { |
|
|
|
|
|
|
|
arrayListOf( |
|
|
|
|
|
|
|
SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.FILE), |
|
|
|
|
|
|
|
SelectItem(getString(R.string.app_folder_picker), 11) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
arrayListOf(SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.FILE)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun checkPermissions(success: (() -> Unit)? = null) { |
|
|
|
private fun checkPermissions(success: (() -> Unit)? = null) { |
|
|
|
PermissionsCompat.Builder(this) |
|
|
|
PermissionsCompat.Builder(this) |
|
|
|
.addPermissions(*Permissions.Group.STORAGE) |
|
|
|
.addPermissions(*Permissions.Group.STORAGE) |
|
|
|