optimize version check

pull/731/head
Li ZongYing 2 years ago
parent b157f1eb28
commit 6fc73efd47
  1. 10
      Makefile
  2. 11
      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 .PHONY: all
all: info all: info gen-version
branch := $(shell git rev-parse --abbrev-ref HEAD) branch := $(shell git rev-parse --abbrev-ref HEAD)
commit := $(shell git rev-parse --short HEAD) commit := $(shell git rev-parse --short HEAD)
@ -9,3 +9,11 @@ info:
@echo 'SHELL='$(SHELL) @echo 'SHELL='$(SHELL)
@echo 'branch='$(branch) @echo 'branch='$(branch)
@echo 'commit='$(commit) @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,8 +13,9 @@ class ConfirmationFragment(
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
val builder = AlertDialog.Builder(it) val builder = AlertDialog.Builder(it)
builder.setTitle("确定更新吗?") builder.setTitle(message)
.setMessage(message) if (message != "版本获取失败") {
builder.setMessage("确定更新吗?")
.setPositiveButton( .setPositiveButton(
"确定" "确定"
) { _, _ -> ) { _, _ ->
@ -25,6 +26,12 @@ class ConfirmationFragment(
) { _, _ -> ) { _, _ ->
listener.onCancel() listener.onCancel()
} }
} else {
builder.setNegativeButton(
"好的"
) { _, _ ->
}
}
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }

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

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

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