Initial commit for customattrs module

This commit is contained in:
MM20 2022-07-18 21:58:30 +02:00
parent e3de50693f
commit 5bd86e6f95
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
11 changed files with 257 additions and 1 deletions

View File

@ -8,7 +8,7 @@ buildscript {
dependencies {
classpath("com.android.tools.build:gradle:7.2.1")
classpath(libs.kotlin.gradle)
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}

1
customattrs/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

View File

@ -0,0 +1,47 @@
plugins {
id("com.android.library")
id("kotlin-android")
}
android {
compileSdk = sdk.versions.compileSdk.get().toInt()
defaultConfig {
minSdk = sdk.versions.minSdk.get().toInt()
targetSdk = sdk.versions.targetSdk.get().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
namespace = "de.mm20.launcher2.customattrs"
}
dependencies {
implementation(libs.bundles.kotlin)
implementation(libs.androidx.core)
implementation(libs.koin.android)
implementation(project(":database"))
implementation(project(":search"))
implementation(project(":ktx"))
}

View File

21
customattrs/proguard-rules.pro vendored Normal file
View File

@ -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

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.mm20.launcher2.customattrs">
</manifest>

View File

@ -0,0 +1,153 @@
package de.mm20.launcher2.customattrs
import android.graphics.Color
import android.util.Log
import de.mm20.launcher2.database.entities.CustomAttributeEntity
import de.mm20.launcher2.ktx.jsonObjectOf
import org.json.JSONObject
sealed interface CustomAttribute {
fun toDatabaseEntity(key: String): CustomAttributeEntity
companion object {
internal fun fromDatabaseEntity(entity: CustomAttributeEntity): CustomAttribute? {
return when (entity.type) {
CustomAttributeType.Label.value -> CustomLabel(
label = entity.value
)
CustomAttributeType.Tag.value -> CustomTag(
tagName = entity.value
)
CustomAttributeType.Icon.value -> CustomIcon.fromDatabaseEntity(entity)
else -> {
Log.e("MM20", "Invalid custom attribute type: ${entity.type}")
null
}
}
}
}
}
class CustomLabel(
val label: String,
) : CustomAttribute {
override fun toDatabaseEntity(key: String): CustomAttributeEntity {
return CustomAttributeEntity(
key = key,
type = CustomAttributeType.Label.value,
value = label,
)
}
}
class CustomTag(
val tagName: String
): CustomAttribute {
override fun toDatabaseEntity(key: String): CustomAttributeEntity {
return CustomAttributeEntity(
key = key,
type = CustomAttributeType.Tag.value,
value = tagName,
)
}
}
sealed class CustomIcon : CustomAttribute {
override fun toDatabaseEntity(key: String): CustomAttributeEntity {
return CustomAttributeEntity(
key = key,
type = CustomAttributeType.Icon.value,
value = this.toDatabaseValue()
)
}
internal abstract fun toDatabaseValue(): String
companion object {
internal fun fromDatabaseEntity(entity: CustomAttributeEntity): CustomIcon? {
val payload = JSONObject(entity.value)
val type = payload.getString("type")
return when (type) {
"custom_icon_pack_icon" -> {
CustomIconPackIcon(
iconName = payload.getString("icon"),
iconPackPackage = payload.getString("icon_pack")
)
}
"custom_themed_icon" -> {
CustomThemedIcon(
iconName = payload.getString("icon"),
iconPackPackage = payload.getString("icon_pack")
)
}
"adaptified_legacy_icon" -> {
AdaptifiedLegacyIcon(
fgScale = payload.getDouble("fg_scale").toFloat(),
bgColor = payload.getInt("bg_color")
)
}
else -> null
}
}
}
}
data class CustomIconPackIcon(
val iconPackPackage: String,
val iconName: String,
) : CustomIcon() {
override fun toDatabaseValue(): String {
return jsonObjectOf(
"type" to "custom_icon_pack_icon",
"icon" to iconName,
"icon_pack" to iconPackPackage,
).toString()
}
}
data class AdaptifiedLegacyIcon(
val fgScale: Float,
/**
* The background color in ARGB format or [UnspecifiedColor] or [ThemeColor]
*/
val bgColor: Int = UnspecifiedColor,
): CustomIcon() {
override fun toDatabaseValue(): String {
return jsonObjectOf(
"type" to "adaptified_legacy_icon",
"fg_scale" to fgScale,
"bg_color" to bgColor,
).toString()
}
companion object {
/**
* Extract color from foreground icon
*/
const val UnspecifiedColor = 1
/**
* Use color from theme
*/
const val ThemeColor = 0
}
}
data class CustomThemedIcon(
val iconPackPackage: String,
val iconName: String,
) : CustomIcon() {
override fun toDatabaseValue(): String {
return jsonObjectOf(
"type" to "custom_themed_icon",
"icon" to iconName,
"icon_pack" to iconPackPackage,
).toString()
}
}

View File

@ -0,0 +1,13 @@
package de.mm20.launcher2.customattrs
enum class CustomAttributeType(val value: String) {
Icon("icon"),
Label("label"),
Tag("tag");
companion object {
internal fun fromValue(value: String): CustomAttributeType {
return values().first { it.value == value }
}
}
}

View File

@ -0,0 +1,8 @@
package de.mm20.launcher2.customattrs
import de.mm20.launcher2.search.data.Searchable
import kotlinx.coroutines.flow.Flow
interface CustomAttributesRepository {
fun getCustomAttributes(searchable: Searchable, type: CustomAttributeType? = null): Flow<List<CustomAttribute>>
}

View File

@ -0,0 +1,7 @@
package de.mm20.launcher2.customattrs
import org.koin.dsl.module
val customAttrsModule = module {
}

View File

@ -390,3 +390,4 @@ include(":accounts")
include(":appshortcuts")
include(":material-color-utilities")
include(":backup")
include(":customattrs")