import com.samsungapps.plasma.*;

class PaymentWrapper implements PlasmaListener {
    private static final int ITEM_AMOUNT = 25;
    private static final String TAG = "Bono.Payment.Samsung";

    private Plasma plasma = null;
    private int transactionId = 0;
    private Set<Integer> pendingTransactions = new HashSet<Integer>();
    private Set<String> ownedItems = new HashSet<String>();

    /**
     * ---- PLASMA INTERFACE START ----
     */

    @Override
    public void onItemInformationListReceived(int transactionId, int statusCode, ArrayList<ItemInformation> itemInformationList)
    {
        // Not used as we don't expose the abillity to list / show all available
        // items in out itemGroup.
    }

    @Override
    public void onPurchasedItemInformationListReceived(int transactionId, int statusCode, ArrayList<PurchasedItemInformation> purchasedItemInformationList)
    {
        // Contains a list of all purchased items in our itemGroup. Triggered
        // via requestPurchasedItemInformationList() after each complete
        // purchase to keep the local item DB in sync.

        itemDbClear();
        for (PurchasedItemInformation itemInformation : purchasedItemInformationList) {
            itemDbAdd(itemInformation.getItemId());
        }
        itemDbSave();
    }

    @Override
    public void onPurchaseItemInitialized(int transactionId, int statusCode, PurchaseTicket purchaseTicket)
    {
        // At this point, the purchase ticket is issued but ticket only means
        // that a purchase transaction has initialized successfully, the purchase
        // is not yet complete.
        //
        // If a purchase transaction is successfully initialized, the statusCode
        // of onPurchaseItemInitialized is set as Plasma.STATUS_CODE_SUCCESS.
        // However, if initialization fails, onPurchaseItemFinished will not be
        // called. This is explained in the following section.

        if (statusCode != Plasma.STATUS_CODE_SUCCESS) {
            Log.v(
                TAG,
                "onPurchaseItemInitialized failed for transactionId "
                + transactionId
                + " with status code "
                + statusCode
            );
            pendingTransactions.remove(transactionId);
        }
    }

    @Override
    public void onPurchaseItemFinished(int transactionId, int statusCode, PurchasedItemInformation purchaseItemInformation)
    {
        // It is called when a user's purchase transaction is completed, and the
        // final information about purchase transaction is provided.
        //
        // The final information means a receipt of a purchase transaction which
        // is completed.

        pendingTransactions.remove(transactionId);

        if (statusCode == Plasma.STATUS_CODE_SUCCESS) {
            itemDbAdd(purchaseItemInformation.getItemId());
            plasma.requestPurchasedItemInformationList(transactionId++, 1, ITEM_AMOUNT);
        } else {
            Log.v(
                TAG,
                "onPurchaseItemInitialized failed for transactionId "
                + transactionId
                + " with status code "
                + statusCode
            );
        }
    }

    /**
     * ---- PLASMA INTERFACE END ----
     */

    private boolean itemDbContains(String productId)
    {
        return ownedItems.contains(productId);
    }

    private void itemDbAdd(String productId)
    {
        ownedItems.add(productId);
    }

    private void itemDbClear()
    {
        SharedPreferences prefs = MonkeyGame.activity.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = prefs.edit();
        edit.clear();
        edit.commit();

        Log.v(TAG, "local item DB cleared");
    }

    private void itemDbSave()
    {
        SharedPreferences prefs = MonkeyGame.activity.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putStringSet(TAG, ownedItems);
        edit.commit();

        Log.v(TAG, "local item DB saved");
    }

    private void itemDbLoad()
    {
        SharedPreferences prefs = MonkeyGame.activity.getPreferences(Context.MODE_PRIVATE);
        try {
            ownedItems = prefs.getStringSet(TAG, new HashSet<String>());
        } catch (ClassCastException e) {
        }

        Log.v(TAG, "local item DB loaded");
    }

    /**
     * ---- MONKEY INTERFACE START ----
     */

    public void Init(String itemGroupId)
    {
        if (plasma == null) {
            plasma = new Plasma(itemGroupId, MonkeyGame.activity);
            plasma.setPlasmaListener(this);
            itemDbLoad();
        }
    }

    public boolean Purchase(String productId)
    {
        if (plasma == null) {
            Log.v(TAG, "not yet initialized");
            return false;
        }

        if (!plasma.requestPurchaseItem(transactionId++, productId)) {
            Log.v(TAG, "failed to request item purchase");
            return false;
        }

        Log.v(
            TAG,
            "purchase requested for productId " + productId
            + " with transactionId " + (transactionId - 1)
        );
        pendingTransactions.add(transactionId - 1);
        return true;
    }

    public boolean IsBought(String productId)
    {
        return itemDbContains(productId);
    }

    public boolean IsPurchaseInProgress()
    {
        return !pendingTransactions.isEmpty();
    }

    /**
     * ---- MONKEY INTERFACE END ----
     */
}

@@ -3966,18 +3966,18 @@
 ring
-Set
 (TAG, 
-o
+getO
 wned
@@ -3981,16 +3981,26 @@
 nedItems
+AsString()
 );%0A     
@@ -4214,33 +4214,37 @@
 y %7B%0A            
-o
+loadO
 wnedItems = pref
@@ -4236,19 +4236,25 @@
 nedItems
- = 
+AsString(
 prefs.ge
@@ -4264,37 +4264,16 @@
 ring
-Set
 (TAG, 
-new HashSet%3CString%3E(
+%22%22
 ));%0A
@@ -4367,32 +4367,428 @@
 oaded%22);%0A    %7D%0A%0A
+    private void loadOwnedItemsAsString(String input)%0A    %7B%0A        for (String productId : input.split(%22%5Cn%22)) %7B%0A            ownedItems.add(productId);%0A        %7D%0A    %7D%0A%0A    private String getOwnedItemsAsString()%0A    %7B%0A        String result = %22%22;%0A        for (String item : ownedItems) %7B%0A            result += item + %22%5Cn%22;%0A        %7D%0A        return result.substring(0, result.length() - 1);%0A    %7D%0A%0A
     /**%0A     * -

make samsung payment provider usable with API < 11