use of io.reactivex.Observable in project Android-ReactiveLocation by mcharmas.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lastKnownLocationView = (TextView) findViewById(R.id.last_known_location_view);
updatableLocationView = (TextView) findViewById(R.id.updated_location_view);
addressLocationView = (TextView) findViewById(R.id.address_for_location_view);
currentActivityView = (TextView) findViewById(R.id.activity_recent_view);
locationProvider = new ReactiveLocationProvider(getApplicationContext(), ReactiveLocationProviderConfiguration.builder().setRetryOnConnectionSuspended(true).build());
lastKnownLocationObservable = locationProvider.getLastKnownLocation().observeOn(AndroidSchedulers.mainThread());
final LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setNumUpdates(5).setInterval(100);
locationUpdatesObservable = locationProvider.checkLocationSettings(new LocationSettingsRequest.Builder().addLocationRequest(locationRequest).setAlwaysShow(// Refrence: http://stackoverflow.com/questions/29824408/google-play-services-locationservices-api-new-option-never
true).build()).doOnNext(new Consumer<LocationSettingsResult>() {
@Override
public void accept(LocationSettingsResult locationSettingsResult) {
Status status = locationSettingsResult.getStatus();
if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
try {
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException th) {
Log.e("MainActivity", "Error opening settings activity.", th);
}
}
}
}).flatMap(new Function<LocationSettingsResult, Observable<Location>>() {
@Override
public Observable<Location> apply(LocationSettingsResult locationSettingsResult) {
return locationProvider.getUpdatedLocation(locationRequest);
}
}).observeOn(AndroidSchedulers.mainThread());
addressObservable = locationProvider.getUpdatedLocation(locationRequest).flatMap(new Function<Location, Observable<List<Address>>>() {
@Override
public Observable<List<Address>> apply(Location location) {
return locationProvider.getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), 1);
}
}).map(new Function<List<Address>, Address>() {
@Override
public Address apply(List<Address> addresses) {
return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
}
}).map(new AddressToStringFunc()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
activityObservable = locationProvider.getDetectedActivity(50).observeOn(AndroidSchedulers.mainThread());
}
use of io.reactivex.Observable in project Android-ReactiveLocation by mcharmas.
the class PlacesActivity method onLocationPermissionGranted.
@Override
protected void onLocationPermissionGranted() {
compositeDisposable = new CompositeDisposable();
compositeDisposable.add(reactiveLocationProvider.getCurrentPlace(null).subscribe(new Consumer<PlaceLikelihoodBuffer>() {
@Override
public void accept(PlaceLikelihoodBuffer buffer) {
PlaceLikelihood likelihood = buffer.get(0);
if (likelihood != null) {
currentPlaceView.setText(likelihood.getPlace().getName());
}
buffer.release();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("PlacesActivity", "Error in observable", throwable);
}
}));
Observable<String> queryObservable = RxTextView.textChanges(queryView).map(new Function<CharSequence, String>() {
@Override
public String apply(CharSequence charSequence) {
return charSequence.toString();
}
}).debounce(1, TimeUnit.SECONDS).filter(new Predicate<String>() {
@Override
public boolean test(String s) {
return !TextUtils.isEmpty(s);
}
});
Observable<Location> lastKnownLocationObservable = reactiveLocationProvider.getLastKnownLocation();
Observable<AutocompletePredictionBuffer> suggestionsObservable = Observable.combineLatest(queryObservable, lastKnownLocationObservable, new BiFunction<String, Location, QueryWithCurrentLocation>() {
@Override
public QueryWithCurrentLocation apply(String query, Location currentLocation) {
return new QueryWithCurrentLocation(query, currentLocation);
}
}).flatMap(new Function<QueryWithCurrentLocation, Observable<AutocompletePredictionBuffer>>() {
@Override
public Observable<AutocompletePredictionBuffer> apply(QueryWithCurrentLocation q) {
if (q.location == null)
return Observable.empty();
double latitude = q.location.getLatitude();
double longitude = q.location.getLongitude();
LatLngBounds bounds = new LatLngBounds(new LatLng(latitude - 0.05, longitude - 0.05), new LatLng(latitude + 0.05, longitude + 0.05));
return reactiveLocationProvider.getPlaceAutocompletePredictions(q.query, bounds, null);
}
});
compositeDisposable.add(suggestionsObservable.subscribe(new Consumer<AutocompletePredictionBuffer>() {
@Override
public void accept(AutocompletePredictionBuffer buffer) {
List<AutocompleteInfo> infos = new ArrayList<>();
for (AutocompletePrediction prediction : buffer) {
infos.add(new AutocompleteInfo(prediction.getFullText(null).toString(), prediction.getPlaceId()));
}
buffer.release();
placeSuggestionsList.setAdapter(new ArrayAdapter<>(PlacesActivity.this, android.R.layout.simple_list_item_1, infos));
}
}));
}
use of io.reactivex.Observable in project AntennaPod by AntennaPod.
the class ImportExportPreferencesFragment method exportWithWriter.
private void exportWithWriter(ExportWriter exportWriter, final Uri uri) {
Context context = getActivity();
progressDialog.show();
if (uri == null) {
Observable<File> observable = new ExportWorker(exportWriter, getContext()).exportObservable();
disposable = observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(output -> {
Uri fileUri = FileProvider.getUriForFile(context.getApplicationContext(), context.getString(R.string.provider_authority), output);
showExportSuccessDialog(output.toString(), fileUri);
}, this::showExportErrorDialog, progressDialog::dismiss);
} else {
DocumentFileExportWorker worker = new DocumentFileExportWorker(exportWriter, context, uri);
disposable = worker.exportObservable().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(output -> showExportSuccessDialog(output.getUri().toString(), output.getUri()), this::showExportErrorDialog, progressDialog::dismiss);
}
}
use of io.reactivex.Observable in project sqlbrite by square.
the class BriteDatabaseTest method setUp.
@Before
public void setUp() throws IOException {
Configuration configuration = Configuration.builder(InstrumentationRegistry.getContext()).callback(testDb).name(dbFolder.newFile().getPath()).build();
Factory factory = new FrameworkSQLiteOpenHelperFactory();
SupportSQLiteOpenHelper helper = factory.create(configuration);
real = helper.getWritableDatabase();
SqlBrite.Logger logger = new SqlBrite.Logger() {
@Override
public void log(String message) {
logs.add(message);
}
};
ObservableTransformer<Query, Query> queryTransformer = new ObservableTransformer<Query, Query>() {
@Override
public ObservableSource<Query> apply(Observable<Query> upstream) {
return upstream.takeUntil(killSwitch);
}
};
db = new BriteDatabase(helper, logger, scheduler, queryTransformer);
}
use of io.reactivex.Observable in project RxCache by VictorAlbertos.
the class ProcessorProvidersTest method getSubscriberCompleted.
private TestObserver getSubscriberCompleted(boolean hasCache, final boolean evictCache, boolean detailResponse, Loader loader, boolean useExpiredDataIfLoaderNotAvailable) {
Observable observable;
switch(loader) {
case VALID:
observable = Observable.just(new Mock("message"));
break;
case NULL:
observable = Observable.error(new RuntimeException("No data"));
break;
default:
observable = Observable.create(new ObservableOnSubscribe<Object>() {
@Override
public void subscribe(ObservableEmitter<Object> e) throws Exception {
throw new RuntimeException("error");
}
});
break;
}
ConfigProvider configProvider = new ConfigProvider("mockKey", null, null, detailResponse, true, false, "", "", observable, new EvictProvider(evictCache));
if (hasCache)
twoLayersCacheMock.save("mockKey", "", "", new Mock("message"), configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
processorProvidersUT = new ProcessorProvidersBehaviour(twoLayersCacheMock, useExpiredDataIfLoaderNotAvailable, evictExpiredRecordsPersistence, getDeepCopy, doMigrations);
TestObserver observerMock = processorProvidersUT.getData(configProvider).test();
observerMock.awaitTerminalEvent();
return observerMock;
}
Aggregations