Fixed DonationManager crash

This commit is contained in:
Moez Bhatti
2016-06-15 00:54:56 -04:00
parent 11537c7265
commit 60b09134d6
@@ -9,7 +9,6 @@ import android.widget.Toast;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.external.iab.IabHelper; import com.moez.QKSMS.external.iab.IabHelper;
import com.moez.QKSMS.external.iab.IabResult; import com.moez.QKSMS.external.iab.IabResult;
import com.moez.QKSMS.external.iab.Inventory;
import com.moez.QKSMS.external.iab.Purchase; import com.moez.QKSMS.external.iab.Purchase;
import com.moez.QKSMS.ui.base.QKActivity; import com.moez.QKSMS.ui.base.QKActivity;
import com.moez.QKSMS.ui.dialog.QKDialog; import com.moez.QKSMS.ui.dialog.QKDialog;
@@ -37,6 +36,8 @@ public class DonationManager {
public static DonationManager getInstance(QKActivity context) { public static DonationManager getInstance(QKActivity context) {
if (sInstance == null) { if (sInstance == null) {
sInstance = new DonationManager(context); sInstance = new DonationManager(context);
} else {
sInstance.mContext = context; // Update the context, in case the previous context was destroyed
} }
return sInstance; return sInstance;
@@ -56,27 +57,23 @@ public class DonationManager {
// Create the helper, passing it our context and the public key to verify signatures with // Create the helper, passing it our context and the public key to verify signatures with
mHelper = new IabHelper(mContext, PUBLIC_KEY); mHelper = new IabHelper(mContext, PUBLIC_KEY);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { mHelper.startSetup(result -> {
// Have we been disposed of in the meantime? If so, quit.
@Override if (mHelper == null) {
public void onIabSetupFinished(IabResult result) { return;
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) {
return;
}
if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.w(TAG, "Problem setting up in-app billing: " + result.getMessage());
return;
}
// IAB is fully set up.
mBillingServiceReady = true;
// IAB is fully set up. Now, let's get an inventory of stuff we own.
mHelper.queryInventoryAsync(iabInventoryListener());
} }
if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.w(TAG, "Problem setting up in-app billing: " + result.getMessage());
return;
}
// IAB is fully set up.
mBillingServiceReady = true;
// IAB is fully set up. Now, let's get an inventory of stuff we own.
mHelper.queryInventoryAsync(iabInventoryListener());
}); });
} }
@@ -119,47 +116,44 @@ public class DonationManager {
* Listener that's called when we finish querying the items and subscriptions we own * Listener that's called when we finish querying the items and subscriptions we own
*/ */
private IabHelper.QueryInventoryFinishedListener iabInventoryListener() { private IabHelper.QueryInventoryFinishedListener iabInventoryListener() {
return new IabHelper.QueryInventoryFinishedListener() { return (result, inventory) -> {
@Override // Have we been disposed of in the meantime? If so, quit.
public void onQueryInventoryFinished(IabResult result, Inventory inventory) { if (mHelper == null) {
// Have we been disposed of in the meantime? If so, quit. return;
if (mHelper == null) { }
return;
}
// Something went wrong // Something went wrong
if (!result.isSuccess()) { if (!result.isSuccess()) {
return; return;
} }
IabHelper.OnConsumeFinishedListener onConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() { IabHelper.OnConsumeFinishedListener onConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
@Override @Override
public void onConsumeFinished(Purchase purchase, IabResult result) { public void onConsumeFinished(Purchase purchase, IabResult result) {
// if we were disposed of in the meantime, quit. // if we were disposed of in the meantime, quit.
if (mHelper == null) { if (mHelper == null) {
return; return;
}
if (result.isSuccess()) {
String[] thanks = {mRes.getString(R.string.thanks_1), mRes.getString(R.string.thanks_2), mRes.getString(R.string.thanks_3), mRes.getString(R.string.thanks_4)};
Toast.makeText(mContext, thanks[new Random().nextInt(thanks.length)], Toast.LENGTH_LONG).show();
} else {
Log.w(TAG, "Error while consuming: " + result);
}
} }
};
if (inventory.hasPurchase(SKU_DONATE_1)) { if (result.isSuccess()) {
mHelper.consumeAsync(inventory.getPurchase(SKU_DONATE_1), onConsumeFinishedListener); String[] thanks = {mRes.getString(R.string.thanks_1), mRes.getString(R.string.thanks_2), mRes.getString(R.string.thanks_3), mRes.getString(R.string.thanks_4)};
Toast.makeText(mContext, thanks[new Random().nextInt(thanks.length)], Toast.LENGTH_LONG).show();
} else {
Log.w(TAG, "Error while consuming: " + result);
}
} }
};
if (inventory.hasPurchase(SKU_DONATE_5)) { if (inventory.hasPurchase(SKU_DONATE_1)) {
mHelper.consumeAsync(inventory.getPurchase(SKU_DONATE_5), onConsumeFinishedListener); mHelper.consumeAsync(inventory.getPurchase(SKU_DONATE_1), onConsumeFinishedListener);
} }
if (inventory.hasPurchase(SKU_DONATE_10)) { if (inventory.hasPurchase(SKU_DONATE_5)) {
mHelper.consumeAsync(inventory.getPurchase(SKU_DONATE_10), onConsumeFinishedListener); mHelper.consumeAsync(inventory.getPurchase(SKU_DONATE_5), onConsumeFinishedListener);
} }
if (inventory.hasPurchase(SKU_DONATE_10)) {
mHelper.consumeAsync(inventory.getPurchase(SKU_DONATE_10), onConsumeFinishedListener);
} }
}; };
} }
@@ -180,23 +174,20 @@ public class DonationManager {
public void showDonateDialog() { public void showDonateDialog() {
View.OnClickListener clickListener = new View.OnClickListener() { View.OnClickListener clickListener = view -> {
@Override switch (view.getId()) {
public void onClick(View view) { case R.id.donate_1:
switch (view.getId()) { onDonateButtonClicked(SKU_DONATE_1);
case R.id.donate_1: break;
onDonateButtonClicked(SKU_DONATE_1); case R.id.donate_5:
break; onDonateButtonClicked(SKU_DONATE_5);
case R.id.donate_5: break;
onDonateButtonClicked(SKU_DONATE_5); case R.id.donate_10:
break; onDonateButtonClicked(SKU_DONATE_10);
case R.id.donate_10: break;
onDonateButtonClicked(SKU_DONATE_10); case R.id.donate_paypal:
break; donatePaypal();
case R.id.donate_paypal: break;
donatePaypal();
break;
}
} }
}; };