17
17
18
18
import java .util .LinkedList ;
19
19
20
+ import static net .robotmedia .billing .BillingRequest .*;
21
+
20
22
import net .robotmedia .billing .utils .Compatibility ;
21
23
22
24
import com .android .vending .billing .IMarketBillingService ;
@@ -100,16 +102,16 @@ private void bindMarketBillingService() {
100
102
}
101
103
}
102
104
103
- private void checkBillingSupported () {
105
+ private void checkBillingSupported (int startId ) {
104
106
final String packageName = getPackageName ();
105
- final BillingRequest . CheckBillingSupported request = new BillingRequest . CheckBillingSupported (packageName );
107
+ final CheckBillingSupported request = new CheckBillingSupported (packageName , startId );
106
108
runRequestOrQueue (request );
107
109
}
108
110
109
- private void confirmNotifications (Intent intent ) {
111
+ private void confirmNotifications (Intent intent , int startId ) {
110
112
final String packageName = getPackageName ();
111
113
final String [] notifyIds = intent .getStringArrayExtra (EXTRA_NOTIFY_IDS );
112
- final BillingRequest . ConfirmNotifications request = new BillingRequest . ConfirmNotifications (packageName , notifyIds );
114
+ final ConfirmNotifications request = new ConfirmNotifications (packageName , startId , notifyIds );
113
115
runRequestOrQueue (request );
114
116
}
115
117
@@ -125,11 +127,11 @@ private Action getActionFromIntent(Intent intent) {
125
127
return Action .valueOf (split [split .length - 1 ]);
126
128
}
127
129
128
- private void getPurchaseInformation (Intent intent ) {
130
+ private void getPurchaseInformation (Intent intent , int startId ) {
129
131
final String packageName = getPackageName ();
130
132
final long nonce = intent .getLongExtra (EXTRA_NONCE , 0 );
131
133
final String [] notifyIds = intent .getStringArrayExtra (EXTRA_NOTIFY_IDS );
132
- final BillingRequest . GetPurchaseInformation request = new BillingRequest . GetPurchaseInformation (packageName , notifyIds );
134
+ final GetPurchaseInformation request = new GetPurchaseInformation (packageName , startId , notifyIds );
133
135
request .setNonce (nonce );
134
136
runRequestOrQueue (request );
135
137
}
@@ -155,62 +157,68 @@ public void onServiceDisconnected(ComponentName name) {
155
157
// method will not be called.
156
158
@ Override
157
159
public void onStart (Intent intent , int startId ) {
158
- handleCommand (intent );
160
+ handleCommand (intent , startId );
159
161
}
160
162
161
163
// @Override // Avoid compile errors on pre-2.0
162
164
public int onStartCommand (Intent intent , int flags , int startId ) {
163
- handleCommand (intent );
165
+ handleCommand (intent , startId );
164
166
return Compatibility .START_NOT_STICKY ;
165
167
}
166
168
167
- private void handleCommand (Intent intent ) {
169
+ private void handleCommand (Intent intent , int startId ) {
168
170
final Action action = getActionFromIntent (intent );
169
171
if (action == null ) {
170
172
return ;
171
173
}
172
174
switch (action ) {
173
175
case CHECK_BILLING_SUPPORTED :
174
- checkBillingSupported ();
176
+ checkBillingSupported (startId );
175
177
break ;
176
178
case REQUEST_PURCHASE :
177
- requestPurchase (intent );
179
+ requestPurchase (intent , startId );
178
180
break ;
179
181
case GET_PURCHASE_INFORMATION :
180
- getPurchaseInformation (intent );
182
+ getPurchaseInformation (intent , startId );
181
183
break ;
182
184
case CONFIRM_NOTIFICATIONS :
183
- confirmNotifications (intent );
185
+ confirmNotifications (intent , startId );
184
186
break ;
185
187
case RESTORE_TRANSACTIONS :
186
- restoreTransactions (intent );
188
+ restoreTransactions (intent , startId );
187
189
}
188
190
}
189
191
190
- private void requestPurchase (Intent intent ) {
192
+ private void requestPurchase (Intent intent , int startId ) {
191
193
final String packageName = getPackageName ();
192
194
final String itemId = intent .getStringExtra (EXTRA_ITEM_ID );
193
195
final String developerPayload = intent .getStringExtra (EXTRA_DEVELOPER_PAYLOAD );
194
- final BillingRequest . RequestPurchase request = new BillingRequest . RequestPurchase (packageName , itemId , developerPayload );
196
+ final RequestPurchase request = new RequestPurchase (packageName , startId , itemId , developerPayload );
195
197
runRequestOrQueue (request );
196
198
}
197
199
198
- private void restoreTransactions (Intent intent ) {
200
+ private void restoreTransactions (Intent intent , int startId ) {
199
201
final String packageName = getPackageName ();
200
202
final long nonce = intent .getLongExtra (EXTRA_NONCE , 0 );
201
- final BillingRequest . RestoreTransactions request = new BillingRequest . RestoreTransactions (packageName );
203
+ final RestoreTransactions request = new RestoreTransactions (packageName , startId );
202
204
request .setNonce (nonce );
203
205
runRequestOrQueue (request );
204
206
}
205
207
206
208
private void runPendingRequests () {
207
209
BillingRequest request ;
210
+ int maxId =-1 ;
208
211
while ((request = mPendingRequests .peek ()) != null ) {
209
212
if (mService != null ) {
210
213
runRequest (request );
211
214
mPendingRequests .remove ();
215
+
216
+ if (maxId <request .getStartId ())
217
+ maxId =request .getStartId ();
212
218
}
213
219
}
220
+ if (maxId >=0 )
221
+ stopSelf (maxId );
214
222
}
215
223
216
224
private void runRequest (BillingRequest request ) {
@@ -224,12 +232,12 @@ private void runRequest(BillingRequest request) {
224
232
}
225
233
226
234
private void runRequestOrQueue (BillingRequest request ) {
227
- if (mService == null ) {
228
- mPendingRequests .add (request );
229
- bindMarketBillingService ();
230
- return ;
235
+ mPendingRequests .add (request );
236
+ if (mService == null ) {
237
+ bindMarketBillingService ();
238
+ } else {
239
+ runPendingRequests ();
231
240
}
232
- runRequest (request );
233
241
}
234
242
235
243
}
0 commit comments