Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active December 17, 2015 07:09
Show Gist options
  • Save ChrisRisner/5571006 to your computer and use it in GitHub Desktop.
Save ChrisRisner/5571006 to your computer and use it in GitHub Desktop.
CountDownLatch Example
private class MyServiceFilter implements ServiceFilter {
@Override
public void handleRequest(final ServiceFilterRequest request, final NextServiceFilterCallback nextServiceFilterCallback,
final ServiceFilterResponseCallback responseCallback) {
nextServiceFilterCallback.onNext(request, new ServiceFilterResponseCallback() {
@Override
public void onResponse(ServiceFilterResponse response, Exception exception) {
StatusLine status = response.getStatus();
int statusCode = status.getStatusCode();
if (statusCode == 401) {
final CountDownLatch latch = new CountDownLatch(1);
//Get the current activity for the context so we can show the login dialog
AuthenticationApplication myApp = (AuthenticationApplication) mContext;
Activity currentActivity = myApp.getCurrentActivity();
mClient.setContext(currentActivity);
currentActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mClient.login(mProvider, new UserAuthenticationCallback() {
@Override
public void onCompleted(MobileServiceUser user, Exception exception,
ServiceFilterResponse response) {
if (exception == null) {
//Update the request object
latch.countDown();
} else {
Log.e(TAG, "User did not login successfully after 401");
}
}
});
}
});
try {
latch.await();
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted exception: " + e.getMessage());
return;
}
nextServiceFilterCallback.onNext(request, responseCallback);
} else {
responseCallback.onResponse(response, exception);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment