com.android.billingclient.api.BillingClientStateListener Java Examples

The following examples show how to use com.android.billingclient.api.BillingClientStateListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BillingManager.java    From play-billing-codelab with Apache License 2.0 11 votes vote down vote up
public BillingManager(Activity activity) {
    mActivity = activity;
    mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
            if (billingResponse == BillingClient.BillingResponse.OK) {
                Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
            } else {
                Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
            }
        }
        @Override
        public void onBillingServiceDisconnected() {
            Log.w(TAG, "onBillingServiceDisconnected()");
        }
    });
}
 
Example #2
Source File: PreferencesBillingHelper.java    From CommonUtils with Apache License 2.0 6 votes vote down vote up
public void onStart(@NonNull Activity activity) {
    billingClient = BillingClient.newBuilder(activity).enablePendingPurchases().setListener(new InternalListener()).build();
    billingClient.startConnection(new BillingClientStateListener() {
        private boolean retried = false;

        @Override
        public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
            if (billingResult.getResponseCode() == BillingResponseCode.OK) {
                synchronized (billingReady) {
                    billingReady.notifyAll();
                }
            } else {
                handleBillingErrors(billingResult.getResponseCode());
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            if (!retried) {
                retried = true;
                billingClient.startConnection(this);
            } else {
                listener.showToast(Toaster.build().message(R.string.failedBillingConnection));
            }
        }
    });
}
 
Example #3
Source File: MainActivity.java    From FCM-for-Mojo with GNU General Public License v3.0 6 votes vote down vote up
private void showDonateGooglePlay() {
    mBillingClient = BillingClient.newBuilder(this).setListener(this).build();

    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
            if (billingResponseCode == BillingResponse.OK) {
                // The billing client is ready. You can query purchases here.
                BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                        .setSku("donate_2")
                        .setType(SkuType.INAPP)
                        .build();
                mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    });
}
 
Example #4
Source File: MainActivity.java    From scroball with MIT License 6 votes vote down vote up
@Override
protected void onResume() {
  super.onResume();
  billingClient = new BillingClient.Builder(this).setListener(this).build();
  billingClient.startConnection(
      new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
          if (billingResponseCode == BillingResponse.OK) {
            Purchase.PurchasesResult purchasesResult =
                billingClient.queryPurchases(BillingClient.SkuType.INAPP);
            onPurchasesUpdated(
                purchasesResult.getResponseCode(), purchasesResult.getPurchasesList());
          }
        }

        @Override
        public void onBillingServiceDisconnected() {}
      });
}
 
Example #5
Source File: BillingManager.java    From UpdogFarmer with GNU General Public License v3.0 6 votes vote down vote up
private void startServiceConnection(final Runnable executeOnSuccess) {
    billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(int responseCode) {
            Log.i(TAG, "Billing setup finished. Response code: " + responseCode);

            if (responseCode == BillingClient.BillingResponse.OK) {
                serviceConnected = true;
                if (executeOnSuccess != null) {
                    executeOnSuccess.run();
                }
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            Log.i(TAG, "Billing service disconnected");
            serviceConnected = false;
        }
    });
}
 
Example #6
Source File: BillingManager.java    From play-billing-codelab with Apache License 2.0 5 votes vote down vote up
/**
 * Trying to restart service connection.
 * <p>Note: It's just a primitive example - it's up to you to develop a real retry-policy.</p>
 * @param executeOnSuccess This runnable will be executed once the connection to the Billing
 *                         service is restored.
 */
private void startServiceConnection(final Runnable executeOnSuccess) {
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingResponse int billingResponse) {
            Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
        }
        @Override
        public void onBillingServiceDisconnected() {
            Log.w(TAG, "onBillingServiceDisconnected()");
        }
    });
}
 
Example #7
Source File: GoogleBillingHelper.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Launch the purchase flow for a product. If there is no connection, then establish connection before.
 *
 * @param activity   The triggering activity.
 * @param skuDetails The details of the product to be purchased.
 * @param listener   a listener called after the purchase has been completed.
 */
