diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index a2f6bf98c7..826eeb285d 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -1512,7 +1512,6 @@ public class Launcher extends StatefulActivity private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) { hostView.setTag(item); - item.onBindAppWidget(this, hostView); hostView.setFocusable(true); hostView.setOnFocusChangeListener(mFocusHandler); } diff --git a/src/com/android/launcher3/model/WorkspaceItemProcessor.kt b/src/com/android/launcher3/model/WorkspaceItemProcessor.kt index 697fbc8fda..f98cab60b7 100644 --- a/src/com/android/launcher3/model/WorkspaceItemProcessor.kt +++ b/src/com/android/launcher3/model/WorkspaceItemProcessor.kt @@ -45,6 +45,7 @@ import com.android.launcher3.util.PackageManagerHelper import com.android.launcher3.util.PackageUserKey import com.android.launcher3.widget.LauncherAppWidgetProviderInfo import com.android.launcher3.widget.WidgetInflater +import com.android.launcher3.widget.util.WidgetSizes /** * This items is used by LoaderTask to process items that have been loaded from the Launcher's DB. @@ -423,44 +424,56 @@ class WorkspaceItemProcessor( } val inflationResult = widgetInflater.inflateAppWidget(appWidgetInfo) var shouldUpdate = inflationResult.isUpdate - if (inflationResult.type == WidgetInflater.TYPE_DELETE) { - c.markDeleted(inflationResult.reason, inflationResult.restoreErrorType) - return - } - val lapi = inflationResult.widgetInfo - if (inflationResult.type == WidgetInflater.TYPE_PENDING) { - tempPackageKey.update(component.packageName, c.user) - val si = installingPkgs[tempPackageKey] - if ( - !c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) && - !isSafeMode && - (si == null) && - (lapi == null) - ) { - // Restore never started - c.markDeleted( - "Unrestored widget removed: $component", - RestoreError.APP_NOT_INSTALLED - ) + when (inflationResult.type) { + WidgetInflater.TYPE_DELETE -> { + c.markDeleted(inflationResult.reason, inflationResult.restoreErrorType) return - } else if ( - !c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) && si != null - ) { - shouldUpdate = true - appWidgetInfo.restoreStatus = - appWidgetInfo.restoreStatus or LauncherAppWidgetInfo.FLAG_RESTORE_STARTED } - appWidgetInfo.installProgress = if (si == null) 0 else (si.getProgress() * 100).toInt() - appWidgetInfo.pendingItemInfo = - WidgetsModel.newPendingItemInfo( + WidgetInflater.TYPE_PENDING -> { + tempPackageKey.update(component.packageName, c.user) + val si = installingPkgs[tempPackageKey] + + if ( + !c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) && + !isSafeMode && + (si == null) && + (lapi == null) + ) { + // Restore never started + c.markDeleted( + "Unrestored widget removed: $component", + RestoreError.APP_NOT_INSTALLED + ) + return + } else if ( + !c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) && si != null + ) { + shouldUpdate = true + appWidgetInfo.restoreStatus = + appWidgetInfo.restoreStatus or LauncherAppWidgetInfo.FLAG_RESTORE_STARTED + } + appWidgetInfo.installProgress = + if (si == null) 0 else (si.getProgress() * 100).toInt() + appWidgetInfo.pendingItemInfo = + WidgetsModel.newPendingItemInfo( + app.context, + appWidgetInfo.providerName, + appWidgetInfo.user + ) + iconCache.getTitleAndIconForApp(appWidgetInfo.pendingItemInfo, false) + } + WidgetInflater.TYPE_REAL -> + WidgetSizes.updateWidgetSizeRangesAsync( + appWidgetInfo.appWidgetId, + lapi, app.context, - appWidgetInfo.providerName, - appWidgetInfo.user + appWidgetInfo.spanX, + appWidgetInfo.spanY ) - iconCache.getTitleAndIconForApp(appWidgetInfo.pendingItemInfo, false) } + if (shouldUpdate) { c.updater() .put(Favorites.APPWIDGET_PROVIDER, component.flattenToString()) diff --git a/src/com/android/launcher3/model/data/LauncherAppWidgetInfo.java b/src/com/android/launcher3/model/data/LauncherAppWidgetInfo.java index 1fbe04f84d..6fa8c546c9 100644 --- a/src/com/android/launcher3/model/data/LauncherAppWidgetInfo.java +++ b/src/com/android/launcher3/model/data/LauncherAppWidgetInfo.java @@ -32,13 +32,11 @@ import android.os.Process; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import com.android.launcher3.Launcher; import com.android.launcher3.LauncherSettings; import com.android.launcher3.logger.LauncherAtom; import com.android.launcher3.util.ContentWriter; import com.android.launcher3.widget.LauncherAppWidgetHostView; import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; -import com.android.launcher3.widget.util.WidgetSizes; /** * Represents a widget (either instantiated or about to be) in the Launcher. @@ -143,8 +141,6 @@ public class LauncherAppWidgetInfo extends ItemInfo { */ private int widgetFeatures; - private boolean mHasNotifiedInitialWidgetSizeChanged; - /** * The container from which this widget was added (e.g. widgets tray, pin widget, search) */ @@ -202,17 +198,6 @@ public class LauncherAppWidgetInfo extends ItemInfo { .put(LauncherSettings.Favorites.APPWIDGET_SOURCE, sourceContainer); } - /** - * When we bind the widget, we should notify the widget that the size has changed if we have not - * done so already (only really for default workspace widgets). - */ - public void onBindAppWidget(Launcher launcher, AppWidgetHostView hostView) { - if (!mHasNotifiedInitialWidgetSizeChanged) { - WidgetSizes.updateWidgetSizeRanges(hostView, launcher, spanX, spanY); - mHasNotifiedInitialWidgetSizeChanged = true; - } - } - @Override protected String dumpProperties() { return super.dumpProperties() diff --git a/src/com/android/launcher3/widget/util/WidgetSizes.java b/src/com/android/launcher3/widget/util/WidgetSizes.java index 7049509bd2..4688359a0c 100644 --- a/src/com/android/launcher3/widget/util/WidgetSizes.java +++ b/src/com/android/launcher3/widget/util/WidgetSizes.java @@ -15,8 +15,11 @@ */ package com.android.launcher3.widget.util; +import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR; + import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProviderInfo; import android.content.ComponentName; import android.content.Context; import android.graphics.Point; @@ -91,20 +94,33 @@ public final class WidgetSizes { */ public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context, int spanX, int spanY) { - AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); - int widgetId = widgetView.getAppWidgetId(); - if (widgetId <= 0) { + updateWidgetSizeRangesAsync( + widgetView.getAppWidgetId(), widgetView.getAppWidgetInfo(), context, spanX, spanY); + } + + /** + * Updates a given {@code widgetId} with size, {@code spanX}, {@code spanY} asynchronously. + * + *

On Android S+, it also updates the given {@code widgetView} with a list of sizes derived + * from {@code spanX}, {@code spanY} in all supported device profiles. + */ + public static void updateWidgetSizeRangesAsync(int widgetId, + AppWidgetProviderInfo info, Context context, int spanX, int spanY) { + if (widgetId <= 0 || info == null) { return; } - Bundle sizeOptions = getWidgetSizeOptions(context, widgetView.getAppWidgetInfo().provider, - spanX, spanY); - if (sizeOptions.getParcelableArrayList( - AppWidgetManager.OPTION_APPWIDGET_SIZES).equals( - widgetManager.getAppWidgetOptions(widgetId).getParcelableArrayList( - AppWidgetManager.OPTION_APPWIDGET_SIZES))) { - return; - } - widgetManager.updateAppWidgetOptions(widgetId, sizeOptions); + + 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))) { + return; + } + widgetManager.updateAppWidgetOptions(widgetId, sizeOptions); + }); } /**