Skip to content

Commit

Permalink
Hide widgets from automotive build and prevent intent registration too
Browse files Browse the repository at this point in the history
  • Loading branch information
dshokouhi committed Oct 22, 2024
1 parent ed85a1d commit 6cf5801
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ class SettingsFragment(
it.entryValues = percentages.map { pct -> pct.toString() }.toTypedArray()
}

findPreference<PreferenceCategory>("widgets")?.isVisible = Build.MODEL != "Quest"
val isAutomotive =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && requireContext().packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)

findPreference<PreferenceCategory>("widgets")?.isVisible = Build.MODEL != "Quest" || isAutomotive
findPreference<Preference>("manage_widgets")?.setOnPreferenceClickListener {
parentFragmentManager.commit {
replace(R.id.content, ManageWidgetsSettingsFragment::class.java, null)
Expand All @@ -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<PreferenceCategory>("shortcuts")?.let {
Expand Down

0 comments on commit 6cf5801

Please sign in to comment.