Smart Location Library


Source link: https://github.com/mrmans0n/smart-location-lib

Smart Location Library

Android library project that intends to simplify the usage of location providers and activity recognition with a nice fluid API.

Supported Android versions: Android 4.0+

Adding to your project

You should add this to your dependencies:

compile 'io.nlopez.smartlocation:library:3.3.3'

Google Play Services compatible version: 11.4.2

If you want the rxjava wrappers, these are now in a separate dependency. Just add this new dependency as well:

compile 'io.nlopez.smartlocation:rx:3.3.3'

If you got any problem compiling, please check the Common Issues section at the bottom of this document.

Location

Starting

For starting the location service:

SmartLocation.with(context).location()
  .start(new OnLocationUpdatedListener() {
 ... 
}
);

If you just want to get a single location (not periodic) you can just use the oneFix modifier. Example:

SmartLocation.with(context).location()
  .oneFix()
  .start(new OnLocationUpdatedListener() {
 ... 
}
);

Stopping

For stopping the location just use the stop method.

SmartLocation.with(context).location().stop();

Status

You can get some information about the current status of location providers to know if you will be able to use the location providers.

// Check if the location services are enabled SmartLocation.with(context).location().state().locationServicesEnabled();
  // Check if any provider (network or gps) is enabled SmartLocation.with(context).location().state().isAnyProviderAvailable();
  // Check if GPS is available SmartLocation.with(context).location().state().isGpsAvailable();
  // Check if Network is available SmartLocation.with(context).location().state().isNetworkAvailable();
  // Check if the passive provider is available SmartLocation.with(context).location().state().isPassiveAvailable();
  // Check if the location is mocked SmartLocation.with(context).location().state().isMockSettingEnabled();

Location strategy

There are three presets for location parameters:

  • LocationParams.BEST_EFFORT (default)
  • LocationParams.NAVIGATION
  • LocationParams.LAZY

You can change it (if you want one other than the default one) by using the config(locationParams) modifier.

If you want to add some custom parameters for the distances or times involved in the location strategy, you can create your own LocationParams class.

Changing providers

There are some providers shipped with the library.

  • LocationGooglePlayServicesProvider (default). This will use the Fused Location Provider.
  • LocationManagerProvider This is the legacy implementation that uses LocationManager.
  • LocationBasedOnActivityProvider This allows you to use the activity recognition system to modify the location strategy depending on the activity changes (if the user is walking, running, on a car, a bike...).
  • LocationGooglePlayServicesWithFallbackProvider This one will use the Fused Location Provider if it's present, or the LocationManager as fallback if it's not.
  • MultiFallbackLocationProvider This lets you create your own "fallback provider" if the underlying location service is not available. See "Multiple Fallback Provider" below for details.

You can implement your own if you want. That's ideal if you wanted to use a mock one for testing or something like that, or add support to another possible provider.

Example:

SmartLocation.with(context).location(new LocationBasedOnActivityProvider(callback))
  .start(new OnLocationUpdatedListener() {
 ... 
}
);

Multiple Fallback Provider

The MultiFallbackProvider lets you create your own provider that utilizes multiple underlying location services. The provider will use the location services in the order in which they are added to its Builder, which has convenience methods for setting up the Google Play Services provider and the default LocationManager provider. Providers must implement the ServiceLocationProvider interface to enable the fallback behavior. Example:

LocationProvider myProvider = new MyLocationProvider();
 LocationProvider fallbackProvider = new MultiFallbackProvider.Builder()
  .withGooglePlayServicesProvider().withProvider(myProvider).build();

Activity

Starting

For starting the activity recognition service, you should run:

SmartLocation.with(context).activityRecognition()
  .start(new OnActivityUpdatedListener() {
 ... 
}
);

Stopping

For stopping the activity recognition you could use the stop method.

SmartLocation.with(context).activityRecognition().stop();

Geofencing

We can add geofences and receive the information when we enter, exit or dwell in a Geofence. The geofences are defined by a GeofenceModel, and you should use the requestId as a identifier.

We can add and remove geofences with a similar syntax as all the others.

