Add Widget::getLabel method
This commit is contained in:
@@ -65,10 +65,6 @@ fun WidgetItem(
|
|||||||
var isDragged by remember { mutableStateOf(false) }
|
var isDragged by remember { mutableStateOf(false) }
|
||||||
val elevation by animateDpAsState(if (isDragged) 8.dp else 0.dp)
|
val elevation by animateDpAsState(if (isDragged) 8.dp else 0.dp)
|
||||||
|
|
||||||
val appWidget = if (widget is AppWidget) remember(widget.config.widgetId) {
|
|
||||||
AppWidgetManager.getInstance(context).getAppWidgetInfo(widget.config.widgetId)
|
|
||||||
} else null
|
|
||||||
|
|
||||||
val backgroundOpacity by animateFloatAsState(
|
val backgroundOpacity by animateFloatAsState(
|
||||||
if (widget is AppWidget && !widget.config.background && !editMode) 0f else MaterialTheme.transparency.surface,
|
if (widget is AppWidget && !widget.config.background && !editMode) 0f else MaterialTheme.transparency.surface,
|
||||||
label = "widgetCardBackgroundOpacity",
|
label = "widgetCardBackgroundOpacity",
|
||||||
@@ -102,19 +98,7 @@ fun WidgetItem(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
Text(
|
Text(
|
||||||
text = when (widget) {
|
text = widget.getLabel(LocalContext.current),
|
||||||
is WeatherWidget -> stringResource(R.string.widget_name_weather)
|
|
||||||
is MusicWidget -> stringResource(R.string.widget_name_music)
|
|
||||||
is CalendarWidget -> stringResource(R.string.widget_name_calendar)
|
|
||||||
is AppsWidget -> stringResource(R.string.widget_name_apps)
|
|
||||||
is NotesWidget -> stringResource(R.string.widget_name_notes)
|
|
||||||
is AppWidget -> remember(widget.config.widgetId) {
|
|
||||||
appWidget?.loadLabel(
|
|
||||||
context.packageManager
|
|
||||||
)
|
|
||||||
}
|
|
||||||
?: stringResource(R.string.widget_name_unknown)
|
|
||||||
},
|
|
||||||
style = MaterialTheme.typography.titleMedium,
|
style = MaterialTheme.typography.titleMedium,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1f)
|
.weight(1f)
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package de.mm20.launcher2.widgets
|
|||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.appwidget.AppWidgetHost
|
import android.appwidget.AppWidgetHost
|
||||||
|
import android.appwidget.AppWidgetManager
|
||||||
import android.appwidget.AppWidgetProviderInfo
|
import android.appwidget.AppWidgetProviderInfo
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
@@ -34,6 +36,23 @@ data class AppWidget(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var label: String? = null
|
||||||
|
|
||||||
|
override fun getLabel(context: Context): String {
|
||||||
|
if (label != null) return label!!
|
||||||
|
val widgetInfo = AppWidgetManager.getInstance(context).getAppWidgetInfo(config.widgetId)
|
||||||
|
if (widgetInfo == null) {
|
||||||
|
label = ""
|
||||||
|
return label!!
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
label = widgetInfo.loadLabel(context.packageManager)
|
||||||
|
} catch (e: PackageManager.NameNotFoundException) {
|
||||||
|
label = ""
|
||||||
|
}
|
||||||
|
return label!!
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val Type = "app"
|
const val Type = "app"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.mm20.launcher2.widgets
|
package de.mm20.launcher2.widgets
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
@@ -27,6 +28,10 @@ data class AppsWidget(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getLabel(context: Context): String {
|
||||||
|
return context.getString(R.string.widget_name_apps)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val Type = "favorites"
|
const val Type = "favorites"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.mm20.launcher2.widgets
|
package de.mm20.launcher2.widgets
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
@@ -31,6 +32,10 @@ data class CalendarWidget(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getLabel(context: Context): String {
|
||||||
|
return context.getString(R.string.widget_name_calendar)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val Type = "calendar"
|
const val Type = "calendar"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.mm20.launcher2.widgets
|
package de.mm20.launcher2.widgets
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
@@ -22,6 +23,10 @@ data class MusicWidget(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getLabel(context: Context): String {
|
||||||
|
return context.getString(R.string.widget_name_music)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val Type = "music"
|
const val Type = "music"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.mm20.launcher2.widgets
|
package de.mm20.launcher2.widgets
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.encodeToString
|
import kotlinx.serialization.encodeToString
|
||||||
@@ -33,6 +34,10 @@ data class NotesWidget(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getLabel(context: Context): String {
|
||||||
|
return context.getString(R.string.widget_name_notes)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val Type = "notes"
|
const val Type = "notes"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,8 @@
|
|||||||
package de.mm20.launcher2.widgets
|
package de.mm20.launcher2.widgets
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.appwidget.AppWidgetHost
|
|
||||||
import android.content.ComponentName
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
|
||||||
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
||||||
import de.mm20.launcher2.ktx.tryStartActivity
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.encodeToString
|
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
|
||||||
@@ -31,6 +25,10 @@ data class WeatherWidget(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getLabel(context: Context): String {
|
||||||
|
return context.getString(R.string.widget_name_weather)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val Type = "weather"
|
const val Type = "weather"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.mm20.launcher2.widgets
|
package de.mm20.launcher2.widgets
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
import de.mm20.launcher2.database.entities.PartialWidgetEntity
|
||||||
import de.mm20.launcher2.database.entities.WidgetEntity
|
import de.mm20.launcher2.database.entities.WidgetEntity
|
||||||
import de.mm20.launcher2.ktx.decodeFromStringOrNull
|
import de.mm20.launcher2.ktx.decodeFromStringOrNull
|
||||||
@@ -21,6 +22,8 @@ sealed class Widget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract fun getLabel(context: Context): String
|
||||||
|
|
||||||
abstract fun toDatabaseEntity(): PartialWidgetEntity
|
abstract fun toDatabaseEntity(): PartialWidgetEntity
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
Reference in New Issue
Block a user