Skip to content

Commit a625937

Browse files
committed
Rebind service on remote exception. Closes robotmedia#52
1 parent b9c6abf commit a625937

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

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

+30-13
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ private void runPendingRequests() {
238238
BillingRequest request;
239239
int maxStartId = -1;
240240
while ((request = mPendingRequests.peek()) != null) {
241-
if (mService != null) {
242-
runRequest(request);
241+
if (runIfConnected(request)) {
243242
mPendingRequests.remove();
244243
if (maxStartId < request.getStartId()) {
245244
maxStartId = request.getStartId();
@@ -253,17 +252,35 @@ private void runPendingRequests() {
253252
stopSelf(maxStartId);
254253
}
255254
}
256-
257-
private void runRequest(BillingRequest request) {
258-
try {
259-
final long requestId = request.run(mService);
260-
BillingController.onRequestSent(requestId, request);
261-
} catch (RemoteException e) {
262-
Log.w(this.getClass().getSimpleName(), "Remote billing service crashed");
263-
// TODO: Retry?
264-
}
265-
}
266-
255+
256+
/**
257+
* Called when a remote exception occurs while trying to execute the
258+
* {@link BillingRequest#run(IMarketBillingService)} method.
259+
* @param e the exception
260+
*/
261+
protected void onRemoteException(RemoteException e) {
262+
Log.w(this.getClass().getSimpleName(), "Remote billing service crashed");
263+
mService = null;
264+
}
265+
266+
/**
267+
* Runs the given billing request if the service is already connected.
268+
* @param request the billing request
269+
* @return true if the request ran successfully; false if the service
270+
* is not connected or there was an error when trying to use it
271+
*/
272+
private boolean runIfConnected(BillingRequest request) {
273+
if (mService == null) return false;
274+
try {
275+
final long requestId = request.run(mService);
276+
BillingController.onRequestSent(requestId, request);
277+
return true;
278+
} catch (RemoteException e) {
279+
onRemoteException(e);
280+
}
281+
return false;
282+
}
283+
267284
private void runRequestOrQueue(BillingRequest request) {
268285
mPendingRequests.add(request);
269286
if (mService == null) {

0 commit comments

Comments
 (0)