Compare commits

...
2 Commits
Author SHA1 Message Date
Li ZongYing 1fd0951f92 channel reversal 2024-01-16 19:04:43 +08:00
Li ZongYing 42b072f5ec support KEYCODE_CHANNEL_UP & KEYCODE_CHANNEL_DOWN 2024-01-14 12:09:12 +08:00
6 changed files with 186 additions and 65 deletions
@@ -1,11 +1,11 @@
package com.lizongying.mytv package com.lizongying.mytv
import android.app.AlertDialog import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageInfo import android.content.pm.PackageInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.Signature import android.content.pm.Signature
import android.content.pm.SigningInfo import android.content.pm.SigningInfo
import android.graphics.Color
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
@@ -16,11 +16,7 @@ import android.view.KeyEvent
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION import android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
import android.view.WindowManager import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import com.lizongying.mytv.models.TVViewModel import com.lizongying.mytv.models.TVViewModel
import java.security.MessageDigest import java.security.MessageDigest
@@ -39,6 +35,9 @@ class MainActivity : FragmentActivity() {
private val handler = Handler() private val handler = Handler()
private val delay: Long = 4000 private val delay: Long = 4000
private lateinit var sharedPref: SharedPreferences
private var channelReversal = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
@@ -57,6 +56,9 @@ class MainActivity : FragmentActivity() {
mainFragment.view?.requestFocus() mainFragment.view?.requestFocus()
} }
gestureDetector = GestureDetector(this, GestureListener()) gestureDetector = GestureDetector(this, GestureListener())
sharedPref = getPreferences(Context.MODE_PRIVATE)
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, false)
} }
fun showInfoFragment(tvViewModel: TVViewModel) { fun showInfoFragment(tvViewModel: TVViewModel) {
@@ -170,59 +172,100 @@ class MainActivity : FragmentActivity() {
} }
} }
fun saveChannelReversal(channelReversal: Boolean) {
with(sharedPref.edit()) {
putBoolean(CHANNEL_REVERSAL, channelReversal)
apply()
}
this.channelReversal = channelReversal
}
private fun showHelp() { private fun showHelp() {
if (!mainFragment.isHidden) {
return
}
val versionName = getPackageInfo().versionName val versionName = getPackageInfo().versionName
val dialogFragment = MyDialogFragment(versionName, channelReversal)
dialogFragment.show(supportFragmentManager, "settings_dialog")
}
val textView = TextView(this) private fun channelUp() {
textView.text = if (mainFragment.isHidden) {
"当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/" if (channelReversal) {
textView.setBackgroundColor(0xFF263238.toInt()) next()
textView.setPadding(20, 50, 20, 20) return
}
prev()
} else {
// if (mainFragment.selectedPosition == 0) {
// mainFragment.setSelectedPosition(
// mainFragment.tvListViewModel.maxNum.size - 1,
// false
// )
// }
}
}
val imageView = ImageView(this) private fun channelDown() {
val drawable = ContextCompat.getDrawable(this, R.drawable.appreciate) if (mainFragment.isHidden) {
imageView.setImageDrawable(drawable) if (channelReversal) {
imageView.setBackgroundColor(Color.WHITE) prev()
return
}
next()
} else {
// if (mainFragment.selectedPosition == mainFragment.tvListViewModel.maxNum.size - 1) {
//// mainFragment.setSelectedPosition(0, false)
// hideMainFragment()
// return false
// }
}
}
val linearLayout = LinearLayout(this) private fun back() {
linearLayout.orientation = LinearLayout.VERTICAL if (!mainFragmentIsHidden()) {
linearLayout.addView(textView) hideMainFragment()
linearLayout.addView(imageView) return
}
val layoutParams = LinearLayout.LayoutParams( if (doubleBackToExitPressedOnce) {
LinearLayout.LayoutParams.MATCH_PARENT, super.onBackPressed()
LinearLayout.LayoutParams.WRAP_CONTENT return
) }
imageView.layoutParams = layoutParams
textView.layoutParams = layoutParams
val builder: AlertDialog.Builder = AlertDialog.Builder(this) doubleBackToExitPressedOnce = true
builder Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show()
.setView(linearLayout)
val dialog: AlertDialog = builder.create() Handler(Looper.getMainLooper()).postDelayed({
dialog.show() doubleBackToExitPressedOnce = false
}, 2000)
} }
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) { when (keyCode) {
KeyEvent.KEYCODE_ESCAPE -> {
back()
return true
}
KeyEvent.KEYCODE_BACK -> { KeyEvent.KEYCODE_BACK -> {
if (!mainFragmentIsHidden()) { back()
hideMainFragment() return true
return true }
}
if (doubleBackToExitPressedOnce) { KeyEvent.KEYCODE_BOOKMARK -> {
super.onBackPressed() showHelp()
return true return true
} }
this.doubleBackToExitPressedOnce = true KeyEvent.KEYCODE_UNKNOWN -> {
Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show() showHelp()
return true
}
Handler(Looper.getMainLooper()).postDelayed({ KeyEvent.KEYCODE_HELP -> {
doubleBackToExitPressedOnce = false showHelp()
}, 2000)
return true return true
} }
@@ -245,28 +288,19 @@ class MainActivity : FragmentActivity() {
} }
KeyEvent.KEYCODE_DPAD_UP -> { KeyEvent.KEYCODE_DPAD_UP -> {
if (mainFragment.isHidden) { channelUp()
prev() }
} else {
// if (mainFragment.selectedPosition == 0) { KeyEvent.KEYCODE_CHANNEL_UP -> {
// mainFragment.setSelectedPosition( channelUp()
// mainFragment.tvListViewModel.maxNum.size - 1,
// false
// )
// }
}
} }
KeyEvent.KEYCODE_DPAD_DOWN -> { KeyEvent.KEYCODE_DPAD_DOWN -> {
if (mainFragment.isHidden) { channelDown()
next() }
} else {
// if (mainFragment.selectedPosition == mainFragment.tvListViewModel.maxNum.size - 1) { KeyEvent.KEYCODE_CHANNEL_DOWN -> {
//// mainFragment.setSelectedPosition(0, false) channelDown()
// hideMainFragment()
// return false
// }
}
} }
KeyEvent.KEYCODE_DPAD_LEFT -> { KeyEvent.KEYCODE_DPAD_LEFT -> {
@@ -351,5 +385,6 @@ class MainActivity : FragmentActivity() {
companion object { companion object {
private const val TAG = "MainActivity" private const val TAG = "MainActivity"
private const val CHANNEL_REVERSAL = "channel_reversal"
} }
} }
@@ -0,0 +1,44 @@
package com.lizongying.mytv
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.lizongying.mytv.databinding.DialogBinding
class MyDialogFragment(private val versionName: String, private val channelReversal: Boolean) :
DialogFragment() {
private var _binding: DialogBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = DialogBinding.inflate(inflater, container, false)
_binding?.version?.text =
"当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
val switchView = _binding?.switchView
switchView?.isChecked = channelReversal
switchView?.setOnCheckedChangeListener { _, isChecked ->
(activity as MainActivity).saveChannelReversal(isChecked)
}
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
companion object {
const val TAG = "MyDialogFragment"
}
}
+44
View File
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:background="#FF263238"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="16sp" />
<TextView
android:id="@+id/version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
></TextView>
<Switch
android:id="@+id/switch_view"
android:text="@string/title_channel_reversal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:src="@drawable/appreciate"
android:background="@color/white" />
</LinearLayout>
</FrameLayout>
-1
View File
@@ -12,7 +12,6 @@
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:contentDescription="@string/logo"
android:padding="10dp" /> android:padding="10dp" />
<LinearLayout <LinearLayout
+1 -1
View File
@@ -1,4 +1,4 @@
<resources> <resources>
<string name="app_name">我的电视</string> <string name="app_name">我的电视</string>
<string name="logo">logo</string> <string name="title_channel_reversal">频道反转</string>
</resources> </resources>
-1
View File
@@ -1,6 +1,5 @@
<resources> <resources>
<style name="CustomImageCardViewStyle" parent="Widget.Leanback.ImageCardViewStyle"> <style name="CustomImageCardViewStyle" parent="Widget.Leanback.ImageCardViewStyle">
<item name="lbImageCardViewType">Title|Content</item> <item name="lbImageCardViewType">Title|Content</item>
</style> </style>