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