Skip to content

Commit

Permalink
master - Fix tcsPurchase Bugs
Browse files Browse the repository at this point in the history
This commit fixes bugs surrounding tcsPurchase in the Andorid
InAppBillingImplementation class.

1. HandleActivityResult uses null-conditional operator to access
tcsPurcahse to avoid NullReferenceException.
2. HandleActivityResult now invokes TrySetException rather than
SetException to avoid InvalidOperationException.
3. ConnectAsync() now invokes TryCancel() and sets tcsPurcahse to null.
This was done to prevent tcsPurcahse from being stuck in a
WaitingForActivation TaskStatus state when the activity is destroyed in
recreated while the purchase modal is being presented to the user. This
was causing all future purchase attempts to return null when invoking
PurchaseAsync().
  • Loading branch information
masterwok committed Jul 9, 2019
1 parent 6d8d340 commit a22f2cf
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions src/Plugin.InAppBilling.Android/InAppBillingImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ async Task<Purchase> PurchaseAsync(string productSku, string itemType, string pa
public override Task<bool> ConnectAsync(ItemType itemType = ItemType.InAppPurchase)
{
serviceConnection = new InAppBillingServiceConnection(Context, itemType);
tcsPurchase?.TrySetCanceled();
tcsPurchase = null;
return serviceConnection.ConnectAsync();
}

Expand Down Expand Up @@ -522,38 +524,38 @@ public static void HandleActivityResult(int requestCode, Result resultCode, Inte

switch (responseCode)
{
case 0:
//Reponse returned OK
var purchaseData = data.GetStringExtra(RESPONSE_IAP_DATA);
var dataSignature = data.GetStringExtra(RESPONSE_IAP_DATA_SIGNATURE);

tcsPurchase?.TrySetResult(new PurchaseResponse
{
PurchaseData = purchaseData,
DataSignature = dataSignature
});
break;
case RESPONSE_CODE_RESULT_USER_CANCELED:
tcsPurchase.SetException(new InAppBillingPurchaseException(PurchaseError.UserCancelled));
break;
case RESPONSE_CODE_RESULT_SERVICE_UNAVAILABLE:
tcsPurchase.SetException(new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable));
break;
case BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE:
tcsPurchase.SetException(new InAppBillingPurchaseException(PurchaseError.ItemUnavailable));
break;
case BILLING_RESPONSE_RESULT_DEVELOPER_ERROR:
tcsPurchase.SetException(new InAppBillingPurchaseException(PurchaseError.DeveloperError));
break;
case BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED:
tcsPurchase.SetException(new InAppBillingPurchaseException(PurchaseError.AlreadyOwned));
break;
case BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED:
tcsPurchase.SetException(new InAppBillingPurchaseException(PurchaseError.NotOwned));
break;
default:
tcsPurchase.SetException(new InAppBillingPurchaseException(PurchaseError.GeneralError, responseCode.ToString()));
break;
case 0:
//Reponse returned OK
var purchaseData = data.GetStringExtra(RESPONSE_IAP_DATA);
var dataSignature = data.GetStringExtra(RESPONSE_IAP_DATA_SIGNATURE);

tcsPurchase?.TrySetResult(new PurchaseResponse
{
PurchaseData = purchaseData, DataSignature = dataSignature
});
break;
case RESPONSE_CODE_RESULT_USER_CANCELED:
tcsPurchase?.TrySetException(new InAppBillingPurchaseException(PurchaseError.UserCancelled));
break;
case RESPONSE_CODE_RESULT_SERVICE_UNAVAILABLE:
tcsPurchase?.TrySetException(new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable));
break;
case BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE:
tcsPurchase?.TrySetException(new InAppBillingPurchaseException(PurchaseError.ItemUnavailable));
break;
case BILLING_RESPONSE_RESULT_DEVELOPER_ERROR:
tcsPurchase?.TrySetException(new InAppBillingPurchaseException(PurchaseError.DeveloperError));
break;
case BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED:
tcsPurchase?.TrySetException(new InAppBillingPurchaseException(PurchaseError.AlreadyOwned));
break;
case BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED:
tcsPurchase?.TrySetException(new InAppBillingPurchaseException(PurchaseError.NotOwned));
break;
default:
tcsPurchase?.TrySetException(
new InAppBillingPurchaseException(PurchaseError.GeneralError, responseCode.ToString()));
break;
}
}

Expand Down

0 comments on commit a22f2cf

Please sign in to comment.