Skip to content

Commit 84d907b

Browse files
committed
feat(ui): show warning or error log info when deploy from the main setting page
1 parent a9d5972 commit 84d907b

File tree

6 files changed

+79
-28
lines changed

6 files changed

+79
-28
lines changed

app/src/main/java/com/osfans/trime/ui/components/log/LogView.kt

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,45 @@ class LogView @JvmOverloads constructor(context: Context, attributeSet: Attribut
5353
super.onDetachedFromWindow()
5454
}
5555

56+
fun fromCustomLogLines(lines: List<String>) {
57+
lines.onEach {
58+
dyeAndAppendString(it)
59+
}
60+
}
61+
5662
fun setLogcat(logcat: Logcat) {
5763
this.logcat = logcat
5864
logcat.initLogFlow()
5965
logcat.logFlow.onEach {
60-
val color = ContextCompat.getColor(
61-
context,
62-
when (it.first()) {
63-
'V' -> R.color.grey_700
64-
'D' -> R.color.grey_700
65-
'I' -> R.color.blue_500
66-
'W' -> R.color.yellow_800
67-
'E' -> R.color.red_400
68-
'F' -> R.color.red_A700
69-
else -> R.color.colorPrimary
70-
}
71-
)
72-
logAdapter.append(
73-
SpannableString(it).apply {
74-
setSpan(
75-
ForegroundColorSpan(color),
76-
0,
77-
it.length,
78-
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
79-
)
80-
}
81-
)
66+
dyeAndAppendString(it)
8267
}.launchIn(findViewTreeLifecycleOwner()!!.lifecycleScope)
8368
}
8469

70+
private fun dyeAndAppendString(str: String) {
71+
val color = ContextCompat.getColor(
72+
context,
73+
when (str.first()) {
74+
'V' -> R.color.grey_700
75+
'D' -> R.color.grey_700
76+
'I' -> R.color.blue_500
77+
'W' -> R.color.yellow_800
78+
'E' -> R.color.red_400
79+
'F' -> R.color.red_A700
80+
else -> R.color.colorPrimary
81+
}
82+
)
83+
logAdapter.append(
84+
SpannableString(str).apply {
85+
setSpan(
86+
ForegroundColorSpan(color),
87+
0,
88+
str.length,
89+
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
90+
)
91+
}
92+
)
93+
}
94+
8595
val currentLog: String
8696
get() = logAdapter.fullLogString()
8797

