Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.splash5.spa.iab.v3;
- import java.util.ArrayList;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.android.vending.billing.IInAppBillingService;
- import android.app.PendingIntent;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.Message;
- import android.os.RemoteException;
- import android.util.Log;
- @SuppressWarnings("unused")
- public class SpaBilling implements ServiceConnection
- {
- private static final boolean DEBUG_ENABLED = true;
- private static final String TAG = “SpaBilling";
- private static final String BILLING_SERVICE_BIND_INTENT = "com.android.vending.billing.InAppBillingService.BIND";
- public static final String BILLING_RESPONSE_CODE = "RESPONSE_CODE";
- public static final String BILLING_RESPONSE_PURCHASE_DATA = "INAPP_PURCHASE_DATA";
- public static final String BILLING_RESPONSE_SIGNATURE = "INAPP_DATA_SIGNATURE";
- private static final String BILLING_RESPONSE_DETAILS_LIST = "DETAILS_LIST";
- private static final String BILLING_RESPONSE_BUY_INTENT = "BUY_INTENT";
- private static final String BILLING_RESPONSE_ITEM_LIST = "INAPP_PURCHASE_ITEM_LIST";
- private static final String BILLING_RESPONSE_DATA_LIST = "INAPP_PURCHASE_DATA_LIST";
- private static final String BILLING_RESPONSE_SIGNATURE_LIST = "INAPP_DATA_SIGNATURE_LIST";
- private static final String BILLING_RESPONSE_CONTINUATION_TOKEN = "INAPP_CONTINUATION_TOKEN";
- private static final String BILLING_ITEM_ID_LIST = "ITEM_ID_LIST";
- private static final String BILLING_ITEM_TYPE_LIST = "ITEM_TYPE_LIST";
- public static final String BILLING_ITEM_TYPE_INAPP = "inapp";
- public static final String BILLING_ITEM_TYPE_SUBS = "subs";
- public static final int BILLING_RESPONSE_RESULT_OK = 0;
- public static final int BILLING_RESPONSE_RESULT_USER_CANCELED = 1;
- public static final int BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE = 3;
- public static final int BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE = 4;
- public static final int BILLING_RESPONSE_RESULT_DEVELOPER_ERROR = 5;
- public static final int BILLING_RESPONSE_RESULT_ERROR = 6;
- public static final int BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED = 7;
- public static final int BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED = 8;
- public static final int BILLING_RESPONSE_RESULT_UNKNOWN = -1;
- private final String mPackageName;
- private IInAppBillingService mService;
- public SpaBilling(Context context)
- {
- mPackageName = context.getPackageName();
- context.bindService(new Intent(BILLING_SERVICE_BIND_INTENT), this, Context.BIND_AUTO_CREATE);
- Log(Log.DEBUG, "start binding billing service, packagename = " + mPackageName);
- }
- public void RequestItemDetail(ArrayList<String> items, String type, Message message)
- {
- new Thread(new GetItemDetailRunnable(items, type, message)).start();
- }
- public void RequestOwnedItems(String type, Message message)
- {
- new Thread(new GetOwnedItemRunnable(type, message)).start();
- }
- public void RequestPurchaseIntent(String item, String type, String payload, Message message)
- {
- new Thread(new PurchaseItemRunnable(item, type, payload, message)).start();
- }
- public void RequestConsumeItem(String token, Message message)
- {
- new Thread(new ConsumeItemRunnable(token, message)).start();
- }
- /**
- * Check is if billing is supported, don't run this on main thread!
- */
- public int isBillingSupported(String type)
- {
- int ret = BILLING_RESPONSE_RESULT_UNKNOWN;
- try
- {
- ret = (mService.isBillingSupported(3, mPackageName, type));
- }
- catch (RemoteException e)
- {
- Log(Log.ERROR, e.toString());
- }
- return ret;
- }
- private class GetItemDetailRunnable implements Runnable
- {
- private ArrayList<String> mItems;
- private Message mMessage;
- private String mType;
- public GetItemDetailRunnable(ArrayList<String> items, String type, Message message)
- {
- mItems = items;
- mMessage = message;
- mType = type;
- }
- @Override
- public void run()
- {
- int response_code = BILLING_RESPONSE_RESULT_UNKNOWN;
- DetailResult result = new DetailResult();
- result.items = new ArrayList<SkuDetail>();
- try
- {
- final Bundle bundle = new Bundle();
- bundle.putStringArrayList(BILLING_ITEM_ID_LIST, mItems);
- // start getting sku detail from google
- final Bundle items = mService.getSkuDetails(3, mPackageName, mType, bundle);
- if ((response_code = items.getInt(BILLING_RESPONSE_CODE, BILLING_RESPONSE_RESULT_UNKNOWN)) == BILLING_RESPONSE_RESULT_OK)
- {
- final ArrayList<String> list = items.getStringArrayList(BILLING_RESPONSE_DETAILS_LIST);
- for (String sku : list)
- {
- try { result.items.add(new SkuDetail(sku)); }
- catch (JSONException e) {}
- }
- }
- }
- catch (Exception e)
- {
- Log(Log.ERROR, e.toString());
- }
- Log(Log.DEBUG, "response = " + response_code + " got " + result.items.size() + " items detail.");
- if (mMessage != null)
- {
- // keep user's object into our object
- result.obj = mMessage.obj;
- mMessage.arg1 = response_code;
- mMessage.obj = result;
- mMessage.sendToTarget();
- }
- }
- }
- private class GetOwnedItemRunnable implements Runnable
- {
- private String mType;
- private Message mMessage;
- public GetOwnedItemRunnable(String type, Message message)
- {
- mType = type;
- mMessage = message;
- }
- @Override
- public void run()
- {
- int response_code = BILLING_RESPONSE_RESULT_UNKNOWN;
- final OwnedItemResult result = new OwnedItemResult();
- result.items = new ArrayList<OwnedItem>();
- try
- {
- ArrayList<String> item_list = null;
- ArrayList<String> data_list = null;
- ArrayList<String> sign_list = null;
- String continuation_token = null;
- do
- {
- final Bundle owned = mService.getPurchases(3, mPackageName, mType, continuation_token);
- if ((response_code = owned.getInt(BILLING_RESPONSE_CODE)) == BILLING_RESPONSE_RESULT_OK)
- {
- item_list = owned.getStringArrayList(BILLING_RESPONSE_ITEM_LIST);
- data_list = owned.getStringArrayList(BILLING_RESPONSE_DATA_LIST);
- sign_list = owned.getStringArrayList(BILLING_RESPONSE_SIGNATURE_LIST);
- continuation_token = owned.getString(BILLING_RESPONSE_CONTINUATION_TOKEN);
- for (int i = 0; i < data_list.size(); i++)
- {
- try { result.items.add(new OwnedItem(data_list.get(i), sign_list.get(i), item_list.get(i))); }
- catch (JSONException e) {}
- }
- }
- } while (continuation_token != null); // check if user has more items
- }
- catch (Exception e)
- {
- Log(Log.ERROR, e.toString());
- }
- Log(Log.DEBUG, "response = " + response_code + " got " + result.items.size() + " owned items.");
- if (mMessage != null)
- {
- // keep user's object into our object
- result.obj = mMessage.obj;
- mMessage.arg1 = response_code;
- mMessage.obj = result;
- mMessage.sendToTarget();
- }
- }
- }
- private class PurchaseItemRunnable implements Runnable
- {
- private String mItem;
- private String mType;
- private String mPayload;
- private Message mMessage;
- public PurchaseItemRunnable(String item, String type, String payload, Message message)
- {
- mItem = item;
- mType = type;
- mPayload = payload;
- mMessage = message;
- }
- @Override
- public void run()
- {
- int response_code = BILLING_RESPONSE_RESULT_UNKNOWN;
- PurchaseRequest request = new PurchaseRequest();
- try
- {
- final Bundle bundle = mService.getBuyIntent(3, mPackageName, mItem, mType, mPayload);
- if ((response_code = bundle.getInt(BILLING_RESPONSE_CODE)) == BILLING_RESPONSE_RESULT_OK)
- request.intent = bundle.getParcelable(BILLING_RESPONSE_BUY_INTENT);
- }
- catch (Exception e)
- {
- Log(Log.ERROR, e.toString());
- }
- Log(Log.DEBUG, "response = " + response_code + ", productID = " + mItem + ", type = " + mType + ", payload = " + mPayload);
- if (mMessage != null)
- {
- // keep user's object into our object
- request.obj = mMessage.obj;
- mMessage.arg1 = response_code;
- mMessage.obj = request;
- mMessage.sendToTarget();
- }
- }
- }
- private class ConsumeItemRunnable implements Runnable
- {
- private String mToken;
- private Message mMessage;
- private int mDelay;
- public ConsumeItemRunnable(String token, Message message)
- {
- mToken = token;
- mMessage = message;
- mDelay = 1000;
- }
- @Override
- public void run()
- {
- int response_code = BILLING_RESPONSE_RESULT_UNKNOWN;
- try
- {
- response_code = mService.consumePurchase(3, mPackageName, mToken);
- }
- catch (Exception e)
- {
- Log(Log.ERROR, e.toString());
- }
- Log(Log.DEBUG, "response = " + response_code + " consumed token = " + mToken);
- if (mMessage != null)
- {
- // keep user's object into our object
- ConsumeResult result = new ConsumeResult();
- result.token = mToken;
- result.obj = mMessage.obj;
- mMessage.arg1 = response_code;
- mMessage.obj = result;
- if (mDelay > 0)
- mMessage.getTarget().sendMessageDelayed(mMessage, mDelay);
- else
- mMessage.sendToTarget();
- }
- }
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service)
- {
- mService = IInAppBillingService.Stub.asInterface(service);
- Log(Log.DEBUG, "in app billing service connected");
- }
- @Override
- public void onServiceDisconnected(ComponentName name)
- {
- mService = null;
- Log(Log.DEBUG, "in app billing service disconnected");
- }
- public static class OwnedItem
- {
- public String data;
- public String signature;
- public String item;
- private JSONObject mObj;
- public OwnedItem(String data, String signature, String item) throws JSONException
- {
- mObj = new JSONObject(data);
- this.data = data;
- this.signature = signature;
- this.item = item;
- }
- public String orderId() { return mObj.optString("orderId"); }
- public String packageName() { return mObj.optString("packageName"); }
- public String productId() { return mObj.optString("productId"); }
- public long purchaseTime() { return mObj.optLong("purchaseTime"); }
- public int purchaseState() { return mObj.optInt("purchaseState"); }
- public String developerPayload() { return mObj.optString("developerPayload"); }
- public String purchaseToken() { return mObj.optString("purchaseToken"); }
- }
- public static class SkuDetail
- {
- public String sku;
- private JSONObject mObj;
- public SkuDetail(String sku) throws JSONException
- {
- mObj = new JSONObject(sku);
- this.sku = sku;
- }
- public String productId() { return mObj.optString("productId"); }
- public String type() { return mObj.optString("type"); }
- public String price() { return mObj.optString("price"); }
- public String title() { return mObj.optString("title"); }
- public String description() { return mObj.optString("description"); }
- }
- public static class OwnedItemResult
- {
- public ArrayList<OwnedItem> items;
- public Object obj;
- }
- public static class PurchaseRequest
- {
- public PendingIntent intent;
- public Object obj;
- }
- public static class DetailResult
- {
- public ArrayList<SkuDetail> items;
- public Object obj;
- }
- public static class ConsumeResult
- {
- public String token;
- public Object obj;
- }
- private static void Log(int priority, String msg)
- {
- if (DEBUG_ENABLED)
- Log.println(priority, TAG, msg);
- }
- }
Add Comment
Please, Sign In to add comment