Java Code Examples for com.google.android.gms.common.api.ResultCallback#onResult()

The following examples show how to use com.google.android.gms.common.api.ResultCallback#onResult() . 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 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 2
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 3
Source File: StorableFenceManagerTest.java    From JCVD with MIT License 5 votes vote down vote up
@Test
public void testAddSucceed() {
    StorableFence fence = StorableHeadphoneFence.during(HeadphoneState.PLUGGED_IN);
    mManager.addFence("fenceId", fence, "");

    // when the gapi is connected, the fence should be added to the gapi
    // nothing in the stores should change
    assertThat(mAddedCalls, is(0));
    assertThat(mRemovedCalls, is(0));
    assertThat(mMockGapiFenceManager.addResultDict.containsKey("fenceId"), is(true));
    assertThat(mManager.mToAddStore.getAllFences().size(), is(1));
    assertThat(mManager.mToAddStore.getAllFences().get(0), is(fence));
    assertThat(mManager.mSyncedStore.getAllFences(), empty());
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(nullValue()));

    // when the fence is really added to the gapi, the fence should be placed in the syncedStore
    // and removed from the toAddStore
    // and the listener should also be called
    ResultCallback<Status> result = mMockGapiFenceManager.addResultDict.get("fenceId");
    result.onResult(new Status(CommonStatusCodes.SUCCESS));
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(0));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences().size(), is(1));
    assertThat(mManager.mSyncedStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(fence));
}
 
Example 4
Source File: StorableFenceManagerTest.java    From JCVD with MIT License 5 votes vote down vote up
@Test
public void testAddFails() {
    StorableFence fence = StorableHeadphoneFence.unplugging();
    mManager.addFence("fenceId", fence, "");

    // when the gapi is connected, the fence should be added to the gapi
    // nothing in the stores should change
    assertThat(mAddedCalls, is(0));
    assertThat(mRemovedCalls, is(0));
    assertThat(mMockGapiFenceManager.addResultDict.containsKey("fenceId"), is(true));
    assertThat(mManager.mToAddStore.getAllFences().size(), is(1));
    assertThat(mManager.mToAddStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mSyncedStore.getAllFences(), empty());
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(nullValue()));

    // when the fence is really added to the gapi, the fence should be placed in the syncedStore
    // and removed from the toAddStore
    // and the listener should also be called
    ResultCallback<Status> result = mMockGapiFenceManager.addResultDict.get("fenceId");
    result.onResult(new Status(CommonStatusCodes.ERROR, "error message"));
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(0));
    assertThat(mManager.mToAddStore.getAllFences().size(), is(1));
    assertThat(mManager.mToAddStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mSyncedStore.getAllFences(), empty());
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(nullValue()));
}
 
Example 5
Source File: StorableFenceManagerTest.java    From JCVD with MIT License 5 votes vote down vote up
@Test
public void testRemoveFailsWhenConnected() {
    // start with an already added fence
    StorableFence fence = StorableTimeFence.inIntervalOfDay(TimeFence.DAY_OF_WEEK_SUNDAY, null, 0, 1);
    mManager.addFence("fenceId", fence, "");
    ResultCallback<Status> addResult = mMockGapiFenceManager.addResultDict.get("fenceId");
    addResult.onResult(new Status(CommonStatusCodes.SUCCESS));
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(0));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences().size(), is(1));
    assertThat(mManager.mSyncedStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(fence));

    mManager.removeFence("fenceId");

    // when the gapi is connected, the fence should be removed from the gapi
    // nothing in the stores should change
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(0));
    assertThat(mMockGapiFenceManager.removeResultDict.containsKey("fenceId"), is(true));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences().size(), is(1));
    assertThat(mManager.mSyncedStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mToRemoveStore.getAllFenceIds().size(), is(1));
    assertThat(mManager.mToRemoveStore.getAllFenceIds().contains("fenceId"), is(true));
    assertThat(mManager.getFence("fenceId"), is(fence));

    ResultCallback<Status> result = mMockGapiFenceManager.removeResultDict.get("fenceId");
    result.onResult(new Status(CommonStatusCodes.ERROR, "error message"));
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(1));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences().size(), is(1));
    assertThat(mManager.mSyncedStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mToRemoveStore.getAllFenceIds().size(), is(1));
    assertThat(mManager.mToRemoveStore.getAllFenceIds().contains("fenceId"), is(true));
    assertThat(mManager.getFence("fenceId"), is(fence));
}
 
Example 6
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;
            }
        });
    }
}
 
