ReactiveAwareness


Source link: https://github.com/Mauin/ReactiveAwareness

ReactiveAwareness: Android Awareness APIs in a reactive manner with RxJava2

ReactiveAwareness lets you use the Android Awareness API with RxJava and without handling all those GoogleApiClients yourself.

Learn more about the Android Awareness APIs at developers.google.com.

This library supports both the Snapshot API for providing polling based information about the users context as well as the Fence API for getting callbacks for specific states (fences) the users context might be in.

Set-up

To use ReactiveAwareness in your project, add the library as a dependency in your build.gradle file:

dependencies {

  compile 'com.mtramin.reactiveawareness2:reactiveawareness:2.0.0' 
}

To use ReactiveAwareness Fences in your project, add the library as a dependency in your build.gradle file:

dependencies {

  compile 'com.mtramin.reactiveawareness2:reactiveawareness-fence:2.0.0' 
}

MinSdk

This libraries minSdkVersion is 15.

Requests regarding beacons need at least API level 18 to run. Make sure at runtime that you have a supported Sdk version for these calls.

Permissions

Depending on your usage of ReactiveAwareness you might also have to declare some permissions in your AndroidManifest.xml. Remember that from Android Marshmallow (API 23) you will have to support runtime permissions for those:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

The Location permission is used for all location based events in the Awareness API.

The Activity Recognition permission is used for recognizing the current activity of the user.

API Keys

As ReactiveAwareness will query Google APIs for the results, you need to provide API Keys in your applications AndroidManifest.xml. For more information about which API Keys you need and how to aquire them, please read the official Google documentation at developers.google.com

Should you call a method that requires an API Key but you don't specify it correctly in your applications Manifest, ReactiveAwareness will throw an Exception.

Using the reactive Snapshot API (ReactiveSnapshot)

All your interactions with the SnapshotAPI through the library will be through the ReactiveSnapshot class.

Obtain an instance by calling:

ReactiveSnapshot.create(context)

Then you can use this instance to reactively retrieve context information about the current state of the device such as the weather at the device location. Be sure to unsubscribe from the Singles as soon as you don't care about their result anymore as this will then also cancel the request with the GoogleApiClient (e.g. in onStop of your Activity).

Simply subscribe to the provided methods (using weather as an example here).

reactiveSnapshot.getWeather()
  .subscribe(

weather -> handleWeather(weather),

throwable -> handleError(throwable)
  );

Using the reactive Fence API (ReactiveFences)

For using reactive fences there are two different options depending on the use case.

To retrieve updates to specific fences while the application is in foreground you can use an ObservableFence which will provide updates to its fences via an Observable.

To get updates to fences while the application is either in the foreground but also when it is in the background you can use a BackgroundFence.

Fences will return you with a boolean value. True indicates that the fence conditions are valid, while false indicates the contrary.

Defining Fences

For more information about how to define AwarenessFences please refer to the documentation on developers.google.com.

ReactiveAwareness supports all Fences that are available in the Fence API.

BackgroundFence

BackgroundFences will also receive updates to fence states when the application is in the background via a BroadcastReceiver. To use BackgroundFences you have to extend the libraries FenceReceiver in your application and implement the onUpdate method which will notify your implementation about state changes in one of your fences.

Please register your implementation of the FenceReceiver in your AndroidManifest.xml. Please also keep the action name and use "ReactiveAwarenessFence" as the action.

<receiver android:name=".ExampleFenceReceiver">
  <intent-filter>

<action android:name="ReactiveAwarenessFence"/>
  </intent-filter> </receiver>

BackgroundFences are named with unique names. This way you can identify which of your fences was updated.

For registering and unregistering use the BackgroundFence class which provides you the necessary methods to do so:

BackgroundFence.register(context, "name_example", fence);
  BackgroundFence.unregister(context, "name_example");

To query which fences are currently registered and to retrieve their current states you can also call the query method.

Fence Data

Fences can have data attached to them. So if you need some more information about a fence other than it's condition being true or false you can attach data to a Fence in form of a Bundle.

Create your fence with a bundle and all callbacks that are delivered to your FenceReceiver implementation will also contain this bundle.

BackgroundFence.registerWithData(this, "name_example", fence, bundle);

ObservableFence

To use an ObservableFence simply create one by calling:

ObservableFence.create(context, fence)
  .subscribe(

 isTrue -> Log.e(TAG, "Observable Fence State: " + isTrue),

 throwable -> handleError(throwable)
  )

As long as you are subscribed to this Observable you will continue to receive status updates when the state of the fence changes. Please remember to unsubscribe from the Observable when appropriate. This will also automatically unregister the fence and will stop further updates.

Dependencies

ReactiveAwareness brings the following dependencies:

  • RxJava (v2.x)
  • Google Play Services (contextmanager and location) which provides the Awareness API
  • Support Annotations to let you know which requests need permissions to successfully run

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

LICENSE

Copyright 2016 Marvin Ramin.

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

RxFunctions is a library to smooth RxJava usage by functionally composing functions.

RxPartialApplication is a library to allow partial application on RxJava function primitives.

RxCurrying is a library to allow currying on RxJava function primitives.

RxActions is a library to smooth RxJava usage by functionally composing actions.

FloatingText is a text widget that can floating above view with animation.

A small, yet full-featured framework that allows building View-based Android applications. Conductor provides a light-weight wrapper around standard Android Views that does just about everything you'd want.

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