mirror of
https://github.com/fankes/TSBattery.git
synced 2025-09-06 10:45:53 +08:00
增加模块自动检查更新功能
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
package="com.fankes.tsbattery">
|
package="com.fankes.tsbattery">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
|
||||||
<!-- 作用域 APP -->
|
<!-- 作用域 APP -->
|
||||||
<queries>
|
<queries>
|
||||||
|
@@ -30,9 +30,11 @@ import android.content.pm.PackageInfo
|
|||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
|
import android.net.ConnectivityManager
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import androidx.core.content.getSystemService
|
||||||
import com.fankes.tsbattery.application.TSApplication.Companion.appContext
|
import com.fankes.tsbattery.application.TSApplication.Companion.appContext
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
|
||||||
@@ -82,6 +84,13 @@ val Context.versionName get() = packageInfo.versionName ?: ""
|
|||||||
*/
|
*/
|
||||||
val Context.versionCode get() = packageInfo.versionCode
|
val Context.versionCode get() = packageInfo.versionCode
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 网络连接是否正常
|
||||||
|
* @return [Boolean] 网络是否连接
|
||||||
|
*/
|
||||||
|
val isNetWorkSuccess
|
||||||
|
get() = safeOfFalse { appContext.getSystemService<ConnectivityManager>()?.activeNetworkInfo != null }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* dp 转换为 pxInt
|
* dp 转换为 pxInt
|
||||||
* @param context 使用的实例
|
* @param context 使用的实例
|
||||||
@@ -115,22 +124,19 @@ fun Context.snake(msg: String, actionText: String = "", it: () -> Unit = {}) =
|
|||||||
setAction(actionText) { it() }
|
setAction(actionText) { it() }
|
||||||
}.show()
|
}.show()
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 跳转 APP 自身设置界面
|
* 跳转 APP 自身设置界面
|
||||||
* @param packageName 包名
|
* @param packageName 包名
|
||||||
*/
|
*/
|
||||||
fun Context.openSelfSetting(packageName: String) = runCatching {
|
fun Context.openSelfSetting(packageName: String = appContext.packageName) = runCatching {
|
||||||
if (packageName.isInstall)
|
if (packageName.isInstall)
|
||||||
startActivity(Intent().apply {
|
startActivity(Intent().apply {
|
||||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
||||||
data = Uri.fromParts("package", packageName, null)
|
data = Uri.fromParts("package", packageName, null)
|
||||||
})
|
})
|
||||||
else Toast.makeText(this, "你没有安装此应用", Toast.LENGTH_SHORT).show()
|
else toast(msg = "你没有安装此应用")
|
||||||
}.onFailure {
|
}.onFailure { toast(msg = "启动 $packageName 应用信息失败") }
|
||||||
Toast.makeText(this, "启动 $packageName 应用信息失败", Toast.LENGTH_SHORT).show()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动系统浏览器
|
* 启动系统浏览器
|
||||||
|
@@ -23,9 +23,7 @@ package com.fankes.tsbattery.utils.tool
|
|||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.fankes.tsbattery.utils.factory.openBrowser
|
import com.fankes.tsbattery.utils.factory.*
|
||||||
import com.fankes.tsbattery.utils.factory.runInSafe
|
|
||||||
import com.fankes.tsbattery.utils.factory.showDialog
|
|
||||||
import okhttp3.*
|
import okhttp3.*
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@@ -48,7 +46,7 @@ object GithubReleaseTool {
|
|||||||
* @param version 当前版本
|
* @param version 当前版本
|
||||||
* @param it 成功后回调 - ([String] 最新版本,[Function] 更新对话框方法体)
|
* @param it 成功后回调 - ([String] 最新版本,[Function] 更新对话框方法体)
|
||||||
*/
|
*/
|
||||||
fun checkingForUpdate(context: Context, version: String, it: (String, () -> Unit) -> Unit) = runInSafe {
|
fun checkingForUpdate(context: Context, version: String, it: (String, () -> Unit) -> Unit) = checkingInternetConnect(context) {
|
||||||
OkHttpClient().newBuilder().build().newCall(
|
OkHttpClient().newBuilder().build().newCall(
|
||||||
Request.Builder()
|
Request.Builder()
|
||||||
.url("https://api.github.com/repos/$repoAuthor/$repoName/releases/latest")
|
.url("https://api.github.com/repos/$repoAuthor/$repoName/releases/latest")
|
||||||
@@ -82,6 +80,37 @@ object GithubReleaseTool {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查网络连接情况
|
||||||
|
* @param context 实例
|
||||||
|
* @param it 已连接回调
|
||||||
|
*/
|
||||||
|
private fun checkingInternetConnect(context: Context, it: () -> Unit) = runInSafe {
|
||||||
|
if (isNetWorkSuccess)
|
||||||
|
OkHttpClient().newBuilder().build().newCall(
|
||||||
|
Request.Builder()
|
||||||
|
.url("https://www.baidu.com")
|
||||||
|
.get()
|
||||||
|
.build()
|
||||||
|
).enqueue(object : Callback {
|
||||||
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
|
(context as? Activity?)?.runOnUiThread {
|
||||||
|
context.showDialog {
|
||||||
|
title = "网络连接失败"
|
||||||
|
msg = "模块的联网权限可能已被禁用,请开启联网权限以定期检查更新。"
|
||||||
|
confirmButton(text = "去开启") { context.openSelfSetting() }
|
||||||
|
cancelButton()
|
||||||
|
noCancelable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResponse(call: Call, response: Response) = runInSafe {
|
||||||
|
(context as? Activity?)?.runOnUiThread { runInSafe { it() } }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Github Release bean
|
* Github Release bean
|
||||||
* @param name 版本名称
|
* @param name 版本名称
|
||||||
|
Reference in New Issue
Block a user