@@ -3,17 +3,14 @@ package com.blurr.voice
33import android.app.Application
44import android.content.Context
55import android.content.Intent
6- import android.util.Log
6+ import com.blurr.voice.utilities.Logger
77import com.android.billingclient.api.*
88import com.blurr.voice.intents.IntentRegistry
99import com.blurr.voice.intents.impl.DialIntent
1010import com.blurr.voice.intents.impl.EmailComposeIntent
1111import com.blurr.voice.intents.impl.ShareTextIntent
1212import com.blurr.voice.intents.impl.ViewUrlIntent
1313import com.blurr.voice.triggers.TriggerMonitoringService
14- import com.google.firebase.auth.ktx.auth
15- import com.google.firebase.firestore.ktx.firestore
16- import com.google.firebase.ktx.Firebase
1714import kotlinx.coroutines.*
1815import kotlinx.coroutines.flow.MutableStateFlow
1916import kotlinx.coroutines.flow.StateFlow
@@ -65,25 +62,24 @@ class MyApplication : Application(), PurchasesUpdatedListener {
6562
6663 private fun connectToBillingService () {
6764 if (billingClient.isReady) {
68- Log .d(" MyApplication" , " BillingClient is already connected." )
65+ Logger .d(" MyApplication" , " BillingClient is already connected." )
6966 return
7067 }
7168 billingClient.startConnection(object : BillingClientStateListener {
7269 override fun onBillingSetupFinished (billingResult : BillingResult ) {
7370 if (billingResult.responseCode == BillingClient .BillingResponseCode .OK ) {
74- Log .d(" MyApplication" , " BillingClient setup successfully." )
71+ Logger .d(" MyApplication" , " BillingClient setup successfully." )
7572 _isBillingClientReady .value = true
7673 reconnectAttempts = 0
77- queryPurchases()
7874 } else {
79- Log .e(" MyApplication" , " BillingClient setup failed: ${billingResult.debugMessage} " )
75+ Logger .e(" MyApplication" , " BillingClient setup failed: ${billingResult.debugMessage} " )
8076 _isBillingClientReady .value = false
8177 retryConnectionWithBackoff()
8278 }
8379 }
8480
8581 override fun onBillingServiceDisconnected () {
86- Log .w(" MyApplication" , " Billing service disconnected. Retrying..." )
82+ Logger .w(" MyApplication" , " Billing service disconnected. Retrying..." )
8783 _isBillingClientReady .value = false
8884 retryConnectionWithBackoff()
8985 }
@@ -95,89 +91,21 @@ class MyApplication : Application(), PurchasesUpdatedListener {
9591 val delay = initialReconnectDelayMs * (2.0 .pow(reconnectAttempts)).toLong()
9692 applicationScope.launch {
9793 delay(delay)
98- Log .d(" MyApplication" , " Retrying connection, attempt #${reconnectAttempts + 1 } " )
94+ reconnectAttempts++
95+ Logger .d(" MyApplication" , " Retrying connection, attempt #$reconnectAttempts " )
9996 connectToBillingService()
10097 }
101- reconnectAttempts++
10298 } else {
103- Log .e(" MyApplication" , " Max reconnect attempts reached. Will not retry further." )
104- }
105- }
106-
107- private fun queryPurchases () {
108- if (! _isBillingClientReady .value) {
109- Log .e(" MyApplication" , " queryPurchases: BillingClient is not ready" )
110- return
111- }
112- applicationScope.launch {
113- val params = QueryPurchasesParams .newBuilder()
114- .setProductType(BillingClient .ProductType .SUBS )
115- .build()
116- val purchasesResult = billingClient.queryPurchasesAsync(params)
117- val billingResult = purchasesResult.billingResult
118- if (billingResult.responseCode == BillingClient .BillingResponseCode .OK ) {
119- purchasesResult.purchasesList.forEach { purchase ->
120- if (purchase.purchaseState == Purchase .PurchaseState .PURCHASED ) {
121- handlePurchase(purchase)
122- }
123- }
124- } else {
125- Log .e(" MyApplication" , " Failed to query purchases: ${billingResult.debugMessage} " )
126- }
99+ Logger .e(" MyApplication" , " Max reconnect attempts reached. Will not retry further." )
127100 }
128101 }
129102
130103 override fun onPurchasesUpdated (billingResult : BillingResult , purchases : MutableList <Purchase >? ) {
131- if (billingResult.responseCode == BillingClient .BillingResponseCode .OK && purchases != null ) {
132- for (purchase in purchases) {
133- handlePurchase(purchase)
134- }
135- } else if (billingResult.responseCode == BillingClient .BillingResponseCode .USER_CANCELED ) {
136- Log .d(" MyApplication" , " User cancelled the purchase." )
137- } else {
138- Log .e(" MyApplication" , " Purchase error: ${billingResult.debugMessage} " )
139- }
140- }
141-
142- private fun handlePurchase (purchase : Purchase ) {
143- applicationScope.launch(Dispatchers .IO ) {
144- try {
145- if (purchase.purchaseState == Purchase .PurchaseState .PURCHASED ) {
146- if (! purchase.isAcknowledged) {
147- val acknowledgePurchaseParams = AcknowledgePurchaseParams .newBuilder()
148- .setPurchaseToken(purchase.purchaseToken)
149- .build()
150- billingClient.acknowledgePurchase(acknowledgePurchaseParams) { billingResult ->
151- if (billingResult.responseCode == BillingClient .BillingResponseCode .OK ) {
152- Log .d(" MyApplication" , " Purchase acknowledged: ${purchase.orderId} " )
153- updateUserToPro()
154- } else {
155- Log .e(" MyApplication" , " Failed to acknowledge purchase: ${billingResult.debugMessage} " )
156- }
157- }
158- } else {
159- // Purchase already acknowledged, ensure backend is updated.
160- updateUserToPro()
161- }
162- }
163- } catch (e: Exception ) {
164- Log .e(" MyApplication" , " Error handling purchase" , e)
165- }
166- }
167- }
168-
169- private fun updateUserToPro () {
170- val auth = Firebase .auth
171- val db = Firebase .firestore
172- auth.currentUser?.uid?.let { uid ->
173- val userDocRef = db.collection(" users" ).document(uid)
174- userDocRef.update(" plan" , " pro" )
175- .addOnSuccessListener {
176- Log .d(" MyApplication" , " User plan updated to 'pro' in Firestore." )
177- }
178- .addOnFailureListener { e ->
179- Log .e(" MyApplication" , " Failed to update user plan in Firestore." , e)
180- }
181- } ? : Log .e(" MyApplication" , " Cannot update user to pro: current user is null." )
104+ Logger .d(" MyApplication" , " Purchase update received" )
105+ // Send broadcast to MainActivity to handle the purchase update
106+ val intent = Intent (" com.blurr.voice.PURCHASE_UPDATED" )
107+ intent.putExtra(" response_code" , billingResult.responseCode)
108+ intent.putExtra(" debug_message" , billingResult.debugMessage)
109+ appContext.sendBroadcast(intent)
182110 }
183111}
0 commit comments