Android Open Source - android-store Virtual Item Storage






From Project

Back to project page android-store.

License

The source code is released under:

Copyright (c) 2012 SOOMLA http://project.soom.la/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to...

If you think the Android project android-store listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright (C) 2012-2014 Soomla Inc./*from  www . ja v a  2  s  .  c o m*/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.soomla.store.data;

import com.soomla.SoomlaUtils;
import com.soomla.data.KeyValueStorage;
import com.soomla.store.domain.VirtualItem;

/**
 * This class is an abstract definition of a Virtual Item Storage.
 */
public abstract class VirtualItemStorage {

    /**
     * Retrieves the balance of the given virtual item.
     *
     * @param itemId the required virtual item's itemId
     * @return the balance of the required virtual item
     */
    public int getBalance(String itemId){
        SoomlaUtils.LogDebug(mTag, "fetching balance for virtual item with itemId: "
                + itemId);

        String key = keyBalance(itemId);
        String val = KeyValueStorage.getValue(key);

        int balance = 0;
        if (val != null) {
            balance = Integer.parseInt(val);
        }

        SoomlaUtils.LogDebug(mTag, "the balance for " + itemId + " is " + balance);

        return balance;
    }

    /**
     * Sets the balance of the given virtual item to be the given balance.
     *
     * @param itemId the required virtual item's itemId
     * @param balance the new balance to be set
     * @return the balance of the required virtual item
     */
    public int setBalance(String itemId, int balance) {
        return setBalance(itemId, balance, true);
    }

    /**
     * Sets the balance of the given virtual item to be the given balance, and if notify is true
     * posts the change in the balance to the event bus.
     *
     * @param itemId the required virtual item's itemId
     * @param balance the new balance to be set
     * @param notify if notify is true post balance change event
     * @return the balance of the required virtual item
     */
    public int setBalance(String itemId, int balance, boolean notify) {
        SoomlaUtils.LogDebug(mTag, "setting balance " + balance + " to " + itemId + ".");

        int oldBalance = getBalance(itemId);
        if (oldBalance == balance) {
            return balance;
        }

        String balanceStr = "" + balance;
        String key = keyBalance(itemId);

        KeyValueStorage.setValue(key, balanceStr);

        if (notify) {
            postBalanceChangeEvent(itemId, balance, 0);
        }

        return balance;
    }

    /**
     * Adds the given amount of items to the storage.
     *
     * @param itemId the required virtual item's itemId
     * @param amount the amount of items to add
     * @return new balance
     */
    public int add(String itemId, int amount){
        return add(itemId, amount, true);
    }

    /**
     * Adds the given amount of items to the storage, and if notify is true
     * posts the change in the balance to the event bus.
     *
     * @param itemId the required virtual item's itemId
     * @param amount the amount of items to add
     * @param notify if true posts balance change event
     * @return new balance
     */
    public int add(String itemId, int amount, boolean notify){
        SoomlaUtils.LogDebug(mTag, "adding " + amount + " " + itemId);

        int balance = getBalance(itemId);
        if (balance < 0) { /* in case the user "adds" a negative value */
            balance = 0;
            amount = 0;
        }
        String balanceStr = "" + (balance + amount);
        String key = keyBalance(itemId);
        KeyValueStorage.setValue(key, balanceStr);

        if (notify) {
            postBalanceChangeEvent(itemId, balance+amount, amount);
        }

        return balance + amount;
    }

    /**
     * Removes the given amount from the given virtual item's balance.
     *
     * @param itemId is the virtual item to remove the given amount from
     * @param amount is the amount to remove
     * @return new balance
     */
    public int remove(String itemId, int amount){
        return remove(itemId, amount, true);
    }

    /**
     * Removes the given amount from the given virtual item's balance, and if notify is true
     * posts the change in the balance to the event bus.
     *
     * @param itemId is the virtual item to remove the given amount from
     * @param amount is the amount to remove
     * @param notify if notify is true post balance change event
     * @return new balance
     */
    public int remove(String itemId, int amount, boolean notify){
        SoomlaUtils.LogDebug(mTag, "Removing " + amount + " " + itemId + ".");

        int balance = getBalance(itemId) - amount;
        if (balance < 0) {
            balance = 0;
            amount = 0;
        }
        String balanceStr = "" + balance;
        String key = keyBalance(itemId);
        KeyValueStorage.setValue(key, balanceStr);

        if (notify) {
            postBalanceChangeEvent(itemId, balance, -1*amount);
        }

        return balance;
    }

