Skip to content

Commit

Permalink
Workaround for image search
Browse files Browse the repository at this point in the history
  • Loading branch information
xb2016 committed Mar 25, 2024
1 parent 2fccac1 commit 48b9772
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 180 deletions.
27 changes: 13 additions & 14 deletions app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@ data class ListUrlBuilder(
private var mJumpTo: String? = null,
var category: Int = EhUtils.NONE,
private var mKeyword: String? = null,
private var mSHash: String? = null,
var hash: String? = null,
var advanceSearch: Int = -1,
var minRating: Int = -1,
var pageFrom: Int = -1,
var pageTo: Int = -1,
var imagePath: String? = null,
var isUseSimilarityScan: Boolean = false,
var isOnlySearchCovers: Boolean = false,
var isShowExpunged: Boolean = false,
) : Parcelable {
fun reset() {
Expand All @@ -60,11 +57,8 @@ data class ListUrlBuilder(
minRating = -1
pageFrom = -1
pageTo = -1
imagePath = null
isUseSimilarityScan = false
isOnlySearchCovers = false
isShowExpunged = false
mSHash = null
hash = null
}

fun setIndex(index: String?, isNext: Boolean = true) {
Expand All @@ -90,9 +84,6 @@ data class ListUrlBuilder(
minRating = q.minRating
pageFrom = q.pageFrom
pageTo = q.pageTo
imagePath = null
isUseSimilarityScan = false
isOnlySearchCovers = false
isShowExpunged = false
}

Expand Down Expand Up @@ -248,7 +239,7 @@ data class ListUrlBuilder(

"f_spf" -> pageFrom = NumberUtils.parseIntSafely(value, -1)
"f_spt" -> pageTo = NumberUtils.parseIntSafely(value, -1)
"f_shash" -> mSHash = value
"f_shash" -> hash = value
}
}
this.category = category
Expand Down Expand Up @@ -291,7 +282,7 @@ data class ListUrlBuilder(
ub.addQuery("f_search", encodeUTF8(this))
}
}
mSHash?.let {
hash?.let {
ub.addQuery("f_shash", it)
}
mJumpTo?.let {
Expand Down Expand Up @@ -363,7 +354,15 @@ data class ListUrlBuilder(
}

MODE_WHATS_HOT -> EhUrl.popularUrl
MODE_IMAGE_SEARCH -> EhUrl.imageSearchUrl

MODE_IMAGE_SEARCH -> {
val ub = UrlBuilder(EhUrl.host)
hash?.let {
ub.addQuery("f_shash", it)
}
ub.build()
}

MODE_TOPLIST -> {
val sb = StringBuilder(EhUrl.HOST_E)
sb.append("toplist.php?tl=")
Expand Down
33 changes: 4 additions & 29 deletions app/src/main/java/com/hippo/ehviewer/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import android.content.pm.PackageManager
import android.content.pm.verify.domain.DomainVerificationManager
import android.content.pm.verify.domain.DomainVerificationUserState
import android.content.res.Configuration
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
Expand All @@ -47,7 +45,6 @@ import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import com.google.android.material.snackbar.Snackbar
import com.hippo.app.EditTextDialogBuilder
import com.hippo.ehviewer.AppConfig
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhUrlOpener
Expand All @@ -72,23 +69,19 @@ import com.hippo.ehviewer.ui.scene.SignInScene
import com.hippo.ehviewer.ui.scene.SolidScene
import com.hippo.ehviewer.ui.scene.WebViewSignInScene
import com.hippo.ehviewer.widget.EhStageLayout
import com.hippo.image.Image
import com.hippo.scene.Announcer
import com.hippo.scene.SceneFragment
import com.hippo.scene.StageActivity
import com.hippo.unifile.UniFile
import com.hippo.unifile.sha1
import com.hippo.util.addTextToClipboard
import com.hippo.util.getClipboardManager
import com.hippo.util.getParcelableExtraCompat
import com.hippo.util.getUrlFromClipboard
import com.hippo.widget.DrawerView
import com.hippo.widget.LoadImageView
import com.hippo.yorozuya.IOUtils
import com.hippo.yorozuya.SimpleHandler
import com.hippo.yorozuya.ViewUtils
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream

class MainActivity : StageActivity(), NavigationView.OnNavigationItemSelectedListener {
private val settingsLauncher =
Expand Down Expand Up @@ -139,22 +132,6 @@ class MainActivity : StageActivity(), NavigationView.OnNavigationItemSelectedLis
return announcer
}

private fun saveImageToTempFile(uri: Uri): File? {
val src = ImageDecoder.createSource(contentResolver, uri)
val bitmap = runCatching { ImageDecoder.decodeBitmap(src, Image.imageSearchDecoderSampleListener) }.getOrNull() ?: return null
val temp = AppConfig.createTempFile() ?: return null
var os: OutputStream? = null
return try {
os = FileOutputStream(temp)
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os)
temp
} catch (e: IOException) {
null
} finally {
IOUtils.closeQuietly(os)
}
}

private fun handleIntent(intent: Intent?): Boolean {
if (intent == null) {
return false
Expand All @@ -177,12 +154,10 @@ class MainActivity : StageActivity(), NavigationView.OnNavigationItemSelectedLis
} else if (type != null && type.startsWith("image/")) {
val uri = intent.getParcelableExtraCompat<Uri>(Intent.EXTRA_STREAM)
if (null != uri) {
val temp = saveImageToTempFile(uri)
if (null != temp) {
UniFile.fromUri(this, uri)?.sha1()?.let {
val builder = ListUrlBuilder()
builder.mode = ListUrlBuilder.MODE_IMAGE_SEARCH
builder.imagePath = temp.path
builder.isUseSimilarityScan = true
builder.hash = it
startScene(processAnnouncer(GalleryListScene.getStartAnnouncer(builder)))
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.lifecycleScope
import androidx.transition.TransitionInflater
import coil.Coil.imageLoader
import com.google.android.material.progressindicator.CircularProgressIndicator
import com.google.android.material.snackbar.Snackbar
import com.hippo.app.CheckBoxDialogBuilder
Expand Down Expand Up @@ -996,22 +995,11 @@ class GalleryDetailScene :
}

private fun showCoverGalleryList() {
val context = context ?: return
val gid = gid
if (-1L == gid) {
return
}
try {
val key = EhCacheKeyFactory.getThumbKey(gid)
val path = imageLoader(context).diskCache!!.openSnapshot(key)!!.use { it.data }
val lub = ListUrlBuilder()
lub.mode = ListUrlBuilder.MODE_IMAGE_SEARCH
lub.imagePath = path.toString()
lub.isUseSimilarityScan = true
GalleryListScene.startScene(this, lub)
} catch (e: Throwable) {
e.printStackTrace()
}
val uri = mGalleryInfo?.thumb ?: mGalleryDetail?.thumb ?: return
val lub = ListUrlBuilder()
lub.mode = ListUrlBuilder.MODE_IMAGE_SEARCH
lub.hash = uri.substringAfterLast('/').substringBefore('-')
GalleryListScene.startScene(this, lub)
}

override fun onClick(v: View) {
Expand Down
33 changes: 8 additions & 25 deletions app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ import com.hippo.yorozuya.AnimationUtils
import com.hippo.yorozuya.LayoutUtils
import com.hippo.yorozuya.MathUtils
import com.hippo.yorozuya.SimpleAnimatorListener
import com.hippo.yorozuya.StringUtils
import com.hippo.yorozuya.ViewUtils
import rikka.core.res.resolveColor
import java.io.File
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
Expand Down Expand Up @@ -1595,29 +1593,14 @@ class GalleryListScene :
mUrlBuilder.setIndex(index, isNext)
mUrlBuilder.setJumpTo(jumpTo)
}
if (ListUrlBuilder.MODE_IMAGE_SEARCH == mUrlBuilder.mode) {
val request = EhRequest()
request.setMethod(EhClient.METHOD_IMAGE_SEARCH)
request.setCallback(
GetGalleryListListener(context, taskId),
)
request.setArgs(
File(StringUtils.avoidNull(mUrlBuilder.imagePath)),
mUrlBuilder.isUseSimilarityScan,
mUrlBuilder.isOnlySearchCovers,
mUrlBuilder.isShowExpunged,
)
request.enqueue(this@GalleryListScene)
} else {
val url = mUrlBuilder.build()
val request = EhRequest()
request.setMethod(EhClient.METHOD_GET_GALLERY_LIST)
request.setCallback(
GetGalleryListListener(context, taskId),
)
request.setArgs(url)
request.enqueue(this@GalleryListScene)
}
val url = mUrlBuilder.build()
val request = EhRequest()
request.setMethod(EhClient.METHOD_GET_GALLERY_LIST)
request.setCallback(
GetGalleryListListener(context, taskId),
)
request.setArgs(url)
request.enqueue(this@GalleryListScene)
}

override val context
Expand Down
47 changes: 14 additions & 33 deletions app/src/main/java/com/hippo/ehviewer/widget/ImageSearchLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,25 @@
package com.hippo.ehviewer.widget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.net.Uri
import android.os.Parcel
import android.os.Parcelable
import android.util.AttributeSet
import android.view.AbsSavedState
import android.view.LayoutInflater
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.hippo.ehviewer.AppConfig
import com.hippo.ehviewer.R
import com.hippo.ehviewer.client.data.ListUrlBuilder
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.image.Image.Companion.imageSearchDecoderSampleListener
import com.hippo.unifile.UniFile
import com.hippo.unifile.openInputStream
import com.hippo.unifile.sha1
import com.hippo.yorozuya.ViewUtils
import java.io.FileInputStream
import java.io.IOException

class ImageSearchLayout @JvmOverloads constructor(
context: Context,
Expand All @@ -52,10 +48,8 @@ class ImageSearchLayout @JvmOverloads constructor(
View.OnClickListener {
private var mPreview: ImageView? = null
private var mSelectImage: View? = null
private var mSearchUSS: CheckBox? = null
private var mSearchOSC: CheckBox? = null
private var mHelper: Helper? = null
private var mImagePath: String? = null
private var mImageUri: Uri? = null

init {
orientation = VERTICAL
Expand All @@ -66,8 +60,6 @@ class ImageSearchLayout @JvmOverloads constructor(
LayoutInflater.from(context).inflate(R.layout.widget_image_search, this)
mPreview = ViewUtils.`$$`(this, R.id.preview) as ImageView
mSelectImage = ViewUtils.`$$`(this, R.id.select_image)
mSearchUSS = ViewUtils.`$$`(this, R.id.search_uss) as CheckBox
mSearchOSC = ViewUtils.`$$`(this, R.id.search_osc) as CheckBox
mSelectImage!!.setOnClickListener(this)
}

Expand All @@ -88,22 +80,11 @@ class ImageSearchLayout @JvmOverloads constructor(
return
}
val context = context
UniFile.fromUri(context, imageUri) ?: return
try {
val src = ImageDecoder.createSource(context.contentResolver, imageUri)
val bitmap = ImageDecoder.decodeBitmap(src, imageSearchDecoderSampleListener)
val temp = AppConfig.createTempFile() ?: return

// TODO ehentai image search is bad when I'm writing this line.
// Re-compress image will make image search failed.
temp.outputStream().use {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, it)
mImagePath = temp.path
mPreview!!.setImageBitmap(bitmap)
mPreview!!.setVisibility(VISIBLE)
}
} catch (e: IOException) {
e.printStackTrace()
UniFile.fromUri(context, imageUri)?.openInputStream().use {
val bitmap = BitmapFactory.decodeStream(it) ?: return
mImageUri = imageUri
mPreview!!.setImageBitmap(bitmap)
mPreview!!.setVisibility(VISIBLE)
}
}

Expand All @@ -113,25 +94,25 @@ class ImageSearchLayout @JvmOverloads constructor(
}
FileInputStream(imagePath).use {
val bitmap = BitmapFactory.decodeStream(it) ?: return
mImagePath = imagePath
mImageUri = Uri.parse(imagePath)
mPreview!!.setImageBitmap(bitmap)
mPreview!!.setVisibility(VISIBLE)
}
}

fun formatListUrlBuilder(builder: ListUrlBuilder) {
if (null == mImagePath) {
if (null == mImageUri) {
throw EhException(context.getString(R.string.select_image_first))
}
builder.imagePath = mImagePath
builder.isUseSimilarityScan = mSearchUSS!!.isChecked
builder.isOnlySearchCovers = mSearchOSC!!.isChecked
UniFile.fromUri(context, mImageUri!!)?.sha1()?.let {
builder.hash = it
}
}

override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
val ss = SavedState(superState)
ss.imagePath = mImagePath
ss.imagePath = mImageUri.toString()
return ss
}

Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/com/hippo/image/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import androidx.core.graphics.drawable.toDrawable
import com.hippo.ehviewer.EhApplication
import com.hippo.ehviewer.R
import java.nio.ByteBuffer
import kotlin.math.min

Expand Down Expand Up @@ -135,17 +134,6 @@ class Image private constructor(
).coerceAtLeast(1)
}

private val imageSearchMaxSize =
EhApplication.application.resources.getDimensionPixelOffset(R.dimen.image_search_max_size)

@JvmStatic
val imageSearchDecoderSampleListener =
ImageDecoder.OnHeaderDecodedListener { decoder, info, _ ->
decoder.setTargetSampleSize(
calculateSampleSize(info, imageSearchMaxSize, imageSearchMaxSize),
)
}

val screenWidth = EhApplication.application.resources.displayMetrics.widthPixels
val screenHeight = EhApplication.application.resources.displayMetrics.heightPixels

Expand Down
Loading

0 comments on commit 48b9772

Please sign in to comment.