use of bolts.Capture in project Parse-SDK-Android by ParsePlatform.
the class LocationNotifier method getCurrentLocationAsync.
/**
* Asynchronously gets the current location of the device.
*
* This will request location updates from the best provider that match the given criteria
* and return the first location received.
*
* You can customize the criteria to meet your specific needs.
* * For higher accuracy, you can set {@link Criteria#setAccuracy(int)}, however result in longer
* times for a fix.
* * For better battery efficiency and faster location fixes, you can set
* {@link Criteria#setPowerRequirement(int)}, however, this will result in lower accuracy.
*
* @param context
* The context used to request location updates.
* @param timeout
* The number of milliseconds to allow before timing out.
* @param criteria
* The application criteria for selecting a location provider.
*
* @see android.location.LocationManager#getBestProvider(android.location.Criteria, boolean)
* @see android.location.LocationManager#requestLocationUpdates(String, long, float, android.location.LocationListener)
*/
/* package */
static Task<Location> getCurrentLocationAsync(Context context, long timeout, Criteria criteria) {
final TaskCompletionSource<Location> tcs = new TaskCompletionSource<>();
final Capture<ScheduledFuture<?>> timeoutFuture = new Capture<ScheduledFuture<?>>();
final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
final LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location == null) {
return;
}
timeoutFuture.get().cancel(true);
tcs.trySetResult(location);
manager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
timeoutFuture.set(ParseExecutors.scheduled().schedule(new Runnable() {
@Override
public void run() {
tcs.trySetError(new ParseException(ParseException.TIMEOUT, "Location fetch timed out."));
manager.removeUpdates(listener);
}
}, timeout, TimeUnit.MILLISECONDS));
String provider = manager.getBestProvider(criteria, true);
if (provider != null) {
manager.requestLocationUpdates(provider, /* minTime */
0, /* minDistance */
0.0f, listener);
}
if (fakeLocation != null) {
listener.onLocationChanged(fakeLocation);
}
return tcs.getTask();
}
use of bolts.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testSubscribeInBackgroundWithCallbackFail.
@Test
public void testSubscribeInBackgroundWithCallbackFail() throws Exception {
ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error");
when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception));
ParseCorePlugins.getInstance().registerPushChannelsController(controller);
ParsePush push = new ParsePush();
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.subscribeInBackground("test", new SaveCallback() {
@Override
public void done(ParseException e) {
exceptionCapture.set(e);
done.release();
}
});
assertSame(exception, exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
verify(controller, times(1)).subscribeInBackground("test");
}
use of bolts.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testSendInBackgroundWithCallbackSuccess.
@Test
public void testSendInBackgroundWithCallbackSuccess() throws Exception {
// Mock controller
ParsePushController controller = mock(ParsePushController.class);
when(controller.sendInBackground(any(ParsePush.State.class), anyString())).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerPushController(controller);
// Make sample ParsePush data and call method
ParsePush push = new ParsePush();
JSONObject data = new JSONObject();
data.put("key", "value");
List<String> channels = new ArrayList<>();
channels.add("test");
channels.add("testAgain");
push.builder.expirationTime((long) 1000).data(data).pushToIOS(true).channelSet(channels);
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.sendInBackground(new SendCallback() {
@Override
public void done(ParseException e) {
exceptionCapture.set(e);
done.release();
}
});
// Make sure controller is executed and state parameter is correct
assertNull(exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString());
ParsePush.State state = stateCaptor.getValue();
assertTrue(state.pushToIOS());
assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE);
assertEquals(2, state.channelSet().size());
assertTrue(state.channelSet().contains("test"));
assertTrue(state.channelSet().contains("testAgain"));
}
use of bolts.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testSubscribeInBackgroundWithCallbackSuccess.
@Test
public void testSubscribeInBackgroundWithCallbackSuccess() throws Exception {
final ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerPushChannelsController(controller);
ParsePush push = new ParsePush();
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.subscribeInBackground("test", new SaveCallback() {
@Override
public void done(ParseException e) {
exceptionCapture.set(e);
done.release();
}
});
assertNull(exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
verify(controller, times(1)).subscribeInBackground("test");
}
use of bolts.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testUnsubscribeInBackgroundWithCallbackSuccess.
@Test
public void testUnsubscribeInBackgroundWithCallbackSuccess() throws Exception {
final ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
when(controller.unsubscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerPushChannelsController(controller);
ParsePush push = new ParsePush();
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.unsubscribeInBackground("test", new SaveCallback() {
@Override
public void done(ParseException e) {
exceptionCapture.set(e);
done.release();
}
});
assertNull(exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
verify(controller, times(1)).unsubscribeInBackground("test");
}
Aggregations