Skip to content

Commit

Permalink
Rebind service on remote exception. Closes robotmedia#52
Browse files Browse the repository at this point in the history
  • Loading branch information
hpique committed Oct 17, 2012
1 parent b9c6abf commit a625937
Showing 1 changed file with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ private void runPendingRequests() {
BillingRequest request;
int maxStartId = -1;
while ((request = mPendingRequests.peek()) != null) {
if (mService != null) {
runRequest(request);
if (runIfConnected(request)) {
mPendingRequests.remove();
if (maxStartId < request.getStartId()) {
maxStartId = request.getStartId();
Expand All @@ -253,17 +252,35 @@ private void runPendingRequests() {
stopSelf(maxStartId);
}
}

private void runRequest(BillingRequest request) {
try {
final long requestId = request.run(mService);
BillingController.onRequestSent(requestId, request);
} catch (RemoteException e) {
Log.w(this.getClass().getSimpleName(), "Remote billing service crashed");
// TODO: Retry?
}
}


/**
* Called when a remote exception occurs while trying to execute the
* {@link BillingRequest#run(IMarketBillingService)} method.
* @param e the exception
*/
protected void onRemoteException(RemoteException e) {
Log.w(this.getClass().getSimpleName(), "Remote billing service crashed");
mService = null;
}

/**
* Runs the given billing request if the service is already connected.
* @param request the billing request
* @return true if the request ran successfully; false if the service
* is not connected or there was an error when trying to use it
*/
private boolean runIfConnected(BillingRequest request) {
if (mService == null) return false;
try {
final long requestId = request.run(mService);
BillingController.onRequestSent(requestId, request);
return true;
} catch (RemoteException e) {
onRemoteException(e);
}
return false;
}

private void runRequestOrQueue(BillingRequest request) {
mPendingRequests.add(request);
if (mService == null) {
Expand Down

0 comments on commit a625937

Please sign in to comment.