Add commit sha and build time, drop PackageUtils

This commit is contained in:
FooIbar authored and tarsin committed 2023-02-24 10:00:45 +08:00
1 parent 66e1315b08
commit 3c6d3bbd80
7 files changed
+49 -120

No files matched your search

+21 -1
View File
@@ -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",
+23 -39
View File
@@ -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")
@@ -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) {
}
@@ -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)
}
}
@@ -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;
}
}
}
@@ -417,6 +417,7 @@
<string name="settings_about_latest_release">最新版本</string>
<string name="settings_about_source">源码</string>
<string name="settings_about_version">版本号</string>
<string name="settings_about_build_time">于 %s 构建</string>
<string name="settings_about_check_for_updates">检查更新</string>
<!-- ExcludedLanguagesScene -->
<string name="please_wait">请稍候</string>
+1
View File
@@ -469,6 +469,7 @@
<string name="settings_about_latest_release">Latest release</string>
<string name="settings_about_source">Source</string>
<string name="settings_about_version">Build version</string>
<string name="settings_about_build_time">Built at %s</string>
<string name="settings_about_check_for_updates">Check for updates</string>
<string name="proxy_direct">Direct</string>
<string name="proxy_system">System Proxy</string>