From 61f62121f256d1a96c4853eff00072013b0a1673 Mon Sep 17 00:00:00 2001 From: Li ZongYing Date: Wed, 10 Jan 2024 18:01:25 +0800 Subject: [PATCH] menu auto close --- README.md | 5 + .../java/com/lizongying/mytv/MainActivity.kt | 13 +++ .../java/com/lizongying/mytv/MainFragment.kt | 9 ++ .../main/java/com/lizongying/mytv/Request.kt | 95 ++++++++----------- .../java/com/lizongying/mytv/api/LiveInfo.kt | 1 + .../com/lizongying/mytv/models/TVViewModel.kt | 33 +++++++ 6 files changed, 103 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index ad628940..3f5a6e01 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,11 @@ ## 更新日志 +### v1.3.3 + +* 部分错误会提示用户 +* 菜单3秒钟后自动关闭 + ### v1.3.2 * 增加重试,减少因网络问题导致的播放失败 diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index bef22552..dc4274bb 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -35,6 +35,9 @@ class MainActivity : FragmentActivity() { private lateinit var gestureDetector: GestureDetector + private val handler = Handler() + private val delay: Long = 3000 + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -86,6 +89,7 @@ class MainActivity : FragmentActivity() { if (mainFragment.isHidden) { transaction.show(mainFragment) + keepRunnable() } else { transaction.hide(mainFragment) } @@ -93,6 +97,15 @@ class MainActivity : FragmentActivity() { transaction.commit() } + fun keepRunnable() { + handler.removeCallbacks(hideRunnable) + handler.postDelayed(hideRunnable, delay) + } + + private val hideRunnable = Runnable { + supportFragmentManager.beginTransaction().hide(mainFragment).commit() + } + private fun mainFragmentIsHidden(): Boolean { return mainFragment.isHidden } diff --git a/app/src/main/java/com/lizongying/mytv/MainFragment.kt b/app/src/main/java/com/lizongying/mytv/MainFragment.kt index 5a99a075..18a941bb 100644 --- a/app/src/main/java/com/lizongying/mytv/MainFragment.kt +++ b/app/src/main/java/com/lizongying/mytv/MainFragment.kt @@ -6,6 +6,7 @@ import android.os.Bundle import android.os.Handler import android.os.Looper import android.util.Log +import android.widget.Toast import androidx.leanback.app.BrowseSupportFragment import androidx.leanback.widget.ArrayObjectAdapter import androidx.leanback.widget.HeaderItem @@ -63,6 +64,13 @@ class MainFragment : BrowseSupportFragment() { handler.post(mUpdateProgramRunnable) tvListViewModel.getTVListViewModel().value?.forEach { tvViewModel -> + tvViewModel.errInfo.observe(viewLifecycleOwner) { _ -> + if (tvViewModel.errInfo.value != null + && tvViewModel.id.value == itemPosition + ) { + Toast.makeText(context, tvViewModel.errInfo.value, Toast.LENGTH_SHORT).show() + } + } tvViewModel.ready.observe(viewLifecycleOwner) { _ -> // not first time && channel not change @@ -305,6 +313,7 @@ class MainFragment : BrowseSupportFragment() { ) { if (item is TVViewModel) { tvListViewModel.setItemPositionCurrent(item.id.value!!) + (activity as MainActivity).keepRunnable() } } } diff --git a/app/src/main/java/com/lizongying/mytv/Request.kt b/app/src/main/java/com/lizongying/mytv/Request.kt index 3d03da17..5f4616be 100644 --- a/app/src/main/java/com/lizongying/mytv/Request.kt +++ b/app/src/main/java/com/lizongying/mytv/Request.kt @@ -86,8 +86,6 @@ class Request { "海南卫视" to "海南卫视", ) - private var token: String? = null - fun initYSP(context: Context) { ysp = YSP(context) } @@ -135,21 +133,26 @@ class Request { Log.e(TAG, "$title key error") if (tvModel.retryTimes < tvModel.retryMaxTimes) { tvModel.retryTimes++ - fetchData(tvModel) + fetchVideo(tvModel, cookie) } } } else { - Log.e(TAG, "$title url error $request") - if (tvModel.retryTimes < tvModel.retryMaxTimes) { - tvModel.retryTimes++ - fetchData(tvModel) + if (liveInfo?.data?.errinfo != null && liveInfo.data.errinfo != "success!") { + Log.e(TAG, "$title url error ${liveInfo.data.errinfo}") + tvModel.setErrInfo(liveInfo.data.errinfo) + } else { + Log.e(TAG, "$title url error $request $liveInfo") + if (tvModel.retryTimes < tvModel.retryMaxTimes) { + tvModel.retryTimes++ + fetchVideo(tvModel, cookie) + } } } } else { Log.e(TAG, "$title status error") if (tvModel.retryTimes < tvModel.retryMaxTimes) { tvModel.retryTimes++ - fetchData(tvModel) + fetchVideo(tvModel, cookie) } } } @@ -158,61 +161,47 @@ class Request { Log.e(TAG, "$title request error") if (tvModel.retryTimes < tvModel.retryMaxTimes) { tvModel.retryTimes++ - fetchData(tvModel) + fetchVideo(tvModel, cookie) } } }) } - fun fetchData(tvModel: TVViewModel) { - var cookie = "guid=1; vplatform=109" - val channels = arrayOf( - "CCTV3 综艺", - "CCTV6 电影", - "CCTV8 电视剧", - "风云剧场", - "第一剧场", - "怀旧剧场", - "世界地理", - "风云音乐", - "兵器科技", - "风云足球", - "高尔夫网球", - "女性时尚", - "央视文化精品", - "央视台球", - "电视指南", - "卫生健康", - ) - if (tvModel.title.value in channels) { - yspTokenService.getInfo() - .enqueue(object : Callback { - override fun onResponse(call: Call, response: Response) { - if (response.isSuccessful) { - val info = response.body() - token = info?.data?.token - Log.i(TAG, "info success $token") - cookie = - "guid=1; vplatform=109; yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token" - fetchVideo(tvModel, cookie) - } else { - Log.e(TAG, "info status error") - if (tvModel.retryTimes < tvModel.retryMaxTimes) { - tvModel.retryTimes++ - fetchData(tvModel) - } - } - } - - override fun onFailure(call: Call, t: Throwable) { - Log.e(TAG, "info request error $t") + fun fetchVideo(tvModel: TVViewModel) { + yspTokenService.getInfo() + .enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + val info = response.body() + val token = info?.data?.token + Log.i(TAG, "info success $token") + val cookie = + "guid=1; vplatform=109; yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token" + fetchVideo(tvModel, cookie) + } else { + Log.e(TAG, "info status error") if (tvModel.retryTimes < tvModel.retryMaxTimes) { tvModel.retryTimes++ - fetchData(tvModel) + fetchVideo(tvModel) } } - }) + } + + override fun onFailure(call: Call, t: Throwable) { + Log.e(TAG, "info request error $t") + if (tvModel.retryTimes < tvModel.retryMaxTimes) { + tvModel.retryTimes++ + fetchVideo(tvModel) + } + } + }) + } + + fun fetchData(tvModel: TVViewModel) { + if (tvModel.needToken) { + fetchVideo(tvModel) } else { + val cookie = "guid=1; vplatform=109" fetchVideo(tvModel, cookie) } } diff --git a/app/src/main/java/com/lizongying/mytv/api/LiveInfo.kt b/app/src/main/java/com/lizongying/mytv/api/LiveInfo.kt index 48176afa..43f64d6d 100644 --- a/app/src/main/java/com/lizongying/mytv/api/LiveInfo.kt +++ b/app/src/main/java/com/lizongying/mytv/api/LiveInfo.kt @@ -15,6 +15,7 @@ data class LiveInfo( data class LiveInfoData( val chanll: String, val playurl: String, + val errinfo: String, ) data class LiveInfoRequest( diff --git a/app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt b/app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt index e4ee5410..dfe9acc9 100644 --- a/app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt +++ b/app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt @@ -23,6 +23,10 @@ class TVViewModel(private var tv: TV) : ViewModel() { var retryMaxTimes: Int = 3 var programUpdateTime: Long = 0 + private val _errInfo = MutableLiveData() + val errInfo: LiveData + get() = _errInfo + private val _programId = MutableLiveData() val programId: LiveData get() = _programId @@ -71,6 +75,27 @@ class TVViewModel(private var tv: TV) : ViewModel() { var seq = 0 + var needToken = false + + private val channelsNeedToken = arrayOf( + "CCTV3 综艺", + "CCTV6 电影", + "CCTV8 电视剧", + "风云剧场", + "第一剧场", + "怀旧剧场", + "世界地理", + "风云音乐", + "兵器科技", + "风云足球", + "高尔夫网球", + "女性时尚", + "央视文化精品", + "央视台球", + "电视指南", + "卫生健康", + ) + fun addVideoUrl(url: String) { if (_videoUrl.value?.isNotEmpty() == true) { if (_videoUrl.value!!.last().contains("cctv.cn")) { @@ -121,6 +146,10 @@ class TVViewModel(private var tv: TV) : ViewModel() { _pid.value = tv.pid _sid.value = tv.sid _program.value = mutableListOf() + + if (tv.title in channelsNeedToken) { + needToken = true + } } fun getRowPosition(): Int { @@ -139,6 +168,10 @@ class TVViewModel(private var tv: TV) : ViewModel() { itemPosition = position } + fun setErrInfo(info: String) { + _errInfo.value = info + } + fun update(t: TV) { tv = t }