From 7abdac72a466e2b6a86914ceed6560e362ddba3b Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Thu, 9 May 2019 17:01:34 +0200 Subject: [PATCH] Replaced log calls from magiskdb to repo --- .../data/database/base/DatabaseDefinition.kt | 7 +-- .../magisk/data/repository/LogRepository.kt | 10 ++++ .../magisk/model/entity/MagiskLog.kt | 6 +- .../magisk/model/entity/recycler/LogRvItem.kt | 14 +++-- .../topjohnwu/magisk/model/flash/Flashing.kt | 3 +- .../topjohnwu/magisk/ui/log/LogViewModel.kt | 56 ++++++++----------- .../java/com/topjohnwu/magisk/utils/XTime.kt | 9 ++- 7 files changed, 55 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/data/database/base/DatabaseDefinition.kt b/app/src/main/java/com/topjohnwu/magisk/data/database/base/DatabaseDefinition.kt index ddf0d0210..2a5c364af 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/database/base/DatabaseDefinition.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/database/base/DatabaseDefinition.kt @@ -18,11 +18,8 @@ object DatabaseDefinition { @AnyThread fun MagiskQuery.query() = query.su() -fun String.suRaw() = Single.just(Shell.su(this)) - .map { it.exec().out } - -fun String.su() = suRaw() - .map { it.toMap() } +fun String.suRaw() = Single.fromCallable { Shell.su(this).exec().out } +fun String.su() = suRaw().map { it.toMap() } fun List.toMap() = map { it.split(Regex("\\|")) } .map { it.toMapInternal() } diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/LogRepository.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/LogRepository.kt index e98bba0bc..a07c7f7ba 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/LogRepository.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/LogRepository.kt @@ -1,10 +1,13 @@ package com.topjohnwu.magisk.data.repository +import com.topjohnwu.magisk.Const import com.topjohnwu.magisk.Constants import com.topjohnwu.magisk.data.database.LogDao import com.topjohnwu.magisk.data.database.base.suRaw import com.topjohnwu.magisk.model.entity.MagiskLog import com.topjohnwu.magisk.model.entity.WrappedMagiskLog +import com.topjohnwu.magisk.utils.toSingle +import com.topjohnwu.superuser.Shell import timber.log.Timber import java.util.concurrent.TimeUnit @@ -21,6 +24,13 @@ class LogRepository( .filter { it.isNotEmpty() } .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.wrap(): List { val day = TimeUnit.DAYS.toMillis(1) var currentDay = firstOrNull()?.date?.time ?: return listOf() diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskLog.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskLog.kt index fba4600e2..4a4ea7187 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskLog.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskLog.kt @@ -1,5 +1,7 @@ package com.topjohnwu.magisk.model.entity +import com.topjohnwu.magisk.utils.timeFormatTime +import com.topjohnwu.magisk.utils.toTime import java.util.* data class MagiskLog( @@ -11,7 +13,9 @@ data class MagiskLog( val command: String, val action: Boolean, val date: Date -) +) { + val timeString = date.time.toTime(timeFormatTime) +} data class WrappedMagiskLog( val time: Long, diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/LogRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/LogRvItem.kt index ea2b03537..ad6eb6ec6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/LogRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/LogRvItem.kt @@ -1,11 +1,13 @@ package com.topjohnwu.magisk.model.entity.recycler -import androidx.databinding.ObservableList import com.skoumal.teanity.databinding.ComparableRvItem import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.util.KObservableField 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 class LogRvItem : ComparableRvItem() { @@ -25,12 +27,12 @@ class LogRvItem : ComparableRvItem() { } class LogItemRvItem( - val items: ObservableList> + item: WrappedMagiskLog ) : ComparableRvItem() { override val layoutRes: Int = R.layout.item_superuser_log - val date = items.filterIsInstance().firstOrNull() - ?.item?.dateString.orEmpty() + val date = item.time.toTime(timeFormatMedium) + val items: List> = item.items.map { LogItemEntryRvItem(it) } val isExpanded = KObservableField(false) fun toggle() = isExpanded.toggle() @@ -41,7 +43,7 @@ class LogItemRvItem( override fun itemSameAs(other: LogItemRvItem): Boolean = date == other.date } -class LogItemEntryRvItem(val item: SuLogEntry) : ComparableRvItem() { +class LogItemEntryRvItem(val item: MagiskLog) : ComparableRvItem() { override val layoutRes: Int = R.layout.item_superuser_log_entry val isExpanded = KObservableField(false) diff --git a/app/src/main/java/com/topjohnwu/magisk/model/flash/Flashing.kt b/app/src/main/java/com/topjohnwu/magisk/model/flash/Flashing.kt index 858ddcc8c..802e6f91d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/flash/Flashing.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/flash/Flashing.kt @@ -4,7 +4,6 @@ import android.content.Context import android.net.Uri import androidx.core.os.postDelayed import com.topjohnwu.magisk.tasks.FlashZip -import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.inject import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.internal.UiThreadHandler @@ -33,7 +32,7 @@ sealed class Flashing( override fun onResult(success: Boolean) { if (success) { - Utils.loadModules() + //Utils.loadModules() } super.onResult(success) } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt index 0d93c32e9..7fb3c7b40 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt @@ -1,10 +1,9 @@ package com.topjohnwu.magisk.ui.log import android.content.res.Resources -import androidx.databinding.ObservableArrayList import com.skoumal.teanity.databinding.ComparableRvItem 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.util.DiffObservableList 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.Const import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.data.database.MagiskDB -import com.topjohnwu.magisk.model.entity.recycler.* +import com.topjohnwu.magisk.data.repository.LogRepository +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.ui.base.MagiskViewModel -import com.topjohnwu.magisk.utils.toSingle -import com.topjohnwu.magisk.utils.zip import com.topjohnwu.superuser.Shell -import io.reactivex.Single import me.tatarka.bindingcollectionadapter2.BindingViewPagerAdapter import me.tatarka.bindingcollectionadapter2.OnItemBind import java.io.File @@ -28,7 +27,7 @@ import java.util.* class LogViewModel( private val resources: Resources, - private val database: MagiskDB + private val logRepo: LogRepository ) : MagiskViewModel(), BindingViewPagerAdapter.PageTitles> { val items = DiffObservableList(ComparableRvItem.callback) @@ -58,9 +57,10 @@ class LogViewModel( else -> "" } - fun refresh() = zip(updateLogs(), updateMagiskLog()) { _, _ -> true } - .subscribeK() - .add() + fun refresh() { + fetchLogs().subscribeK { logItem.update(it) } + fetchMagiskLog().subscribeK { magiskLogItem.update(it) } + } fun saveLog() { val now = Calendar.getInstance() @@ -88,35 +88,25 @@ class LogViewModel( else -> Unit } - private fun clearLogs(callback: () -> Unit) { - Single.fromCallable { database.clearLogs() } - .subscribeK { - SnackbarEvent(R.string.logs_cleared).publish() - callback() - } - .add() - } + private fun clearLogs(callback: () -> Unit) = logRepo.clearLogs() + .doOnSubscribeUi(callback) + .subscribeK { SnackbarEvent(R.string.logs_cleared).publish() } + .add() - private fun clearMagiskLogs(callback: () -> Unit) { - Shell.su("echo -n > " + Const.MAGISK_LOG).submit { - SnackbarEvent(R.string.logs_cleared).publish() - callback() - } - } + private fun clearMagiskLogs(callback: () -> Unit) = logRepo.clearMagiskLogs() + .ignoreElement() + .doOnComplete(callback) + .subscribeK { SnackbarEvent(R.string.logs_cleared).publish() } + .add() - private fun updateLogs() = Single.fromCallable { database.logs } + private fun fetchLogs() = logRepo.fetchLogs() .flattenAsFlowable { it } - .map { it.map { LogItemEntryRvItem(it) } } - .map { LogItemRvItem(ObservableArrayList>().apply { addAll(it) }) } + .map { LogItemRvItem(it) } .toList() - .doOnSuccessUi { logItem.update(it) } - private fun updateMagiskLog() = Shell.su("tail -n 5000 ${Const.MAGISK_LOG}").toSingle() - .map { it.exec() } - .map { it.out } + private fun fetchMagiskLog() = logRepo.fetchMagiskLogs() .flattenAsFlowable { it } .map { ConsoleRvItem(it) } .toList() - .doOnSuccessUi { magiskLogItem.update(it) } } \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/XTime.kt b/app/src/main/java/com/topjohnwu/magisk/utils/XTime.kt index 3c4bf54ca..26128fda4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/XTime.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/XTime.kt @@ -1,13 +1,14 @@ package com.topjohnwu.magisk.utils +import java.text.DateFormat import java.text.ParseException import java.text.SimpleDateFormat import java.util.* val now get() = System.currentTimeMillis() -fun Long.toTime(format: SimpleDateFormat) = format.format(this).orEmpty() -fun String.toTime(format: SimpleDateFormat) = try { +fun Long.toTime(format: DateFormat) = format.format(this).orEmpty() +fun String.toTime(format: DateFormat) = try { format.parse(this)?.time ?: -1 } catch (e: ParseException) { -1L @@ -15,4 +16,6 @@ fun String.toTime(format: SimpleDateFormat) = try { private val locale get() = Locale.getDefault() 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) } \ No newline at end of file +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) } \ No newline at end of file