pull/1295/head
gedoor 3 years ago
parent 595cd99dc3
commit b5c8b6be05
  1. 2
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  2. 4
      app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt
  3. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  4. 75
      app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt
  5. 9
      app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt
  6. 23
      app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt
  7. 10
      app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt

@ -48,8 +48,6 @@ val okHttpClient: OkHttpClient by lazy {
//提供CookieJar 用于同步Cookie
builder.addInterceptor(CronetInterceptor(null))
}
builder.build()
}

@ -86,6 +86,10 @@ fun ResponseBody.text(encode: String? = null): String {
fun Request.Builder.addHeaders(headers: Map<String, String>) {
headers.forEach {
if (it.key == AppConst.UA_NAME) {
//防止userAgent重复
removeHeader(AppConst.UA_NAME)
}
addHeader(it.key, it.value)
}
}

@ -280,7 +280,6 @@ class AnalyzeUrl(
return getWebViewSrc(params)
}
return getProxyClient(proxy).newCallStrResponse(retry) {
removeHeader(UA_NAME)
addHeaders(headerMap)
when (method) {
RequestMethod.POST -> {
@ -300,7 +299,6 @@ class AnalyzeUrl(
setCookie(tag)
@Suppress("BlockingMethodInNonBlockingContext")
return getProxyClient(proxy).newCall(retry) {
removeHeader(UA_NAME)
addHeaders(headerMap)
when (method) {
RequestMethod.POST -> {

@ -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)

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.activity.result.contract.ActivityResultContract
import io.legado.app.help.IntentDataHelp
import io.legado.app.lib.dialogs.SelectItem
@Suppress("unused")
@ -28,6 +29,12 @@ class HandleFileContract :
intent.putExtra("title", it.title)
intent.putExtra("allowExtensions", it.allowExtensions)
intent.putExtra("otherActions", it.otherActions)
it.fileName?.let { fileName ->
intent.putExtra("fileName", fileName)
}
it.file?.let { file ->
intent.putExtra("fileKey", IntentDataHelp.putData(file))
}
}
return intent
}
@ -45,6 +52,8 @@ class HandleFileContract :
var title: String? = null,
var allowExtensions: Array<String> = arrayOf(),
var otherActions: ArrayList<SelectItem>? = null,
var fileName: String? = null,
var file: ByteArray? = null
)
}

@ -0,0 +1,23 @@
package io.legado.app.ui.document
import android.app.Application
import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel
import io.legado.app.help.DirectLinkUpload
class HandleFileViewModel(application: Application) : BaseViewModel(application) {
val errorLiveData = MutableLiveData<String>()
fun upload(fileName: String, byteArray: ByteArray, success: (url: String) -> Unit) {
execute {
DirectLinkUpload.upLoad(fileName, byteArray)
}.onSuccess {
success.invoke(it)
}.onError {
it.printStackTrace()
errorLiveData.postValue(it.localizedMessage)
}
}
}

@ -32,7 +32,11 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
viewModel.importBookshelf(text, groupId)
}
}
private val exportBookshelf = registerForActivityResult(HandleFileContract()) {
it?.let {
}
}
abstract val groupId: Long
abstract val books: List<Book>
@ -55,7 +59,11 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
putExtra("groupId", groupId)
}
R.id.menu_export_bookshelf -> viewModel.exportBookshelf(books) {
exportBookshelf.launch {
mode = HandleFileContract.EXPORT
fileName = "bookshelf.json"
file = it.toByteArray()
}
}
R.id.menu_import_bookshelf -> importBookshelfAlert(groupId)
}

Loading…
Cancel
Save