Main.java
/*
* Copyright © 2012 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.battletank;
import com.nokia.example.battletank.menu.BuyMenu;
import com.nokia.mid.payment.IAPClientPaymentException;
import com.nokia.mid.payment.IAPClientPaymentListener;
import com.nokia.mid.payment.IAPClientPaymentManager;
import com.nokia.mid.payment.IAPClientProductData;
import com.nokia.mid.payment.IAPClientUserAndDeviceData;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
public class Main
extends MIDlet
implements IAPClientPaymentListener {
public static final String PURCHASE_ID = "681803";
// trial version as default
private static boolean trial = true;
private static IAPClientPaymentManager manager;
private static Display display;
private BattleTankCanvas battleTankCanvas = null;
/**
* Initializes display.
*/
public void startApp() {
if (battleTankCanvas == null) {
battleTankCanvas = new BattleTankCanvas(this);
display = Display.getDisplay(this);
display.setCurrent(battleTankCanvas);
}
manager = getIAPManager();
manager.setIAPClientPaymentListener(this);
// If the trial flag has not been set to false in record store,
// the boolean gets
// set during the instantiation of BattleTankCanvas
if (trial) {
manager.getProductData(PURCHASE_ID);
}
}
public void pauseApp() {
}
/**
* Saves game before destroying the application.
*
* @param unconditional
*/
public void destroyApp(boolean unconditional) {
if (battleTankCanvas != null) {
battleTankCanvas.saveGame();
}
}
public void close() {
destroyApp(true);
notifyDestroyed();
}
public static boolean isTrial() {
return trial;
}
public static void setTrial(boolean t) {
trial = t;
}
/**
* Purchases the full versions of BattleTank.
*
* @return true if purchase was successful
*/
public static boolean purchaseFullVersion() {
int status = manager.purchaseProduct(PURCHASE_ID,
IAPClientPaymentManager.FORCED_AUTOMATIC_RESTORATION);
if (status != IAPClientPaymentManager.SUCCESS) {
showAlertMessage(display, "Purchase failure", "Purchase process failed. "
+ Messages.getPaymentError(status), AlertType.ERROR);
return false;
}
return true;
}
/**
* Restore the full version for free if it is already purchased.
*
* @return true if restore was successful
*/
public static boolean restoreFullVersion() {
int status = manager.restoreProduct(PURCHASE_ID,
IAPClientPaymentManager.DEFAULT_AUTHENTICATION);
if (status != IAPClientPaymentManager.SUCCESS) {
showAlertMessage(display, "Restoration failure", "Restoration process failed. "
+ Messages.getPaymentError(status), AlertType.ERROR);
return false;
}
return true;
}
public void userAndDeviceDataReceived(int status,
IAPClientUserAndDeviceData ud) {
}
public void restorableProductsReceived(int status,
IAPClientProductData[] productDataList) {
}
public void productDataListReceived(int status,
IAPClientProductData[] productDataList) {
}
public void productDataReceived(int status, IAPClientProductData pd) {
if (status == OK) {
BuyMenu.setPrice(pd.getPrice());
}
}
public void purchaseCompleted(int status, String purchaseTicket) {
battleTankCanvas.hideBuyMenuWaitIndicator();
if (status == OK || status == RESTORABLE) {
setTrial(false);
battleTankCanvas.hideBuyOption();
battleTankCanvas.hideCurrentMenu();
}
else {
showAlertMessage("Purchase failure", "Purchase process failed. "
+ Messages.getPaymentError(status), AlertType.ERROR);
}
}
public void restorationCompleted(int status, String purchaseTicket) {
battleTankCanvas.hideBuyMenuWaitIndicator();
if (status == OK || status == RESTORABLE) {
setTrial(false);
battleTankCanvas.hideBuyOption();
battleTankCanvas.hideCurrentMenu();
}
else {
showAlertMessage("Restoraiton failure", "Restoration failed. "
+ Messages.getPaymentError(status), AlertType.ERROR);
}
}
public static IAPClientPaymentManager getIAPManager() {
if (manager == null) {
try {
manager = IAPClientPaymentManager.getIAPClientPaymentManager();
}
catch (IAPClientPaymentException ipe) {
}
}
return manager;
}
/**
* Displays an alert message.
*
* @param title Title of the message
* @param alertText Text of the message
* @param type Type of the alert
*/
public void showAlertMessage(String title, String alertText, AlertType type) {
showAlertMessage(display, title, alertText, type);
}
public static void showAlertMessage(Display display, String title,
String alertText, AlertType type) {
Alert alert = new Alert(title, alertText, null, type);
display.setCurrent(alert, display.getCurrent());
}
}