use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.
the class StoreExampleActivity method onCreate.
/**
* Called when the activity starts.
* Displays the main UI screen of the game.
*
* @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.main);
// PurchasingManager.registerObserver(new PurchasingObserver(this));
mRobotView = (ImageView) findViewById(R.id.drag_img);
mRobotView.setOnTouchListener(new MyTouchListener());
findViewById(R.id.rightbox).setOnDragListener(new MyDragListener());
Typeface font = Typeface.createFromAsset(getAssets(), "GoodDog.otf");
((TextView) findViewById(R.id.title_text)).setTypeface(font);
((TextView) findViewById(R.id.main_text)).setTypeface(font);
/*
Initialize SoomlaStore and EventHandler before the store is opened.
Compute your public key (that you got from the Android Market publisher site).
Instead of just storing the entire literal string here embedded in the program,
construct the key at runtime from pieces or use bit manipulation (for example,
XOR with some other string) to hide the actual key. The key itself is not secret
information, but we don't want to make it easy for an adversary to replace the
public key with one of their own and then fake messages from the server.
Generally, encryption keys/passwords should only be kept in memory
long enough to perform the operation they need to perform.
*/
IStoreAssets storeAssets = new MuffinRushAssets();
mEventHandler = new ExampleEventHandler(mHandler, this);
Soomla.initialize("[CUSTOM SECRET HERE]");
SoomlaConfig.logDebug = true;
SoomlaStore.getInstance().initialize(storeAssets);
GooglePlayIabService.AllowAndroidTestPurchases = true;
GooglePlayIabService iabService = GooglePlayIabService.getInstance();
iabService.setPublicKey("xxx");
iabService.configVerifyPurchases(new HashMap<String, Object>() {
{
put("clientId", "xxx.apps.googleusercontent.com");
put("clientSecret", "xxx");
put("refreshToken", "1/xxx");
}
});
//FOR TESTING PURPOSES ONLY: Check if it's a first run, if so add 10000 currencies.
SharedPreferences prefs = SoomlaApp.getAppContext().getSharedPreferences(SoomlaConfig.PREFS_NAME, Context.MODE_PRIVATE);
boolean initialized = prefs.getBoolean(FIRST_RUN, false);
if (!initialized) {
try {
for (VirtualCurrency currency : storeAssets.getCurrencies()) {
StoreInventory.giveVirtualItem(currency.getItemId(), 10000);
}
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(FIRST_RUN, true);
edit.commit();
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogError("Example Activity", "Couldn't add first 10000 currencies.");
}
}
}
use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.
the class StoreGoodsActivity method onGoodBalanceChanged.
/**
* Receives the given <code>goodBalanceChangedEvent</code>. Upon notification, fetches the
* good associated with the given <code>goodBalanceChangedEvent</code> and displays its price
* and the balance.
*
* @param goodBalanceChangedEvent the event received
*/
@Subscribe
public void onGoodBalanceChanged(GoodBalanceChangedEvent goodBalanceChangedEvent) {
VirtualGood good = null;
try {
good = (VirtualGood) StoreInfo.getVirtualItem(goodBalanceChangedEvent.getGoodItemId());
int id = 0;
for (int i = 0; i < StoreInfo.getGoods().size(); i++) {
if (StoreInfo.getGoods().get(i).getItemId().equals(good.getItemId())) {
id = i;
break;
}
}
ListView list = (ListView) findViewById(R.id.list);
TextView info = (TextView) list.getChildAt(id).findViewById(R.id.item_info);
PurchaseType purchaseType = good.getPurchaseType();
if (purchaseType instanceof PurchaseWithVirtualItem) {
info.setText("price: " + ((PurchaseWithVirtualItem) purchaseType).getAmount() + " balance: " + goodBalanceChangedEvent.getBalance());
}
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogDebug("StoreGoodsActivity", e.getMessage());
}
}
use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.
the class SoomlaStore method handleSuccessfulPurchase.
/**
* Checks the state of the purchase and responds accordingly, giving the user an item,
* throwing an error, or taking the item away and paying the user back.
*
* @param purchase purchase whose state is to be checked.
*/
private void handleSuccessfulPurchase(IabPurchase purchase, boolean isRestoring) {
String sku = purchase.getSku();
PurchasableVirtualItem pvi;
try {
pvi = StoreInfo.getPurchasableItem(sku);
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogError(TAG, "(handleSuccessfulPurchase - purchase or query-inventory) " + "ERROR : Couldn't find the " + " VirtualCurrencyPack OR MarketItem with productId: " + sku + ". It's unexpected so an unexpected error is being emitted.");
BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL));
return;
}
switch(purchase.getPurchaseState()) {
case 0:
{
if (purchase.isServerVerified()) {
this.finalizeTransaction(purchase, pvi, isRestoring);
} else {
BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(purchase.getVerificationErrorCode() != null ? purchase.getVerificationErrorCode() : UnexpectedStoreErrorEvent.ErrorCode.GENERAL));
}
break;
}
case 1:
case 2:
SoomlaUtils.LogDebug(TAG, "IabPurchase refunded.");
if (!StoreConfig.friendlyRefunds) {
pvi.take(1);
}
BusProvider.getInstance().post(new MarketRefundEvent(pvi, purchase.getDeveloperPayload()));
break;
}
}
use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.
the class StoreInventory method upgradeVirtualGood.
/**
* Upgrades the virtual good with the given <code>goodItemId</code> by doing the following:
* 1. Checks if the good is currently upgraded or if this is the first time being upgraded.
* 2. If the good is currently upgraded, upgrades to the next upgrade in the series, or in
* other words, <code>buy()</code>s the next upgrade. In case there are no more upgrades
* available(meaning the current upgrade is the last available), the function returns.
* 3. If the good has never been upgraded before, the function upgrades it to the first
* available upgrade, or in other words, <code>buy()</code>s the first upgrade in the series.
*
* @param goodItemId the id of the virtual good to be upgraded
* @throws VirtualItemNotFoundException
* @throws InsufficientFundsException
*/
public static void upgradeVirtualGood(String goodItemId) throws VirtualItemNotFoundException, InsufficientFundsException {
VirtualGood good = (VirtualGood) StoreInfo.getVirtualItem(goodItemId);
String upgradeVGItemId = StorageManager.getVirtualGoodsStorage().getCurrentUpgrade(good.getItemId());
UpgradeVG upgradeVG = null;
try {
upgradeVG = (UpgradeVG) StoreInfo.getVirtualItem(upgradeVGItemId);
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogDebug("SOOMLA StoreInventory", "This is BAD! Can't find the current upgrade (" + upgradeVGItemId + ") of: " + good.getItemId());
}
if (upgradeVG != null) {
String nextItemId = upgradeVG.getNextItemId();
if (TextUtils.isEmpty(nextItemId)) {
return;
}
UpgradeVG vgu = (UpgradeVG) StoreInfo.getVirtualItem(nextItemId);
vgu.buy("");
} else {
UpgradeVG first = StoreInfo.getGoodFirstUpgrade(goodItemId);
if (first != null) {
first.buy("");
}
}
}
use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.
the class PurchaseWithVirtualItem method buy.
/**
* Buys the virtual item with other virtual items.
*
* @throws InsufficientFundsException
*/
@Override
public void buy(String payload) throws InsufficientFundsException {
SoomlaUtils.LogDebug(TAG, "Trying to buy a " + getAssociatedItem().getName() + " with " + mAmount + " pieces of " + mTargetItemId);
VirtualItem item = null;
try {
item = StoreInfo.getVirtualItem(mTargetItemId);
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogError(TAG, "Target virtual item doesn't exist !");
return;
}
BusProvider.getInstance().post(new ItemPurchaseStartedEvent(getAssociatedItem().getItemId()));
VirtualItemStorage storage = StorageManager.getVirtualItemStorage(item);
assert storage != null;
int balance = storage.getBalance(item.getItemId());
if (balance < mAmount) {
throw new InsufficientFundsException(mTargetItemId);
}
storage.remove(item.getItemId(), mAmount);
getAssociatedItem().give(1);
BusProvider.getInstance().post(new ItemPurchasedEvent(getAssociatedItem().getItemId(), payload));
}
Aggregations