From 6cf5801138c71d8f01ac5c4af2a84d80db122694 Mon Sep 17 00:00:00 2001 From: Daniel Shokouhi Date: Tue, 22 Oct 2024 12:21:53 -0700 Subject: [PATCH] Hide widgets from automotive build and prevent intent registration too --- .../android/HomeAssistantApplication.kt | 31 ++++++++++--------- .../android/settings/SettingsFragment.kt | 8 ++--- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/HomeAssistantApplication.kt b/app/src/main/java/io/homeassistant/companion/android/HomeAssistantApplication.kt index fe3b274011b..5c0bc6f63eb 100644 --- a/app/src/main/java/io/homeassistant/companion/android/HomeAssistantApplication.kt +++ b/app/src/main/java/io/homeassistant/companion/android/HomeAssistantApplication.kt @@ -5,6 +5,7 @@ import android.app.NotificationManager import android.bluetooth.BluetoothAdapter import android.content.Intent import android.content.IntentFilter +import android.content.pm.PackageManager import android.media.AudioManager import android.net.wifi.WifiManager import android.nfc.NfcAdapter @@ -282,19 +283,21 @@ open class HomeAssistantApplication : Application() { ContextCompat.RECEIVER_EXPORTED ) - // Update widgets when the screen turns on, updates are skipped if widgets were not added - val buttonWidget = ButtonWidget() - val entityWidget = EntityWidget() - val mediaPlayerWidget = MediaPlayerControlsWidget() - val templateWidget = TemplateWidget() - - val screenIntentFilter = IntentFilter() - screenIntentFilter.addAction(Intent.ACTION_SCREEN_ON) - screenIntentFilter.addAction(Intent.ACTION_SCREEN_OFF) - - ContextCompat.registerReceiver(this, buttonWidget, screenIntentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) - ContextCompat.registerReceiver(this, entityWidget, screenIntentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) - ContextCompat.registerReceiver(this, mediaPlayerWidget, screenIntentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) - ContextCompat.registerReceiver(this, templateWidget, screenIntentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) + if (!this.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) { + // Update widgets when the screen turns on, updates are skipped if widgets were not added + val buttonWidget = ButtonWidget() + val entityWidget = EntityWidget() + val mediaPlayerWidget = MediaPlayerControlsWidget() + val templateWidget = TemplateWidget() + + val screenIntentFilter = IntentFilter() + screenIntentFilter.addAction(Intent.ACTION_SCREEN_ON) + screenIntentFilter.addAction(Intent.ACTION_SCREEN_OFF) + + ContextCompat.registerReceiver(this, buttonWidget, screenIntentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) + ContextCompat.registerReceiver(this, entityWidget, screenIntentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) + ContextCompat.registerReceiver(this, mediaPlayerWidget, screenIntentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) + ContextCompat.registerReceiver(this, templateWidget, screenIntentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) + } } } diff --git a/app/src/main/java/io/homeassistant/companion/android/settings/SettingsFragment.kt b/app/src/main/java/io/homeassistant/companion/android/settings/SettingsFragment.kt index 121f4bdfe79..270048611bb 100644 --- a/app/src/main/java/io/homeassistant/companion/android/settings/SettingsFragment.kt +++ b/app/src/main/java/io/homeassistant/companion/android/settings/SettingsFragment.kt @@ -173,7 +173,10 @@ class SettingsFragment( it.entryValues = percentages.map { pct -> pct.toString() }.toTypedArray() } - findPreference("widgets")?.isVisible = Build.MODEL != "Quest" + val isAutomotive = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && requireContext().packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) + + findPreference("widgets")?.isVisible = Build.MODEL != "Quest" || isAutomotive findPreference("manage_widgets")?.setOnPreferenceClickListener { parentFragmentManager.commit { replace(R.id.content, ManageWidgetsSettingsFragment::class.java, null) @@ -182,9 +185,6 @@ class SettingsFragment( return@setOnPreferenceClickListener true } - val isAutomotive = - Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && requireContext().packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) - if (Build.MODEL != "Quest") { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { findPreference("shortcuts")?.let {