Skip to content

Commit 29d71f6

Browse files
Add files via upload
1 parent 7b771f6 commit 29d71f6

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

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

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ import com.android.billingclient.api.PurchasesUpdatedListener
6060
import com.android.billingclient.api.QueryProductDetailsParams
6161
import com.android.billingclient.api.QueryPurchasesParams
6262
import com.google.ai.sample.feature.multimodal.PhotoReasoningRoute
63+
import com.google.ai.sample.feature.multimodal.PhotoReasoningViewModel // Added import
6364
import com.google.ai.sample.ui.theme.GenerativeAISample
6465
import kotlinx.coroutines.launch
6566

6667
class MainActivity : ComponentActivity() {
6768

68-
private var photoReasoningViewModel: com.google.ai.sample.feature.multimodal.PhotoReasoningViewModel? = null
69+
private var photoReasoningViewModel: PhotoReasoningViewModel? = null // Corrected type
6970
private lateinit var apiKeyManager: ApiKeyManager
7071
private var showApiKeyDialog by mutableStateOf(false)
7172

@@ -147,7 +148,6 @@ class MainActivity : ComponentActivity() {
147148
}
148149
}
149150

150-
// Corrected: Made public to be accessible from ViewModels and other classes
151151
fun getCurrentApiKey(): String? {
152152
return if (::apiKeyManager.isInitialized) {
153153
apiKeyManager.getCurrentApiKey()
@@ -156,30 +156,22 @@ class MainActivity : ComponentActivity() {
156156
}
157157
}
158158

159-
// Corrected: Made internal to be accessible from other classes in the same module
160159
internal fun checkAccessibilityServiceEnabled(): Boolean {
161-
// Dummy implementation - replace with actual check
162-
Log.d(TAG, "Checking accessibility service (dummy check).")
160+
Log.d(TAG, "Checking accessibility service.")
163161
val service = packageName + "/" + ScreenOperatorAccessibilityService::class.java.canonicalName
164162
val enabledServices = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
165163
val isEnabled = enabledServices?.contains(service, ignoreCase = true) == true
166164
if (!isEnabled) {
167-
Log.d(TAG, "Accessibility Service not enabled. Prompting user.")
168-
// Optionally, prompt user to enable it here or show a persistent message
165+
Log.d(TAG, "Accessibility Service not enabled.")
169166
}
170167
return isEnabled
171168
}
172169

173-
// Corrected: Made internal to be accessible from other classes in the same module
174170
internal fun requestManageExternalStoragePermission() {
175-
// Dummy implementation - replace with actual request if needed for specific Android versions
176171
Log.d(TAG, "Requesting manage external storage permission (dummy).")
177172
}
178173

179-
// Corrected: Changed signature to accept a Boolean for error state
180174
fun updateStatusMessage(message: String, isError: Boolean = false) {
181-
// Displaying as a Toast for now, can be changed to Snackbar or other UI element
182-
// You might want to change the Toast duration or appearance based on isError
183175
Toast.makeText(this, message, if (isError) Toast.LENGTH_LONG else Toast.LENGTH_SHORT).show()
184176
if (isError) {
185177
Log.e(TAG, "Status Message (Error): $message")
@@ -188,19 +180,29 @@ class MainActivity : ComponentActivity() {
188180
}
189181
}
190182

183+
// Added to restore functionality
184+
fun getPhotoReasoningViewModel(): PhotoReasoningViewModel? {
185+
return photoReasoningViewModel
186+
}
187+
188+
// Added to restore functionality
189+
fun setPhotoReasoningViewModel(viewModel: PhotoReasoningViewModel) {
190+
this.photoReasoningViewModel = viewModel
191+
}
192+
191193
override fun onCreate(savedInstanceState: Bundle?) {
192194
super.onCreate(savedInstanceState)
193195
instance = this
194196
Log.d(TAG, "onCreate: Setting MainActivity instance")
195197

196198
apiKeyManager = ApiKeyManager.getInstance(this)
197-
val apiKey = getCurrentApiKey() // Use the corrected public method
199+
val apiKey = getCurrentApiKey()
198200
if (apiKey.isNullOrEmpty()) {
199201
showApiKeyDialog = true
200202
}
201203

202204
checkAndRequestPermissions()
203-
checkAccessibilityServiceEnabled() // Call the corrected internal method
205+
checkAccessibilityServiceEnabled()
204206
setupBillingClient()
205207

206208
TrialManager.initializeTrialStateFlagsIfNecessary(this)
@@ -216,7 +218,6 @@ class MainActivity : ComponentActivity() {
216218
registerReceiver(trialStatusReceiver, intentFilter)
217219
}
218220

219-
// Initial check of trial state without internet time (will likely be INTERNET_UNAVAILABLE or NOT_YET_STARTED)
220221
updateTrialState(TrialManager.getTrialState(this, null))
221222
startTrialServiceIfNeeded()
222223

@@ -240,21 +241,20 @@ class MainActivity : ComponentActivity() {
240241
}
241242
)
242243
}
243-
// Handle different trial states with dialogs
244244
when (currentTrialState) {
245245
TrialManager.TrialState.EXPIRED_INTERNET_TIME_CONFIRMED -> {
246246
TrialExpiredDialog(
247247
onPurchaseClick = { initiateDonationPurchase() },
248-
onDismiss = { /* Persistent dialog, dismiss does nothing or closes app */ }
248+
onDismiss = { /* Persistent dialog */ }
249249
)
250250
}
251251
TrialManager.TrialState.NOT_YET_STARTED_AWAITING_INTERNET,
252252
TrialManager.TrialState.INTERNET_UNAVAILABLE_CANNOT_VERIFY -> {
253-
if (showTrialInfoDialog) { // Show a less intrusive dialog/banner for these states
253+
if (showTrialInfoDialog) {
254254
InfoDialog(message = trialInfoMessage, onDismiss = { showTrialInfoDialog = false })
255255
}
256256
}
257-
else -> { /* ACTIVE or PURCHASED, no special dialog needed here */ }
257+
else -> { /* ACTIVE or PURCHASED */ }
258258
}
259259
}
260260
}
@@ -324,15 +324,14 @@ class MainActivity : ComponentActivity() {
324324
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
325325
Log.d(TAG, "BillingClient setup successful.")
326326
queryProductDetails()
327-
queryActiveSubscriptions() // Check for existing purchases
327+
queryActiveSubscriptions()
328328
} else {
329329
Log.e(TAG, "BillingClient setup failed: ${billingResult.debugMessage}")
330330
}
331331
}
332332

