summaryrefslogtreecommitdiff
path: root/external/plyer/platforms/android/gyroscope.py
diff options
context:
space:
mode:
authorLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
committerLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
commit9a13903a2f7d3a65fdf15a65fb59cccd622e2066 (patch)
tree9403b7dff39eb5e5d7fa0f79efb69b496add4c4b /external/plyer/platforms/android/gyroscope.py
parent11cc316b74d5f3f283413a33e7693b314741aa4a (diff)
downloadmanachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.gz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.bz2
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.xz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.zip
Initial commit
Diffstat (limited to 'external/plyer/platforms/android/gyroscope.py')
-rw-r--r--external/plyer/platforms/android/gyroscope.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/external/plyer/platforms/android/gyroscope.py b/external/plyer/platforms/android/gyroscope.py
new file mode 100644
index 0000000..58747d7
--- /dev/null
+++ b/external/plyer/platforms/android/gyroscope.py
@@ -0,0 +1,74 @@
+'''
+Android Gyroscope
+---------------------
+'''
+
+from plyer.facades import Gyroscope
+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 GyroscopeSensorListener(PythonJavaClass):
+ __javainterfaces__ = ['android/hardware/SensorEventListener']
+
+ def __init__(self):
+ super(GyroscopeSensorListener, self).__init__()
+ self.SensorManager = cast('android.hardware.SensorManager',
+ activity.getSystemService(Context.SENSOR_SERVICE))
+ self.sensor = self.SensorManager.getDefaultSensor(
+ Sensor.TYPE_GYROSCOPE)
+
+ 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 AndroidGyroscope(Gyroscope):
+ def __init__(self):
+ super(AndroidGyroscope, self).__init__()
+ self.bState = False
+
+ def _enable(self):
+ if (not self.bState):
+ self.listener = GyroscopeSensorListener()
+ self.listener.enable()
+ self.bState = True
+
+ def _disable(self):
+ if (self.bState):
+ self.bState = False
+ self.listener.disable()
+ del self.listener
+
+ def _get_orientation(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 AndroidGyroscope()