Skip to content

Commit

Permalink
opitimize request
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed May 1, 2024
1 parent df5b006 commit 39e5cc7
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 74 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 更新日志

### v1.9.0(通用)

* 减少视频播放失败情况
* 全面屏手机居中显示

### v1.8.8(通用)

* 样式优化
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv/ChannelFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ChannelFragment : Fragment() {
binding.main.layoutParams.width = application.shouldWidthPx()
binding.main.layoutParams.height = application.shouldHeightPx()

(activity as MainActivity).fragmentReady("ChannelFragment")
(activity as MainActivity).fragmentReady(TAG)
return binding.root
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv/InfoFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class InfoFragment : Fragment() {

_binding!!.root.visibility = View.GONE

(activity as MainActivity).fragmentReady("InfoFragment")
(activity as MainActivity).fragmentReady(TAG)
return binding.root
}

Expand Down
79 changes: 55 additions & 24 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
import android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
import android.view.WindowManager
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.lizongying.mytv.models.TVViewModel
Expand Down Expand Up @@ -65,6 +66,13 @@ class MainActivity : FragmentActivity(), Request.RequestListener {

Request.setRequestListener(this)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val lp = window.attributes
lp.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
window.setAttributes(lp)
}

window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.decorView.systemUiVisibility =
Expand All @@ -89,28 +97,32 @@ class MainActivity : FragmentActivity(), Request.RequestListener {
.hide(mainFragment)
.hide(errorFragment)
// .hide(loadingFragment)
.hide(timeFragment)
.commit()
}
gestureDetector = GestureDetector(this, GestureListener())

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val connectivityManager =
getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.registerDefaultNetworkCallback(object :
ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
Log.i(TAG, "net ${Build.VERSION.SDK_INT}")
if (this@MainActivity.isNetworkConnected) {
Log.i(TAG, "net isNetworkConnected")
ready++
}
}
})
} else {
Log.i(TAG, "net ${Build.VERSION.SDK_INT}")
ready++
}
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// val connectivityManager =
// getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
// connectivityManager.registerDefaultNetworkCallback(object :
// ConnectivityManager.NetworkCallback() {
// override fun onAvailable(network: Network) {
// super.onAvailable(network)
// Log.i(TAG, "net ${Build.VERSION.SDK_INT}")
// if ([email protected]) {
// Log.i(TAG, "net isNetworkConnected")
// ready++
// }
// }
// })
// } else {
// Log.i(TAG, "net ${Build.VERSION.SDK_INT}")
// ready++
// }

showTime()
mainFragment.changeMenu()
}

