Skip to content

Commit 7409004

Browse files
committed
add logo cache
1 parent 30b9c89 commit 7409004

File tree

11 files changed

+195
-134
lines changed

11 files changed

+195
-134
lines changed

HISTORY.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
## 更新日誌
22

3+
### v1.3.8.16
4+
5+
* 增加LOGO緩存
6+
* 支持設置多個EPG地址
7+
8+
### v1.3.8.15-kitkat
9+
10+
* 修復頻道記憶失敗的問題
11+
* 打開頻道列表同時顯示組
12+
* 增加EPG緩存
13+
314
### v1.3.8.15
415

516
* 修復頻道記憶失敗的問題

app/src/main/java/com/lizongying/mytv0/ImageHelper.kt

Lines changed: 96 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,106 @@ package com.lizongying.mytv0
33
import android.content.Context
44
import android.graphics.Bitmap
55
import android.graphics.drawable.BitmapDrawable
6-
import android.graphics.drawable.Drawable
7-
import android.os.Handler
6+
import android.util.Log
87
import android.widget.ImageView
98
import com.bumptech.glide.Glide
10-
import com.bumptech.glide.load.DataSource
11-
import com.bumptech.glide.load.engine.GlideException
12-
import com.bumptech.glide.request.RequestListener
13-
import com.bumptech.glide.request.target.Target
9+
import com.lizongying.mytv0.requests.HttpClient
10+
import kotlinx.coroutines.Dispatchers
11+
import kotlinx.coroutines.withContext
12+
import java.io.File
1413

