Skip to content

Commit

Permalink
More plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Nov 20, 2022
1 parent ae31a87 commit 439be13
Show file tree
Hide file tree
Showing 145 changed files with 2,449 additions and 665 deletions.
1 change: 1 addition & 0 deletions app-common-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
59 changes: 59 additions & 0 deletions app-common-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("kotlin-android")
id("kotlin-kapt")
}

android {
namespace = "eu.darken.sdmse.common"
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"))

addAndroidCore()
addIO()
addSerialization()

implementation("junit:junit:4.13.2")
implementation("org.junit.vintage:junit-vintage-engine:5.8.2")
implementation("androidx.test:core-ktx:1.4.0")

implementation("io.mockk:mockk:1.12.4")

runtimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")
implementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
implementation("org.junit.jupiter:junit-jupiter-params:5.8.2")


implementation("io.kotest:kotest-runner-junit5:5.3.0")
implementation("io.kotest:kotest-assertions-core-jvm:5.3.0")
implementation("io.kotest:kotest-property-jvm:5.3.0")
}
Empty file.
21 changes: 21 additions & 0 deletions app-common-test/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
4 changes: 4 additions & 0 deletions app-common-test/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
@@ -1,24 +1,22 @@
package testhelper
package testhelpers

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
import io.mockk.unmockkAll
import org.junit.jupiter.api.AfterAll
import testhelpers.logging.JUnitLogger


open class BaseTest {
init {
Logging.clearAll()
Logging.install(JUnitLogger())
Logging.install(eu.darken.sdmse.common.JUnitLogger())
testClassName = this.javaClass.simpleName
}

companion object {
const val IO_TEST_BASEDIR = "build/tmp/unit_tests"

private var testClassName: String? = null
const val IO_TEST_BASEDIR = "build/tmp/unit_tests"

@JvmStatic
@AfterAll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testhelper.coroutine
package testhelpers.coroutine

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testhelper.coroutine
package testhelpers.coroutine

import eu.darken.sdmse.common.coroutine.DispatcherProvider
import kotlinx.coroutines.CoroutineDispatcher
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testhelper.coroutine
package testhelpers.coroutine

import eu.darken.sdmse.common.debug.logging.asLog
import kotlinx.coroutines.CancellationException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testhelper.flow
package testhelpers.flow

import eu.darken.sdmse.common.debug.logging.Logging.Priority.WARN
import eu.darken.sdmse.common.debug.logging.asLog
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testhelper.json
package testhelpers.json

import com.squareup.moshi.JsonReader
import com.squareup.moshi.Moshi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testhelper.livedata
package testhelpers.livedata

import androidx.arch.core.executor.ArchTaskExecutor
import androidx.arch.core.executor.TaskExecutor
Expand Down
1 change: 1 addition & 0 deletions app-common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
51 changes: 51 additions & 0 deletions app-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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"
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}")
testImplementation(project(":app-common-test"))

addAndroidCore()
addAndroidUI()
addDI()
addCoroutines()
addSerialization()
addIO()
addTesting()
}
Empty file added app-common/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions app-common/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

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.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions app-common/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
@@ -0,0 +1,63 @@
package eu.darken.sdmse.common

import android.util.Log
import androidx.annotation.Keep
import java.lang.reflect.Field
import java.time.Instant


// Can't be const because that prevents them from being mocked in tests
@Suppress("MayBeConstant")
@Keep
object BuildConfigWrap {
val APPLICATION_ID = getBuildConfigValue("PACKAGENAME") as String
val DEBUG: Boolean = getBuildConfigValue("DEBUG") as Boolean
val BUILD_TYPE: BuildType = when (val typ = getBuildConfigValue("BUILD_TYPE") as String) {
"debug" -> BuildType.DEV
"beta" -> BuildType.BETA
"release" -> BuildType.RELEASE
else -> throw IllegalArgumentException("Unknown buildtype: $typ")
}

@Keep
enum class BuildType {
DEV,
BETA,
RELEASE,
;
}

val FLAVOR: Flavor = when (val flav = getBuildConfigValue("FLAVOR") as String?) {
"gplay" -> Flavor.GPLAY
"foss" -> Flavor.FOSS
null -> Flavor.NONE
else -> throw IllegalStateException("Unknown flavor: $flav")
}

enum class Flavor {
GPLAY,
FOSS,
NONE,
;
}

val BUILD_TIME: Instant = Instant.parse(getBuildConfigValue("BUILDTIME") as String)

val VERSION_CODE: Long = (getBuildConfigValue("VERSION_CODE") as String).toLong()
val VERSION_NAME: String = getBuildConfigValue("VERSION_NAME") as String
val GIT_SHA: String = getBuildConfigValue("GITSHA") as String

val VERSION_DESCRIPTION: String = "v$VERSION_NAME ($VERSION_CODE) ~ $GIT_SHA/$FLAVOR/$BUILD_TYPE"

private fun getBuildConfigValue(fieldName: String): Any? = try {
val c = Class.forName("eu.darken.sdmse.BuildConfig")
val f: Field = c.getField(fieldName).apply {
isAccessible = true
}
f.get(null)
} catch (e: Exception) {
Log.e("getBuildConfigValue", "fieldName: $fieldName")
e.printStackTrace()
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ object BuildWrap {
}

fun hasApiLevel(level: Int): Boolean = BuildWrap.VERSION.SDK_INT >= level

inline fun <reified R> ifApiLevel(level: Int, block: () -> R): R? = if (hasApiLevel(level)) block() else null
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eu.darken.sdmse.common

import eu.darken.sdmse.common.debug.logging.Logging

class JUnitLogger(private val minLogLevel: Logging.Priority = Logging.Priority.VERBOSE) : Logging.Logger {

override fun isLoggable(priority: Logging.Priority): Boolean = priority.intValue >= minLogLevel.intValue

override fun log(priority: Logging.Priority, tag: String, message: String, metaData: Map<String, Any>?) {
println("${System.currentTimeMillis()} ${priority.shortLabel}/$tag: $message")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package eu.darken.sdmse.common

import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable

fun PackageManager.getPackageInfo2(
packageName: String,
flags: Int = 0
): PackageInfo? = try {
getPackageInfo(packageName, flags)
} catch (_: PackageManager.NameNotFoundException) {
null
}

fun PackageManager.getLabel2(
pkgId: String,
): String? = getPackageInfo2(pkgId)
?.applicationInfo
?.let {
if (it.labelRes != 0) it.loadLabel(this).toString()
else it.nonLocalizedLabel?.toString()
}

fun PackageManager.getIcon2(
pkgId: String,
): Drawable? = getPackageInfo2(pkgId)
?.applicationInfo
?.let { if (it.icon != 0) it.loadIcon(this) else null }

val PackageInfo.isSystemApp: Boolean
get() = applicationInfo?.run { flags and ApplicationInfo.FLAG_SYSTEM != 0 } ?: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package eu.darken.sdmse.common

import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import androidx.annotation.ColorInt

fun colorString(@ColorInt color: Int, string: String): SpannableString {
val colored = SpannableString(string)
colored.setSpan(ForegroundColorSpan(color), 0, string.length, 0)
return colored
}
Loading

0 comments on commit 439be13

Please sign in to comment.