fun showInfoFragment(tvViewModel: TVViewModel) {
Expand Down Expand Up @@ -211,6 +223,7 @@ class MainActivity : FragmentActivity(), Request.RequestListener {
handler.removeCallbacks(hideSetting)
handler.postDelayed(hideSetting, delayHideSetting)
showTime()
mainFragment.changeMenu()
}

fun settingNeverHide() {
Expand Down Expand Up @@ -238,20 +251,37 @@ class MainActivity : FragmentActivity(), Request.RequestListener {
fun fragmentReady(tag: String) {
ready++
Log.i(TAG, "ready $tag $ready ")
if (ready == 8) {
if (ready == 7) {
mainFragment.fragmentReady()
showTime()
}
}

private fun showTime() {
Log.i(TAG, "showTime ${SP.time}")
if (SP.time) {
timeFragment.show()
showFragment(timeFragment)
} else {
timeFragment.hide()
hideFragment(timeFragment)
}
}

private fun showFragment(fragment: Fragment) {
if (!fragment.isHidden) {
return
}
mainFragment.changeStyle()

supportFragmentManager.beginTransaction()
.show(fragment)
.commitNow()
}

private fun hideFragment(fragment: Fragment) {
if (fragment.isHidden) {
return
}

supportFragmentManager.beginTransaction()
.hide(fragment)
.commitNow()
}

override fun onTouchEvent(event: MotionEvent?): Boolean {
Expand Down Expand Up @@ -326,6 +356,7 @@ class MainActivity : FragmentActivity(), Request.RequestListener {
if (settingFragment.isVisible) {
settingFragment.dismiss()
showTime()
mainFragment.changeMenu()
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/lizongying/mytv/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
}
}
}
(activity as MainActivity).fragmentReady("MainFragment")
(activity as MainActivity).fragmentReady(TAG)
}
}

fun changeStyle() {
fun changeMenu() {
if (SP.grid) {
for (i in rowList) {
if (i is RecyclerView) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/lizongying/mytv/PlayerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class PlayerFragment : Fragment(), SurfaceHolder.Callback {
})
}
})
(activity as MainActivity).fragmentReady("PlayerFragment")
(activity as MainActivity).fragmentReady(TAG)
return _binding!!.root
}

Expand Down Expand Up @@ -154,7 +154,7 @@ class PlayerFragment : Fragment(), SurfaceHolder.Callback {
}

companion object {
private const val TAG = "PlaybackVideoFragment"
private const val TAG = "PlayerFragment"
}

override fun surfaceCreated(holder: SurfaceHolder) {
Expand Down
26 changes: 6 additions & 20 deletions app/src/main/java/com/lizongying/mytv/TimeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,20 @@ class TimeFragment : Fragment() {
binding.main.layoutParams.width = application.shouldWidthPx()
binding.main.layoutParams.height = application.shouldHeightPx()

(activity as MainActivity).fragmentReady("TimeFragment")
(activity as MainActivity).fragmentReady(TAG)
return binding.root
}

override fun onResume() {
super.onResume()
if (view?.visibility == View.VISIBLE) {
override fun onHiddenChanged(hidden: Boolean) {
super.onHiddenChanged(hidden)
if (!hidden) {
handler.removeCallbacks(showRunnable)
handler.postDelayed(showRunnable, 0)
} else {
handler.removeCallbacks(showRunnable)
}
}

override fun onPause() {
super.onPause()
handler.removeCallbacks(showRunnable)
}

private val showRunnable: Runnable = Runnable {
run {
if (_binding == null) {
Expand All @@ -67,17 +64,6 @@ class TimeFragment : Fragment() {
}
}

fun show() {
view?.visibility = View.VISIBLE
handler.removeCallbacks(showRunnable)
handler.postDelayed(showRunnable, 0)
}

fun hide() {
view?.visibility = View.GONE
handler.removeCallbacks(showRunnable)
}

override fun onDestroyView() {
handler.removeCallbacks(showRunnable)
super.onDestroyView()
Expand Down
87 changes: 63 additions & 24 deletions app/src/main/java/com/lizongying/mytv/requests/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ object Request {
if (!tvModel.getTV().mustToken) {
fetchAuth(tvModel, cookie)
} else {
// TODO
val err = "错误"
Log.e(TAG, "$title $err")
tvModel.setErrInfo(err)
}
}
} else {
Expand All @@ -144,7 +146,9 @@ object Request {
if (!tvModel.getTV().mustToken) {
fetchAuth(tvModel, cookie)
} else {
// TODO
val err = "错误"
Log.e(TAG, "$title $err")
tvModel.setErrInfo(err)
}
}
} else {
Expand All @@ -171,7 +175,9 @@ object Request {
if (!tvModel.getTV().mustToken) {
fetchAuth(tvModel, cookie)
} else {
// TODO
val err = "错误"
Log.e(TAG, "$title $err")
tvModel.setErrInfo(err)
}
}
} else {
Expand Down Expand Up @@ -241,19 +247,30 @@ object Request {
if (needToken && tvModel.tokenYSPRetryTimes < tvModel.tokenYSPRetryMaxTimes) {
tvModel.tokenYSPRetryTimes++
tvModel.needGetToken = true
fetchVideo(tvModel)
// fetchAuth(tvModel)
if (needAuth) {
fetchAuth(tvModel)
} else {
fetchVideo(tvModel)
}
} else {
if (!tvModel.getTV().mustToken) {
fetchVideo(tvModel, cookie)
// fetchAuth(tvModel, cookie)
if (needAuth) {
fetchAuth(tvModel, cookie)
} else {
fetchVideo(tvModel, cookie)
}
} else {
// TODO
val err = "错误"
Log.e(TAG, "$title $err")
tvModel.setErrInfo(err)
}
}
} else {
fetchVideo(tvModel, cookie)
// fetchAuth(tvModel, cookie)
if (needAuth) {
fetchAuth(tvModel, cookie)
} else {
fetchVideo(tvModel, cookie)
}
}
} else {
val err = "结果错误"
Expand All @@ -274,19 +291,30 @@ object Request {
if (needToken && tvModel.tokenYSPRetryTimes < tvModel.tokenYSPRetryMaxTimes) {
tvModel.tokenYSPRetryTimes++
tvModel.needGetToken = true
fetchVideo(tvModel)
// fetchAuth(tvModel)
if (needAuth) {
fetchAuth(tvModel)
} else {
fetchVideo(tvModel)
}
} else {
if (!tvModel.getTV().mustToken) {
fetchVideo(tvModel, cookie)
// fetchAuth(tvModel, cookie)
if (needAuth) {
fetchAuth(tvModel, cookie)
} else {
fetchVideo(tvModel, cookie)
}
} else {
// TODO
val err = "错误"
Log.e(TAG, "$title $err")
tvModel.setErrInfo(err)
}
}
} else {
fetchVideo(tvModel, cookie)
// fetchAuth(tvModel, cookie)
if (needAuth) {
fetchAuth(tvModel, cookie)
} else {
fetchVideo(tvModel, cookie)
}
}
} else {
val err = "其他错误"
Expand All @@ -303,19 +331,30 @@ object Request {
if (needToken && tvModel.tokenYSPRetryTimes < tvModel.tokenYSPRetryMaxTimes) {
tvModel.tokenYSPRetryTimes++
tvModel.needGetToken = true
fetchVideo(tvModel)
// fetchAuth(tvModel)
if (needAuth) {
fetchAuth(tvModel, cookie)
} else {
fetchVideo(tvModel, cookie)
}
} else {
if (!tvModel.getTV().mustToken) {
fetchVideo(tvModel, cookie)
// fetchAuth(tvModel, cookie)
if (needAuth) {
fetchAuth(tvModel, cookie)
} else {
fetchVideo(tvModel, cookie)
}
} else {
// TODO
val err = "错误"
Log.e(TAG, "$title $err")
tvModel.setErrInfo(err)
}
}
} else {
fetchVideo(tvModel, cookie)
// fetchAuth(tvModel, cookie)
if (needAuth) {
fetchAuth(tvModel, cookie)
} else {
fetchVideo(tvModel, cookie)
}
}
} else {
val err = "状态错误"
Expand Down

0 comments on commit 39e5cc7

Please sign in to comment.