Skip to content

Commit

Permalink
Merge pull request #1 from AminSojoudi/master
Browse files Browse the repository at this point in the history
bug fix: calling a method on a null iabHelper or service connection
  • Loading branch information
AliA74 authored Oct 1, 2024
2 parents bfe7773 + c93061f commit 7121529
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 32 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Android CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: JavaPlugin
steps:
- uses: actions/checkout@v3
- name: set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JavaPlugin/.idea/
JavaPlugin/local.properties
3 changes: 3 additions & 0 deletions JavaPlugin/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 70 additions & 24 deletions JavaPlugin/src/main/java/com/bazaar/BazaarIABPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class BazaarIABPlugin extends BazaarIABPluginBase
, IabHelper.OnConsumeFinishedListener
, IabHelper.OnConsumeMultiFinishedListener
{
private static String BILLING_NOT_RUNNING_ERROR = "The billing service is not running or billing is not supported. Aborting.";
private static final String BILLING_NOT_RUNNING_ERROR = "The billing service is not running or billing is not supported. Aborting.";
private static final String STORE_CONNECTION_IS_NULL = "The billing service connection is null";

private static BazaarIABPlugin mInstance;

private IabHelper mHelper;
Expand Down Expand Up @@ -72,12 +74,12 @@ public void onIabSetupFinished(IabResult result)
{
if (result.isSuccess())
{
UnitySendMessage("billingSupported", "");
UnitySendMessage(BazaarUnityCallbacks.BILLING_SUPPORTED, "");
}
else
{
Log.i(TAG, "billing not supported: " + result.getMessage());
UnitySendMessage("billingNotSupported", result.getMessage());
UnitySendMessage(BazaarUnityCallbacks.BILLING_NOT_SUPPORTED, result.getMessage());
mHelper = null;
}
}
Expand All @@ -100,6 +102,10 @@ public boolean areSubscriptionsSupported()
if (mHelper == null) {
return false;
}
if (mHelper.connectionIsNull()){
Log.i(TAG, STORE_CONNECTION_IS_NULL);
return false;
}
return mHelper.subscriptionsSupported();
}

Expand All @@ -109,6 +115,12 @@ public void queryInventory(final String[] skus)
if (mHelper == null)
{
Log.i(TAG, BILLING_NOT_RUNNING_ERROR);
UnitySendMessage(BazaarUnityCallbacks.QUERY_INVENTORY_FAILED, BILLING_NOT_RUNNING_ERROR);
return;
}
if (mHelper.connectionIsNull()){
Log.i(TAG, STORE_CONNECTION_IS_NULL);
UnitySendMessage(BazaarUnityCallbacks.QUERY_INVENTORY_FAILED, STORE_CONNECTION_IS_NULL);
return;
}
runSafelyOnUiThread(new Runnable()
Expand All @@ -117,7 +129,7 @@ public void run()
{
mHelper.queryInventoryAsync(true, Arrays.asList(skus), BazaarIABPlugin.this);
}
}, "queryInventoryFailed");
}, BazaarUnityCallbacks.QUERY_INVENTORY_FAILED);
}

public void onQueryInventoryFinished(IabResult result, Inventory inventory)
Expand All @@ -127,11 +139,11 @@ public void onQueryInventoryFinished(IabResult result, Inventory inventory)
mPurchases = inventory.getAllPurchases();
mSkus = inventory.getAllSkuDetails();

UnitySendMessage("queryInventorySucceeded", inventory.getAllSkusAndPurchasesAsJson());
UnitySendMessage(BazaarUnityCallbacks.QUERY_INVENTORY_SUCCEEDED, inventory.getAllSkusAndPurchasesAsJson());
}
else
{
UnitySendMessage("queryInventoryFailed", result.getMessage());
UnitySendMessage(BazaarUnityCallbacks.QUERY_INVENTORY_FAILED, result.getMessage());
}
}

Expand All @@ -141,6 +153,12 @@ public void querySkuDetails(final String[] skus)
if (mHelper == null)
{
Log.i(TAG, BILLING_NOT_RUNNING_ERROR);
UnitySendMessage(BazaarUnityCallbacks.QUERY_SKU_DETAILS_FAILED, BILLING_NOT_RUNNING_ERROR);
return;
}
if (mHelper.connectionIsNull()){
Log.i(TAG, STORE_CONNECTION_IS_NULL);
UnitySendMessage(BazaarUnityCallbacks.QUERY_SKU_DETAILS_FAILED, STORE_CONNECTION_IS_NULL);
return;
}
runSafelyOnUiThread(new Runnable()
Expand All @@ -149,7 +167,7 @@ public void run()
{
mHelper.querySkuDetailsAsync(Arrays.asList(skus), BazaarIABPlugin.this);
}
}, "querySkuDetailsFailed");
}, BazaarUnityCallbacks.QUERY_SKU_DETAILS_FAILED);
}

