summaryrefslogtreecommitdiff
path: root/external/plyer/platforms/android/accelerometer.py
diff options
context:
space:
mode:
Diffstat (limited to 'external/plyer/platforms/android/accelerometer.py')
-rw-r--r--external/plyer/platforms/android/accelerometer.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/external/plyer/platforms/android/accelerometer.py b/external/plyer/platforms/android/accelerometer.py
new file mode 100644
index 0000000..af07c52
--- /dev/null
+++ b/external/plyer/platforms/android/accelerometer.py
@@ -0,0 +1,74 @@
+'''
+Android accelerometer
+---------------------
+'''
+
+from plyer.facades import Accelerometer
+from jnius import PythonJavaClass, java_method, autoclass, cast
+from plyer.platforms.android import activity
+
+Context = autoclass('android.content.Context')
+Sensor = autoclass('android.hardware.Sensor')
+SensorManager = autoclass('android.hardware.SensorManager')
+
+
+class AccelerometerSensorListener(PythonJavaClass):
+ __javainterfaces__ = ['android/hardware/SensorEventListener']
+
+ def __init__(self):
+ super(AccelerometerSensorListener, self).__init__()
+ self.SensorManager = cast('android.hardware.SensorManager',
+ activity.getSystemService(Context.SENSOR_SERVICE))
+ self.sensor = self.SensorManager.getDefaultSensor(
+ Sensor.TYPE_ACCELEROMETER)
+
+ self.values = [None, None, None]
+
+ def enable(self):
+ self.SensorManager.registerListener(self, self.sensor,
+ SensorManager.SENSOR_DELAY_NORMAL)
+
+ def disable(self):
+ self.SensorManager.unregisterListener(self, self.sensor)
+
+ @java_method('(Landroid/hardware/SensorEvent;)V')
+ def onSensorChanged(self, event):
+ self.values = event.values[:3]
+
+ @java_method('(Landroid/hardware/Sensor;I)V')
+ def onAccuracyChanged(self, sensor, accuracy):
+ # Maybe, do something in future?
+ pass
+
+
+class AndroidAccelerometer(Accelerometer):
+ def __init__(self):
+ super(AndroidAccelerometer, self).__init__()
+ self.bState = False
+
+ def _enable(self):
+ if (not self.bState):
+ self.listener = AccelerometerSensorListener()
+ self.listener.enable()
+ self.bState = True
+
+ def _disable(self):
+ if (self.bState):
+ self.bState = False
+ self.listener.disable()
+ del self.listener
+
+ def _get_acceleration(self):
+ if (self.bState):
+ return tuple(self.listener.values)
+ else:
+ return (None, None, None)
+
+ def __del__(self):
+ if(self.bState):
+ self._disable()
+ super(self.__class__, self).__del__()
+
+
+def instance():
+ return AndroidAccelerometer()