Refactor widgets

This commit is contained in:
MM20
2023-04-10 01:15:27 +02:00
parent 2054386179
commit 3cda75bc77
37 changed files with 1153 additions and 347 deletions
+1
View File
@@ -0,0 +1 @@
/build
+46
View File
@@ -0,0 +1,46 @@
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.services.widgets"
}
dependencies {
implementation(libs.bundles.kotlin)
implementation(libs.androidx.core)
implementation(libs.koin.android)
implementation(project(":core:base"))
implementation(project(":core:i18n"))
implementation(project(":data:widgets"))
}
View File
+21
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
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
@@ -0,0 +1,7 @@
package de.mm20.launcher2.services.widgets
data class BuiltInWidgetInfo(
val type: String,
val label: String,
)
@@ -0,0 +1,9 @@
package de.mm20.launcher2.services.widgets
import WidgetsService
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module
val widgetsServiceModule = module {
single { WidgetsService(androidContext(), get()) }
}
@@ -0,0 +1,71 @@
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
import android.content.pm.LauncherApps
import androidx.core.content.getSystemService
import de.mm20.launcher2.services.widgets.BuiltInWidgetInfo
import de.mm20.launcher2.services.widgets.R
import de.mm20.launcher2.widgets.CalendarWidget
import de.mm20.launcher2.widgets.FavoritesWidget
import de.mm20.launcher2.widgets.MusicWidget
import de.mm20.launcher2.widgets.WeatherWidget
import de.mm20.launcher2.widgets.Widget
import de.mm20.launcher2.widgets.WidgetRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import java.util.UUID
class WidgetsService(
private val context: Context,
private val widgetRepository: WidgetRepository,
) {
suspend fun getAppWidgetProviders(): List<AppWidgetProviderInfo> = withContext(Dispatchers.IO) {
val appWidgetManager = AppWidgetManager.getInstance(context)
val launcherApps = context.getSystemService<LauncherApps>() ?: return@withContext emptyList()
val profiles = launcherApps.profiles
val widgets = mutableListOf<AppWidgetProviderInfo>()
for (profile in profiles) {
widgets.addAll(appWidgetManager.getInstalledProvidersForProfile(profile))
}
widgets
}
fun getBuiltInWidgets(): List<BuiltInWidgetInfo> {
return listOf(
BuiltInWidgetInfo(
type = WeatherWidget.Type,
label = context.getString(R.string.widget_name_weather),
),
BuiltInWidgetInfo(
type = MusicWidget.Type,
label = context.getString(R.string.widget_name_music),
),
BuiltInWidgetInfo(
type = CalendarWidget.Type,
label = context.getString(R.string.widget_name_calendar),
),
BuiltInWidgetInfo(
type = FavoritesWidget.Type,
label = context.getString(R.string.widget_name_favorites),
),
)
}
fun addWidget(widget: Widget, position: Int, parentId: UUID? = null) {
widgetRepository.create(widget, position, parentId)
}
fun getWidgets() = widgetRepository.get()
fun isFavoritesWidgetFirst(): Flow<Boolean> {
return widgetRepository.get(limit = 1).map {
it.firstOrNull() is FavoritesWidget
}
}
companion object {
const val AppWidgetHostId = 44203
}
}