From 3c6d3bbd8053a253663b355b4a41467caa9d3220 Mon Sep 17 00:00:00 2001
From: FooIbar <118464521+FooIbar@users.noreply.github.com>
Date: Fri, 24 Feb 2023 05:57:53 +0800
Subject: [PATCH] Add commit sha and build time, drop PackageUtils
---
app/build.gradle.kts | 22 +++++-
app/src/main/java/com/hippo/ehviewer/Crash.kt | 62 ++++++---------
.../java/com/hippo/ehviewer/EhApplication.kt | 2 +-
.../ehviewer/preference/VersionPreference.kt | 3 +-
.../java/com/hippo/util/PackageUtils.java | 78 -------------------
app/src/main/res/values-zh-rCN/strings.xml | 1 +
app/src/main/res/values/strings.xml | 1 +
7 files changed, 49 insertions(+), 120 deletions(-)
delete mode 100644 app/src/main/java/com/hippo/util/PackageUtils.java
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 7d05a445a..951e342c6 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -1,3 +1,8 @@
+import java.io.ByteArrayOutputStream
+import java.time.Instant
+import java.time.ZoneOffset
+import java.time.format.DateTimeFormatter
+
plugins {
id("com.android.application")
kotlin("android")
@@ -30,6 +35,20 @@ android {
enableV4Signing = true
}
+ val commitSha by lazy {
+ val stdout = ByteArrayOutputStream()
+ exec {
+ commandLine = "git rev-parse --short=7 HEAD".split(' ')
+ standardOutput = stdout
+ }
+ stdout.toString().trim()
+ }
+
+ val buildTime by lazy {
+ val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm").withZone(ZoneOffset.UTC)
+ formatter.format(Instant.now())
+ }
+
defaultConfig {
applicationId = "moe.tarsin.ehviewer"
minSdk = 28
@@ -52,6 +71,8 @@ android {
"nb-rNO"
)
)
+ buildConfigField("String", "COMMIT_SHA", "\"$commitSha\"")
+ buildConfigField("String", "BUILD_TIME", "\"$buildTime\"")
}
externalNativeBuild {
@@ -74,7 +95,6 @@ android {
"-opt-in=coil.annotation.ExperimentalCoilApi",
"-opt-in=androidx.compose.foundation.layout.ExperimentalLayoutApi",
"-opt-in=androidx.compose.material3.ExperimentalMaterial3Api",
- "-opt-in=androidx.compose.material.ExperimentalMaterialApi",
"-opt-in=androidx.compose.ui.ExperimentalComposeUiApi",
"-opt-in=androidx.compose.foundation.ExperimentalFoundationApi",
"-opt-in=androidx.compose.animation.ExperimentalAnimationApi",
diff --git a/app/src/main/java/com/hippo/ehviewer/Crash.kt b/app/src/main/java/com/hippo/ehviewer/Crash.kt
index 48618d239..fbcec5f99 100644
--- a/app/src/main/java/com/hippo/ehviewer/Crash.kt
+++ b/app/src/main/java/com/hippo/ehviewer/Crash.kt
@@ -15,11 +15,8 @@
*/
package com.hippo.ehviewer
-import android.content.Context
-import android.content.pm.PackageManager
import android.os.Build
import android.os.Debug
-import com.hippo.util.PackageUtils
import com.hippo.util.ReadableTime
import com.hippo.yorozuya.FileUtils
import com.hippo.yorozuya.IOUtils
@@ -31,45 +28,32 @@ import java.io.PrintWriter
import java.util.Arrays
object Crash {
- private fun avoidNull(str: String?): String {
- return str ?: "null"
- }
@Throws(IOException::class)
- private fun collectInfo(context: Context, fw: FileWriter) {
- try {
- val pm = context.packageManager
- val pi = pm.getPackageInfo(context.packageName, PackageManager.GET_ACTIVITIES)
- if (pi != null) {
- val versionName = if (pi.versionName == null) "null" else pi.versionName
- val versionCode = pi.versionCode.toString()
- fw.write("======== PackageInfo ========\r\n")
- fw.write("PackageName=")
- fw.write(pi.packageName)
- fw.write("\r\n")
- fw.write("VersionName=")
- fw.write(versionName)
- fw.write("\r\n")
- fw.write("VersionCode=")
- fw.write(versionCode)
- fw.write("\r\n")
- val signature = PackageUtils.getSignature(context, pi.packageName)
- fw.write("Signature=")
- fw.write(signature ?: "null")
- fw.write("\r\n")
- fw.write("\r\n")
- }
- } catch (e: PackageManager.NameNotFoundException) {
- fw.write("======== PackageInfo ========\r\n")
- fw.write("Can't get package information\r\n")
- fw.write("\r\n")
- }
+ private fun collectInfo(fw: FileWriter) {
+ fw.write("======== PackageInfo ========\r\n")
+ fw.write("PackageName=")
+ fw.write(BuildConfig.APPLICATION_ID)
+ fw.write("\r\n")
+ fw.write("VersionName=")
+ fw.write(BuildConfig.VERSION_NAME)
+ fw.write("\r\n")
+ fw.write("VersionCode=")
+ fw.write(BuildConfig.VERSION_CODE)
+ fw.write("\r\n")
+ fw.write("CommitSha=")
+ fw.write(BuildConfig.COMMIT_SHA)
+ fw.write("\r\n")
+ fw.write("BuildTime=")
+ fw.write(BuildConfig.BUILD_TIME)
+ fw.write("\r\n")
+ fw.write("\r\n")
// Runtime
- val topActivityClazzName = "null"
+ val topActivityClazzName = EhApplication.application.topActivity?.javaClass?.name
fw.write("======== Runtime ========\r\n")
fw.write("TopActivity=")
- fw.write(avoidNull(topActivityClazzName))
+ fw.write(topActivityClazzName ?: "null")
fw.write("\r\n")
fw.write("\r\n")
fw.write("\r\n")
@@ -134,7 +118,7 @@ object Crash {
fw.write(Build.VERSION.RELEASE)
fw.write("\r\n")
fw.write("SDK=")
- fw.write(Integer.toString(Build.VERSION.SDK_INT))
+ fw.write(Build.VERSION.SDK_INT.toString())
fw.write("\r\n")
fw.write("MEMORY=")
fw.write(
@@ -163,7 +147,7 @@ object Crash {
}
}
- fun saveCrashLog(context: Context, t: Throwable) {
+ fun saveCrashLog(t: Throwable) {
val dir = AppConfig.getExternalCrashDir() ?: return
val nowString = ReadableTime.getFilenamableTime(System.currentTimeMillis())
val fileName = "crash-$nowString.log"
@@ -175,7 +159,7 @@ object Crash {
fw.write(nowString)
fw.write("\r\n")
fw.write("\r\n")
- collectInfo(context, fw)
+ collectInfo(fw)
fw.write("======== CrashInfo ========\r\n")
getThrowableInfo(t, fw)
fw.write("\r\n")
diff --git a/app/src/main/java/com/hippo/ehviewer/EhApplication.kt b/app/src/main/java/com/hippo/ehviewer/EhApplication.kt
index c3b6b4c1d..88f85978e 100644
--- a/app/src/main/java/com/hippo/ehviewer/EhApplication.kt
+++ b/app/src/main/java/com/hippo/ehviewer/EhApplication.kt
@@ -76,7 +76,7 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
Thread.setDefaultUncaughtExceptionHandler { t, e ->
try {
if (Settings.getSaveCrashLog()) {
- Crash.saveCrashLog(application, e)
+ Crash.saveCrashLog(e)
}
} catch (ignored: Throwable) {
}
diff --git a/app/src/main/java/com/hippo/ehviewer/preference/VersionPreference.kt b/app/src/main/java/com/hippo/ehviewer/preference/VersionPreference.kt
index 1a50b4a7c..7cb45a656 100644
--- a/app/src/main/java/com/hippo/ehviewer/preference/VersionPreference.kt
+++ b/app/src/main/java/com/hippo/ehviewer/preference/VersionPreference.kt
@@ -26,6 +26,7 @@ class VersionPreference @JvmOverloads constructor(
) : Preference(context, attrs) {
init {
setTitle(R.string.settings_about_version)
- summary = BuildConfig.VERSION_NAME
+ summary = "${BuildConfig.VERSION_NAME} (${BuildConfig.COMMIT_SHA})\n" +
+ context.getString(R.string.settings_about_build_time, BuildConfig.BUILD_TIME)
}
}
\ No newline at end of file
diff --git a/app/src/main/java/com/hippo/util/PackageUtils.java b/app/src/main/java/com/hippo/util/PackageUtils.java
deleted file mode 100644
index a551d14b5..000000000
--- a/app/src/main/java/com/hippo/util/PackageUtils.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Copyright 2015 Hippo Seven
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hippo.util;
-
-import android.annotation.SuppressLint;
-import android.content.Context;
-import android.content.pm.PackageInfo;
-import android.content.pm.PackageManager;
-import android.content.pm.Signature;
-import android.util.Log;
-
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-
-public final class PackageUtils {
-
- private static final String TAG = PackageUtils.class.getSimpleName();
-
- public static String getSignature(Context context, String packageName) {
- try {
- @SuppressLint("PackageManagerGetSignatures")
- PackageInfo pi = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
- Signature[] ss = pi.signatures;
- if (ss != null && ss.length >= 1) {
- return computeSHA1(ss[0].toByteArray());
- } else {
- Log.e(TAG, "Can't find signature in package " + packageName);
- }
- } catch (Throwable e) {
- ExceptionUtils.throwIfFatal(e);
- Log.e(TAG, "Can't find package " + packageName, e);
- }
- return null;
- }
-
- /**
- * @return looks like A1:43:6B:34... or null
- */
- public static String computeSHA1(final byte[] certRaw) {
- StringBuilder sb = new StringBuilder(59);
- MessageDigest md;
- try {
- md = MessageDigest.getInstance("SHA1");
- byte[] sha1 = md.digest(certRaw);
- int length = sha1.length;
- for (int i = 0; i < length; i++) {
- if (i != 0) {
- sb.append(':');
- }
- byte b = sha1[i];
- String appendStr = Integer.toString(b & 0xff, 16);
- if (appendStr.length() == 1) {
- sb.append(0);
- }
- sb.append(appendStr);
- }
-
- return sb.toString().toUpperCase();
- } catch (NoSuchAlgorithmException ex) {
- Log.e(TAG, "Can't final Algorithm SHA1", ex);
- return null;
- }
- }
-}
diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml
index 6f7485b23..eafafc877 100644
--- a/app/src/main/res/values-zh-rCN/strings.xml
+++ b/app/src/main/res/values-zh-rCN/strings.xml
@@ -417,6 +417,7 @@
最新版本
源码
版本号
+ 于 %s 构建
检查更新
请稍候
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 1a4bcecce..5ffbdd135 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -469,6 +469,7 @@
Latest release
Source
Build version
+ Built at %s
Check for updates
Direct
System Proxy