333333
override fun onBillingServiceDisconnected() {
334334
Log.w(TAG, "BillingClient service disconnected.")
335-
// Consider a retry policy
336335
}
337336
})
338337
}
@@ -413,7 +412,7 @@ class MainActivity : ComponentActivity() {
413412
updateTrialState(TrialManager.TrialState.PURCHASED)
414413
val stopIntent = Intent(this, TrialTimerService::class.java)
415414
stopIntent.action = TrialTimerService.ACTION_STOP_TIMER
416-
startService(stopIntent) // Stop the service
415+
startService(stopIntent)
417416
} else {
418417
Log.e(TAG, "Failed to acknowledge purchase: ${ackBillingResult.debugMessage}")
419418
updateStatusMessage("Fehler beim Bestätigen des Kaufs: ${ackBillingResult.debugMessage}", true)
@@ -422,7 +421,7 @@ class MainActivity : ComponentActivity() {
422421
} else {
423422
Log.d(TAG, "Subscription already acknowledged.")
424423
updateStatusMessage("Abonnement bereits aktiv.")
425-
TrialManager.markAsPurchased(this) // Ensure state is correct
424+
TrialManager.markAsPurchased(this)
426425
updateTrialState(TrialManager.TrialState.PURCHASED)
427426
}
428427
}
@@ -441,7 +440,7 @@ class MainActivity : ComponentActivity() {
441440
purchases.forEach { purchase ->
442441
if (purchase.products.contains(subscriptionProductId) && purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
443442
isSubscribed = true
444-
if (!purchase.isAcknowledged) handlePurchase(purchase) // Acknowledge if needed
443+
if (!purchase.isAcknowledged) handlePurchase(purchase)
445444
}
446445
}
447446
if (isSubscribed) {
@@ -450,16 +449,14 @@ class MainActivity : ComponentActivity() {
450449
updateTrialState(TrialManager.TrialState.PURCHASED)
451450
val stopIntent = Intent(this, TrialTimerService::class.java)
452451
stopIntent.action = TrialTimerService.ACTION_STOP_TIMER
453-
startService(stopIntent) // Stop service if already purchased
452+
startService(stopIntent)
454453
} else {
455454
Log.d(TAG, "User has no active subscription. Trial logic will apply.")
456-
// If not purchased, ensure trial state is checked and service started if needed
457-
updateTrialState(TrialManager.getTrialState(this, null)) // Re-check with null initially
455+
updateTrialState(TrialManager.getTrialState(this, null))
458456
startTrialServiceIfNeeded()
459457
}
460458
} else {
461459
Log.e(TAG, "Failed to query active subscriptions: ${billingResult.debugMessage}")
462-
// Fallback: if query fails, still check local trial status and start service
463460
updateTrialState(TrialManager.getTrialState(this, null))
464461
startTrialServiceIfNeeded()
465462
}
@@ -472,9 +469,8 @@ class MainActivity : ComponentActivity() {
472469
Log.d(TAG, "onResume: Setting MainActivity instance")
473470
checkAccessibilityServiceEnabled()
474471
if (::billingClient.isInitialized && billingClient.isReady) {
475-
queryActiveSubscriptions() // This will also trigger trial state updates
472+
queryActiveSubscriptions()
476473
} else {
477-
// If billing client not ready, still update trial state based on local info and start service
478474
updateTrialState(TrialManager.getTrialState(this, null))
479475
startTrialServiceIfNeeded()
480476
}
@@ -498,7 +494,6 @@ class MainActivity : ComponentActivity() {
498494
if (permissionsToRequest.isNotEmpty()) {
499495
requestPermissionLauncher.launch(permissionsToRequest)
500496
} else {
501-
// Permissions already granted, ensure service starts if needed
502497
startTrialServiceIfNeeded()
503498
}
504499
}
@@ -507,7 +502,7 @@ class MainActivity : ComponentActivity() {
507502
arrayOf(
508503
Manifest.permission.READ_MEDIA_IMAGES,
509504
Manifest.permission.READ_MEDIA_VIDEO,
510-
Manifest.permission.POST_NOTIFICATIONS // For foreground service notifications if used
505+
Manifest.permission.POST_NOTIFICATIONS
511506
)
512507
} else {
513508
arrayOf(
@@ -527,7 +522,6 @@ class MainActivity : ComponentActivity() {
527522
} else {
528523
Log.d(TAG, "Some permissions denied")
529524
updateStatusMessage("Einige Berechtigungen wurden verweigert. Die App benötigt diese für volle Funktionalität.", true)
530-
// Handle specific permission denials if necessary
531525
}
532526
}
533527

@@ -543,7 +537,7 @@ class MainActivity : ComponentActivity() {
543537
@Composable
544538
fun TrialExpiredDialog(
545539
onPurchaseClick: () -> Unit,
546-
onDismiss: () -> Unit // Usually, a persistent dialog isn't dismissed by user action other than purchase
540+
onDismiss: () -> Unit
547541
) {
548542
Dialog(onDismissRequest = onDismiss) {
549543
Card(

0 commit comments

Comments
 (0)