adding some null checks at AppWidgetManager

This commit is contained in:
tibbi 2021-05-06 21:23:06 +02:00
parent f646d4e5b2
commit e588494ff5
3 changed files with 6 additions and 6 deletions

View File

@ -47,7 +47,7 @@ val Context.eventsHelper: EventsHelper get() = EventsHelper(this)
val Context.calDAVHelper: CalDAVHelper get() = CalDAVHelper(this)
fun Context.updateWidgets() {
val widgetIDs = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(applicationContext, MyWidgetMonthlyProvider::class.java))
val widgetIDs = AppWidgetManager.getInstance(applicationContext)?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetMonthlyProvider::class.java)) ?: return
if (widgetIDs.isNotEmpty()) {
Intent(applicationContext, MyWidgetMonthlyProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
@ -61,7 +61,7 @@ fun Context.updateWidgets() {
}
fun Context.updateListWidget() {
val widgetIDs = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(applicationContext, MyWidgetListProvider::class.java))
val widgetIDs = AppWidgetManager.getInstance(applicationContext)?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetListProvider::class.java)) ?: return
if (widgetIDs.isNotEmpty()) {
Intent(applicationContext, MyWidgetListProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
@ -72,7 +72,7 @@ fun Context.updateListWidget() {
}
fun Context.updateDateWidget() {
val widgetIDs = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(applicationContext, MyWidgetDateProvider::class.java))
val widgetIDs = AppWidgetManager.getInstance(applicationContext)?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetDateProvider::class.java)) ?: return
if (widgetIDs.isNotEmpty()) {
Intent(applicationContext, MyWidgetDateProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE

View File

@ -31,7 +31,7 @@ class MyWidgetListProvider : AppWidgetProvider() {
val fontSize = context.getWidgetFontSize()
val textColor = context.config.widgetTextColor
val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetManager = AppWidgetManager.getInstance(context) ?: return
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = RemoteViews(context.packageName, R.layout.widget_event_list).apply {
applyColorFilter(R.id.widget_event_list_background, context.config.widgetBgColor)
@ -97,7 +97,7 @@ class MyWidgetListProvider : AppWidgetProvider() {
// hacky solution for reseting the events list
private fun goToToday(context: Context) {
val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetManager = AppWidgetManager.getInstance(context) ?: return
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = RemoteViews(context.packageName, R.layout.widget_event_list)
Intent(context, WidgetServiceEmpty::class.java).apply {

View File

@ -177,7 +177,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider() {
val textColor = context.config.widgetTextColor
val resources = context.resources
val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetManager = AppWidgetManager.getInstance(context) ?: return
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = RemoteViews(context.packageName, R.layout.fragment_month_widget)
views.setText(R.id.top_value, month)