use of com.google.android.gms.tasks.TaskCompletionSource in project FirebaseUI-Android by firebase.
the class GoogleApiClientTaskHelper method getConnectedGoogleApiClient.
public Task<GoogleApiClient> getConnectedGoogleApiClient() {
final TaskCompletionSource<GoogleApiClient> source = new TaskCompletionSource<>();
if (!mConnectTaskRef.compareAndSet(null, source)) {
// mConnectTaskRef Task was not null, return Task
return mConnectTaskRef.get().getTask();
}
final GoogleApiClient client = mBuilder.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
source.setResult(mClientRef.get());
if (mClientRef.get() != null) {
mClientRef.get().unregisterConnectionCallbacks(this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
source.setException(new IOException("Failed to connect GoogleApiClient: " + connectionResult.getErrorMessage()));
if (mClientRef.get() != null) {
mClientRef.get().unregisterConnectionFailedListener(this);
}
}
}).build();
mClientRef.set(client);
client.connect();
return source.getTask();
}
use of com.google.android.gms.tasks.TaskCompletionSource in project Robot-Scouter by SUPERCILEX.
the class Scouts method build.
public Task<Map<TeamHelper, List<Scout>>> build() {
List<Task<Pair<TeamHelper, List<String>>>> scoutIndicesTasks = new ArrayList<>();
for (TeamHelper helper : mTeamHelpers) {
TaskCompletionSource<Pair<TeamHelper, List<String>>> scoutIndicesTask = new TaskCompletionSource<>();
scoutIndicesTasks.add(scoutIndicesTask.getTask());
getScoutIndicesRef(helper.getTeam().getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
AsyncTaskExecutor.Companion.execute(() -> {
List<String> scoutKeys = new ArrayList<>();
for (DataSnapshot scoutKeyTemplate : snapshot.getChildren()) {
scoutKeys.add(scoutKeyTemplate.getKey());
}
return scoutKeys;
}).addOnSuccessListener(scoutKeys -> scoutIndicesTask.setResult(Pair.create(helper, scoutKeys)));
}
@Override
public void onCancelled(DatabaseError error) {
scoutIndicesTask.setException(error.toException());
FirebaseCrash.report(error.toException());
}
});
}
for (Task<Pair<TeamHelper, List<String>>> scoutKeysTask : scoutIndicesTasks) {
scoutKeysTask.addOnSuccessListener(this).addOnFailureListener(this);
}
Tasks.whenAll(scoutIndicesTasks).addOnSuccessListener(aVoid -> Tasks.whenAll(mScoutMetricsTasks).addOnSuccessListener(aVoid1 -> mScoutsTask.setResult(mScouts)).addOnFailureListener(this)).addOnFailureListener(this);
return mScoutsTask.getTask();
}
Aggregations