use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.
the class StorePacksActivity method onCreate.
/**
* Called when the activity starts.
*
* @param savedInstanceState if the activity should be re-initialized after previously being
* shut down then this <code>Bundle</code> will contain the most
* recent data, otherwise it will be null.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
LinearLayout getMore = (LinearLayout) findViewById(R.id.getMore);
TextView title = (TextView) findViewById(R.id.title);
getMore.setVisibility(View.INVISIBLE);
title.setText("Virtual Currency Packs");
mImages = generateImagesHash();
mStoreAdapter = new StoreAdapter();
/* configuring the list with an adapter */
final Activity activity = this;
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(mStoreAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
/*
* The user decided to make an actual purchase of virtual goods. We try to buy() the
* user's desired good and SoomlaStore tells us if the user has enough funds to
* make the purchase. If he/she doesn't have enough then an
* InsufficientFundsException will be thrown.
*/
PurchaseWithMarket pwm = null;
VirtualCurrencyPack pack = StoreInfo.getCurrencyPacks().get(i);
pwm = (PurchaseWithMarket) pack.getPurchaseType();
try {
pwm.buy("this is just a payload");
} catch (InsufficientFundsException e) {
AlertDialog ad = new AlertDialog.Builder(activity).create();
// This blocks the 'BACK' button
ad.setCancelable(false);
ad.setMessage("Can't continue with purchase (You don't have enough muffins !)");
ad.setButton(DialogInterface.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
}
/* fetching the currency balance and placing it in the balance label */
TextView muffinsBalance = (TextView) activity.findViewById(R.id.balance);
muffinsBalance.setText("" + StorageManager.getVirtualCurrencyStorage().getBalance(StoreInfo.getCurrencies().get(0).getItemId()));
}
});
}
use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.
the class StoreInfo method initializeWithStoreAssets.
/**
* Initializes from <code>IStoreAssets</code>.
* This happens only once - when the game is loaded for the first time.
*
* @param storeAssets game economy
*/
private static void initializeWithStoreAssets(IStoreAssets storeAssets) {
// fall-back here if the json doesn't exist,
// we load the store from the given {@link IStoreAssets}.
mCurrencies = new LinkedList<VirtualCurrency>(Arrays.asList(storeAssets.getCurrencies()));
mCurrencyPacks = new LinkedList<VirtualCurrencyPack>(Arrays.asList(storeAssets.getCurrencyPacks()));
mGoods = new LinkedList<VirtualGood>(Arrays.asList(storeAssets.getGoods()));
mCategories = new LinkedList<VirtualCategory>(Arrays.asList(storeAssets.getCategories()));
mVirtualItems = new HashMap<String, VirtualItem>();
mPurchasableItems = new HashMap<String, PurchasableVirtualItem>();
mGoodsCategories = new HashMap<String, VirtualCategory>();
mGoodsUpgrades = new HashMap<String, List<UpgradeVG>>();
for (VirtualCurrency vi : mCurrencies) {
mVirtualItems.put(vi.getItemId(), vi);
}
for (VirtualCurrencyPack vi : mCurrencyPacks) {
mVirtualItems.put(vi.getItemId(), vi);
PurchaseType purchaseType = vi.getPurchaseType();
if (purchaseType instanceof PurchaseWithMarket) {
mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vi);
}
}
for (VirtualGood vi : mGoods) {
mVirtualItems.put(vi.getItemId(), vi);
if (vi instanceof UpgradeVG) {
List<UpgradeVG> upgrades = mGoodsUpgrades.get(((UpgradeVG) vi).getGoodItemId());
if (upgrades == null) {
upgrades = new ArrayList<UpgradeVG>();
mGoodsUpgrades.put(((UpgradeVG) vi).getGoodItemId(), upgrades);
}
upgrades.add((UpgradeVG) vi);
}
PurchaseType purchaseType = vi.getPurchaseType();
if (purchaseType instanceof PurchaseWithMarket) {
mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vi);
}
}
for (VirtualCategory category : mCategories) {
for (String goodItemId : category.getGoodsItemIds()) {
mGoodsCategories.put(goodItemId, category);
}
}
// Remove this code when no longer needed.
if (mNonConsumableMigrationNeeded) {
SoomlaUtils.LogDebug(TAG, "NonConsumables balance migration is required. Doing it now.");
nonConsBalancesToLTVGs();
}
save();
}
use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.
the class StoreInfo method nonConsBalancesToLTVGs.
/**
* IStoreAssets was changed and version number was bumped but we need to check if we need to do balance migration for non-consumables.
* The metadata in DB was deleted and we're overwriting it.
* We just need to set the balances of the lifetime items instead of the non-consumables.
*/
private static void nonConsBalancesToLTVGs() {
for (VirtualGood good : mGoods) {
if ((good instanceof LifetimeVG) && good.getPurchaseType() instanceof PurchaseWithMarket) {
String keyNonConsExist = DB_NONCONSUMABLE_KEY_PREFIX + good.getItemId() + ".exists";
if (KeyValueStorage.getValue(keyNonConsExist) != null) {
good.give(1);
KeyValueStorage.deleteKeyValue(keyNonConsExist);
}
}
}
mNonConsumableMigrationNeeded = false;
}
use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.
the class StoreInfo method addVG.
/**
* Adds the given virtual good to <code>StoreInfo</code>'s <code>mGoods</code>,
* <code>mVirtualItems</code>, and if the good has purchase type <code>PurchaseWithMarket</code>
* then it is also added to <code>mPurchasableItems</code>.
*
* @param g virtual good to be added
*/
private static void addVG(VirtualGood g) {
mGoods.add(g);
mVirtualItems.put(g.getItemId(), g);
PurchaseType purchaseType = g.getPurchaseType();
if (purchaseType instanceof PurchaseWithMarket) {
mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), g);
}
}
use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.
the class PurchasableVirtualItem method toJSONObject.
/**
* @{inheritDoc}
*/
@Override
public JSONObject toJSONObject() {
JSONObject parentJsonObject = super.toJSONObject();
JSONObject jsonObject = new JSONObject();
try {
Iterator<?> keys = parentJsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
jsonObject.put(key, parentJsonObject.get(key));
}
JSONObject purchasableObj = new JSONObject();
if (mPurchaseType instanceof PurchaseWithMarket) {
purchasableObj.put(StoreJSONConsts.PURCHASE_TYPE, StoreJSONConsts.PURCHASE_TYPE_MARKET);
MarketItem mi = ((PurchaseWithMarket) mPurchaseType).getMarketItem();
purchasableObj.put(StoreJSONConsts.PURCHASE_MARKET_ITEM, mi.toJSONObject());
} else if (mPurchaseType instanceof PurchaseWithVirtualItem) {
purchasableObj.put(StoreJSONConsts.PURCHASE_TYPE, StoreJSONConsts.PURCHASE_TYPE_VI);
purchasableObj.put(StoreJSONConsts.PURCHASE_VI_ITEMID, ((PurchaseWithVirtualItem) mPurchaseType).getTargetItemId());
purchasableObj.put(StoreJSONConsts.PURCHASE_VI_AMOUNT, ((PurchaseWithVirtualItem) mPurchaseType).getAmount());
}
jsonObject.put(StoreJSONConsts.PURCHASABLE_ITEM, purchasableObj);
} catch (JSONException e) {
SoomlaUtils.LogError(TAG, "An error occurred while generating JSON object.");
}
return jsonObject;
}
Aggregations