diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt b/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt index 931f7311e..b2d4d0df8 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt @@ -125,14 +125,6 @@ object EhClient { params[2] as String?, ) - METHOD_DOWNLOAD_ARCHIVE -> EhEngine.downloadArchive( - params[0] as Long, - params[1] as String?, - params[2] as String?, - params[3] as String?, - params[4] as Boolean, - ) - else -> throw IllegalStateException("Can't detect method $method") } } @@ -156,5 +148,4 @@ object EhClient { const val METHOD_VOTE_COMMENT = 15 const val METHOD_IMAGE_SEARCH = 16 const val METHOD_ARCHIVE_LIST = 17 - const val METHOD_DOWNLOAD_ARCHIVE = 18 } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index 8017d28ed..1f90aa5da 100755 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -170,6 +170,7 @@ import com.hippo.util.ExceptionUtils import com.hippo.util.ReadableTime import com.hippo.util.addTextToClipboard import com.hippo.util.getParcelableCompat +import com.hippo.util.getSystemService import com.hippo.widget.ObservedTextView import com.hippo.widget.recyclerview.getSpanCountForSuitableSize import com.hippo.yorozuya.FileUtils @@ -1543,41 +1544,6 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener { } } - private class DownloadArchiveListener( - context: Context, - private val mGalleryInfo: GalleryInfo?, - ) : EhCallback(context) { - override fun onSuccess(result: String?) { - // TODO: Don't use buggy system download service - val r = DownloadManager.Request(Uri.parse(result)) - val name = - mGalleryInfo!!.gid.toString() + "-" + EhUtils.getSuitableTitle(mGalleryInfo) + ".zip" - r.setDestinationInExternalPublicDir( - Environment.DIRECTORY_DOWNLOADS, - FileUtils.sanitizeFilename(name), - ) - r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) - val dm = application.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager - try { - dm.enqueue(r) - } catch (e: Throwable) { - e.printStackTrace() - ExceptionUtils.throwIfFatal(e) - } - showTip(R.string.download_archive_started, LENGTH_SHORT) - } - - override fun onFailure(e: Exception) { - if (e is NoHAtHClientException) { - showTip(R.string.download_archive_failure_no_hath, LENGTH_LONG) - } else { - showTip(R.string.download_archive_failure, LENGTH_LONG) - } - } - - override fun onCancel() {} - } - private inner class DeleteDialogHelper( private val mDownloadManager: com.hippo.ehviewer.download.DownloadManager?, private val mGalleryInfo: GalleryInfo, @@ -1713,17 +1679,30 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener { } val res = mArchiveList!![position].res val isHAtH = mArchiveList!![position].isHAtH - val request = EhRequest() - request.setMethod(EhClient.METHOD_DOWNLOAD_ARCHIVE) - request.setArgs( - composeBindingGD!!.gid, - composeBindingGD!!.token!!, - mArchiveFormParamOr!!, - res, - isHAtH, - ) - request.setCallback(DownloadArchiveListener(context, composeBindingGD)) - request.enqueue(this@GalleryDetailScene) + composeBindingGD?.run { + lifecycleScope.launchIO { + runSuspendCatching { + EhEngine.downloadArchive(gid, token, mArchiveFormParamOr, res, isHAtH) + }.onSuccess { result -> + val r = DownloadManager.Request(Uri.parse(result)) + val name = gid.toString() + "-" + EhUtils.getSuitableTitle(this@run) + ".zip" + r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, FileUtils.sanitizeFilename(name)) + r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) + runCatching { + requireContext().getSystemService()!!.enqueue(r) + }.onFailure { + it.printStackTrace() + } + showTip(R.string.download_archive_started, LENGTH_SHORT) + }.onFailure { + if (it is NoHAtHClientException) { + showTip(R.string.download_archive_failure_no_hath, LENGTH_LONG) + } else { + showTip(R.string.download_archive_failure, LENGTH_LONG) + } + } + } + } } if (mDialog != null) { mDialog!!.dismiss() diff --git a/app/src/main/java/com/hippo/util/AppHelper.kt b/app/src/main/java/com/hippo/util/AppHelper.kt index 9876b663b..f9b92d02a 100644 --- a/app/src/main/java/com/hippo/util/AppHelper.kt +++ b/app/src/main/java/com/hippo/util/AppHelper.kt @@ -16,8 +16,10 @@ package com.hippo.util import android.app.Activity +import android.content.Context import android.content.Intent import android.widget.Toast +import androidx.core.content.ContextCompat import com.hippo.ehviewer.R object AppHelper { @@ -37,3 +39,5 @@ object AppHelper { } } } + +inline fun Context.getSystemService(): T? = ContextCompat.getSystemService(this, T::class.java)