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
#4751)

* Hide widgets from automotive build and prevent intent registration too

* Correct condition

* Correct condition check

* Null check around appWidgetManager to further prevent a crash
  • Loading branch information
dshokouhi authored Oct 23, 2024
1 parent 228320c commit ba75715
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 26 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
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ abstract class BaseWidgetProvider : AppWidgetProvider() {
context: Context
) {
val widgetProvider = getWidgetProvider(context)
val systemWidgetIds = AppWidgetManager.getInstance(context)
.getAppWidgetIds(widgetProvider)
.toSet()
val appWidgetManager = AppWidgetManager.getInstance(context) ?: return
val systemWidgetIds = appWidgetManager.getAppWidgetIds(widgetProvider).toSet()
val dbWidgetIds = getAllWidgetIdsWithEntities(context).keys

val invalidWidgetIds = dbWidgetIds.minus(systemWidgetIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ButtonWidget : AppWidgetProvider() {

private fun updateAllWidgets(context: Context) {
mainScope.launch {
val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetManager = AppWidgetManager.getInstance(context) ?: return@launch
val systemWidgetIds = appWidgetManager.getAppWidgetIds(ComponentName(context, ButtonWidget::class.java))
val dbWidgetList = buttonWidgetDao.getAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CameraWidget : AppWidgetProvider() {

private fun updateAllWidgets(context: Context) {
mainScope.launch {
val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetManager = AppWidgetManager.getInstance(context) ?: return@launch
val systemWidgetIds = appWidgetManager.getAppWidgetIds(ComponentName(context, CameraWidget::class.java))
val dbWidgetList = cameraWidgetDao.getAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ class TemplateWidget : AppWidgetProvider() {
private suspend fun updateAllWidgets(
context: Context
) {
val systemWidgetIds = AppWidgetManager.getInstance(context)
.getAppWidgetIds(ComponentName(context, TemplateWidget::class.java))
.toSet()
val appWidgetManager = AppWidgetManager.getInstance(context) ?: return
val systemWidgetIds = appWidgetManager.getAppWidgetIds(ComponentName(context, TemplateWidget::class.java)).toSet()
val dbWidgetIds = templateWidgetDao.getAll().map { it.id }

val invalidWidgetIds = dbWidgetIds.minus(systemWidgetIds)
Expand Down

0 comments on commit ba75715

Please sign in to comment.