From 40b27395a459605a10f99793323f2ec738411379 Mon Sep 17 00:00:00 2001 From: Zagura Date: Wed, 13 Apr 2022 19:25:44 +0200 Subject: [PATCH] Major refactor, removed all of the Computable stuff in favour of an icon loader --- README.md | 68 ++++- app/build.gradle.kts | 10 +- app/src/main/AndroidManifest.xml | 8 +- .../posidon/android/launcherutils/demo/App.kt | 13 - .../launcherutils/demo/AppCollection.kt | 43 --- .../posidon/android/launcherutil/demo/App.kt | 10 + .../launcherutil}/demo/AppViewHolder.kt | 2 +- .../android/launcherutil}/demo/AppsAdapter.kt | 28 +- .../launcherutil}/demo/MainActivity.kt | 53 +++- app/src/main/res/values/strings.xml | 2 +- app/src/main/res/values/themes.xml | 2 +- build.gradle.kts | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- lib/build.gradle.kts | 8 +- lib/src/main/AndroidManifest.xml | 2 +- .../posidon/android/computable/Computable.kt | 106 ------- .../android/computable/ComputableImpl.kt | 52 ---- .../android/computable/ConstComputableImpl.kt | 21 -- .../io/posidon/android/computable/Depend.kt | 29 -- .../posidon/android/launcherutils/Launcher.kt | 16 - .../launcherutils/appLoading/AppLoader.kt | 285 ------------------ .../appLoading/SimpleAppCollection.kt | 121 -------- .../android/launcherutil}/IconTheming.kt | 2 +- .../posidon/android/launcherutil/Launcher.kt | 56 ++++ .../launcherutil}/liveWallpaper/Kustom.kt | 2 +- .../liveWallpaper/LiveWallpaper.kt | 2 +- .../launcherutil/loader/AppIconLoader.kt | 238 +++++++++++++++ .../android/launcherutil/loader/AppLoader.kt | 60 ++++ .../launcherutil/loader}/IconConfig.kt | 2 +- .../android/launcherutil/loader/IconData.kt | 8 + .../launcherutil/loader/LoadedLauncherItem.kt | 13 + .../system/GestureNavContract.kt | 2 +- settings.gradle.kts | 2 +- 33 files changed, 522 insertions(+), 750 deletions(-) delete mode 100644 app/src/main/java/io/posidon/android/launcherutils/demo/App.kt delete mode 100644 app/src/main/java/io/posidon/android/launcherutils/demo/AppCollection.kt create mode 100644 app/src/main/kotlin/io/posidon/android/launcherutil/demo/App.kt rename app/src/main/{java/io/posidon/android/launcherutils => kotlin/io/posidon/android/launcherutil}/demo/AppViewHolder.kt (87%) rename app/src/main/{java/io/posidon/android/launcherutils => kotlin/io/posidon/android/launcherutil}/demo/AppsAdapter.kt (55%) rename app/src/main/{java/io/posidon/android/launcherutils => kotlin/io/posidon/android/launcherutil}/demo/MainActivity.kt (51%) delete mode 100644 lib/src/main/java/io/posidon/android/computable/Computable.kt delete mode 100644 lib/src/main/java/io/posidon/android/computable/ComputableImpl.kt delete mode 100644 lib/src/main/java/io/posidon/android/computable/ConstComputableImpl.kt delete mode 100644 lib/src/main/java/io/posidon/android/computable/Depend.kt delete mode 100644 lib/src/main/java/io/posidon/android/launcherutils/Launcher.kt delete mode 100644 lib/src/main/java/io/posidon/android/launcherutils/appLoading/AppLoader.kt delete mode 100644 lib/src/main/java/io/posidon/android/launcherutils/appLoading/SimpleAppCollection.kt rename lib/src/main/{java/io/posidon/android/launcherutils => kotlin/io/posidon/android/launcherutil}/IconTheming.kt (99%) create mode 100644 lib/src/main/kotlin/io/posidon/android/launcherutil/Launcher.kt rename lib/src/main/{java/io/posidon/android/launcherutils => kotlin/io/posidon/android/launcherutil}/liveWallpaper/Kustom.kt (94%) rename lib/src/main/{java/io/posidon/android/launcherutils => kotlin/io/posidon/android/launcherutil}/liveWallpaper/LiveWallpaper.kt (90%) create mode 100644 lib/src/main/kotlin/io/posidon/android/launcherutil/loader/AppIconLoader.kt create mode 100644 lib/src/main/kotlin/io/posidon/android/launcherutil/loader/AppLoader.kt rename lib/src/main/{java/io/posidon/android/launcherutils/appLoading => kotlin/io/posidon/android/launcherutil/loader}/IconConfig.kt (69%) create mode 100644 lib/src/main/kotlin/io/posidon/android/launcherutil/loader/IconData.kt create mode 100644 lib/src/main/kotlin/io/posidon/android/launcherutil/loader/LoadedLauncherItem.kt rename lib/src/main/{java/io/posidon/android/launcherutils => kotlin/io/posidon/android/launcherutil}/system/GestureNavContract.kt (97%) diff --git a/README.md b/README.md index 476b7ce..4bb85c9 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,64 @@ -# Launcher utils -A library for android launcher creation (very much in alpha stage) +# launcherutil + +A library for android launcher creation (in alpha stage) ## Features - App loading -- Kustom variables - Icon-related utilities - Send signals to live wallpaper +- Kustom variables -## How to use -Make sure you have the jitpack.io repo added in your project -```kotlin -allprojects { +## Installation +Add the dependency to your `build.gradle(.kts)` file: + + + + + + + + + + + +
KotlinGroovy
+
dependencies {
+    // ...
+    implementation("io.posidon:android.launcherutil:22.0")
+}
+
+
dependencies {
+    // ...
+    implementation 'io.posidon:android.launcherutil:22.0'
+}
+
+ +And also make sure that the `jitpack.io` repository is included + + + + + + + + + + + +
KotlinGroovy
+
allprojects {
     repositories {
+        // ...
         maven {
             url = uri("https://jitpack.io")
         }
     }
-}
-```
-Add the dependency
-```kotlin
-dependencies {
-    implementation("io.posidon:android.launcherUtils:${VERSION}")
-}
-```
\ No newline at end of file
+}
+
+
allprojects {
+    repositories {
+        // ...
+        maven { url 'https://jitpack.io' }
+    }
+}
+
\ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c5528a5..eb290ff 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -6,14 +6,14 @@ plugins { } android { - compileSdk = 31 + compileSdk = 32 defaultConfig { - applicationId = "io.posidon.android.launcherutils.demo" + applicationId = "io.posidon.android.launcherutil.demo" minSdk = 26 - targetSdk = 31 + targetSdk = 32 versionCode = 1 - versionName = "1.0" + versionName = "22.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } @@ -31,7 +31,7 @@ dependencies { implementation("io.posidon:android.convenienceLib:master-SNAPSHOT") implementation("org.jetbrains.kotlin:kotlin-stdlib:${getKotlinPluginVersion()}") implementation("androidx.core:core-ktx:1.7.0") - implementation("com.google.android.material:material:1.4.0") + implementation("com.google.android.material:material:1.5.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.3") androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8bc77c8..55f3b47 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,22 +1,20 @@ + package="io.posidon.android.launcherutil.demo"> + android:theme="@style/Theme.launcherutil"> - - \ No newline at end of file diff --git a/app/src/main/java/io/posidon/android/launcherutils/demo/App.kt b/app/src/main/java/io/posidon/android/launcherutils/demo/App.kt deleted file mode 100644 index b711336..0000000 --- a/app/src/main/java/io/posidon/android/launcherutils/demo/App.kt +++ /dev/null @@ -1,13 +0,0 @@ -package io.posidon.android.launcherutils.demo - -import android.graphics.drawable.Drawable -import android.os.UserHandle -import io.posidon.android.computable.Computable - -class App( - val packageName: String, - val name: String, - val profile: UserHandle, - val label: String, - val icon: Computable -) \ No newline at end of file diff --git a/app/src/main/java/io/posidon/android/launcherutils/demo/AppCollection.kt b/app/src/main/java/io/posidon/android/launcherutils/demo/AppCollection.kt deleted file mode 100644 index b3fc270..0000000 --- a/app/src/main/java/io/posidon/android/launcherutils/demo/AppCollection.kt +++ /dev/null @@ -1,43 +0,0 @@ -package io.posidon.android.launcherutils.demo - -import android.content.Context -import android.graphics.ColorMatrix -import android.graphics.ColorMatrixColorFilter -import android.graphics.drawable.Drawable -import android.os.UserHandle -import io.posidon.android.computable.Computable -import io.posidon.android.computable.copy -import io.posidon.android.launcherutils.appLoading.AppLoader -import io.posidon.android.launcherutils.appLoading.SimpleAppCollection - -class AppCollection(size: Int) : SimpleAppCollection() { - - val list = ArrayList(size) - - override fun addApp( - context: Context, - packageName: String, - name: String, - profile: UserHandle, - label: String, - icon: Computable, - extra: AppLoader.ExtraAppInfo, - ) { - val icon = if (!extra.isUserRunning) { - icon.copy { it.convertToGrayscale(); it } - } else icon - list.add(App(packageName, name, profile, label, icon)) - } - - override fun finalize(context: Context) { - list.sortWith { o1, o2 -> - o1.label.compareTo(o2.label, ignoreCase = true) - } - } - - fun Drawable.convertToGrayscale() { - colorFilter = ColorMatrixColorFilter(ColorMatrix().apply { - setSaturation(0f) - }) - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/io/posidon/android/launcherutil/demo/App.kt b/app/src/main/kotlin/io/posidon/android/launcherutil/demo/App.kt new file mode 100644 index 0000000..1122957 --- /dev/null +++ b/app/src/main/kotlin/io/posidon/android/launcherutil/demo/App.kt @@ -0,0 +1,10 @@ +package io.posidon.android.launcherutil.demo + +import android.os.UserHandle + +class App( + val packageName: String, + val name: String, + val profile: UserHandle, + val label: String +) \ No newline at end of file diff --git a/app/src/main/java/io/posidon/android/launcherutils/demo/AppViewHolder.kt b/app/src/main/kotlin/io/posidon/android/launcherutil/demo/AppViewHolder.kt similarity index 87% rename from app/src/main/java/io/posidon/android/launcherutils/demo/AppViewHolder.kt rename to app/src/main/kotlin/io/posidon/android/launcherutil/demo/AppViewHolder.kt index e83bf91..9a55a50 100644 --- a/app/src/main/java/io/posidon/android/launcherutils/demo/AppViewHolder.kt +++ b/app/src/main/kotlin/io/posidon/android/launcherutil/demo/AppViewHolder.kt @@ -1,4 +1,4 @@ -package io.posidon.android.launcherutils.demo +package io.posidon.android.launcherutil.demo import android.view.View import android.widget.ImageView diff --git a/app/src/main/java/io/posidon/android/launcherutils/demo/AppsAdapter.kt b/app/src/main/kotlin/io/posidon/android/launcherutil/demo/AppsAdapter.kt similarity index 55% rename from app/src/main/java/io/posidon/android/launcherutils/demo/AppsAdapter.kt rename to app/src/main/kotlin/io/posidon/android/launcherutil/demo/AppsAdapter.kt index 96336c7..06a7a3a 100644 --- a/app/src/main/java/io/posidon/android/launcherutils/demo/AppsAdapter.kt +++ b/app/src/main/kotlin/io/posidon/android/launcherutil/demo/AppsAdapter.kt @@ -1,11 +1,11 @@ -package io.posidon.android.launcherutils.demo +package io.posidon.android.launcherutil.demo import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView -import io.posidon.android.computable.compute +import java.util.concurrent.Future -class AppsAdapter : RecyclerView.Adapter() { +class AppsAdapter(val mainActivity: MainActivity) : RecyclerView.Adapter() { private var list = emptyList() @@ -15,21 +15,29 @@ class AppsAdapter : RecyclerView.Adapter() { return AppViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.app_item, parent, false)) } + private var workers = HashMap>() + override fun onBindViewHolder(holder: AppViewHolder, i: Int) { + val img = holder.icon + workers[i]?.cancel(true) + img.setImageDrawable(null) val app = list[i] - holder.icon.setImageDrawable(null) - app.icon.compute { - holder.icon.post { - holder.icon.setImageDrawable(it) + workers[i] = mainActivity.iconLoader.load( + holder.itemView.context, + app.packageName, + app.name, + app.profile, + ) { + img.post { + img.setImageDrawable(it.icon) } } holder.label.text = app.label } override fun onViewRecycled(holder: AppViewHolder) { - val i = holder.adapterPosition - if (i != -1) - list[i].icon.offload() + workers[holder.adapterPosition]?.cancel(true) + holder.icon.setImageDrawable(null) } fun update(list: List) { diff --git a/app/src/main/java/io/posidon/android/launcherutils/demo/MainActivity.kt b/app/src/main/kotlin/io/posidon/android/launcherutil/demo/MainActivity.kt similarity index 51% rename from app/src/main/java/io/posidon/android/launcherutils/demo/MainActivity.kt rename to app/src/main/kotlin/io/posidon/android/launcherutil/demo/MainActivity.kt index ba08738..f1a8d22 100644 --- a/app/src/main/java/io/posidon/android/launcherutils/demo/MainActivity.kt +++ b/app/src/main/kotlin/io/posidon/android/launcherutil/demo/MainActivity.kt @@ -1,19 +1,23 @@ -package io.posidon.android.launcherutils.demo +package io.posidon.android.launcherutil.demo +import android.graphics.ColorMatrix +import android.graphics.ColorMatrixColorFilter +import android.graphics.drawable.Drawable import android.os.Bundle import android.view.View import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView -import io.posidon.android.launcherutils.IconTheming -import io.posidon.android.launcherutils.appLoading.AppLoader -import io.posidon.android.launcherutils.appLoading.IconConfig +import io.posidon.android.launcherutil.IconTheming +import io.posidon.android.launcherutil.Launcher +import io.posidon.android.launcherutil.isUserRunning +import io.posidon.android.launcherutil.loader.AppIconLoader class MainActivity : AppCompatActivity() { - val appsAdapter = AppsAdapter() - val appLoader = AppLoader(::AppCollection) + val appsAdapter = AppsAdapter(this) + lateinit var iconLoader: AppIconLoader override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -37,16 +41,41 @@ class MainActivity : AppCompatActivity() { } fun loadApps(iconPack: String? = null) { - val iconConfig = IconConfig( + iconLoader = Launcher.iconLoader( + this, size = (resources.displayMetrics.density * 128f).toInt(), density = resources.configuration.densityDpi, packPackages = iconPack?.let { arrayOf(it) } ?: emptyArray(), + ) { _, _, profile, icon -> + if (!isUserRunning(profile)) { + icon?.convertToGrayscale() to null + } else icon to null + } + val list = ArrayList() + Launcher.appLoader.loadAsync( + this, + onEnd = { + list.sortWith { o1, o2 -> + o1.label.compareTo(o2.label, ignoreCase = true) + } + runOnUiThread { + appsAdapter.update(list) + } + }, + forEachApp = { + list += App( + it.packageName, + it.name, + it.profile, + it.getBadgedLabel(this) + ) + } ) + } - appLoader.async(this, iconConfig) { - runOnUiThread { - appsAdapter.update(it.list) - } - } + private fun Drawable.convertToGrayscale(): Drawable = apply { + colorFilter = ColorMatrixColorFilter(ColorMatrix().apply { + setSaturation(0f) + }) } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9c7bca9..24a4047 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,4 @@ - LauncherUtils + launcherutil Choose icon pack \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 6f7f65e..e6f3dae 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,6 +1,6 @@ -