<commit_before>'''
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.ac_x = 0
        self.ac_y = 0
        self.ac_z = 0

    def enable(self):
        self.SensorManager.registerListener(self, self.sensor,
                    SensorManager.SENSOR_DELAY_NORMAL)

    def disable(self):
        self.SensorManager.unregisterListener(self, self.sensor)

    @java_method('()I')
    def hashCode(self):
        return id(self)

    @java_method('(Landroid/hardware/SensorEvent;)V')
    def onSensorChanged(self, event):
        self.ac_x = event.values[0]
        self.ac_y = event.values[1]
        self.ac_z = event.values[2]

    @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.listener = AccelerometerSensorListener()

    def _enable(self):
        self.listener.enable()

    def _disable(self):
        self.listener.disable()

    def _get_acceleration(self):
        return (self.listener.ac_x, self.listener.ac_y, self.listener.ac_z) 
        
def instance():
    return AndroidAccelerometer()<commit_msg>Make values assignment atomic<commit_after>'''
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 = [0, 0, 0]

    def enable(self):
        self.SensorManager.registerListener(self, self.sensor,
                    SensorManager.SENSOR_DELAY_NORMAL)

    def disable(self):
        self.SensorManager.unregisterListener(self, self.sensor)

    @java_method('()I')
    def hashCode(self):
        return id(self)

    @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.listener = AccelerometerSensorListener()

    def _enable(self):
        self.listener.enable()

    def _disable(self):
        self.listener.disable()

    def _get_acceleration(self):
        return tuple(self.listener.values)
        
def instance():
    return AndroidAccelerometer()
{'fraction_non_alphanumeric': 0.11292378281927097, 'fraction_numerical': 0.0025490695895997962, 'mean_word_length': 3.1789137380191694, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '963250', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1148, 'n_words': 200}