Skip to content

Commit

Permalink
Move PkgRepo to extra module, if possible later split of pkg related …
Browse files Browse the repository at this point in the history
…code from the -io module
  • Loading branch information
d4rken committed Dec 20, 2022
1 parent cdb3e38 commit d494ff1
Show file tree
Hide file tree
Showing 51 changed files with 189 additions and 70 deletions.
3 changes: 2 additions & 1 deletion app-common-io/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencies {
addSerialization()
addIO()
addTesting()
testImplementation("org.robolectric:robolectric:4.5.1")

testImplementation("org.robolectric:robolectric:4.9.1")
testImplementation("androidx.test.ext:junit:1.1.4")
}
1 change: 1 addition & 0 deletions app-common-pkgs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
57 changes: 57 additions & 0 deletions app-common-pkgs/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("kotlin-android")
id("kotlin-kapt")
id("kotlin-parcelize")
}

apply(plugin = "dagger.hilt.android.plugin")

android {
namespace = "${ProjectConfig.packageName}.common.pkgs"
compileSdk = ProjectConfig.compileSdk

defaultConfig {
minSdk = ProjectConfig.minSdk
targetSdk = ProjectConfig.targetSdk
}

setupModuleBuildTypes()

buildFeatures {
viewBinding = true
}

setupCompileOptions()

setupKotlinOptions()

testOptions {
unitTests {
isIncludeAndroidResources = true
}
tasks.withType<Test> {
useJUnitPlatform()
}
}
}

dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:${Versions.Desugar.core}")
implementation(project(":app-common"))
implementation(project(":app-common-io"))
implementation(project(":app-common-root"))
testImplementation(project(":app-common-test"))

addAndroidCore()
addDI()
addCoroutines()
addTesting()

implementation("com.github.d4rken.rxshell:core:v3.0.0")
implementation("com.github.d4rken.rxshell:root:v3.0.0")

