Skip to content

Commit

Permalink
Close 13; Call Billing library listeners on the UI thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Dibrivnyi committed May 30, 2020
1 parent c2a7b13 commit d6adca2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
24 changes: 24 additions & 0 deletions library/src/main/java/com/billing/BillingService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.billing

import android.app.Activity
import android.os.Handler
import android.os.Looper
import androidx.annotation.CallSuper

abstract class BillingService {
Expand Down Expand Up @@ -29,6 +31,12 @@ abstract class BillingService {
* @param isRestore - a flag indicating whether it's a fresh purchase or restored product
*/
fun productOwned(sku: String?, isRestore: Boolean) {
findUiHandler().post {
productOwnedInternal(sku, isRestore)
}
}

fun productOwnedInternal(sku: String?, isRestore: Boolean) {
for (purchaseServiceListener in purchaseServiceListeners) {
if (isRestore) {
purchaseServiceListener.onProductRestored(sku)
Expand All @@ -43,6 +51,12 @@ abstract class BillingService {
* @param isRestore - a flag indicating whether it's a fresh purchase or restored subscription
*/
fun subscriptionOwned(sku: String, isRestore: Boolean) {
findUiHandler().post {
subscriptionOwnedInternal(sku, isRestore)
}
}

fun subscriptionOwnedInternal(sku: String, isRestore: Boolean) {
for (subscriptionServiceListener in subscriptionServiceListeners) {
if (isRestore) {
subscriptionServiceListener.onSubscriptionRestored(sku)
Expand All @@ -53,6 +67,12 @@ abstract class BillingService {
}

fun updatePrices(iapkeyPrices: Map<String, String>) {
findUiHandler().post {
updatePricesInternal(iapkeyPrices)
}
}

fun updatePricesInternal(iapkeyPrices: Map<String, String>) {
for (billingServiceListener in purchaseServiceListeners) {
billingServiceListener.onPricesUpdated(iapkeyPrices)
}
Expand All @@ -72,4 +92,8 @@ abstract class BillingService {
subscriptionServiceListeners.clear()
purchaseServiceListeners.clear()
}
}

fun findUiHandler(): Handler {
return Handler(Looper.getMainLooper())
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void onProductOwnedEvent_eachRegisteredListenerShouldBeTriggered() throws
PurchaseServiceListener secondListener = Mockito.spy(PurchaseServiceListener.class);
IAPManager.addPurchaseListener(secondListener);

IAPManager.getBillingService().productOwned(TestConstants.TEST_SKU, false);
IAPManager.getBillingService().productOwnedInternal(TestConstants.TEST_SKU, false);

Mockito.verify(firstListener, Mockito.times(1)).onProductPurchased(TestConstants.TEST_SKU);
Mockito.verify(secondListener, Mockito.times(1)).onProductPurchased(TestConstants.TEST_SKU);
Expand All @@ -51,7 +51,7 @@ public void onProductDetailsFetched_eachRegisteredListenerShouldBeTriggered() {
products.put(TestConstants.TEST_SKU, TestConstants.TEST_PRODUCT);
products.put(TestConstants.TEST_SKU_1, TestConstants.TEST_PRODUCT);

IAPManager.getBillingService().updatePrices(products);
IAPManager.getBillingService().updatePricesInternal(products);

Mockito.verify(firstListener, Mockito.times(1)).onPricesUpdated(products);
Mockito.verify(secondListener, Mockito.times(1)).onPricesUpdated(products);
Expand All @@ -69,7 +69,7 @@ public void onIapManagerInteraction_onlyRegisteredListenersShouldBeTriggered() {

IAPManager.removePurchaseListener(firstListener);

IAPManager.getBillingService().productOwned(TestConstants.TEST_SKU, false);
IAPManager.getBillingService().productOwnedInternal(TestConstants.TEST_SKU, false);

Mockito.verify(firstListener, Mockito.never()).onProductPurchased(Mockito.anyString());
Mockito.verify(secondListener, Mockito.times(1)).onProductPurchased(TestConstants.TEST_SKU);
Expand All @@ -83,7 +83,7 @@ public void onProductPurchase_purchaseCallbackShouldBeTriggered() {
PurchaseServiceListener listener = Mockito.spy(PurchaseServiceListener.class);
IAPManager.addPurchaseListener(listener);

IAPManager.getBillingService().productOwned(TestConstants.TEST_SKU, false);
IAPManager.getBillingService().productOwnedInternal(TestConstants.TEST_SKU, false);

Mockito.verify(listener, Mockito.times(1)).onProductPurchased(TestConstants.TEST_SKU);
}
Expand All @@ -95,7 +95,7 @@ public void onProductRestore_restoreCallbackShouldBeTriggered() {
PurchaseServiceListener listener = Mockito.spy(PurchaseServiceListener.class);
IAPManager.addPurchaseListener(listener);

IAPManager.getBillingService().productOwned(TestConstants.TEST_SKU, true);
IAPManager.getBillingService().productOwnedInternal(TestConstants.TEST_SKU, true);

Mockito.verify(listener, Mockito.times(1)).onProductRestored(TestConstants.TEST_SKU);
}
Expand All @@ -107,7 +107,7 @@ public void onSubscriptionPurchase_purchaseCallbackShouldBeTriggered() {
SubscriptionServiceListener listener = Mockito.spy(SubscriptionServiceListener.class);
IAPManager.addSubscriptionListener(listener);

IAPManager.getBillingService().subscriptionOwned(TestConstants.TEST_SKU, false);
IAPManager.getBillingService().subscriptionOwnedInternal(TestConstants.TEST_SKU, false);

Mockito.verify(listener, Mockito.times(1)).onSubscriptionPurchased(TestConstants.TEST_SKU);
}
Expand All @@ -119,7 +119,7 @@ public void onSubscriptionRestore_restoreCallbackShouldBeTriggered() {
SubscriptionServiceListener listener = Mockito.spy(SubscriptionServiceListener.class);
IAPManager.addSubscriptionListener(listener);

IAPManager.getBillingService().subscriptionOwned(TestConstants.TEST_SKU, true);
IAPManager.getBillingService().subscriptionOwnedInternal(TestConstants.TEST_SKU, true);

Mockito.verify(listener, Mockito.times(1)).onSubscriptionRestored(TestConstants.TEST_SKU);
}
Expand Down

0 comments on commit d6adca2

Please sign in to comment.