diff --git a/app/src/main/kotlin/cn/super12138/todo/utils/VersionUtils.kt b/app/src/main/kotlin/cn/super12138/todo/utils/VersionUtils.kt new file mode 100644 index 0000000..c3d9c91 --- /dev/null +++ b/app/src/main/kotlin/cn/super12138/todo/utils/VersionUtils.kt @@ -0,0 +1,17 @@ +package cn.super12138.todo.utils + +import android.content.Context +import android.os.Build + +object VersionUtils { + fun getAppVersion(context: Context): String { + val pkgInfo = context.packageManager.getPackageInfo(context.packageName, 0) + val verName = pkgInfo.versionName + val verCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + pkgInfo.longVersionCode.toInt() + } else { + pkgInfo.versionCode + } + return "$verName($verCode)" + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/cn/super12138/todo/views/about/AboutActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/about/AboutActivity.kt index 865276d..715258f 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/about/AboutActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/about/AboutActivity.kt @@ -6,6 +6,7 @@ import android.os.Build import android.os.Bundle import cn.super12138.todo.constant.Constants import cn.super12138.todo.databinding.ActivityAboutBinding +import cn.super12138.todo.utils.VersionUtils import cn.super12138.todo.utils.showToast import cn.super12138.todo.views.BaseActivity @@ -19,14 +20,7 @@ class AboutActivity : BaseActivity() { binding = ActivityAboutBinding.inflate(layoutInflater) setContentView(binding.root) - val pkgInfo = packageManager.getPackageInfo(packageName, 0) - val verName = pkgInfo.versionName - val verCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - pkgInfo.longVersionCode.toInt() - } else { - pkgInfo.versionCode - } - binding.appVersion.text = "$verName($verCode)" + binding.appVersion.text = VersionUtils.getAppVersion(this) binding.toolBar.setNavigationOnClickListener { finish() diff --git a/app/src/main/kotlin/cn/super12138/todo/views/crash/CrashActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/crash/CrashActivity.kt index 1b00ddb..cf2ee5c 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/crash/CrashActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/crash/CrashActivity.kt @@ -8,6 +8,7 @@ import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.updateLayoutParams import cn.super12138.todo.databinding.ActivityCrashBinding +import cn.super12138.todo.utils.VersionUtils import cn.super12138.todo.views.BaseActivity import java.text.SimpleDateFormat import java.util.Calendar @@ -42,17 +43,8 @@ class CrashActivity : BaseActivity() { val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) val formattedDateTime = formatter.format(currentDateTime) - val pkgInfo = packageManager.getPackageInfo(packageName, 0) - val verName = pkgInfo.versionName - val verCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - pkgInfo.longVersionCode.toInt() - } else { - pkgInfo.versionCode - } - val mergeVer = "$verName($verCode)" - val deviceInfo = StringBuilder().apply { - append("ToDo version: ").append(mergeVer).append('\n') + append("ToDo version: ").append(VersionUtils.getAppVersion(this@CrashActivity)).append('\n') append("Brand: ").append("").append(deviceBrand).append('\n') append("Model: ").append(deviceModel).append('\n') append("Device SDK: ").append(sdkLevel).append('\n').append('\n') diff --git a/app/src/main/res/layout/activity_crash.xml b/app/src/main/res/layout/activity_crash.xml index 8729cdf..46273c5 100644 --- a/app/src/main/res/layout/activity_crash.xml +++ b/app/src/main/res/layout/activity_crash.xml @@ -6,7 +6,7 @@ android:layout_height="match_parent" tools:context=".views.crash.CrashActivity"> - @@ -46,7 +46,7 @@ android:textIsSelectable="true" android:textSize="13sp" /> - +