15-
fun loadNextUrl(
16-
context: Context,
17-
imageView: ImageView,
18-
bitmap: Bitmap,
19-
urlList: List<String>,
20-
index: Int,
21-
handler: Handler,
22-
onSuccess: (Int) -> Unit
23-
) {
24-
if (urlList.isEmpty()) {
25-
return
26-
}
27-
if (index >= urlList.size) {
28-
return
14+
15+
class ImageHelper(private val context: Context) {
16+
val cacheDir = context.cacheDir
17+
val files: MutableMap<String, File> = mutableMapOf()
18+
19+
init {
20+
val dir = File(cacheDir, LOGO)
21+
if (!dir.exists()) {
22+
dir.mkdir()
23+
}
24+
dir.listFiles()?.forEach { file ->
25+
val name = file.name.substringBeforeLast(".")
26+
files[name] = file
27+
}
2928
}
30-
val url = urlList[index]
31-
if (url.isEmpty()) {
32-
Glide.with(context)
33-
.load(bitmap)
34-
.fitCenter()
35-
.into(imageView)
36-
} else {
37-
Glide.with(context)
38-
.load(url)
39-
.listener(object : RequestListener<Drawable> {
40-
override fun onResourceReady(
41-
resource: Drawable,
42-
model: Any,
43-
target: Target<Drawable>?,
44-
dataSource: DataSource,
45-
isFirstResource: Boolean
46-
): Boolean {
47-
onSuccess(index)
48-
return false
49-
}
5029

51-
override fun onLoadFailed(
52-
e: GlideException?,
53-
model: Any?,
54-
target: Target<Drawable>,
55-
isFirstResource: Boolean
56-
): Boolean {
57-
handler.post {
58-
loadNextUrl(
59-
context,
60-
imageView,
61-
bitmap,
62-
urlList,
63-
index + 1,
64-
handler,
65-
onSuccess
66-
)
67-
}
68-
return true
30+
private suspend fun downloadImage(url: String, file: File): Boolean {
31+
return withContext(Dispatchers.IO) {
32+
try {
33+
val request = okhttp3.Request.Builder()
34+
.url(url)
35+
.build()
36+
37+
HttpClient.okHttpClient.newCall(request).execute().use { response ->
38+
if (!response.isSuccessful) return@withContext false
39+
response.bodyAlias()?.byteStream()?.copyTo(file.outputStream())
40+
Log.i(TAG, "downloadImage success $url")
41+
true
6942
}
70-
})
71-
.placeholder(BitmapDrawable(context.resources, bitmap))
72-
.fitCenter()
73-
.into(imageView)
43+
} catch (e: Exception) {
44+
Log.e(TAG, "downloadImage", e)
45+
false
46+
}
47+
}
48+
}
49+
50+
suspend fun preloadImage(
51+
key: String,
52+
urlList: List<String>,
53+
) {
54+
val file = files[key]
55+
if (file != null) {
56+
Log.i(TAG, "image exists ${file.absolutePath}")
57+
return
58+
}
59+
60+
if (urlList.isEmpty()) {
61+
return
62+
}
63+
64+
for (url in urlList) {
65+
val ext = url.substringAfterLast(".")
66+
val file = File(cacheDir, "$LOGO/$key.$ext")
67+
if (downloadImage(url, file)) {
68+
Log.i(TAG, "image download success ${file.absolutePath}")
69+
break
70+
}
71+
}
72+
}
73+
74+
fun loadImage(
75+
key: String,
76+
imageView: ImageView,
77+
bitmap: Bitmap,
78+
url: String,
79+
) {
80+
val file = files[key]
81+
if (file != null) {
82+
Log.i(TAG, "image exists ${file.absolutePath}")
83+
Glide.with(context)
84+
.load(file)
85+
.fitCenter()
86+
.into(imageView)
87+
return
88+
}
89+
90+
if (url.isEmpty()) {
91+
Glide.with(context)
92+
.load(bitmap)
93+
.fitCenter()
94+
.into(imageView)
95+
} else {
96+
Glide.with(context)
97+
.load(url)
98+
.placeholder(BitmapDrawable(context.resources, bitmap))
99+
.fitCenter()
100+
.into(imageView)
101+
}
102+
}
103+
104+
companion object {
105+
const val TAG = "ImageHelper"
106+
const val LOGO = "logo"
74107
}
75-
}
108+
}

app/src/main/java/com/lizongying/mytv0/InfoFragment.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import androidx.core.view.marginBottom
1414
import androidx.core.view.marginStart
1515
import androidx.core.view.marginTop
1616
import androidx.fragment.app.Fragment
17-
import com.lizongying.mytv0.Utils.getUrls
1817
import com.lizongying.mytv0.databinding.InfoBinding
1918
import com.lizongying.mytv0.models.TVModel
2019

@@ -79,6 +78,8 @@ class InfoFragment : Fragment() {
7978
}
8079

8180
val context = requireContext()
81+
val application = context.applicationContext as MyTVApplication
82+
val imageHelper = application.imageHelper
8283

8384
binding.title.text = tvModel.tv.title
8485

@@ -107,17 +108,12 @@ class InfoFragment : Fragment() {
107108
canvas.drawText(channelNum.toString(), x, y, paint)
108109

109110
val url = tvModel.tv.logo
110-
val name = tvModel.tv.name
111-
var urls =
112-
getUrls(
113-
"live.fanmingming.com/tv/$name.png"
114-
) + getUrls("https://raw.githubusercontent.com/fanmingming/live/main/tv/$name.png")
115-
if (url.isNotEmpty()) {
116-
urls = (getUrls(url) + urls).distinct()
117-
}
118-
loadNextUrl(context, binding.logo, bitmap, urls, 0, handler) {
119-
tvModel.tv.logo = urls[it]
111+
var name = tvModel.tv.name
112+
if (name.isEmpty()) {
113+
name = tvModel.tv.title
120114
}
115+
116+
imageHelper.loadImage(name, binding.logo, bitmap, url)
121117
}
122118
}
123119

app/src/main/java/com/lizongying/mytv0/ListAdapter.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import android.content.Context
44
import android.graphics.Bitmap
55
import android.graphics.Canvas
66
import android.graphics.Paint
7-
import android.os.Handler
8-
import android.os.Looper
97
import android.util.Log
108
import android.view.KeyEvent
119
import android.view.LayoutInflater
@@ -18,7 +16,6 @@ import androidx.core.content.ContextCompat
1816
import androidx.core.view.setPadding
1917
import androidx.recyclerview.widget.LinearLayoutManager
2018
import androidx.recyclerview.widget.RecyclerView
21-
import com.lizongying.mytv0.Utils.getUrls
2219
import com.lizongying.mytv0.databinding.ListItemBinding
2320
import com.lizongying.mytv0.models.TVListModel
2421
import com.lizongying.mytv0.models.TVModel
@@ -187,7 +184,11 @@ class ListAdapter(
187184

188185
viewHolder.bindTitle(tvModel.tv.title)
189186

190-
viewHolder.bindImage(tvModel.tv.logo, tvModel.tv.id, tvModel.tv.name, tvModel)
187+
var name = tvModel.tv.name
188+
if (name.isEmpty()) {
189+
name = tvModel.tv.title
190+
}
191+
viewHolder.bindImage(tvModel.tv.logo, tvModel.tv.id, name, tvModel)
191192
}
192193
}
193194

@@ -196,13 +197,15 @@ class ListAdapter(
196197
class ViewHolder(private val context: Context, val binding: ListItemBinding) :
197198
RecyclerView.ViewHolder(binding.root) {
198199

199-
val handler = Handler(Looper.getMainLooper())
200+
val application = context.applicationContext as MyTVApplication
201+
val imageHelper = application.imageHelper
202+
200203

201204
fun bindTitle(text: String) {
202205
binding.title.text = text
203206
}
204207

205-
fun bindImage(url: String?, id: Int, name: String, tvModel: TVModel) {
208+
fun bindImage(url: String, id: Int, name: String, tvModel: TVModel) {
206209
val width = 300
207210
val height = 180
208211
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
@@ -225,16 +228,7 @@ class ListAdapter(
225228
val y = height / 2f - (paint.descent() + paint.ascent()) / 2
226229
canvas.drawText(channelNum.toString(), x, y, paint)
227230

228-
var urls =
229-
getUrls(
230-
"live.fanmingming.com/tv/$name.png"
231-
) + getUrls("https://raw.githubusercontent.com/fanmingming/live/main/tv/$name.png")
232-
if (!url.isNullOrEmpty()) {
233-
urls = (getUrls(url) + urls).distinct()
234-
}
235-
loadNextUrl(context, binding.icon, bitmap, urls, 0, handler) {
236-
tvModel.tv.logo = urls[it]
237-
}
231+
imageHelper.loadImage(name, binding.icon, bitmap, tvModel.tv.logo)
238232
}
239233

240234
fun focus(hasFocus: Boolean) {

0 commit comments

Comments
 (0)