diff --git a/library/src/main/java/com/billing/BillingService.java b/library/src/main/java/com/billing/BillingService.java deleted file mode 100644 index e4e1d4b..0000000 --- a/library/src/main/java/com/billing/BillingService.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.billing; - -import android.app.Activity; - -import androidx.annotation.CallSuper; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public abstract class BillingService { - - private List purchaseServiceListeners; - private List subscriptionServiceListeners; - - @SuppressWarnings("WeakerAccess") - public BillingService() { - purchaseServiceListeners = new ArrayList<>(); - subscriptionServiceListeners = new ArrayList<>(); - } - - public void addPurchaseListener(PurchaseServiceListener purchaseServiceListener) { - purchaseServiceListeners.add(purchaseServiceListener); - } - - public void removePurchaseListener(PurchaseServiceListener purchaseServiceListener) { - purchaseServiceListeners.remove(purchaseServiceListener); - } - - public void addSubscriptionListener(SubscriptionServiceListener subscriptionServiceListener) { - subscriptionServiceListeners.add(subscriptionServiceListener); - } - - public void removeSubscriptionListener(SubscriptionServiceListener subscriptionServiceListener) { - subscriptionServiceListeners.remove(subscriptionServiceListener); - } - - /** - * @param sku - product specificator - * @param isRestore - a flag indicating whether it's a fresh purchase or restored product - */ - public void productOwned(String sku, boolean isRestore) { - for (PurchaseServiceListener purchaseServiceListener : purchaseServiceListeners) { - if (isRestore) { - purchaseServiceListener.onProductRestored(sku); - } else { - purchaseServiceListener.onProductPurchased(sku); - } - } - } - - /** - * @param sku - subscription specificator - * @param isRestore - a flag indicating whether it's a fresh purchase or restored subscription - */ - public void subscriptionOwned(String sku, boolean isRestore) { - for (SubscriptionServiceListener subscriptionServiceListener : subscriptionServiceListeners) { - if (isRestore) { - subscriptionServiceListener.onSubscriptionRestored(sku); - } else { - subscriptionServiceListener.onSubscriptionPurchased(sku); - } - } - } - - public void updatePrices(Map iapkeyPrices) { - for (BillingServiceListener billingServiceListener : purchaseServiceListeners) { - billingServiceListener.onPricesUpdated(iapkeyPrices); - } - - for (BillingServiceListener billingServiceListener : subscriptionServiceListeners) { - billingServiceListener.onPricesUpdated(iapkeyPrices); - } - } - - public abstract void init(String key); - - public abstract void buy(Activity activity, String sku); - - public abstract void subscribe(Activity activity, String sku); - - public abstract void unsubscribe(Activity activity, String sku); - - public abstract void enableDebugLogging(boolean enable); - - @CallSuper - public void close() { - subscriptionServiceListeners.clear(); - purchaseServiceListeners.clear(); - } -} diff --git a/library/src/main/java/com/billing/BillingService.kt b/library/src/main/java/com/billing/BillingService.kt new file mode 100644 index 0000000..dd181d4 --- /dev/null +++ b/library/src/main/java/com/billing/BillingService.kt @@ -0,0 +1,75 @@ +package com.billing + +import android.app.Activity +import androidx.annotation.CallSuper + +abstract class BillingService { + + private val purchaseServiceListeners: MutableList = mutableListOf() + private val subscriptionServiceListeners: MutableList = mutableListOf() + + fun addPurchaseListener(purchaseServiceListener: PurchaseServiceListener) { + purchaseServiceListeners.add(purchaseServiceListener) + } + + fun removePurchaseListener(purchaseServiceListener: PurchaseServiceListener) { + purchaseServiceListeners.remove(purchaseServiceListener) + } + + fun addSubscriptionListener(subscriptionServiceListener: SubscriptionServiceListener) { + subscriptionServiceListeners.add(subscriptionServiceListener) + } + + fun removeSubscriptionListener(subscriptionServiceListener: SubscriptionServiceListener) { + subscriptionServiceListeners.remove(subscriptionServiceListener) + } + + /** + * @param sku - product specificator + * @param isRestore - a flag indicating whether it's a fresh purchase or restored product + */ + fun productOwned(sku: String?, isRestore: Boolean) { + for (purchaseServiceListener in purchaseServiceListeners) { + if (isRestore) { + purchaseServiceListener.onProductRestored(sku) + } else { + purchaseServiceListener.onProductPurchased(sku) + } + } + } + + /** + * @param sku - subscription specificator + * @param isRestore - a flag indicating whether it's a fresh purchase or restored subscription + */ + fun subscriptionOwned(sku: String, isRestore: Boolean) { + for (subscriptionServiceListener in subscriptionServiceListeners) { + if (isRestore) { + subscriptionServiceListener.onSubscriptionRestored(sku) + } else { + subscriptionServiceListener.onSubscriptionPurchased(sku) + } + } + } + + fun updatePrices(iapkeyPrices: Map) { + for (billingServiceListener in purchaseServiceListeners) { + billingServiceListener.onPricesUpdated(iapkeyPrices) + } + for (billingServiceListener in subscriptionServiceListeners) { + billingServiceListener.onPricesUpdated(iapkeyPrices) + } + } + + abstract fun init(key: String?) + abstract fun buy(activity: Activity, sku: String) + abstract fun subscribe(activity: Activity, sku: String) + abstract fun unsubscribe(activity: Activity, sku: String) + abstract fun enableDebugLogging(enable: Boolean) + + @CallSuper + open fun close() { + subscriptionServiceListeners.clear() + purchaseServiceListeners.clear() + } +} \ No newline at end of file