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

Simple View injection framework to be used as a tool for learning compile-time Annotation Processing.

Simple to use library for animating color change for ActionBar, NavigationBar, and status bar. Apply background tinting to the Android system UI when using KitKat translucent modes.

A data-binding Presentation Model (MVVM) framework for the Android platform.

ColorMixer is a widget for choosing a color in HSV color-space.

Conversations is an open source XMPP (formally known as Jabber) client for Android 4.0+ smart phones.

Features:

  • End-to-end encryption with either OTR or openPGP
  • Sending and receiving images
  • Indication when your contact has read your message
  • Intuitive UI that follows Android Design guidelines
  • Pictures / Avatars for your Contacts
  • Syncs with desktop client
  • Conferences (with support for bookmarks)
  • Address book integration
  • Multiple Accounts / unified inbox
  • Very low impact on battery life

Fragment-Switcher is an adapter-based, state-saving fragment container similar to a ViewPager.

It was designed with the NavigationDrawer and Tab patterns in mind but can be used anywhere that you switch between fragments and would like to retain instance state.

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