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