Skip to content

Commit

Permalink
channel reversal
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Jan 16, 2024
1 parent 42b072f commit 1fd0951
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 44 deletions.
92 changes: 51 additions & 41 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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.PackageManager
import android.content.pm.Signature
import android.content.pm.SigningInfo
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
Expand All @@ -16,11 +16,7 @@ import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.lizongying.mytv.models.TVViewModel
import java.security.MessageDigest
Expand All @@ -39,6 +35,9 @@ class MainActivity : FragmentActivity() {
private val handler = Handler()
private val delay: Long = 4000

private lateinit var sharedPref: SharedPreferences
private var channelReversal = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -57,6 +56,9 @@ class MainActivity : FragmentActivity() {
mainFragment.view?.requestFocus()
}
gestureDetector = GestureDetector(this, GestureListener())

sharedPref = getPreferences(Context.MODE_PRIVATE)
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, false)
}

fun showInfoFragment(tvViewModel: TVViewModel) {
Expand Down Expand Up @@ -170,42 +172,30 @@ class MainActivity : FragmentActivity() {
}
}

fun saveChannelReversal(channelReversal: Boolean) {
with(sharedPref.edit()) {
putBoolean(CHANNEL_REVERSAL, channelReversal)
apply()
}
this.channelReversal = channelReversal
}

private fun showHelp() {
val versionName = getPackageInfo().versionName
if (!mainFragment.isHidden) {
return
}

val textView = TextView(this)
textView.text =
"当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
textView.setBackgroundColor(0xFF263238.toInt())
textView.setPadding(20, 50, 20, 20)

val imageView = ImageView(this)
val drawable = ContextCompat.getDrawable(this, R.drawable.appreciate)
imageView.setImageDrawable(drawable)
imageView.setBackgroundColor(Color.WHITE)

val linearLayout = LinearLayout(this)
linearLayout.orientation = LinearLayout.VERTICAL
linearLayout.addView(textView)
linearLayout.addView(imageView)

val layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
imageView.layoutParams = layoutParams
textView.layoutParams = layoutParams

val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder
.setView(linearLayout)

val dialog: AlertDialog = builder.create()
dialog.show()
val versionName = getPackageInfo().versionName
val dialogFragment = MyDialogFragment(versionName, channelReversal)
dialogFragment.show(supportFragmentManager, "settings_dialog")
}

private fun channelUp() {
if (mainFragment.isHidden) {
if (channelReversal) {
next()
return
}
prev()
} else {
// if (mainFragment.selectedPosition == 0) {
Expand All @@ -219,6 +209,10 @@ class MainActivity : FragmentActivity() {

private fun channelDown() {
if (mainFragment.isHidden) {
if (channelReversal) {
prev()
return
}
next()
} else {
// if (mainFragment.selectedPosition == mainFragment.tvListViewModel.maxNum.size - 1) {
Expand Down Expand Up @@ -250,12 +244,22 @@ class MainActivity : FragmentActivity() {

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_ESCAPE -> {
back()
return true
}

KeyEvent.KEYCODE_BACK -> {
back()
return true
}

KeyEvent.KEYCODE_SETTINGS -> {
KeyEvent.KEYCODE_BOOKMARK -> {
showHelp()
return true
}

KeyEvent.KEYCODE_UNKNOWN -> {
showHelp()
return true
}
Expand All @@ -265,6 +269,11 @@ class MainActivity : FragmentActivity() {
return true
}

KeyEvent.KEYCODE_SETTINGS -> {
showHelp()
return true
}

KeyEvent.KEYCODE_MENU -> {
showHelp()
return true
Expand All @@ -282,14 +291,14 @@ class MainActivity : FragmentActivity() {
channelUp()
}

KeyEvent.KEYCODE_DPAD_DOWN -> {
channelDown()
}

KeyEvent.KEYCODE_CHANNEL_UP -> {
channelUp()
}

KeyEvent.KEYCODE_DPAD_DOWN -> {
channelDown()
}

KeyEvent.KEYCODE_CHANNEL_DOWN -> {
channelDown()
}
Expand Down Expand Up @@ -376,5 +385,6 @@ class MainActivity : FragmentActivity() {

companion object {
private const val TAG = "MainActivity"
private const val CHANNEL_REVERSAL = "channel_reversal"
}
}
44 changes: 44 additions & 0 deletions app/src/main/java/com/lizongying/mytv/MyDialogFragment.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 44 additions & 0 deletions app/src/main/res/layout/dialog.xml
Original file line number Diff line number Diff line change
@@ -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 change: 0 additions & 1 deletion app/src/main/res/layout/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:contentDescription="@string/logo"
android:padding="10dp" />

<LinearLayout
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<resources>
<string name="app_name">我的电视</string>
<string name="logo">logo</string>
<string name="title_channel_reversal">频道反转</string>
</resources>
1 change: 0 additions & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<resources>


<style name="CustomImageCardViewStyle" parent="Widget.Leanback.ImageCardViewStyle">
<item name="lbImageCardViewType">Title|Content</item>
</style>
Expand Down

0 comments on commit 1fd0951

Please sign in to comment.