Skip to content

Commit

Permalink
performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Jan 28, 2024
1 parent bcf9bab commit cc2514a
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 59 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

## 更新日志

### v1.5.1(高版本专用)

* 性能优化

### v1.4.9(高版本专用)

* 同步v1.4.8
Expand Down
Binary file modified app/src/main/cpp/arm64-v8a/libnative.so
Binary file not shown.
Binary file modified app/src/main/cpp/armeabi-v7a/libnative.so
Binary file not shown.
5 changes: 2 additions & 3 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MainActivity : FragmentActivity() {
private val delay: Long = 4000
private val delayHideHelp: Long = 10000

private lateinit var sharedPref: SharedPreferences
lateinit var sharedPref: SharedPreferences
private var channelReversal = false
private var channelNum = true

Expand All @@ -60,7 +60,6 @@ class MainActivity : FragmentActivity() {
.add(R.id.main_browse_fragment, mainFragment)
.hide(mainFragment)
.commit()
mainFragment.view?.requestFocus()
}
gestureDetector = GestureDetector(this, GestureListener())

Expand Down Expand Up @@ -477,7 +476,7 @@ class MainActivity : FragmentActivity() {

private fun hashSignature(signature: Signature): String {
return try {
val md = MessageDigest.getInstance("SHA-256")
val md = MessageDigest.getInstance("MD5")
md.update(signature.toByteArray())
val digest = md.digest()
digest.let { it -> it.joinToString("") { "%02x".format(it) } }
Expand Down
12 changes: 5 additions & 7 deletions app/src/main/java/com/lizongying/mytv/MainFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lizongying.mytv

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.os.Handler
Expand Down Expand Up @@ -35,7 +34,7 @@ class MainFragment : BrowseSupportFragment() {

var tvListViewModel = TVListViewModel()

private var sharedPref: SharedPreferences? = null
private lateinit var sharedPref: SharedPreferences

private var lastVideoUrl = ""

Expand All @@ -53,7 +52,7 @@ class MainFragment : BrowseSupportFragment() {
super.onActivityCreated(savedInstanceState)

activity?.let { request.initYSP(it) }
sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
sharedPref = (activity as? MainActivity)?.sharedPref!!

loadRows()

Expand Down Expand Up @@ -84,9 +83,8 @@ class MainFragment : BrowseSupportFragment() {
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 ${tvViewModel.pid.value}")
Log.i(TAG, "request $title")
lifecycleScope.launch(Dispatchers.IO) {
tvViewModel.let { request.fetchData(it) }
}
Expand Down Expand Up @@ -152,7 +150,7 @@ class MainFragment : BrowseSupportFragment() {

adapter = rowsAdapter

itemPosition = sharedPref?.getInt(POSITION, 0)!!
itemPosition = sharedPref.getInt(POSITION, 0)
if (itemPosition >= tvListViewModel.size()) {
itemPosition = 0
}
Expand Down Expand Up @@ -313,7 +311,7 @@ class MainFragment : BrowseSupportFragment() {
override fun onStop() {
Log.i(TAG, "onStop")
super.onStop()
with(sharedPref!!.edit()) {
with(sharedPref.edit()) {
putInt(POSITION, itemPosition)
apply()
}
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/lizongying/mytv/PlayerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,22 @@ class PlayerFragment : Fragment() {
@OptIn(UnstableApi::class)
fun play(tvViewModel: TVViewModel) {
this.tvViewModel = tvViewModel
val videoUrlCurrent = tvViewModel.getVideoUrlCurrent()
playerView?.player?.run {
setMediaItem(MediaItem.fromUri(videoUrlCurrent))
setMediaItem(MediaItem.fromUri(tvViewModel.getVideoUrlCurrent()))
prepare()
}
}

override fun onStart() {
super.onStart()
if (playerView != null) {
if (playerView != null && playerView!!.player?.isPlaying == false) {
playerView!!.player?.play()
}
}

override fun onStop() {
super.onStop()
if (playerView != null) {
if (playerView != null && playerView!!.player?.isPlaying == true) {
playerView!!.player?.stop()
}
}
Expand Down
121 changes: 90 additions & 31 deletions app/src/main/java/com/lizongying/mytv/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ class Request {
private var yspBtraceService: YSPBtraceService = ApiClient().yspBtraceService
private var yspProtoService: YSPProtoService = ApiClient().yspProtoService
private var ysp: YSP? = null
private var token = ""

// TODO onDestroy
private val handler = Handler(Looper.getMainLooper())
private lateinit var myRunnable: MyRunnable
private lateinit var btraceRunnable: BtraceRunnable
private var tokenRunnable: TokenRunnable = TokenRunnable()

private var mapping = mapOf(
"CCTV4K" to "CCTV4K 超高清",
Expand Down Expand Up @@ -85,6 +87,10 @@ class Request {
"新疆卫视" to "新疆卫视",
)

init {
handler.post(tokenRunnable)
}

fun initYSP(context: Context) {
ysp = YSP(context)
}
Expand All @@ -93,8 +99,8 @@ class Request {

fun fetchVideo(tvModel: TVViewModel, cookie: String) {
call?.cancel()
if (::myRunnable.isInitialized) {
handler.removeCallbacks(myRunnable)
if (::btraceRunnable.isInitialized) {
handler.removeCallbacks(btraceRunnable)
}

val title = tvModel.title.value
Expand Down Expand Up @@ -129,13 +135,18 @@ class Request {
tvModel.addVideoUrl(url)
tvModel.allReady()
tvModel.retryTimes = 0
myRunnable = MyRunnable(tvModel)
handler.post(myRunnable)
btraceRunnable = BtraceRunnable(tvModel)
handler.post(btraceRunnable)
} else {
Log.e(TAG, "$title key error")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchVideo(tvModel, cookie)
if (tvModel.needToken) {
token = ""
fetchVideo(tvModel)
} else {
fetchVideo(tvModel, cookie)
}
}
}
} else {
Expand All @@ -146,15 +157,25 @@ class Request {
Log.e(TAG, "$title url error $request $liveInfo")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchVideo(tvModel, cookie)
if (tvModel.needToken) {
token = ""
fetchVideo(tvModel)
} else {
fetchVideo(tvModel, cookie)
}
}
}
}
} else {
Log.e(TAG, "$title status error")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchVideo(tvModel, cookie)
if (tvModel.needToken) {
token = ""
fetchVideo(tvModel)
} else {
fetchVideo(tvModel, cookie)
}
}
}
}
Expand All @@ -163,39 +184,50 @@ class Request {
Log.e(TAG, "$title request error")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchVideo(tvModel, cookie)
if (tvModel.needToken) {
token = ""
fetchVideo(tvModel)
} else {
fetchVideo(tvModel, cookie)
}
}
}
})
}

fun fetchVideo(tvModel: TVViewModel) {
yspTokenService.getInfo()
.enqueue(object : Callback<Info> {
override fun onResponse(call: Call<Info>, response: Response<Info>) {
if (response.isSuccessful) {
val token = response.body()?.data?.token
Log.i(TAG, "info success $token")
val cookie =
"vplatform=109; yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
fetchVideo(tvModel, cookie)
} else {
Log.e(TAG, "info status error")
if (token == "") {
yspTokenService.getInfo()
.enqueue(object : Callback<Info> {
override fun onResponse(call: Call<Info>, response: Response<Info>) {
if (response.isSuccessful) {
token = response.body()?.data?.token!!
Log.i(TAG, "info success $token")
val cookie =
"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++
fetchVideo(tvModel)
}
}
}

override fun onFailure(call: Call<Info>, t: Throwable) {
Log.e(TAG, "info request error $t")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchVideo(tvModel)
}
}
}

override fun onFailure(call: Call<Info>, t: Throwable) {
Log.e(TAG, "info request error $t")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchVideo(tvModel)
}
}
})
})
} else {
val cookie =
"vplatform=109; yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
fetchVideo(tvModel, cookie)
}
}

fun fetchData(tvModel: TVViewModel) {
Expand All @@ -207,7 +239,32 @@ class Request {
}
}

inner class MyRunnable(private val tvModel: TVViewModel) : Runnable {
inner class TokenRunnable : Runnable {
override fun run() {
fetchToken()
handler.postDelayed(this, 600000)
}
}

fun fetchToken() {
yspTokenService.getInfo()
.enqueue(object : Callback<Info> {
override fun onResponse(call: Call<Info>, response: Response<Info>) {
if (response.isSuccessful) {
token = response.body()?.data?.token!!
Log.i(TAG, "info success $token")
} else {
Log.e(TAG, "token status error")
}
}

override fun onFailure(call: Call<Info>, t: Throwable) {
Log.e(TAG, "token request error $t")
}
})
}

inner class BtraceRunnable(private val tvModel: TVViewModel) : Runnable {
override fun run() {
fetchBtrace(tvModel)
handler.postDelayed(this, 60000)
Expand Down Expand Up @@ -306,6 +363,8 @@ class Request {
tvViewModel.addProgram(program.dataListList)
Log.i(TAG, "$title program ${program.dataListList.size}")
}
} else {
Log.w(TAG, "$title program error")
}
}

Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/lizongying/mytv/TVList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,6 @@ CGTN 纪录频道,https://livedoc.cgtn.com/500d/prog_index.m3u8
}
if (!i.contains(",")) {
channel = i.trim()
if (channel == "移动专区") {
break
}
continue
}
val p = i.split(",")
Expand Down
15 changes: 5 additions & 10 deletions app/src/main/java/com/lizongying/mytv/api/YSP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ class YSP(var context: Context) {
private var signature = ""

private var encryptor: Encryptor? = null
private var sharedPref: SharedPreferences? = null
private lateinit var sharedPref: SharedPreferences

init {
if (context is MainActivity) {
encryptor = Encryptor()
encryptor!!.init(context)

sharedPref = (context as MainActivity).getPreferences(Context.MODE_PRIVATE)
sharedPref = (context as MainActivity).sharedPref
}

guid = getGuid()
Expand All @@ -77,15 +77,10 @@ class YSP(var context: Context) {

timeStr = getTimeStr()

// guid = "lq3oqitm_1e15dnzgjnb"
// randStr = "BfcCPQp8Hq"
// timeStr = "1702166501"

cKey =
encryptor!!.encrypt(cnlid, timeStr, appVer, guid, platform)
signature = getSignature()
return """{"cnlid":"$cnlid","livepid":"$livepid","stream":"$stream","guid":"$guid","cKey":"$cKey","adjust":$adjust,"sphttps":"$sphttps","platform":"$platform","cmd":"$cmd","encryptVer":"$encryptVer","dtype":"$dtype","devid":"$devid","otype":"$otype","appVer":"$appVer","app_version":"$appVersion","rand_str":"$randStr","channel":"$channel","defn":"$defn","signature":"$signature"}"""
// return """{"cnlid":"2000203803","livepid":"600001801","stream":"2","guid":"lq1y36mb_ccfwmja9zan","cKey":"--01A9F5E89BB86A0C61F4025BEDE15309B6913A79FD1AF1EE7F5EC9C7605F377D1D2281488385C32DEB9E7D0DD3559CB700BD7AF44DD5C9DE0AE14D94B8214027B5D664C108AEE23532348DCC61B86F7C8FBB6CF14D588E6093A25E97DF6D66F4882AB28F17016472DD43D45EF076B7F505176A5E8DEDF2662E5F9AB12B69CB20BCE1579BE724091F3AF6826AE34B713906F3FE139C3783F80EECBD08416DC525E1","adjust":1,"sphttps":"1","platform":"5910204","cmd":"2","encryptVer":"8.1","dtype":"1","devid":"devid","otype":"ojson","appVer":"V1.0.0","app_version":"V1.0.0","rand_str":"IOS2soOw44","channel":"ysp_tx","defn":"fhd","signature":"6ca945f651817de8c6e6910457ceafd6"}"""
}

private fun getTimeStr(): String {
Expand All @@ -99,10 +94,10 @@ class YSP(var context: Context) {
}

fun getGuid(): String {
var guid = sharedPref?.getString("guid", "")
var guid = sharedPref.getString("guid", "")
if (guid == null || guid.length < 18) {
guid = generateGuid()
with(sharedPref!!.edit()) {
with(sharedPref.edit()) {
putString("guid", guid)
apply()
}
Expand All @@ -112,7 +107,7 @@ class YSP(var context: Context) {

private fun newGuid(): String {
guid = generateGuid()
with(sharedPref!!.edit()) {
with(sharedPref.edit()) {
putString("guid", guid)
apply()
}
Expand Down
Loading

0 comments on commit cc2514a

Please sign in to comment.