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 //提供CookieJar 用于同步Cookie
builder.addInterceptor(CronetInterceptor(null)) builder.addInterceptor(CronetInterceptor(null))
} }
builder.build() builder.build()
} }

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

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

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

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.activity.result.contract.ActivityResultContract import androidx.activity.result.contract.ActivityResultContract
import io.legado.app.help.IntentDataHelp
import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.SelectItem
@Suppress("unused") @Suppress("unused")
@ -28,6 +29,12 @@ class HandleFileContract :
intent.putExtra("title", it.title) intent.putExtra("title", it.title)
intent.putExtra("allowExtensions", it.allowExtensions) intent.putExtra("allowExtensions", it.allowExtensions)
intent.putExtra("otherActions", it.otherActions) 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 return intent
} }
@ -45,6 +52,8 @@ class HandleFileContract :
var title: String? = null, var title: String? = null,
var allowExtensions: Array<String> = arrayOf(), var allowExtensions: Array<String> = arrayOf(),
var otherActions: ArrayList<SelectItem>? = null, 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) viewModel.importBookshelf(text, groupId)
} }
} }
private val exportBookshelf = registerForActivityResult(HandleFileContract()) {
it?.let {
}
}
abstract val groupId: Long abstract val groupId: Long
abstract val books: List<Book> abstract val books: List<Book>
@ -55,7 +59,11 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
putExtra("groupId", groupId) putExtra("groupId", groupId)
} }
R.id.menu_export_bookshelf -> viewModel.exportBookshelf(books) { 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) R.id.menu_import_bookshelf -> importBookshelfAlert(groupId)
} }

Loading…
Cancel
Save