get time online

This commit is contained in:
Li ZongYing 2024-02-11 10:42:00 +08:00
parent 8e4f7df8d4
commit 809ec920a5
5 changed files with 50 additions and 23 deletions

View File

@ -43,6 +43,10 @@ class InfoFragment : Fragment() {
.load(R.drawable.xinjiang) .load(R.drawable.xinjiang)
.into(binding.infoLogo) .into(binding.infoLogo)
"兵团卫视" -> Glide.with(this)
.load(R.drawable.bingtuan)
.into(binding.infoLogo)
else -> Glide.with(this) else -> Glide.with(this)
.load(tvViewModel.logo.value) .load(tvViewModel.logo.value)
.into(binding.infoLogo) .into(binding.infoLogo)

View File

@ -18,7 +18,12 @@ import android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
import android.view.WindowManager import android.view.WindowManager
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.lizongying.mytv.models.TVViewModel import com.lizongying.mytv.models.TVViewModel
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.security.MessageDigest import java.security.MessageDigest
@ -44,6 +49,16 @@ class MainActivity : FragmentActivity() {
private var versionName = "" private var versionName = ""
init {
lifecycleScope.launch(Dispatchers.IO) {
val utilsJob = async(start = CoroutineStart.LAZY) { Utils.init() }
utilsJob.start()
utilsJob.await()
}
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
Log.i(TAG, "onCreate") Log.i(TAG, "onCreate")
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)

View File

@ -91,8 +91,8 @@ object TVList {
0, 0,
"央视频道", "央视频道",
"https://resources.yangshipin.cn/assets/oms/image/202306/741515efda91f03f455df8a7da4ee11fa9329139c276435cf0a9e2af398d5bf2.png?imageMogr2/format/webp", "https://resources.yangshipin.cn/assets/oms/image/202306/741515efda91f03f455df8a7da4ee11fa9329139c276435cf0a9e2af398d5bf2.png?imageMogr2/format/webp",
"600001802", "600108442",
"2022574301", "2013693901",
"600001802", "600001802",
true, true,
mustToken = true mustToken = true

View File

@ -1,55 +1,63 @@
package com.lizongying.mytv package com.lizongying.mytv
import android.content.res.Resources import android.content.res.Resources
import android.os.SystemClock
import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import com.google.gson.Gson import com.google.gson.Gson
import com.lizongying.mytv.api.TimeResponse import com.lizongying.mytv.api.TimeResponse
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.IOException
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
object Utils { object Utils {
private var between: Long = 0
fun getDateFormat(format: String): String { fun getDateFormat(format: String): String {
return SimpleDateFormat(format, Locale.CHINA).format(Date()) return SimpleDateFormat(
format,
Locale.CHINA
).format(Date(System.currentTimeMillis() - between))
} }
fun getDateTimestamp(): Long { fun getDateTimestamp(): Long {
return Date().time / 1000 return (System.currentTimeMillis() - between) / 1000
} }
suspend fun init() {
init { var currentTimeMillis: Long = 0
CoroutineScope(Dispatchers.Default).launch { try {
updateTimestampFromServer() currentTimeMillis = getTimestampFromServer()
} catch (e: Exception) {
println("Failed to retrieve timestamp from server: ${e.message}")
} }
between = System.currentTimeMillis() - currentTimeMillis
} }
/** /**
* 从服务器获取时间戳 * 从服务器获取时间戳
* @return Long 时间戳 * @return Long 时间戳
*/ */
private suspend fun updateTimestampFromServer() { private suspend fun getTimestampFromServer(): Long {
val currentTimeMillis = withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
val client = okhttp3.OkHttpClient.Builder() val client = okhttp3.OkHttpClient.Builder()
.connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS) .connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS)
.readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build() .readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build()
client.newCall( val request = okhttp3.Request.Builder()
okhttp3.Request.Builder() .url("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp")
.url("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp") .build()
.build() try {
).execute().use { response -> client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") if (!response.isSuccessful) throw IOException("Unexpected code $response")
val string = response.body()?.string() val string = response.body()?.string()
Gson().fromJson(string, TimeResponse::class.java).data.t.toLong() Gson().fromJson(string, TimeResponse::class.java).data.t.toLong()
}
} catch (e: IOException) {
// Handle network errors
throw IOException("Error during network request", e)
} }
} }
SystemClock.setCurrentTimeMillis(currentTimeMillis)
} }
fun dpToPx(dp: Float): Int { fun dpToPx(dp: Float): Int {

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB