Chocolade19721309

Untitled

Aug 18th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package com.example.flashlight
  2.  
  3. import android.annotation.SuppressLint
  4. import android.content.Context
  5. import android.graphics.Color
  6. import android.hardware.camera2.CameraAccessException
  7. import android.hardware.camera2.CameraManager
  8. import androidx.appcompat.app.AppCompatActivity
  9. import android.os.Bundle
  10. import android.os.Handler
  11. import android.os.Looper
  12. import android.widget.Button
  13. import android.widget.ImageButton
  14. import android.widget.SeekBar
  15. import android.widget.TextView
  16.  
  17. class MainActivity : AppCompatActivity() {
  18.  
  19. var flashLightStatus: Boolean = false
  20. var flashLightOn = false
  21. var counter: Long = 0
  22. private val handler = Handler(Looper.getMainLooper())
  23.  
  24. @SuppressLint("SetTextI18n", "MissingInflatedId")
  25. override fun onCreate(savedInstanceState: Bundle?) {
  26. super.onCreate(savedInstanceState)
  27. setContentView(R.layout.activity_main)
  28.  
  29. val textView = findViewById<TextView>(R.id.text_view)
  30. val stateTextView = findViewById<TextView>(R.id.textview)
  31. val seek = findViewById<SeekBar>(R.id.seekBar)
  32. val button = findViewById<Button>(R.id.my_button)
  33.  
  34. stateTextView.text = "Current State: " + flashLightOn.toString()
  35.  
  36. // Initially, disable the SeekBar
  37. seek.isEnabled = false
  38.  
  39. seek.setOnSeekBarChangeListener(
  40. object : SeekBar.OnSeekBarChangeListener {
  41. override fun onProgressChanged(
  42. seekBar: SeekBar?,
  43. progress: Int,
  44. fromUser: Boolean
  45. ) {
  46. counter = progress.toLong()
  47. textView.text = counter.toString()
  48. }
  49.  
  50. override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
  51. override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
  52. },
  53. )
  54.  
  55. // Set an OnClickListener for the button
  56. button.setOnClickListener {
  57. stateTextView.text = "Current State: " + flashLightOn.toString()
  58. if (flashLightOn) {
  59. // If the flashlight is on, turn it off and change the button background to "Turn On"
  60. seek.isEnabled = false
  61. flashLightOn = false
  62. handler.removeCallbacksAndMessages(null) // Stop the flashing effect
  63. openFlashLight() // Ensure the flashlight is turned off
  64. } else {
  65. // If the flashlight is off, turn it on and change the button background to "Turn Off"
  66. seek.isEnabled = true
  67. flashLightOn = true
  68. HandleFlashlight() // Start the flashlight logic with the flashing effect
  69. }
  70. }
  71. }
  72.  
  73. override fun onBackPressed() {
  74. finish()
  75. System.exit(0)
  76. }
  77.  
  78. private fun openFlashLight() {
  79. val cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
  80. val cameraId = cameraManager.cameraIdList[0]
  81. if (flashLightOn) {
  82. try {
  83. cameraManager.setTorchMode(cameraId, true)
  84. flashLightStatus = true
  85. }
  86. catch (e: CameraAccessException) {
  87. e.printStackTrace()
  88. }
  89. } else {
  90. try {
  91. cameraManager.setTorchMode(cameraId, false)
  92. flashLightStatus = false
  93. }
  94. catch (e: CameraAccessException) {
  95. e.printStackTrace()
  96. }
  97. }
  98. }
  99.  
  100. private fun HandleFlashlight() {
  101. // Clear any existing callbacks
  102. handler.removeCallbacksAndMessages(null)
  103.  
  104. // Set up the repeating task
  105. handler.postDelayed(object : Runnable {
  106. override fun run() {
  107. if (flashLightOn) {
  108. openFlashLight() // Turn the flashlight on
  109. } else {
  110. openFlashLight() // Turn the flashlight off
  111. }
  112.  
  113. // Toggle the flashlight state for the next cycle
  114. flashLightOn = !flashLightOn
  115.  
  116. // Schedule the next toggle after 'counter' milliseconds
  117. handler.postDelayed(this, counter)
  118. }
  119. }, counter)
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment