|
@@ -0,0 +1,115 @@
|
|
|
+package com.ys.imageProcess.access
|
|
|
+
|
|
|
+import android.content.ContentValues.TAG
|
|
|
+import android.util.Log
|
|
|
+import okhttp3.*
|
|
|
+import org.json.JSONObject
|
|
|
+import java.io.IOException
|
|
|
+import java.time.OffsetDateTime
|
|
|
+import java.time.format.DateTimeFormatter
|
|
|
+import java.time.Duration
|
|
|
+
|
|
|
+object UsagePermission {
|
|
|
+ val client = OkHttpClient()
|
|
|
+
|
|
|
+ private const val START_VALID_DATE_STR: String =
|
|
|
+ "2024-09-24T00:00:00.000000+08:00"
|
|
|
+ private const val END_VALID_DATE_STR: String =
|
|
|
+ "2024-12-24T00:00:00.000000+08:00"
|
|
|
+ private const val NOW_DATE_STR: String =
|
|
|
+ "2024-10-25T00:00:00.000000+08:00"
|
|
|
+
|
|
|
+ // 时间服务器的 URL
|
|
|
+ private const val TIME_SERVER_URL = "https://worldtimeapi.org/api/ip"
|
|
|
+
|
|
|
+ // 异步回调接口
|
|
|
+ interface TimeCallback {
|
|
|
+ fun onSuccess(dateTime: OffsetDateTime)
|
|
|
+ fun onFailure(e: IOException)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun checkDatePermissionLocal(): Boolean {
|
|
|
+ val formatter =
|
|
|
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME
|
|
|
+ val start: OffsetDateTime =
|
|
|
+ OffsetDateTime.parse(START_VALID_DATE_STR, formatter)
|
|
|
+ val end: OffsetDateTime =
|
|
|
+ OffsetDateTime.parse(END_VALID_DATE_STR, formatter)
|
|
|
+ val now: OffsetDateTime = OffsetDateTime.now()
|
|
|
+// now = OffsetDateTime.parse(NOW_DATE_STR, formatter)
|
|
|
+
|
|
|
+ val startToNow: Duration = Duration.between(start, now)
|
|
|
+ val nowToEnd: Duration = Duration.between(now, end)
|
|
|
+ val valid = !(startToNow.isNegative || nowToEnd.isNegative)
|
|
|
+ Log.i(TAG, "passed: $startToNow, left: $nowToEnd, valid:$valid")
|
|
|
+
|
|
|
+ return valid
|
|
|
+ }
|
|
|
+
|
|
|
+ fun checkDatePermissionOnline(valid: ((Boolean) -> Unit)? = null) {
|
|
|
+ fetchNetworkTime(object : TimeCallback {
|
|
|
+ val formatter =
|
|
|
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME
|
|
|
+ val validDate: OffsetDateTime =
|
|
|
+ OffsetDateTime.parse(END_VALID_DATE_STR, formatter)
|
|
|
+
|
|
|
+ override fun onSuccess(dateTime: OffsetDateTime) {
|
|
|
+ val timeDiff: Duration = Duration.between(dateTime, validDate)
|
|
|
+ Log.i(
|
|
|
+ TAG,
|
|
|
+ "Network time: $dateTime, valid time: $validDate"
|
|
|
+ )
|
|
|
+ Log.i(TAG, "valid seconds: ${timeDiff.seconds}")
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFailure(e: IOException) {
|
|
|
+ Log.i(TAG, "Failed to fetch network time: ${e.message}")
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取网络时间
|
|
|
+ private fun fetchNetworkTime(callback: TimeCallback) {
|
|
|
+ val request = Request.Builder()
|
|
|
+ .url(TIME_SERVER_URL)
|
|
|
+ .build()
|
|
|
+
|
|
|
+ client.newCall(request).enqueue(object : Callback {
|
|
|
+ override fun onFailure(call: Call, e: IOException) {
|
|
|
+ callback.onFailure(e)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onResponse(call: Call, response: Response) {
|
|
|
+ if (response.isSuccessful) {
|
|
|
+ val responseBody = response.body?.string()
|
|
|
+ if (responseBody != null) {
|
|
|
+ try {
|
|
|
+ // 解析 JSON 响应
|
|
|
+ val json = JSONObject(responseBody)
|
|
|
+ val dateTimeString = json.getString("datetime")
|
|
|
+ // 将时间字符串转换为 Date 对象
|
|
|
+ val formatter =
|
|
|
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME
|
|
|
+ // 解析时间字符串为 OffsetDateTime 对象
|
|
|
+ val offsetDateTime: OffsetDateTime =
|
|
|
+ OffsetDateTime.parse(dateTimeString, formatter)
|
|
|
+ // 回调成功
|
|
|
+ callback.onSuccess(offsetDateTime)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ callback.onFailure(
|
|
|
+ IOException(
|
|
|
+ "Failed to parse date and time",
|
|
|
+ e
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ callback.onFailure(IOException("Empty response body"))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ callback.onFailure(IOException("Unexpected code ${response.code}"))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|