Skip to content

Commit

Permalink
Stats: Track packages that were affected by AppControl
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Jul 3, 2024
1 parent d99050e commit 0a37929
Show file tree
Hide file tree
Showing 32 changed files with 754 additions and 70 deletions.
42 changes: 40 additions & 2 deletions app/schemas/eu.darken.sdmse.stats.core.db.ReportsRoomDb/1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "dede40c19096d29c113a2592912301ad",
"identityHash": "14953ab8beb5f65930d7bd6da4f1868b",
"entities": [
{
"tableName": "reports",
Expand Down Expand Up @@ -115,12 +115,50 @@
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "affected_pkgs",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `report_id` TEXT NOT NULL, `action` TEXT NOT NULL, `pkg_id` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "reportId",
"columnName": "report_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "action",
"columnName": "action",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "pkgId",
"columnName": "pkg_id",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'dede40c19096d29c113a2592912301ad')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '14953ab8beb5f65930d7bd6da4f1868b')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package eu.darken.sdmse.appcontrol.core.export

import android.net.Uri
import eu.darken.sdmse.appcontrol.core.AppControlTask
import eu.darken.sdmse.common.R
import eu.darken.sdmse.common.ca.CaString
import eu.darken.sdmse.common.ca.toCaString
import eu.darken.sdmse.common.ca.caString
import eu.darken.sdmse.common.getQuantityString2
import eu.darken.sdmse.common.pkgs.Pkg
import eu.darken.sdmse.common.pkgs.features.Installed
import eu.darken.sdmse.stats.core.AffectedPkg
import eu.darken.sdmse.stats.core.ReportDetails
import eu.darken.sdmse.stats.core.Reportable
import kotlinx.parcelize.Parcelize
Expand All @@ -19,8 +23,18 @@ data class AppExportTask(
data class Result(
val success: Set<AppExporter.Result>,
val failed: Set<Installed.InstallId>,
) : AppControlTask.Result, ReportDetails {
) : AppControlTask.Result, ReportDetails.AffectedPkgs {
override val affectedPkgs: Set<Pkg.Id>
get() = success.map { it.installId.pkgId }.toSet()

override val action: AffectedPkg.Action
get() = AffectedPkg.Action.EXPORTED

override val primaryInfo: CaString
get() = eu.darken.sdmse.common.R.string.general_result_success_message.toCaString()
get() = caString {
val succ = getQuantityString2(R.plurals.result_x_successful, success.size)
val failed = getQuantityString2(R.plurals.result_x_failed, failed.size)
"$succ | $failed"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package eu.darken.sdmse.appcontrol.core.forcestop

import eu.darken.sdmse.appcontrol.core.AppControlTask
import eu.darken.sdmse.common.ca.CaString
import eu.darken.sdmse.common.ca.toCaString
import eu.darken.sdmse.common.ca.caString
import eu.darken.sdmse.common.getQuantityString2
import eu.darken.sdmse.common.pkgs.Pkg
import eu.darken.sdmse.common.pkgs.features.Installed
import eu.darken.sdmse.stats.core.AffectedPkg
import eu.darken.sdmse.stats.core.ReportDetails
import eu.darken.sdmse.stats.core.Reportable
import kotlinx.parcelize.Parcelize
Expand All @@ -17,12 +20,22 @@ data class ForceStopTask(
data class Result(
val success: Set<Installed.InstallId>,
val failed: Set<Installed.InstallId>,
) : AppControlTask.Result, ReportDetails {
) : AppControlTask.Result, ReportDetails.AffectedPkgs {

override val affectedPkgs: Set<Pkg.Id>
get() = success.map { it.pkgId }.toSet()

override val action: AffectedPkg.Action
get() = AffectedPkg.Action.STOPPED

val isSuccess: Boolean
get() = success.isNotEmpty() && failed.isEmpty()

override val primaryInfo: CaString
get() = eu.darken.sdmse.common.R.string.general_result_success_message.toCaString()
get() = caString {
val succ = getQuantityString2(eu.darken.sdmse.common.R.plurals.result_x_successful, success.size)
val failed = getQuantityString2(eu.darken.sdmse.common.R.plurals.result_x_failed, failed.size)
"$succ | $failed"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package eu.darken.sdmse.appcontrol.core.toggle
import eu.darken.sdmse.appcontrol.core.AppControlTask
import eu.darken.sdmse.common.ca.CaString
import eu.darken.sdmse.common.ca.toCaString
import eu.darken.sdmse.common.pkgs.Pkg
import eu.darken.sdmse.common.pkgs.features.Installed
import eu.darken.sdmse.stats.core.AffectedPkg
import eu.darken.sdmse.stats.core.ReportDetails
import eu.darken.sdmse.stats.core.Reportable
import kotlinx.parcelize.Parcelize
Expand All @@ -17,7 +19,14 @@ data class AppControlToggleTask(
data class Result(
private val success: Set<Installed.InstallId>,
private val failed: Set<Installed.InstallId>,
) : AppControlTask.Result, ReportDetails {
) : AppControlTask.Result, ReportDetails.AffectedPkgs {

override val affectedPkgs: Set<Pkg.Id>
get() = success.map { it.pkgId }.toSet()

override val action: AffectedPkg.Action
get() = AffectedPkg.Action.TOGGLED

override val primaryInfo: CaString
get() = eu.darken.sdmse.common.R.string.general_result_success_message.toCaString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package eu.darken.sdmse.appcontrol.core.uninstall

import eu.darken.sdmse.appcontrol.core.AppControlTask
import eu.darken.sdmse.common.ca.CaString
import eu.darken.sdmse.common.ca.toCaString
import eu.darken.sdmse.common.ca.caString
import eu.darken.sdmse.common.getQuantityString2
import eu.darken.sdmse.common.pkgs.Pkg
import eu.darken.sdmse.common.pkgs.features.Installed
import eu.darken.sdmse.stats.core.AffectedPkg
import eu.darken.sdmse.stats.core.ReportDetails
import eu.darken.sdmse.stats.core.Reportable
import kotlinx.parcelize.Parcelize
Expand All @@ -17,8 +20,19 @@ data class UninstallTask(
data class Result(
val success: Set<Installed.InstallId>,
val failed: Set<Installed.InstallId>,
) : AppControlTask.Result, ReportDetails {
) : AppControlTask.Result, ReportDetails.AffectedPkgs {

override val affectedPkgs: Set<Pkg.Id>
get() = success.map { it.pkgId }.toSet()

override val action: AffectedPkg.Action
get() = AffectedPkg.Action.DELETED

override val primaryInfo: CaString
get() = eu.darken.sdmse.common.R.string.general_result_success_message.toCaString()
get() = caString {
val succ = getQuantityString2(eu.darken.sdmse.common.R.plurals.result_x_successful, success.size)
val failed = getQuantityString2(eu.darken.sdmse.common.R.plurals.result_x_failed, failed.size)
"$succ | $failed"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import eu.darken.sdmse.common.upgrade.UpgradeRepo
import eu.darken.sdmse.common.upgrade.isPro
import eu.darken.sdmse.exclusion.core.ExclusionManager
import eu.darken.sdmse.exclusion.core.types.PkgExclusion
import eu.darken.sdmse.main.core.taskmanager.TaskManager
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
Expand All @@ -57,13 +58,14 @@ class AppControlListViewModel @Inject constructor(
private val settings: AppControlSettings,
private val exclusionManager: ExclusionManager,
private val upgradeRepo: UpgradeRepo,
private val taskManager: TaskManager,
) : ViewModel3(dispatcherProvider) {

init {
appControl.state
.take(1)
.filter { it.data == null }
.onEach { appControl.submit(AppControlScanTask()) }
.onEach { taskManager.submit(AppControlScanTask()) }
.launchInViewModel()
}

Expand Down Expand Up @@ -317,7 +319,7 @@ class AppControlListViewModel @Inject constructor(

fun refresh(refreshPkgCache: Boolean = false) = launch {
log(TAG) { "refresh()" }
appControl.submit(AppControlScanTask(refreshPkgCache = refreshPkgCache))
taskManager.submit(AppControlScanTask(refreshPkgCache = refreshPkgCache))
}

fun exclude(items: Collection<AppControlListAdapter.Item>) = launch {
Expand All @@ -337,7 +339,7 @@ class AppControlListViewModel @Inject constructor(
return@launch
}
val targets = items.map { it.appInfo.installId }.toSet()
appControl.submit(AppControlToggleTask(targets = targets))
taskManager.submit(AppControlToggleTask(targets = targets))
}

fun uninstall(items: Collection<AppControlListAdapter.Item>, confirmed: Boolean = false) = launch {
Expand All @@ -347,7 +349,7 @@ class AppControlListViewModel @Inject constructor(
return@launch
}
val targets = items.map { it.appInfo.installId }.toSet()
appControl.submit(UninstallTask(targets = targets))
taskManager.submit(UninstallTask(targets = targets))
}

fun export(items: Collection<AppControlListAdapter.Item>, saveDir: Uri? = null) = launch {
Expand All @@ -363,7 +365,7 @@ class AppControlListViewModel @Inject constructor(
return@launch
}
val targets = items.map { it.appInfo.installId }.toSet()
val result = appControl.submit(AppExportTask(targets = targets, saveDir)) as AppExportTask.Result
val result = taskManager.submit(AppExportTask(targets = targets, saveDir)) as AppExportTask.Result

events.postValue(AppControlListEvents.ExportResult(result.success, result.failed))
}
Expand All @@ -380,7 +382,7 @@ class AppControlListViewModel @Inject constructor(
return@launch
}
val targets = items.map { it.appInfo.installId }.toSet()
val result = appControl.submit(ForceStopTask(targets = targets)) as ForceStopTask.Result
val result = taskManager.submit(ForceStopTask(targets = targets)) as ForceStopTask.Result
events.postValue(AppControlListEvents.ForceStopResult(result))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ class AppActionViewModel @Inject constructor(
UninstallActionVH.Item(
appInfo = appInfo,
onItemClicked = { info ->
val task = UninstallTask(setOf(info.installId))
launch {
val result = appControl.submit(UninstallTask(setOf(info.installId))) as UninstallTask.Result
val result = taskManager.submit(task) as UninstallTask.Result
if (result.failed.isNotEmpty()) throw UninstallException(result.failed.first())
}
}
Expand Down Expand Up @@ -254,7 +255,7 @@ class AppActionViewModel @Inject constructor(
}
log(TAG) { "exportApp($saveDir)" }

val result = appControl.submit(
val result = taskManager.submit(
AppExportTask(
targets = setOf(state.value!!.appInfo.installId),
savePath = saveDir,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eu.darken.sdmse.common.room

import androidx.room.TypeConverter
import eu.darken.sdmse.common.pkgs.Pkg
import eu.darken.sdmse.common.pkgs.toPkgId

class PkgIdTypeConverter {
@TypeConverter
fun from(value: Pkg.Id): String = value.name

@TypeConverter
fun to(value: String): Pkg.Id = value.toPkgId()
}
16 changes: 0 additions & 16 deletions app/src/main/java/eu/darken/sdmse/stats/core/AffectedApp.kt

This file was deleted.

27 changes: 27 additions & 0 deletions app/src/main/java/eu/darken/sdmse/stats/core/AffectedPkg.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package eu.darken.sdmse.stats.core

import androidx.annotation.DrawableRes
import eu.darken.sdmse.R
import eu.darken.sdmse.common.pkgs.Pkg

interface AffectedPkg {
val reportId: ReportId
val action: Action
val pkgId: Pkg.Id

enum class Action {
EXPORTED,
STOPPED,
TOGGLED,
DELETED,
;
}
}

@get:DrawableRes val AffectedPkg.Action.iconRes: Int
get() = when (this) {
AffectedPkg.Action.EXPORTED -> R.drawable.ic_baseline_save_24
AffectedPkg.Action.STOPPED -> R.drawable.ic_alert_octagon_outline_24
AffectedPkg.Action.TOGGLED -> R.drawable.ic_snowflake_24
AffectedPkg.Action.DELETED -> R.drawable.ic_delete
}
5 changes: 5 additions & 0 deletions app/src/main/java/eu/darken/sdmse/stats/core/ReportDetails.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ interface ReportDetails {
interface AffectedPaths : ReportDetails, AffectedCount {
val affectedPaths: Set<APath>

val action: AffectedPath.Action
get() = AffectedPath.Action.DELETED

override val affectedCount: Int
get() = affectedPaths.size
}

interface AffectedPkgs : ReportDetails, AffectedCount {
val affectedPkgs: Set<Pkg.Id>

val action: AffectedPkg.Action

override val affectedCount: Int
get() = affectedPkgs.size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ val ReportDetails.affectedPaths: Collection<APath>?
get() = (this as? ReportDetails.AffectedPaths)?.affectedPaths

val ReportDetails.affectedPkgs: Collection<Pkg.Id>?
get() = (this as? ReportDetails.AffectedPkgs)?.affectedPkgs
get() = (this as? ReportDetails.AffectedPkgs)?.affectedPkgs

val ReportDetails.affectedPkgsAction: AffectedPkg.Action?
get() = (this as? ReportDetails.AffectedPkgs)?.action
Loading

0 comments on commit 0a37929

Please sign in to comment.