@NonNull @Override public Result await() { if (!canceled && latch != null) { try { latch.await(); } catch (InterruptedException e) { return new Result() { @Override public Status getStatus() { return Canceled; } }; } } return new Result() { @Override public Status getStatus() { return canceled ? Canceled : Success; } }; }
public static <R extends Result> FlowableTransformer<R, R> forFlowable() { return upstream -> upstream.onErrorResumeNext(throwable -> { if(throwable instanceof StatusException) { StatusException statusException = (StatusException) throwable; if(statusException.getStatus().hasResolution()) { return Flowable.just((R) statusException.getResult()); } else { return Flowable.error(throwable); } } else { return Flowable.error(throwable); } }); }
@Override public PendingResult<DeleteSnapshotResult> delete(GoogleApiClient googleApiClient, final SnapshotMetadata snapshotMetadata) { if (!isAlreadyOpen(snapshotMetadata.getUniqueName()) && !isAlreadyClosing(snapshotMetadata.getUniqueName())) { setIsClosing(snapshotMetadata.getUniqueName()); try { return new CoordinatedPendingResult<>( Games.Snapshots.delete(googleApiClient, snapshotMetadata), new ResultListener() { @Override public void onResult(Result result) { // deleted files are closed. setClosed(snapshotMetadata.getUniqueName()); } }); } catch (RuntimeException e) { setClosed(snapshotMetadata.getUniqueName()); throw e; } } else { throw new IllegalStateException(snapshotMetadata.getUniqueName() + " is either open or is busy"); } }
@NonNull @Override public Result await(long l, @NonNull TimeUnit timeUnit) { if (!canceled && latch != null) { try { latch.await(l, timeUnit); } catch (InterruptedException e) { return new Result() { @Override public Status getStatus() { return Canceled; } }; } } return new Result() { @Override public Status getStatus() { return canceled ? Canceled : Success; } }; }
public static <R extends Result> ObservableTransformer<R, R> forObservable() { return upstream -> upstream.onErrorResumeNext(throwable -> { if(throwable instanceof StatusException) { StatusException statusException = (StatusException) throwable; if(statusException.getStatus().hasResolution()) { return Observable.just((R) statusException.getResult()); } else { return Observable.error(throwable); } } else { return Observable.error(throwable); } }); }
public static <R extends Result> SingleTransformer<R, R> forSingle() { return upstream -> upstream.onErrorResumeNext(throwable -> { if(throwable instanceof StatusException) { StatusException statusException = (StatusException) throwable; if(statusException.getStatus().hasResolution()) { return Single.just((R) statusException.getResult()); } else { return Single.error(throwable); } } else { return Single.error(throwable); } }); }
protected final <T extends Result> void setupLocationPendingResult(PendingResult<T> pendingResult, ResultCallback<T> resultCallback) { if (timeoutTime != null && timeoutUnit != null) { pendingResult.setResultCallback(resultCallback, timeoutTime, timeoutUnit); } else { pendingResult.setResultCallback(resultCallback); } }
@Override public void cancel() { if (listener != null) { listener.onResult(new Result() { @Override public Status getStatus() { return new Status(CommonStatusCodes.CANCELED); } }); } innerResult.cancel(); }
/** * Blocking wait for the given file to be closed. Returns immediately if the * file is not open. * * @param filename - the file name in question. */ public PendingResult<Result> waitForClosed(String filename) { CountDownLatch l; synchronized (this) { l = opened.get(filename); } return new CountDownPendingResult(l); }
@Override public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient, final SnapshotMetadata snapshotMetadata) { // check if the file is already open if (!isAlreadyOpen(snapshotMetadata.getUniqueName())) { setIsOpening(snapshotMetadata.getUniqueName()); try { return new CoordinatedPendingResult<>( Games.Snapshots.open(googleApiClient, snapshotMetadata), new ResultListener() { @Override public void onResult(Result result) { // if open failed, set the file to closed, otherwise, keep it open. if (!result.getStatus().isSuccess()) { Log.d(TAG, "Open was not a success: " + result.getStatus() + " for filename " + snapshotMetadata.getUniqueName()); setClosed(snapshotMetadata.getUniqueName()); } else { Log.d(TAG, "Open was successful: " + snapshotMetadata.getUniqueName()); } } }); } catch (RuntimeException e) { setClosed(snapshotMetadata.getUniqueName()); throw e; } } else { throw new IllegalStateException(snapshotMetadata.getUniqueName() + " is already open"); } }
@Override public PendingResult<CommitSnapshotResult> commitAndClose(GoogleApiClient googleApiClient, final Snapshot snapshot, SnapshotMetadataChange snapshotMetadataChange) { if (isAlreadyOpen(snapshot.getMetadata().getUniqueName()) && !isAlreadyClosing(snapshot.getMetadata().getUniqueName())) { setIsClosing(snapshot.getMetadata().getUniqueName()); try { return new CoordinatedPendingResult<>( Games.Snapshots.commitAndClose(googleApiClient, snapshot, snapshotMetadataChange), new ResultListener() { @Override public void onResult(Result result) { // even if commit and close fails, the file is closed. Log.d(TAG, "CommitAndClose complete, closing " + snapshot.getMetadata().getUniqueName()); setClosed(snapshot.getMetadata().getUniqueName()); } }); } catch (RuntimeException e) { setClosed(snapshot.getMetadata().getUniqueName()); throw e; } } else { throw new IllegalStateException(snapshot.getMetadata().getUniqueName() + " is either closed or is closing"); } }
@Override public PendingResult<OpenSnapshotResult> resolveConflict(GoogleApiClient googleApiClient, String conflictId, final Snapshot snapshot) { if (!isAlreadyOpen(snapshot.getMetadata().getUniqueName()) && !isAlreadyClosing(snapshot.getMetadata().getUniqueName())) { setIsOpening(snapshot.getMetadata().getUniqueName()); try { return new CoordinatedPendingResult<>( Games.Snapshots.resolveConflict(googleApiClient, conflictId, snapshot), new ResultListener() { @Override public void onResult(Result result) { if (!result.getStatus().isSuccess()) { setClosed(snapshot.getMetadata().getUniqueName()); } } }); } catch (RuntimeException e) { setClosed(snapshot.getMetadata().getUniqueName()); throw e; } } else { throw new IllegalStateException(snapshot.getMetadata().getUniqueName() + " is already open or is busy"); } }
@Override public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient, final String filename, boolean createIfNotFound) { // check if the file is already open if (!isAlreadyOpen(filename)) { setIsOpening(filename); try { return new CoordinatedPendingResult<>( Games.Snapshots.open(googleApiClient, filename, createIfNotFound), new ResultListener() { @Override public void onResult(Result result) { // if open failed, set the file to closed, otherwise, keep it open. if (!result.getStatus().isSuccess()) { Log.d(TAG, "Open was not a success: " + result.getStatus() + " for filename " + filename); setClosed(filename); } else { Log.d(TAG, "Open successful: " + filename); } } }); } catch (RuntimeException e) { // catch runtime exceptions here - they should not happen, but they do. // mark the file as closed so it can be attempted to be opened again. setClosed(filename); throw e; } } else { // a more sophisticated solution could attach this operation to a future // that would be triggered by closing the file, but this will at least avoid // corrupting the data with non-resolvable conflicts. throw new IllegalStateException(filename + " is already open"); } }
@Override public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient, final String filename, boolean createIfNotFound, int conflictPolicy) { // check if the file is already open if (!isAlreadyOpen(filename)) { setIsOpening(filename); try { return new CoordinatedPendingResult<>( Games.Snapshots.open(googleApiClient, filename, createIfNotFound, conflictPolicy), new ResultListener() { @Override public void onResult(Result result) { // if open failed, set the file to closed, otherwise, keep it open. if (!result.getStatus().isSuccess()) { Log.d(TAG, "Open was not a success: " + result.getStatus() + " for filename " + filename); setClosed(filename); } else { Log.d(TAG, "Open successful: " + filename); } } }); } catch (RuntimeException e) { setClosed(filename); throw e; } } else { throw new IllegalStateException(filename + " is already open"); } }
@Override public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient, final SnapshotMetadata snapshotMetadata, int conflictPolicy) { // check if the file is already open if (!isAlreadyOpen(snapshotMetadata.getUniqueName())) { setIsOpening(snapshotMetadata.getUniqueName()); try { return new CoordinatedPendingResult<>(Games.Snapshots.open( googleApiClient, snapshotMetadata, conflictPolicy), new ResultListener() { @Override public void onResult(Result result) { // if open failed, set the file to closed, otherwise, keep it open. if (!result.getStatus().isSuccess()) { Log.d(TAG, "Open was not a success: " + result.getStatus() + " for filename " + snapshotMetadata.getUniqueName()); setClosed(snapshotMetadata.getUniqueName()); } else { Log.d(TAG, "Open was successful: " + snapshotMetadata.getUniqueName()); } } }); } catch (RuntimeException e) { setClosed(snapshotMetadata.getUniqueName()); throw e; } } else { throw new IllegalStateException(snapshotMetadata.getUniqueName() + " is already open"); } }