Use Splitties systemServices

This commit is contained in:
tarsin committed 2023-06-01 16:13:37 +08:00
1 parent 2c30deb298
commit f33ff6d380
7 files changed
+25 -179

No files matched your search

@@ -37,7 +37,6 @@ import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import androidx.core.content.getSystemService
import androidx.core.view.GravityCompat
import androidx.customview.widget.Openable
import androidx.drawerlayout.widget.DrawerLayout
@@ -69,7 +68,6 @@ import com.hippo.ehviewer.ui.scene.GalleryListScene
import com.hippo.ehviewer.ui.scene.GalleryListScene.Companion.toStartArgs
import com.hippo.ehviewer.ui.scene.ProgressScene
import com.hippo.ehviewer.util.addTextToClipboard
import com.hippo.ehviewer.util.getClipboardManager
import com.hippo.ehviewer.util.getParcelableExtraCompat
import com.hippo.ehviewer.util.getUrlFromClipboard
import com.hippo.ehviewer.yorozuya.IOUtils
@@ -77,6 +75,8 @@ import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.lang.withUIContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import splitties.systemservices.clipboardManager
import splitties.systemservices.connectivityManager
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
@@ -85,7 +85,6 @@ import java.io.OutputStream
class MainActivity : EhActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
private val connectivityManager by lazy { getSystemService<ConnectivityManager>()!! }
private val availableNetworks = mutableListOf<Network>()
private fun saveImageToTempFile(uri: Uri): File? {
@@ -354,7 +353,7 @@ class MainActivity : EhActivity() {
}
private suspend fun checkClipboardUrl() {
val text = this.getClipboardManager().getUrlFromClipboard(this)
val text = clipboardManager.getUrlFromClipboard(this)
val hashCode = text?.hashCode() ?: 0
if (text != null && hashCode != 0 && Settings.clipboardTextHashCode != hashCode) {
val result1 = GalleryDetailUrlParser.parse(text, false)
@@ -20,7 +20,6 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.app.DownloadManager
import android.content.Context
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.net.Uri
@@ -109,7 +108,6 @@ import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import arrow.core.partially1
import coil.Coil.imageLoader
import coil.annotation.ExperimentalCoilApi
import coil.imageLoader
import com.google.android.material.progressindicator.CircularProgressIndicator
@@ -173,7 +171,6 @@ import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.ReadableTime
import com.hippo.ehviewer.util.addTextToClipboard
import com.hippo.ehviewer.util.getParcelableCompat
import com.hippo.ehviewer.util.getSystemService
import com.hippo.ehviewer.yorozuya.FileUtils
import com.hippo.ehviewer.yorozuya.ViewUtils
import com.hippo.ehviewer.yorozuya.collect.IntList
@@ -183,8 +180,9 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import moe.tarsin.coroutines.runSuspendCatching
import okhttp3.HttpUrl.Companion.toHttpUrl
import splitties.systemservices.downloadManager
import kotlin.math.roundToInt
import com.hippo.ehviewer.download.DownloadManager as downloadManager
import com.hippo.ehviewer.download.DownloadManager as ehDownloadManager
class GalleryDetailScene : BaseScene(), DownloadInfoListener {
private var mDownloadState = 0
@@ -385,7 +383,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
// Get download state
val gid = gid
mDownloadState = if (gid != -1L) {
downloadManager.getDownloadState(gid)
ehDownloadManager.getDownloadState(gid)
} else {
DownloadInfo.STATE_INVALID
}
@@ -403,7 +401,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
} else {
getDetailError = getString(R.string.error_cannot_find_gallery)
}
downloadManager.addDownloadInfoListener(this)
ehDownloadManager.addDownloadInfoListener(this)
(requireActivity() as MainActivity).mShareUrl = galleryDetailUrl
return ComposeView(requireContext()).apply {
setMD3Content {
@@ -828,7 +826,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
private fun onDownloadButtonClick() {
val galleryDetail = composeBindingGD ?: return
if (downloadManager.getDownloadState(galleryDetail.gid) == DownloadInfo.STATE_INVALID) {
if (ehDownloadManager.getDownloadState(galleryDetail.gid) == DownloadInfo.STATE_INVALID) {
CommonOperations.startDownload(
activity as MainActivity,
galleryDetail,
@@ -845,7 +843,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
Settings.removeImageFiles,
)
val helper = DeleteDialogHelper(
downloadManager,
ehDownloadManager,
galleryDetail,
builder,
)
@@ -859,7 +857,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
override fun onDestroyView() {
super.onDestroyView()
downloadManager.removeDownloadInfoListener(this)
ehDownloadManager.removeDownloadInfoListener(this)
(requireActivity() as MainActivity).mShareUrl = null
}
@@ -1197,7 +1195,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
@OptIn(ExperimentalCoilApi::class)
private fun showCoverGalleryList() {
val context = context ?: return
context ?: return
val gid = gid
if (-1L == gid) {
return
@@ -1621,7 +1619,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, FileUtils.sanitizeFilename(name))
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
runCatching {
requireContext().getSystemService<DownloadManager>()!!.enqueue(r)
downloadManager.enqueue(r)
}.onFailure {
it.printStackTrace()
}
@@ -1726,9 +1724,8 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
)
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
r.addRequestHeader("Cookie", EhCookieStore.getCookieHeader(url.toHttpUrl()))
val dm = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
try {
dm.enqueue(r)
downloadManager.enqueue(r)
showTip(R.string.download_torrent_started, LENGTH_SHORT)
} catch (e: Throwable) {
e.printStackTrace()
@@ -16,10 +16,8 @@
package com.hippo.ehviewer.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 {
@@ -39,5 +37,3 @@ object AppHelper {
}
}
}
inline fun <reified T> Context.getSystemService(): T? = ContextCompat.getSystemService(this, T::class.java)
@@ -29,14 +29,7 @@ import com.hippo.ehviewer.R
import com.hippo.ehviewer.ui.MainActivity
import com.hippo.ehviewer.ui.SettingsActivity
import com.hippo.ehviewer.ui.scene.BaseScene
fun Context.getClipboardManager(): ClipboardManager {
return getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
}
infix fun Context.tellClipboard(text: String?) {
addTextToClipboard(text, false)
}
import splitties.systemservices.clipboardManager
infix fun Context.tellClipboardWithToast(text: String?) {
addTextToClipboard(text, false, true)
@@ -44,7 +37,7 @@ infix fun Context.tellClipboardWithToast(text: String?) {
@JvmOverloads
fun Context.addTextToClipboard(text: CharSequence?, isSensitive: Boolean, useToast: Boolean = false) {
getClipboardManager().apply {
clipboardManager.apply {
setPrimaryClip(
ClipData.newPlainText(null, text).apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && isSensitive) {
@@ -65,21 +58,9 @@ fun Context.addTextToClipboard(text: CharSequence?, isSensitive: Boolean, useToa
}
}
fun ClipboardManager.getTextFromClipboard(context: Context): String? {
val item = primaryClip?.getItemAt(0)
val string = item?.coerceToText(context).toString()
return if (!TextUtils.isEmpty(string)) string else null
}
fun ClipboardManager.getUrlFromClipboard(context: Context): String? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && primaryClipDescription?.classificationStatus == ClipDescription.CLASSIFICATION_COMPLETE) {
if ((
primaryClipDescription?.getConfidenceScore(TextClassifier.TYPE_URL)
?.let { it <= 0 }
) == true
) {
return null
}
if ((primaryClipDescription?.getConfidenceScore(TextClassifier.TYPE_URL)?.let { it <= 0 }) == true) return null
}
val item = primaryClip?.getItemAt(0)
val string = item?.coerceToText(context).toString()