Fix NPE on devices that don't support widgets

Turns out, AppWidgetManager.getInstance(context) can return null. I've
tested that this change fixes the issue on the device.
This commit is contained in:
Ilya Bizyaev 2023-05-10 00:02:50 +02:00
parent 0220e1d800
commit f647e50a2e
No known key found for this signature in database
GPG key ID: 29AACC9F9F66C5B4

View file

@ -14,7 +14,7 @@ class MessageListWidgetManager(
private val messageListRepository: MessageListRepository,
private val config: MessageListWidgetConfig,
) {
private lateinit var appWidgetManager: AppWidgetManager
private var appWidgetManager: AppWidgetManager? = null
private var listenerAdded = false
private val listener = MessageListChangedListener {
@ -83,7 +83,7 @@ class MessageListWidgetManager(
private fun triggerMessageListWidgetUpdate() {
val appWidgetIds = getAppWidgetIds()
if (appWidgetIds.isNotEmpty()) {
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listView)
appWidgetManager?.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listView)
}
}
@ -101,6 +101,6 @@ class MessageListWidgetManager(
private fun getAppWidgetIds(): IntArray {
val componentName = ComponentName(context, config.providerClass)
return appWidgetManager.getAppWidgetIds(componentName)
return appWidgetManager?.getAppWidgetIds(componentName) ?: intArrayOf()
}
}