Skip to content

Commit

Permalink
Added accelerometer
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek284 committed Apr 27, 2020
1 parent fe56535 commit e863033
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation project(path: ':sensorutil')
}
26 changes: 25 additions & 1 deletion app/src/main/java/com/lib/sensorutilexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@ package com.lib.sensorutilexample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast

class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity() , AccelerometerManager.AccelerometerListener{

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
AccelerometerManager.isSupported(this)
}

override fun onResume() {
super.onResume()
AccelerometerManager.startListening(this)
}

override fun onAccelerationChanged(x: Float, y: Float, z: Float) {
Toast.makeText(this, " Accelerometer $x $y", Toast.LENGTH_SHORT).show();

}

override fun onShake(force: Float) {
}

override fun onDestroy() {
super.onDestroy()
if (AccelerometerManager.isListening) {
AccelerometerManager.stopListening();

Toast.makeText(this, "onDestroy Accelerometer Stopped", Toast.LENGTH_SHORT).show();
}
}
}
200 changes: 200 additions & 0 deletions sensorutil/src/main/java/com/lib/sensorutil/SensorManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.widget.Toast

object AccelerometerManager {
private var context: Context? = null
/**
* Accuracy configuration
*/
private var threshold = 15.0f
private var interval = 200
private var sensor: Sensor? = null
private var sensorManager: SensorManager? = null
// you could use an OrientationListener array instead
// if you plans to use more than one listener
private var listener: AccelerometerListener? = null
/**
* indicates whether or not Accelerometer Sensor is supported
*/
private var supported: Boolean? = null
/**
* Returns true if the manager is listening to orientation changes
*/
/**
* indicates whether or not Accelerometer Sensor is running
*/
var isListening = false


/**
* Unregisters listeners
*/
fun stopListening() {
isListening = false
try {
if (sensorManager != null && sensorEventListener != null) {
sensorManager!!.unregisterListener(sensorEventListener)
}
} catch (e: Exception) {
}
}

/**
* Returns true if at least one Accelerometer sensor is available
*/
fun isSupported(cntxt: Context?): Boolean {
context = cntxt
if (supported == null) {
if (context != null) {
sensorManager =
context!!.getSystemService(Context.SENSOR_SERVICE) as SensorManager
// Get all sensors in device
val sensors =
sensorManager!!.getSensorList(
Sensor.TYPE_ACCELEROMETER
)
supported = sensors.size > 0
} else {
supported = java.lang.Boolean.FALSE
}
}
return supported!!
}

/**
* Configure the listener for shaking
*
* @param threshold minimum acceleration variation for considering shaking
* @param interval minimum interval between to shake events
*/
fun configure(threshold: Int, interval: Int) {
AccelerometerManager.threshold = threshold.toFloat()
AccelerometerManager.interval = interval
}

/**
* Registers a listener and start listening
*
* @param accelerometerListener callback for accelerometer events
*/
fun startListening(accelerometerListener: AccelerometerListener?) {
sensorManager =
context!!.getSystemService(Context.SENSOR_SERVICE) as SensorManager
// Take all sensors in device
val sensors =
sensorManager!!.getSensorList(
Sensor.TYPE_ACCELEROMETER
)
if (sensors.size > 0) {
sensor = sensors[0]
// Register Accelerometer Listener
isListening = sensorManager!!.registerListener(
sensorEventListener, sensor,
SensorManager.SENSOR_DELAY_GAME
)
listener = accelerometerListener
}
}

/**
* Configures threshold and interval
* And registers a listener and start listening
*
* @param accelerometerListener callback for accelerometer events
* @param threshold minimum acceleration variation for considering shaking
* @param interval minimum interval between to shake events
*/
fun startListening(
accelerometerListener: AccelerometerListener?,
threshold: Int,
interval: Int
) {
configure(threshold, interval)
startListening(accelerometerListener)
}

private val sensorEventListener: SensorEventListener? = object : SensorEventListener {
private var now: Long = 0
private var timeDiff: Long = 0
private var lastUpdate: Long = 0
private var lastShake: Long = 0
private var x = 0f
private var y = 0f
private var z = 0f
private var lastX = 0f
private var lastY = 0f
private var lastZ = 0f
private var force = 0f
override fun onAccuracyChanged(
sensor: Sensor,
accuracy: Int
) {
}

override fun onSensorChanged(event: SensorEvent) { // use the event timestamp as reference
// so the manager precision won't depends
// on the AccelerometerListener implementation
// processing time
now = event.timestamp
x = event.values[0]
y = event.values[1]
z = event.values[2]
// if not interesting in shake events
// just remove the whole if then else block
if (lastUpdate == 0L) {
lastUpdate = now
lastShake = now
lastX = x
lastY = y
lastZ = z
Toast.makeText(
context,
"No Motion detected",
Toast.LENGTH_SHORT
).show()
} else {
timeDiff = now - lastUpdate
if (timeDiff > 0) {
force = Math.abs(x + y + z - lastX - lastY - lastZ)
if (java.lang.Float.compare(force, threshold) > 0) {
if (now - lastShake >= interval) { // trigger shake event
listener?.onShake(force)
} else {
Toast.makeText(
context, "No Motion detected",
Toast.LENGTH_SHORT
).show()
}
lastShake = now
}
lastX = x
lastY = y
lastZ = z
lastUpdate = now
} else {
Toast.makeText(
context,
"No Motion detected",
Toast.LENGTH_SHORT
).show()
}
}
// trigger change event
listener?.onAccelerationChanged(x, y, z)
}
}

interface AccelerometerListener {
fun onAccelerationChanged(
x: Float,
y: Float,
z: Float
)

fun onShake(force: Float)
}
}

0 comments on commit e863033

Please sign in to comment.