Enhance back button handling in HighScoreEntryActivity and update vibration logic in MainActivity for better compatibility with Android versions. Implement edge-to-edge display adjustments for newer Android versions.

This commit is contained in:
cmclark00 2025-04-01 12:21:28 -04:00
parent af54b735f9
commit 5952cac760
2 changed files with 52 additions and 19 deletions

View file

@ -10,6 +10,7 @@ import com.pixelmintdrop.model.PlayerProgressionManager
import android.graphics.Color
import android.view.KeyEvent
import android.view.InputDevice
import androidx.activity.OnBackPressedCallback
class HighScoreEntryActivity : AppCompatActivity() {
private lateinit var binding: HighScoreEntryBinding
@ -26,6 +27,17 @@ class HighScoreEntryActivity : AppCompatActivity() {
binding = HighScoreEntryBinding.inflate(layoutInflater)
setContentView(binding.root)
// Set up back button handling
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// If they haven't saved yet, consider it a cancel
if (!hasSaved) {
setResult(Activity.RESULT_CANCELED)
}
finish()
}
})
highScoreManager = HighScoreManager(this)
progressionManager = PlayerProgressionManager(this)
@ -87,16 +99,6 @@ class HighScoreEntryActivity : AppCompatActivity() {
return (sources and InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD ||
(sources and InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK
}
// Prevent accidental back button press from causing issues
override fun onBackPressed() {
super.onBackPressed()
// If they haven't saved yet, consider it a cancel
if (!hasSaved) {
setResult(Activity.RESULT_CANCELED)
}
finish()
}
private fun loadThemePreference(): String {
return progressionManager.getSelectedTheme()

View file

@ -6,6 +6,7 @@ import android.os.Build
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.view.View
import android.view.HapticFeedbackConstants
import androidx.appcompat.app.AppCompatActivity
@ -490,6 +491,14 @@ class MainActivity : AppCompatActivity(),
// Enable edge-to-edge display
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Use the WindowInsetsController for Android 11+ edge-to-edge display
window.insetsController?.let { controller ->
controller.hide(android.view.WindowInsets.Type.statusBars() or android.view.WindowInsets.Type.navigationBars())
controller.systemBarsBehavior = android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// For Android 10, use the deprecated method
@Suppress("DEPRECATION")
window.setDecorFitsSystemWindows(false)
}
@ -897,8 +906,17 @@ class MainActivity : AppCompatActivity(),
* Trigger device vibration with predefined effect
*/
private fun vibrate(effectId: Int) {
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(VibrationEffect.createPredefined(effectId))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// For Android 12+ (API 31+)
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
val vibrator = vibratorManager.defaultVibrator
vibrator.vibrate(VibrationEffect.createPredefined(effectId))
} else {
// For older Android versions
@Suppress("DEPRECATION")
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(VibrationEffect.createPredefined(effectId))
}
}
private fun updateMusicToggleUI() {
@ -1202,13 +1220,26 @@ class MainActivity : AppCompatActivity(),
// Set the entire window to be excluded from the system gesture areas
window.decorView.post {
// Create a list of rectangles representing the edges of the screen to exclude from system gestures
val gestureInsets = window.decorView.rootWindowInsets?.systemGestureInsets
if (gestureInsets != null) {
val leftEdge = Rect(0, 0, 50, window.decorView.height)
val rightEdge = Rect(window.decorView.width - 50, 0, window.decorView.width, window.decorView.height)
val bottomEdge = Rect(0, window.decorView.height - 50, window.decorView.width, window.decorView.height)
window.decorView.systemGestureExclusionRects = listOf(leftEdge, rightEdge, bottomEdge)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Use the new API for Android 11+
window.decorView.rootWindowInsets?.let { insets ->
val leftEdge = Rect(0, 0, 50, window.decorView.height)
val rightEdge = Rect(window.decorView.width - 50, 0, window.decorView.width, window.decorView.height)
val bottomEdge = Rect(0, window.decorView.height - 50, window.decorView.width, window.decorView.height)
window.decorView.systemGestureExclusionRects = listOf(leftEdge, rightEdge, bottomEdge)
}
} else {
// Use the deprecated API for Android 10
@Suppress("DEPRECATION")
val gestureInsets = window.decorView.rootWindowInsets?.systemGestureInsets
if (gestureInsets != null) {
val leftEdge = Rect(0, 0, 50, window.decorView.height)
val rightEdge = Rect(window.decorView.width - 50, 0, window.decorView.width, window.decorView.height)
val bottomEdge = Rect(0, window.decorView.height - 50, window.decorView.width, window.decorView.height)
window.decorView.systemGestureExclusionRects = listOf(leftEdge, rightEdge, bottomEdge)
}
}
}
}