optimize version check

pull/731/head
Li ZongYing 2 years ago
parent b157f1eb28
commit 6fc73efd47
  1. 10
      Makefile
  2. 27
      app/src/main/java/com/lizongying/mytv/ConfirmationFragment.kt
  3. 30
      app/src/main/java/com/lizongying/mytv/UpdateManager.kt
  4. 7
      app/src/main/java/com/lizongying/mytv/api/ApiClient.kt
  5. 13
      app/src/main/java/com/lizongying/mytv/requests/ReleaseRequest.kt
  6. 7
      app/src/main/java/com/lizongying/mytv/requests/ReleaseResponse.kt
  7. 10
      app/src/main/java/com/lizongying/mytv/requests/ReleaseService.kt
  8. 1
      version.json

@ -1,6 +1,6 @@
.PHONY: all
all: info
all: info gen-version
branch := $(shell git rev-parse --abbrev-ref HEAD)
commit := $(shell git rev-parse --short HEAD)
@ -9,3 +9,11 @@ info:
@echo 'SHELL='$(SHELL)
@echo 'branch='$(branch)
@echo 'commit='$(commit)
gen-version:
git describe --tags --always
git describe --tags --always | sed 's/v/ /g' | sed 's/\./ /g' | sed 's/-/ /g' | awk '{print ($$1*16777216)+($$2*65536)+($$3*256)+$$4}'
#make gen v=v1.1.3
gen:
echo $(v) | sed 's/v/ /g' | sed 's/\./ /g' | sed 's/-/ /g' | awk '{print "{\"version_code\": " ($$1*16777216)+($$2*65536)+($$3*256)+$$4 ", \"version_name\": \"" "$(v)" "\"}"}' > version.json

@ -13,18 +13,25 @@ class ConfirmationFragment(
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
builder.setTitle("确定更新吗?")
.setMessage(message)
.setPositiveButton(
"确定"
builder.setTitle(message)
if (message != "版本获取失败") {
builder.setMessage("确定更新吗?")
.setPositiveButton(
"确定"
) { _, _ ->
listener.onConfirm()
}
.setNegativeButton(
"取消"
) { _, _ ->
listener.onCancel()
}
} else {
builder.setNegativeButton(
"好的"
) { _, _ ->
listener.onConfirm()
}
.setNegativeButton(
"取消"
) { _, _ ->
listener.onCancel()
}
}
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}

@ -17,8 +17,10 @@ import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.PermissionChecker
import androidx.core.content.PermissionChecker.checkSelfPermission
import com.lizongying.mytv.api.ApiClient
import com.lizongying.mytv.api.ReleaseV2
import com.lizongying.mytv.requests.ReleaseRequest
import com.lizongying.mytv.requests.ReleaseResponse
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -32,8 +34,8 @@ class UpdateManager(
) :
ConfirmationFragment.ConfirmationListener {
private var myRequest = ReleaseRequest()
private var release: ReleaseV2? = null
private var releaseRequest = ReleaseRequest()
private var release: ReleaseResponse? = null
private var downloadReceiver: DownloadReceiver? = null
@ -44,11 +46,11 @@ class UpdateManager(
CoroutineScope(Dispatchers.Main).launch {
var text = "版本获取失败"
try {
release = myRequest.getRelease()
Log.i(TAG, "versionCode $versionCode ${release?.c}")
if (release?.c != null) {
text = if (release?.c!! > versionCode) {
"最新版本:${release?.n}\n${release?.d ?: ""}"
release = releaseRequest.getRelease()
Log.i(TAG, "versionCode $versionCode ${release?.version_code}")
if (release?.version_code != null) {
text = if (release?.version_code!! >= versionCode) {
"最新版本:${release?.version_name}}"
} else {
"已是最新版本,不需要更新"
}
@ -88,20 +90,24 @@ class UpdateManager(
}
private fun startDownload(release: ReleaseV2) {
val apkFileName = "my-tv-${release.n}.apk"
private fun startDownload(release: ReleaseResponse) {
val apkFileName = "my-tv-${release.version_name}.apk"
Log.i(TAG, "apkFileName $apkFileName")
val downloadManager =
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val request = Request(Uri.parse(release.u))
Log.i(TAG, "url ${Uri.parse(release.u)}")
val request =
Request(Uri.parse("${ApiClient.HOST}/release/download/${release.version_name}/my-tv-${release.version_name}.apk"))
Log.i(
TAG,
"url ${Uri.parse("${ApiClient.HOST}/release/download/${release.version_name}/my-tv-0-${release.version_name}.apk")}"
)
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.mkdirs()
request.setDestinationInExternalFilesDir(
context,
Environment.DIRECTORY_DOWNLOADS,
apkFileName
)
request.setTitle("${settingFragment.resources.getString(R.string.app_name)} ${release.n}")
request.setTitle("${settingFragment.resources.getString(R.string.app_name)} ${release.version_name}")
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setAllowedOverRoaming(false)
request.setMimeType("application/vnd.android.package-archive")

@ -4,6 +4,7 @@ package com.lizongying.mytv.api
import android.os.Build
import android.util.Log
import com.lizongying.mytv.jce.JceConverterFactory
import com.lizongying.mytv.requests.ReleaseService
import okhttp3.ConnectionSpec
import okhttp3.OkHttpClient
import okhttp3.TlsVersion
@ -47,7 +48,7 @@ class ApiClient {
val releaseService: ReleaseService by lazy {
Retrofit.Builder()
.baseUrl(myUrl)
.baseUrl(HOST)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build().create(ReleaseService::class.java)
@ -170,4 +171,8 @@ class ApiClient {
throw RuntimeException(e)
}
}
companion object {
const val HOST = "https://gitee.com/lizongying/my-tv/"
}
}

@ -13,17 +13,20 @@ import kotlin.coroutines.suspendCoroutine
class ReleaseRequest {
private var releaseService = ApiClient().releaseService
suspend fun getRelease(): ReleaseV2? {
suspend fun getRelease(): ReleaseResponse? {
return withContext(Dispatchers.IO) {
fetchRelease()
}
}
private suspend fun fetchRelease(): ReleaseV2? {
private suspend fun fetchRelease(): ReleaseResponse? {
return suspendCoroutine { continuation ->
releaseService.getRelease()
.enqueue(object : Callback<ReleaseV2> {
override fun onResponse(call: Call<ReleaseV2>, response: Response<ReleaseV2>) {
.enqueue(object : Callback<ReleaseResponse> {
override fun onResponse(
call: Call<ReleaseResponse>,
response: Response<ReleaseResponse>
) {
if (response.isSuccessful) {
continuation.resume(response.body())
} else {
@ -31,7 +34,7 @@ class ReleaseRequest {
}
}
override fun onFailure(call: Call<ReleaseV2>, t: Throwable) {
override fun onFailure(call: Call<ReleaseResponse>, t: Throwable) {
continuation.resume(null)
}
})

@ -0,0 +1,7 @@
package com.lizongying.mytv.requests
data class ReleaseResponse(
val version_code: Int?,
val version_name: String?,
)

@ -0,0 +1,10 @@
package com.lizongying.mytv.requests
import retrofit2.Call
import retrofit2.http.GET
interface ReleaseService {
@GET("/raw/main/version.json")
fun getRelease(
): Call<ReleaseResponse>
}

@ -0,0 +1 @@
{"version_code": 17368064, "version_name": "v1.9.4"}
Loading…
Cancel
Save