Example 7
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 8
Source File: StorableFenceManagerTest.java    From JCVD with MIT License 4 votes vote down vote up
@Test
public void testRemoveSucceed() {
    // start with an already added fence
    StorableFence fence = StorableTimeFence.inDailyInterval(null, 0, 1);
    mManager.addFence("fenceId", fence, "");
    ResultCallback<Status> addResult = mMockGapiFenceManager.addResultDict.get("fenceId");
    addResult.onResult(new Status(CommonStatusCodes.SUCCESS));
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(0));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences().size(), is(1));
    assertThat(mManager.mSyncedStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(fence));

    mManager.removeFence("fenceId");

    // when the gapi is connected, the fence should be removed from the gapi
    // nothing in the stores should change
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(0));
    assertThat(mMockGapiFenceManager.removeResultDict.containsKey("fenceId"), is(true));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences().size(), is(1));
    assertThat(mManager.mSyncedStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mToRemoveStore.getAllFenceIds().size(), is(1));
    assertThat(mManager.mToRemoveStore.getAllFenceIds().contains("fenceId"), is(true));
    assertThat(mManager.getFence("fenceId"), is(fence));

    // when the fence is really removed to the gapi, the fence should be removed from the syncedStore
    // and removed from the toRemoveStore
    // and the listener should also be called
    ResultCallback<Status> result = mMockGapiFenceManager.removeResultDict.get("fenceId");
    result.onResult(new Status(CommonStatusCodes.SUCCESS));
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(1));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences(), empty());
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(nullValue()));
}
 
Example 9
Source File: StorableFenceManagerTest.java    From JCVD with MIT License 4 votes vote down vote up
@Test
public void testSynchronizeAll() {
    // start with multiple already added fences
    StorableFence fence1 = StorableTimeFence.aroundTimeInstant(TimeFence.TIME_INSTANT_SUNRISE, 0, 1);
    mManager.addFence("fenceId1", fence1, "");
    ResultCallback<Status> addResult = mMockGapiFenceManager.addResultDict.get("fenceId1");
    addResult.onResult(new Status(CommonStatusCodes.SUCCESS));
    StorableFence fence2 = StorableTimeFence.inDailyInterval(null, 0, 1);
    mManager.addFence("fenceId2", fence2, "");
    addResult = mMockGapiFenceManager.addResultDict.get("fenceId2");
    addResult.onResult(new Status(CommonStatusCodes.SUCCESS));

    assertThat(mAddedCalls, is(2));
    assertThat(mRemovedCalls, is(0));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences().size(), is(2));
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());

    // disconnect to add non-committed fences
    mManager.removeFence("fenceId1");
    StorableFence fence3 = StorableTimeFence.inInterval(0, 1);
    mManager.addFence("fenceId3", fence3, "");

    // ask to synchronize all
    mManager.synchronizeAllToGoogleApi();

    // when the gapi is connected:
    // 1: existing fences should have been re-submitted (without statuses)
    // 2: perform non-committed actions
    assertThat(mAddedCalls, is(2));
    assertThat(mRemovedCalls, is(0));
    // check that, even if the status are not set, calls to add existing fences is correctly
    // made
    assertThat(mMockGapiFenceManager.addResultDict, allOf(
            hasEntry("fenceId1", null),
            hasEntry("fenceId2", null)));
    // check that non-committed remove and add have been called
    assertThat(mMockGapiFenceManager.removeResultDict, hasKey("fenceId1"));
    assertThat(mMockGapiFenceManager.addResultDict, hasKey("fenceId3"));
    assertThat(mManager.mToAddStore.getAllFences(), hasSize(1));
    assertThat(mManager.mToAddStore.getAllFences(), contains(fence3));

    assertThat(mManager.mSyncedStore.getAllFences(), hasSize(2));
    assertThat(mManager.mSyncedStore.getAllFences(), containsInAnyOrder(fence1, fence2));

    assertThat(mManager.mToRemoveStore.getAllFenceIds(), hasSize(1));
    assertThat(mManager.mToRemoveStore.getAllFenceIds(), contains("fenceId1"));
    assertThat(mManager.getFence("fenceId1"), is(fence1));
    assertThat(mManager.getFence("fenceId2"), is(fence2));
    assertThat(mManager.getFence("fenceId3"), nullValue());

    ResultCallback<Status> result = mMockGapiFenceManager.removeResultDict.get("fenceId1");
    result.onResult(new Status(CommonStatusCodes.SUCCESS));
    result = mMockGapiFenceManager.addResultDict.get("fenceId3");
    result.onResult(new Status(CommonStatusCodes.SUCCESS));
    assertThat(mAddedCalls, is(3));
    assertThat(mRemovedCalls, is(1));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences(), hasSize(2));
    assertThat(mManager.mSyncedStore.getAllFences(), containsInAnyOrder(fence2, fence3));
    assertThat(mManager.mToRemoveStore.getAllFenceIds(), empty());
    assertThat(mManager.getFence("fenceId1"), nullValue());
    assertThat(mManager.getFence("fenceId2"), is(fence2));
    assertThat(mManager.getFence("fenceId3"), is(fence3));
}
 
Example 10
Source File: InstantPendingResult.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
@Override
public void setResultCallback(ResultCallback<R> callback, long time, TimeUnit unit) {
    callback.onResult(value);
}
 
Example 11
Source File: InstantPendingResult.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
@Override
public void setResultCallback(ResultCallback<R> callback) {
    callback.onResult(value);
}