Last active
December 17, 2015 07:09
-
-
Save ChrisRisner/5571006 to your computer and use it in GitHub Desktop.
CountDownLatch Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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