package com.exemple.evaluateurstb

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.VibrationEffect
import android.os.Vibrator
import android.view.MotionEvent
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private var startTime: Long = 0
    private var fautesMineures = 0
    private var fautesMajeures = 0
    
    private val limitSTB = 2000L // Limite stricte de 2 secondes en millisecondes
    private val handler = Handler(Looper.getMainLooper())

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

        val btnStbTimer = findViewById<Button>(R.id.btn_stb_timer)
        val tvStatus = findViewById<TextView>(R.id.tv_timer_status)
        val tvMineures = findViewById<TextView>(R.id.tv_mineures)
        val tvMajeures = findViewById<TextView>(R.id.tv_majeures)
        val btnAddMineure = findViewById<Button>(R.id.btn_add_mineure)
        val btnAddMajeure = findViewById<Button>(R.id.btn_add_majeure)

        // Tâche pour déclencher la vibration si on dépasse 2 secondes
        val timeoutRunnable = Runnable {
            declencherVibration()
            tvStatus.text = "FAUTE MAJEURE : Fixation > 2 secondes !"
            tvStatus.setTextColor(getColor(android.R.color.holo_red_dark))
            fautesMajeures++
            tvMajeures.text = fautesMajeures.toString()
        }

        // Gestion du bouton tactile
        btnStbTimer.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    // L'instructeur appuie : l'élève commence à regarder dans le STB
                    startTime = System.currentTimeMillis()
                    tvStatus.text = "Observation STB en cours..."
                    tvStatus.setTextColor(getColor(android.R.color.black))
                    // Lance le compte à rebours de 2 secondes
                    handler.postDelayed(timeoutRunnable, limitSTB)
                    true
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                    // L'instructeur relâche : l'élève est revenu à la lentille porteuse
                    handler.removeCallbacks(timeoutRunnable)
                    val duration = System.currentTimeMillis() - startTime
                    
                    if (duration <= limitSTB) {
                        tvStatus.text = "Bon : Coup d'œil de ${duration / 1000.0} sec"
                        tvStatus.setTextColor(getColor(android.R.color.holo_green_dark))
                    }
                    true
                }
                else -> false
            }
        }

        // Boutons manuels pour les autres fautes
        btnAddMineure.setOnClickListener {
            fautesMineures++
            tvMineures.text = fautesMineures.toString()
        }

        btnAddMajeure.setOnClickListener {
            fautesMajeures++
            tvMajeures.text = fautesMajeures.toString()
        }
    }

    private fun declencherVibration() {
        val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        if (vibrator.hasVibrator()) {
            val effect = VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)
            vibrator.vibrate(effect)
        }
    }
}
Android Studio 