From f6940364c84b8fb9186ddf10598f7f8857196262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Mon, 31 Jan 2022 16:40:54 +0100 Subject: [PATCH] Fix adding widgets with invalid config via app to home screen --- .../widgets/button/ButtonWidgetConfigureActivity.kt | 7 +++++-- .../widgets/camera/CameraWidgetConfigureActivity.kt | 4 ++-- .../MediaPlayerControlsWidgetConfigureActivity.kt | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt index d8ced06a19f..bca42e19aa7 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt @@ -286,7 +286,10 @@ class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback { binding.addFieldButton.setOnClickListener(onAddFieldListener) binding.addButton.setOnClickListener { if (requestLauncherSetup) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val widgetConfigService = binding.widgetTextConfigService.text.toString() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && + (widgetConfigService in services || widgetConfigService.split(".", limit = 2).size == 2) + ) { getSystemService()?.requestPinAppWidget( ComponentName(this, ButtonWidget::class.java), null, @@ -297,7 +300,7 @@ class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback { PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE ) ) - } else showAddWidgetError() // this shouldn't be possible + } else showAddWidgetError() } else { onAddWidget() } diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/camera/CameraWidgetConfigureActivity.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/camera/CameraWidgetConfigureActivity.kt index 55ea3030595..d54ed89da1d 100755 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/camera/CameraWidgetConfigureActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/camera/CameraWidgetConfigureActivity.kt @@ -57,7 +57,7 @@ class CameraWidgetConfigureActivity : BaseActivity() { binding.addButton.setOnClickListener { if (requestLauncherSetup) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && selectedEntity != null) { getSystemService()?.requestPinAppWidget( ComponentName(this, CameraWidget::class.java), null, @@ -68,7 +68,7 @@ class CameraWidgetConfigureActivity : BaseActivity() { PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE ) ) - } else showAddWidgetError() // this shouldn't be possible + } else showAddWidgetError() } else { onAddWidget() } diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/media_player_controls/MediaPlayerControlsWidgetConfigureActivity.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/media_player_controls/MediaPlayerControlsWidgetConfigureActivity.kt index 249697f7b01..6f88fc6b037 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/media_player_controls/MediaPlayerControlsWidgetConfigureActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/media_player_controls/MediaPlayerControlsWidgetConfigureActivity.kt @@ -63,7 +63,7 @@ class MediaPlayerControlsWidgetConfigureActivity : BaseActivity() { binding.addButton.setOnClickListener { if (requestLauncherSetup) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && selectedEntity != null) { getSystemService()?.requestPinAppWidget( ComponentName(this, MediaPlayerControlsWidget::class.java), null, @@ -74,7 +74,7 @@ class MediaPlayerControlsWidgetConfigureActivity : BaseActivity() { PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE ) ) - } else showAddWidgetError() // this shouldn't be possible + } else showAddWidgetError() } else { onAddWidget() }