Skip to content

Commit

Permalink
fix time error
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Feb 7, 2024
1 parent aa4d526 commit 764f2a1
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 26 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

## 更新日志

### v1.5.6(通用版)

* 解决部分设备系统时间不对导致播放失败的问题
*

### v1.5.2(通用版)

* 修复APP恢复后频道号、频道列表不自动消失的问题
Expand Down
32 changes: 26 additions & 6 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ import android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
import android.view.WindowManager
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
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


class MainActivity : FragmentActivity() {

var playerFragment = PlayerFragment()
private val mainFragment = MainFragment()
private val infoFragment = InfoFragment()
private val channelFragment = ChannelFragment()
private var ready = 0
private var playerFragment = PlayerFragment()
private var mainFragment = MainFragment()
private var infoFragment = InfoFragment()
private var channelFragment = ChannelFragment()
private lateinit var settingFragment: SettingFragment

private var doubleBackToExitPressedOnce = false
Expand All @@ -44,6 +50,16 @@ class MainActivity : FragmentActivity() {

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?) {
Log.i(TAG, "onCreate")
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -156,7 +172,11 @@ class MainActivity : FragmentActivity() {
}

fun fragmentReady() {
mainFragment.fragmentReady()
ready++
Log.i(TAG, "ready $ready")
if (ready == 4) {
mainFragment.fragmentReady()
}
}

override fun onTouchEvent(event: MotionEvent?): Boolean {
Expand Down Expand Up @@ -496,7 +516,7 @@ class MainActivity : FragmentActivity() {
override fun onResume() {
Log.i(TAG, "onResume")
super.onResume()
if (!mainFragment.isHidden){
if (!mainFragment.isHidden) {
handler.postDelayed(hideMain, delayHideMain)
}
}
Expand Down
16 changes: 7 additions & 9 deletions app/src/main/java/com/lizongying/mytv/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
private val handler = Handler(Looper.getMainLooper())
private lateinit var mUpdateProgramRunnable: UpdateProgramRunnable

private var ready = 0

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down Expand Up @@ -149,7 +147,7 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
}
}

fragmentReady()
(activity as MainActivity).fragmentReady()
}
}

Expand Down Expand Up @@ -180,6 +178,8 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {

fun setPosition() {
val tvViewModel = tvListViewModel.getTVViewModel(itemPosition)
Log.i(TAG, "tvViewModel $tvViewModel")
Log.i(TAG, "rowList ${rowList.size}")
rowList[tvViewModel!!.getRowPosition()].post {
((rowList[tvViewModel.getRowPosition()] as RecyclerView).layoutManager as LinearLayoutManager).findViewByPosition(
tvViewModel.getItemPosition()
Expand Down Expand Up @@ -212,12 +212,8 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
}

fun fragmentReady() {
ready++
Log.i(TAG, "ready $ready")
if (ready == 4) {
// request.fetchPage()
tvListViewModel.getTVViewModel(itemPosition)?.changed()
}
tvListViewModel.getTVViewModel(itemPosition)?.changed()
}

fun play(itemPosition: Int) {
Expand Down Expand Up @@ -299,7 +295,9 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
override fun onDestroy() {
Log.i(TAG, "onDestroy")
super.onDestroy()
handler.removeCallbacks(mUpdateProgramRunnable)
if (::mUpdateProgramRunnable.isInitialized) {
handler.removeCallbacks(mUpdateProgramRunnable)
}
}

override fun onDestroyView() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv/PlayerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class PlayerFragment : Fragment() {
): View {
_binding = PlayerBinding.inflate(inflater, container, false)
playerView = _binding!!.playerView
(activity as MainActivity).playerFragment = this
playerView?.viewTreeObserver?.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
Expand Down Expand Up @@ -86,6 +85,7 @@ class PlayerFragment : Fragment() {
if (playerView != null && playerView!!.player?.isPlaying == false) {
Log.i(TAG, "replay")
playerView!!.player?.prepare()
playerView!!.player?.play()
} else {
Log.i(TAG, "playing")
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/lizongying/mytv/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ class Request {

for ((_, v) in TVList.list) {
for (v2 in v) {
Log.i(TAG, "$v2")
if (v2.title == item.channelName || v2.alias == item.channelName) {
v2.pid = item.pid
v2.sid = item.streamId
Expand Down
101 changes: 101 additions & 0 deletions app/src/main/java/com/lizongying/mytv/UpdateManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.lizongying.mytv

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 androidx.annotation.RequiresApi
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 myRequest = MyRequest()

@RequiresApi(Build.VERSION_CODES.O)
fun checkAndUpdate() {
CoroutineScope(Dispatchers.Main).launch {
try {
val release = myRequest.getRelease()
// 在主线程中更新 UI
updateUI(release)
if (release?.data?.versionCode!! > 0) {
startDownload(release)
}
} catch (e: Exception) {
// 处理异常情况
Log.e(TAG, "Error occurred: ${e.message}", e)
}
}
}

private fun updateUI(release: Release?) {
}

@RequiresApi(Build.VERSION_CODES.O)
private fun startDownload(release: Release) {
val apkFileName = "my-tv-${release.data.versionName}.apk"

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)

// 注册广播接收器,监听下载完成事件
context!!.registerReceiver(
DownloadReceiver(context!!, apkFileName, downloadReference),
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), Context.RECEIVER_NOT_EXPORTED
)
}


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)

// 检查是否是我们发起的下载
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"
}
}
53 changes: 49 additions & 4 deletions app/src/main/java/com/lizongying/mytv/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,63 @@ package com.lizongying.mytv
import android.content.Context
import android.content.res.Resources
import android.util.TypedValue
import com.google.gson.Gson
import com.lizongying.mytv.api.TimeResponse
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

object Utils {
private var between: Long = 0

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 {
return Date().time / 1000
return (System.currentTimeMillis() - between) / 1000
}

suspend fun init() {
var currentTimeMillis: Long = 0
try {
currentTimeMillis = getTimestampFromServer()
} catch (e: Exception) {
println("Failed to retrieve timestamp from server: ${e.message}")
}
between = System.currentTimeMillis() - currentTimeMillis
}

/**
* 从服务器获取时间戳
* @return Long 时间戳
*/
private suspend fun getTimestampFromServer(): Long {
return 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("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp")
.build()
try {
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
val string = response.body()?.string()
Gson().fromJson(string, TimeResponse::class.java).data.t.toLong()
}
} catch (e: IOException) {
// Handle network errors
throw IOException("Error during network request", e)
}
}
}

fun dpToPx(dp: Float): Int {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/lizongying/mytv/api/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class ApiClient {
.build().create(YSPTokenService::class.java)
}

val ReleaseService by lazy {
Retrofit.Builder()
.baseUrl(myUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build().create(ReleaseService::class.java)
}

val yspProtoService: YSPProtoService by lazy {
Retrofit.Builder()
.baseUrl(protoUrl)
Expand Down
31 changes: 26 additions & 5 deletions app/src/main/java/com/lizongying/mytv/api/Info.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,30 @@ package com.lizongying.mytv.api
data class Info(
val code: Int?,
val msg: String?,
val data: InfoData,
)
val data: Data,
) {
data class Data(
val token: String,
)
}

data class InfoData(
val token: String,
)
data class Release(
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(
val data: Time
) {
data class Time(
val t: String
)
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/lizongying/mytv/api/ReleaseService.kt
Original file line number Diff line number Diff line change
@@ -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>
}
Loading

0 comments on commit 764f2a1

Please sign in to comment.