From a4c95efd71b8032775c97ff09e765272438cc4c8 Mon Sep 17 00:00:00 2001 From: LeGend-wLw <874644990@qq.com> Date: Sun, 4 Feb 2024 16:37:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=8E=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E4=B8=8A=E8=8E=B7=E5=8F=96channel.json=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E4=BD=86=E6=9C=AA=E5=90=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/lizongying/mytv/Utils.kt | 59 ++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/Utils.kt b/app/src/main/java/com/lizongying/mytv/Utils.kt index dddda49..8f56a45 100644 --- a/app/src/main/java/com/lizongying/mytv/Utils.kt +++ b/app/src/main/java/com/lizongying/mytv/Utils.kt @@ -1,10 +1,12 @@ 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.Date -import java.util.Locale +import java.util.* +import java.io.IOException object Utils { fun getDateFormat(format: String): String { @@ -17,17 +19,58 @@ object Utils { fun dpToPx(dp: Float): Int { return TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - dp, - Resources.getSystem().displayMetrics + TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().displayMetrics ).toInt() } fun dpToPx(dp: Int): Int { return TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - dp.toFloat(), - Resources.getSystem().displayMetrics + 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