public void onQuerySkuDetailsFinished(IabResult result, Inventory inventory)
Expand All @@ -159,11 +177,11 @@ public void onQuerySkuDetailsFinished(IabResult result, Inventory inventory)
mSkus = inventory.getAllSkuDetails();
String skusStr = inventory.getAllSkusAsJson().toString();

UnitySendMessage("querySkuDetailsSucceeded", skusStr);
UnitySendMessage(BazaarUnityCallbacks.QUERY_SKU_DETAILS_SUCCEEDED, skusStr);
}
else
{
UnitySendMessage("querySkuDetailsFailed", result.getMessage());
UnitySendMessage(BazaarUnityCallbacks.QUERY_SKU_DETAILS_FAILED, result.getMessage());
}
}

Expand All @@ -173,6 +191,12 @@ public void queryPurchases()
if (mHelper == null)
{
Log.i(TAG, BILLING_NOT_RUNNING_ERROR);
UnitySendMessage(BazaarUnityCallbacks.QUERY_PURCHASES_FAILED, BILLING_NOT_RUNNING_ERROR);
return;
}
if (mHelper.connectionIsNull()){
Log.i(TAG, STORE_CONNECTION_IS_NULL);
UnitySendMessage(BazaarUnityCallbacks.QUERY_PURCHASES_FAILED, STORE_CONNECTION_IS_NULL);
return;
}
runSafelyOnUiThread(new Runnable()
Expand All @@ -181,7 +205,7 @@ public void run()
{
mHelper.queryPurchasesAsync(BazaarIABPlugin.this);
}
}, "queryInventoryFailed");
}, BazaarUnityCallbacks.QUERY_PURCHASES_FAILED);
}

public void onQueryPurchasesFinished(IabResult result, Inventory inventory)
Expand All @@ -191,11 +215,11 @@ public void onQueryPurchasesFinished(IabResult result, Inventory inventory)
mPurchases = inventory.getAllPurchases();
String purchasesStr = inventory.getAllPurchasesAsJson().toString();

UnitySendMessage("queryPurchasesSucceeded", purchasesStr);
UnitySendMessage(BazaarUnityCallbacks.QUERY_PURCHASES_SUCCEEDED, purchasesStr);
}
else
{
UnitySendMessage("queryPurchasesFailed", result.getMessage());
UnitySendMessage(BazaarUnityCallbacks.QUERY_PURCHASES_FAILED, result.getMessage());
}
}

Expand All @@ -205,6 +229,12 @@ public void purchaseProduct(final String sku, final String developerPayload)
if (mHelper == null)
{
Log.i(TAG, BILLING_NOT_RUNNING_ERROR);
UnitySendMessage(BazaarUnityCallbacks.PURCHASE_FAILED, BILLING_NOT_RUNNING_ERROR);
return;
}
if (mHelper.connectionIsNull()){
Log.i(TAG, STORE_CONNECTION_IS_NULL);
UnitySendMessage(BazaarUnityCallbacks.PURCHASE_FAILED, STORE_CONNECTION_IS_NULL);
return;
}

Expand All @@ -227,7 +257,7 @@ public void run()
proxyStarter.putExtra("developerPayload", developerPayload);
getActivity().startActivity(proxyStarter);
}
}, "purchaseFailed");
}, BazaarUnityCallbacks.PURCHASE_FAILED);
}

public void onIabPurchaseFinished(IabResult result, Purchase info)
Expand All @@ -237,11 +267,11 @@ public void onIabPurchaseFinished(IabResult result, Purchase info)
if (!mPurchases.contains(info)) {
mPurchases.add(info);
}
UnitySendMessage("purchaseSucceeded", info.toJson());
UnitySendMessage(BazaarUnityCallbacks.PURCHASE_SUCCEEDED, info.toJson());
}
else
{
UnitySendMessage("purchaseFailed", result.getMessage());
UnitySendMessage(BazaarUnityCallbacks.PURCHASE_FAILED, result.getMessage());
}
}

Expand All @@ -251,14 +281,20 @@ public void consumeProduct(String sku)
if (mHelper == null)
{
Log.i(TAG, BILLING_NOT_RUNNING_ERROR);
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, BILLING_NOT_RUNNING_ERROR);
return;
}
if (mHelper.connectionIsNull()){
Log.i(TAG, STORE_CONNECTION_IS_NULL);
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, STORE_CONNECTION_IS_NULL);
return;
}

final Purchase purchase = getPurchasedProductForSku(sku);
if (purchase == null)
{
Log.i(TAG, "Attempting to consume an item that has not been purchased. Aborting to avoid exception. sku: " + sku);
UnitySendMessage("consumePurchaseFailed", sku + ": you cannot consume a project that has not been purchased or if you have not first queried your inventory to retreive the purchases.");
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, sku + ": you cannot consume a project that has not been purchased or if you have not first queried your inventory to retreive the purchases.");
return;
}

