~hokiegeek/qwingo-roller

ref: 6a77655ce0d243f1a13fa15449e55f654bdf353d qwingo-roller/app/src/main/java/net/hokiegeek/qwingoroller/MainActivity.kt -rw-r--r-- 3.7 KiB
6a77655cHokieGeek Adds the loader and finishes the whole shebang for v1 at least 7 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
90
91
92
93
94
95
96
97
98
99
100
101
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.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import kotlin.math.sqrt
import kotlin.random.Random

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 val logTag = "QWINGO"

    private val dieColors = arrayOf(R.color.die_green, R.color.die_blue, R.color.die_pink, R.color.die_purple, R.color.die_yellow, R.color.black)

    private val dieFaces = arrayOf(R.drawable.watermelon, R.drawable.cup, R.drawable.tomato, R.drawable.cone, R.drawable.banana, R.drawable.lightning)

    private var mSensorMgr: SensorManager? = null

    private val mSensorListener = object : SensorEventListener {
        private var mShakeTimestamp: Long = 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
                mShakeTimestamp = now

                Log.d(logTag, "ACTION: shake")
                roll()
            }
        }

        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?

        roll() // initialize the view
    }

    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, "roll!")

        findViewById<ConstraintLayout>(R.id.layout).setBackgroundResource(R.color.loading_background)
        Glide.with(this).load(R.raw.hamster).into(findViewById(R.id.dieFace))

        Handler(Looper.getMainLooper()).postDelayed({
            val selection = Random.nextInt(dieFaces.size)
            findViewById<ImageView>(R.id.dieFace).setImageResource(dieFaces[selection])
            findViewById<ConstraintLayout>(R.id.layout).setBackgroundResource(dieColors[selection])
        }, Random.nextLong(1500) + 800L)
    }
}