Ejemplo n.º 1
0
 public Map<String, Integer> getAllInventory() {
   Map<String, Integer> res = new LinkedHashMap<String, Integer>();
   SQLiteDatabase db = helper.getReadableDatabase();
   Cursor c =
       db.query("Inventory", new String[] {"productId", "amount"}, null, null, null, null, null);
   try {
     while (c.moveToNext()) {
       String productId = c.getString(0);
       int amount = c.getInt(1);
       res.put(productId, amount);
     }
   } finally {
     c.close();
   }
   return res;
 }
Ejemplo n.º 2
0
 public int getInventoryAmount(String productId) {
   SQLiteDatabase db = helper.getReadableDatabase();
   Cursor c =
       db.query(
           "Inventory",
           new String[] {"amount"},
           "productId=?",
           new String[] {productId},
           null,
           null,
           null);
   try {
     if (c.moveToNext()) {
       return c.getInt(0);
     }
     return 0;
   } finally {
     c.close();
   }
 }
Ejemplo n.º 3
0
  public void updateWithChange(SignedData.Order change, Set<EasyBillingListener> listenerSet) {
    SQLiteDatabase db = helper.getWritableDatabase();
    String orderId = change.orderId;
    String productId = change.productId;

    Log.d(TAG, "@@updateWithOrder orderId: " + orderId);

    db.beginTransaction();
    try {
      { // put to permanent log
        ContentValues cv = new ContentValues();
        cv.put("orderId", change.orderId);
        cv.put("packageName", change.packageName);
        cv.put("productId", change.productId);
        cv.put("purchaseTime", change.purchaseTime);
        cv.put("purchaseState", change.purchaseState.ordinal());
        cv.put("developerPayload", change.developerPayload);
        db.insert("Changes", null, cv);
      }

      boolean orderInDb = count(db, "Orders", "orderId=?", orderId) > 0;
      Log.d(TAG, "This order is in db?: " + orderInDb);

      if (orderInDb) {
        PurchaseState oldPurchaseState =
            PurchaseState.valueOf(getInt(db, "Orders", "purchaseState", -1, "orderId=?", orderId));
        PurchaseState newPurchaseState = change.purchaseState;
        boolean oldHave = oldPurchaseState == PurchaseState.PURCHASED;
        boolean newHave = newPurchaseState == PurchaseState.PURCHASED;

        { // update db
          ContentValues cv = new ContentValues();
          cv.put("orderId", change.orderId);
          cv.put("purchaseState", change.purchaseState.ordinal());
          db.update("Orders", cv, "orderId=?", new String[] {orderId});
        }

        if (oldHave == newHave) {
          Log.d(
              TAG,
              "productId "
                  + change.productId
                  + " inventory not updated, purchase state old="
                  + oldPurchaseState
                  + " new="
                  + newPurchaseState);
        } else {
          int[] amounts = modifyAmount(db, productId, newHave ? +1 : -1);
          for (EasyBillingListener listener : listenerSet) {
            listener.onInventoryAmountChange(productId, newPurchaseState, amounts[0], amounts[1]);
          }
        }
      } else {
        PurchaseState newPurchaseState = change.purchaseState;
        boolean newHave = newPurchaseState == PurchaseState.PURCHASED;

        { // insert to db
          ContentValues cv = new ContentValues();
          cv.put("orderId", change.orderId);
          cv.put("purchaseState", change.purchaseState.ordinal());
          db.insert("Orders", null, cv);
        }

        if (newHave) {
          int[] amounts = modifyAmount(db, productId, +1);
          for (EasyBillingListener listener : listenerSet) {
            listener.onInventoryAmountChange(productId, newPurchaseState, amounts[0], amounts[1]);
          }
        } else {
          Log.d(
              TAG,
              "new purchase state: "
                  + newPurchaseState
                  + " for non-existing product in inventory, not modifying amount");
        }
      }

      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
    }
  }