From 42b072f5ec1e526f6828cdd014866dd95f96c16b Mon Sep 17 00:00:00 2001 From: Li ZongYing Date: Sun, 14 Jan 2024 12:09:12 +0800 Subject: [PATCH] support KEYCODE_CHANNEL_UP & KEYCODE_CHANNEL_DOWN --- .../java/com/lizongying/mytv/MainActivity.kt | 95 ++++++++++++------- 1 file changed, 60 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index 326466c..4a39d67 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -204,25 +204,54 @@ class MainActivity : FragmentActivity() { dialog.show() } + private fun channelUp() { + if (mainFragment.isHidden) { + prev() + } else { +// if (mainFragment.selectedPosition == 0) { +// mainFragment.setSelectedPosition( +// mainFragment.tvListViewModel.maxNum.size - 1, +// false +// ) +// } + } + } + + private fun channelDown() { + if (mainFragment.isHidden) { + next() + } else { +// if (mainFragment.selectedPosition == mainFragment.tvListViewModel.maxNum.size - 1) { +//// mainFragment.setSelectedPosition(0, false) +// hideMainFragment() +// return false +// } + } + } + + private fun back() { + if (!mainFragmentIsHidden()) { + hideMainFragment() + return + } + + if (doubleBackToExitPressedOnce) { + super.onBackPressed() + return + } + + doubleBackToExitPressedOnce = true + Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show() + + Handler(Looper.getMainLooper()).postDelayed({ + doubleBackToExitPressedOnce = false + }, 2000) + } + override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { when (keyCode) { KeyEvent.KEYCODE_BACK -> { - if (!mainFragmentIsHidden()) { - hideMainFragment() - return true - } - - if (doubleBackToExitPressedOnce) { - super.onBackPressed() - return true - } - - this.doubleBackToExitPressedOnce = true - Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show() - - Handler(Looper.getMainLooper()).postDelayed({ - doubleBackToExitPressedOnce = false - }, 2000) + back() return true } @@ -231,6 +260,11 @@ class MainActivity : FragmentActivity() { return true } + KeyEvent.KEYCODE_HELP -> { + showHelp() + return true + } + KeyEvent.KEYCODE_MENU -> { showHelp() return true @@ -245,28 +279,19 @@ class MainActivity : FragmentActivity() { } KeyEvent.KEYCODE_DPAD_UP -> { - if (mainFragment.isHidden) { - prev() - } else { -// if (mainFragment.selectedPosition == 0) { -// mainFragment.setSelectedPosition( -// mainFragment.tvListViewModel.maxNum.size - 1, -// false -// ) -// } - } + channelUp() } KeyEvent.KEYCODE_DPAD_DOWN -> { - if (mainFragment.isHidden) { - next() - } else { -// if (mainFragment.selectedPosition == mainFragment.tvListViewModel.maxNum.size - 1) { -//// mainFragment.setSelectedPosition(0, false) -// hideMainFragment() -// return false -// } - } + channelDown() + } + + KeyEvent.KEYCODE_CHANNEL_UP -> { + channelUp() + } + + KeyEvent.KEYCODE_CHANNEL_DOWN -> { + channelDown() } KeyEvent.KEYCODE_DPAD_LEFT -> {