~hokiegeek/qwingo-roller

ref: 09584e5208d37143723bc28b1e2caa05c090e56a qwingo-roller/app/src/main/java/net/hokiegeek/qwingoroller/MainActivity.kt -rw-r--r-- 3.1 KiB
09584e52HokieGeek Includes framework for clicks and shaking 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package net.hokiegeek.qwingoroller

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import kotlin.math.sqrt

class MainActivity : AppCompatActivity() {
    companion object {
     /*
     * The gForce that is necessary to register as shake.
     * Must be greater than 1G (one earth gravity unit).
     * You can install "G-Force", by Blake La Pierre
     * from the Google Play Store and run it to see how
     *  many G's it takes to register a shake
     */
        private const val SHAKE_THRESHOLD_GRAVITY = 2.7f
        private const val SHAKE_SLOP_TIME_MS = 500
        // private const val SHAKE_COUNT_RESET_TIME_MS = 3000
    }

    private val logTag = "QWINGO"

    private var mSensorMgr: SensorManager? = null

    private val mSensorListener = object : SensorEventListener {
        private var mShakeTimestamp: Long = 0
        // private var mShakeCount = 0
        override fun onSensorChanged(se: SensorEvent) {
            val gX = se.values[0] / SensorManager.GRAVITY_EARTH
            val gY = se.values[1] / SensorManager.GRAVITY_EARTH
            val gZ = se.values[2] / SensorManager.GRAVITY_EARTH
            // gForce will be close to 1 when there is no movement.
            val gForce: Float = sqrt(gX * gX + gY * gY + gZ * gZ)
            if (gForce > SHAKE_THRESHOLD_GRAVITY) {
                val now = System.currentTimeMillis()
                // ignore shake events too close to each other (500ms)
                if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
                    return
                }
                // reset the shake count after 3 seconds of no shakes
                /*
                if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
                    mShakeCount = 0
                }
                */
                mShakeTimestamp = now
                // mShakeCount++
                Log.d(logTag, "ACTION: shake?")
                roll()
                // mListener!!.onShake(mShakeCount)
            }
        }

        override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
    }

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

        findViewById<ConstraintLayout>(R.id.layout).setOnClickListener {
            Log.d(logTag, "ACTION: click")
            roll()
        }

        mSensorMgr = getSystemService(Context.SENSOR_SERVICE) as SensorManager?
    }

    override fun onResume() {
        super.onResume()
        mSensorMgr?.registerListener(mSensorListener, mSensorMgr!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI)
    }

    override fun onPause() {
        mSensorMgr?.unregisterListener(mSensorListener);
        super.onPause()
    }

    private fun roll() {
        Log.d(logTag, "TODO: roll!")
    }
}