Skip to content

Commit

Permalink
Merge pull request #2 from YuS1aN/dev
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
YuS1aN committed Mar 24, 2024
2 parents c827a60 + b607345 commit 5b07a50
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 22 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/me/kbai/zhenxunui/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import com.google.gson.GsonBuilder
import com.google.gson.ToNumberPolicy
import me.kbai.zhenxunui.api.ErrorHandleAdapterFactory
import me.kbai.zhenxunui.model.BotBaseInfo
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

/**
* @author Sean on 2023/5/31
Expand All @@ -35,6 +38,13 @@ object Constants {

var currentBot: BotBaseInfo? = null

val logFile by lazy {
val date = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
.format(System.currentTimeMillis())
mApplication.getExternalFilesDir(null)
?.let { File(it, "$date.log") }
}

@JvmStatic
fun init(app: Application) {
mApplication = app
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/me/kbai/zhenxunui/ZxApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;

import me.kbai.zhenxunui.tool.CrashHandler;
import me.kbai.zhenxunui.tool.GlobalToast;

/**
Expand All @@ -16,6 +17,7 @@ public void onCreate() {
mApp = this;
Constants.init(this);
GlobalToast.init(this);
CrashHandler.INSTANCE.init(this);
}

public static Application getApplication() {
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/me/kbai/zhenxunui/extends/AppExtends.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Point
import android.net.Uri
import android.os.Build
Expand Down Expand Up @@ -93,10 +94,15 @@ fun WindowManager.displaySize(): Size =
bounds.height() - insetsHeight
)
} else {
val point = Point()
val point0 = Point()
val point1 = Point()
@Suppress("DEPRECATION")
defaultDisplay.getSize(point)
Size(point.x, point.y)
defaultDisplay.getCurrentSizeRange(point0, point1)

when (Resources.getSystem().configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> Size(point1.x, point0.y)
else -> Size(point0.x, point1.y)
}
}

fun Context.displaySize(): Size {
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/me/kbai/zhenxunui/extends/LogExtends.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package me.kbai.zhenxunui.extends

import android.text.format.DateFormat
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.kbai.zhenxunui.BuildConfig
import me.kbai.zhenxunui.Constants
import java.util.Date

/**
* @author sean on 2022/5/12
*/

private val mCoroutineScope = CoroutineScope(Dispatchers.IO)

fun Any.logI(msg: String, tag: String? = null): Int {
val tagOrUnknown = tag ?: nameOrSuperclassName ?: "INFO"
outputLogFile(msg, tagOrUnknown)
Expand Down Expand Up @@ -37,8 +45,13 @@ private inline fun whenDebug(action: () -> Int): Int {
return 0
}

private fun outputLogFile(msg: String, tag: String) {
//
private fun outputLogFile(msg: String, tag: String) = mCoroutineScope.launch {
val file = Constants.logFile ?: return@launch
if (!file.exists()) {
if (!file.createNewFile()) return@launch
}
val dateTime = DateFormat.format("yyyy-MM-dd HH:mm:ss", Date()).toString()
file.appendText("$dateTime $tag $msg \n")
}

/**
Expand Down
34 changes: 20 additions & 14 deletions app/src/main/java/me/kbai/zhenxunui/tool/Ansi2SpannedHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
import android.text.style.UnderlineSpan
import androidx.annotation.RequiresApi
import me.kbai.zhenxunui.extends.logE
import pk.ansi4j.core.DefaultFunctionFinder
import pk.ansi4j.core.DefaultParserFactory
import pk.ansi4j.core.DefaultTextHandler
Expand All @@ -30,7 +31,7 @@ import pk.ansi4j.core.iso6429.IndependentControlFunctionHandler
import java.util.AbstractCollection

class Ansi2SpannedHelper {
private val mFactory: ParserFactory? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
private val mFactory: ParserFactory? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
initFactory()
} else {
null
Expand All @@ -41,22 +42,27 @@ class Ansi2SpannedHelper {
val builder = SpannableStringBuilder()
val parser = (mFactory ?: return SpannedString(text)).createParser(text)

while (true) {
val fragment = parser.parse() ?: break
if (fragment.type == FragmentType.TEXT) {
val start = builder.length
builder.append(fragment.text)
mStyles.forEach {
builder.setSpan(it, start, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
} else if (fragment.type == FragmentType.FUNCTION) {
val functionFragment = fragment as FunctionFragment
if (functionFragment.function == ControlSequenceFunction.SGR_SELECT_GRAPHIC_RENDITION) {
functionFragment.arguments.forEach { parseArgument(it) }
try {
while (true) {
val fragment = parser.parse() ?: break
if (fragment.type == FragmentType.TEXT) {
val start = builder.length
builder.append(fragment.text)
mStyles.forEach {
builder.setSpan(it, start, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
} else if (fragment.type == FragmentType.FUNCTION) {
val functionFragment = fragment as FunctionFragment
if (functionFragment.function == ControlSequenceFunction.SGR_SELECT_GRAPHIC_RENDITION) {
functionFragment.arguments.forEach { parseArgument(it) }
}
}
}
mStyles.clear()
} catch (e: Exception) {
logE(e)
return SpannedString(text)
}
mStyles.clear()
return builder
}

Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/me/kbai/zhenxunui/tool/CrashHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.kbai.zhenxunui.tool

import android.content.Context
import android.content.pm.PackageManager.NameNotFoundException
import android.os.Build
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import me.kbai.zhenxunui.extends.logE

object CrashHandler {

fun init(context: Context) {
Thread.setDefaultUncaughtExceptionHandler { _, exception ->
runBlocking(Dispatchers.IO) {
log(context, exception)
}
}
}

private fun log(context: Context, throwable: Throwable) = logE(
"MODEL: ${Build.MODEL} " +
"SDK: ${Build.VERSION.SDK_INT} " +
"CPU_ABI: ${Build.SUPPORTED_ABIS.contentToString()} " +
"VERSION: ${getPackageVersionName(context)} " +
"\n StackTrace: \n" +
Log.getStackTraceString(throwable),
"CRASH"
)


private fun getPackageVersionName(context: Context) = try {
context.packageManager.getPackageInfo(context.packageName, 0).versionName
} catch (e: NameNotFoundException) {
""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class BaseEditInfoFragment<VB : ViewBinding> : BaseFragment<VB>() {
.build()
}

protected abstract fun getWebViews(): Array<WebView>
protected abstract fun getWebViews(): Array<out WebView>

protected abstract fun updateInfo(): Any

Expand All @@ -45,8 +45,8 @@ abstract class BaseEditInfoFragment<VB : ViewBinding> : BaseFragment<VB>() {
}

override fun onDestroyView() {
super.onDestroyView()
getWebViews().forEach { it.destroy() }
super.onDestroyView()
}

@SuppressLint("SetJavaScriptEnabled")
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/dialog_edit_text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
android:background="@drawable/bg_dialog_round"
android:minWidth="260dp"
android:orientation="vertical"
android:padding="@dimen/box_padding">
android:paddingHorizontal="@dimen/box_padding"
android:paddingTop="@dimen/box_padding"
android:paddingBottom="14dp">

<TextView
android:id="@+id/tv_title"
Expand Down

0 comments on commit 5b07a50

Please sign in to comment.