    /**
     * Retrieves the balance of the virtual item with the given itemId from the
     * <code>KeyValDatabase</code>.
     *
     * @param itemId id of the virtual item whose balance is to be retrieved
     * @return String containing name of storage base, itemId, and balance
     */
    protected abstract String keyBalance(String itemId);

    /**
     * Posts the given amount changed in the given balance of the given virtual item.
     *
     * @param itemId virtual item whose balance has changed
     * @param balance the balance that has changed
     * @param amountAdded the amount added to the item's balance
     */
    protected abstract void postBalanceChangeEvent(String itemId, int balance, int amountAdded);


    /** Private Members */

    protected String mTag = "SOOMLA VirtualItemStorage"; //used for Log messages
}




Java Source Code List

com.soomla.example.ExampleEventHandler.java
com.soomla.example.MuffinRushAssets.java
com.soomla.example.StoreExampleActivity.java
com.soomla.example.StoreGoodsActivity.java
com.soomla.example.StorePacksActivity.java
com.soomla.rewards.VirtualItemReward.java
com.soomla.store.IStoreAssets.java
com.soomla.store.SoomlaStore.java
com.soomla.store.StoreConfig.java
com.soomla.store.StoreForeground.java
com.soomla.store.StoreInventory.java
com.soomla.store.billing.IIabService.java
com.soomla.store.billing.IabCallbacks.java
com.soomla.store.billing.IabException.java
com.soomla.store.billing.IabHelper.java
com.soomla.store.billing.IabInventory.java
com.soomla.store.billing.IabPurchase.java
com.soomla.store.billing.IabResult.java
com.soomla.store.billing.IabSkuDetails.java
com.soomla.store.data.StorageManager.java
com.soomla.store.data.StoreInfo.java
com.soomla.store.data.StoreJSONConsts.java
com.soomla.store.data.VirtualCurrencyStorage.java
com.soomla.store.data.VirtualGoodsStorage.java
com.soomla.store.data.VirtualItemStorage.java
com.soomla.store.domain.MarketItem.java
com.soomla.store.domain.PurchasableVirtualItem.java
com.soomla.store.domain.VirtualCategory.java
com.soomla.store.domain.VirtualItem.java
com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack.java
com.soomla.store.domain.virtualCurrencies.VirtualCurrency.java
com.soomla.store.domain.virtualGoods.EquippableVG.java
com.soomla.store.domain.virtualGoods.LifetimeVG.java
com.soomla.store.domain.virtualGoods.SingleUsePackVG.java
com.soomla.store.domain.virtualGoods.SingleUseVG.java
com.soomla.store.domain.virtualGoods.UpgradeVG.java
com.soomla.store.domain.virtualGoods.VirtualGood.java
com.soomla.store.events.BillingNotSupportedEvent.java
com.soomla.store.events.BillingSupportedEvent.java
com.soomla.store.events.CurrencyBalanceChangedEvent.java
com.soomla.store.events.GoodBalanceChangedEvent.java
com.soomla.store.events.GoodEquippedEvent.java
com.soomla.store.events.GoodUnEquippedEvent.java
com.soomla.store.events.GoodUpgradeEvent.java
com.soomla.store.events.IabServiceStartedEvent.java
com.soomla.store.events.IabServiceStoppedEvent.java
com.soomla.store.events.ItemPurchaseStartedEvent.java
com.soomla.store.events.ItemPurchasedEvent.java
com.soomla.store.events.MarketItemsRefreshFinishedEvent.java
com.soomla.store.events.MarketItemsRefreshStartedEvent.java
com.soomla.store.events.MarketPurchaseCancelledEvent.java
com.soomla.store.events.MarketPurchaseEvent.java
com.soomla.store.events.MarketPurchaseStartedEvent.java
com.soomla.store.events.MarketRefundEvent.java
com.soomla.store.events.RestoreTransactionsFinishedEvent.java
com.soomla.store.events.RestoreTransactionsStartedEvent.java
com.soomla.store.events.SoomlaStoreInitializedEvent.java
com.soomla.store.events.UnexpectedStoreErrorEvent.java
com.soomla.store.exceptions.InsufficientFundsException.java
com.soomla.store.exceptions.NotEnoughGoodsException.java
com.soomla.store.exceptions.VirtualItemNotFoundException.java
com.soomla.store.purchaseTypes.PurchaseType.java
com.soomla.store.purchaseTypes.PurchaseWithMarket.java
com.soomla.store.purchaseTypes.PurchaseWithVirtualItem.java