GeofenceModel mestalla = new GeofenceModel.Builder("id_mestalla")
  .setTransition(Geofence.GEOFENCE_TRANSITION_ENTER)
  .setLatitude(39.47453120000001)
  .setLongitude(-0.358065799999963)
  .setRadius(500)
  .build();
  GeofenceModel cuenca = new GeofenceModel.Builder("id_cuenca")
  .setTransition(Geofence.GEOFENCE_TRANSITION_EXIT)
  .setLatitude(40.0703925)
  .setLongitude(-2.1374161999999615)
  .setRadius(2000)
  .build();
  SmartLocation.with(context).geofencing()
  .add(mestalla)
  .add(cuenca)
  .remove("already_existing_geofence_id")
  .start(new OnGeofencingTransitionListener() {
 ... 
}
);

If you want to capture the Geofence transitions without the app running, you can hook up a BroadcastReceiver to the intent action stored in the GeofencingGooglePlayServicesProvider.BROADCAST_INTENT_ACTION constant. The intent will come with the geofence, the location and the type of transition within the bundle.

Geocoding

The library has support for direct geocoding (aka getting a Location object based on a String) and reverse geocoding (getting the Street name based on a Location object).

There are pretty basic calls in the API for both operations separatedly.

Direct geocoding

SmartLocation.with(context).geocoding()
  .direct("Estadi de Mestalla", new OnGeocodingListener() {

@Override

public void onLocationResolved(String name, List<LocationAddress> results) {

 // name is the same you introduced in the parameters of the call

 // results could come empty if there is no match, so please add some checks around that

 // LocationAddress is a wrapper class for Address that has a Location based on its data

 if (results.size() > 0) {

  Location mestallaLocation = results.get(0).getLocation();

  // [...] Do your thing! :D

 
}

}

  
}
);

Reverse geocoding

SmartLocation.with(context).geocoding()
  .reverse(location, new OnReverseGeocodingListener() {

@Override

public onAddressResolved(Location original, List<Address> results) {

 // ...

}

  
}
);

Mixing things up

But we can mix and batch those requests, if needed. Also, you can provide the number of maximum possible matches you want to receive for each one of the lookups separatedly.

Location myLocation1 = new Location(...);
  SmartLocation.with(context).geocoding()
  .add("Estadi de Mestalla", 5)
  .add("Big Ben", 2)
  .add(myLocation1, 4)
  .start(directGeocodingListener, reverseGeocodingListener);

This will launch a new call to the callbacks everytime one of the geofence lookups is resolved.

Stopping

You should invoke the stop method whenever the calling activity/fragment or whatever is going to be destroyed, for cleanup purposes.

RxJava / RxAndroid support

The wrappers to rxjava2 are located in this package.

compile 'io.nlopez.smartlocation:rx:3.3.1'

You can wrap the calls with ObservableFactory methods to retrieve an Observable object. You won't need to call start, just subscribe to the observable to get the updates.

For example, for location:

Observable<Location> locationObservable = ObservableFactory.from(SmartLocation.with(context).location());
 locationObservable.subscribe(new Action1<Location>() {

  @Override
  public void call(Location location) {

// Do your stuff here :)
  
}
 
}
);

Common issues

If you are already using Google Play Services in your project and have problems compiling, you can try setting the transitive property to false:

compile ('io.nlopez.smartlocation:library:3.3.3') {
  transitive = false 
}

If you got an error in the manifest merging, like this one:

> Manifest merger failed : Attribute meta-data#com.google.android.gms.version@value value=(@integer/google_play_services_version) from AndroidManifest.xml:44:13
  is also present at io.nlopez.smartlocation:library:3.0.5:28:13 value=(6587000)
  Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:42:9 to override

 Error:(46, 13) Attribute meta-data#com.google.android.gms.version@value value=(@integer/google_play_services_version) from AndroidManifest.xml:46:13 

If you follow the suggestion provided, you can get rid of it easily. Just change in your manifest the meta-data tag with the google play services version, like this:

<meta-data tools:replace="android:value" android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

Contributing

Forks, patches and other feedback are welcome.

Creators

Nacho López @mrmans0n

License

The MIT License (MIT)

Copyright (c) 2013-2017 Nacho Lopez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Resources

A simple LoadingView for Android with animation.

An example with SideBar component.

A marquee layout library.

The T2 Encryption Library provides FIPS 140-2 compliant data-at-rest security to Android applications.

ExoPlayer's wrapper for using with TextureView.

Easy and quick weather fetching from OpenWeatherMap API for Android.

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