<commit_before>// ref: http://developer.android.com/google/play/billing/billing_integrate.html
package com.baroq.pico.google;

import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.content.ServiceConnection;
import android.content.Context;
import android.util.Log;

import com.android.vending.billing.IInAppBillingService;

public class InAppBilling extends CordovaPlugin{
    private static final String publicKey = "<KEY>";
    
    private static final String ACTION_INIT = "iabInit";
    private static final String ACTION_INV = "iabInventory";
    private static final String ACTION_GOODS = "iabGoods";
    private static final String ACTION_BUY = "iabBuy";
    private static final String ACTION_TRANSACT = "iabTransact";
    private static final String ACTION_CONSUME = "iabConsume";

    private IInAppBillingService mService;

    private ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IInAppBillingService.Stub.asInterface(service);
        }
    };

    // Plugin action handler
    @Override
    public boolean execute(String action, JSONArray data,  CallbackContext callbackContext) {
        // Save the callback id
        // this.callbackId = callbackId;
        boolean result = true;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        pluginResult.setKeepCallback(true);

        if (ACTION_INIT.equals(action)) {
            init(callbackContext);
            callbackContext.sendPluginResult(pluginResult);
        } else if (ACTION_INV.equals(action)){
        } else if (ACTION_GOODS.equals(action)){
        } else if (ACTION_BUY.equals(action)){
        } else if (ACTION_TRANSACT.equals(action)){
        } else if (ACTION_CONSUME.equals(action)){
        }

        return result;
    }

    private void init(CallbackContext callbackContext){
        bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
        PluginResult result;
        if (mServiceConn != null){
            result = new PluginResult(PluginResult.Status.OK, "In app billing initialized");
        }else{
            result = new PluginResult(PluginResult.Status.ERROR, "In app billing not supported");
        }
        result.setKeepCallback(false);
        callbackContext.sendPluginResult(result);
    }

    public void deinit(){
        if (mServiceConn != null) {
            unbindService(mServiceConn);
        }   
    }

    public void getSKUDetails(){
        ArrayList skuList = new ArrayList();
        skuList.add("premiumUpgrade");
        skuList.add("gas");
        Bundle querySkus = new Bundle();
        querySkus.putStringArrayList(“ITEM_ID_LIST”, skuList);

        Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);

        int response = skuDetails.getInt("RESPONSE_CODE");
        if (response == 0) {
            ArrayList responseList = skuDetails.getStringArrayList("DETAILS_LIST");

            for (String thisResponse : responseList) {
                JSONObject object = new JSONObject(thisResponse);
                String sku = object.getString("productId");
                String price = object.getString("price");
                if (sku.equals("premiumUpgrade")) mPremiumUpgradePrice = price;
                else if (sku.equals("gas")) mGasPrice = price;
            }
        }
    }

    public void itemPurchase(String sku){
        Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
        switch(buyIntentBundle.getInt("RESPONSE_CODE", 9)){
            case 0: // BILLING_RESPONSE_RESULT_OK
                PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
                startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
                break;
            case 1: // BILLING_RESPONSE_RESULT_USER_CANCELED 
                break;
            case 3: // BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE 
                break;
            case 4: // BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE 
                break;
            case 5: // BILLING_RESPONSE_RESULT_DEVELOPER_ERROR 
                break;
            case 6: // BILLING_RESPONSE_RESULT_ERROR 
                break;
            case 7: // BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED 
                break;
            case 8: // BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED 
                break;
            case 9: // BILLING_RESPONSE_RESULT_OK
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode == 1001) {           
            int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
            String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
            String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

            if (resultCode == RESULT_OK) {
                try {
                    JSONObject jo = new JSONObject(purchaseData);
                    String sku = jo.getString("productId");
                    alert("You have bought the " + sku + ". Excellent choice, 
                    adventurer!");
                }
                catch (JSONException e) {
                    alert("Failed to parse purchase data.");
                    e.printStackTrace();
                }
            }
        }
    }

    public void getPurchase(){
        Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
        int response = ownedItems.getInt("RESPONSE_CODE");
        if (response == 0) {
            ArrayList ownedSkus = 
            ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
            ArrayList purchaseDataList = 
            ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
            ArrayList signatureList = 
            ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
            String continuationToken = 
            ownedItems.getString("INAPP_CONTINUATION_TOKEN");

            for (int i = 0; i < purchaseDataList.size(); ++i) {
                String purchaseData = purchaseDataList.get(i);
                String signature = signatureList.get(i);
                String sku = ownedSkus.get(i);

                // do something with this purchase information
                // e.g. display the updated list of products owned by user
            } 

            // if continuationToken != null, call getPurchases again 
            // and pass in the token to retrieve more items
        }
    }

    public void consumeItem(){
        int response = mService.consumePurchase(3, getPackageName(), token);
    }
};<commit_msg>added trivial drive<commit_after>// ref: http://developer.android.com/google/play/billing/billing_integrate.html
package com.baroq.pico.google;

import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.example.android.trivialdrivesample.util.IabHelper;
import com.example.android.trivialdrivesample.util.IabResult;
import com.example.android.trivialdrivesample.util.Inventory;
import com.example.android.trivialdrivesample.util.Purchase;

import com.android.vending.billing.IInAppBillingService;

public class InAppBilling extends CordovaPlugin{
    private static final String publicKey = "<KEY>";
    
    private static final String ACTION_INIT = "iabInit";
    private static final String ACTION_INV = "iabInventory";
    private static final String ACTION_GOODS = "iabGoods";
    private static final String ACTION_BUY = "iabBuy";
    private static final String ACTION_TRANSACT = "iabTransact";
    private static final String ACTION_CONSUME = "iabConsume";

    // The helper object
    IabHelper mHelper;

    // Plugin action handler
    @Override
    public boolean execute(String action, JSONArray data,  CallbackContext callbackContext) {
        // Save the callback id
        // this.callbackId = callbackId;
        boolean result = true;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        pluginResult.setKeepCallback(true);

        if (ACTION_INIT.equals(action)) {
            init(cordova.getActivity(), publicKey, callbackContext);
            callbackContext.sendPluginResult(pluginResult);
        } else if (ACTION_INV.equals(action)){
        } else if (ACTION_GOODS.equals(action)){
        } else if (ACTION_BUY.equals(action)){
        } else if (ACTION_TRANSACT.equals(action)){
        } else if (ACTION_CONSUME.equals(action)){
        }

        return result;
    }

    private void init(Activity activity, String key, CallbackContext callbackContext){
        // Create the helper, passing it our context and the public key to verify signatures with
        Log.d(TAG, "Creating IAB helper.");
        mHelper = new IabHelper(activity, key);

        // enable debug logging (for a production application, you should set this to false).
        mHelper.enableDebugLogging(true);

        // Start setup. This is asynchronous and the specified listener
        // will be called once setup completes.
        Log.d(TAG, "Starting setup.");
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                Log.d(TAG, "Setup finished.");

                if (!result.isSuccess()) {
                    // Oh noes, there was a problem.
                    callbackContext.error("Problem setting up in-app billing: " + result);
                    return;
                }

                // Have we been disposed of in the meantime? If so, quit.
                if (mHelper == null) {
                    callbackContext.error("The billing helper has been disposed");
                    return;
                }

                // IAB is fully set up. Now, let's get an inventory of stuff we own.
                Log.d(TAG, "Setup successful. Querying inventory.");
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        });
    }
};
{'fraction_non_alphanumeric': 0.09058663028649386, 'fraction_numerical': 0.0030013642564802184, 'mean_word_length': 2.4535175879396984, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2640068', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2917, 'n_words': 699}