package org.onepf.life2.oms;

import android.content.Context;
import android.util.Log;
import org.onepf.life2.oms.appstore.GooglePlayBillingService;
import org.onepf.life2.oms.appstore.googleUtils.IabHelper;

/**
 * User: Boris Minaev
 * Date: 16.04.13
 * Time: 16:42
 */
public class OpenIabHelper {
    Context mContext;
    AppstoreServiceManager mServiceManager;
    Appstore mAppstore;
    AppstoreInAppBillingService mAppstoreBillingService;

    // Is debug logging enabled?
    boolean mDebugLog = false;
    String mDebugTag = "IabHelper";

    // Is an asynchronous operation in progress?
    // (only one at a time can be in progress)
    boolean mAsyncInProgress = false;

    // (for logging/debugging)
    // if mAsyncInProgress == true, what asynchronous operation is in progress?
    String mAsyncOperation = "";

    // The request code used to launch purchase flow
    int mRequestCode;

    // The item type of the current purchase flow
    String mPurchasingItemType;

    public OpenIabHelper(Context context, String googlePublicKey) {
        mContext = context;
        mServiceManager = AppstoreServiceManager.getInstance(context, googlePublicKey);
        mAppstore = mServiceManager.getAppstoreForService(AppstoreService.APPSTORE_SERVICE_IN_APP_BILLING);
        mAppstoreBillingService = mAppstore.getInAppBillingService();
    }

    public void startSetup(final IabHelper.OnIabSetupFinishedListener listener) {
        mAppstoreBillingService.startSetup(listener);
    }

    public void dispose() {
        logDebug("Disposing.");
        if (mAppstore.getAppstoreName() == AppstoreName.APPSTORE_GOOGLE) {
            ((GooglePlayBillingService) mAppstoreBillingService).dispose();
        }
    }


    void logDebug(String msg) {
        if (mDebugLog) Log.d(mDebugTag, msg);
    }

    void logError(String msg) {
        Log.e(mDebugTag, "In-app billing error: " + msg);
    }

    void logWarn(String msg) {
        Log.w(mDebugTag, "In-app billing warning: " + msg);
    }

}

@@ -42,22 +42,82 @@
 oid.
-content.Contex
+app.Activity;%0Aimport android.content.Context;%0Aimport android.content.Inten
 t;%0Ai
@@ -1038,24 +1038,157 @@
 gItemType;%0A%0A
+    // Item types%0A    public static final String ITEM_TYPE_INAPP = %22inapp%22;%0A    public static final String ITEM_TYPE_SUBS = %22subs%22;%0A%0A
     public O
@@ -1894,24 +1894,24 @@
 .dispose();%0A
-
         %7D%0A  
@@ -1915,16 +1915,1646 @@
 %0A    %7D%0A%0A
+    public boolean subscriptionsSupported() %7B%0A        return true;%0A    %7D%0A%0A    public void launchPurchaseFlow(Activity act, String sku, int requestCode, IabHelper.OnIabPurchaseFinishedListener listener) %7B%0A        launchPurchaseFlow(act, sku, requestCode, listener, %22%22);%0A    %7D%0A%0A    public void launchPurchaseFlow(Activity act, String sku, int requestCode,%0A                                   IabHelper.OnIabPurchaseFinishedListener listener, String extraData) %7B%0A        launchPurchaseFlow(act, sku, ITEM_TYPE_INAPP, requestCode, listener, extraData);%0A    %7D%0A%0A    public void launchSubscriptionPurchaseFlow(Activity act, String sku, int requestCode,%0A                                               IabHelper.OnIabPurchaseFinishedListener listener) %7B%0A        launchSubscriptionPurchaseFlow(act, sku, requestCode, listener, %22%22);%0A    %7D%0A%0A    public void launchSubscriptionPurchaseFlow(Activity act, String sku, int requestCode,%0A                                               IabHelper.OnIabPurchaseFinishedListener listener, String extraData) %7B%0A        launchPurchaseFlow(act, sku, ITEM_TYPE_SUBS, requestCode, listener, extraData);%0A    %7D%0A%0A    public void launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode,%0A                                   IabHelper.OnIabPurchaseFinishedListener listener, String extraData) %7B%0A        mAppstoreBillingService.launchPurchaseFlow(act, sku, itemType, requestCode, listener, extraData);%0A    %7D%0A%0A    public boolean handleActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        return mAppstoreBillingService.handleActivityResult(requestCode, resultCode, data);%0A    %7D%0A%0A
 %0A    voi

add OpenIabHelper functionality