app/src/main/java/com/osfans/trime/ui/main/PrefMainActivity.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.osfans.trime.data.AppPrefs
2626
import com.osfans.trime.databinding.ActivityPrefBinding
2727
import com.osfans.trime.ui.setup.SetupActivity
2828
import com.osfans.trime.util.applyTranslucentSystemBars
29+
import com.osfans.trime.util.briefResultLogDialog
2930
import com.osfans.trime.util.withLoadingDialog
3031
import kotlinx.coroutines.Dispatchers
3132
import kotlinx.coroutines.withContext
@@ -113,13 +114,16 @@ class PrefMainActivity : AppCompatActivity() {
113114
return when (item.itemId) {
114115
R.id.preference__menu_deploy -> {
115116
lifecycleScope.withLoadingDialog(
116-
this, 200L, R.string.deploy_progress
117+
context = this, titleId = R.string.deploy_progress
117118
) {
119+
withContext(Dispatchers.IO) {
120+
Runtime.getRuntime().exec(arrayOf("logcat", "-c"))
121+
}
118122
withContext(Dispatchers.Default) {
119123
Rime.destroy()
120124
Rime.get(true)
121125
}
122-
ToastUtils.showLong(R.string.deploy_finish)
126+
briefResultLogDialog("native", "W", 1)
123127
}
124128
true
125129
}

app/src/main/java/com/osfans/trime/util/DialogUtils.kt

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package com.osfans.trime.util
22

33
import android.content.Context
44
import android.view.ViewGroup
5+
import android.view.ViewGroup.MarginLayoutParams
56
import android.widget.LinearLayout
67
import android.widget.ProgressBar
78
import androidx.annotation.StringRes
89
import androidx.appcompat.app.AlertDialog
910
import androidx.lifecycle.LifecycleCoroutineScope
10-
import com.blankj.utilcode.util.SizeUtils
11+
import com.blankj.utilcode.util.ToastUtils
1112
import com.osfans.trime.R
13+
import com.osfans.trime.ui.components.log.LogView
14+
import kotlinx.coroutines.Dispatchers
1215
import kotlinx.coroutines.cancelAndJoin
1316
import kotlinx.coroutines.delay
1417
import kotlinx.coroutines.launch
18+
import kotlinx.coroutines.withContext
1519

1620
@Suppress("FunctionName")
1721
// Adapted from https://github.com/fcitx5-android/fcitx5-android/blob/e37f5513239bab279a9e58cf0c9b163e0dbf5efb/app/src/main/java/org/fcitx/fcitx5/android/ui/common/Preset.kt#L60
@@ -28,12 +32,12 @@ fun Context.ProgressBarDialogIndeterminate(@StringRes titleId: Int): AlertDialog
2832
).apply {
2933
isIndeterminate = true
3034
},
31-
ViewGroup.MarginLayoutParams(
35+
MarginLayoutParams(
3236
ViewGroup.LayoutParams.MATCH_PARENT,
3337
ViewGroup.LayoutParams.WRAP_CONTENT
3438
).apply {
35-
val verticalMargin = SizeUtils.dp2px(26F)
36-
val horizontalMargin = SizeUtils.dp2px(20F)
39+
val verticalMargin = dp2px(26)
40+
val horizontalMargin = dp2px(20)
3741
topMargin = horizontalMargin
3842
bottomMargin = horizontalMargin
3943
leftMargin = verticalMargin
@@ -65,3 +69,33 @@ fun LifecycleCoroutineScope.withLoadingDialog(
6569
}
6670
}
6771
}
72+
73+
suspend fun Context.briefResultLogDialog(
74+
tag: String,
75+
priority: String,
76+
thresholds: Int
77+
) = withContext(Dispatchers.Main.immediate) {
78+
val log = withContext(Dispatchers.IO) {
79+
Runtime.getRuntime()
80+
.exec(arrayOf("logcat", "-d", "-v", "brief", "-s", "$tag:$priority"))
81+
.inputStream
82+
.bufferedReader()
83+
.readLines()
84+
}
85+
if (log.size > thresholds) {
86+
val logView = LogView(this@briefResultLogDialog).apply {
87+
fromCustomLogLines(log)
88+
layoutParams = MarginLayoutParams(MarginLayoutParams.MATCH_PARENT, MarginLayoutParams.WRAP_CONTENT).apply {
89+
setPadding(dp2px(20), paddingTop, dp2px(20), paddingBottom)
90+
}
91+
}
92+
AlertDialog.Builder(this@briefResultLogDialog)
93+
.setTitle(R.string.setup__done)
94+
.setMessage(R.string.found_some_problems)
95+
.setView(logView)
96+
.setPositiveButton(R.string.setup__skip_hint_yes, null)
97+
.show()
98+
} else {
99+
ToastUtils.showShort(R.string.setup__done)
100+
}
101+
}

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,5 @@
260260
<string name="simple_key_pin">置顶</string>
261261
<string name="delete_all">删除全部</string>
262262
<string name="disable">禁用</string>
263+
<string name="found_some_problems">但是我们似乎发现了些问题,请检查:</string>
263264
</resources>

app/src/main/res/values-zh-rTW/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,5 @@
261261
<string name="simple_key_pin">置頂</string>
262262
<string name="delete_all">刪除全部</string>
263263
<string name="disable">禁用</string>
264+
<string name="found_some_problems">但是我們似乎發現了些問題,請檢查:</string>
264265
</resources>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,5 @@
264264
<string name="simple_key_pin">Pin</string>
265265
<string name="delete_all">Delete all</string>
266266
<string name="disable">Disbale</string>
267+
<string name="found_some_problems">But we may found some problems, please check:</string>
267268
</resources>

0 commit comments

Comments
 (0)