Skip to content

Commit 0dc6cbf

Browse files
authored
Merge pull request #156 from Moustachauve/fix-widget-crash
fix(widget): prevent crash when publishing widget previews
2 parents ad0eab6 + c5e4985 commit 0dc6cbf

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

app/src/main/java/ca/cgagnier/wlednativeandroid/widget/WidgetPreviewPublisher.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.appwidget.AppWidgetProviderInfo
55
import android.content.ComponentName
66
import android.content.Context
77
import android.os.Build
8+
import android.util.Log
89
import androidx.annotation.RequiresApi
910
import androidx.collection.intSetOf
1011
import androidx.core.content.edit
@@ -15,6 +16,8 @@ import kotlinx.coroutines.CoroutineScope
1516
import kotlinx.coroutines.Dispatchers
1617
import kotlinx.coroutines.launch
1718

19+
private const val TAG = "WidgetPreviewPublisher"
20+
1821
/**
1922
* Publishes widget previews for the launcher widget picker (Android 15+).
2023
* Handles rate limiting by checking if preview is already published and tracking app version.
@@ -57,15 +60,25 @@ object WidgetPreviewPublisher {
5760
return@launch
5861
}
5962

60-
val result = GlanceAppWidgetManager(context)
61-
.setWidgetPreviews(
62-
WledWidgetReceiver::class,
63-
intSetOf(AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN),
64-
)
63+
try {
64+
val result = GlanceAppWidgetManager(context)
65+
.setWidgetPreviews(
66+
WledWidgetReceiver::class,
67+
intSetOf(AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN),
68+
)
6569

66-
// Only save version if successful (not rate-limited)
67-
if (result == SET_WIDGET_PREVIEWS_RESULT_SUCCESS) {
68-
prefs.edit { putInt(KEY_PREVIEW_VERSION, currentVersionCode) }
70+
// Only save version if successful (not rate-limited)
71+
if (result == SET_WIDGET_PREVIEWS_RESULT_SUCCESS) {
72+
prefs.edit { putInt(KEY_PREVIEW_VERSION, currentVersionCode) }
73+
}
74+
} catch (e: kotlinx.coroutines.CancellationException) {
75+
throw e
76+
} catch (e: IllegalArgumentException) {
77+
Log.e(TAG, "Failed to publish widget previews: system limit or invalid state", e)
78+
} catch (e: android.os.RemoteException) {
79+
Log.e(TAG, "Failed to publish widget previews: binder error", e)
80+
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
81+
Log.e(TAG, "Failed to publish widget previews: unexpected error", e)
6982
}
7083
}
7184
}

0 commit comments

Comments
 (0)