Skip to content

Commit 93e861f

Browse files
Add files via upload
1 parent 0500643 commit 93e861f

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/TrialTimerService.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class TrialTimerService : Service() {
3535
const val EXTRA_CURRENT_UTC_TIME_MS = "extra_current_utc_time_ms"
3636
private const val TAG = "TrialTimerService"
3737
private const val CHECK_INTERVAL_MS = 60 * 1000L // 1 minute
38-
private const val TIME_API_URL = "http://worldclockapi.com/api/json/utc/now" // Changed API URL
38+
// Changed API URL to timeapi.io for UTC time
39+
private const val TIME_API_URL = "https://timeapi.io/api/time/current/zone?timeZone=Etc/UTC"
3940
private const val CONNECTION_TIMEOUT_MS = 15000 // 15 seconds
4041
private const val READ_TIMEOUT_MS = 15000 // 15 seconds
4142
private const val MAX_RETRIES = 3
@@ -105,8 +106,9 @@ class TrialTimerService : Service() {
105106
Log.d(TAG, "startTimerLogic: Connection disconnected.")
106107

107108
val jsonObject = JSONObject(result)
108-
val currentDateTimeStr = jsonObject.getString("currentDateTime")
109-
Log.d(TAG, "startTimerLogic: Parsed currentDateTime string: $currentDateTimeStr")
109+
// Updated to parse "dateTime" field from timeapi.io
110+
val currentDateTimeStr = jsonObject.getString("dateTime")
111+
Log.d(TAG, "startTimerLogic: Parsed dateTime string: $currentDateTimeStr")
110112
// Parse ISO 8601 string to milliseconds since epoch
111113
val currentUtcTimeMs = OffsetDateTime.parse(currentDateTimeStr).toInstant().toEpochMilli()
112114

@@ -154,7 +156,8 @@ class TrialTimerService : Service() {
154156
} catch (e: SocketTimeoutException) {
155157
Log.e(TAG, "Failed to fetch internet time: Socket Timeout after $CONNECTION_TIMEOUT_MS ms (connect) or $READ_TIMEOUT_MS ms (read). Attempt ${attempt + 1}", e)
156158
} catch (e: MalformedURLException) {
157-
Log.e(TAG, "Failed to fetch internet time: Malformed URL '$TIME_API_URL'. Stopping timer logic.", e)
159+
Log.e(TAG, "Failed to fetch internet time: Malformed URL neğinTIME_API_URL
160+
. Stopping timer logic.", e)
158161
stopTimerLogic() // URL is wrong, no point in retrying
159162
return@launch
160163
} catch (e: IOException) {

0 commit comments

Comments
 (0)