-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEnrichedImageSpan.kt
More file actions
206 lines (175 loc) · 5.53 KB
/
Copy pathEnrichedImageSpan.kt
File metadata and controls
206 lines (175 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.swmansion.enriched.common.spans
import android.content.res.Resources
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.ImageDecoder
import android.graphics.Paint
import android.graphics.drawable.AnimatedImageDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.text.Spannable
import android.text.style.ImageSpan
import android.util.Log
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.graphics.drawable.toDrawable
import androidx.core.graphics.withSave
import com.swmansion.enriched.common.AsyncDrawable
import com.swmansion.enriched.common.ForceRedrawSpan
import com.swmansion.enriched.common.spans.interfaces.EnrichedInlineSpan
import java.io.File
open class EnrichedImageSpan :
ImageSpan,
EnrichedInlineSpan {
private var width: Int = 0
private var height: Int = 0
private var isStaticPlaceholder: Boolean = false
constructor(
drawable: Drawable,
source: String,
width: Int,
height: Int,
isStaticPlaceholder: Boolean = false,
) : super(drawable, source, ALIGN_BASELINE) {
this.width = width
this.height = height
this.isStaticPlaceholder = isStaticPlaceholder
}
fun refreshPlaceholderTint(color: Int) {
val d = drawable
if (d is AsyncDrawable) {
d.applyPlaceholderTint(color)
} else if (isStaticPlaceholder) {
DrawableCompat.setTint(d, color)
}
}
override fun draw(
canvas: Canvas,
text: CharSequence?,
start: Int,
end: Int,
x: Float,
top: Int,
y: Int,
bottom: Int,
paint: Paint,
) {
val drawable = drawable
canvas.withSave {
val transY = bottom - drawable.bounds.bottom - paint.fontMetricsInt.descent
translate(x, transY.toFloat())
drawable.draw(this)
}
}
override fun getDrawable(): Drawable {
val drawable = super.getDrawable()
val scale = Resources.getSystem().displayMetrics.density
drawable.setBounds(0, 0, (width * scale).toInt(), (height * scale).toInt())
return drawable
}
override fun getSize(
paint: Paint,
text: CharSequence?,
start: Int,
end: Int,
fm: Paint.FontMetricsInt?,
): Int {
val d = drawable
val rect = d.bounds
if (fm != null) {
val imageHeight = rect.bottom - rect.top
// We want the image bottom to sit on the baseline (0).
// Therefore, the image top will be at: -imageHeight.
val targetTop = -imageHeight
// Expand the line UPWARDS if the image is taller than the current font
if (targetTop < fm.ascent) {
fm.ascent = targetTop
fm.top = targetTop
}
}
return rect.right
}
private fun registerDrawableLoadCallback(
d: AsyncDrawable,
text: Spannable?,
) {
d.onLoaded = onLoaded@{
val spannable = text
if (spannable == null) {
return@onLoaded
}
// Ensure we are on the Main Thread before modifying the Spannable
Handler(Looper.getMainLooper()).post {
val start = spannable.getSpanStart(this@EnrichedImageSpan)
val end = spannable.getSpanEnd(this@EnrichedImageSpan)
if (start != -1 && end != -1) {
// trick for adding empty span to force redraw when image is loaded
val redrawSpan = ForceRedrawSpan()
spannable.setSpan(redrawSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.removeSpan(redrawSpan)
}
}
}
}
fun observeAsyncDrawableLoaded(text: Spannable?) {
val d = drawable
if (d !is AsyncDrawable) {
return
}
registerDrawableLoadCallback(d, text)
// If it's already loaded (race condition), run logic immediately
if (d.isLoaded) {
d.onLoaded?.invoke()
}
}
fun getWidth(): Int = width
fun getHeight(): Int = height
companion object {
fun prepareDrawableForImage(
src: String,
width: Int,
height: Int,
placeholderTintColor: Int,
): Drawable? {
var cleanPath = src
if (cleanPath.startsWith("http://") || cleanPath.startsWith("https://")) {
return AsyncDrawable(cleanPath, placeholderTintColor)
}
if (cleanPath.startsWith("file://")) {
cleanPath = cleanPath.substring(7)
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
return try {
val bitmap = BitmapFactory.decodeFile(cleanPath) ?: return null
val drawable = bitmap.toDrawable(Resources.getSystem())
drawable.setBounds(0, 0, bitmap.width, bitmap.height)
return drawable
} catch (e: Exception) {
Log.e("EnrichedImageSpan", "Failed to load legacy image: $cleanPath", e)
null
}
}
return try {
val file = File(cleanPath)
val source = ImageDecoder.createSource(file)
val density = Resources.getSystem().displayMetrics.density
val targetWidthPx = (width * density).toInt()
val targetHeightPx = (height * density).toInt()
val drawable =
ImageDecoder.decodeDrawable(source) { decoder, info, source ->
decoder.setTargetSize(targetWidthPx, targetHeightPx)
}
if (drawable is AnimatedImageDrawable) {
drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
drawable.repeatCount = AnimatedImageDrawable.REPEAT_INFINITE
drawable.start()
}
drawable
} catch (e: Exception) {
Log.e("EnrichedImageSpan", "Failed to load image: $cleanPath", e)
null
}
}
}
}