Sensor Annotations


Source link: https://github.com/dvoiss/sensorannotations

SensorAnnotations

Annotate methods to use as listeners for sensor events.

public class MyActivity extends Activity {

  /** 
  * Perform actions as accelerometer data changes... 
  */
  @OnSensorChanged(Sensor.TYPE_ACCELEROMETER)
  void accelerometerSensorChanged(@NonNull SensorEvent event) {

doSomething(event.values);

  
}

/** 
  * If the sensor isn't available, update UI accordingly... 
  */
  @OnSensorNotAvailable(Sensor.TYPE_ACCELEROMETER)
  void testTemperatureSensorNotAvailable() {

hideAccelerometerUi();

  
}

 @Override protected void onResume() {

super.onResume();

SensorAnnotations.bind(this);

  
}

@Override protected void onPause() {

super.onPause();

SensorAnnotations.unbind(this);
 // Unbind to save the user's battery life.
  
}
 
}

There are four possible annotations: @OnSensorChanged, @OnAccuracyChanged, @OnSensorNotAvailable, and @OnTrigger. The annotated methods must have the method signatures specified in the Sensors Overview Android docs.

@OnSensorChanged(Sensor.TYPE_HEART_RATE) void method(@NonNull SensorEvent event) {

}
  // or the following syntax can be used which accepts a delay value: @OnSensorChanged(value = Sensor.TYPE_LIGHT, delay = SensorManager.SENSOR_DELAY_NORMAL) void method(@NonNull SensorEvent event) {

}
  @OnAccuracyChanged(Sensor.TYPE_MAGNETIC_FIELD) void method(@NonNull Sensor sensor, int accuracy) {

}
  @OnSensorNotAvailable(Sensor.TYPE_AMBIENT_TEMPERATURE) void method() {

}
  @OnTrigger void method(@NonNull TriggerEvent event) {

}

For information about sensor delays and accuracy events see the "Monitoring Sensor Events" portion of the Android docs.

Calling SensorAnnotations.bind should be done when you want to start receiving sensor events. Because this consumes battery life you need to call unbind when you are finished. The bind method needs to take a Context object. There are two variations:

SensorAnnotations.bind(context);
 // Use this alternative to bind to a different target. See the example application. SensorAnnotations.bind(this, context);

The @OnTrigger annotation is a specific annotation for sensors of TYPE_SIGNIFICANT_MOTION (introduced in 4.3). This type has a different method and parameter than the others. For more info see the Android docs on Using the Significant Motion Sensor.

View the Demo app for usage

How does it work?

A binding class is created for each class that has annotations. In the example app, the classes MainActivity and AccelerometerManager will have two classes generated at compile time: MainActivity$$SensorBinder and AccelerometerManager$$SensorBinder. Because these classes are generated at compile time no reflection is needed.

These classes register the listener with the sensor system service. If the sensor isn't available on the device and a method has been annotated with @OnSensorNotAvailable it will be invoked. If an accuracy event occurs and a method has been annotated with @OnAccuracyChanged it will be invoked. The TYPE_SIGNIFICANT_MOTION sensor doesn't have an accuracy callback.

Use in your project

buildscript {

  dependencies {

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  
}
 
}
  apply plugin: 'com.neenbedankt.android-apt'  dependencies {

  compile 'com.dvoiss:sensorannotations:0.1.0'
  apt 'com.dvoiss:sensorannotations-compiler:0.1.0' 
}

Using Android Gradle Plugin version 2.2.0+:

dependencies {

  compile 'com.dvoiss:sensorannotations:0.1.0'
  annotationProcessor 'com.dvoiss:sensorannotations-compiler:0.1.0' 
}

Resources

An easy to use implementation for fast scroll RecyclerView.

Simple Android app written in Kotlin.

The app uses JSONPlaceholder as a REST API. Communication with API is done using Retrofit together with RxJava2 Adapter. RxJava2 is used for all network related threading. The presentation layer is based on MVP architectural pattern.

Simply accessible and secure shared preferences for the Android platform.

A simple and easy way to create Intro/Welcome screen for your android application.

This library follows Google Material guideline for Android Developers.

Logging helper for the Android platform.

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