From aa8dd44d6a6265a69dfbd30262198c0b2a7c5b88 Mon Sep 17 00:00:00 2001 From: MrSluffy Date: Mon, 30 Dec 2024 08:31:17 +0800 Subject: [PATCH] Fix widget configuration for two-step process - closes : #5124 Bug: java.lang.IllegalArgumentException: Bad widget id 2367 --- src/com/android/launcher3/Launcher.java | 4 +++- .../widget/LauncherWidgetHolder.java | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 6c5e6071b5..03da536167 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -1532,8 +1532,10 @@ public class Launcher extends StatefulActivity if (appWidgetInfo == null) { appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId, itemInfo.getTargetComponent()); - if (appWidgetInfo == null) + if (appWidgetInfo == null){ + mAppWidgetHolder.deleteAppWidgetId(appWidgetId); return; + } } if (hostView == null && !showPendingWidget) { diff --git a/src/com/android/launcher3/widget/LauncherWidgetHolder.java b/src/com/android/launcher3/widget/LauncherWidgetHolder.java index ffa8d80a57..2ddb481e72 100644 --- a/src/com/android/launcher3/widget/LauncherWidgetHolder.java +++ b/src/com/android/launcher3/widget/LauncherWidgetHolder.java @@ -238,6 +238,22 @@ public class LauncherWidgetHolder { } catch (ActivityNotFoundException | SecurityException e) { Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); sendActionCancelled(activity, requestCode); + } catch (IllegalArgumentException e) { + // Widget ID became invalid, possibly due to two-step configuration some cases + Log.e(this.getClass().getName(), "Widget ID became invalid during configuration", e); + handleInvalidWidgetId(activity, widgetId, requestCode); + } + } + + private void handleInvalidWidgetId(BaseDraggingActivity activity, int widgetId, int requestCode) { + // Remove the invalid widget + deleteAppWidgetId(widgetId); + + int newWidgetId = allocateAppWidgetId(); + if (newWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { + startConfigActivity(activity, newWidgetId, requestCode); + } else { + sendActionCancelled(activity, requestCode); } } @@ -272,7 +288,9 @@ public class LauncherWidgetHolder { // Must allow background activity start for U. Utilities.allowBGLaunch(activityOptionsWrapper.options); Bundle bundle = activityOptionsWrapper.toBundle(); - bundle.putInt(KEY_SPLASH_SCREEN_STYLE, SPLASH_SCREEN_STYLE_EMPTY); + if (Utilities.ATLEAST_S) { + bundle.putInt(KEY_SPLASH_SCREEN_STYLE, SPLASH_SCREEN_STYLE_EMPTY); + } return bundle; }