From b5c8b6be053a0c83cf559f0dc19b6174904ce9bc Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 30 Aug 2021 12:29:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/http/HttpHelper.kt | 2 - .../io/legado/app/help/http/OkHttpUtils.kt | 4 + .../app/model/analyzeRule/AnalyzeUrl.kt | 2 - .../app/ui/document/HandleFileActivity.kt | 75 ++++++++++++++----- .../app/ui/document/HandleFileContract.kt | 9 +++ .../app/ui/document/HandleFileViewModel.kt | 23 ++++++ .../main/bookshelf/BaseBookshelfFragment.kt | 10 ++- 7 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index e67090fb3..58e880e48 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -48,8 +48,6 @@ val okHttpClient: OkHttpClient by lazy { //提供CookieJar 用于同步Cookie builder.addInterceptor(CronetInterceptor(null)) } - - builder.build() } diff --git a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt index e9921fb2e..0d995fe23 100644 --- a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt +++ b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt @@ -86,6 +86,10 @@ fun ResponseBody.text(encode: String? = null): String { fun Request.Builder.addHeaders(headers: Map) { headers.forEach { + if (it.key == AppConst.UA_NAME) { + //防止userAgent重复 + removeHeader(AppConst.UA_NAME) + } addHeader(it.key, it.value) } } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 7adccdeab..31f7bcc26 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -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 -> { diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt index 3785d094d..f1f1bee30 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt @@ -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( + VMBaseActivity( theme = Theme.Transparent ), FilePickerDialog.CallBack { override val binding by viewBinding(ActivityTranslucenceBinding::inflate) + override val viewModel by viewModels() + 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("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? { + val fileName = intent.getStringExtra("fileName") + val file = intent.getStringExtra("fileKey")?.let { + IntentDataHelp.getData(it) + } + if (fileName != null && file != null) { + return Pair(fileName, file) + } + return null + } + + private fun getDirActions(): ArrayList { + 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 { + 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) diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt index 54d5588da..a90169a6c 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt @@ -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 = arrayOf(), var otherActions: ArrayList? = null, + var fileName: String? = null, + var file: ByteArray? = null ) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt new file mode 100644 index 000000000..66702b436 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt @@ -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() + + 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) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt index 20b6f6a93..a203add69 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt @@ -32,7 +32,11 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment @@ -55,7 +59,11 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment viewModel.exportBookshelf(books) { - + exportBookshelf.launch { + mode = HandleFileContract.EXPORT + fileName = "bookshelf.json" + file = it.toByteArray() + } } R.id.menu_import_bookshelf -> importBookshelfAlert(groupId) }