Replaced log calls from magiskdb to repo

This commit is contained in:
Viktor De Pasquale 2019-05-09 17:01:34 +02:00
parent 90d85eaf7d
commit 7abdac72a4
7 changed files with 55 additions and 50 deletions

View File

@ -18,11 +18,8 @@ object DatabaseDefinition {
@AnyThread @AnyThread
fun MagiskQuery.query() = query.su() fun MagiskQuery.query() = query.su()
fun String.suRaw() = Single.just(Shell.su(this)) fun String.suRaw() = Single.fromCallable { Shell.su(this).exec().out }
.map { it.exec().out } fun String.su() = suRaw().map { it.toMap() }
fun String.su() = suRaw()
.map { it.toMap() }
fun List<String>.toMap() = map { it.split(Regex("\\|")) } fun List<String>.toMap() = map { it.split(Regex("\\|")) }
.map { it.toMapInternal() } .map { it.toMapInternal() }

View File

@ -1,10 +1,13 @@
package com.topjohnwu.magisk.data.repository package com.topjohnwu.magisk.data.repository
import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.Constants import com.topjohnwu.magisk.Constants
import com.topjohnwu.magisk.data.database.LogDao import com.topjohnwu.magisk.data.database.LogDao
import com.topjohnwu.magisk.data.database.base.suRaw import com.topjohnwu.magisk.data.database.base.suRaw
import com.topjohnwu.magisk.model.entity.MagiskLog import com.topjohnwu.magisk.model.entity.MagiskLog
import com.topjohnwu.magisk.model.entity.WrappedMagiskLog import com.topjohnwu.magisk.model.entity.WrappedMagiskLog
import com.topjohnwu.magisk.utils.toSingle
import com.topjohnwu.superuser.Shell
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -21,6 +24,13 @@ class LogRepository(
.filter { it.isNotEmpty() } .filter { it.isNotEmpty() }
.map { Timber.i(it.toString()); it } .map { Timber.i(it.toString()); it }
fun clearLogs() = logDao.deleteAll()
fun clearOutdated() = logDao.deleteOutdated()
fun clearMagiskLogs() = Shell.su("echo -n > " + Const.MAGISK_LOG)
.toSingle()
.map { it.exec() }
private fun List<MagiskLog>.wrap(): List<WrappedMagiskLog> { private fun List<MagiskLog>.wrap(): List<WrappedMagiskLog> {
val day = TimeUnit.DAYS.toMillis(1) val day = TimeUnit.DAYS.toMillis(1)
var currentDay = firstOrNull()?.date?.time ?: return listOf() var currentDay = firstOrNull()?.date?.time ?: return listOf()

View File

@ -1,5 +1,7 @@
package com.topjohnwu.magisk.model.entity package com.topjohnwu.magisk.model.entity
import com.topjohnwu.magisk.utils.timeFormatTime
import com.topjohnwu.magisk.utils.toTime
import java.util.* import java.util.*
data class MagiskLog( data class MagiskLog(
@ -11,7 +13,9 @@ data class MagiskLog(
val command: String, val command: String,
val action: Boolean, val action: Boolean,
val date: Date val date: Date
) ) {
val timeString = date.time.toTime(timeFormatTime)
}
data class WrappedMagiskLog( data class WrappedMagiskLog(
val time: Long, val time: Long,

View File

@ -1,11 +1,13 @@
package com.topjohnwu.magisk.model.entity.recycler package com.topjohnwu.magisk.model.entity.recycler
import androidx.databinding.ObservableList
import com.skoumal.teanity.databinding.ComparableRvItem import com.skoumal.teanity.databinding.ComparableRvItem
import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.util.DiffObservableList
import com.skoumal.teanity.util.KObservableField import com.skoumal.teanity.util.KObservableField
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.model.entity.SuLogEntry import com.topjohnwu.magisk.model.entity.MagiskLog
import com.topjohnwu.magisk.model.entity.WrappedMagiskLog
import com.topjohnwu.magisk.utils.timeFormatMedium
import com.topjohnwu.magisk.utils.toTime
import com.topjohnwu.magisk.utils.toggle import com.topjohnwu.magisk.utils.toggle
class LogRvItem : ComparableRvItem<LogRvItem>() { class LogRvItem : ComparableRvItem<LogRvItem>() {
@ -25,12 +27,12 @@ class LogRvItem : ComparableRvItem<LogRvItem>() {
} }
class LogItemRvItem( class LogItemRvItem(
val items: ObservableList<ComparableRvItem<*>> item: WrappedMagiskLog
) : ComparableRvItem<LogItemRvItem>() { ) : ComparableRvItem<LogItemRvItem>() {
override val layoutRes: Int = R.layout.item_superuser_log override val layoutRes: Int = R.layout.item_superuser_log
val date = items.filterIsInstance<LogItemEntryRvItem>().firstOrNull() val date = item.time.toTime(timeFormatMedium)
?.item?.dateString.orEmpty() val items: List<ComparableRvItem<*>> = item.items.map { LogItemEntryRvItem(it) }
val isExpanded = KObservableField(false) val isExpanded = KObservableField(false)
fun toggle() = isExpanded.toggle() fun toggle() = isExpanded.toggle()
@ -41,7 +43,7 @@ class LogItemRvItem(
override fun itemSameAs(other: LogItemRvItem): Boolean = date == other.date override fun itemSameAs(other: LogItemRvItem): Boolean = date == other.date
} }
class LogItemEntryRvItem(val item: SuLogEntry) : ComparableRvItem<LogItemEntryRvItem>() { class LogItemEntryRvItem(val item: MagiskLog) : ComparableRvItem<LogItemEntryRvItem>() {
override val layoutRes: Int = R.layout.item_superuser_log_entry override val layoutRes: Int = R.layout.item_superuser_log_entry
val isExpanded = KObservableField(false) val isExpanded = KObservableField(false)

View File

@ -4,7 +4,6 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
import com.topjohnwu.magisk.tasks.FlashZip import com.topjohnwu.magisk.tasks.FlashZip
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.inject import com.topjohnwu.magisk.utils.inject
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.internal.UiThreadHandler import com.topjohnwu.superuser.internal.UiThreadHandler
@ -33,7 +32,7 @@ sealed class Flashing(
override fun onResult(success: Boolean) { override fun onResult(success: Boolean) {
if (success) { if (success) {
Utils.loadModules() //Utils.loadModules()
} }
super.onResult(success) super.onResult(success)
} }

View File

@ -1,10 +1,9 @@
package com.topjohnwu.magisk.ui.log package com.topjohnwu.magisk.ui.log
import android.content.res.Resources import android.content.res.Resources
import androidx.databinding.ObservableArrayList
import com.skoumal.teanity.databinding.ComparableRvItem import com.skoumal.teanity.databinding.ComparableRvItem
import com.skoumal.teanity.extensions.addOnPropertyChangedCallback import com.skoumal.teanity.extensions.addOnPropertyChangedCallback
import com.skoumal.teanity.extensions.doOnSuccessUi import com.skoumal.teanity.extensions.doOnSubscribeUi
import com.skoumal.teanity.extensions.subscribeK import com.skoumal.teanity.extensions.subscribeK
import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.util.DiffObservableList
import com.skoumal.teanity.util.KObservableField import com.skoumal.teanity.util.KObservableField
@ -12,14 +11,14 @@ import com.skoumal.teanity.viewevents.SnackbarEvent
import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.Const import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.database.MagiskDB import com.topjohnwu.magisk.data.repository.LogRepository
import com.topjohnwu.magisk.model.entity.recycler.* import com.topjohnwu.magisk.model.entity.recycler.ConsoleRvItem
import com.topjohnwu.magisk.model.entity.recycler.LogItemRvItem
import com.topjohnwu.magisk.model.entity.recycler.LogRvItem
import com.topjohnwu.magisk.model.entity.recycler.MagiskLogRvItem
import com.topjohnwu.magisk.model.events.PageChangedEvent import com.topjohnwu.magisk.model.events.PageChangedEvent
import com.topjohnwu.magisk.ui.base.MagiskViewModel import com.topjohnwu.magisk.ui.base.MagiskViewModel
import com.topjohnwu.magisk.utils.toSingle
import com.topjohnwu.magisk.utils.zip
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import io.reactivex.Single
import me.tatarka.bindingcollectionadapter2.BindingViewPagerAdapter import me.tatarka.bindingcollectionadapter2.BindingViewPagerAdapter
import me.tatarka.bindingcollectionadapter2.OnItemBind import me.tatarka.bindingcollectionadapter2.OnItemBind
import java.io.File import java.io.File
@ -28,7 +27,7 @@ import java.util.*
class LogViewModel( class LogViewModel(
private val resources: Resources, private val resources: Resources,
private val database: MagiskDB private val logRepo: LogRepository
) : MagiskViewModel(), BindingViewPagerAdapter.PageTitles<ComparableRvItem<*>> { ) : MagiskViewModel(), BindingViewPagerAdapter.PageTitles<ComparableRvItem<*>> {
val items = DiffObservableList(ComparableRvItem.callback) val items = DiffObservableList(ComparableRvItem.callback)
@ -58,9 +57,10 @@ class LogViewModel(
else -> "" else -> ""
} }
fun refresh() = zip(updateLogs(), updateMagiskLog()) { _, _ -> true } fun refresh() {
.subscribeK() fetchLogs().subscribeK { logItem.update(it) }
.add() fetchMagiskLog().subscribeK { magiskLogItem.update(it) }
}
fun saveLog() { fun saveLog() {
val now = Calendar.getInstance() val now = Calendar.getInstance()
@ -88,35 +88,25 @@ class LogViewModel(
else -> Unit else -> Unit
} }
private fun clearLogs(callback: () -> Unit) { private fun clearLogs(callback: () -> Unit) = logRepo.clearLogs()
Single.fromCallable { database.clearLogs() } .doOnSubscribeUi(callback)
.subscribeK { .subscribeK { SnackbarEvent(R.string.logs_cleared).publish() }
SnackbarEvent(R.string.logs_cleared).publish()
callback()
}
.add() .add()
}
private fun clearMagiskLogs(callback: () -> Unit) { private fun clearMagiskLogs(callback: () -> Unit) = logRepo.clearMagiskLogs()
Shell.su("echo -n > " + Const.MAGISK_LOG).submit { .ignoreElement()
SnackbarEvent(R.string.logs_cleared).publish() .doOnComplete(callback)
callback() .subscribeK { SnackbarEvent(R.string.logs_cleared).publish() }
} .add()
}
private fun updateLogs() = Single.fromCallable { database.logs } private fun fetchLogs() = logRepo.fetchLogs()
.flattenAsFlowable { it } .flattenAsFlowable { it }
.map { it.map { LogItemEntryRvItem(it) } } .map { LogItemRvItem(it) }
.map { LogItemRvItem(ObservableArrayList<ComparableRvItem<*>>().apply { addAll(it) }) }
.toList() .toList()
.doOnSuccessUi { logItem.update(it) }
private fun updateMagiskLog() = Shell.su("tail -n 5000 ${Const.MAGISK_LOG}").toSingle() private fun fetchMagiskLog() = logRepo.fetchMagiskLogs()
.map { it.exec() }
.map { it.out }
.flattenAsFlowable { it } .flattenAsFlowable { it }
.map { ConsoleRvItem(it) } .map { ConsoleRvItem(it) }
.toList() .toList()
.doOnSuccessUi { magiskLogItem.update(it) }
} }

View File

@ -1,13 +1,14 @@
package com.topjohnwu.magisk.utils package com.topjohnwu.magisk.utils
import java.text.DateFormat
import java.text.ParseException import java.text.ParseException
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
val now get() = System.currentTimeMillis() val now get() = System.currentTimeMillis()
fun Long.toTime(format: SimpleDateFormat) = format.format(this).orEmpty() fun Long.toTime(format: DateFormat) = format.format(this).orEmpty()
fun String.toTime(format: SimpleDateFormat) = try { fun String.toTime(format: DateFormat) = try {
format.parse(this)?.time ?: -1 format.parse(this)?.time ?: -1
} catch (e: ParseException) { } catch (e: ParseException) {
-1L -1L
@ -16,3 +17,5 @@ fun String.toTime(format: SimpleDateFormat) = try {
private val locale get() = Locale.getDefault() private val locale get() = Locale.getDefault()
val timeFormatFull by lazy { SimpleDateFormat("yyyy/MM/dd_HH:mm:ss", locale) } val timeFormatFull by lazy { SimpleDateFormat("yyyy/MM/dd_HH:mm:ss", locale) }
val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", locale) } val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", locale) }
val timeFormatMedium by lazy { DateFormat.getDateInstance(DateFormat.MEDIUM, LocaleManager.locale) }
val timeFormatTime by lazy { SimpleDateFormat("h:mm a", LocaleManager.locale) }