diff --git a/Makefile b/Makefile index 7edb479..681568e 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv/ConfirmationFragment.kt b/app/src/main/java/com/lizongying/mytv/ConfirmationFragment.kt index 87d58be..ec249a7 100644 --- a/app/src/main/java/com/lizongying/mytv/ConfirmationFragment.kt +++ b/app/src/main/java/com/lizongying/mytv/ConfirmationFragment.kt @@ -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") } diff --git a/app/src/main/java/com/lizongying/mytv/UpdateManager.kt b/app/src/main/java/com/lizongying/mytv/UpdateManager.kt index caa85fb..7e354af 100644 --- a/app/src/main/java/com/lizongying/mytv/UpdateManager.kt +++ b/app/src/main/java/com/lizongying/mytv/UpdateManager.kt @@ -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") diff --git a/app/src/main/java/com/lizongying/mytv/api/ApiClient.kt b/app/src/main/java/com/lizongying/mytv/api/ApiClient.kt index cf0d918..f51178f 100644 --- a/app/src/main/java/com/lizongying/mytv/api/ApiClient.kt +++ b/app/src/main/java/com/lizongying/mytv/api/ApiClient.kt @@ -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/" + } } \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv/requests/ReleaseRequest.kt b/app/src/main/java/com/lizongying/mytv/requests/ReleaseRequest.kt index 293d464..34c5083 100644 --- a/app/src/main/java/com/lizongying/mytv/requests/ReleaseRequest.kt +++ b/app/src/main/java/com/lizongying/mytv/requests/ReleaseRequest.kt @@ -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 { - override fun onResponse(call: Call, response: Response) { + .enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { if (response.isSuccessful) { continuation.resume(response.body()) } else { @@ -31,7 +34,7 @@ class ReleaseRequest { } } - override fun onFailure(call: Call, t: Throwable) { + override fun onFailure(call: Call, t: Throwable) { continuation.resume(null) } }) diff --git a/app/src/main/java/com/lizongying/mytv/requests/ReleaseResponse.kt b/app/src/main/java/com/lizongying/mytv/requests/ReleaseResponse.kt new file mode 100644 index 0000000..d72422f --- /dev/null +++ b/app/src/main/java/com/lizongying/mytv/requests/ReleaseResponse.kt @@ -0,0 +1,7 @@ +package com.lizongying.mytv.requests + + +data class ReleaseResponse( + val version_code: Int?, + val version_name: String?, +) \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv/requests/ReleaseService.kt b/app/src/main/java/com/lizongying/mytv/requests/ReleaseService.kt new file mode 100644 index 0000000..e543030 --- /dev/null +++ b/app/src/main/java/com/lizongying/mytv/requests/ReleaseService.kt @@ -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 +} \ No newline at end of file diff --git a/version.json b/version.json new file mode 100644 index 0000000..52075b0 --- /dev/null +++ b/version.json @@ -0,0 +1 @@ +{"version_code": 17368064, "version_name": "v1.9.4"}