mirror of
https://github.com/topjohnwu/Magisk.git
synced 2026-01-12 04:56:13 -08:00
Use RV to show Magisk logs
This commit is contained in:
@@ -1,30 +1,29 @@
|
||||
package com.topjohnwu.magisk.ui.log
|
||||
|
||||
import androidx.databinding.Bindable
|
||||
import com.topjohnwu.magisk.BR
|
||||
import androidx.databinding.ViewDataBinding
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.textview.MaterialTextView
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.core.model.su.SuLog
|
||||
import com.topjohnwu.magisk.databinding.ObservableDiffRvItem
|
||||
import com.topjohnwu.magisk.databinding.RvContainer
|
||||
import com.topjohnwu.magisk.databinding.set
|
||||
import com.topjohnwu.magisk.ktx.timeDateFormat
|
||||
import com.topjohnwu.magisk.ktx.toTime
|
||||
import com.topjohnwu.magisk.databinding.ViewAwareRvItem
|
||||
|
||||
class LogRvItem(
|
||||
override val item: SuLog
|
||||
) : ObservableDiffRvItem<LogRvItem>(), RvContainer<SuLog> {
|
||||
override val item: String
|
||||
) : ObservableDiffRvItem<LogRvItem>(), RvContainer<String>, ViewAwareRvItem {
|
||||
|
||||
override val layoutRes = R.layout.item_log_access_md2
|
||||
override val layoutRes = R.layout.item_log_textview
|
||||
|
||||
val date = item.time.toTime(timeDateFormat)
|
||||
override fun itemSameAs(other: LogRvItem) = item == other.item
|
||||
|
||||
@get:Bindable
|
||||
var isTop = false
|
||||
set(value) = set(value, field, { field = it }, BR.top)
|
||||
|
||||
@get:Bindable
|
||||
var isBottom = false
|
||||
set(value) = set(value, field, { field = it }, BR.bottom)
|
||||
|
||||
override fun itemSameAs(other: LogRvItem) = item.appName == other.item.appName
|
||||
override fun onBind(binding: ViewDataBinding, recyclerView: RecyclerView) {
|
||||
val view = binding.root as MaterialTextView
|
||||
view.measure(0, 0)
|
||||
val desiredWidth = view.measuredWidth
|
||||
val layoutParams = view.layoutParams
|
||||
layoutParams.width = desiredWidth
|
||||
if (recyclerView.width < desiredWidth) {
|
||||
recyclerView.requestLayout()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,9 @@ import java.io.FileInputStream
|
||||
class LogViewModel(
|
||||
private val repo: LogRepository
|
||||
) : AsyncLoadViewModel() {
|
||||
@get:Bindable
|
||||
var loading = true
|
||||
private set(value) = set(value, field, { field = it }, BR.loading)
|
||||
|
||||
// --- empty view
|
||||
|
||||
@@ -34,27 +37,34 @@ class LogViewModel(
|
||||
|
||||
// --- su log
|
||||
|
||||
val items = DiffRvItemList<LogRvItem>()
|
||||
val items = DiffRvItemList<SuLogRvItem>()
|
||||
val extraBindings = bindExtra {
|
||||
it.put(BR.viewModel, this)
|
||||
}
|
||||
|
||||
// --- magisk log
|
||||
@get:Bindable
|
||||
var consoleText = " "
|
||||
set(value) = set(value, field, { field = it }, BR.consoleText)
|
||||
val logs = DiffRvItemList<LogRvItem>()
|
||||
var magiskLogRaw = " "
|
||||
|
||||
override suspend fun doLoadWork() {
|
||||
consoleText = repo.fetchMagiskLogs()
|
||||
val (suLogs, diff) = withContext(Dispatchers.Default) {
|
||||
val suLogs = repo.fetchSuLogs().map { LogRvItem(it) }
|
||||
loading = true
|
||||
val (logs, logDiff) = withContext(Dispatchers.Default) {
|
||||
magiskLogRaw = repo.fetchMagiskLogs()
|
||||
val logs = magiskLogRaw.split('\n').map { LogRvItem(it) }
|
||||
logs to this@LogViewModel.logs.calculateDiff(logs)
|
||||
}
|
||||
this.logs.update(logs, logDiff)
|
||||
|
||||
val (suLogs, suDiff) = withContext(Dispatchers.Default) {
|
||||
val suLogs = repo.fetchSuLogs().map { SuLogRvItem(it) }
|
||||
suLogs to items.calculateDiff(suLogs)
|
||||
}
|
||||
items.firstOrNull()?.isTop = false
|
||||
items.lastOrNull()?.isBottom = false
|
||||
items.update(suLogs, diff)
|
||||
items.update(suLogs, suDiff)
|
||||
items.firstOrNull()?.isTop = true
|
||||
items.lastOrNull()?.isBottom = true
|
||||
loading = false
|
||||
}
|
||||
|
||||
fun saveMagiskLog() = withExternalRW {
|
||||
@@ -82,7 +92,7 @@ class LogViewModel(
|
||||
|
||||
file.write("\n---Magisk Logs---\n")
|
||||
file.write("${Info.env.versionString} (${Info.env.versionCode})\n\n")
|
||||
if (Info.env.isActive) file.write(consoleText)
|
||||
if (Info.env.isActive) file.write(magiskLogRaw)
|
||||
|
||||
file.write("\n---Manager Logs---\n")
|
||||
file.write("${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})\n\n")
|
||||
|
||||
30
app/src/main/java/com/topjohnwu/magisk/ui/log/SuLogRvItem.kt
Normal file
30
app/src/main/java/com/topjohnwu/magisk/ui/log/SuLogRvItem.kt
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.topjohnwu.magisk.ui.log
|
||||
|
||||
import androidx.databinding.Bindable
|
||||
import com.topjohnwu.magisk.BR
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.core.model.su.SuLog
|
||||
import com.topjohnwu.magisk.databinding.ObservableDiffRvItem
|
||||
import com.topjohnwu.magisk.databinding.RvContainer
|
||||
import com.topjohnwu.magisk.databinding.set
|
||||
import com.topjohnwu.magisk.ktx.timeDateFormat
|
||||
import com.topjohnwu.magisk.ktx.toTime
|
||||
|
||||
class SuLogRvItem(
|
||||
override val item: SuLog
|
||||
) : ObservableDiffRvItem<SuLogRvItem>(), RvContainer<SuLog> {
|
||||
|
||||
override val layoutRes = R.layout.item_log_access_md2
|
||||
|
||||
val date = item.time.toTime(timeDateFormat)
|
||||
|
||||
@get:Bindable
|
||||
var isTop = false
|
||||
set(value) = set(value, field, { field = it }, BR.top)
|
||||
|
||||
@get:Bindable
|
||||
var isBottom = false
|
||||
set(value) = set(value, field, { field = it }, BR.bottom)
|
||||
|
||||
override fun itemSameAs(other: SuLogRvItem) = item.appName == other.item.appName
|
||||
}
|
||||
Reference in New Issue
Block a user