public void launchPurchaseFlow(final Activity activity, final SkuDetails skuDetails, final OnPurchaseSuccessListener listener) {
	if (mIsConnected) {
		doLaunchPurchaseFlow(activity, skuDetails, listener);
	}
	else {
		mBillingClient.startConnection(new BillingClientStateListener() {
			@Override
			public void onBillingSetupFinished(final BillingResult billingResult) {
				if (billingResult.getResponseCode() == BillingResponseCode.OK) {
					Log.d(TAG, "Google Billing Connection established.");
					mIsConnected = true;
					doLaunchPurchaseFlow(activity, skuDetails, listener);
				}
				else {
					Log.i(TAG, "Google Billing Connection failed - " + billingResult.getDebugMessage());
					mIsConnected = false;
				}
			}

			@Override
			public void onBillingServiceDisconnected() {
				Log.d(TAG, "Google Billing Connection lost.");
				mIsConnected = false;
			}
		});
	}
}
 
Example #8
Source File: GoogleBillingHelper.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Query SKU details. If no connection available, establish connection.
 *
 * @param listener The listener called when the query is finished.
 */
public void querySkuDetails(final OnInventoryFinishedListener listener) {
	if (mIsConnected) {
		doQuerySkuDetails(listener);
	}
	else {
		mBillingClient.startConnection(new BillingClientStateListener() {
			@Override
			public void onBillingSetupFinished(final BillingResult billingResult) {
				if (billingResult.getResponseCode() == BillingResponseCode.OK) {
					Log.d(TAG, "Google Billing Connection established.");
					mIsConnected = true;
					doQuerySkuDetails(listener);
				}
				else {
					Log.i(TAG, "Google Billing Connection failed - " + billingResult.getDebugMessage());
					mIsConnected = false;
				}
			}

			@Override
			public void onBillingServiceDisconnected() {
				Log.d(TAG, "Google Billing Connection lost.");
				mIsConnected = false;
			}
		});
	}
}
 
Example #9
Source File: GoogleIap.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
private void startServiceConnection() {
    if (mIsServiceConnecting) {
        return;
    }

    mIsServiceConnecting = true;
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@NotNull BillingResult billingResult) {

            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                mIsServiceConnected = true;
                for (Runnable runnable : mRequests) {
                    getExecutor().execute(runnable);
                }
            } else {
                EventCollector.logException("google play billing:" + billingResult.getDebugMessage());
            }
            mIsServiceConnecting = false;
        }

        @Override
        public void onBillingServiceDisconnected() {
            mIsServiceConnected = false;
            mIsServiceConnecting = false;
        }
    });
}
 
Example #10
Source File: BillingManager.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
private void startServiceConnectionIfNeeded(final Runnable executeOnSuccess) {
    //PPApplication.logE(TAG, "startServiceConnectionIfNeeded");
    if (mBillingClient.isReady()) {
        if (executeOnSuccess != null) {
            executeOnSuccess.run();
        }
    } else {
        DonationFragment fragment = getFragment();
        if (fragment != null)
            fragment.setWaitScreen(true);

        mBillingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    //Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);

                    if (executeOnSuccess == null) {
                        if (getFragment() != null)
                            getFragment().updateGUIAfterBillingConnected();
                    }

                    if (executeOnSuccess != null) {
                        executeOnSuccess.run();
                    }
                }/* else {
                    Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
                }*/
            }

            @Override
            public void onBillingServiceDisconnected() {
                //Log.w(TAG, "onBillingServiceDisconnected()");
            }
        });
    }
}
 
