Search in sources :

Example 1 with PurchasesUpdatedListener

use of com.android.billingclient.api.PurchasesUpdatedListener in project sms-backup-plus by jberkel.

the class DonationActivity method checkUserDonationStatus.

public static void checkUserDonationStatus(Context context, final DonationStatusListener listener) {
    final BillingClient helper = BillingClient.newBuilder(context).enablePendingPurchases().setListener(new PurchasesUpdatedListener() {

        @Override
        public void onPurchasesUpdated(BillingResult result, @Nullable List<Purchase> purchases) {
            log("onPurchasesUpdated(" + result + ", " + purchases + ")");
        }
    }).build();
    helper.startConnection(new BillingClientStateListener() {

        @Override
        public void onBillingSetupFinished(BillingResult result) {
            log("checkUserHasDonated: onBillingSetupFinished(" + result + ")");
            try {
                if (result.getResponseCode() == OK) {
                    PurchasesResult purchasesResult = helper.queryPurchases(INAPP);
                    if (result.getResponseCode() == OK) {
                        listener.userDonationState(userHasDonated(purchasesResult.getPurchasesList()) ? DONATED : NOT_DONATED);
                    } else {
                        listener.userDonationState(UNKNOWN);
                    }
                } else {
                    listener.userDonationState(result.getResponseCode() == BILLING_UNAVAILABLE ? NOT_AVAILABLE : UNKNOWN);
                }
            } finally {
                try {
                    helper.endConnection();
                } catch (Exception ignored) {
                }
            }
        }

        public void onBillingServiceDisconnected() {
        }
    });
}
Also used : BillingClientStateListener(com.android.billingclient.api.BillingClientStateListener) PurchasesResult(com.android.billingclient.api.Purchase.PurchasesResult) PurchasesUpdatedListener(com.android.billingclient.api.PurchasesUpdatedListener) BillingResult(com.android.billingclient.api.BillingResult) ArrayList(java.util.ArrayList) List(java.util.List) BillingClient(com.android.billingclient.api.BillingClient) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)1 BillingClient (com.android.billingclient.api.BillingClient)1 BillingClientStateListener (com.android.billingclient.api.BillingClientStateListener)1 BillingResult (com.android.billingclient.api.BillingResult)1 PurchasesResult (com.android.billingclient.api.Purchase.PurchasesResult)1 PurchasesUpdatedListener (com.android.billingclient.api.PurchasesUpdatedListener)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1