mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +00:00
Added multiple entity state widget options (#771)
* Added entity widget text size, separator options * Fix kLint * kLint#2 * Fixed DB migration * kLint fix * Improved code quality
This commit is contained in:
parent
1372902dae
commit
58312c134e
|
@ -22,7 +22,7 @@ import io.homeassistant.companion.android.database.widget.StaticWidgetEntity
|
|||
ButtonWidgetEntity::class,
|
||||
StaticWidgetEntity::class
|
||||
],
|
||||
version = 3
|
||||
version = 4
|
||||
)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract fun authenticationDao(): AuthenticationDao
|
||||
|
@ -48,7 +48,8 @@ abstract class AppDatabase : RoomDatabase() {
|
|||
.allowMainThreadQueries()
|
||||
.addMigrations(
|
||||
MIGRATION_1_2,
|
||||
MIGRATION_2_3
|
||||
MIGRATION_2_3,
|
||||
MIGRATION_3_4
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
@ -66,5 +67,12 @@ abstract class AppDatabase : RoomDatabase() {
|
|||
database.execSQL("CREATE TABLE IF NOT EXISTS `static_widget` (`id` INTEGER NOT NULL, `entity_id` TEXT NOT NULL, `attribute_id` TEXT, `label` TEXT, PRIMARY KEY(`id`))")
|
||||
}
|
||||
}
|
||||
|
||||
private val MIGRATION_3_4 = object : Migration(3, 4) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE `static_widget` ADD `text_size` FLOAT NOT NULL DEFAULT '30'")
|
||||
database.execSQL("ALTER TABLE `static_widget` ADD `separator` TEXT NOT NULL DEFAULT ' '")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,5 +13,9 @@ data class StaticWidgetEntity(
|
|||
@ColumnInfo(name = "attribute_id")
|
||||
val attributeId: String?,
|
||||
@ColumnInfo(name = "label")
|
||||
val label: String?
|
||||
val label: String?,
|
||||
@ColumnInfo(name = "text_size")
|
||||
val textSize: Float = 30F,
|
||||
@ColumnInfo(name = "separator")
|
||||
val separator: String = " "
|
||||
)
|
||||
|
|
|
@ -78,7 +78,9 @@ class Migrations constructor(
|
|||
id.toInt(),
|
||||
entityId,
|
||||
attribute,
|
||||
label
|
||||
label,
|
||||
30F,
|
||||
" "
|
||||
))
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.util.TypedValue
|
||||
import android.widget.RemoteViews
|
||||
import io.homeassistant.companion.android.R
|
||||
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
|
||||
|
@ -32,6 +33,8 @@ class StaticWidget : AppWidgetProvider() {
|
|||
internal const val EXTRA_ENTITY_ID = "EXTRA_ENTITY_ID"
|
||||
internal const val EXTRA_ATTRIBUTE_ID = "EXTRA_ATTRIBUTE_ID"
|
||||
internal const val EXTRA_LABEL = "EXTRA_LABEL"
|
||||
internal const val EXTRA_TEXT_SIZE = "EXTRA_TEXT_SIZE"
|
||||
internal const val EXTRA_SEPARATOR = "EXTRA_SEPARATOR"
|
||||
}
|
||||
|
||||
@Inject
|
||||
|
@ -76,26 +79,35 @@ class StaticWidget : AppWidgetProvider() {
|
|||
|
||||
val views = RemoteViews(context.packageName, R.layout.widget_static).apply {
|
||||
val widget = staticWidgetDao.get(appWidgetId)
|
||||
val entityId: String? = widget?.entityId
|
||||
val attributeId: String? = widget?.attributeId
|
||||
val label: String? = widget?.label
|
||||
setTextViewText(
|
||||
R.id.widgetText,
|
||||
resolveTextToShow(entityId, attributeId)
|
||||
)
|
||||
setTextViewText(
|
||||
R.id.widgetLabel,
|
||||
label ?: entityId
|
||||
)
|
||||
setOnClickPendingIntent(
|
||||
R.id.widgetTextLayout,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
appWidgetId,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
if (widget != null) {
|
||||
val entityId: String = widget.entityId
|
||||
val attributeId: String? = widget.attributeId
|
||||
val label: String? = widget.label
|
||||
val textSize: Float = widget.textSize
|
||||
val separator: String = widget.separator
|
||||
setTextViewTextSize(
|
||||
R.id.widgetText,
|
||||
TypedValue.COMPLEX_UNIT_SP,
|
||||
textSize
|
||||
)
|
||||
)
|
||||
setTextViewText(
|
||||
R.id.widgetText,
|
||||
resolveTextToShow(entityId, attributeId, separator)
|
||||
)
|
||||
setTextViewText(
|
||||
R.id.widgetLabel,
|
||||
label ?: entityId
|
||||
)
|
||||
setOnClickPendingIntent(
|
||||
R.id.widgetTextLayout,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
appWidgetId,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return views
|
||||
|
@ -103,7 +115,8 @@ class StaticWidget : AppWidgetProvider() {
|
|||
|
||||
private suspend fun resolveTextToShow(
|
||||
entityId: String?,
|
||||
attributeId: String?
|
||||
attributeId: String?,
|
||||
separator: String
|
||||
): CharSequence? {
|
||||
val entity = integrationUseCase.getEntities().find { e -> e.entityId.equals(entityId) }
|
||||
|
||||
|
@ -111,7 +124,7 @@ class StaticWidget : AppWidgetProvider() {
|
|||
|
||||
val fetchedAttributes = entity?.attributes as Map<*, *>
|
||||
val attributeValue = fetchedAttributes.get(attributeId)?.toString()
|
||||
return entity.state.plus(if (attributeValue != null && attributeValue.isNotEmpty()) " " else "").plus(attributeValue ?: "")
|
||||
return entity.state.plus(if (attributeValue != null && attributeValue.isNotEmpty()) separator else "").plus(attributeValue ?: "")
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
|
@ -142,6 +155,8 @@ class StaticWidget : AppWidgetProvider() {
|
|||
val entitySelection: String? = extras.getString(EXTRA_ENTITY_ID)
|
||||
val attributeSelection: String? = extras.getString(EXTRA_ATTRIBUTE_ID)
|
||||
val labelSelection: String? = extras.getString(EXTRA_LABEL)
|
||||
val textSizeSelection: String? = extras.getString(EXTRA_TEXT_SIZE)
|
||||
val separatorSelection: String? = extras.getString(EXTRA_SEPARATOR)
|
||||
|
||||
if (entitySelection == null) {
|
||||
Log.e(TAG, "Did not receive complete service call data")
|
||||
|
@ -158,7 +173,9 @@ class StaticWidget : AppWidgetProvider() {
|
|||
appWidgetId,
|
||||
entitySelection,
|
||||
attributeSelection,
|
||||
labelSelection
|
||||
labelSelection,
|
||||
textSizeSelection?.toFloatOrNull() ?: 30F,
|
||||
if (separatorSelection == "") " " else separatorSelection ?: " "
|
||||
))
|
||||
|
||||
onUpdate(context, AppWidgetManager.getInstance(context), intArrayOf(appWidgetId))
|
||||
|
|
|
@ -17,12 +17,7 @@ import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
|
|||
import io.homeassistant.companion.android.domain.integration.Entity
|
||||
import io.homeassistant.companion.android.domain.integration.IntegrationUseCase
|
||||
import javax.inject.Inject
|
||||
import kotlinx.android.synthetic.main.widget_static_configure.add_button
|
||||
import kotlinx.android.synthetic.main.widget_static_configure.append_attribute_value_checkbox
|
||||
import kotlinx.android.synthetic.main.widget_static_configure.attribute_value_linear_layout
|
||||
import kotlinx.android.synthetic.main.widget_static_configure.label
|
||||
import kotlinx.android.synthetic.main.widget_static_configure.widget_text_config_attribute
|
||||
import kotlinx.android.synthetic.main.widget_static_configure.widget_text_config_entity_id
|
||||
import kotlinx.android.synthetic.main.widget_static_configure.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
|
@ -163,6 +158,16 @@ class StaticWidgetConfigureActivity : Activity() {
|
|||
label.text.toString()
|
||||
)
|
||||
|
||||
intent.putExtra(
|
||||
StaticWidget.EXTRA_TEXT_SIZE,
|
||||
textSize.text.toString()
|
||||
)
|
||||
|
||||
intent.putExtra(
|
||||
StaticWidget.EXTRA_SEPARATOR,
|
||||
separator.text.toString()
|
||||
)
|
||||
|
||||
if (appendAttribute) intent.putExtra(
|
||||
StaticWidget.EXTRA_ATTRIBUTE_ID,
|
||||
selectedAttributeId
|
||||
|
|
|
@ -74,6 +74,53 @@
|
|||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:gravity="center"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:padding="5dp"
|
||||
android:text="@string/widget_text_size_label"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatEditText
|
||||
android:id="@+id/textSize"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:imeOptions="actionDone"
|
||||
android:textAlignment="center"
|
||||
android:inputType="number"
|
||||
android:text="@string/widget_text_size_default" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:gravity="center"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:padding="5dp"
|
||||
android:text="@string/widget_separator_label"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatEditText
|
||||
android:id="@+id/separator"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:imeOptions="actionDone"
|
||||
android:textAlignment="center"
|
||||
android:hint="@string/widget_separator_input_hint" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
|
|
|
@ -165,4 +165,8 @@ like to connect to:</string>
|
|||
<string name="widget_text_hint_service_data">Entity ID</string>
|
||||
<string name="widget_text_hint_service_domain">Domain</string>
|
||||
<string name="widget_text_hint_service_service">Service</string>
|
||||
<string name="widget_text_size_label">Widget text size:</string>
|
||||
<string name="widget_text_size_default">30</string>
|
||||
<string name="widget_separator_label">State and attribute separator:</string>
|
||||
<string name="widget_separator_input_hint">No separator</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue