<commit_before>package com.benjaminearley.droidbot

import android.app.Activity
import android.os.Bundle
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject

open class ControllerActivity : Activity() {

    private lateinit var joysticksSubject: PublishSubject<Joysticks>
    private lateinit var buttonsSubject: PublishSubject<Buttons>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        joysticksSubject = PublishSubject.create()
        buttonsSubject = PublishSubject.create()
    }

    fun getJoysticks(): Observable<Joysticks> = joysticksSubject
    fun getButtons(): Observable<Buttons> = buttonsSubject

    override fun dispatchGenericMotionEvent(event: MotionEvent?): Boolean {
        if (event != null &&
            event.source != InputDevice.SOURCE_UNKNOWN &&
            InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK &&
            event.action == MotionEvent.ACTION_MOVE) {

            val historySize = event.historySize

            for (i in 0..historySize - 1) {
                processJoystickInput(event, i)
            }

            processJoystickInput(event)
            return true
        }
        return super.dispatchGenericMotionEvent(event)
    }

    private fun processJoystickInput(event: MotionEvent,
                                     historyPos: Int? = null) {

        val mInputDevice = event.device


        joysticksSubject.onNext(
            Joysticks(
                JoystickPosition(
                    getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_X, historyPos),
                    getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Y, historyPos)),
                JoystickPosition(
                    getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RX, historyPos),
                    getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RY, historyPos))))
    }

    private fun getCenteredAxis(event: MotionEvent,
                                device: InputDevice, axis: Int, historyPos: Int?): Float {
        val range = device.getMotionRange(axis, event.source)

        if (range != null) {
            val flat = range.flat

            val value = historyPos?.let {
                event.getHistoricalAxisValue(axis, it)
            } ?: run {
                event.getAxisValue(axis)
            }

            if (Math.abs(value) > flat) {
                return value
            }
        }

        return 0f
    }

    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.repeatCount == 0 && event.action == KeyEvent.ACTION_DOWN) {
            return when (event.keyCode) {
                KeyEvent.KEYCODE_BACK -> {
                    buttonsSubject.onNext(Buttons.BACK)
                    true
                }
                else -> false

            }
        }
        return super.dispatchKeyEvent(event)
    }
}

data class Joysticks(val leftStick: JoystickPosition, val rightStick: JoystickPosition)
data class JoystickPosition(val x: Float, val y: Float)

enum class Buttons {
    X, Y, A, B, LB, RB, BACK, START
}<commit_msg>Removing Device flat usage.<commit_after>package com.benjaminearley.droidbot

import android.app.Activity
import android.os.Bundle
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject

open class ControllerActivity : Activity() {

    private lateinit var joysticksSubject: PublishSubject<Joysticks>
    private lateinit var buttonsSubject: PublishSubject<Buttons>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        joysticksSubject = PublishSubject.create()
        buttonsSubject = PublishSubject.create()
    }

    fun getJoysticks(): Observable<Joysticks> = joysticksSubject
    fun getButtons(): Observable<Buttons> = buttonsSubject

    override fun dispatchGenericMotionEvent(event: MotionEvent?): Boolean {
        if (event != null &&
            event.source != InputDevice.SOURCE_UNKNOWN &&
            InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK &&
            event.action == MotionEvent.ACTION_MOVE) {

            val historySize = event.historySize

            for (i in 0..historySize - 1) {
                processJoystickInput(event, i)
            }

            processJoystickInput(event)
            return true
        }
        return super.dispatchGenericMotionEvent(event)
    }

    private fun processJoystickInput(event: MotionEvent,
                                     historyPos: Int? = null) {
        val mInputDevice = event.device

        joysticksSubject.onNext(
            Joysticks(
                JoystickPosition(
                    getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_X, historyPos),
                    getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Y, historyPos)),
                JoystickPosition(
                    getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RX, historyPos),
                    getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RY, historyPos))))
    }

    private fun getCenteredAxis(event: MotionEvent,
                                device: InputDevice, axis: Int, historyPos: Int?) =
        device.getMotionRange(axis, event.source)?.let {
            historyPos?.let {
                event.getHistoricalAxisValue(axis, it)
            } ?: run {
                event.getAxisValue(axis)
            }
        } ?: run {
            0f
        }

    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.repeatCount == 0 && event.action == KeyEvent.ACTION_DOWN) {
            return when (event.keyCode) {
                KeyEvent.KEYCODE_BACK -> {
                    buttonsSubject.onNext(Buttons.BACK)
                    true
                }
                else -> false

            }
        }
        return super.dispatchKeyEvent(event)
    }
}

data class Joysticks(val leftStick: JoystickPosition, val rightStick: JoystickPosition)
data class JoystickPosition(val x: Float, val y: Float)

enum class Buttons {
    X, Y, A, B, LB, RB, BACK, START
}
{'fraction_non_alphanumeric': 0.08125591669296307, 'fraction_numerical': 0.0012622278321236984, 'mean_word_length': 2.164752870693959, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3872713', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1635, 'n_words': 363}