From 5bd86e6f95b0d6b2fcf302413a3be4d84f19d2e2 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Mon, 18 Jul 2022 21:58:30 +0200 Subject: [PATCH] Initial commit for customattrs module --- build.gradle.kts | 2 +- customattrs/.gitignore | 1 + customattrs/build.gradle.kts | 47 ++++++ customattrs/consumer-rules.pro | 0 customattrs/proguard-rules.pro | 21 +++ customattrs/src/main/AndroidManifest.xml | 5 + .../launcher2/customattrs/CustomAttribute.kt | 153 ++++++++++++++++++ .../customattrs/CustomAttributeType.kt | 13 ++ .../customattrs/CustomAttributesRepository.kt | 8 + .../de/mm20/launcher2/customattrs/Module.kt | 7 + settings.gradle.kts | 1 + 11 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 customattrs/.gitignore create mode 100644 customattrs/build.gradle.kts create mode 100644 customattrs/consumer-rules.pro create mode 100644 customattrs/proguard-rules.pro create mode 100644 customattrs/src/main/AndroidManifest.xml create mode 100644 customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttribute.kt create mode 100644 customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributeType.kt create mode 100644 customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributesRepository.kt create mode 100644 customattrs/src/main/java/de/mm20/launcher2/customattrs/Module.kt diff --git a/build.gradle.kts b/build.gradle.kts index 79918af1..1a28ac60 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 } diff --git a/customattrs/.gitignore b/customattrs/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/customattrs/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/customattrs/build.gradle.kts b/customattrs/build.gradle.kts new file mode 100644 index 00000000..cb82cac1 --- /dev/null +++ b/customattrs/build.gradle.kts @@ -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")) + +} \ No newline at end of file diff --git a/customattrs/consumer-rules.pro b/customattrs/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/customattrs/proguard-rules.pro b/customattrs/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/customattrs/proguard-rules.pro @@ -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 \ No newline at end of file diff --git a/customattrs/src/main/AndroidManifest.xml b/customattrs/src/main/AndroidManifest.xml new file mode 100644 index 00000000..94f8301e --- /dev/null +++ b/customattrs/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttribute.kt b/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttribute.kt new file mode 100644 index 00000000..3bbaa718 --- /dev/null +++ b/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttribute.kt @@ -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() + } +} diff --git a/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributeType.kt b/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributeType.kt new file mode 100644 index 00000000..b0f384a2 --- /dev/null +++ b/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributeType.kt @@ -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 } + } + } +} \ No newline at end of file diff --git a/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributesRepository.kt b/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributesRepository.kt new file mode 100644 index 00000000..d50fcdb1 --- /dev/null +++ b/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributesRepository.kt @@ -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> +} \ No newline at end of file diff --git a/customattrs/src/main/java/de/mm20/launcher2/customattrs/Module.kt b/customattrs/src/main/java/de/mm20/launcher2/customattrs/Module.kt new file mode 100644 index 00000000..ea9b181f --- /dev/null +++ b/customattrs/src/main/java/de/mm20/launcher2/customattrs/Module.kt @@ -0,0 +1,7 @@ +package de.mm20.launcher2.customattrs + +import org.koin.dsl.module + +val customAttrsModule = module { + +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index ed8172b2..56c90284 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -390,3 +390,4 @@ include(":accounts") include(":appshortcuts") include(":material-color-utilities") include(":backup") +include(":customattrs")