com.google.android.gms.common.api.Result Java Examples

The following examples show how to use com.google.android.gms.common.api.Result. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 6 votes vote down vote up
@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;
        }
    };
}
 
Example #2
Source File: SnapshotCoordinator.java    From android-basic-samples with Apache License 2.0 6 votes vote down vote up
@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;
    }
  };
}
 
Example #3
Source File: StatusExceptionResumeNextTransformer.java    From RxGps with Apache License 2.0 6 votes vote down vote up
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);
        }
    });
}
 
Example #4
Source File: StatusExceptionResumeNextTransformer.java    From RxGps with Apache License 2.0 6 votes vote down vote up
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);
        }
    });
}
 
Example #5
Source File: StatusExceptionResumeNextTransformer.java    From RxGps with Apache License 2.0 6 votes vote down vote up
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);
        }
    });
}
 
Example #6
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 6 votes vote down vote up
@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;
        }
    };
}
 
Example #7
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 6 votes vote down vote up
@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");
    }
}
 
Example #8
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #9
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #10
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #11
Source File: RxLocationBaseOnSubscribe.java    From RxGps with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #12
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public Result await() {
    if (!canceled && latch != null) {
        try {
            latch.await();
        } catch (InterruptedException e) {
            return () -> Canceled;
        }
    }
    return () -> canceled ? Canceled : Success;
}
 
Example #13
Source File: DriveFio.java    From Drive-Database-Sync with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #14
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@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");
    }
}
 
Example #15
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #16
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public void cancel() {
    if (listener != null) {
        listener.onResult(new Result() {

            @Override
            public Status getStatus() {
                return new Status(CommonStatusCodes.CANCELED);
            }
        });
    }
    innerResult.cancel();
}
 
Example #17
Source File: SnapshotCoordinator.java    From android-basic-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #18
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@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");
    }
}
 
Example #19
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #20
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@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");
    }
}
 
Example #21
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@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");
    }
}
 
Example #22
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@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");
    }
}
 
Example #23
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@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");
    }
}
 
Example #24
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 4 votes vote down vote up
@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;
            }
        });
    }
}
 
Example #25
Source File: GmsConnector.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
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();
}
 
Example #26
Source File: SingleResultCallBack.java    From RxGps with Apache License 2.0 4 votes vote down vote up
static <T extends Result> ResultCallback<T> get(@NonNull SingleEmitter<T> emitter) {
    //noinspection unchecked
    return new SingleResultCallBack<>(emitter, ID_FUNC);
}
 
Example #27
Source File: SingleResultCallBack.java    From RxGps with Apache License 2.0 4 votes vote down vote up
static <T extends Result, R> ResultCallback<T> get(@NonNull SingleEmitter<R> emitter, @NonNull Function<T, R> mapper) {
    return new SingleResultCallBack<>(emitter, mapper);
}
 
Example #28
Source File: StatusException.java    From RxGps with Apache License 2.0 4 votes vote down vote up
public Result getResult() {
    return result;
}
 
Example #29
Source File: StatusException.java    From RxGps with Apache License 2.0 4 votes vote down vote up
public StatusException(Result result) {
    super(result.getStatus().toString());
    this.result = result;
}
 
Example #30
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 4 votes vote down vote up
@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;
            }
        });
    }
}