Skip to content

Commit 107d750

Browse files
Add files via upload
1 parent b788ac1 commit 107d750

File tree

1 file changed

+40
-55
lines changed

1 file changed

+40
-55
lines changed

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

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object TrialManager {
2121
enum class TrialState {
2222
NOT_YET_STARTED_AWAITING_INTERNET,
2323
ACTIVE_INTERNET_TIME_CONFIRMED,
24-
EXPIRED_INTERNET_TIME_CONFIRMED,
24+
EXPIRED_INTERNET_TIME_CONFIRMED, // Dieser Status wird nun auch bei Ablauf durch lokale Zeit gesetzt
2525
PURCHASED,
2626
INTERNET_UNAVAILABLE_CANNOT_VERIFY
2727
}
@@ -81,86 +81,74 @@ object TrialManager {
8181
}
8282
}
8383

84-
fun getTrialState(context: Context, currentUtcTimeMs: Long?): TrialState {
85-
Log.d(TAG, "getTrialState called with currentUtcTimeMs: $currentUtcTimeMs")
86-
val prefs = getSharedPreferences(context) // Get prefs instance early
84+
fun getTrialState(context: Context, currentUtcTimeMsFromInternet: Long?): TrialState {
85+
Log.d(TAG, "getTrialState called with currentUtcTimeMsFromInternet: $currentUtcTimeMsFromInternet")
86+
val prefs = getSharedPreferences(context)
8787

88-
if (isPurchased(context)) { // isPurchased uses its own prefs instance, which is fine
88+
if (isPurchased(context)) {
8989
Log.d(TAG, "getTrialState: App is purchased. Returning TrialState.PURCHASED")
9090
return TrialState.PURCHASED
9191
}
9292

9393
val isAwaitingFirstInternetTime = prefs.getBoolean(KEY_TRIAL_AWAITING_FIRST_INTERNET_TIME, true)
94-
val trialUtcEndTime = getTrialUtcEndTime(context) // getTrialUtcEndTime uses its own prefs instance
94+
val trialUtcEndTime = getTrialUtcEndTime(context) // Kann null sein, wenn nicht gestartet oder Fehler
9595
val confirmedExpired = prefs.getBoolean(KEY_TRIAL_CONFIRMED_EXPIRED, false)
96-
Log.d(TAG, "getTrialState: isAwaitingFirstInternetTime: $isAwaitingFirstInternetTime, trialUtcEndTime: $trialUtcEndTime, confirmedExpired: $confirmedExpired")
96+
val currentLocalUtcTimeMs = System.currentTimeMillis() // Aktuelle lokale Zeit als UTC Millisekunden
97+
98+
Log.d(TAG, "getTrialState Details: isAwaitingFirstInternetTime: $isAwaitingFirstInternetTime, trialUtcEndTime: $trialUtcEndTime, confirmedExpired: $confirmedExpired, currentLocalUtcTimeMs: $currentLocalUtcTimeMs, currentUtcTimeMsFromInternet: $currentUtcTimeMsFromInternet")
9799

98100
if (confirmedExpired) {
99-
Log.d(TAG, "getTrialState: Trial previously confirmed expired (flag was true). Returning EXPIRED_INTERNET_TIME_CONFIRMED.")
101+
Log.d(TAG, "getTrialState: Trial previously confirmed expired (flag KEY_TRIAL_CONFIRMED_EXPIRED was true). Returning EXPIRED_INTERNET_TIME_CONFIRMED.")
100102
return TrialState.EXPIRED_INTERNET_TIME_CONFIRMED
101103
}
102104

103-
if (currentUtcTimeMs == null) {
104-
Log.d(TAG, "getTrialState: currentUtcTimeMs is null.")
105-
// If confirmedExpired was false, and currentUtcTimeMs is null, we cannot confirm expiry.
106-
// We rely on isAwaitingFirstInternetTime and trialUtcEndTime to determine initial/pending states.
107-
return if (trialUtcEndTime == null && isAwaitingFirstInternetTime) {
108-
Log.d(TAG, "getTrialState: Returning NOT_YET_STARTED_AWAITING_INTERNET (no end time, awaiting internet, not confirmed expired)")
105+
// Fall 1: Die Test-Endzeit wurde noch nicht per Internetzeit festgelegt.
106+
if (trialUtcEndTime == null) {
107+
return if (isAwaitingFirstInternetTime) {
108+
Log.d(TAG, "getTrialState: No trialUtcEndTime set, and awaiting first internet time. Returning NOT_YET_STARTED_AWAITING_INTERNET.")
109109
TrialState.NOT_YET_STARTED_AWAITING_INTERNET
110110
} else {
111-
// This path means either trial has started (endTime exists) or it's not awaiting first internet,
112-
// but we don't have current time to check. Or, endTime is null but we are not awaiting (inconsistent state).
113-
Log.d(TAG, "getTrialState: Returning INTERNET_UNAVAILABLE_CANNOT_VERIFY (end time might exist or not awaiting, but no current time, not confirmed expired)")
111+
// Inkonsistenter Zustand: Sollte nicht passieren, wenn startTrialIfNecessaryWithInternetTime korrekt funktioniert.
112+
// Bedeutet, KEY_TRIAL_AWAITING_FIRST_INTERNET_TIME ist false, aber es gibt keine Endzeit.
113+
Log.e(TAG, "getTrialState: Inconsistent state - trialUtcEndTime is null, but KEY_TRIAL_AWAITING_FIRST_INTERNET_TIME is false. Returning INTERNET_UNAVAILABLE_CANNOT_VERIFY.")
114114
TrialState.INTERNET_UNAVAILABLE_CANNOT_VERIFY
115115
}
116116
}
117117

118-
// currentUtcTimeMs is NOT null from this point onwards
119-
Log.d(TAG, "getTrialState: currentUtcTimeMs is $currentUtcTimeMs. Evaluating state based on time.")
120-
return when {
121-
trialUtcEndTime == null && isAwaitingFirstInternetTime -> {
122-
// This case implies startTrialIfNecessaryWithInternetTime hasn't run yet with a valid internet time.
123-
// Since currentUtcTimeMs is available now, TrialTimerService should call startTrialIfNecessaryWithInternetTime.
124-
// For now, we report it as NOT_YET_STARTED.
125-
Log.d(TAG, "getTrialState: Case 1: trialUtcEndTime is null AND isAwaitingFirstInternetTime is true. Returning NOT_YET_STARTED_AWAITING_INTERNET")
126-
TrialState.NOT_YET_STARTED_AWAITING_INTERNET
127-
}
128-
trialUtcEndTime == null && !isAwaitingFirstInternetTime -> {
129-
// This is an inconsistent state: trial supposedly started (not awaiting), but no end time.
130-
// This might happen if saveTrialUtcEndTime failed or was cleared erroneously.
131-
Log.e(TAG, "CRITICAL INCONSISTENCY: Trial marked as started (not awaiting internet), but no trial end time found. Check save/load logic. Returning INTERNET_UNAVAILABLE_CANNOT_VERIFY.")
132-
// Cannot confirm active or expired without an end time.
133-
TrialState.INTERNET_UNAVAILABLE_CANNOT_VERIFY
134-
}
135-
trialUtcEndTime != null && currentUtcTimeMs < trialUtcEndTime -> {
136-
Log.d(TAG, "getTrialState: Case 2: trialUtcEndTime ($trialUtcEndTime) > currentUtcTimeMs ($currentUtcTimeMs). Returning ACTIVE_INTERNET_TIME_CONFIRMED")
137-
TrialState.ACTIVE_INTERNET_TIME_CONFIRMED
138-
}
139-
trialUtcEndTime != null && currentUtcTimeMs >= trialUtcEndTime -> {
140-
Log.i(TAG, "getTrialState: Case 3: trialUtcEndTime ($trialUtcEndTime) <= currentUtcTimeMs ($currentUtcTimeMs). Trial EXPIRED. Setting KEY_TRIAL_CONFIRMED_EXPIRED=true.")
141-
// --- MODIFICATION START ---
142-
// Persist that the trial has been confirmed expired with internet time.
118+
// Ab hier ist trialUtcEndTime NICHT null, d.h. der Trial wurde initial gestartet.
119+
120+
// Fall 2: Prüfung auf Ablauf, wenn Internetzeit verfügbar ist.
121+
if (currentUtcTimeMsFromInternet != null) {
122+
if (currentUtcTimeMsFromInternet >= trialUtcEndTime || currentLocalUtcTimeMs >= trialUtcEndTime) {
123+
Log.i(TAG, "getTrialState: Trial EXPIRED. InternetTime: $currentUtcTimeMsFromInternet, LocalUTCTime: $currentLocalUtcTimeMs, EndTime: $trialUtcEndTime. Setting KEY_TRIAL_CONFIRMED_EXPIRED=true.")
143124
prefs.edit().putBoolean(KEY_TRIAL_CONFIRMED_EXPIRED, true).apply()
144-
// --- MODIFICATION END ---
145-
TrialState.EXPIRED_INTERNET_TIME_CONFIRMED
125+
return TrialState.EXPIRED_INTERNET_TIME_CONFIRMED
126+
} else {
127+
Log.d(TAG, "getTrialState: Trial ACTIVE (confirmed by available internet time). Returning ACTIVE_INTERNET_TIME_CONFIRMED.")
128+
return TrialState.ACTIVE_INTERNET_TIME_CONFIRMED
146129
}
147-
else -> {
148-
// Fallback for any unhandled scenarios, though ideally all paths should be covered.
149-
Log.e(TAG, "Unhandled case in getTrialState. isAwaiting: $isAwaitingFirstInternetTime, endTime: $trialUtcEndTime, currentTime: $currentUtcTimeMs. Defaulting to NOT_YET_STARTED_AWAITING_INTERNET.")
150-
TrialState.NOT_YET_STARTED_AWAITING_INTERNET
130+
} else {
131+
// Fall 3: Internetzeit ist NICHT verfügbar. Prüfung auf Ablauf nur mit lokaler UTC-Zeit.
132+
if (currentLocalUtcTimeMs >= trialUtcEndTime) {
133+
Log.i(TAG, "getTrialState: Trial EXPIRED (based on local UTC time, no internet time). LocalUTCTime: $currentLocalUtcTimeMs, EndTime: $trialUtcEndTime. Setting KEY_TRIAL_CONFIRMED_EXPIRED=true.")
134+
prefs.edit().putBoolean(KEY_TRIAL_CONFIRMED_EXPIRED, true).apply()
135+
return TrialState.EXPIRED_INTERNET_TIME_CONFIRMED
136+
} else {
137+
// Nicht abgelaufen gemäß lokaler Zeit, aber Internetzeit fehlt zur Bestätigung des "Aktiv"-Status.
138+
Log.d(TAG, "getTrialState: Trial NOT YET EXPIRED by local UTC time, but internet time is unavailable. Returning INTERNET_UNAVAILABLE_CANNOT_VERIFY.")
139+
return TrialState.INTERNET_UNAVAILABLE_CANNOT_VERIFY
151140
}
152141
}
153142
}
154143

144+
155145
fun markAsPurchased(context: Context) {
156146
Log.d(TAG, "markAsPurchased called")
157147
val editor = getSharedPreferences(context).edit()
158148
Log.d(TAG, "Removing trial-related keys: KEY_TRIAL_END_TIME_UNENCRYPTED, KEY_TRIAL_AWAITING_FIRST_INTERNET_TIME, KEY_TRIAL_CONFIRMED_EXPIRED")
159149
editor.remove(KEY_TRIAL_END_TIME_UNENCRYPTED)
160150
editor.remove(KEY_TRIAL_AWAITING_FIRST_INTERNET_TIME)
161-
// --- MODIFICATION START ---
162-
editor.remove(KEY_TRIAL_CONFIRMED_EXPIRED) // Ensure this flag is cleared on purchase
163-
// --- MODIFICATION END ---
151+
editor.remove(KEY_TRIAL_CONFIRMED_EXPIRED) // Sicherstellen, dass dieser Flag bei Kauf entfernt wird
164152
Log.d(TAG, "Setting KEY_PURCHASED_FLAG to true")
165153
editor.putBoolean(KEY_PURCHASED_FLAG, true)
166154
editor.apply()
@@ -180,8 +168,6 @@ object TrialManager {
180168
val awaitingFlagExists = prefs.contains(KEY_TRIAL_AWAITING_FIRST_INTERNET_TIME)
181169
val purchasedFlagExists = prefs.contains(KEY_PURCHASED_FLAG)
182170
val endTimeExists = prefs.contains(KEY_TRIAL_END_TIME_UNENCRYPTED)
183-
// We don't need to check for KEY_TRIAL_CONFIRMED_EXPIRED here, as it's set only after expiry.
184-
// Its absence is normal for a new or active trial.
185171
Log.d(TAG, "Checking for existing flags: awaitingFlagExists=$awaitingFlagExists, purchasedFlagExists=$purchasedFlagExists, endTimeExists=$endTimeExists")
186172

187173
if (!awaitingFlagExists && !purchasedFlagExists && !endTimeExists) {
@@ -192,5 +178,4 @@ object TrialManager {
192178
Log.d(TAG, "One or more core trial-related flags already exist. No initialization needed for KEY_TRIAL_AWAITING_FIRST_INTERNET_TIME.")
193179
}
194180
}
195-
}
196-
181+
}

0 commit comments

Comments
 (0)