Skip to content

Commit

Permalink
support source setting
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Mar 10, 2024
1 parent 6a988c7 commit 1d369d1
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 72 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

## 更新日志

### v1.0.1

* 支持返回键退出
* 支持基本的视频源配置

### v1.0.0

* 基本视频播放
Expand Down
95 changes: 55 additions & 40 deletions app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class MainActivity : FragmentActivity() {

private val handler = Handler(Looper.myLooper()!!)
private val delayHideMenu = 10000L
private val delayHideSetting = 10000L
private val delayHideSetting = 60000L

private var doubleBackToExitPressedOnce = false

private var position = 0

Expand All @@ -37,13 +39,24 @@ class MainActivity : FragmentActivity() {
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

val packageInfo = getPackageInfo()
val versionName = packageInfo.versionName
val versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageInfo.longVersionCode
} else {
packageInfo.versionCode.toLong()
}
settingFragment = SettingFragment(versionName, versionCode)

if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.add(R.id.main_browse_fragment, playerFragment)
.add(R.id.main_browse_fragment, infoFragment)
.add(R.id.main_browse_fragment, channelFragment)
.add(R.id.main_browse_fragment, menuFragment)
.add(R.id.main_browse_fragment, settingFragment)
.hide(menuFragment)
.hide(settingFragment)
.commitNow()
}

Expand Down Expand Up @@ -72,15 +85,6 @@ class MainActivity : FragmentActivity() {
}

TVList.setPosition(SP.position)

val packageInfo = getPackageInfo()
val versionName = packageInfo.versionName
val versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageInfo.longVersionCode
} else {
packageInfo.versionCode.toLong()
}
settingFragment = SettingFragment(versionName, versionCode)
}

fun play(position: Int) {
Expand Down Expand Up @@ -112,7 +116,7 @@ class MainActivity : FragmentActivity() {
handler.postDelayed(hideMenu, delayHideMenu)
}

val hideMenu = Runnable {
private val hideMenu = Runnable {
if (!menuFragment.isHidden) {
supportFragmentManager.beginTransaction().hide(menuFragment).commit()
}
Expand Down Expand Up @@ -162,7 +166,7 @@ class MainActivity : FragmentActivity() {


private fun channelUp() {
if (menuFragment.isHidden) {
if (menuFragment.isHidden && settingFragment.isHidden) {
if (SP.channelReversal) {
next()
return
Expand All @@ -172,7 +176,7 @@ class MainActivity : FragmentActivity() {
}

private fun channelDown() {
if (menuFragment.isHidden) {
if (menuFragment.isHidden && settingFragment.isHidden) {
if (SP.channelReversal) {
prev()
return
Expand All @@ -186,35 +190,23 @@ class MainActivity : FragmentActivity() {
hideMenuFragment()
return
}
//
// if (doubleBackToExitPressedOnce) {
// super.onBackPressed()
// return
// }
//
// doubleBackToExitPressedOnce = true
// Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show()
//
// Handler(Looper.getMainLooper()).postDelayed({
// doubleBackToExitPressedOnce = false
// }, 2000)
}

if (!settingFragment.isHidden) {
hideSettingFragment()
return
}

private fun showSetting() {
if (!menuFragment.isHidden) {
if (doubleBackToExitPressedOnce) {
super.onBackPressed()
return
}

doubleBackToExitPressedOnce = true
Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show()

// Log.i(TAG, "settingFragment ${settingFragment.isVisible}")
// if (!settingFragment.isVisible) {
// settingFragment.show(supportFragmentManager, "setting")
// settingActive()
// } else {
// handler.removeCallbacks(hideSetting)
// settingFragment.dismiss()
// }
Handler(Looper.getMainLooper()).postDelayed({
doubleBackToExitPressedOnce = false
}, 2000)
}

fun switchMainFragment() {
Expand All @@ -232,19 +224,40 @@ class MainActivity : FragmentActivity() {
}


private fun showMenuFragment() {
private fun showMenu() {
if (!settingFragment.isHidden) {
return
}

supportFragmentManager.beginTransaction()
.show(menuFragment)
.commitNow()
.commit()
menuActive()
}

private fun showSetting() {
if (!menuFragment.isHidden) {
return
}

supportFragmentManager.beginTransaction()
.show(settingFragment)
.commit()
settingActive()
}

fun hideMenuFragment() {
supportFragmentManager.beginTransaction()
.hide(menuFragment)
.commit()
}

fun hideSettingFragment() {
supportFragmentManager.beginTransaction()
.hide(settingFragment)
.commit()
}

fun onKey(keyCode: Int): Boolean {
Log.d(TAG, "keyCode $keyCode")
when (keyCode) {
Expand Down Expand Up @@ -358,11 +371,13 @@ class MainActivity : FragmentActivity() {
}

KeyEvent.KEYCODE_DPAD_LEFT -> {
showMenuFragment()
showMenu()
// return true
}

KeyEvent.KEYCODE_DPAD_RIGHT -> {
channelDown()
showSetting()
// return true
}
}
return false
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/com/lizongying/mytv0/SettingFragment.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.lizongying.mytv0

import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.lizongying.mytv0.databinding.SettingBinding
import com.lizongying.mytv0.models.TVList


class SettingFragment(
Expand All @@ -27,7 +29,7 @@ class SettingFragment(
_binding = SettingBinding.inflate(inflater, container, false)

_binding?.version?.text =
"当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
"当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv-0/releases/"

val switchChannelReversal = _binding?.switchChannelReversal
switchChannelReversal?.isChecked = SP.channelReversal
Expand All @@ -52,6 +54,18 @@ class SettingFragment(

// _binding?.checkVersion?.setOnClickListener(OnClickListenerCheckVersion(updateManager))

binding.confirmButton.setOnClickListener {
val uriEditText = binding.myEditText
var uri = uriEditText.text.toString()

uri = Utils.formatUrl(uri)
if (Uri.parse(uri).isAbsolute) {
TVList.update(uri)
} else {
uriEditText.error = "无效的地址"
}
}

return binding.root
}

Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,19 @@ object Utils {
}

fun isTmallDevice() = Build.MANUFACTURER.equals("Tmall", ignoreCase = true)

fun formatUrl(url: String): String {
// Check if the URL already starts with "http://" or "https://"
if (url.startsWith("http://") || url.startsWith("https://")) {
return url
}

// Check if the URL starts with "//"
if (url.startsWith("//")) {
return "http://$url"
}

// Otherwise, add "http://" to the beginning of the URL
return "http://${url}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class TVCategoryModel : ViewModel() {
_position.value = position
}

fun setTVListModelList(tvTVListModelList: MutableList<TVListModel>) {
_tvCategoryModel.value = tvTVListModelList
}

fun addTVListModel(tvListModel: TVListModel) {
if (_tvCategoryModel.value == null) {
_tvCategoryModel.value = mutableListOf(tvListModel)
Expand Down
53 changes: 37 additions & 16 deletions app/src/main/java/com/lizongying/mytv0/models/TVList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.lizongying.mytv0.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File

object TVList {
Expand All @@ -13,7 +17,7 @@ object TVList {
private lateinit var serverUrl: String
private lateinit var list: List<TV>
lateinit var listModel: List<TVModel>
lateinit var categoryModel: TVCategoryModel
val categoryModel = TVCategoryModel()

private val _position = MutableLiveData<Int>()
val position: LiveData<Int>
Expand All @@ -30,25 +34,45 @@ object TVList {
context.resources.openRawResource(R.raw.channels).bufferedReader()
.use { it.readText() }
}
Log.i("", "channel $str")
str2List(str)
}

fun update() {
val client = okhttp3.OkHttpClient()
val request = okhttp3.Request.Builder().url(serverUrl).build()
client.newCall(request).execute().use { response ->
if (response.isSuccessful) {
val file = File(appDirectory, FILE_NAME)
if (!file.exists()) {
file.createNewFile()
private fun update() {
CoroutineScope(Dispatchers.IO).launch {
try {
Log.i("", "do request $serverUrl")
val client = okhttp3.OkHttpClient()
val request = okhttp3.Request.Builder().url(serverUrl).build()
val response = client.newCall(request).execute()

if (response.isSuccessful) {
val file = File(appDirectory, FILE_NAME)
if (!file.exists()) {
file.createNewFile()
}
val str = response.body()!!.string()
Log.i("", "request str $str")

file.writeText(str)
withContext(Dispatchers.Main) {
str2List(str)
}
} else {
Log.e("", "request status ${response.code()}")
}
val str = response.body()!!.string()
file.writeText(str)
str2List(str)
} catch (e: Exception) {
Log.e("", "request error $e")
}
}
}

fun update(serverUrl: String) {
this.serverUrl = serverUrl
Log.i("", "update $serverUrl")
update()
}

private fun str2List(str: String) {
val type = object : com.google.gson.reflect.TypeToken<List<TV>>() {}.type
list = com.google.gson.Gson().fromJson(str, type)
Expand Down Expand Up @@ -84,10 +108,7 @@ object TVList {
category.add(tvListModel)
}

categoryModel = TVCategoryModel()
for (v in category) {
categoryModel.addTVListModel(v)
}
categoryModel.setTVListModelList(category)
}

fun getTVModel(idx: Int): TVModel {
Expand Down
Loading

0 comments on commit 1d369d1

Please sign in to comment.