Skip to content

Commit

Permalink
optimize time
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed May 9, 2024
1 parent 0286d11 commit c16a4bc
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 64 deletions.
9 changes: 7 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 更新日志

### v1.9.8(通用)

* 优化在线升级
* 优化可能状态错误的问题

### v1.9.6(通用)

* 优化在线升级
Expand Down Expand Up @@ -41,6 +46,8 @@

* 修复无法播放问题

### 以下已不可用

### v1.8.0(通用)

* 修复返回键无法退出问题
Expand Down Expand Up @@ -176,8 +183,6 @@

* 修复播放失败的问题

### 以下已不可用

### v1.5.3(安卓5及以上专用)

* 修复部分情况下APP切换后无法继续播放的问题
Expand Down
82 changes: 64 additions & 18 deletions app/src/main/java/com/lizongying/mytv/SettingFragment.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.lizongying.mytv

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.marginEnd
import androidx.core.view.marginTop
import androidx.fragment.app.DialogFragment
Expand Down Expand Up @@ -87,13 +92,10 @@ class SettingFragment : DialogFragment() {
}
}

updateManager = UpdateManager(context, context.appVersionCode)
binding.checkVersion.setOnClickListener(
OnClickListenerCheckVersion(
activity as MainActivity,
updateManager
)
)
binding.checkVersion.setOnClickListener {
(activity as MainActivity).settingDelayHide()
requestInstallPermissions()
}

val application = requireActivity().applicationContext as MyTvApplication

Expand Down Expand Up @@ -162,20 +164,63 @@ class SettingFragment : DialogFragment() {
return binding.root
}

fun setVersionName(versionName: String) {
if (_binding != null) {
binding.versionName.text = versionName
private fun requestInstallPermissions() {
val context = requireContext()
val permissionsList: MutableList<String> = ArrayList()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !context.packageManager.canRequestPackageInstalls()) {
permissionsList.add(Manifest.permission.REQUEST_INSTALL_PACKAGES)
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
ContextCompat.checkSelfPermission(
context,
Manifest.permission.READ_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) {
permissionsList.add(Manifest.permission.READ_EXTERNAL_STORAGE)
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
ContextCompat.checkSelfPermission(
context,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) {
permissionsList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}

if (permissionsList.isNotEmpty()) {
ActivityCompat.requestPermissions(
requireActivity(),
permissionsList.toTypedArray<String>(),
PERMISSIONS_REQUEST_CODE
)
} else {
UpdateManager(context, context.appVersionCode).checkAndUpdate()
}
}

internal class OnClickListenerCheckVersion(
private val mainActivity: MainActivity,
private val updateManager: UpdateManager
) :
View.OnClickListener {
override fun onClick(view: View?) {
mainActivity.settingDelayHide()
updateManager.checkAndUpdate()
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSIONS_REQUEST_CODE) {
var allPermissionsGranted = true
for (result in grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allPermissionsGranted = false
break
}
}
if (allPermissionsGranted) {
val context = requireContext()
UpdateManager(context, context.appVersionCode).checkAndUpdate()
} else {
Toast.makeText(context, "权限授权失败", Toast.LENGTH_LONG).show()
}
}
}

Expand All @@ -186,6 +231,7 @@ class SettingFragment : DialogFragment() {

companion object {
const val TAG = "SettingFragment"
const val PERMISSIONS_REQUEST_CODE = 1
}
}

36 changes: 0 additions & 36 deletions app/src/main/java/com/lizongying/mytv/UpdateManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lizongying.mytv

import android.app.Activity
import android.app.DownloadManager
import android.app.DownloadManager.Request
import android.content.BroadcastReceiver
Expand All @@ -14,9 +13,6 @@ import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.PermissionChecker
import androidx.core.content.PermissionChecker.checkSelfPermission
import androidx.fragment.app.FragmentActivity
import com.lizongying.mytv.api.ApiClient
import com.lizongying.mytv.requests.ReleaseRequest
Expand All @@ -39,9 +35,6 @@ class UpdateManager(
private var downloadReceiver: DownloadReceiver? = null

fun checkAndUpdate() {
if (!haveStoragePermission()) {
return
}
CoroutineScope(Dispatchers.Main).launch {
var text = "版本获取失败"
var update = false
Expand All @@ -68,36 +61,7 @@ class UpdateManager(
dialog.show((context as FragmentActivity).supportFragmentManager, TAG)
}

private fun haveStoragePermission(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
=== PermissionChecker.PERMISSION_GRANTED
) {
Log.e("Permission error", "You have permission")
return true
} else {
Log.e("Permission error", "You have asked for permission")
ActivityCompat.requestPermissions(
context as Activity, arrayOf(
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
), 1
)
return false
}
} else { //you don't need to worry about these stuff below api level 23
Log.e("Permission error", "You already have the permission")
return true
}
}


private fun startDownload(release: ReleaseResponse) {
val packageInstaller = context.packageManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!packageInstaller.canRequestPackageInstalls()) {
}
}

val apkName = "my-tv"
val apkFileName = "$apkName-${release.version_name}.apk"
Log.i(TAG, "apkFileName $apkFileName")
Expand Down
11 changes: 4 additions & 7 deletions app/src/main/java/com/lizongying/mytv/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.lizongying.mytv

import android.content.res.Resources
import android.os.Build
import android.util.Log
import android.util.TypedValue
import com.google.gson.Gson
import com.lizongying.mytv.api.TimeResponse
Expand Down Expand Up @@ -37,13 +36,14 @@ object Utils {
}

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

withContext(Dispatchers.Main) {
listener?.onRequestFinished(null)
Expand Down Expand Up @@ -71,8 +71,6 @@ object Utils {
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)
.addInterceptor(RetryInterceptor(3))
.build()
val request = okhttp3.Request.Builder()
Expand All @@ -85,7 +83,6 @@ object Utils {
Gson().fromJson(string, TimeResponse::class.java).data.t.toLong()
}
} catch (e: IOException) {
// Handle network errors
throw IOException("Error during network request", e)
}
}
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version_code": 17368576, "version_name": "v1.9.6"}
{"version_code": 17369088, "version_name": "v1.9.8"}

0 comments on commit c16a4bc

Please sign in to comment.