Created
September 14, 2016 09:25
-
-
Save bearprada/b19170c9e507539d437f2d22609bafeb to your computer and use it in GitHub Desktop.
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 Task<WeatherResult> queryWeather() { | |
final TaskCompletionSource<WeatherResult> task = new TaskCompletionSource<>(); | |
Awareness.SnapshotApi.getWeather(mApiClient) | |
.setResultCallback(new ResultCallback<WeatherResult>() { | |
@Override | |
public void onResult(@NonNull WeatherResult weatherResult) { | |
if (!weatherResult.getStatus().isSuccess()) { | |
task.setError(new IllegalStateException("Could not get weather.")); | |
return; | |
} | |
task.setResult(weatherResult); | |
} | |
}); | |
return task.getTask(); | |
} | |
private Task<List<PlaceLikelihood>> queryPlaces() { | |
final TaskCompletionSource<List<PlaceLikelihood>> task = new TaskCompletionSource<>(); | |
Awareness.SnapshotApi.getPlaces(mApiClient).setResultCallback(new ResultCallback<PlacesResult>() { | |
@Override | |
public void onResult(@NonNull PlacesResult placesResult) { | |
if (!placesResult.getStatus().isSuccess()) { | |
task.setError(new IllegalStateException("Could not get places.")); | |
return; | |
} | |
List<PlaceLikelihood> places = placesResult.getPlaceLikelihoods(); | |
task.setResult(places); | |
} | |
}); | |
return task.getTask(); | |
} | |
private Task<Void> setupGoogleApiClient() { | |
final TaskCompletionSource<Void> task = new TaskCompletionSource<>(); | |
mApiClient = new GoogleApiClient.Builder(this) | |
.addApi(Awareness.API) | |
.enableAutoManage(this, 1, null) | |
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
task.trySetResult(null); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
task.trySetResult(null); | |
} | |
}).build(); | |
return task.getTask(); | |
} | |
public static void main() { | |
// pre-query the location/weather information | |
setupGoogleApiClient().onSuccessTask(new Continuation<Void, Task<WeatherResult>>() { | |
@Override | |
public Task<WeatherResult> then(Task<Void> task) throws Exception { | |
return queryWeather(); | |
} | |
}).onSuccessTask(new Continuation<WeatherResult, Task<List<PlaceLikelihood>>>() { | |
@Override | |
public Task<List<PlaceLikelihood>> then(Task<WeatherResult> task) throws Exception { | |
WeatherResult weather = task.getResult(); // getting the weather information here | |
return queryPlaces(); | |
} | |
}).onSuccess(new Continuation<List<PlaceLikelihood>, Void>() { | |
@Override | |
public Void then(Task<List<PlaceLikelihood>> task) throws Exception { | |
List<PlaceLikelihood> places = task.getResult(); // getting the list of places here | |
return null; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment