mirror of
https://github.com/lizongying/my-tv.git
synced 2026-11-13 06:56:30 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7a60531ab | ||
|
|
764f2a1549 | ||
|
|
aa4d52681f | ||
|
|
8137515605 | ||
|
|
6c16408b90 | ||
|
|
44ab3a6669 | ||
|
|
917b245bbe | ||
|
|
dd4550cf4f | ||
|
|
4bdc625718 | ||
|
|
cda19995b2 | ||
|
|
1eb2f257b5 | ||
|
|
9feb0dc4bc | ||
|
|
46311036d6 | ||
|
|
1aade3a48d | ||
|
|
2528cf33db | ||
|
|
4364f66ec5 | ||
|
|
49cd7b3af2 |
@@ -14,6 +14,16 @@
|
|||||||
|
|
||||||
## 更新日志
|
## 更新日志
|
||||||
|
|
||||||
|
### v1.5.8(通用版)
|
||||||
|
|
||||||
|
* 修复央视6画质差的问题
|
||||||
|
* 增加兵团卫视
|
||||||
|
* 播放失败重试
|
||||||
|
|
||||||
|
### v1.5.6(通用版)
|
||||||
|
|
||||||
|
* 解决部分设备系统时间不对导致播放失败的问题
|
||||||
|
|
||||||
### v1.5.2(通用版)
|
### v1.5.2(通用版)
|
||||||
|
|
||||||
* 修复APP恢复后频道号、频道列表不自动消失的问题
|
* 修复APP恢复后频道号、频道列表不自动消失的问题
|
||||||
@@ -112,4 +122,4 @@ adb install my-tv.apk
|
|||||||
|
|
||||||
## 赞赏
|
## 赞赏
|
||||||
|
|
||||||

|

|
||||||
+1
-1
@@ -101,7 +101,7 @@ dependencies {
|
|||||||
implementation "androidx.media3:media3-exoplayer-hls:$media3_version"
|
implementation "androidx.media3:media3-exoplayer-hls:$media3_version"
|
||||||
|
|
||||||
// 21:2.9.0 17:2.6.4
|
// 21:2.9.0 17:2.6.4
|
||||||
def retrofit2_version = "2.9.0"
|
def retrofit2_version = "2.6.4"
|
||||||
|
|
||||||
implementation 'com.google.protobuf:protobuf-kotlin:3.25.1'
|
implementation 'com.google.protobuf:protobuf-kotlin:3.25.1'
|
||||||
implementation "com.squareup.retrofit2:converter-gson:$retrofit2_version"
|
implementation "com.squareup.retrofit2:converter-gson:$retrofit2_version"
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,124 @@
|
|||||||
|
package com.lizongying.mytv
|
||||||
|
|
||||||
|
import android.graphics.Color
|
||||||
|
import android.view.ContextThemeWrapper
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.view.animation.ScaleAnimation
|
||||||
|
import android.widget.ImageView
|
||||||
|
import androidx.leanback.widget.ImageCardView
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.bumptech.glide.Glide
|
||||||
|
import com.lizongying.mytv.models.TVListViewModel
|
||||||
|
import com.lizongying.mytv.models.TVViewModel
|
||||||
|
|
||||||
|
|
||||||
|
class CardAdapter(private val owner: LifecycleOwner, private var tvListViewModel: TVListViewModel) :
|
||||||
|
RecyclerView.Adapter<CardAdapter.ViewHolder>() {
|
||||||
|
|
||||||
|
private var listener: ItemListener? = null
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
|
val cardView = object :
|
||||||
|
ImageCardView(ContextThemeWrapper(parent.context, R.style.CustomImageCardTheme)) {}
|
||||||
|
startScaleAnimation(cardView, 1.0f, 0.9f)
|
||||||
|
cardView.isFocusable = true
|
||||||
|
cardView.isFocusableInTouchMode = true
|
||||||
|
|
||||||
|
return ViewHolder(cardView)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startScaleAnimation(view: View, fromScale: Float, toScale: Float) {
|
||||||
|
val scaleAnimation = ScaleAnimation(
|
||||||
|
fromScale, toScale,
|
||||||
|
fromScale, toScale,
|
||||||
|
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
|
||||||
|
ScaleAnimation.RELATIVE_TO_SELF, 0.5f
|
||||||
|
)
|
||||||
|
scaleAnimation.duration = 200
|
||||||
|
scaleAnimation.fillAfter = true
|
||||||
|
|
||||||
|
view.startAnimation(scaleAnimation)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
|
||||||
|
val item = tvListViewModel.getTVViewModel(position)
|
||||||
|
|
||||||
|
val tvViewModel = item as TVViewModel
|
||||||
|
val cardView = viewHolder.view as ImageCardView
|
||||||
|
|
||||||
|
cardView.setOnFocusChangeListener { _, hasFocus ->
|
||||||
|
listener?.onItemFocusChange(item, hasFocus)
|
||||||
|
if (hasFocus) {
|
||||||
|
startScaleAnimation(cardView, 0.9f, 1.0f)
|
||||||
|
} else {
|
||||||
|
startScaleAnimation(cardView, 1.0f, 0.9f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cardView.setOnClickListener { _ ->
|
||||||
|
listener?.onItemClicked(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
cardView.titleText = tvViewModel.title.value
|
||||||
|
cardView.tag = tvViewModel.videoUrl.value
|
||||||
|
|
||||||
|
when (tvViewModel.title.value) {
|
||||||
|
"CCTV8K 超高清" -> Glide.with(viewHolder.view.context)
|
||||||
|
.load(R.drawable.cctv8k)
|
||||||
|
.centerInside()
|
||||||
|
.into(cardView.mainImageView)
|
||||||
|
|
||||||
|
"天津卫视" -> Glide.with(viewHolder.view.context)
|
||||||
|
.load(R.drawable.tianjin)
|
||||||
|
.centerInside()
|
||||||
|
.into(cardView.mainImageView)
|
||||||
|
|
||||||
|
"新疆卫视" -> Glide.with(viewHolder.view.context)
|
||||||
|
.load(R.drawable.xinjiang)
|
||||||
|
.centerInside()
|
||||||
|
.into(cardView.mainImageView)
|
||||||
|
|
||||||
|
"兵团卫视" -> Glide.with(viewHolder.view.context)
|
||||||
|
.load(R.drawable.bingtuan)
|
||||||
|
.centerInside()
|
||||||
|
.into(cardView.mainImageView)
|
||||||
|
|
||||||
|
else -> Glide.with(viewHolder.view.context)
|
||||||
|
.load(tvViewModel.logo.value)
|
||||||
|
.centerInside()
|
||||||
|
.into(cardView.mainImageView)
|
||||||
|
}
|
||||||
|
|
||||||
|
cardView.mainImageView.setBackgroundColor(Color.WHITE)
|
||||||
|
cardView.setMainImageScaleType(ImageView.ScaleType.CENTER_INSIDE)
|
||||||
|
|
||||||
|
tvViewModel.program.observe(owner) { _ ->
|
||||||
|
val program = tvViewModel.getProgramOne()
|
||||||
|
if (program != null) {
|
||||||
|
cardView.contentText = program.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount() = tvListViewModel.size()
|
||||||
|
|
||||||
|
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||||
|
val view = itemView
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ItemListener {
|
||||||
|
fun onItemFocusChange(tvViewModel: TVViewModel, hasFocus: Boolean)
|
||||||
|
fun onItemClicked(tvViewModel: TVViewModel)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setItemListener(listener: ItemListener) {
|
||||||
|
this.listener = listener
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "CardAdapter"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -47,6 +47,11 @@ class CardPresenter(
|
|||||||
.centerInside()
|
.centerInside()
|
||||||
.into(cardView.mainImageView)
|
.into(cardView.mainImageView)
|
||||||
|
|
||||||
|
"兵团卫视" -> Glide.with(viewHolder.view.context)
|
||||||
|
.load(R.drawable.bingtuan)
|
||||||
|
.centerInside()
|
||||||
|
.into(cardView.mainImageView)
|
||||||
|
|
||||||
else -> Glide.with(viewHolder.view.context)
|
else -> Glide.with(viewHolder.view.context)
|
||||||
.load(tvViewModel.logo.value)
|
.load(tvViewModel.logo.value)
|
||||||
.centerInside()
|
.centerInside()
|
||||||
|
|||||||
@@ -1,157 +0,0 @@
|
|||||||
package com.lizongying.mytv
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
/**
|
|
||||||
*@author LeGend
|
|
||||||
*@date 2024/2/4 22:42
|
|
||||||
*/
|
|
||||||
object ChannelUtils {
|
|
||||||
/**
|
|
||||||
* 获取服务器channel版本
|
|
||||||
*
|
|
||||||
* @param context Context
|
|
||||||
*
|
|
||||||
* @return 服务器channel版本
|
|
||||||
*/
|
|
||||||
suspend fun getServerVersion(context: Context): Int {
|
|
||||||
return withContext(Dispatchers.IO) {
|
|
||||||
val client = okhttp3.OkHttpClient.Builder().connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS)
|
|
||||||
.readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build()
|
|
||||||
client.newCall(okhttp3.Request.Builder().url(getServerVersionUrl(context)).build()).execute()
|
|
||||||
.use { response ->
|
|
||||||
if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response")
|
|
||||||
val body = response.body()
|
|
||||||
body?.string()?.toInt() ?: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取服务器channel
|
|
||||||
*
|
|
||||||
* @param url String 服务器地址
|
|
||||||
*
|
|
||||||
* @return Array<TV> 服务器channel
|
|
||||||
*
|
|
||||||
* @throws java.io.IOException 网络请求失败
|
|
||||||
*/
|
|
||||||
suspend fun getServerChannel(url: String): List<TV> {
|
|
||||||
val result = withContext(Dispatchers.IO) {
|
|
||||||
val client = okhttp3.OkHttpClient.Builder().connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS)
|
|
||||||
.readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build()
|
|
||||||
val request = okhttp3.Request.Builder().url(url).build()
|
|
||||||
client.newCall(request).execute().use { response ->
|
|
||||||
if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response")
|
|
||||||
val body = response.body()
|
|
||||||
body?.string() ?: ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return withContext(Dispatchers.Default) {
|
|
||||||
val type = object : com.google.gson.reflect.TypeToken<List<TV>>() {}.type
|
|
||||||
com.google.gson.Gson().fromJson(result, type)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取服务器地址
|
|
||||||
*
|
|
||||||
* @param context Context
|
|
||||||
*
|
|
||||||
* @return 服务器地址
|
|
||||||
*/
|
|
||||||
fun getServerUrl(context: Context): String {
|
|
||||||
return context.resources.getString(R.string.server_url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取serverVersion的URL
|
|
||||||
*
|
|
||||||
* @param context Context
|
|
||||||
*
|
|
||||||
* @return serverVersionURL 服务器版本地址
|
|
||||||
*/
|
|
||||||
suspend fun getServerVersionUrl(context: Context): String {
|
|
||||||
return withContext(Dispatchers.IO) {
|
|
||||||
context.resources.getString(R.string.server_version_url)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取本地channel版本
|
|
||||||
*
|
|
||||||
* @param context Context
|
|
||||||
*
|
|
||||||
* @return 本地channel
|
|
||||||
*/
|
|
||||||
suspend fun getLocalVersion(context: Context): Int {
|
|
||||||
return withContext(Dispatchers.IO) {
|
|
||||||
val file = File(getAppDirectory(context), "channels")
|
|
||||||
//检查本地是否已经有保存的channels.json,若无保存的Channel.json则从读取assert中文件
|
|
||||||
val savedVersion =
|
|
||||||
context.getSharedPreferences("saved_version", Context.MODE_PRIVATE).getInt("version", Integer.MIN_VALUE)
|
|
||||||
if (!file.exists() || savedVersion == Integer.MIN_VALUE) {
|
|
||||||
context.resources.getInteger(R.integer.local_channel_version)
|
|
||||||
} else {
|
|
||||||
savedVersion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取本地可读取的目录
|
|
||||||
* @param context Context
|
|
||||||
*
|
|
||||||
* @return 可读取的目录
|
|
||||||
*/
|
|
||||||
private fun getAppDirectory(context: Context): File {
|
|
||||||
return context.filesDir
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取本地channel
|
|
||||||
*
|
|
||||||
* @param context Context
|
|
||||||
*
|
|
||||||
* @return Array<TV> 本地channel
|
|
||||||
*/
|
|
||||||
suspend fun getLocalChannel(context: Context): List<TV> {
|
|
||||||
val str = withContext(Dispatchers.IO) {
|
|
||||||
if (File(getAppDirectory(context), "channels").exists()) {
|
|
||||||
File(getAppDirectory(context), "channels").readText()
|
|
||||||
} else {
|
|
||||||
context.resources.openRawResource(R.raw.channels).bufferedReader().use { it.readText() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return withContext(Dispatchers.Default) {
|
|
||||||
val type = object : com.google.gson.reflect.TypeToken<List<TV>>() {}.type
|
|
||||||
com.google.gson.Gson().fromJson(str, type)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 更新channels.json
|
|
||||||
*
|
|
||||||
* @param context Context
|
|
||||||
*
|
|
||||||
* @return 无
|
|
||||||
*
|
|
||||||
* @throws java.io.IOException 写入失败
|
|
||||||
*/
|
|
||||||
suspend fun updateLocalChannel(context: Context, version: Int, channels: List<TV>) {
|
|
||||||
withContext(Dispatchers.IO) {
|
|
||||||
val file = File(getAppDirectory(context), "channels")
|
|
||||||
if (!file.exists()) {
|
|
||||||
file.createNewFile()
|
|
||||||
}
|
|
||||||
file.writeText(com.google.gson.Gson().toJson(channels))
|
|
||||||
context.getSharedPreferences("saved_version", Context.MODE_PRIVATE).edit().putInt(
|
|
||||||
"version", version
|
|
||||||
).apply()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.lizongying.mytv
|
||||||
|
|
||||||
|
import android.app.AlertDialog
|
||||||
|
import android.app.Dialog
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.fragment.app.DialogFragment
|
||||||
|
|
||||||
|
class ConfirmationDialogFragment(private val listener: ConfirmationDialogListener) :
|
||||||
|
DialogFragment() {
|
||||||
|
|
||||||
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
|
return activity?.let {
|
||||||
|
val builder = AlertDialog.Builder(it)
|
||||||
|
builder.setTitle("确认")
|
||||||
|
.setMessage("确认更新吗?")
|
||||||
|
.setPositiveButton(
|
||||||
|
"确定"
|
||||||
|
) { _, _ ->
|
||||||
|
listener.onConfirm()
|
||||||
|
}
|
||||||
|
.setNegativeButton(
|
||||||
|
"取消"
|
||||||
|
) { _, _ ->
|
||||||
|
listener.onCancel()
|
||||||
|
}
|
||||||
|
// 创建并返回 AlertDialog 对象
|
||||||
|
builder.create()
|
||||||
|
} ?: throw IllegalStateException("Activity cannot be null")
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ConfirmationDialogListener {
|
||||||
|
fun onConfirm()
|
||||||
|
fun onCancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -43,6 +43,10 @@ class InfoFragment : Fragment() {
|
|||||||
.load(R.drawable.xinjiang)
|
.load(R.drawable.xinjiang)
|
||||||
.into(binding.infoLogo)
|
.into(binding.infoLogo)
|
||||||
|
|
||||||
|
"兵团卫视" -> Glide.with(this)
|
||||||
|
.load(R.drawable.bingtuan)
|
||||||
|
.into(binding.infoLogo)
|
||||||
|
|
||||||
else -> Glide.with(this)
|
else -> Glide.with(this)
|
||||||
.load(tvViewModel.logo.value)
|
.load(tvViewModel.logo.value)
|
||||||
.into(binding.infoLogo)
|
.into(binding.infoLogo)
|
||||||
|
|||||||
@@ -18,16 +18,22 @@ import android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
|||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.fragment.app.FragmentActivity
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.lizongying.mytv.models.TVViewModel
|
import com.lizongying.mytv.models.TVViewModel
|
||||||
|
import kotlinx.coroutines.CoroutineStart
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : FragmentActivity() {
|
class MainActivity : FragmentActivity() {
|
||||||
|
|
||||||
var playerFragment = PlayerFragment()
|
private var ready = 0
|
||||||
private val mainFragment = MainFragment()
|
private var playerFragment = PlayerFragment()
|
||||||
private val infoFragment = InfoFragment()
|
private var mainFragment = MainFragment()
|
||||||
private val channelFragment = ChannelFragment()
|
private var infoFragment = InfoFragment()
|
||||||
|
private var channelFragment = ChannelFragment()
|
||||||
private lateinit var settingFragment: SettingFragment
|
private lateinit var settingFragment: SettingFragment
|
||||||
|
|
||||||
private var doubleBackToExitPressedOnce = false
|
private var doubleBackToExitPressedOnce = false
|
||||||
@@ -44,6 +50,16 @@ class MainActivity : FragmentActivity() {
|
|||||||
|
|
||||||
private var versionName = ""
|
private var versionName = ""
|
||||||
|
|
||||||
|
init {
|
||||||
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
val utilsJob = async(start = CoroutineStart.LAZY) { Utils.init() }
|
||||||
|
|
||||||
|
utilsJob.start()
|
||||||
|
|
||||||
|
utilsJob.await()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
Log.i(TAG, "onCreate")
|
Log.i(TAG, "onCreate")
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@@ -68,8 +84,14 @@ class MainActivity : FragmentActivity() {
|
|||||||
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, channelReversal)
|
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, channelReversal)
|
||||||
channelNum = sharedPref.getBoolean(CHANNEL_NUM, channelNum)
|
channelNum = sharedPref.getBoolean(CHANNEL_NUM, channelNum)
|
||||||
|
|
||||||
versionName = getPackageInfo().versionName
|
val packageInfo = getPackageInfo()
|
||||||
settingFragment = SettingFragment(versionName, channelReversal, channelNum)
|
versionName = packageInfo.versionName
|
||||||
|
val versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||||
|
packageInfo.longVersionCode
|
||||||
|
} else {
|
||||||
|
packageInfo.versionCode.toLong()
|
||||||
|
}
|
||||||
|
settingFragment = SettingFragment(versionName, versionCode, channelReversal, channelNum)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun showInfoFragment(tvViewModel: TVViewModel) {
|
fun showInfoFragment(tvViewModel: TVViewModel) {
|
||||||
@@ -122,6 +144,7 @@ class MainActivity : FragmentActivity() {
|
|||||||
val transaction = supportFragmentManager.beginTransaction()
|
val transaction = supportFragmentManager.beginTransaction()
|
||||||
|
|
||||||
if (mainFragment.isHidden) {
|
if (mainFragment.isHidden) {
|
||||||
|
mainFragment.setPosition()
|
||||||
transaction.show(mainFragment)
|
transaction.show(mainFragment)
|
||||||
keepRunnable()
|
keepRunnable()
|
||||||
} else {
|
} else {
|
||||||
@@ -155,7 +178,11 @@ class MainActivity : FragmentActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun fragmentReady() {
|
fun fragmentReady() {
|
||||||
mainFragment.fragmentReady()
|
ready++
|
||||||
|
Log.i(TAG, "ready $ready")
|
||||||
|
if (ready == 4) {
|
||||||
|
mainFragment.fragmentReady()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTouchEvent(event: MotionEvent?): Boolean {
|
override fun onTouchEvent(event: MotionEvent?): Boolean {
|
||||||
@@ -495,7 +522,7 @@ class MainActivity : FragmentActivity() {
|
|||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
Log.i(TAG, "onResume")
|
Log.i(TAG, "onResume")
|
||||||
super.onResume()
|
super.onResume()
|
||||||
if (!mainFragment.isHidden){
|
if (!mainFragment.isHidden) {
|
||||||
handler.postDelayed(hideMain, delayHideMain)
|
handler.postDelayed(hideMain, delayHideMain)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,30 +5,31 @@ import android.os.Bundle
|
|||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.leanback.app.BrowseSupportFragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.leanback.widget.ArrayObjectAdapter
|
|
||||||
import androidx.leanback.widget.HeaderItem
|
|
||||||
import androidx.leanback.widget.ListRow
|
|
||||||
import androidx.leanback.widget.ListRowPresenter
|
|
||||||
import androidx.leanback.widget.ListRowPresenter.SelectItemViewHolderTask
|
|
||||||
import androidx.leanback.widget.OnItemViewClickedListener
|
|
||||||
import androidx.leanback.widget.OnItemViewSelectedListener
|
|
||||||
import androidx.leanback.widget.Presenter
|
|
||||||
import androidx.leanback.widget.Row
|
|
||||||
import androidx.leanback.widget.RowPresenter
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.lizongying.mytv.Utils.dpToPx
|
||||||
import com.lizongying.mytv.Utils.getDateTimestamp
|
import com.lizongying.mytv.Utils.getDateTimestamp
|
||||||
|
import com.lizongying.mytv.databinding.RowBinding
|
||||||
|
import com.lizongying.mytv.databinding.ShowBinding
|
||||||
import com.lizongying.mytv.models.TVListViewModel
|
import com.lizongying.mytv.models.TVListViewModel
|
||||||
import com.lizongying.mytv.models.TVViewModel
|
import com.lizongying.mytv.models.TVViewModel
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class MainFragment : BrowseSupportFragment() {
|
class MainFragment : Fragment(), CardAdapter.ItemListener {
|
||||||
|
|
||||||
private var itemPosition = 0
|
private var itemPosition = 0
|
||||||
|
|
||||||
private var rowsAdapter: ArrayObjectAdapter? = null
|
private var rowList: MutableList<View> = mutableListOf()
|
||||||
|
|
||||||
|
private var _binding: ShowBinding? = null
|
||||||
|
private val binding get() = _binding!!
|
||||||
|
|
||||||
private var request = Request()
|
private var request = Request()
|
||||||
|
|
||||||
@@ -41,17 +42,12 @@ class MainFragment : BrowseSupportFragment() {
|
|||||||
private val handler = Handler(Looper.getMainLooper())
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
private lateinit var mUpdateProgramRunnable: UpdateProgramRunnable
|
private lateinit var mUpdateProgramRunnable: UpdateProgramRunnable
|
||||||
|
|
||||||
private var ready = 0
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater, container: ViewGroup?,
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
savedInstanceState: Bundle?
|
||||||
Log.i(TAG, "onCreate")
|
): View {
|
||||||
super.onCreate(savedInstanceState)
|
_binding = ShowBinding.inflate(inflater, container, false)
|
||||||
headersState = HEADERS_DISABLED
|
return binding.root
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStart() {
|
|
||||||
Log.i(TAG, "onStart")
|
|
||||||
super.onStart()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||||
@@ -60,171 +56,142 @@ class MainFragment : BrowseSupportFragment() {
|
|||||||
activity?.let { request.initYSP(it) }
|
activity?.let { request.initYSP(it) }
|
||||||
sharedPref = (activity as? MainActivity)?.sharedPref!!
|
sharedPref = (activity as? MainActivity)?.sharedPref!!
|
||||||
|
|
||||||
loadRows()
|
view?.post {
|
||||||
|
val content = binding.content
|
||||||
|
|
||||||
setupEventListeners()
|
var idx: Long = 0
|
||||||
|
for ((k, v) in TVList.list) {
|
||||||
|
val itemBinding: RowBinding =
|
||||||
|
RowBinding.inflate(layoutInflater, content, false)
|
||||||
|
|
||||||
mUpdateProgramRunnable = UpdateProgramRunnable()
|
val tvListViewModelCurrent = TVListViewModel()
|
||||||
handler.post(mUpdateProgramRunnable)
|
for ((idx2, v1) in v.withIndex()) {
|
||||||
|
val tvViewModel = TVViewModel(v1)
|
||||||
tvListViewModel.tvListViewModel.value?.forEach { tvViewModel ->
|
tvViewModel.setRowPosition(idx.toInt())
|
||||||
tvViewModel.errInfo.observe(viewLifecycleOwner) { _ ->
|
tvViewModel.setItemPosition(idx2)
|
||||||
if (tvViewModel.errInfo.value != null
|
tvListViewModelCurrent.addTVViewModel(tvViewModel)
|
||||||
&& tvViewModel.id.value == itemPosition
|
tvListViewModel.addTVViewModel(tvViewModel)
|
||||||
) {
|
|
||||||
Toast.makeText(context, tvViewModel.errInfo.value, Toast.LENGTH_SHORT).show()
|
|
||||||
}
|
}
|
||||||
}
|
tvListViewModel.maxNum.add(v.size)
|
||||||
tvViewModel.ready.observe(viewLifecycleOwner) { _ ->
|
|
||||||
|
|
||||||
// not first time && channel not change
|
val adapter = CardAdapter(viewLifecycleOwner, tvListViewModelCurrent)
|
||||||
if (tvViewModel.ready.value != null
|
rowList.add(itemBinding.rowItems)
|
||||||
&& tvViewModel.id.value == itemPosition
|
|
||||||
&& check(tvViewModel)
|
adapter.setItemListener(this)
|
||||||
) {
|
|
||||||
Log.i(TAG, "ready ${tvViewModel.title.value}")
|
itemBinding.rowHeader.text = k
|
||||||
(activity as? MainActivity)?.play(tvViewModel)
|
itemBinding.rowItems.adapter = adapter
|
||||||
}
|
itemBinding.rowItems.layoutManager =
|
||||||
|
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
||||||
|
|
||||||
|
val layoutParams = itemBinding.row.layoutParams as ViewGroup.MarginLayoutParams
|
||||||
|
layoutParams.topMargin = dpToPx(11F)
|
||||||
|
itemBinding.row.layoutParams = layoutParams
|
||||||
|
content.addView(itemBinding.row)
|
||||||
|
|
||||||
|
idx++
|
||||||
}
|
}
|
||||||
tvViewModel.change.observe(viewLifecycleOwner) { _ ->
|
|
||||||
if (tvViewModel.change.value != null) {
|
mUpdateProgramRunnable = UpdateProgramRunnable()
|
||||||
val title = tvViewModel.title.value
|
handler.post(mUpdateProgramRunnable)
|
||||||
if (tvViewModel.pid.value != "") {
|
|
||||||
Log.i(TAG, "request $title")
|
itemPosition = sharedPref.getInt(POSITION, 0)
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
if (itemPosition >= tvListViewModel.size()) {
|
||||||
tvViewModel.let { request.fetchData(it) }
|
itemPosition = 0
|
||||||
}
|
}
|
||||||
(activity as? MainActivity)?.showInfoFragment(tvViewModel)
|
tvListViewModel.setItemPosition(itemPosition)
|
||||||
setSelectedPosition(
|
setPosition()
|
||||||
tvViewModel.getRowPosition(), true,
|
tvListViewModel.tvListViewModel.value?.forEach { tvViewModel ->
|
||||||
SelectItemViewHolderTask(tvViewModel.getItemPosition())
|
tvViewModel.errInfo.observe(viewLifecycleOwner) { _ ->
|
||||||
)
|
if (tvViewModel.errInfo.value != null
|
||||||
} else {
|
&& tvViewModel.id.value == itemPosition
|
||||||
if (check(tvViewModel)) {
|
) {
|
||||||
(activity as? MainActivity)?.play(tvViewModel)
|
Toast.makeText(context, tvViewModel.errInfo.value, Toast.LENGTH_SHORT)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tvViewModel.ready.observe(viewLifecycleOwner) { _ ->
|
||||||
|
|
||||||
|
// not first time && channel not change
|
||||||
|
if (tvViewModel.ready.value != null
|
||||||
|
&& tvViewModel.id.value == itemPosition
|
||||||
|
&& check(tvViewModel)
|
||||||
|
) {
|
||||||
|
Log.i(TAG, "ready ${tvViewModel.title.value}")
|
||||||
|
(activity as? MainActivity)?.play(tvViewModel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tvViewModel.change.observe(viewLifecycleOwner) { _ ->
|
||||||
|
if (tvViewModel.change.value != null) {
|
||||||
|
val title = tvViewModel.title.value
|
||||||
|
Log.i(TAG, "switch $title")
|
||||||
|
if (tvViewModel.pid.value != "") {
|
||||||
|
Log.i(TAG, "request $title")
|
||||||
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
tvViewModel.let { request.fetchData(it) }
|
||||||
|
}
|
||||||
(activity as? MainActivity)?.showInfoFragment(tvViewModel)
|
(activity as? MainActivity)?.showInfoFragment(tvViewModel)
|
||||||
setSelectedPosition(
|
setPosition(
|
||||||
tvViewModel.getRowPosition(), true,
|
tvViewModel.getRowPosition(), tvViewModel.getItemPosition()
|
||||||
SelectItemViewHolderTask(tvViewModel.getItemPosition())
|
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
if (check(tvViewModel)) {
|
||||||
|
(activity as? MainActivity)?.play(tvViewModel)
|
||||||
|
(activity as? MainActivity)?.showInfoFragment(tvViewModel)
|
||||||
|
setPosition(
|
||||||
|
tvViewModel.getRowPosition(), tvViewModel.getItemPosition()
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fragmentReady()
|
(activity as MainActivity).fragmentReady()
|
||||||
}
|
|
||||||
|
|
||||||
fun toLastPosition() {
|
|
||||||
setSelectedPosition(
|
|
||||||
selectedPosition, false,
|
|
||||||
SelectItemViewHolderTask(tvListViewModel.maxNum[selectedPosition] - 1)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toFirstPosition() {
|
|
||||||
setSelectedPosition(
|
|
||||||
selectedPosition, false,
|
|
||||||
SelectItemViewHolderTask(0)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun startHeadersTransition(withHeaders: Boolean) {
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun loadRows() {
|
|
||||||
rowsAdapter = ArrayObjectAdapter(ListRowPresenter())
|
|
||||||
|
|
||||||
val cardPresenter = CardPresenter(viewLifecycleOwner)
|
|
||||||
|
|
||||||
var idx: Long = 0
|
|
||||||
for ((k, v) in TVList.list) {
|
|
||||||
val listRowAdapter = ArrayObjectAdapter(cardPresenter)
|
|
||||||
for ((idx2, v1) in v.withIndex()) {
|
|
||||||
val tvViewModel = TVViewModel(v1)
|
|
||||||
tvViewModel.setRowPosition(idx.toInt())
|
|
||||||
tvViewModel.setItemPosition(idx2)
|
|
||||||
tvListViewModel.addTVViewModel(tvViewModel)
|
|
||||||
listRowAdapter.add(tvViewModel)
|
|
||||||
}
|
|
||||||
tvListViewModel.maxNum.add(v.size)
|
|
||||||
val header = HeaderItem(idx, k)
|
|
||||||
rowsAdapter!!.add(ListRow(header, listRowAdapter))
|
|
||||||
idx++
|
|
||||||
}
|
|
||||||
|
|
||||||
adapter = rowsAdapter
|
|
||||||
|
|
||||||
itemPosition = sharedPref.getInt(POSITION, 0)
|
|
||||||
if (itemPosition >= tvListViewModel.size()) {
|
|
||||||
itemPosition = 0
|
|
||||||
}
|
|
||||||
tvListViewModel.setItemPosition(itemPosition)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun prevSource() {
|
|
||||||
view?.post {
|
|
||||||
val tvViewModel = tvListViewModel.getTVViewModel(itemPosition)
|
|
||||||
if (tvViewModel != null) {
|
|
||||||
if (tvViewModel.videoUrl.value!!.size > 1) {
|
|
||||||
val videoIndex = tvViewModel.videoIndex.value?.minus(1)
|
|
||||||
if (videoIndex == -1) {
|
|
||||||
tvViewModel.setVideoIndex(tvViewModel.videoUrl.value!!.size - 1)
|
|
||||||
}
|
|
||||||
tvViewModel.changed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun nextSource() {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
view?.post {
|
super.onViewCreated(view, savedInstanceState)
|
||||||
val tvViewModel = tvListViewModel.getTVViewModel(itemPosition)
|
}
|
||||||
if (tvViewModel != null) {
|
|
||||||
if (tvViewModel.videoUrl.value!!.size > 1) {
|
override fun onItemFocusChange(tvViewModel: TVViewModel, hasFocus: Boolean) {
|
||||||
val videoIndex = tvViewModel.videoIndex.value?.plus(1)
|
if (hasFocus) {
|
||||||
if (videoIndex == tvViewModel.videoUrl.value!!.size) {
|
tvListViewModel.setItemPositionCurrent(tvViewModel.id.value!!)
|
||||||
tvViewModel.setVideoIndex(0)
|
(activity as MainActivity).keepRunnable()
|
||||||
}
|
|
||||||
tvViewModel.changed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupEventListeners() {
|
override fun onItemClicked(tvViewModel: TVViewModel) {
|
||||||
onItemViewClickedListener = ItemViewClickedListener()
|
if (this.isHidden) {
|
||||||
onItemViewSelectedListener = ItemViewSelectedListener()
|
(activity as? MainActivity)?.switchMainFragment()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (itemPosition != tvViewModel.id.value!!) {
|
||||||
|
itemPosition = tvViewModel.id.value!!
|
||||||
|
tvListViewModel.setItemPosition(itemPosition)
|
||||||
|
tvListViewModel.getTVViewModel(itemPosition)?.changed()
|
||||||
|
}
|
||||||
|
(activity as? MainActivity)?.switchMainFragment()
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class ItemViewClickedListener : OnItemViewClickedListener {
|
fun setPosition() {
|
||||||
override fun onItemClicked(
|
val tvViewModel = tvListViewModel.getTVViewModel(itemPosition)
|
||||||
itemViewHolder: Presenter.ViewHolder,
|
Log.i(TAG, "tvViewModel $tvViewModel")
|
||||||
item: Any,
|
Log.i(TAG, "rowList ${rowList.size}")
|
||||||
rowViewHolder: RowPresenter.ViewHolder,
|
rowList[tvViewModel!!.getRowPosition()].post {
|
||||||
row: Row
|
((rowList[tvViewModel.getRowPosition()] as RecyclerView).layoutManager as LinearLayoutManager).findViewByPosition(
|
||||||
) {
|
tvViewModel.getItemPosition()
|
||||||
if (item is TVViewModel) {
|
)?.requestFocus()
|
||||||
if (itemPosition != item.id.value!!) {
|
|
||||||
itemPosition = item.id.value!!
|
|
||||||
tvListViewModel.setItemPosition(itemPosition)
|
|
||||||
tvListViewModel.getTVViewModel(itemPosition)?.changed()
|
|
||||||
}
|
|
||||||
(activity as? MainActivity)?.switchMainFragment()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class ItemViewSelectedListener : OnItemViewSelectedListener {
|
fun setPosition(rowPosition: Int, itemPosition: Int) {
|
||||||
override fun onItemSelected(
|
rowList[rowPosition].post {
|
||||||
itemViewHolder: Presenter.ViewHolder?, item: Any?,
|
((rowList[rowPosition] as RecyclerView).layoutManager as LinearLayoutManager).findViewByPosition(
|
||||||
rowViewHolder: RowPresenter.ViewHolder, row: Row
|
itemPosition
|
||||||
) {
|
)?.requestFocus()
|
||||||
if (item is TVViewModel) {
|
|
||||||
tvListViewModel.setItemPositionCurrent(item.id.value!!)
|
|
||||||
(activity as MainActivity).keepRunnable()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,12 +212,8 @@ class MainFragment : BrowseSupportFragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun fragmentReady() {
|
fun fragmentReady() {
|
||||||
ready++
|
|
||||||
Log.i(TAG, "ready $ready")
|
|
||||||
if (ready == 4) {
|
|
||||||
// request.fetchPage()
|
// request.fetchPage()
|
||||||
tvListViewModel.getTVViewModel(itemPosition)?.changed()
|
tvListViewModel.getTVViewModel(itemPosition)?.changed()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun play(itemPosition: Int) {
|
fun play(itemPosition: Int) {
|
||||||
@@ -332,7 +295,15 @@ class MainFragment : BrowseSupportFragment() {
|
|||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.i(TAG, "onDestroy")
|
Log.i(TAG, "onDestroy")
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
handler.removeCallbacks(mUpdateProgramRunnable)
|
if (::mUpdateProgramRunnable.isInitialized) {
|
||||||
|
handler.removeCallbacks(mUpdateProgramRunnable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView() {
|
||||||
|
Log.i(TAG, "onDestroyView")
|
||||||
|
super.onDestroyView()
|
||||||
|
_binding = null
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ class PlayerFragment : Fragment() {
|
|||||||
): View {
|
): View {
|
||||||
_binding = PlayerBinding.inflate(inflater, container, false)
|
_binding = PlayerBinding.inflate(inflater, container, false)
|
||||||
playerView = _binding!!.playerView
|
playerView = _binding!!.playerView
|
||||||
(activity as MainActivity).playerFragment = this
|
|
||||||
playerView?.viewTreeObserver?.addOnGlobalLayoutListener(object :
|
playerView?.viewTreeObserver?.addOnGlobalLayoutListener(object :
|
||||||
ViewTreeObserver.OnGlobalLayoutListener {
|
ViewTreeObserver.OnGlobalLayoutListener {
|
||||||
override fun onGlobalLayout() {
|
override fun onGlobalLayout() {
|
||||||
@@ -63,6 +62,7 @@ class PlayerFragment : Fragment() {
|
|||||||
super.onPlayerError(error)
|
super.onPlayerError(error)
|
||||||
|
|
||||||
Log.e(TAG, "PlaybackException $error")
|
Log.e(TAG, "PlaybackException $error")
|
||||||
|
tvViewModel?.changed()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -86,6 +86,7 @@ class PlayerFragment : Fragment() {
|
|||||||
if (playerView != null && playerView!!.player?.isPlaying == false) {
|
if (playerView != null && playerView!!.player?.isPlaying == false) {
|
||||||
Log.i(TAG, "replay")
|
Log.i(TAG, "replay")
|
||||||
playerView!!.player?.prepare()
|
playerView!!.player?.prepare()
|
||||||
|
playerView!!.player?.play()
|
||||||
} else {
|
} else {
|
||||||
Log.i(TAG, "playing")
|
Log.i(TAG, "playing")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,54 +43,6 @@ class Request {
|
|||||||
private val input =
|
private val input =
|
||||||
"""{"mver":"1","subver":"1.2","host":"www.yangshipin.cn/#/tv/home?pid=","referer":"","canvas":"YSPANGLE(Apple,ANGLEMetalRenderer:AppleM1Pro,UnspecifiedVersion)"}""".toByteArray()
|
"""{"mver":"1","subver":"1.2","host":"www.yangshipin.cn/#/tv/home?pid=","referer":"","canvas":"YSPANGLE(Apple,ANGLEMetalRenderer:AppleM1Pro,UnspecifiedVersion)"}""".toByteArray()
|
||||||
|
|
||||||
private var mapping = mapOf(
|
|
||||||
"CCTV4K" to "CCTV4K 超高清",
|
|
||||||
"CCTV1" to "CCTV1 综合",
|
|
||||||
"CCTV2" to "CCTV2 财经",
|
|
||||||
"CCTV4" to "CCTV4 中文国际",
|
|
||||||
"CCTV5" to "CCTV5 体育",
|
|
||||||
"CCTV5+" to "CCTV5+ 体育赛事",
|
|
||||||
"CCTV7" to "CCTV7 国防军事",
|
|
||||||
"CCTV9" to "CCTV9 记录",
|
|
||||||
"CCTV10" to "CCTV10 科教",
|
|
||||||
"CCTV11" to "CCTV11 戏曲",
|
|
||||||
"CCTV12" to "CCTV12 社会与法",
|
|
||||||
"CCTV13" to "CCTV13 新闻",
|
|
||||||
"CCTV14" to "CCTV14 少儿",
|
|
||||||
"CCTV15" to "CCTV15 音乐",
|
|
||||||
"CCTV16-HD" to "CCTV16 奥林匹克",
|
|
||||||
"CCTV17" to "CCTV17 农业农村",
|
|
||||||
"CGTN" to "CGTN",
|
|
||||||
"CGTN法语频道" to "CGTN 法语频道",
|
|
||||||
"CGTN俄语频道" to "CGTN 俄语频道",
|
|
||||||
"CGTN阿拉伯语频道" to "CGTN 阿拉伯语频道",
|
|
||||||
"CGTN西班牙语频道" to "CGTN 西班牙语频道",
|
|
||||||
"CGTN外语纪录频道" to "CGTN 纪录频道",
|
|
||||||
"东方卫视" to "东方卫视",
|
|
||||||
"湖南卫视" to "湖南卫视",
|
|
||||||
"湖北卫视" to "湖北卫视",
|
|
||||||
"辽宁卫视" to "辽宁卫视",
|
|
||||||
"江苏卫视" to "江苏卫视",
|
|
||||||
"江西卫视" to "江西卫视",
|
|
||||||
"山东卫视" to "山东卫视",
|
|
||||||
"广东卫视" to "广东卫视",
|
|
||||||
"广西卫视" to "广西卫视",
|
|
||||||
"重庆卫视" to "重庆卫视",
|
|
||||||
"河南卫视" to "河南卫视",
|
|
||||||
"河北卫视" to "河北卫视",
|
|
||||||
"贵州卫视" to "贵州卫视",
|
|
||||||
"北京卫视" to "北京卫视",
|
|
||||||
"黑龙江卫视" to "黑龙江卫视",
|
|
||||||
"浙江卫视" to "浙江卫视",
|
|
||||||
"安徽卫视" to "安徽卫视",
|
|
||||||
"深圳卫视" to "深圳卫视",
|
|
||||||
"四川卫视" to "四川卫视",
|
|
||||||
"福建东南卫视" to "东南卫视",
|
|
||||||
"海南卫视" to "海南卫视",
|
|
||||||
"天津卫视" to "天津卫视",
|
|
||||||
"新疆卫视" to "新疆卫视",
|
|
||||||
)
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
handler.post(tokenRunnable)
|
handler.post(tokenRunnable)
|
||||||
}
|
}
|
||||||
@@ -364,7 +316,6 @@ class Request {
|
|||||||
|
|
||||||
for ((_, v) in TVList.list) {
|
for ((_, v) in TVList.list) {
|
||||||
for (v2 in v) {
|
for (v2 in v) {
|
||||||
Log.i(TAG, "$v2")
|
|
||||||
if (v2.title == item.channelName || v2.alias == item.channelName) {
|
if (v2.title == item.channelName || v2.alias == item.channelName) {
|
||||||
v2.pid = item.pid
|
v2.pid = item.pid
|
||||||
v2.sid = item.streamId
|
v2.sid = item.streamId
|
||||||
|
|||||||
@@ -1,22 +1,28 @@
|
|||||||
package com.lizongying.mytv
|
package com.lizongying.mytv
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import com.lizongying.mytv.databinding.DialogBinding
|
import com.lizongying.mytv.databinding.DialogBinding
|
||||||
|
|
||||||
|
|
||||||
class SettingFragment(private val versionName: String,
|
class SettingFragment(
|
||||||
private val channelReversal: Boolean,
|
private val versionName: String,
|
||||||
private val channelNum: Boolean,
|
private val versionCode: Long,
|
||||||
) :
|
private val channelReversal: Boolean,
|
||||||
|
private val channelNum: Boolean,
|
||||||
|
) :
|
||||||
DialogFragment() {
|
DialogFragment() {
|
||||||
|
|
||||||
private var _binding: DialogBinding? = null
|
private var _binding: DialogBinding? = null
|
||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
|
|
||||||
|
private lateinit var updateManager: UpdateManager
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setStyle(STYLE_NO_TITLE, 0)
|
setStyle(STYLE_NO_TITLE, 0)
|
||||||
@@ -43,14 +49,33 @@ class SettingFragment(private val versionName: String,
|
|||||||
(activity as MainActivity).saveChannelNum(isChecked)
|
(activity as MainActivity).saveChannelNum(isChecked)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateManager = UpdateManager(context, this, versionCode)
|
||||||
|
_binding?.checkVersion?.setOnClickListener(OnClickListenerCheckVersion(updateManager))
|
||||||
|
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setVersionName(versionName: String) {
|
||||||
|
binding.versionName.text = versionName
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class OnClickListenerCheckVersion(private val updateManager: UpdateManager) :
|
||||||
|
View.OnClickListener {
|
||||||
|
override fun onClick(view: View?) {
|
||||||
|
updateManager.checkAndUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
super.onDestroyView()
|
super.onDestroyView()
|
||||||
_binding = null
|
_binding = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
// updateManager.destroy()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val TAG = "SettingFragment"
|
const val TAG = "SettingFragment"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
package com.lizongying.mytv
|
package com.lizongying.mytv
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.util.Log
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import java.io.IOException
|
|
||||||
|
|
||||||
object TVList {
|
object TVList {
|
||||||
var list = mapOf(
|
var list = mapOf(
|
||||||
"央视频道" to listOf(
|
"央视频道" to listOf(
|
||||||
@@ -91,8 +84,8 @@ object TVList {
|
|||||||
0,
|
0,
|
||||||
"央视频道",
|
"央视频道",
|
||||||
"https://resources.yangshipin.cn/assets/oms/image/202306/741515efda91f03f455df8a7da4ee11fa9329139c276435cf0a9e2af398d5bf2.png?imageMogr2/format/webp",
|
"https://resources.yangshipin.cn/assets/oms/image/202306/741515efda91f03f455df8a7da4ee11fa9329139c276435cf0a9e2af398d5bf2.png?imageMogr2/format/webp",
|
||||||
"600001802",
|
"600108442",
|
||||||
"2022574301",
|
"2013693901",
|
||||||
"600001802",
|
"600001802",
|
||||||
true,
|
true,
|
||||||
mustToken = true
|
mustToken = true
|
||||||
@@ -859,10 +852,24 @@ object TVList {
|
|||||||
true,
|
true,
|
||||||
mustToken = false
|
mustToken = false
|
||||||
),
|
),
|
||||||
|
TV(
|
||||||
|
56,
|
||||||
|
"兵团卫视",
|
||||||
|
"兵团卫视",
|
||||||
|
listOf(),
|
||||||
|
0,
|
||||||
|
"地方频道",
|
||||||
|
"",
|
||||||
|
"600170344",
|
||||||
|
"2022606701",
|
||||||
|
"",
|
||||||
|
true,
|
||||||
|
mustToken = false
|
||||||
|
),
|
||||||
),
|
),
|
||||||
"国际频道" to listOf(
|
"国际频道" to listOf(
|
||||||
TV(
|
TV(
|
||||||
56,
|
57,
|
||||||
"CGTN",
|
"CGTN",
|
||||||
"CGTN",
|
"CGTN",
|
||||||
listOf("http://live.cgtn.com/1000/prog_index.m3u8"),
|
listOf("http://live.cgtn.com/1000/prog_index.m3u8"),
|
||||||
@@ -876,7 +883,7 @@ object TVList {
|
|||||||
mustToken = false
|
mustToken = false
|
||||||
),
|
),
|
||||||
TV(
|
TV(
|
||||||
57,
|
58,
|
||||||
"CGTN 法语频道",
|
"CGTN 法语频道",
|
||||||
"CGTN法语频道",
|
"CGTN法语频道",
|
||||||
listOf("https://livefr.cgtn.com/1000f/prog_index.m3u8"),
|
listOf("https://livefr.cgtn.com/1000f/prog_index.m3u8"),
|
||||||
@@ -890,7 +897,7 @@ object TVList {
|
|||||||
mustToken = false
|
mustToken = false
|
||||||
),
|
),
|
||||||
TV(
|
TV(
|
||||||
58,
|
59,
|
||||||
"CGTN 俄语频道",
|
"CGTN 俄语频道",
|
||||||
"CGTN俄语频道",
|
"CGTN俄语频道",
|
||||||
listOf("http://liveru.cgtn.com/1000r/prog_index.m3u8"),
|
listOf("http://liveru.cgtn.com/1000r/prog_index.m3u8"),
|
||||||
@@ -904,7 +911,7 @@ object TVList {
|
|||||||
mustToken = false
|
mustToken = false
|
||||||
),
|
),
|
||||||
TV(
|
TV(
|
||||||
59,
|
60,
|
||||||
"CGTN 阿拉伯语频道",
|
"CGTN 阿拉伯语频道",
|
||||||
"CGTN阿拉伯语频道",
|
"CGTN阿拉伯语频道",
|
||||||
listOf("http://livear.cgtn.com/1000a/prog_index.m3u8"),
|
listOf("http://livear.cgtn.com/1000a/prog_index.m3u8"),
|
||||||
@@ -918,7 +925,7 @@ object TVList {
|
|||||||
mustToken = false
|
mustToken = false
|
||||||
),
|
),
|
||||||
TV(
|
TV(
|
||||||
60,
|
61,
|
||||||
"CGTN 西班牙语频道",
|
"CGTN 西班牙语频道",
|
||||||
"CGTN西班牙语频道",
|
"CGTN西班牙语频道",
|
||||||
listOf(
|
listOf(
|
||||||
@@ -935,7 +942,7 @@ object TVList {
|
|||||||
mustToken = false
|
mustToken = false
|
||||||
),
|
),
|
||||||
TV(
|
TV(
|
||||||
61,
|
62,
|
||||||
"CGTN 纪录频道",
|
"CGTN 纪录频道",
|
||||||
"CGTN外语纪录频道",
|
"CGTN外语纪录频道",
|
||||||
listOf("https://livedoc.cgtn.com/500d/prog_index.m3u8"),
|
listOf("https://livedoc.cgtn.com/500d/prog_index.m3u8"),
|
||||||
@@ -950,63 +957,4 @@ object TVList {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化
|
|
||||||
*
|
|
||||||
* @param context Context
|
|
||||||
*/
|
|
||||||
fun init(context: Context) {
|
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
|
||||||
//获取本地版本号
|
|
||||||
val localVersion = ChannelUtils.getLocalVersion(context)
|
|
||||||
//获取服务器版本号
|
|
||||||
val serverVersion = try {
|
|
||||||
ChannelUtils.getServerVersion(context)
|
|
||||||
} catch (e: IOException) {
|
|
||||||
Log.e("TVList", "无法从服务器获取版本信息", e)
|
|
||||||
Integer.MIN_VALUE
|
|
||||||
}
|
|
||||||
//频道列表
|
|
||||||
val channelTVMap: MutableMap<String, MutableList<TV>> = mutableMapOf()
|
|
||||||
//是否从服务器更新
|
|
||||||
var updateFromServer = false
|
|
||||||
//获取频道列表
|
|
||||||
val tvList: List<TV> = if (localVersion < serverVersion) {
|
|
||||||
//获取服务器地址
|
|
||||||
val url = ChannelUtils.getServerUrl(context)
|
|
||||||
//是否从服务器更新
|
|
||||||
updateFromServer = true
|
|
||||||
Log.i("TVList", "从服务器获取频道信息")
|
|
||||||
try {
|
|
||||||
ChannelUtils.getServerChannel(url)
|
|
||||||
} catch (e: IOException) {
|
|
||||||
Log.e("TVList", "无法从服务器获取频道信息", e)
|
|
||||||
updateFromServer = false
|
|
||||||
ChannelUtils.getLocalChannel(context)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Log.i("TVList", "从本地获取频道信息")
|
|
||||||
//获取本地频道
|
|
||||||
ChannelUtils.getLocalChannel(context)
|
|
||||||
}
|
|
||||||
//按频道分类
|
|
||||||
for (tv in tvList) {
|
|
||||||
val key = tv.channel
|
|
||||||
if (channelTVMap.containsKey(key)) {
|
|
||||||
val list = channelTVMap[key]!!
|
|
||||||
list.add(tv)
|
|
||||||
channelTVMap[key] = list
|
|
||||||
} else {
|
|
||||||
channelTVMap[key] = mutableListOf(tv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//保存频道列表
|
|
||||||
list = channelTVMap
|
|
||||||
//保存版本号
|
|
||||||
if (updateFromServer) {
|
|
||||||
ChannelUtils.updateLocalChannel(context, serverVersion, tvList)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,143 @@
|
|||||||
package com.lizongying.mytv
|
package com.lizongying.mytv
|
||||||
|
|
||||||
class UpdateManager {
|
import android.app.DownloadManager
|
||||||
|
import android.app.DownloadManager.Request
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.IntentFilter
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Environment
|
||||||
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
|
import com.lizongying.mytv.api.Release
|
||||||
|
import com.lizongying.mytv.requests.MyRequest
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
|
||||||
|
class UpdateManager(
|
||||||
|
private var context: Context?,
|
||||||
|
private var settingFragment: SettingFragment,
|
||||||
|
private var versionCode: Long
|
||||||
|
) :
|
||||||
|
ConfirmationDialogFragment.ConfirmationDialogListener {
|
||||||
|
|
||||||
|
private var myRequest = MyRequest()
|
||||||
|
private var release: Release? = null
|
||||||
|
|
||||||
|
private var downloadReceiver: DownloadReceiver? = null
|
||||||
|
|
||||||
|
fun checkAndUpdate() {
|
||||||
|
CoroutineScope(Dispatchers.Main).launch {
|
||||||
|
try {
|
||||||
|
release = myRequest.getRelease()
|
||||||
|
updateUI(release)
|
||||||
|
Log.i(TAG, "versionCode $versionCode ${release?.data?.versionCode}")
|
||||||
|
if (release != null) {
|
||||||
|
if (release?.data?.versionCode!! >= versionCode) {
|
||||||
|
val dialog = ConfirmationDialogFragment(this@UpdateManager)
|
||||||
|
dialog.show(settingFragment.fragmentManager, "ConfirmationDialogFragment")
|
||||||
|
} else {
|
||||||
|
Toast.makeText(context, "不需要更新", Toast.LENGTH_LONG)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Error occurred: ${e.message}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateUI(release: Release?) {
|
||||||
|
if (release?.data?.versionName.isNullOrEmpty()) {
|
||||||
|
settingFragment.setVersionName("版本获取失败")
|
||||||
|
} else {
|
||||||
|
settingFragment.setVersionName("最新版本:${release?.data?.versionName!!}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startDownload(release: Release) {
|
||||||
|
val apkFileName = "my-tv-${release.data.versionName}.apk"
|
||||||
|
Log.i(TAG, "apkFileName $apkFileName")
|
||||||
|
val downloadManager =
|
||||||
|
context!!.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
||||||
|
val request = Request(Uri.parse(release.data.downloadUrl))
|
||||||
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkFileName)
|
||||||
|
request.setTitle("New Version Download")
|
||||||
|
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
||||||
|
|
||||||
|
// 获取下载任务的引用
|
||||||
|
val downloadReference = downloadManager.enqueue(request)
|
||||||
|
|
||||||
|
downloadReceiver = DownloadReceiver(context!!, apkFileName, downloadReference)
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
context!!.registerReceiver(
|
||||||
|
downloadReceiver,
|
||||||
|
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE),
|
||||||
|
Context.RECEIVER_NOT_EXPORTED,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
context!!.registerReceiver(
|
||||||
|
downloadReceiver,
|
||||||
|
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DownloadReceiver(
|
||||||
|
private val context: Context,
|
||||||
|
private val apkFileName: String,
|
||||||
|
private val downloadReference: Long
|
||||||
|
) : BroadcastReceiver() {
|
||||||
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
val reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
||||||
|
Log.i(TAG, "reference $reference")
|
||||||
|
val progress = intent.getIntExtra("progress", 0)
|
||||||
|
Log.i(TAG, "progress $progress")
|
||||||
|
|
||||||
|
// 检查是否是我们发起的下载
|
||||||
|
if (reference == downloadReference) {
|
||||||
|
// 下载完成,触发安装
|
||||||
|
installNewVersion()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun installNewVersion() {
|
||||||
|
val installIntent = Intent(Intent.ACTION_VIEW)
|
||||||
|
val apkUri = Uri.fromFile(
|
||||||
|
File(
|
||||||
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
|
||||||
|
apkFileName
|
||||||
|
)
|
||||||
|
)
|
||||||
|
installIntent.setDataAndType(apkUri, "application/vnd.android.package-archive")
|
||||||
|
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
context.startActivity(installIntent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "UpdateManager"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onConfirm() {
|
||||||
|
Log.i(TAG, "onConfirm $release")
|
||||||
|
release?.let { startDownload(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCancel() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
if (downloadReceiver != null) {
|
||||||
|
context!!.unregisterReceiver(downloadReceiver)
|
||||||
|
Log.i(TAG, "destroy downloadReceiver")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,55 +1,65 @@
|
|||||||
package com.lizongying.mytv
|
package com.lizongying.mytv
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
import android.os.SystemClock
|
|
||||||
import android.util.Log
|
|
||||||
import android.util.TypedValue
|
import android.util.TypedValue
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.lizongying.mytv.api.TimeResponse
|
import com.lizongying.mytv.api.TimeResponse
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.io.File
|
||||||
|
import java.io.IOException
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
object Utils {
|
object Utils {
|
||||||
|
private var between: Long = 0
|
||||||
|
|
||||||
fun getDateFormat(format: String): String {
|
fun getDateFormat(format: String): String {
|
||||||
return SimpleDateFormat(format, Locale.CHINA).format(Date())
|
return SimpleDateFormat(
|
||||||
|
format,
|
||||||
|
Locale.CHINA
|
||||||
|
).format(Date(System.currentTimeMillis() - between))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getDateTimestamp(): Long {
|
fun getDateTimestamp(): Long {
|
||||||
return Date().time / 1000
|
return (System.currentTimeMillis() - between) / 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun init() {
|
||||||
init {
|
var currentTimeMillis: Long = 0
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
try {
|
||||||
updateTimestampFromServer()
|
currentTimeMillis = getTimestampFromServer()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Failed to retrieve timestamp from server: ${e.message}")
|
||||||
}
|
}
|
||||||
|
between = System.currentTimeMillis() - currentTimeMillis
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从服务器获取时间戳
|
* 从服务器获取时间戳
|
||||||
* @return Long 时间戳
|
* @return Long 时间戳
|
||||||
*/
|
*/
|
||||||
private suspend fun updateTimestampFromServer() {
|
private suspend fun getTimestampFromServer(): Long {
|
||||||
val currentTimeMillis = withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
val client = okhttp3.OkHttpClient.Builder()
|
val client = okhttp3.OkHttpClient.Builder()
|
||||||
.connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS)
|
.connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS)
|
||||||
.readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build()
|
.readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build()
|
||||||
client.newCall(
|
val request = okhttp3.Request.Builder()
|
||||||
okhttp3.Request.Builder()
|
.url("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp")
|
||||||
.url("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp")
|
.build()
|
||||||
.build()
|
try {
|
||||||
).execute().use { response ->
|
client.newCall(request).execute().use { response ->
|
||||||
if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response")
|
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
||||||
val string = response.body()?.string()
|
val string = response.body()?.string()
|
||||||
Gson().fromJson(string, TimeResponse::class.java).data.t.toLong()
|
Gson().fromJson(string, TimeResponse::class.java).data.t.toLong()
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
// Handle network errors
|
||||||
|
throw IOException("Error during network request", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SystemClock.setCurrentTimeMillis(currentTimeMillis)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun dpToPx(dp: Float): Int {
|
fun dpToPx(dp: Float): Int {
|
||||||
@@ -63,4 +73,49 @@ object Utils {
|
|||||||
TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), Resources.getSystem().displayMetrics
|
TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), Resources.getSystem().displayMetrics
|
||||||
).toInt()
|
).toInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取可读写的目录
|
||||||
|
*
|
||||||
|
* @param context 应用环境信息
|
||||||
|
*
|
||||||
|
* @return 可读写的目录
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
fun getAppDirectory(context: Context): File {
|
||||||
|
return context.filesDir
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新channels.json
|
||||||
|
*
|
||||||
|
* @param context 应用环境信息
|
||||||
|
*
|
||||||
|
* @return 无
|
||||||
|
*
|
||||||
|
* @throws IOException 网络请求失败
|
||||||
|
*/
|
||||||
|
fun updateChannel(context: Context) {
|
||||||
|
val client = okhttp3.OkHttpClient()
|
||||||
|
val request = okhttp3.Request.Builder().url(getServerUrl(context)).build()
|
||||||
|
client.newCall(request).execute().use { response ->
|
||||||
|
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
||||||
|
val body = response.body()
|
||||||
|
//覆盖channels.json
|
||||||
|
val file = File(getAppDirectory(context), "channels.json")
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.createNewFile()
|
||||||
|
}
|
||||||
|
file.writeText(body!!.string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从res/values/server.xml获取服务器地址
|
||||||
|
* @param context 应用环境信息
|
||||||
|
* @return 服务器地址
|
||||||
|
*/
|
||||||
|
private fun getServerUrl(context: Context): String {
|
||||||
|
return context.resources.getString(R.string.server_url)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,9 +13,11 @@ import javax.net.ssl.X509TrustManager
|
|||||||
class ApiClient {
|
class ApiClient {
|
||||||
private val yspUrl = "https://player-api.yangshipin.cn/"
|
private val yspUrl = "https://player-api.yangshipin.cn/"
|
||||||
private val myUrl = "https://lyrics.run/"
|
private val myUrl = "https://lyrics.run/"
|
||||||
|
private val devUrl = "http://10.0.2.2:8081/"
|
||||||
private val protoUrl = "https://capi.yangshipin.cn/"
|
private val protoUrl = "https://capi.yangshipin.cn/"
|
||||||
private val traceUrl = "https://btrace.yangshipin.cn/"
|
private val traceUrl = "https://btrace.yangshipin.cn/"
|
||||||
|
|
||||||
|
|
||||||
private var okHttpClient = getUnsafeOkHttpClient()
|
private var okHttpClient = getUnsafeOkHttpClient()
|
||||||
|
|
||||||
val yspApiService: YSPApiService by lazy {
|
val yspApiService: YSPApiService by lazy {
|
||||||
@@ -34,6 +36,14 @@ class ApiClient {
|
|||||||
.build().create(YSPTokenService::class.java)
|
.build().create(YSPTokenService::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val releaseService: ReleaseService by lazy {
|
||||||
|
Retrofit.Builder()
|
||||||
|
.baseUrl(devUrl)
|
||||||
|
.client(okHttpClient)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.build().create(ReleaseService::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
val yspProtoService: YSPProtoService by lazy {
|
val yspProtoService: YSPProtoService by lazy {
|
||||||
Retrofit.Builder()
|
Retrofit.Builder()
|
||||||
.baseUrl(protoUrl)
|
.baseUrl(protoUrl)
|
||||||
|
|||||||
@@ -3,12 +3,25 @@ package com.lizongying.mytv.api
|
|||||||
data class Info(
|
data class Info(
|
||||||
val code: Int?,
|
val code: Int?,
|
||||||
val msg: String?,
|
val msg: String?,
|
||||||
val data: InfoData,
|
val data: Data,
|
||||||
)
|
) {
|
||||||
|
data class Data(
|
||||||
|
val token: String,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
data class InfoData(
|
data class Release(
|
||||||
val token: String,
|
val code: Int?,
|
||||||
)
|
val msg: String?,
|
||||||
|
val data: Data,
|
||||||
|
) {
|
||||||
|
data class Data(
|
||||||
|
val versionName: String,
|
||||||
|
val versionCode: Int,
|
||||||
|
val downloadUrl: String,
|
||||||
|
val updateTime: Int,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
data class TimeResponse(
|
data class TimeResponse(
|
||||||
val data: Time
|
val data: Time
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.lizongying.mytv.api
|
||||||
|
|
||||||
|
import retrofit2.Call
|
||||||
|
import retrofit2.http.GET
|
||||||
|
|
||||||
|
|
||||||
|
interface ReleaseService {
|
||||||
|
@GET("my-tv/v1/release")
|
||||||
|
fun getRelease(
|
||||||
|
): Call<Release>
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.lizongying.mytv.requests
|
||||||
|
|
||||||
|
import com.lizongying.mytv.api.ApiClient
|
||||||
|
import com.lizongying.mytv.api.Release
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import retrofit2.Call
|
||||||
|
import retrofit2.Callback
|
||||||
|
import retrofit2.Response
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
|
||||||
|
class MyRequest {
|
||||||
|
private var releaseService = ApiClient().releaseService
|
||||||
|
|
||||||
|
suspend fun getRelease(): Release? {
|
||||||
|
return withContext(Dispatchers.IO) {
|
||||||
|
fetchRelease()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun fetchRelease(): Release? {
|
||||||
|
return suspendCoroutine { continuation ->
|
||||||
|
releaseService.getRelease()
|
||||||
|
.enqueue(object : Callback<Release> {
|
||||||
|
override fun onResponse(call: Call<Release>, response: Response<Release>) {
|
||||||
|
if (response.isSuccessful) {
|
||||||
|
continuation.resume(response.body())
|
||||||
|
} else {
|
||||||
|
continuation.resume(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(call: Call<Release>, t: Throwable) {
|
||||||
|
continuation.resume(null)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "MyRequest"
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
@@ -27,7 +27,31 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="10dp"
|
android:layout_marginTop="10dp"
|
||||||
android:text="当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
|
android:text="当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
|
||||||
></TextView>
|
/>
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="300dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
android:id="@+id/check_version"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="10dp"
|
||||||
|
android:text="@string/check_version"
|
||||||
|
/>
|
||||||
|
<ProgressBar
|
||||||
|
android:visibility="gone"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
/>
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/version_name"
|
||||||
|
android:text=""
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
<Switch
|
<Switch
|
||||||
android:id="@+id/switch_channel_reversal"
|
android:id="@+id/switch_channel_reversal"
|
||||||
android:text="@string/title_channel_reversal"
|
android:text="@string/title_channel_reversal"
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/row"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/row_header"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="start"
|
||||||
|
android:layout_marginTop="0dp"
|
||||||
|
android:layout_marginStart="10dp"
|
||||||
|
android:layout_marginBottom="5dp"
|
||||||
|
android:gravity="start"
|
||||||
|
android:textColor="#FFEEEEEE"
|
||||||
|
android:textSize="22sp" />
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/row_items"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
</androidx.recyclerview.widget.RecyclerView>
|
||||||
|
</LinearLayout>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/show_fragment"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
<ScrollView
|
||||||
|
android:id="@+id/scroll_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/content"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical">
|
||||||
|
</LinearLayout>
|
||||||
|
</ScrollView>
|
||||||
|
</FrameLayout>
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<integer name="local_channel_version">1</integer>
|
<string name="server_url">http://localhost:8080</string>
|
||||||
<string name="server_version_url">https://raw.githubusercontent.com/LeGend-wLw/my-tv-json-utils/main/version.txt</string>
|
|
||||||
<string name="server_url">https://github.com/LeGend-wLw/my-tv-json-utils/raw/main/channels.json</string>
|
|
||||||
</resources>
|
</resources>
|
||||||
@@ -2,4 +2,5 @@
|
|||||||
<string name="app_name">我的电视</string>
|
<string name="app_name">我的电视</string>
|
||||||
<string name="title_channel_reversal">换台反转</string>
|
<string name="title_channel_reversal">换台反转</string>
|
||||||
<string name="title_channel_num">数字选台</string>
|
<string name="title_channel_num">数字选台</string>
|
||||||
|
<string name="check_version">检查更新</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -1,10 +1,25 @@
|
|||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
<style name="CustomImageCardViewStyle" parent="Widget.Leanback.ImageCardViewStyle">
|
<style name="CustomImageCardViewStyle" parent="Widget.Leanback.ImageCardViewStyle">
|
||||||
<item name="lbImageCardViewType">Title|Content</item>
|
<item name="cardType">infoUnder</item>
|
||||||
|
<item name="infoVisibility">always</item>
|
||||||
|
<!-- In order to keep backward compatibility we have to create an icon on right. -->
|
||||||
|
<item name="lbImageCardViewType">Title|Content|IconOnRight</item>
|
||||||
|
<!-- Deprecated. Use 'Widget.Leanback.ImageCardView.InfoAreaStyle' instead. -->
|
||||||
|
<item name="infoAreaBackground">@null</item>
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<style name="CustomImageCardViewImageStyle" parent="Widget.Leanback.ImageCardView.ImageStyle">
|
||||||
|
<item name="android:layout_width">143dp</item>
|
||||||
|
<item name="android:layout_height">50dp</item>
|
||||||
|
<item name="android:adjustViewBounds">true</item>
|
||||||
|
<item name="android:contentDescription">@null</item>
|
||||||
|
<item name="android:scaleType">centerCrop</item>
|
||||||
|
<item name="layout_viewType">main</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="CustomImageCardTheme" parent="Theme.Leanback">
|
<style name="CustomImageCardTheme" parent="Theme.Leanback">
|
||||||
<item name="imageCardViewStyle">@style/CustomImageCardViewStyle</item>
|
<item name="imageCardViewStyle">@style/CustomImageCardViewStyle</item>
|
||||||
|
<item name="imageCardViewImageStyle">@style/CustomImageCardViewImageStyle</item>
|
||||||
</style>
|
</style>
|
||||||
</resources>
|
</resources>
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 73 KiB |
Reference in New Issue
Block a user