From 8535689e95a5b7ba4b6c619cc22fceec9fa8cfa8 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:41:51 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=93=E9=97=A8?= =?UTF-8?q?=E7=9A=84ChannelUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/lizongying/mytv/ChannelUtils.kt | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 app/src/main/java/com/lizongying/mytv/ChannelUtils.kt diff --git a/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt b/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt new file mode 100644 index 0000000..b87fd2a --- /dev/null +++ b/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt @@ -0,0 +1,154 @@ +package com.lizongying.mytv + +import android.content.Context +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.io.File + +/** + *@author LeGend + *@date 2024/2/4 22:42 + */ +object ChannelUtils { + /** + * 获取服务器channel版本 + * + * @param context Context + * + * @return 服务器channel版本 + */ + suspend fun getServerVersion(context: Context): Int { + return withContext(Dispatchers.IO) { + val client = okhttp3.OkHttpClient() + val request = okhttp3.Request.Builder().url(getServerVersionUrl(context)).build() + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") + val body = response.body() + body?.string()?.toInt() ?: 0 + } + } + } + + /** + * 获取服务器channel + * + * @param url String 服务器地址 + * + * @return Array 服务器channel + * + * @throws java.io.IOException 网络请求失败 + */ + suspend fun getServerChannel(url: String): List { + val result = withContext(Dispatchers.IO) { + val client = okhttp3.OkHttpClient() + val request = okhttp3.Request.Builder().url(url).build() + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") + val body = response.body() + body?.string() ?: "" + } + } + return withContext(Dispatchers.Default) { + val type = object : com.google.gson.reflect.TypeToken>() {}.type + com.google.gson.Gson().fromJson(result, type) + } + } + + /** + * 获取服务器地址 + * + * @param context Context + * + * @return 服务器地址 + */ + fun getServerUrl(context: Context): String { + return context.resources.getString(R.string.server_url) + } + + /** + * 获取serverVersion的URL + * + * @param context Context + * + * @return serverVersionURL 服务器版本地址 + */ + suspend fun getServerVersionUrl(context: Context): String { + return withContext(Dispatchers.IO) { + context.resources.getString(R.string.server_version_url) + } + } + + + /** + * 获取本地channel版本 + * + * @param context Context + * + * @return 本地channel + */ + suspend fun getLocalVersion(context: Context): Int { + return withContext(Dispatchers.IO) { + val file = File(getAppDirectory(context), "channels") + val savedVersion = + context.getSharedPreferences("saved_version", Context.MODE_PRIVATE).getInt("version", Integer.MIN_VALUE) + if (!file.exists() || savedVersion == Integer.MIN_VALUE) { + context.resources.getInteger(R.integer.local_channel_version) + } else { + savedVersion + } + } + } + + /** + * 获取本地可读取的目录 + * @param context Context + * + * @return 可读取的目录 + */ + private fun getAppDirectory(context: Context): File { + return context.filesDir + } + + /** + * 获取本地channel + * + * @param context Context + * + * @return Array 本地channel + */ + suspend fun getLocalChannel(context: Context): List { + var str = withContext(Dispatchers.IO) { + if (File(getAppDirectory(context), "channels").exists()) { + File(getAppDirectory(context), "channels").readText() + } else { + context.resources.openRawResource(R.raw.channels).bufferedReader().use { it.readText() } + } + } + return withContext(Dispatchers.Default) { + val type = object : com.google.gson.reflect.TypeToken>() {}.type + com.google.gson.Gson().fromJson(str, type) + } + } + + /** + * 更新channels.json + * + * @param context Context + * + * @return 无 + * + * @throws java.io.IOException 写入失败 + */ + suspend fun updateLocalChannel(context: Context, version: Int, channels: List) { + withContext(Dispatchers.IO) { + val file = File(getAppDirectory(context), "channels") + if (!file.exists()) { + file.createNewFile() + } + file.writeText(com.google.gson.Gson().toJson(channels)) + context.getSharedPreferences("saved_version", Context.MODE_PRIVATE).edit().putInt( + "version", version + ).apply() + } + } +} From a93cfb455c942415ecddff8de2fc578acd6bed70 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:46:57 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E5=9C=A8=E5=BA=94=E7=94=A8=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E6=97=B6=E5=88=9D=E5=A7=8B=E5=8C=96TVlist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/lizongying/mytv/MainActivity.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index 4c9837f..7ecd0fd 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -46,6 +46,7 @@ class MainActivity : FragmentActivity() { override fun onCreate(savedInstanceState: Bundle?) { Log.i(TAG, "onCreate") + TVList.init(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) From 54b43664ec12ff750ddeae488c1a0990bf5be783 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:47:10 +0800 Subject: [PATCH 03/14] =?UTF-8?q?add:=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/lizongying/mytv/ChannelUtils.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt b/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt index b87fd2a..1d70396 100644 --- a/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt +++ b/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt @@ -89,6 +89,7 @@ object ChannelUtils { suspend fun getLocalVersion(context: Context): Int { return withContext(Dispatchers.IO) { val file = File(getAppDirectory(context), "channels") + //检查本地是否已经有保存的channels.json,若无保存的Channel.json则从读取assert中文件 val savedVersion = context.getSharedPreferences("saved_version", Context.MODE_PRIVATE).getInt("version", Integer.MIN_VALUE) if (!file.exists() || savedVersion == Integer.MIN_VALUE) { From 825a872a09034d2111fb38a283a00848a8a0953b Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:47:54 +0800 Subject: [PATCH 04/14] =?UTF-8?q?Fix:=E7=A7=BB=E9=99=A4TVList=E7=9A=84?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/lizongying/mytv/MainFragment.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/MainFragment.kt b/app/src/main/java/com/lizongying/mytv/MainFragment.kt index b65a57f..208fa69 100644 --- a/app/src/main/java/com/lizongying/mytv/MainFragment.kt +++ b/app/src/main/java/com/lizongying/mytv/MainFragment.kt @@ -139,8 +139,7 @@ class MainFragment : BrowseSupportFragment() { val cardPresenter = CardPresenter(viewLifecycleOwner) var idx: Long = 0 - TVList.init(requireContext()) - for ((k, v) in TVList.list) { + for ((k, v) in TVList.list!!) { val listRowAdapter = ArrayObjectAdapter(cardPresenter) for ((idx2, v1) in v.withIndex()) { val tvViewModel = TVViewModel(v1) From 700c8b4c042cd138494bd3d1774f8ca84fb0e507 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:48:03 +0800 Subject: [PATCH 05/14] =?UTF-8?q?Fix:=E7=A7=BB=E9=99=A4TVList=E7=9A=84?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/lizongying/mytv/Request.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/Request.kt b/app/src/main/java/com/lizongying/mytv/Request.kt index e37aef6..2f073f6 100644 --- a/app/src/main/java/com/lizongying/mytv/Request.kt +++ b/app/src/main/java/com/lizongying/mytv/Request.kt @@ -95,8 +95,6 @@ class Request { fun initYSP(context: Context) { ysp = YSP(context) - //TODO 不确定在哪里初始化 - TVList.init(context) } var call: Call? = null @@ -369,7 +367,7 @@ class Request { continue } val tv = - TVList.list[channelType]?.find { it.title == mapping[item.channelName] } + TVList.list?.get(channelType)?.find { it.title == mapping[item.channelName] } if (tv != null) { tv.logo = item.tvLogo tv.pid = item.pid From c778ac07badaf570b9803e3e1fc116877d237ec9 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:48:39 +0800 Subject: [PATCH 06/14] =?UTF-8?q?Add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=87=E4=BB=B6=E7=89=88=E6=9C=AC=EF=BC=8C=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E7=89=88=E6=9C=AC=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/res/values/server.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/res/values/server.xml b/app/src/main/res/values/server.xml index 394661b..2e5b0c3 100644 --- a/app/src/main/res/values/server.xml +++ b/app/src/main/res/values/server.xml @@ -1,4 +1,6 @@ + 1 + http://localhost:8080 http://localhost:8080 \ No newline at end of file From aa3ba1d41f91ce5046290547d9cc6c6a204b3266 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:48:58 +0800 Subject: [PATCH 07/14] =?UTF-8?q?Fix:=E7=A7=BB=E9=99=A4ChannelUtils?= =?UTF-8?q?=E6=9C=89=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/lizongying/mytv/Utils.kt | 48 ------------------- 1 file changed, 48 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/Utils.kt b/app/src/main/java/com/lizongying/mytv/Utils.kt index 8f56a45..d1c53a0 100644 --- a/app/src/main/java/com/lizongying/mytv/Utils.kt +++ b/app/src/main/java/com/lizongying/mytv/Utils.kt @@ -1,12 +1,9 @@ package com.lizongying.mytv -import android.content.Context import android.content.res.Resources import android.util.TypedValue -import java.io.File import java.text.SimpleDateFormat import java.util.* -import java.io.IOException object Utils { fun getDateFormat(format: String): String { @@ -28,49 +25,4 @@ object Utils { TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), Resources.getSystem().displayMetrics ).toInt() } - - /** - * 获取可读写的目录 - * - * @param context 应用环境信息 - * - * @return 可读写的目录 - * - */ - fun getAppDirectory(context: Context): File { - return context.filesDir - } - - /** - * 更新channels.json - * - * @param context 应用环境信息 - * - * @return 无 - * - * @throws IOException 网络请求失败 - */ - fun updateChannel(context: Context) { - val client = okhttp3.OkHttpClient() - val request = okhttp3.Request.Builder().url(getServerUrl(context)).build() - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) throw IOException("Unexpected code $response") - val body = response.body() - //覆盖channels.json - val file = File(getAppDirectory(context), "channels.json") - if (!file.exists()) { - file.createNewFile() - } - file.writeText(body!!.string()) - } - } - - /** - * 从res/values/server.xml获取服务器地址 - * @param context 应用环境信息 - * @return 服务器地址 - */ - fun getServerUrl(context: Context): String { - return context.resources.getString(R.string.server_url) - } } \ No newline at end of file From cd8b37e63cbe2dd7eed41aa7cd5239dbe0ce9ac1 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:49:19 +0800 Subject: [PATCH 08/14] =?UTF-8?q?Fix:=E4=BC=98=E5=8C=96=E4=BB=8E=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/lizongying/mytv/TVList.kt | 101 +++++++++++------- 1 file changed, 63 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/TVList.kt b/app/src/main/java/com/lizongying/mytv/TVList.kt index 75ed3db..dc6dba9 100644 --- a/app/src/main/java/com/lizongying/mytv/TVList.kt +++ b/app/src/main/java/com/lizongying/mytv/TVList.kt @@ -1,54 +1,79 @@ package com.lizongying.mytv import android.content.Context -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken -import java.io.File +import android.util.Log +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import java.io.IOException object TVList { - lateinit var list: Map> - - fun init(context: Context){ - if(::list.isInitialized){ - return - } - synchronized(this){ - if(::list.isInitialized){ - return + @Volatile + var list: Map>? = null + get():Map>? { + //等待初始化完成 + while (this.list === null) { + Thread.sleep(10) } - list = setupTV(context) + return this.list } - } - private fun setupTV(context:Context): Map> { - val map: MutableMap> = mutableMapOf() - val appDirectory = Utils.getAppDirectory(context) + /** + * 初始化 + * + * @param context Context + */ + fun init(context: Context) { + CoroutineScope(Dispatchers.Default).launch { + //获取本地版本号 + val localVersion = ChannelUtils.getLocalVersion(context) + //获取服务器版本号 + val serverVersion = try { + ChannelUtils.getServerVersion(context) + } catch (e: IOException) { + Log.e("TVList", "无法从服务器获取版本信息", e) + Integer.MIN_VALUE + } + //频道列表 + val channelTVMap: MutableMap> = mutableMapOf() + //是否从服务器更新 + var updateFromServer = false + //获取频道列表 + val tvList: List = if (localVersion < serverVersion) { + //获取服务器地址 + val url = ChannelUtils.getServerUrl(context) + //是否从服务器更新 + updateFromServer = true - //检查当前目录下是否存在channels.json - var file = File(appDirectory, "channels.json") - if (!file.exists()) { - //不存在则从assets中拷贝 - file = File(appDirectory, "channels.json") - file.createNewFile() - context.assets.open("channels.json").use { input -> - file.outputStream().use { output -> - input.copyTo(output) + try { + ChannelUtils.getServerChannel(url) + } catch (e: IOException) { + Log.e("TVList", "无法从服务器获取频道信息", e) + updateFromServer = false + ChannelUtils.getLocalChannel(context) + } + } else { + //获取本地频道 + ChannelUtils.getLocalChannel(context) + } + //按频道分类 + for (tv in tvList) { + val key = tv.channel + if (channelTVMap.containsKey(key)) { + val list = channelTVMap[key]!! + list.add(tv) + channelTVMap[key] = list + } else { + channelTVMap[key] = mutableListOf(tv) } } - } - - //读取channels.json,并转换为Map> - val json = file.readText() - //防止类型擦除 - val type = object : TypeToken>() {}.type - Gson().fromJson>(json, type).forEach { - if (map.containsKey(it.channel)) { - map[it.channel]?.add(it) - } else { - map[it.channel] = mutableListOf(it) + //保存频道列表 + list = channelTVMap + //保存版本号 + if (updateFromServer) { + ChannelUtils.updateLocalChannel(context, serverVersion, tvList) } } - return map } } \ No newline at end of file From a746fe6c656246f343f67b8698e703ec9654e4ab Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 23:58:11 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=20Add:=E6=B7=BB=E5=8A=A0log=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/lizongying/mytv/TVList.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/lizongying/mytv/TVList.kt b/app/src/main/java/com/lizongying/mytv/TVList.kt index dc6dba9..39e131d 100644 --- a/app/src/main/java/com/lizongying/mytv/TVList.kt +++ b/app/src/main/java/com/lizongying/mytv/TVList.kt @@ -6,6 +6,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import java.io.IOException +import kotlin.math.log object TVList { @Volatile @@ -45,7 +46,7 @@ object TVList { val url = ChannelUtils.getServerUrl(context) //是否从服务器更新 updateFromServer = true - + Log.i("TVList", "从服务器获取频道信息") try { ChannelUtils.getServerChannel(url) } catch (e: IOException) { @@ -54,6 +55,7 @@ object TVList { ChannelUtils.getLocalChannel(context) } } else { + Log.i("TVList", "从本地获取频道信息") //获取本地频道 ChannelUtils.getLocalChannel(context) } From fd1dc8d3c7359159f47d16c985324580dc0b06c6 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Mon, 5 Feb 2024 13:21:44 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=20Fix:=E6=9B=B4=E6=94=B9=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E5=9C=B0=E5=9D=80=E4=B8=BAGithub=E5=9C=B0?= =?UTF-8?q?=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/res/values/server.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values/server.xml b/app/src/main/res/values/server.xml index 2e5b0c3..16407dc 100644 --- a/app/src/main/res/values/server.xml +++ b/app/src/main/res/values/server.xml @@ -1,6 +1,6 @@ 1 - http://localhost:8080 - http://localhost:8080 + https://raw.githubusercontent.com/LeGend-wLw/my-tv-json-utils/main/version.txt + https://github.com/LeGend-wLw/my-tv-json-utils/raw/main/channels.json \ No newline at end of file From 36eacb7a57047d8f2858366abc73fd0a2176d88b Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Mon, 5 Feb 2024 14:14:37 +0800 Subject: [PATCH 11/14] =?UTF-8?q?Fix:=E6=B7=BB=E5=8A=A01s=E8=B6=85?= =?UTF-8?q?=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/lizongying/mytv/ChannelUtils.kt | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt b/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt index 1d70396..0408317 100644 --- a/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt +++ b/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt @@ -19,13 +19,14 @@ object ChannelUtils { */ suspend fun getServerVersion(context: Context): Int { return withContext(Dispatchers.IO) { - val client = okhttp3.OkHttpClient() - val request = okhttp3.Request.Builder().url(getServerVersionUrl(context)).build() - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") - val body = response.body() - body?.string()?.toInt() ?: 0 - } + val client = okhttp3.OkHttpClient.Builder().connectTimeout(1, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build() + client.newCall(okhttp3.Request.Builder().url(getServerVersionUrl(context)).build()).execute() + .use { response -> + if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") + val body = response.body() + body?.string()?.toInt() ?: 0 + } } } @@ -40,7 +41,8 @@ object ChannelUtils { */ suspend fun getServerChannel(url: String): List { val result = withContext(Dispatchers.IO) { - val client = okhttp3.OkHttpClient() + val client = okhttp3.OkHttpClient.Builder().connectTimeout(1, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build() val request = okhttp3.Request.Builder().url(url).build() client.newCall(request).execute().use { response -> if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") @@ -49,7 +51,7 @@ object ChannelUtils { } } return withContext(Dispatchers.Default) { - val type = object : com.google.gson.reflect.TypeToken>() {}.type + val type = object : com.google.gson.reflect.TypeToken>() {}.type com.google.gson.Gson().fromJson(result, type) } } From 807300918c1fd3bcd082143bf410e5f25b1d0732 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Mon, 5 Feb 2024 14:15:52 +0800 Subject: [PATCH 12/14] =?UTF-8?q?Fix:=E7=BD=91=E7=BB=9C=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?500ms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/lizongying/mytv/ChannelUtils.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt b/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt index 0408317..2767416 100644 --- a/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt +++ b/app/src/main/java/com/lizongying/mytv/ChannelUtils.kt @@ -19,7 +19,7 @@ object ChannelUtils { */ suspend fun getServerVersion(context: Context): Int { return withContext(Dispatchers.IO) { - val client = okhttp3.OkHttpClient.Builder().connectTimeout(1, java.util.concurrent.TimeUnit.SECONDS) + val client = okhttp3.OkHttpClient.Builder().connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS) .readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build() client.newCall(okhttp3.Request.Builder().url(getServerVersionUrl(context)).build()).execute() .use { response -> @@ -41,7 +41,7 @@ object ChannelUtils { */ suspend fun getServerChannel(url: String): List { val result = withContext(Dispatchers.IO) { - val client = okhttp3.OkHttpClient.Builder().connectTimeout(1, java.util.concurrent.TimeUnit.SECONDS) + val client = okhttp3.OkHttpClient.Builder().connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS) .readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build() val request = okhttp3.Request.Builder().url(url).build() client.newCall(request).execute().use { response -> @@ -120,7 +120,7 @@ object ChannelUtils { * @return Array 本地channel */ suspend fun getLocalChannel(context: Context): List { - var str = withContext(Dispatchers.IO) { + val str = withContext(Dispatchers.IO) { if (File(getAppDirectory(context), "channels").exists()) { File(getAppDirectory(context), "channels").readText() } else { From 6387f4840910968dea22bbd8d0531410c11343b0 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Mon, 5 Feb 2024 14:24:46 +0800 Subject: [PATCH 13/14] =?UTF-8?q?Add:=E6=B7=98=E5=AE=9D=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E8=BF=94=E5=9B=9E=E6=97=B6=E9=97=B4=E6=88=B3=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/lizongying/mytv/api/Info.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/src/main/java/com/lizongying/mytv/api/Info.kt b/app/src/main/java/com/lizongying/mytv/api/Info.kt index 7708ac8..867acaa 100644 --- a/app/src/main/java/com/lizongying/mytv/api/Info.kt +++ b/app/src/main/java/com/lizongying/mytv/api/Info.kt @@ -9,3 +9,11 @@ data class Info( data class InfoData( val token: String, ) + +data class TimeResponse( + val api: String, val v: String, val ret: List, val data: Time +) { + data class Time( + val t: String + ) +} \ No newline at end of file From d8342e8c6a4119931a3831e4a306698f8049a557 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Mon, 5 Feb 2024 14:25:18 +0800 Subject: [PATCH 14/14] =?UTF-8?q?Add:=E4=BB=8E=E6=B7=98=E5=AE=9D=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D=E6=97=B6?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/lizongying/mytv/Utils.kt | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/Utils.kt b/app/src/main/java/com/lizongying/mytv/Utils.kt index d1c53a0..206a407 100644 --- a/app/src/main/java/com/lizongying/mytv/Utils.kt +++ b/app/src/main/java/com/lizongying/mytv/Utils.kt @@ -2,6 +2,10 @@ package com.lizongying.mytv import android.content.res.Resources import android.util.TypedValue +import com.google.gson.Gson +import com.lizongying.mytv.api.TimeResponse +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.text.SimpleDateFormat import java.util.* @@ -10,8 +14,28 @@ object Utils { return SimpleDateFormat(format, Locale.CHINA).format(Date()) } - fun getDateTimestamp(): Long { - return Date().time / 1000 + suspend fun getDateTimestamp(): Long { + return getTimestampFromServer() / 1000 + } + + /** + * 从服务器获取时间戳 + * @return Long 时间戳 + */ + private suspend fun getTimestampFromServer(): Long { + return withContext(Dispatchers.IO) { + val client = okhttp3.OkHttpClient.Builder().connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS) + .readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build() + client.newCall( + okhttp3.Request.Builder().url("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp") + .build() + ).execute().use { response -> + if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") + val body = response.body() + val string = body?.toString() + Gson().fromJson(string, TimeResponse::class.java).data.t.toLong() + } + } } fun dpToPx(dp: Float): Int {