Android-ReactiveLocation


Source link: https://github.com/mcharmas/Android-ReactiveLocation

ReactiveLocation library for Android

Small library that wraps Google Play Services API in brilliant RxJava Observables reducing boilerplate to minimum.

Current stable version - 2.0

This version works with Google Play Services 11+ and RxJava 2.+

Artifact name: android-reactive-location2

RxJava1 stable version - 1.0

RxJava1 version:

Artifact name: android-reactive-location

What can you do with that?

  • easily connect to Play Services API
  • obtain last known location
  • subscribe for location updates
  • use location settings API
  • manage geofences
  • geocode location to list of addresses
  • activity recognition
  • use current place API
  • fetch place autocomplete suggestions

How does the API look like?

Simple. All you need is to create ReactiveLocationProvider using your context. All observables are already there. Examples are worth more than 1000 words:

Getting last known location

ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
 locationProvider.getLastKnownLocation()
  .subscribe(new Consumer<Location>() {

@Override

public void call(Location location) {

 doSthImportantWithObtainedLocation(location);

}

  
}
);

Yep, Java 8 is not there yet (and on Android it will take a while) but there is absolutely no Google Play Services LocationClient callbacks hell and there is no clean-up you have to do.

Subscribing for location updates

LocationRequest request = LocationRequest.create() //standard GMS LocationRequest

  .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)

  .setNumUpdates(5)

  .setInterval(100);
  ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
 Subscription subscription = locationProvider.getUpdatedLocation(request)
  .filter(...)
 // you can filter location updates
  .map(...)

 // you can map location to sth different
  .flatMap(...)
// or event flat map
  ...

 // and do everything else that is provided by RxJava
  .subscribe(new Consumer<Location>() {

@Override

public void call(Location location) {

 doSthImportantWithObtainedLocation(location);

}

  
}
);

When you are done (for example in onStop()) remember to unsubscribe.

subscription.unsubscribe();

Subscribing for Activity Recognition

Getting activity recognition is just as simple

ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
 Subscription subscription = locationProvider.getDetectedActivity(0) // detectionIntervalMillis
  .filter(...)
 // you can filter location updates
  .map(...)

 // you can map location to sth different
  .flatMap(...)
// or event flat map
  ...

 // and do everything else that is provided by RxJava
  .subscribe(new Consumer<ActivityRecognitionResult>() {

@Override

public void call(ActivityRecognitionResult detectedActivity) {

 doSthImportantWithObtainedActivity(detectedActivity);

}

  
}
);

Reverse geocode location

Do you need address for location?

Observable<List<Address>> reverseGeocodeObservable = locationProvider
  .getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), MAX_ADDRESSES);
  reverseGeocodeObservable
  .subscribeOn(Schedulers.io())

// use I/O thread to query for addresses
  .observeOn(AndroidSchedulers.mainThread())  // return result in main android thread to manipulate UI
  .subscribe(...);

Geocode location

Do you need address for a text search query?

Observable<List<Address>> geocodeObservable = locationProvider
  .getGeocodeObservable(String userQuery, MAX_ADDRESSES);
  geocodeObservable
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(...);

Managing geofences

For geofence management use addGeofences and removeGeofences methods.

Checking location settings though location settings API

To get LocationSettingsResponse for your LocationRequest check out ReactiveLocationProvider.checkLocationSettings() method. Sample usage can be found in sample project in MainActivity class.

Connecting to Google Play Services API

If you just need managed connection to Play Services API use ReactiveLocationProvider.getGoogleApiClientObservable(). On subscription it will connect to the API. Unsubscription will close the connection.

Creating observable from PendingResult

If you are manually using Google Play Services and you are dealing with PendingResult you can easily transform them to observables with ReactiveLocationProvider.fromPendingResult() method.

Transforming buffers to observable

To transform any buffer to observable and autorelease it on unsubscription use DataBufferObservable.from() method. It will let you easily flatMap such data as PlaceLikelihoodBuffer or AutocompletePredictionBuffer from Places API. For usage example see PlacesActivity sample.

Places API

You can fetch current place or place suggestions using:

  • ReactiveLocationProvider.getCurrentPlace()
  • ReactiveLocationProvider.getPlaceAutocompletePredictions()
  • ReactiveLocationProvider.getPlaceById()

For more info see sample project and PlacesActivity.

Cooler examples

Do you need location with certain accuracy but don't want to wait for it more than 4 sec? No problem.

LocationRequest req = LocationRequest.create()

  .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)

  .setExpirationDuration(TimeUnit.SECONDS.toMillis(LOCATION_TIMEOUT_IN_SECONDS))

  .setInterval(LOCATION_UPDATE_INTERVAL);
  Observable<Location> goodEnoughQuicklyOrNothingObservable = locationProvider.getUpdatedLocation(req)

 .filter(new Func1<Location, Boolean>() {

  @Override

  public Boolean call(Location location) {

return location.getAccuracy() < SUFFICIENT_ACCURACY;

  
}

 
}
)

 .timeout(LOCATION_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS, Observable.just((Location) null), AndroidSchedulers.mainThread())

 .first()

 .observeOn(AndroidSchedulers.mainThread());
  goodEnoughQuicklyOrNothingObservable.subscribe(...);

How to use it?

Library is available in maven central.

Gradle

Just use it as dependency in your build.gradle file along with Google Play Services and RxJava.

dependencies {

  ...
  compile 'pl.charmas.android:android-reactive-location2:2.0@aar'
  compile 'com.google.android.gms:play-services-location:11.0.4' //you can use newer GMS version if you need
  compile 'com.google.android.gms:play-services-places:11.0.4'
  compile 'io.reactivex:rxjava:2.0.5' //you can override RxJava version if you need 
}

Maven

Ensure you have android-maven-plugin version that support aar archives and add following dependency:

<dependency>
  <groupId>pl.charmas.android</groupId>
  <artifactId>android-reactive-location2</artifactId>
  <version>2.0</version>
  <type>aar</type> </dependency>

It may be necessary to add google play services and rxanroid dependency as well.

Sample

Sample usage is available in sample directory.

Places API requires API Key. Before running samples you need to create project on API console and obtain API Key using this guide. Obtained key should be exported as gradle property named: REACTIVE_LOCATION_GMS_API_KEY for example in ~/.gradle/gradle.properties.

References

If you need Google Fit library rxified please take a look at RxFit.

License

Copyright (C) 2015 Micha? Charmas (http://blog.charmas.pl)  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

An indicator with preview support that you can use in the ViewPager. Easy to use.

A simple 1 class reactive extension over the Facebook SDK allowing fully customization and support of the library.

Supports both RxJava 1.X and 2.X

Add multiple TextWatcher on multiple Editext and receive callback at one place.

Gradle plugin to generate config classes for your Android projects.

A reactive layer on top of android palette (easy and hassle-free) using RxJava 2.

Remove boilerplate code for using SharePreference to persist app's config data.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes