package com.monkey2.lib; import android.os.*; import android.app.*; import android.content.*; import java.util.*; import com.android.vending.billing.*; import org.json.*; public class Monkey2IAP extends Monkey2Activity.Delegate implements ServiceConnection{ private static final String TAG = "Monkey2IAP"; private static final int ACTIVITY_RESULT_REQUEST_CODE=101; public static class Product{ public boolean valid; public String title; public String identifier; public String description; public String price; public int type; public boolean owned; public boolean interrupted; Product( String identifier,int type ){ this.identifier=identifier; this.type=type; } } public Monkey2IAP(){ //Log.v( TAG,"Monkey2IAP()" ); _activity=Monkey2Activity.instance(); Intent intent=new Intent( "com.android.vending.billing.InAppBillingService.BIND" ); intent.setPackage( "com.android.vending" ); _activity.bindService( intent,this,Context.BIND_AUTO_CREATE ); } public boolean OpenStoreAsync( Product[] products ){ if( _running ) return false; _products=products; OpenStoreThread thread=new OpenStoreThread(); _running=true; _result=-1; thread.start(); return true; } public boolean BuyProductAsync( Product p ){ if( _running ) return false; _result=-1; try{ Bundle buy=_service.getBuyIntent( 3,_activity.getPackageName(),p.identifier,"inapp","NOP" ); int response=buy.getInt( "RESPONSE_CODE" ); if( response==0 ){ PendingIntent intent=buy.getParcelable( "BUY_INTENT" ); if( intent!=null ){ Integer zero=Integer.valueOf( 0 ); _activity.startIntentSenderForResult( intent.getIntentSender(),ACTIVITY_RESULT_REQUEST_CODE,new Intent(),zero,zero,zero ); _running=true; return true; } } switch( response ){ case 1: _result=1; //cancelled break; case 7: _result=0; //already purchased break; } }catch( IntentSender.SendIntentException ex ){ }catch( RemoteException ex ){ } return true; } public boolean GetOwnedProductsAsync(){ if( _running ) return false; _result=0; return true; } public boolean IsRunning(){ return _running; } public int GetResult(){ return _result; } // ***** PRIVATE ***** Activity _activity; IInAppBillingService _service; Object _mutex=new Object(); boolean _running; int _result=-1; Product[] _products; ArrayList unconsumed=new ArrayList(); Product FindProduct( String id ){ for( int i=0;i<_products.length;++i ){ if( id.equals( _products[i].identifier ) ) return _products[i]; } return null; } class OpenStoreThread extends Thread{ public void run(){ //wait for service to start synchronized( _mutex ){ while( _service==null ){ try{ _mutex.wait(); }catch( InterruptedException ex ){ }catch( IllegalMonitorStateException ex ){ } } } int i0=0; while( i0<_products.length ){ ArrayList list=new ArrayList(); for( int i1=Math.min( i0+20,_products.length );i0