11package es.hegocre.scorecounter
22
3- import android.os.Bundle
3+ import android.content.Context
4+ import android.os.*
5+ import android.view.KeyEvent
46import android.view.View
57import androidx.core.content.ContextCompat
68import androidx.databinding.DataBindingUtil
79import androidx.fragment.app.FragmentActivity
810import androidx.preference.PreferenceManager
911import androidx.wear.ambient.AmbientModeSupport
12+ import androidx.wear.input.WearableButtons
1013import es.hegocre.scorecounter.data.Score
1114import es.hegocre.scorecounter.databinding.ActivityMainBinding
1215
@@ -16,6 +19,8 @@ class MainActivity : FragmentActivity(), AmbientModeSupport.AmbientCallbackProvi
1619 private lateinit var ambientController: AmbientModeSupport .AmbientController
1720 private var needsBurnProtect = false
1821
22+ private val buttonsAvailable = mutableListOf (false , false , false )
23+
1924 override fun onCreate (savedInstanceState : Bundle ? ) {
2025 super .onCreate(savedInstanceState)
2126 binding = DataBindingUtil .setContentView(this , R .layout.activity_main)
@@ -30,6 +35,10 @@ class MainActivity : FragmentActivity(), AmbientModeSupport.AmbientCallbackProvi
3035 loadScore(score, binding.add2Layout, binding.sub2Layout)
3136 }
3237
38+ buttonsAvailable[0 ] = WearableButtons .getButtonInfo(this , KeyEvent .KEYCODE_STEM_1 ) != null
39+ buttonsAvailable[1 ] = WearableButtons .getButtonInfo(this , KeyEvent .KEYCODE_STEM_2 ) != null
40+ buttonsAvailable[2 ] = WearableButtons .getButtonInfo(this , KeyEvent .KEYCODE_STEM_3 ) != null
41+
3342 ambientController = AmbientModeSupport .attach(this )
3443 }
3544
@@ -103,6 +112,103 @@ class MainActivity : FragmentActivity(), AmbientModeSupport.AmbientCallbackProvi
103112 }
104113 }
105114
115+ override fun onKeyDown (keyCode : Int , event : KeyEvent ? ): Boolean {
116+ return when (event?.repeatCount ? : 0 ) {
117+ 0 -> {
118+ when (keyCode) {
119+ KeyEvent .KEYCODE_STEM_1 -> {
120+ binding.score1?.inc()
121+ vibrate()
122+ true
123+ }
124+ KeyEvent .KEYCODE_STEM_2 -> {
125+ if (buttonsAvailable[0 ]) binding.score2?.inc()
126+ else binding.score1?.inc()
127+ vibrate()
128+ true
129+ }
130+ KeyEvent .KEYCODE_STEM_3 -> {
131+ if (! (buttonsAvailable[0 ] && buttonsAvailable[1 ])) {
132+ if (buttonsAvailable[0 ] || buttonsAvailable[1 ])
133+ // Button 1 or 2 available
134+ binding.score2?.inc()
135+ else
136+ // No other button available
137+ binding.score1?.inc()
138+ }
139+ vibrate()
140+ true
141+ }
142+ else -> {
143+ super .onKeyDown(keyCode, event)
144+ }
145+ }
146+ }
147+ 1 -> onKeyLongPress(keyCode, event)
148+ else -> super .onKeyDown(keyCode, event)
149+ }
150+ }
151+
152+ override fun onKeyLongPress (keyCode : Int , event : KeyEvent ? ): Boolean {
153+ return when (keyCode) {
154+ KeyEvent .KEYCODE_STEM_1 -> {
155+ binding.score1?.reset()
156+ vibrate()
157+ true
158+ }
159+ KeyEvent .KEYCODE_STEM_2 -> {
160+ if (buttonsAvailable[0 ])
161+ // Button 1 available
162+ binding.score2?.reset()
163+ else
164+ // Buttons 2 and/or 3 available
165+ binding.score1?.reset()
166+
167+ vibrate()
168+ true
169+ }
170+ KeyEvent .KEYCODE_STEM_3 -> {
171+ if (! (buttonsAvailable[0 ] && buttonsAvailable[1 ])) {
172+ if (buttonsAvailable[0 ] || buttonsAvailable[1 ])
173+ // Button 1 or 2 available
174+ binding.score2?.reset()
175+ else
176+ // No other button available
177+ binding.score1?.reset()
178+ }
179+ vibrate()
180+ true
181+ }
182+ else -> {
183+ super .onKeyDown(keyCode, event)
184+ }
185+ }
186+ }
187+
188+ @Suppress(" deprecation" )
189+ private fun Context.vibrate () {
190+ val vibrator =
191+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
192+ val vibratorManager =
193+ getSystemService(Context .VIBRATOR_MANAGER_SERVICE ) as VibratorManager
194+ vibratorManager.defaultVibrator
195+ } else {
196+ getSystemService(Context .VIBRATOR_SERVICE ) as Vibrator
197+ }
198+ if (vibrator.hasVibrator()) {
199+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
200+ vibrator.vibrate(
201+ VibrationEffect .createOneShot(
202+ 250 ,
203+ VibrationEffect .DEFAULT_AMPLITUDE
204+ )
205+ )
206+ } else {
207+ vibrator.vibrate(250 )
208+ }
209+ }
210+ }
211+
106212 companion object {
107213 private const val BURN_IN_OFFSET_PX = 10
108214 }
0 commit comments