下面列出了io.reactivex.SingleTransformer#com.google.android.gms.common.api.Result 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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() {
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;
}
};
}
@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> 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);
}
});
}
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);
}
});
}
@NonNull
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;
}
};
}
/**
* 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);
}
@NonNull
@Override
public Result await() {
if (!canceled && latch != null) {
try {
latch.await();
} catch (InterruptedException e) {
return () -> Canceled;
}
}
return () -> canceled ? Canceled : Success;
}
@NonNull
@Override
public Result await(long l, @NonNull TimeUnit timeUnit) {
if (!canceled && latch != null) {
try {
latch.await(l, timeUnit);
} catch (InterruptedException e) {
return () -> Canceled;
}
}
return () -> canceled ? Canceled : Success;
}
@Override
public void setResultCallback(
@NonNull final ResultCallback<? super Result> resultCallback) {
if (!canceled && latch != null) {
AsyncTask<Object, Object, Void> task = new AsyncTask<Object, Object, Void>() {
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
* <p/>
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param params The parameters of the task.
* @return A result, defined by the subclass of this task.
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
@Override
protected Void doInBackground(Object... params) {
try {
latch.await();
resultCallback.onResult((Result) () -> canceled ? Canceled : Success);
} catch (InterruptedException e) {
resultCallback.onResult((Result) () -> Canceled);
}
return null;
}
};
task.execute(latch);
} else {
resultCallback.onResult((Result) () -> canceled ? Canceled : Success);
}
}
@Override
public void setResultCallback(@NonNull final ResultCallback<? super Result> resultCallback,
final long l, @NonNull final TimeUnit timeUnit) {
if (!canceled && latch != null) {
AsyncTask<Object, Object, Void> task = new AsyncTask<Object, Object, Void>() {
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
* <p/>
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param params The parameters of the task.
* @return A result, defined by the subclass of this task.
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
@Override
protected Void doInBackground(Object... params) {
try {
latch.await(l, timeUnit);
resultCallback.onResult((Result) () -> canceled ? Canceled : Success);
} catch (InterruptedException e) {
resultCallback.onResult((Result) () -> Canceled);
}
return null;
}
};
task.execute(latch);
} else {
resultCallback.onResult((Result) () -> canceled ? Canceled : Success);
}
}
/**
* 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 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) {
// 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<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");
}
}
@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 void cancel() {
if (listener != null) {
listener.onResult(new Result() {
@Override
public Status getStatus() {
return new Status(CommonStatusCodes.CANCELED);
}
});
}
innerResult.cancel();
}
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);
}
}
/**
* Results handler for the asynchronous work methods in this class. Triages the Result and then
* passes it up to the appropriate callback based on the type.
* @param result the returned result.
*/
@Override
public void onResult(Result result) {
if (result instanceof DriveApi.DriveContentsResult) {
callback.onFioResult((DriveApi.DriveContentsResult) result);
}
if (result instanceof DriveFolder.DriveFileResult) {
callback.onFileCreatedResult((DriveFolder.DriveFileResult) result);
}
}
/**
* Returns a task that will complete when given file is closed. Returns immediately if the
* file is not open.
*
* @param filename - the file name in question.
*/
public Task<Result> waitForClosed(String filename) {
final TaskCompletionSource<Result> taskCompletionSource = new TaskCompletionSource<>();
final CountDownLatch latch;
synchronized (this) {
latch = opened.get(filename);
}
if (latch == null) {
taskCompletionSource.setResult(null);
return taskCompletionSource.getTask();
}
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
Result result = new CountDownTask(latch).await();
taskCompletionSource.setResult(result);
return null;
}
}.execute();
return taskCompletionSource.getTask();
}
@Override
public void setResultCallback(
@NonNull final ResultCallback<? super Result> resultCallback) {
if (!canceled && latch != null) {
AsyncTask<Object, Object, Void> task = new AsyncTask<Object, Object, Void>() {
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
* <p/>
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param params The parameters of the task.
* @return A result, defined by the subclass of this task.
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
@Override
protected Void doInBackground(Object... params) {
try {
latch.await();
resultCallback.onResult(new Result() {
@Override
public com.google.android.gms.common.api.Status getStatus() {
return canceled ? Canceled : Success;
}
});
} catch (InterruptedException e) {
resultCallback.onResult(new Result() {
@Override
public com.google.android.gms.common.api.Status getStatus() {
return Canceled;
}
});
}
return null;
}
};
task.execute(latch);
} else {
resultCallback.onResult(new Result() {
@Override
public com.google.android.gms.common.api.Status getStatus() {
return canceled ? Canceled : Success;
}
});
}
}
@Override
public void setResultCallback(@NonNull final ResultCallback<? super Result> resultCallback,
final long l, @NonNull final TimeUnit timeUnit) {
if (!canceled && latch != null) {
AsyncTask<Object, Object, Void> task = new AsyncTask<Object, Object, Void>() {
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
* <p/>
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param params The parameters of the task.
* @return A result, defined by the subclass of this task.
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
@Override
protected Void doInBackground(Object... params) {
try {
latch.await(l, timeUnit);
resultCallback.onResult(new Result() {
@Override
public com.google.android.gms.common.api.Status getStatus() {
return canceled ? Canceled : Success;
}
});
} catch (InterruptedException e) {
resultCallback.onResult(new Result() {
@Override
public com.google.android.gms.common.api.Status getStatus() {
return Canceled;
}
});
}
return null;
}
};
task.execute(latch);
} else {
resultCallback.onResult(new Result() {
@Override
public com.google.android.gms.common.api.Status getStatus() {
return canceled ? Canceled : Success;
}
});
}
}
public StatusException(Result result) {
super(result.getStatus().toString());
this.result = result;
}
public Result getResult() {
return result;
}
static <T extends Result, R> ResultCallback<T> get(@NonNull SingleEmitter<R> emitter, @NonNull Function<T, R> mapper) {
return new SingleResultCallBack<>(emitter, mapper);
}
static <T extends Result> ResultCallback<T> get(@NonNull SingleEmitter<T> emitter) {
//noinspection unchecked
return new SingleResultCallBack<>(emitter, ID_FUNC);
}
public static <C extends ApiConnection, R extends Result> PendingResult<R> call(GoogleApiClient client, Api api, GmsConnector.Callback<C, R> callback) {
return new GmsConnector<C, R>(client, api, callback).connect();
}