示例#1
0
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
          Log.d(TAG, "Query inventory finished.");
          if (result.isFailure()) {
            complain("Failed to query inventory: " + result);
            return;
          }

          Log.d(TAG, "Query inventory was successful.");

          /*
           * Check for items we own. Notice that for each purchase, we check
           * the developer payload to see if it's correct! See
           * verifyDeveloperPayload().
           */

          // Do we have the premium upgrade?
          Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
          mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
          Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));

          // Do we have the infinite gas plan?
          Purchase infiniteGasPurchase = inventory.getPurchase(SKU_INFINITE_GAS);
          mSubscribedToInfiniteGas =
              (infiniteGasPurchase != null && verifyDeveloperPayload(infiniteGasPurchase));
          Log.d(
              TAG,
              "User "
                  + (mSubscribedToInfiniteGas ? "HAS" : "DOES NOT HAVE")
                  + " infinite gas subscription.");
          if (mSubscribedToInfiniteGas) mTank = TANK_MAX;

          // Check for gas delivery -- if we own gas, we should fill up the tank immediately
          Purchase gasPurchase = inventory.getPurchase(SKU_GAS);
          if (gasPurchase != null && verifyDeveloperPayload(gasPurchase)) {
            Log.d(TAG, "We have gas. Consuming it.");
            mHelper.consumeAsync(inventory.getPurchase(SKU_GAS), mConsumeFinishedListener);
            return;
          }

          updateUi();
          setWaitScreen(false);
          Log.d(TAG, "Initial inventory query finished; enabling main UI.");
        }
  private String inventoryToJson(Inventory inventory) throws JSONException {
    JSONStringer json = new JSONStringer().object();

    json.key("purchaseMap").array();
    for (Map.Entry<String, Purchase> entry : inventory.getPurchaseMap().entrySet()) {
      json.array();
      json.value(entry.getKey());
      json.value(purchaseToJson(entry.getValue()));
      json.endArray();
    }
    json.endArray();

    json.key("skuMap").array();
    for (Map.Entry<String, SkuDetails> entry : inventory.getSkuMap().entrySet()) {
      json.array();
      json.value(entry.getKey());
      json.value(skuDetailsToJson(entry.getValue()));
      json.endArray();
    }
    json.endArray();

    json.endObject();
    return json.toString();
  }