diff --git a/src/com/android/launcher3/widget/util/WidgetSizes.java b/src/com/android/launcher3/widget/util/WidgetSizes.java index 4688359a0c..695feb2a29 100644 --- a/src/com/android/launcher3/widget/util/WidgetSizes.java +++ b/src/com/android/launcher3/widget/util/WidgetSizes.java @@ -32,6 +32,7 @@ import android.util.SizeF; import com.android.launcher3.DeviceProfile; import com.android.launcher3.LauncherAppState; import com.android.launcher3.R; +import com.android.launcher3.Utilities; import com.android.launcher3.model.WidgetItem; import java.util.ArrayList; @@ -105,7 +106,7 @@ public final class WidgetSizes { * from {@code spanX}, {@code spanY} in all supported device profiles. */ public static void updateWidgetSizeRangesAsync(int widgetId, - AppWidgetProviderInfo info, Context context, int spanX, int spanY) { + AppWidgetProviderInfo info, Context context, int spanX, int spanY) { if (widgetId <= 0 || info == null) { return; } @@ -113,13 +114,28 @@ public final class WidgetSizes { UI_HELPER_EXECUTOR.execute(() -> { AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); Bundle sizeOptions = getWidgetSizeOptions(context, info.provider, spanX, spanY); - if (sizeOptions.getParcelableArrayList( - AppWidgetManager.OPTION_APPWIDGET_SIZES).equals( - widgetManager.getAppWidgetOptions(widgetId).getParcelableArrayList( - AppWidgetManager.OPTION_APPWIDGET_SIZES))) { + + if (sizeOptions == null) { + Log.e("WidgetSizes", "Failed to get widget size options for widgetId: " + widgetId); return; } - widgetManager.updateAppWidgetOptions(widgetId, sizeOptions); + + Bundle currentOptions = widgetManager.getAppWidgetOptions(widgetId); + if (currentOptions == null) { + currentOptions = new Bundle(); + } + + if(!Utilities.ATLEAST_S) return; + ArrayList newSizes = sizeOptions.getParcelableArrayList(AppWidgetManager.OPTION_APPWIDGET_SIZES); + ArrayList currentSizes = currentOptions.getParcelableArrayList(AppWidgetManager.OPTION_APPWIDGET_SIZES); + + if (newSizes != null && !newSizes.equals(currentSizes)) { + try { + widgetManager.updateAppWidgetOptions(widgetId, sizeOptions); + } catch (Exception e) { + Log.e("WidgetSizes", "Error updating widget options for widgetId: " + widgetId, e); + } + } }); } @@ -129,6 +145,10 @@ public final class WidgetSizes { public static Bundle getWidgetSizeOptions(Context context, ComponentName provider, int spanX, int spanY) { ArrayList paddedSizes = getWidgetSizesDp(context, spanX, spanY); + if (paddedSizes == null || paddedSizes.isEmpty()) { + Log.e("WidgetSizes", "Failed to get widget sizes for provider: " + provider); + return null; + } Rect rect = getMinMaxSizes(paddedSizes); Bundle options = new Bundle(); @@ -136,7 +156,9 @@ public final class WidgetSizes { options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, rect.top); options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, rect.right); options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, rect.bottom); - options.putParcelableArrayList(AppWidgetManager.OPTION_APPWIDGET_SIZES, paddedSizes); + if (Utilities.ATLEAST_S) { + options.putParcelableArrayList(AppWidgetManager.OPTION_APPWIDGET_SIZES, paddedSizes); + } Log.d("b/267448330", "provider: " + provider + ", paddedSizes: " + paddedSizes + ", getMinMaxSizes: " + rect); return options;