Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide widgets from automotive build and prevent intent registration too #4751

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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