Skip to content

Commit

Permalink
fix drawable unexpected canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed May 4, 2020
1 parent bb7054d commit 52919c8
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 184 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
11 changes: 8 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ android {
}

buildTypes {
debug {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
release {
minifyEnabled false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
Expand All @@ -74,8 +80,7 @@ dependencies {
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
implementation "androidx.appcompat:appcompat:1.1.0"
implementation "androidx.core:core-ktx:1.1.0"
implementation "androidx.core:core-ktx:1.2.0"
implementation "androidx.preference:preference:1.1.0"
testImplementation "junit:junit:4.12"
androidTestImplementation "androidx.test:runner:1.2.0"
Expand Down
29 changes: 2 additions & 27 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,5 @@
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# Retrofit
-dontwarn retrofit2.Platform$Java8
# okhttp
-dontwarn okio.**
-dontwarn org.conscrypt.**
-dontwarn javax.annotation.**

-dontwarn org.xmlpull.v1.XmlPullParser
-dontwarn org.xmlpull.v1.XmlSerializer
-keep class org.xmlpull.v1.* {*;}

# Gson
-keep class soko.ekibun.bangumi.api.**.bean.**{*;} # 自定义数据模型的bean目录

#不混淆Parcelable和它的子类,还有Creator成员变量
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

# custom view
-keep class soko.ekibun.bangumi.ui.view.NotifyActionProvider {*;}
-keep class soko.ekibun.bangumi.ui.view.*View {*;}

# glide webp
-keep public class com.bumptech.glide.integration.webp.WebpImage { *; }
-keep public class com.bumptech.glide.integration.webp.WebpFrame { *; }
-keep public class com.bumptech.glide.integration.webp.WebpBitmapFactory { *; }
-dontobfuscate
-keep class soko.ekibun.bangumi.**{*;}

This file was deleted.

10 changes: 3 additions & 7 deletions app/src/main/java/soko/ekibun/bangumi/ui/topic/ReplyDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.content.DialogInterface
import android.content.Intent
import android.provider.OpenableColumns
import android.text.Spanned
import android.text.style.ImageSpan
import android.view.KeyEvent
import android.view.MenuItem
import android.view.View
Expand All @@ -31,10 +30,7 @@ import soko.ekibun.bangumi.api.bangumi.Bangumi
import soko.ekibun.bangumi.ui.view.BaseDialog
import soko.ekibun.bangumi.util.HtmlUtil
import soko.ekibun.bangumi.util.ResourceUtil
import soko.ekibun.bangumi.util.span.ClickableImageSpan
import soko.ekibun.bangumi.util.span.ClickableUrlSpan
import soko.ekibun.bangumi.util.span.CollapseUrlDrawable
import soko.ekibun.bangumi.util.span.UploadDrawable
import soko.ekibun.bangumi.util.span.*
import java.lang.ref.WeakReference

/**
Expand Down Expand Up @@ -196,7 +192,7 @@ class ReplyDialog : BaseDialog(R.layout.dialog_reply) {
drawable.url = emojiList[position].second
view.item_input.editableText.insert(
view.item_input.selectionStart,
HtmlUtil.createImageSpan(ImageSpan(drawable, emojiList[position].first, ImageSpan.ALIGN_BASELINE))
HtmlUtil.createImageSpan(UrlImageSpan(drawable, emojiList[position].first))
)
drawable.container = WeakReference(view.item_input)
drawable.loadImage()
Expand Down Expand Up @@ -329,7 +325,7 @@ class ReplyDialog : BaseDialog(R.layout.dialog_reply) {
item_input.editableText.replace(start, end, "[img]$it[/img]")
}
}
val imageSpan = ImageSpan(drawable, "", ImageSpan.ALIGN_BASELINE)
val imageSpan = UrlImageSpan(drawable, "")
span = ClickableImageSpan(imageSpan, collapseImageGetter.onClick)
item_input.editableText.insert(
item_input.selectionStart,
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/soko/ekibun/bangumi/util/GlideUtil.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("DEPRECATION")

package soko.ekibun.bangumi.util

import android.app.Activity
Expand All @@ -15,7 +13,7 @@ import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.Headers
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition

Expand Down Expand Up @@ -63,7 +61,8 @@ object GlideUtil {
Headers { mapOf("referer" to url, "user-agent" to HttpUtil.ua) })
)
}
}.apply(options).into(object : SimpleTarget<Drawable>() {
}.apply(options).into(object : CustomTarget<Drawable>() {

override fun onLoadStarted(placeholder: Drawable?) {
callback(TYPE_PLACEHOLDER, placeholder)
}
Expand All @@ -75,7 +74,7 @@ object GlideUtil {

override fun onLoadCleared(placeholder: Drawable?) {
if (circularProgressDrawable.isRunning) circularProgressDrawable.stop()
callback(TYPE_PLACEHOLDER, null)
ProgressAppGlideModule.forget(url)
}

override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
Expand All @@ -87,7 +86,6 @@ object GlideUtil {
ProgressAppGlideModule.forget(url)
}
})

}

fun with(context: Context): RequestManager? {
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/java/soko/ekibun/bangumi/util/HtmlUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ object HtmlUtil {
fun attachToTextView(span: Spanned, textView: TextView) {
// 结束之前的请求
(textView.text as? Spanned)?.let { text ->
text.getSpans(0, text.length, ImageSpan::class.java).forEach {
text.getSpans(0, text.length, UrlImageSpan::class.java).filterNot {
span.getSpanFlags(it) == 0
}.forEach {
(it.drawable as? UrlDrawable)?.cancel()
}
}
// 更新引用
val weakRef = WeakReference(textView)
span.getSpans(0, span.length, ImageSpan::class.java).forEach { imageSpan ->
span.getSpans(0, span.length, UrlImageSpan::class.java).forEach { imageSpan ->
(imageSpan.drawable as? UrlDrawable)?.let {
it.container = weakRef
it.loadImage()
Expand Down Expand Up @@ -124,14 +126,13 @@ object HtmlUtil {
val sources = Bangumi.parseUrl(src)
val alt = node.attr("alt")
val isSmile = node.hasAttr("smileid")
val imageSpan = ImageSpan(
val imageSpan = UrlImageSpan(
imageGetter.getDrawable(sources),
if (isSmile) alt else src,
ImageSpan.ALIGN_BASELINE
if (isSmile) alt else src
)
createImageSpan(imageSpan).also {
if (!isSmile) {
imageGetter.drawables.add(sources)
imageGetter.drawables.add(imageSpan.url)
setSpan(
ClickableImageSpan(imageSpan, imageGetter.onClick),
it, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
Expand All @@ -149,7 +150,7 @@ object HtmlUtil {
return span
}

fun createImageSpan(imageSpan: ImageSpan): SpannableStringBuilder {
fun createImageSpan(imageSpan: UrlImageSpan): SpannableStringBuilder {
return SpannableStringBuilder("").also {
setSpan(imageSpan, it, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
Expand Down Expand Up @@ -204,10 +205,10 @@ object HtmlUtil {
) {
val drawables = ArrayList<String>()

open val onClick: (View, ImageSpan) -> Unit = { itemView, span ->
open val onClick: (View, UrlImageSpan) -> Unit = { itemView, span ->
PhotoPagerAdapter.showWindow(
itemView, drawables,
index = drawables.indexOf(span.source ?: "")
index = drawables.indexOf(span.url)
)
}

Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/soko/ekibun/bangumi/util/SpanFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import android.text.TextUtils
import android.text.style.*
import soko.ekibun.bangumi.api.bangumi.Bangumi
import soko.ekibun.bangumi.ui.topic.ReplyDialog
import soko.ekibun.bangumi.util.span.ClickableUrlSpan
import soko.ekibun.bangumi.util.span.CodeLineSpan
import soko.ekibun.bangumi.util.span.MaskSpan
import soko.ekibun.bangumi.util.span.QuoteLineSpan
import soko.ekibun.bangumi.util.span.*
import kotlin.math.roundToInt

abstract class SpanFormatter {
Expand Down Expand Up @@ -73,8 +70,8 @@ abstract class SpanFormatter {
is ForegroundColorSpan -> return "${
String.format("[color=#%06X]", 0xFFFFFF and span.foregroundColor)}${inner()}[/color]"
is MaskSpan -> return "[mask]${inner()}[/mask]"
is ImageSpan -> return if (span.source?.startsWith("(") == true) span.source ?: ""
else "[img]${span.source}[/img]"
is UrlImageSpan -> return if (span.url.startsWith("(")) span.url
else "[img]${span.url}[/img]"
}
return inner()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package soko.ekibun.bangumi.util.span

import android.text.style.ClickableSpan
import android.text.style.ImageSpan
import android.util.Log
import android.view.View

/**
*
*/
class ClickableImageSpan(
var image: ImageSpan,
private val onClick: (View, ImageSpan) -> Unit
var image: UrlImageSpan,
private val onClick: (View, UrlImageSpan) -> Unit
) : ClickableSpan() {
override fun onClick(widget: View) {
Log.v("click", this.toString())
Log.v("click", image.drawable.toString())
val drawable = image.drawable
if (drawable is UrlDrawable) {
if (drawable.error == true) drawable.loadImage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package soko.ekibun.bangumi.util.span
import android.graphics.*
import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
import android.text.style.ImageSpan
import android.util.Log
import android.text.Spannable
import android.util.Size
import android.view.View
import android.widget.TextView
Expand All @@ -21,45 +20,41 @@ open class CollapseUrlDrawable(
sizeCache: HashMap<String, Size>
) : UrlDrawable(wrapWidth, sizeCache) {

override var drawable: Drawable? = null
set(drawable) {
(field as? Animatable)?.stop()
field?.callback = null
field = drawable
field?.callback = drawableCallback
(drawable as? Animatable)?.start()
val drawableBounds = drawable?.bounds ?: bounds
setBounds(0, 0, drawableBounds.width(), Math.min(drawableBounds.height(), 250))
mBuffer = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888)
updateBuffer()

container?.get()?.let {
val text = (it.text as? Spannable) ?: return@let
text.getSpans(0, text.length, UrlImageSpan::class.java)?.filter { span ->
span.drawable == this
}?.forEach { span ->
span.url = url ?: ""
val start = text.getSpanStart(span)
val end = text.getSpanEnd(span)
val flags = text.getSpanFlags(span)
text.removeSpan(span)
text.setSpan(span, start, end, flags)
}
it.invalidate()
}
}

override fun update(drawable: Drawable) {
val size = {
val width = wrapWidth(if (error == false) drawable.intrinsicWidth.toFloat() else -1f)
Size(width.toInt(), (drawable.intrinsicHeight * width / drawable.intrinsicWidth).toInt())
}()
Log.v("update", "$drawable size: $size")
(this.drawable as? Animatable)?.stop()
this.drawable?.callback = null
drawable.setBounds(0, 0, size.width, size.height)
this.drawable = drawable
this.drawable?.callback = this.drawableCallback
(drawable as? Animatable)?.start()
setBounds(0, 0, size.width, Math.min(size.height, 250))
mBuffer = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888)
this.drawable?.setBounds(0, 0, size.width, size.height)
updateBuffer()

container?.get()?.let {
it.editableText.getSpans(0, it.editableText.length, ClickableImageSpan::class.java).filter { span ->
span.image.drawable == this
}.forEach { span ->
val start = it.editableText.getSpanStart(span)
val end = it.editableText.getSpanEnd(span)
val flags = it.editableText.getSpanFlags(span)

it.editableText.removeSpan(span.image)
span.image = ImageSpan(this, url ?: "", ImageSpan.ALIGN_BASELINE)
it.editableText.setSpan(span.image, start, end, flags)
}
it.editableText.getSpans(0, it.editableText.length, ImageSpan::class.java).filter { span ->
span.drawable == this
}.forEach { span ->
val start = it.editableText.getSpanStart(span)
val end = it.editableText.getSpanEnd(span)
val flags = it.editableText.getSpanFlags(span)
it.editableText.removeSpan(span)
it.editableText.setSpan(span, start, end, flags)
}
it.invalidate()
}
}

private val gradientPaint by lazy {
Expand Down Expand Up @@ -87,8 +82,8 @@ open class CollapseUrlDrawable(
class CollapseImageGetter(container: TextView) : HtmlUtil.ImageGetter(wrapWidth = {
Math.min(container.width.toFloat(), Math.max(container.textSize, it))
}) {
override val onClick: (View, ImageSpan) -> Unit = { itemView, span ->
Toast.makeText(itemView.context, span.source, Toast.LENGTH_LONG).show()
override val onClick: (View, UrlImageSpan) -> Unit = { itemView, span ->
Toast.makeText(itemView.context, span.url, Toast.LENGTH_LONG).show()
}

override fun createDrawable(): UrlDrawable {
Expand Down
Loading

0 comments on commit 52919c8

Please sign in to comment.