Example #11
Source File: PictureInPictureUpgradeActivity.java    From dtube-mobile-unofficial with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pipm_upgrade_activity);

    billingClient = BillingClient.newBuilder(this).setListener(this).enablePendingPurchases().build();
    billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(BillingResult billingResult) {
            if (billingResult.getResponseCode() ==  BillingClient.BillingResponseCode.OK) {
                Log.d("bill", "onBillingSetupFinished");
                loadSKUs();

                for (Purchase p: billingClient.queryPurchases("inapp" ).getPurchasesList()) {
                    Log.d("billp", "purchases: " + p.getPurchaseToken());
                    //already purchased
                    PreferenceManager.getDefaultSharedPreferences(PictureInPictureUpgradeActivity.this)
                            .edit().putBoolean("upgraded",true).apply();
                    Preferences.hasUpgrade = true;
                }
            }
        }
        @Override
        public void onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
            Log.d("bill", "onBillingServiceDisconnected");
        }
    });
}
 
Example #12
Source File: BillingManager.java    From play-billing-codelab with Apache License 2.0 5 votes vote down vote up
public BillingManager(Activity activity) {
    mActivity = activity;
    mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingResponse int billingResponse) {
            Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
        }
        @Override
        public void onBillingServiceDisconnected() {
            Log.w(TAG, "onBillingServiceDisconnected()");
        }
    });
}
 
Example #13
Source File: PlayBillingWrapper.java    From android-browser-helper with Apache License 2.0 5 votes vote down vote up
@Override
public void connect() {
    mClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(BillingResult billingResult) {
            mListener.onConnected();
        }

        @Override
        public void onBillingServiceDisconnected() {
            mListener.onDisconnected();
        }
    });
}
 
Example #14
Source File: BillingManager.java    From play-billing-codelab with Apache License 2.0 5 votes vote down vote up
public BillingManager(Activity activity) {
    mActivity = activity;
    mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingResponse int billingResponse) {
            Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
        }
        @Override
        public void onBillingServiceDisconnected() {
            Log.w(TAG, "onBillingServiceDisconnected()");
        }
    });
}
 
Example #15
Source File: BillingManager.java    From play-billing-codelab with Apache License 2.0 5 votes vote down vote up
/**
 * Trying to restart service connection if it's needed or just execute a request.
 * <p>Note: It's just a primitive example - it's up to you to implement a real retry-policy.</p>
 * @param executeOnSuccess This runnable will be executed once the connection to the Billing
 *                         service is restored.
 */
private void startServiceConnectionIfNeeded(final Runnable executeOnSuccess) {
    if (mBillingClient.isReady()) {
        if (executeOnSuccess != null) {
            executeOnSuccess.run();
        }
    } else {
        mBillingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(@BillingResponse int billingResponse) {
                if (billingResponse == BillingResponse.OK) {
                    Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
                    if (executeOnSuccess != null) {
                        executeOnSuccess.run();
                    }
                } else {
                    Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                Log.w(TAG, "onBillingServiceDisconnected()");
            }
        });
    }
}
 
Example #16
Source File: BillingPlugin.java    From flutter_billing with Apache License 2.0 5 votes vote down vote up
private void startServiceConnection() {
    if (billingServiceStatus != BillingServiceStatus.IDLE) return;
    billingServiceStatus = BillingServiceStatus.STARTING;
    billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
            Log.d(TAG, "Billing service was setup with code " + billingResponseCode);

            billingServiceStatus = billingResponseCode == BillingResponse.OK ? BillingServiceStatus.READY : BillingServiceStatus.IDLE;
            Request request;

            while ((request = pendingRequests.poll()) != null) {
                if (billingServiceStatus == BillingServiceStatus.READY) {
                    request.execute();
                } else {
                    request.failed();
                }
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            Log.d(TAG, "Billing service was disconnected!");
            billingServiceStatus = BillingServiceStatus.IDLE;
        }
    });
}
 
Example #17
Source File: BillingManager.java    From SchoolQuest with GNU General Public License v3.0 5 votes vote down vote up
private void startServiceConnection(final Runnable runnable) {
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
            if (billingResponse == BillingClient.BillingResponse.OK) {
                isServiceConnected = true;
                if (runnable != null) {
                    runnable.run();
                }
                queryPurchases();
                Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
            } else {
                Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
            }
        }
        @Override
        public void onBillingServiceDisconnected() {
            isServiceConnected = false;
            Log.w(TAG, "onBillingServiceDisconnected()");
        }
    });
}