Expand All @@ -268,7 +304,7 @@ public void run()
{
mHelper.consumeAsync(purchase, BazaarIABPlugin.this);
}
}, "consumePurchaseFailed");
}, BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED);
}

private Purchase getPurchasedProductForSku(String sku)
Expand All @@ -292,12 +328,12 @@ public void onConsumeFinished(Purchase purchase, IabResult result)
mPurchases.remove(purchase);
}

UnitySendMessage("consumePurchaseSucceeded", purchase.toJson());
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_SUCCEEDED, purchase.toJson());
}
else
{
String res = purchase.getSku() + ": " + result.getMessage();
UnitySendMessage("consumePurchaseFailed", res);
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, res);
}
}

Expand All @@ -307,12 +343,20 @@ public void consumeProducts(String[] skus)
if (mHelper == null)
{
Log.i(TAG, BILLING_NOT_RUNNING_ERROR);
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, BILLING_NOT_RUNNING_ERROR);
return;
}
if (mHelper.connectionIsNull()){
Log.i(TAG, STORE_CONNECTION_IS_NULL);
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, STORE_CONNECTION_IS_NULL);
return;
}

if ((mPurchases == null) || (mPurchases.size() == 0))
{
Log.e(TAG, "there are no purchases available to consume");
String error = "there are no purchases available to consume";
Log.e(TAG, error);
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, error);
return;
}

Expand All @@ -328,7 +372,9 @@ public void consumeProducts(String[] skus)

if (confirmedPurchases.size() != skus.length)
{
Log.i(TAG, "Attempting to consume " + skus.length + " item(s) but only " + confirmedPurchases.size() + " item(s) were found to be purchased. Aborting.");
String error = "Attempting to consume " + skus.length + " item(s) but only " + confirmedPurchases.size() + " item(s) were found to be purchased. Aborting.";
Log.i(TAG, error);
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, error);
return;
}

Expand All @@ -338,7 +384,7 @@ public void run()
{
mHelper.consumeAsync(confirmedPurchases, BazaarIABPlugin.this);
}
}, "consumePurchaseFailed");
}, BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED);
}

public void onConsumeMultiFinished(List<Purchase> purchases, List<IabResult> results)
Expand All @@ -354,12 +400,12 @@ public void onConsumeMultiFinished(List<Purchase> purchases, List<IabResult> res
mPurchases.remove(purchase);
}

UnitySendMessage("consumePurchaseSucceeded", purchase.toJson());
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_SUCCEEDED, purchase.toJson());
}
else
{
String res = purchase.getSku() + ": " + result.getMessage();
UnitySendMessage("consumePurchaseFailed", res);
UnitySendMessage(BazaarUnityCallbacks.CONSUME_PURCHASE_FAILED, res);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions JavaPlugin/src/main/java/com/bazaar/BazaarUnityCallbacks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.bazaar;

public class BazaarUnityCallbacks {

public static final String BILLING_SUPPORTED = "billingSupported";
public static final String BILLING_NOT_SUPPORTED = "billingNotSupported";
public static final String QUERY_INVENTORY_FAILED = "queryInventoryFailed";
public static final String QUERY_INVENTORY_SUCCEEDED = "queryInventorySucceeded";
public static final String QUERY_SKU_DETAILS_FAILED = "querySkuDetailsFailed";
public static final String QUERY_SKU_DETAILS_SUCCEEDED = "querySkuDetailsSucceeded";
public static final String QUERY_PURCHASES_FAILED = "queryPurchasesFailed";
public static final String QUERY_PURCHASES_SUCCEEDED = "queryPurchasesSucceeded";
public static final String PURCHASE_FAILED = "purchaseFailed";
public static final String PURCHASE_SUCCEEDED = "purchaseSucceeded";
public static final String CONSUME_PURCHASE_FAILED = "consumePurchaseFailed";
public static final String CONSUME_PURCHASE_SUCCEEDED = "consumePurchaseSucceeded";
}
5 changes: 4 additions & 1 deletion JavaPlugin/src/main/java/com/bazaar/util/BroadcastIAB.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ public class BroadcastIAB extends IAB {
private WeakReference<BillingSupportCommunication> billingSupportWeakReference;
private WeakReference<Activity> launchPurchaseActivityWeakReference;

private final BroadcastIAB mybroadcastIab;

BroadcastIAB(Context context, IABLogger logger, String mSignatureBase64) {
super(logger);
this.context = context;
this.signatureBase64 = mSignatureBase64 != null ? mSignatureBase64 : "secureBroadcastKey";
mybroadcastIab = this;
}

@Override
Expand Down Expand Up @@ -124,7 +127,7 @@ public void onNewBroadcastReceived(Intent intent) {
OnConnectListener listener = safeGetFromWeakReference(connectListenerWeakReference);
mSetupDone = true;
if (listener != null) {
listener.connected();
listener.connected(mybroadcastIab);
}
break;
case receivePurchaseAction:
Expand Down
Loading

0 comments on commit 7121529

Please sign in to comment.