Skip to content

Commit 6ca5442

Browse files
author
unknown
committed
Stop billing service when it isn't needed
1 parent 8a47b04 commit 6ca5442

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

AndroidBillingLibrary/src/net/robotmedia/billing/BillingRequest.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public abstract class BillingRequest {
2626

2727
public static class CheckBillingSupported extends BillingRequest {
2828

29-
public CheckBillingSupported(String packageName) {
30-
super(packageName);
29+
public CheckBillingSupported(String packageName,int startId) {
30+
super(packageName,startId);
3131
}
3232

3333
@Override
@@ -48,8 +48,8 @@ public static class ConfirmNotifications extends BillingRequest {
4848

4949
private static final String KEY_NOTIFY_IDS = "NOTIFY_IDS";
5050

51-
public ConfirmNotifications(String packageName, String[] notifyIds) {
52-
super(packageName);
51+
public ConfirmNotifications(String packageName,int startId, String[] notifyIds) {
52+
super(packageName,startId);
5353
this.notifyIds = notifyIds;
5454
}
5555

@@ -70,8 +70,8 @@ public static class GetPurchaseInformation extends BillingRequest {
7070

7171
private static final String KEY_NOTIFY_IDS = "NOTIFY_IDS";
7272

73-
public GetPurchaseInformation(String packageName, String[] notifyIds) {
74-
super(packageName);
73+
public GetPurchaseInformation(String packageName,int startId, String[] notifyIds) {
74+
super(packageName,startId);
7575
this.notifyIds = notifyIds;
7676
}
7777

@@ -97,8 +97,8 @@ public static class RequestPurchase extends BillingRequest {
9797
private static final String KEY_DEVELOPER_PAYLOAD = "DEVELOPER_PAYLOAD";
9898
private static final String KEY_PURCHASE_INTENT = "PURCHASE_INTENT";
9999

100-
public RequestPurchase(String packageName, String itemId, String developerPayload) {
101-
super(packageName);
100+
public RequestPurchase(String packageName, int startId,String itemId, String developerPayload) {
101+
super(packageName,startId);
102102
this.itemId = itemId;
103103
this.developerPayload = developerPayload;
104104
}
@@ -154,8 +154,8 @@ public static ResponseCode valueOf(int index) {
154154
}
155155
public static class RestoreTransactions extends BillingRequest {
156156

157-
public RestoreTransactions(String packageName) {
158-
super(packageName);
157+
public RestoreTransactions(String packageName,int startId) {
158+
super(packageName,startId);
159159
}
160160

161161
@Override
@@ -187,10 +187,12 @@ public void onResponseCode(ResponseCode response) {
187187
public static final long IGNORE_REQUEST_ID = -1;
188188
private String packageName;
189189

190+
private int startId;
190191
private boolean success;
191192
private long nonce;
192-
public BillingRequest(String packageName) {
193+
public BillingRequest(String packageName,int startId) {
193194
this.packageName = packageName;
195+
this.startId=startId;
194196
}
195197

196198
protected void addParams(Bundle request) {
@@ -255,4 +257,8 @@ protected boolean validateResponse(Bundle response) {
255257
return success;
256258
}
257259

260+
public int getStartId() {
261+
return startId;
262+
}
263+
258264
}

AndroidBillingLibrary/src/net/robotmedia/billing/BillingService.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.LinkedList;
1919

20+
import static net.robotmedia.billing.BillingRequest.*;
21+
2022
import net.robotmedia.billing.utils.Compatibility;
2123

2224
import com.android.vending.billing.IMarketBillingService;
@@ -100,16 +102,16 @@ private void bindMarketBillingService() {
100102
}
101103
}
102104

103-
private void checkBillingSupported() {
105+
private void checkBillingSupported(int startId) {
104106
final String packageName = getPackageName();
105-
final BillingRequest.CheckBillingSupported request = new BillingRequest.CheckBillingSupported(packageName);
107+
final CheckBillingSupported request = new CheckBillingSupported(packageName,startId);
106108
runRequestOrQueue(request);
107109
}
108110

109-
private void confirmNotifications(Intent intent) {
111+
private void confirmNotifications(Intent intent,int startId) {
110112
final String packageName = getPackageName();
111113
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);
113115
runRequestOrQueue(request);
114116
}
115117

@@ -125,11 +127,11 @@ private Action getActionFromIntent(Intent intent) {
125127
return Action.valueOf(split[split.length - 1]);
126128
}
127129

128-
private void getPurchaseInformation(Intent intent) {
130+
private void getPurchaseInformation(Intent intent,int startId) {
129131
final String packageName = getPackageName();
130132
final long nonce = intent.getLongExtra(EXTRA_NONCE, 0);
131133
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);
133135
request.setNonce(nonce);
134136
runRequestOrQueue(request);
135137
}
@@ -155,62 +157,68 @@ public void onServiceDisconnected(ComponentName name) {
155157
// method will not be called.
156158
@Override
157159
public void onStart(Intent intent, int startId) {
158-
handleCommand(intent);
160+
handleCommand(intent,startId);
159161
}
160162

161163
// @Override // Avoid compile errors on pre-2.0
162164
public int onStartCommand(Intent intent, int flags, int startId) {
163-
handleCommand(intent);
165+
handleCommand(intent,startId);
164166
return Compatibility.START_NOT_STICKY;
165167
}
166168

167-
private void handleCommand(Intent intent) {
169+
private void handleCommand(Intent intent,int startId) {
168170
final Action action = getActionFromIntent(intent);
169171
if (action == null) {
170172
return;
171173
}
172174
switch (action) {
173175
case CHECK_BILLING_SUPPORTED:
174-
checkBillingSupported();
176+
checkBillingSupported(startId);
175177
break;
176178
case REQUEST_PURCHASE:
177-
requestPurchase(intent);
179+
requestPurchase(intent,startId);
178180
break;
179181
case GET_PURCHASE_INFORMATION:
180-
getPurchaseInformation(intent);
182+
getPurchaseInformation(intent,startId);
181183
break;
182184
case CONFIRM_NOTIFICATIONS:
183-
confirmNotifications(intent);
185+
confirmNotifications(intent,startId);
184186
break;
185187
case RESTORE_TRANSACTIONS:
186-
restoreTransactions(intent);
188+
restoreTransactions(intent,startId);
187189
}
188190
}
189191

190-
private void requestPurchase(Intent intent) {
192+
private void requestPurchase(Intent intent,int startId) {
191193
final String packageName = getPackageName();
192194
final String itemId = intent.getStringExtra(EXTRA_ITEM_ID);
193195
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);
195197
runRequestOrQueue(request);
196198
}
197199

198-
private void restoreTransactions(Intent intent) {
200+
private void restoreTransactions(Intent intent,int startId) {
199201
final String packageName = getPackageName();
200202
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);
202204
request.setNonce(nonce);
203205
runRequestOrQueue(request);
204206
}
205207

206208
private void runPendingRequests() {
207209
BillingRequest request;
210+
int maxId=-1;
208211
while ((request = mPendingRequests.peek()) != null) {
209212
if (mService != null) {
210213
runRequest(request);
211214
mPendingRequests.remove();
215+
216+
if(maxId<request.getStartId())
217+
maxId=request.getStartId();
212218
}
213219
}
220+
if(maxId>=0)
221+
stopSelf(maxId);
214222
}
215223

216224
private void runRequest(BillingRequest request) {
@@ -224,12 +232,12 @@ private void runRequest(BillingRequest request) {
224232
}
225233

226234
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();
231240
}
232-
runRequest(request);
233241
}
234242

235243
}

0 commit comments

Comments
 (0)