testImplementation("org.robolectric:robolectric:4.9.1")
testImplementation("androidx.test.ext:junit:1.1.4")
}
Empty file.
21 changes: 21 additions & 0 deletions app-common-pkgs/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package eu.darken.sdmse.common.pkgs

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("eu.darken.sdmse.common.pkgs.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions app-common-pkgs/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eu.darken.sdmse.common.pkgs

import eu.darken.sdmse.common.collections.mutate
import eu.darken.sdmse.common.coroutine.AppScope
import eu.darken.sdmse.common.debug.Bugs
import eu.darken.sdmse.common.debug.logging.Logging.Priority.VERBOSE
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
Expand Down Expand Up @@ -35,12 +36,17 @@ class PkgRepo @Inject constructor(
.onEach {
log(TAG) { "Refreshing package cache due to event: $it" }
cacheLock.withLock {
pkgCache.value = generatePkgcache()
reload()
}
}
.launchIn(appScope)
}

suspend fun reload() = cacheLock.withLock {
log(TAG) { "reload()" }
pkgCache.value = generatePkgcache()
}

private suspend fun generatePkgcache(): Map<Pkg.Id, CachedInfo> {
log(TAG) { "Generating package cache" }
return gatewaySwitch.useRes {
Expand All @@ -53,7 +59,10 @@ class PkgRepo @Inject constructor(
}
.flatten()
.distinctBy { it.id }
.map { CachedInfo(id = it.id, data = it) }
.map {
if (Bugs.isTrace) log(TAG, VERBOSE) { "Installed package: $it" }
CachedInfo(id = it.id, data = it)
}
.associateBy { it.id }
.also { log(TAG) { "Pkgs total: ${it.size}" } }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import eu.darken.sdmse.common.debug.Bugs
import eu.darken.sdmse.common.debug.logging.Logging.Priority.VERBOSE
import eu.darken.sdmse.common.debug.logging.asLog
import eu.darken.sdmse.common.debug.logging.log
Expand Down Expand Up @@ -50,20 +51,23 @@ class SharedLibraryPkgSource @Inject constructor(
)
}
log(TAG) { "Found ${libraryPkgs.size} library pkgs" }
log(TAG, VERBOSE) { libraryPkgs.joinToString { "$it\n" } }
log(TAG, VERBOSE) { libraryPkgs.joinToString("\n") }

libraryPkgs
}


private fun SharedLibraryInfo.clawOutPath(): LocalPath? {
val path = try {
val parcel = Parcel.obtain()
parcel.writeParcelable(this, 0)
parcel.setDataPosition(0)
val raw = String(parcel.marshall())
parcel.recycle()
LIBRARY_PATH_CLAW.find(raw)?.groupValues?.getOrNull(1)
if (Bugs.isDebug) log(TAG, VERBOSE) { "Trying to claw: $raw" }
getClawPatterns(this.name).firstNotNullOfOrNull {
it.find(raw)?.groupValues?.getOrNull(1)
}

} catch (e: Exception) {
log(TAG) { "Library claw failed on $this: ${e.asLog()}" }
null
Expand All @@ -78,7 +82,18 @@ class SharedLibraryPkgSource @Inject constructor(
}

companion object {
private val LIBRARY_PATH_CLAW = Regex("^.+(/data/.+?\\.apk).+\$")
private val LIBRARY_DATA_PATH_CLAW = Regex("^.+(/data/.+?\\.apk).+\$")
private val LIBRARY_PRODUCT_PATH_CLAW = Regex("^.+(/product/.+?\\.apk).+\$")
private val LIBRARY_GENERIC_PATH_CLAW = Regex("((?:/\\w+)+/.+\\.apk)")
internal fun getClawPatterns(pkgName: String): Set<Regex> {
return setOf(
Regex("((?:/\\w+)+/.+\\.apk)(?:\\W+#\\W+)(${Regex.escape(pkgName)})"),
LIBRARY_DATA_PATH_CLAW,
LIBRARY_PRODUCT_PATH_CLAW,
LIBRARY_GENERIC_PATH_CLAW,
)
}

private val TAG = logTag("PkgRepo", "Source", "SharedLibrary")
}
}
Binary file not shown.
3 changes: 3 additions & 0 deletions app-common-test/src/main/java/testhelpers/BaseTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package testhelpers

import eu.darken.sdmse.common.debug.Bugs
import eu.darken.sdmse.common.debug.logging.Logging
import eu.darken.sdmse.common.debug.logging.Logging.Priority.VERBOSE
import eu.darken.sdmse.common.debug.logging.log
Expand All @@ -12,6 +13,7 @@ open class BaseTest {
Logging.clearAll()
Logging.install(eu.darken.sdmse.common.JUnitLogger())
testClassName = this.javaClass.simpleName
Bugs.isTrace = true
}

companion object {
Expand All @@ -24,6 +26,7 @@ open class BaseTest {
unmockkAll()
log(testClassName!!, VERBOSE) { "onTestClassFinished()" }
Logging.clearAll()
Bugs.isTrace = false
}
}
}
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ dependencies {
testImplementation(project(":app-common-test"))
implementation(project(":app-common-root"))
implementation(project(":app-common-io"))
implementation(project(":app-common-pkgs"))

addDI()
addCoroutines()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import eu.darken.sdmse.common.clutter.MarkerSource
import eu.darken.sdmse.common.pkgs.PkgRepo
import javax.inject.Inject

@Reusable
class DebugMarkerSource @Inject constructor(
pkgRepo: PkgRepo,
pkgRepo: eu.darken.sdmse.common.pkgs.PkgRepo,
jsonMarkerParser: JsonMarkerParser,
) : ManualMarkerSource(
pkgRepo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import eu.darken.sdmse.common.debug.logging.Logging.Priority.WARN
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.pkgs.Pkg
import eu.darken.sdmse.common.pkgs.PkgRepo
import eu.darken.sdmse.common.pkgs.currentPkgs
import eu.darken.sdmse.common.pkgs.features.Installed
import eu.darken.sdmse.common.pkgs.toPkgId
Expand All @@ -17,7 +16,7 @@ import kotlinx.coroutines.sync.withLock
import java.util.regex.Pattern

open class ManualMarkerSource(
private val pkgRepo: PkgRepo,
private val pkgRepo: eu.darken.sdmse.common.pkgs.PkgRepo,
private val clutterEntriesProvider: () -> Collection<JsonMarkerGroup>
) : MarkerSource {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import eu.darken.sdmse.common.clutter.MarkerSource
import eu.darken.sdmse.common.pkgs.PkgRepo
import javax.inject.Inject

@Reusable
class ProductionMarkerSource @Inject constructor(
pkgRepo: PkgRepo,
pkgRepo: eu.darken.sdmse.common.pkgs.PkgRepo,
jsonMarkerParser: JsonMarkerParser,
) : ManualMarkerSource(
pkgRepo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.files.core.APath
import eu.darken.sdmse.common.files.core.GatewaySwitch
import eu.darken.sdmse.common.files.core.local.LocalPath
import eu.darken.sdmse.common.pkgs.PkgRepo
import eu.darken.sdmse.common.pkgs.pkgops.PkgOps
import eu.darken.sdmse.common.sharedresource.HasSharedResource
import eu.darken.sdmse.common.sharedresource.SharedResource
Expand All @@ -23,7 +22,7 @@ import javax.inject.Singleton
class FileForensics @Inject constructor(
@AppScope private val appScope: CoroutineScope,
@ApplicationContext val context: Context,
private val pkgRepo: PkgRepo,
private val pkgRepo: eu.darken.sdmse.common.pkgs.PkgRepo,
private val csiProcessors: Set<@JvmSuppressWildcards CSIProcessor>,
gatewaySwitch: GatewaySwitch,
pkgOps: PkgOps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package eu.darken.sdmse.common.forensics.csi
import eu.darken.sdmse.common.forensics.AreaInfo
import eu.darken.sdmse.common.forensics.CSIProcessor
import eu.darken.sdmse.common.forensics.Owner
import eu.darken.sdmse.common.pkgs.PkgRepo

interface LocalCSIProcessor : CSIProcessor {

suspend fun PkgRepo.isInstalled(owner: Owner): Boolean = isInstalled(owner.pkgId)
suspend fun eu.darken.sdmse.common.pkgs.PkgRepo.isInstalled(owner: Owner): Boolean = isInstalled(owner.pkgId)

override suspend fun findOwners(areaInfo: AreaInfo): CSIProcessor.Result {
require(hasJurisdiction(areaInfo.type)) { "Wrong jurisdiction: ${areaInfo.type}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import eu.darken.sdmse.common.forensics.AreaInfo
import eu.darken.sdmse.common.forensics.Owner
import eu.darken.sdmse.common.forensics.csi.dalvik.DalvikCheck
import eu.darken.sdmse.common.pathChopOffLast
import eu.darken.sdmse.common.pkgs.PkgRepo
import eu.darken.sdmse.common.pkgs.currentPkgs
import java.io.File
import javax.inject.Inject

@Reusable
class CustomDexOptCheck @Inject constructor(
private val pkgRepo: PkgRepo,
private val pkgRepo: eu.darken.sdmse.common.pkgs.PkgRepo,
) : DalvikCheck {

suspend fun check(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import eu.darken.sdmse.common.forensics.AreaInfo
import eu.darken.sdmse.common.forensics.Owner
import eu.darken.sdmse.common.forensics.csi.dalvik.DalvikCheck
import eu.darken.sdmse.common.getFirstDirElement
import eu.darken.sdmse.common.pkgs.PkgRepo
import eu.darken.sdmse.common.pkgs.toPkgId
import javax.inject.Inject

@Reusable
class DirNameCheck @Inject constructor(
private val pkgRepo: PkgRepo,
private val pkgRepo: eu.darken.sdmse.common.pkgs.PkgRepo,
) : DalvikCheck {

suspend fun process(areaInfo: AreaInfo): DalvikCheck.Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import dagger.Reusable
import eu.darken.sdmse.common.files.core.local.LocalPath
import eu.darken.sdmse.common.forensics.Owner
import eu.darken.sdmse.common.forensics.csi.dalvik.DalvikCheck
import eu.darken.sdmse.common.pkgs.PkgRepo
import eu.darken.sdmse.common.pkgs.currentPkgs
import javax.inject.Inject

@Reusable
class SourceDirCheck @Inject constructor(
private val pkgRepo: PkgRepo
private val pkgRepo: eu.darken.sdmse.common.pkgs.PkgRepo
) : DalvikCheck {

suspend fun check(candidates: Collection<LocalPath>): DalvikCheck.Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import eu.darken.sdmse.common.forensics.Owner
import eu.darken.sdmse.common.forensics.csi.LocalCSIProcessor
import eu.darken.sdmse.common.forensics.csi.toOwners
import eu.darken.sdmse.common.getFirstDirElement
import eu.darken.sdmse.common.pkgs.PkgRepo
import eu.darken.sdmse.common.pkgs.pkgops.PkgOps
import eu.darken.sdmse.common.pkgs.toPkgId
import eu.darken.sdmse.common.storage.StorageEnvironment
Expand All @@ -30,7 +29,7 @@ import javax.inject.Inject

@Reusable
class PrivateDataCSI @Inject constructor(
private val pkgRepo: PkgRepo,
private val pkgRepo: eu.darken.sdmse.common.pkgs.PkgRepo,
private val pkgOps: PkgOps,
private val areaManager: DataAreaManager,
private val clutterRepo: ClutterRepo,
Expand Down
Loading

0 comments on commit d494ff1

Please sign in to comment.