diff --git a/quickstep/src/com/android/launcher3/hybridhotseat/HotseatEduDialog.java b/quickstep/src/com/android/launcher3/hybridhotseat/HotseatEduDialog.java index db225be4a9..a4c6ae03a4 100644 --- a/quickstep/src/com/android/launcher3/hybridhotseat/HotseatEduDialog.java +++ b/quickstep/src/com/android/launcher3/hybridhotseat/HotseatEduDialog.java @@ -38,7 +38,6 @@ import com.android.launcher3.Launcher; import com.android.launcher3.R; import com.android.launcher3.celllayout.CellLayoutLayoutParams; import com.android.launcher3.model.data.WorkspaceItemInfo; -import com.android.launcher3.uioverrides.PredictedAppIcon; import com.android.launcher3.views.AbstractSlideInView; import java.util.List; @@ -184,10 +183,9 @@ public class HotseatEduDialog extends AbstractSlideInView implements I private void populatePreview(List predictions) { for (int i = 0; i < mActivityContext.getDeviceProfile().numShownHotseatIcons; i++) { WorkspaceItemInfo info = predictions.get(i); - PredictedAppIcon icon = PredictedAppIcon.createIcon(mSampleHotseat, info); + View icon = mActivityContext.getItemInflater().inflateItem(info, mSampleHotseat); icon.setEnabled(false); icon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); - icon.verifyHighRes(); CellLayoutLayoutParams lp = new CellLayoutLayoutParams(i, 0, 1, 1); mSampleHotseat.addViewToCellLayout(icon, i, info.getViewId(), lp, true); } diff --git a/quickstep/src/com/android/launcher3/hybridhotseat/HotseatPredictionController.java b/quickstep/src/com/android/launcher3/hybridhotseat/HotseatPredictionController.java index c2cabd0557..44aa764900 100644 --- a/quickstep/src/com/android/launcher3/hybridhotseat/HotseatPredictionController.java +++ b/quickstep/src/com/android/launcher3/hybridhotseat/HotseatPredictionController.java @@ -47,6 +47,7 @@ import com.android.launcher3.LauncherPrefs; import com.android.launcher3.LauncherSettings; import com.android.launcher3.R; import com.android.launcher3.anim.AnimationSuccessListener; +import com.android.launcher3.celllayout.CellLayoutLayoutParams; import com.android.launcher3.dragndrop.DragController; import com.android.launcher3.dragndrop.DragOptions; import com.android.launcher3.graphics.DragPreviewProvider; @@ -232,7 +233,7 @@ public class HotseatPredictionController implements DragController.DragListener, if (icon.applyFromWorkspaceItemWithAnimation(predictedItem, numViewsAnimated)) { numViewsAnimated++; } - icon.finishBinding(mPredictionLongClickListener); + finishBinding(icon); } else { newItems.add(predictedItem); } @@ -246,9 +247,9 @@ public class HotseatPredictionController implements DragController.DragListener, private void bindItems(List itemsToAdd, boolean animate) { AnimatorSet animationSet = new AnimatorSet(); for (WorkspaceItemInfo item : itemsToAdd) { - PredictedAppIcon icon = PredictedAppIcon.createIcon(mHotseat, item); + View icon = mLauncher.getItemInflater().inflateItem(item, mHotseat); mLauncher.getWorkspace().addInScreenFromBind(icon, item); - icon.finishBinding(mPredictionLongClickListener); + finishBinding(icon); if (animate) { animationSet.play(ObjectAnimator.ofFloat(icon, SCALE_PROPERTY, 0.2f, 1)); } @@ -262,6 +263,11 @@ public class HotseatPredictionController implements DragController.DragListener, } } + private void finishBinding(View view) { + view.setOnLongClickListener(mPredictionLongClickListener); + ((CellLayoutLayoutParams) view.getLayoutParams()).canReorder = false; + } + private void removeOutlineDrawings() { if (mOutlineDrawings.isEmpty()) return; for (PredictedAppIcon.PredictedIconOutlineDrawing outlineDrawing : mOutlineDrawings) { diff --git a/quickstep/src/com/android/launcher3/uioverrides/PredictedAppIcon.java b/quickstep/src/com/android/launcher3/uioverrides/PredictedAppIcon.java index 42c3526e5e..050358b546 100644 --- a/quickstep/src/com/android/launcher3/uioverrides/PredictedAppIcon.java +++ b/quickstep/src/com/android/launcher3/uioverrides/PredictedAppIcon.java @@ -39,14 +39,11 @@ import android.util.AttributeSet; import android.util.FloatProperty; import android.util.Log; import android.util.Property; -import android.view.LayoutInflater; -import android.view.ViewGroup; import androidx.core.graphics.ColorUtils; import com.android.launcher3.DeviceProfile; import com.android.launcher3.Flags; -import com.android.launcher3.Launcher; import com.android.launcher3.R; import com.android.launcher3.anim.AnimatedFloat; import com.android.launcher3.anim.AnimatorListeners; @@ -279,16 +276,6 @@ public class PredictedAppIcon extends DoubleShadowBubbleTextView { invalidate(); } - /** - * prepares prediction icon for usage after bind - */ - public void finishBinding(OnLongClickListener longClickListener) { - setOnLongClickListener(longClickListener); - ((CellLayoutLayoutParams) getLayoutParams()).canReorder = false; - setTextVisibility(false); - verifyHighRes(); - } - @Override public void getIconBounds(Rect outBounds) { super.getIconBounds(outBounds); @@ -458,19 +445,6 @@ public class PredictedAppIcon extends DoubleShadowBubbleTextView { }; } - /** - * Creates and returns a new instance of PredictedAppIcon from WorkspaceItemInfo - */ - public static PredictedAppIcon createIcon(ViewGroup parent, WorkspaceItemInfo info) { - PredictedAppIcon icon = (PredictedAppIcon) LayoutInflater.from(parent.getContext()) - .inflate(R.layout.predicted_app_icon, parent, false); - icon.applyFromWorkspaceItem(info); - Launcher launcher = Launcher.getLauncher(parent.getContext()); - icon.setOnClickListener(launcher.getItemOnClickListener()); - icon.setOnFocusChangeListener(launcher.getFocusHandler()); - return icon; - } - private class AnimColorHolder { public final AnimatedFloat progress = new AnimatedFloat(this::onUpdate, 1); diff --git a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java index a062a54ee3..f865162513 100644 --- a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java +++ b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java @@ -20,25 +20,21 @@ import static android.view.View.MeasureSpec.EXACTLY; import static android.view.View.MeasureSpec.makeMeasureSpec; import static android.view.View.VISIBLE; -import static com.android.launcher3.BubbleTextView.DISPLAY_TASKBAR; -import static com.android.launcher3.BubbleTextView.DISPLAY_WORKSPACE; import static com.android.launcher3.DeviceProfile.DEFAULT_SCALE; import static com.android.launcher3.Flags.extendibleThemeManager; import static com.android.launcher3.Hotseat.ALPHA_CHANNEL_PREVIEW_RENDERER; import static com.android.launcher3.LauncherPrefs.GRID_NAME; -import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT; import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION; import static com.android.launcher3.Utilities.SHOULD_SHOW_FIRST_PAGE_WIDGET; import static com.android.launcher3.graphics.ThemeManager.PREF_ICON_SHAPE; import static com.android.launcher3.model.ModelUtils.currentScreenContentFilter; import static com.android.launcher3.widget.LauncherWidgetHolder.APPWIDGET_HOST_ID; +import static java.util.Comparator.comparingDouble; + import android.app.Fragment; import android.app.WallpaperColors; import android.app.WallpaperManager; -import android.appwidget.AppWidgetHost; -import android.appwidget.AppWidgetHostView; -import android.appwidget.AppWidgetProviderInfo; import android.content.Context; import android.content.res.Configuration; import android.content.res.TypedArray; @@ -57,25 +53,23 @@ import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; -import android.widget.FrameLayout; import android.widget.TextClock; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.lifecycle.DefaultLifecycleObserver; +import androidx.lifecycle.LifecycleOwner; -import com.android.launcher3.BubbleTextView; import com.android.launcher3.CellLayout; import com.android.launcher3.DeviceProfile; import com.android.launcher3.Hotseat; import com.android.launcher3.InsettableFrameLayout; import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.LauncherPrefs; -import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.ProxyPrefs; import com.android.launcher3.R; import com.android.launcher3.Workspace; import com.android.launcher3.WorkspaceLayoutManager; -import com.android.launcher3.apppairs.AppPairIcon; import com.android.launcher3.celllayout.CellLayoutLayoutParams; import com.android.launcher3.celllayout.CellPosMapper; import com.android.launcher3.config.FeatureFlags; @@ -87,32 +81,24 @@ import com.android.launcher3.dagger.LauncherComponentProvider; import com.android.launcher3.dagger.PluginManagerWrapperModule; import com.android.launcher3.dagger.StaticObjectModule; import com.android.launcher3.dagger.WindowManagerProxyModule; -import com.android.launcher3.folder.FolderIcon; import com.android.launcher3.model.BaseLauncherBinder.BaseLauncherBinderFactory; import com.android.launcher3.model.BgDataModel; import com.android.launcher3.model.BgDataModel.FixedContainerItems; import com.android.launcher3.model.LayoutParserFactory; import com.android.launcher3.model.LayoutParserFactory.XmlLayoutParserFactory; import com.android.launcher3.model.LoaderTask.LoaderTaskFactory; -import com.android.launcher3.model.data.AppPairInfo; -import com.android.launcher3.model.data.CollectionInfo; -import com.android.launcher3.model.data.FolderInfo; import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.WorkspaceItemInfo; import com.android.launcher3.util.BaseContext; -import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.DisplayController; -import com.android.launcher3.util.IntArray; import com.android.launcher3.util.IntSet; +import com.android.launcher3.util.ItemInflater; import com.android.launcher3.util.SandboxContext; import com.android.launcher3.util.Themes; -import com.android.launcher3.util.WindowBounds; import com.android.launcher3.util.window.WindowManagerProxy; import com.android.launcher3.views.ActivityContext; import com.android.launcher3.views.BaseDragLayer; -import com.android.launcher3.widget.BaseLauncherAppWidgetHostView; -import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; import com.android.launcher3.widget.LauncherWidgetHolder; import com.android.launcher3.widget.LauncherWidgetHolder.WidgetHolderFactory; import com.android.launcher3.widget.LocalColorExtractor; @@ -128,10 +114,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.UUID; -import java.util.stream.Collectors; -import java.util.stream.IntStream; /** * Utility class for generating the preview of Launcher for a given InvariantDeviceProfile. @@ -213,7 +196,6 @@ public class LauncherPreviewRenderer extends BaseContext } private final Handler mUiHandler; - private final Context mContext; private final InvariantDeviceProfile mIdp; private final DeviceProfile mDp; private final DeviceProfile mDpOrig; @@ -222,28 +204,24 @@ public class LauncherPreviewRenderer extends BaseContext private final InsettableFrameLayout mRootView; private final Hotseat mHotseat; private final Map mWorkspaceScreens = new HashMap<>(); - private final AppWidgetHost mAppWidgetHost; - private final SparseIntArray mWallpaperColorResources; + private final ItemInflater mItemInflater; private final SparseArray mLauncherWidgetSpanInfo; public LauncherPreviewRenderer(Context context, InvariantDeviceProfile idp, - int widgetHostId, WallpaperColors wallpaperColorsOverride, @Nullable final SparseArray launcherWidgetSpanInfo) { - this(context, idp, widgetHostId, null, wallpaperColorsOverride, launcherWidgetSpanInfo); + this(context, idp, null, wallpaperColorsOverride, launcherWidgetSpanInfo); } public LauncherPreviewRenderer(Context context, InvariantDeviceProfile idp, - int widgetHostId, SparseIntArray previewColorOverride, WallpaperColors wallpaperColorsOverride, @Nullable final SparseArray launcherWidgetSpanInfo) { super(context, Themes.getActivityThemeRes(context)); mUiHandler = new Handler(Looper.getMainLooper()); - mContext = context; mIdp = idp; mDp = getDeviceProfileForPreview(context).toBuilder(context).setViewScaleProvider( this::getAppWidgetScale).build(); @@ -296,16 +274,17 @@ public class LauncherPreviewRenderer extends BaseContext mWorkspaceScreens.put(Workspace.SECOND_SCREEN_ID, rightPanel); } + SparseIntArray wallpaperColorResources; if (Flags.newCustomizationPickerUi()) { if (previewColorOverride != null) { - mWallpaperColorResources = previewColorOverride; + wallpaperColorResources = previewColorOverride; } else if (wallpaperColorsOverride != null) { - mWallpaperColorResources = LocalColorExtractor.newInstance( + wallpaperColorResources = LocalColorExtractor.newInstance( context).generateColorsOverride(wallpaperColorsOverride); } else { WallpaperColors wallpaperColors = WallpaperManager.getInstance( context).getWallpaperColors(FLAG_SYSTEM); - mWallpaperColorResources = wallpaperColors != null + wallpaperColorResources = wallpaperColors != null ? LocalColorExtractor.newInstance(context).generateColorsOverride( wallpaperColors) : null; @@ -314,18 +293,33 @@ public class LauncherPreviewRenderer extends BaseContext WallpaperColors wallpaperColors = wallpaperColorsOverride != null ? wallpaperColorsOverride : WallpaperManager.getInstance(context).getWallpaperColors(FLAG_SYSTEM); - mWallpaperColorResources = wallpaperColors != null + wallpaperColorResources = wallpaperColors != null ? LocalColorExtractor.newInstance(context).generateColorsOverride( wallpaperColors) : null; } - mAppWidgetHost = new LauncherPreviewAppWidgetHost(context, widgetHostId); - onViewCreated(); - - if (widgetHostId != APPWIDGET_HOST_ID) { - mAppWidgetHost.stopListening(); + LauncherWidgetHolder widgetHolder = LauncherComponentProvider.get(this) + .getWidgetHolderFactory().newInstance(this); + if (wallpaperColorResources != null) { + widgetHolder.setOnViewCreationCallback( + v -> v.setColorResources(wallpaperColorResources)); } + + mItemInflater = new ItemInflater<>( + this, + widgetHolder, + view -> { }, + (view, b) -> { }, + mHotseat + ); + onViewCreated(); + getLifecycle().addObserver(new DefaultLifecycleObserver() { + @Override + public void onDestroy(@NonNull LifecycleOwner owner) { + widgetHolder.destroy(); + } + }); } @Override @@ -352,27 +346,14 @@ public class LauncherPreviewRenderer extends BaseContext * Returns the insets of the screen closest to the display given by the context */ private Rect getInsets(Context context) { - DisplayController.Info info = DisplayController.INSTANCE.get(context).getInfo(); - float maxDiff = Float.MAX_VALUE; Display display = context.getDisplay(); - Rect insets = new Rect(); - for (WindowBounds supportedBound : info.supportedBounds) { - double diff = Math.pow(display.getWidth() - supportedBound.availableSize.x, 2) - + Math.pow(display.getHeight() - supportedBound.availableSize.y, 2); - if (supportedBound.rotationHint == context.getDisplay().getRotation() - && diff < maxDiff) { - maxDiff = (float) diff; - insets = supportedBound.insets; - } - } - return new Rect(insets); - } - - /** Populate preview and render it. */ - public View getRenderedView(BgDataModel dataModel, - Map widgetProviderInfoMap) { - populate(dataModel, widgetProviderInfoMap); - return mRootView; + return DisplayController.INSTANCE.get(context).getInfo().supportedBounds.stream() + .filter(w -> w.rotationHint == display.getRotation()) + .min(comparingDouble(w -> + Math.pow(display.getWidth() - w.availableSize.x, 2) + + Math.pow(display.getHeight() - w.availableSize.y, 2))) + .map(w -> new Rect(w.insets)) + .orElse(new Rect()); } @Override @@ -398,6 +379,7 @@ public class LauncherPreviewRenderer extends BaseContext View view = f.onCreateView(LayoutInflater.from(context), (ViewGroup) parent, null); view.setId(ta.getInt(R.styleable.PreviewFragment_android_id, View.NO_ID)); + ta.recycle(); return view; } @@ -450,60 +432,11 @@ public class LauncherPreviewRenderer extends BaseContext return CellPosMapper.DEFAULT; } - private void inflateAndAddIcon(WorkspaceItemInfo info) { - CellLayout screen = mWorkspaceScreens.get(info.screenId); - BubbleTextView icon = (BubbleTextView) mHomeElementInflater.inflate( - R.layout.app_icon, screen, false); - icon.applyFromWorkspaceItem(info); - addInScreenFromBind(icon, info); - } - - private void inflateAndAddCollectionIcon(CollectionInfo info) { - boolean isOnDesktop = info.container == Favorites.CONTAINER_DESKTOP; - CellLayout screen = isOnDesktop - ? mWorkspaceScreens.get(info.screenId) - : mHotseat; - FrameLayout collectionIcon = info.itemType == Favorites.ITEM_TYPE_FOLDER - ? FolderIcon.inflateIcon(R.layout.folder_icon, this, screen, (FolderInfo) info) - : AppPairIcon.inflateIcon(R.layout.app_pair_icon, this, screen, (AppPairInfo) info, - isOnDesktop ? DISPLAY_WORKSPACE : DISPLAY_TASKBAR); - addInScreenFromBind(collectionIcon, info); - } - - private void inflateAndAddWidgets( - LauncherAppWidgetInfo info, - Map widgetProviderInfoMap) { - if (widgetProviderInfoMap == null) { - return; - } - AppWidgetProviderInfo providerInfo = widgetProviderInfoMap.get( - new ComponentKey(info.providerName, info.user)); - if (providerInfo == null) { - return; - } - inflateAndAddWidgets(info, LauncherAppWidgetProviderInfo.fromProviderInfo( - getApplicationContext(), providerInfo)); - } - - private void inflateAndAddWidgets( - LauncherAppWidgetInfo info, LauncherAppWidgetProviderInfo providerInfo) { - AppWidgetHostView view = mAppWidgetHost.createView( - mContext, info.appWidgetId, providerInfo); - - if (mWallpaperColorResources != null) { - view.setColorResources(mWallpaperColorResources); - } - - view.setTag(info); - addInScreenFromBind(view, info); - } - @NonNull private PointF getAppWidgetScale(@Nullable ItemInfo itemInfo) { - if (!(itemInfo instanceof LauncherAppWidgetInfo)) { + if (!(itemInfo instanceof LauncherAppWidgetInfo info)) { return DEFAULT_SCALE; } - LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) itemInfo; final Size launcherWidgetSize = mLauncherWidgetSpanInfo.get(info.appWidgetId); if (launcherWidgetSize == null) { return DEFAULT_SCALE; @@ -515,14 +448,6 @@ public class LauncherPreviewRenderer extends BaseContext (float) newSize.getHeight() / origSize.getHeight()); } - private void inflateAndAddPredictedIcon(WorkspaceItemInfo info) { - CellLayout screen = mWorkspaceScreens.get(info.screenId); - BubbleTextView icon = (BubbleTextView) mHomeElementInflater.inflate( - R.layout.predicted_app_icon, screen, false); - icon.applyFromWorkspaceItem(info); - addInScreenFromBind(icon, info); - } - private void dispatchVisibilityAggregated(View view, boolean isVisible) { // Similar to View.dispatchVisibilityAggregated implementation. final boolean thisVisible = view.getVisibility() == VISIBLE; @@ -530,9 +455,8 @@ public class LauncherPreviewRenderer extends BaseContext view.onVisibilityAggregated(isVisible); } - if (view instanceof ViewGroup) { + if (view instanceof ViewGroup vg) { isVisible = thisVisible && isVisible; - ViewGroup vg = (ViewGroup) view; int count = vg.getChildCount(); for (int i = 0; i < count; i++) { @@ -541,66 +465,14 @@ public class LauncherPreviewRenderer extends BaseContext } } - private void populate(BgDataModel dataModel, - Map widgetProviderInfoMap) { - IntSet missingHotseatRank = new IntSet(); - IntStream.range(0, mDp.numShownHotseatIcons).forEach(missingHotseatRank::add); - - Map[] widgetsMap = new Map[] { widgetProviderInfoMap}; - + /** Populate preview and render it. */ + public void populate(BgDataModel dataModel) { // Separate the items that are on the current screen, and the other remaining items. dataModel.itemsIdMap.stream() .filter(currentScreenContentFilter(IntSet.wrap(mWorkspaceScreens.keySet()))) - .forEach(itemInfo -> { - switch (itemInfo.itemType) { - case Favorites.ITEM_TYPE_APPLICATION: - case Favorites.ITEM_TYPE_DEEP_SHORTCUT: - inflateAndAddIcon((WorkspaceItemInfo) itemInfo); - break; - case Favorites.ITEM_TYPE_FOLDER: - case Favorites.ITEM_TYPE_APP_PAIR: - inflateAndAddCollectionIcon((CollectionInfo) itemInfo); - break; - case Favorites.ITEM_TYPE_APPWIDGET: - case Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: - if (widgetsMap[0] == null) { - widgetsMap[0] = dataModel.widgetsModel.getWidgetsByComponentKey() - .entrySet() - .stream() - .filter(entry -> entry.getValue().widgetInfo != null) - .collect(Collectors.toMap( - Entry::getKey, - entry -> entry.getValue().widgetInfo - )); - } - inflateAndAddWidgets((LauncherAppWidgetInfo) itemInfo, widgetsMap[0]); - break; - default: - break; - } + .forEach(this::inflateAndAdd); + populateHotseatPredictions(dataModel); - if (itemInfo.container == CONTAINER_HOTSEAT) { - missingHotseatRank.remove(itemInfo.screenId); - } - }); - - IntArray ranks = missingHotseatRank.getArray(); - FixedContainerItems hotseatPredictions = - dataModel.extraItems.get(CONTAINER_HOTSEAT_PREDICTION); - List predictions = hotseatPredictions == null - ? Collections.emptyList() : hotseatPredictions.items; - int count = Math.min(ranks.size(), predictions.size()); - for (int i = 0; i < count; i++) { - int rank = ranks.get(i); - WorkspaceItemInfo itemInfo = - new WorkspaceItemInfo((WorkspaceItemInfo) predictions.get(i)); - itemInfo.container = CONTAINER_HOTSEAT_PREDICTION; - itemInfo.rank = rank; - itemInfo.cellX = mHotseat.getCellXFromOrder(rank); - itemInfo.cellY = mHotseat.getCellYFromOrder(rank); - itemInfo.screenId = rank; - inflateAndAddPredictedIcon(itemInfo); - } // Add first page QSB if (FeatureFlags.QSB_ON_FIRST_SCREEN && dataModel.isFirstPagePinnedItemEnabled @@ -621,37 +493,43 @@ public class LauncherPreviewRenderer extends BaseContext measureView(mRootView, mDp.widthPx, mDp.heightPx); } + private void populateHotseatPredictions(BgDataModel dataModel) { + FixedContainerItems hotseatPredictions = + dataModel.extraItems.get(CONTAINER_HOTSEAT_PREDICTION); + List predictions = hotseatPredictions == null + ? Collections.emptyList() : hotseatPredictions.items; + + int predictionIndex = 0; + for (int rank = 0; rank < mDp.numShownHotseatIcons; rank++) { + if (predictions.size() <= predictionIndex) continue; + + int cellX = mHotseat.getCellXFromOrder(rank); + int cellY = mHotseat.getCellYFromOrder(rank); + if (mHotseat.isOccupied(cellX, cellY)) continue; + + WorkspaceItemInfo itemInfo = + new WorkspaceItemInfo((WorkspaceItemInfo) predictions.get(predictionIndex)); + predictionIndex++; + itemInfo.rank = rank; + itemInfo.cellX = cellX; + itemInfo.cellY = cellY; + itemInfo.screenId = rank; + inflateAndAdd(itemInfo); + } + } + + private void inflateAndAdd(ItemInfo itemInfo) { + View view = mItemInflater.inflateItem(itemInfo); + if (view != null) { + addInScreenFromBind(view, itemInfo); + } + } + private static void measureView(View view, int width, int height) { view.measure(makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY)); view.layout(0, 0, width, height); } - private class LauncherPreviewAppWidgetHost extends AppWidgetHost { - - private LauncherPreviewAppWidgetHost(Context context, int hostId) { - super(context, hostId); - } - - @Override - protected AppWidgetHostView onCreateView( - Context context, - int appWidgetId, - AppWidgetProviderInfo appWidget) { - return new LauncherPreviewAppWidgetHostView(LauncherPreviewRenderer.this); - } - } - - private static class LauncherPreviewAppWidgetHostView extends BaseLauncherAppWidgetHostView { - private LauncherPreviewAppWidgetHostView(Context context) { - super(context); - } - - @Override - protected boolean shouldAllowDirectClick() { - return false; - } - } - /** Root layout for launcher preview that intercepts all touch events. */ public static class LauncherPreviewLayout extends InsettableFrameLayout { public LauncherPreviewLayout(Context context, AttributeSet attrs) { diff --git a/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java b/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java index 93e5f325bf..7010abc5ec 100644 --- a/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java +++ b/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java @@ -32,7 +32,6 @@ import static com.android.launcher3.widget.LauncherWidgetHolder.APPWIDGET_HOST_I import android.app.WallpaperColors; import android.appwidget.AppWidgetHost; -import android.appwidget.AppWidgetProviderInfo; import android.content.Context; import android.content.res.Configuration; import android.database.Cursor; @@ -71,14 +70,11 @@ import com.android.launcher3.model.BgDataModel.Callbacks; import com.android.launcher3.model.LoaderTask; import com.android.launcher3.model.ModelDbController; import com.android.launcher3.model.UserManagerState; -import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.RunnableList; import com.android.launcher3.util.Themes; import com.android.launcher3.widget.LocalColorExtractor; import com.android.systemui.shared.Flags; -import java.util.HashMap; -import java.util.Map; import java.util.concurrent.TimeUnit; /** Render preview using surface view. */ @@ -121,6 +117,8 @@ public class PreviewSurfaceRenderer { private final RunnableList mLifeCycleTracker; private final SurfaceControlViewHost mSurfaceControlViewHost; + private LauncherPreviewRenderer mCurrentRenderer; + public PreviewSurfaceRenderer(Context context, RunnableList lifecycleTracker, Bundle bundle, int callingPid) throws Exception { mContext = context; @@ -171,8 +169,16 @@ public class PreviewSurfaceRenderer { return mSurfaceControlViewHost.getSurfacePackage(); } + private void setCurrentRenderer(LauncherPreviewRenderer renderer) { + if (mCurrentRenderer != null) { + mCurrentRenderer.onViewDestroyed(); + } + mCurrentRenderer = renderer; + } + private void destroy() { mDestroyed = true; + setCurrentRenderer(null); } /** @@ -378,20 +384,17 @@ public class PreviewSurfaceRenderer { String query = deviceProfile.isTwoPanels ? selectionForWorkspaceScreen(FIRST_SCREEN_ID, SECOND_SCREEN_ID) : selectionForWorkspaceScreen(FIRST_SCREEN_ID); - Map widgetProviderInfoMap = new HashMap<>(); - task.loadWorkspaceForPreview(query, widgetProviderInfoMap); + task.loadWorkspaceForPreview(query); final SparseArray spanInfo = getLoadedLauncherWidgetInfo(); MAIN_EXECUTOR.execute(() -> { - renderView(previewContext, appComponent.getDataModel(), widgetHostId, - widgetProviderInfoMap, spanInfo, idp); + renderView(previewContext, appComponent.getDataModel(), spanInfo, idp); mLifeCycleTracker.add(previewContext::onDestroy); }); } else { LauncherAppState.getInstance(inflationContext).getModel().loadAsync(dataModel -> { if (dataModel != null) { MAIN_EXECUTOR.execute(() -> renderView(inflationContext, dataModel, - APPWIDGET_HOST_ID, null, null, - LauncherAppState.getIDP(inflationContext))); + null, LauncherAppState.getIDP(inflationContext))); } else { Log.e(TAG, "Model loading failed"); } @@ -400,22 +403,24 @@ public class PreviewSurfaceRenderer { } @UiThread - private void renderView(Context inflationContext, BgDataModel dataModel, int widgetHostId, - Map widgetProviderInfoMap, + private void renderView(Context inflationContext, BgDataModel dataModel, @Nullable final SparseArray launcherWidgetSpanInfo, InvariantDeviceProfile idp) { if (mDestroyed) { return; } LauncherPreviewRenderer renderer; if (Flags.newCustomizationPickerUi()) { - renderer = new LauncherPreviewRenderer(inflationContext, idp, widgetHostId, + renderer = new LauncherPreviewRenderer(inflationContext, idp, mPreviewColorOverride, mWallpaperColors, launcherWidgetSpanInfo); } else { - renderer = new LauncherPreviewRenderer(inflationContext, idp, widgetHostId, + renderer = new LauncherPreviewRenderer(inflationContext, idp, mWallpaperColors, launcherWidgetSpanInfo); } renderer.hideBottomRow(mHideQsb); - View view = renderer.getRenderedView(dataModel, widgetProviderInfoMap); + renderer.populate(dataModel); + + View view = renderer.getRootView(); + setCurrentRenderer(renderer); view.setPivotX(0); view.setPivotY(0); diff --git a/src/com/android/launcher3/model/LoaderTask.java b/src/com/android/launcher3/model/LoaderTask.java index 6dc20de490..f54c56e83d 100644 --- a/src/com/android/launcher3/model/LoaderTask.java +++ b/src/com/android/launcher3/model/LoaderTask.java @@ -96,7 +96,6 @@ import com.android.launcher3.shortcuts.ShortcutKey; import com.android.launcher3.shortcuts.ShortcutRequest; import com.android.launcher3.shortcuts.ShortcutRequest.QueryResult; import com.android.launcher3.util.ApiWrapper; -import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.IOUtils; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.IntSet; @@ -275,7 +274,7 @@ public class LoaderTask implements Runnable { } try (LauncherModel.LoaderTransaction transaction = mModel.beginLoader(this)) { List allShortcuts = new ArrayList<>(); - loadWorkspace(allShortcuts, "", new HashMap<>(), memoryLogger, restoreEventLogger); + loadWorkspace(allShortcuts, "", memoryLogger, restoreEventLogger); // Sanitize data re-syncs widgets/shortcuts based on the workspace loaded from db. // sanitizeData should not be invoked if the workspace is loaded from a db different @@ -419,22 +418,19 @@ public class LoaderTask implements Runnable { this.notify(); } - public void loadWorkspaceForPreview(String selection, - Map widgetProviderInfoMap) { - loadWorkspace(new ArrayList<>(), selection, widgetProviderInfoMap, null, null); + public void loadWorkspaceForPreview(String selection) { + loadWorkspace(new ArrayList<>(), selection, null, null); } private void loadWorkspace( List allDeepShortcuts, String selection, - Map widgetProviderInfoMap, @Nullable LoaderMemoryLogger memoryLogger, @Nullable LauncherRestoreEventLogger restoreEventLogger ) { Trace.beginSection("LoadWorkspace"); try { - loadWorkspaceImpl(allDeepShortcuts, selection, widgetProviderInfoMap, - memoryLogger, restoreEventLogger); + loadWorkspaceImpl(allDeepShortcuts, selection, memoryLogger, restoreEventLogger); } finally { Trace.endSection(); } @@ -448,7 +444,6 @@ public class LoaderTask implements Runnable { private void loadWorkspaceImpl( List allDeepShortcuts, String selection, - Map widgetProviderInfoMap, @Nullable LoaderMemoryLogger memoryLogger, @Nullable LauncherRestoreEventLogger restoreEventLogger) { final boolean isSdCardReady = Utilities.isBootCompleted(); @@ -498,9 +493,8 @@ public class LoaderTask implements Runnable { mUserCache, mUserManagerState, mLauncherApps, mPendingPackages, mShortcutKeyToPinnedShortcuts, mContext, mIDP, mIconCache, mIsSafeModeEnabled, mBgDataModel, - widgetProviderInfoMap, installingPkgs, isSdCardReady, - widgetInflater, mPmHelper, mWorkspaceIconRequestInfos, unlockedUsers, - allDeepShortcuts); + installingPkgs, isSdCardReady, widgetInflater, mPmHelper, + mWorkspaceIconRequestInfos, unlockedUsers, allDeepShortcuts); if (mStopped) { Log.w(TAG, "loadWorkspaceImpl: Loader stopped, skipping item processing"); diff --git a/src/com/android/launcher3/model/WorkspaceItemProcessor.kt b/src/com/android/launcher3/model/WorkspaceItemProcessor.kt index bf71099f41..235ca9fc56 100644 --- a/src/com/android/launcher3/model/WorkspaceItemProcessor.kt +++ b/src/com/android/launcher3/model/WorkspaceItemProcessor.kt @@ -16,7 +16,6 @@ package com.android.launcher3.model import android.annotation.SuppressLint -import android.appwidget.AppWidgetProviderInfo import android.content.ComponentName import android.content.Context import android.content.Intent @@ -49,7 +48,6 @@ import com.android.launcher3.shortcuts.ShortcutKey import com.android.launcher3.shortcuts.ShortcutRequest import com.android.launcher3.util.ApiWrapper import com.android.launcher3.util.ApplicationInfoWrapper -import com.android.launcher3.util.ComponentKey import com.android.launcher3.util.PackageManagerHelper import com.android.launcher3.util.PackageUserKey import com.android.launcher3.widget.LauncherAppWidgetProviderInfo @@ -76,7 +74,6 @@ class WorkspaceItemProcessor( private val iconCache: IconCache, private val isSafeMode: Boolean, private val bgDataModel: BgDataModel, - private val widgetProvidersMap: MutableMap, private val installingPkgs: HashMap, private val isSdCardReady: Boolean, private val widgetInflater: WidgetInflater, @@ -584,7 +581,6 @@ class WorkspaceItemProcessor( .commit() } if (lapi != null) { - widgetProvidersMap[ComponentKey(lapi.provider, lapi.user)] = inflationResult.widgetInfo if (appWidgetInfo.spanX < lapi.minSpanX || appWidgetInfo.spanY < lapi.minSpanY) { FileLog.d( TAG, diff --git a/src/com/android/launcher3/util/ItemInflater.kt b/src/com/android/launcher3/util/ItemInflater.kt index 647c26c522..b26a490356 100644 --- a/src/com/android/launcher3/util/ItemInflater.kt +++ b/src/com/android/launcher3/util/ItemInflater.kt @@ -61,10 +61,10 @@ class ItemInflater( Favorites.ITEM_TYPE_DEEP_SHORTCUT, Favorites.ITEM_TYPE_SEARCH_ACTION -> { var info = - if (item is WorkspaceItemFactory) { - (item as WorkspaceItemFactory).makeWorkspaceItem(context) - } else { - item as WorkspaceItemInfo + when (item) { + is WorkspaceItemFactory -> item.makeWorkspaceItem(context) + is WorkspaceItemInfo -> item + else -> return null } if (info.container == Favorites.CONTAINER_PREDICTION) { // Came from all apps prediction row -- make a copy @@ -104,12 +104,17 @@ class ItemInflater( * @return A View inflated from layoutResId. */ private fun createShortcut(info: WorkspaceItemInfo, parent: ViewGroup): View { + val layout = + if (info.container == Favorites.CONTAINER_HOTSEAT_PREDICTION) + R.layout.predicted_app_icon + else R.layout.app_icon val favorite = - LayoutInflater.from(parent.context).inflate(R.layout.app_icon, parent, false) - as BubbleTextView + LayoutInflater.from(parent.context).inflate(layout, parent, false) as BubbleTextView favorite.applyFromWorkspaceItem(info) favorite.setOnClickListener(clickListener) favorite.onFocusChangeListener = focusListener + + if (info.container == Favorites.CONTAINER_HOTSEAT_PREDICTION) favorite.verifyHighRes() return favorite } diff --git a/tests/multivalentTests/src/com/android/launcher3/model/WorkspaceItemProcessorTest.kt b/tests/multivalentTests/src/com/android/launcher3/model/WorkspaceItemProcessorTest.kt index 09deb59905..571265879f 100644 --- a/tests/multivalentTests/src/com/android/launcher3/model/WorkspaceItemProcessorTest.kt +++ b/tests/multivalentTests/src/com/android/launcher3/model/WorkspaceItemProcessorTest.kt @@ -16,7 +16,6 @@ package com.android.launcher3.model -import android.appwidget.AppWidgetProviderInfo import android.content.ComponentName import android.content.Intent import android.content.pm.ApplicationInfo @@ -52,7 +51,6 @@ import com.android.launcher3.model.data.WorkspaceItemInfo.FLAG_RESTORED_ICON import com.android.launcher3.model.data.WorkspaceItemInfo.FLAG_RESTORE_STARTED import com.android.launcher3.pm.UserCache import com.android.launcher3.shortcuts.ShortcutKey -import com.android.launcher3.util.ComponentKey import com.android.launcher3.util.ContentWriter import com.android.launcher3.util.PackageManagerHelper import com.android.launcher3.util.PackageUserKey @@ -107,8 +105,6 @@ class WorkspaceItemProcessorTest { private var mKeyToPinnedShortcutsMap: MutableMap = mutableMapOf() private var mInstallingPkgs: HashMap = hashMapOf() private var mAllDeepShortcuts: MutableList = mutableListOf() - private var mWidgetProvidersMap: MutableMap = - mutableMapOf() private var mPendingPackages: MutableSet = mutableSetOf() private lateinit var itemProcessorUnderTest: WorkspaceItemProcessor @@ -153,7 +149,6 @@ class WorkspaceItemProcessorTest { mKeyToPinnedShortcutsMap = mutableMapOf() mInstallingPkgs = hashMapOf() mAllDeepShortcuts = mutableListOf() - mWidgetProvidersMap = mutableMapOf() mIconRequestInfos = mutableListOf() mPendingPackages = mutableSetOf() } @@ -171,7 +166,6 @@ class WorkspaceItemProcessorTest { launcherApps: LauncherApps = mLauncherApps, shortcutKeyToPinnedShortcuts: Map = mKeyToPinnedShortcutsMap, bgDataModel: BgDataModel = mockBgDataModel, - widgetProvidersMap: MutableMap = mWidgetProvidersMap, widgetInflater: WidgetInflater = mockWidgetInflater, pmHelper: PackageManagerHelper = mockPmHelper, iconRequestInfos: MutableList> = mIconRequestInfos, @@ -189,7 +183,6 @@ class WorkspaceItemProcessorTest { launcherApps = launcherApps, context = mContext, bgDataModel = bgDataModel, - widgetProvidersMap = widgetProvidersMap, widgetInflater = widgetInflater, pmHelper = pmHelper, unlockedUsers = unlockedUsers, @@ -647,8 +640,7 @@ class WorkspaceItemProcessorTest { mInstallingPkgs[packageUserKey] = PackageInstaller.SessionInfo() // When - itemProcessorUnderTest = - createWorkspaceItemProcessorUnderTest(widgetProvidersMap = mWidgetProvidersMap) + itemProcessorUnderTest = createWorkspaceItemProcessorUnderTest() itemProcessorUnderTest.processItem() // Then @@ -662,9 +654,6 @@ class WorkspaceItemProcessorTest { assertThat(targetComponent).isEqualTo(expectedWidgetInfo.targetComponent) assertThat(appWidgetId).isEqualTo(expectedWidgetInfo.appWidgetId) } - val expectedComponentKey = - ComponentKey(expectedWidgetProviderInfo.provider, expectedWidgetProviderInfo.user) - assertThat(mWidgetProvidersMap[expectedComponentKey]).isEqualTo(expectedWidgetProviderInfo) } @Test @@ -702,8 +691,7 @@ class WorkspaceItemProcessorTest { mock().apply { whenever(inflateAppWidget(any())).thenReturn(inflationResult) } - itemProcessorUnderTest = - createWorkspaceItemProcessorUnderTest(widgetProvidersMap = mWidgetProvidersMap) + itemProcessorUnderTest = createWorkspaceItemProcessorUnderTest() // When itemProcessorUnderTest.processItem() @@ -743,8 +731,7 @@ class WorkspaceItemProcessorTest { val expectedComponentName = ComponentName.unflattenFromString(expectedProvider) // When - itemProcessorUnderTest = - createWorkspaceItemProcessorUnderTest(widgetProvidersMap = mWidgetProvidersMap) + itemProcessorUnderTest = createWorkspaceItemProcessorUnderTest() itemProcessorUnderTest.processItem() // Then @@ -783,8 +770,7 @@ class WorkspaceItemProcessorTest { mock().apply { whenever(inflateAppWidget(any())).thenReturn(inflationResult) } - itemProcessorUnderTest = - createWorkspaceItemProcessorUnderTest(widgetProvidersMap = mWidgetProvidersMap) + itemProcessorUnderTest = createWorkspaceItemProcessorUnderTest() // When itemProcessorUnderTest.processItem() diff --git a/tests/src/com/android/launcher3/model/WorkspaceItemProcessorExtraTest.kt b/tests/src/com/android/launcher3/model/WorkspaceItemProcessorExtraTest.kt index fd44023de4..9d234190e6 100644 --- a/tests/src/com/android/launcher3/model/WorkspaceItemProcessorExtraTest.kt +++ b/tests/src/com/android/launcher3/model/WorkspaceItemProcessorExtraTest.kt @@ -16,7 +16,6 @@ package com.android.launcher3.model -import android.appwidget.AppWidgetProviderInfo import android.content.ComponentName import android.content.Context import android.content.Intent @@ -44,7 +43,6 @@ import com.android.launcher3.model.data.LauncherAppWidgetInfo.FLAG_UI_NOT_READY import com.android.launcher3.model.data.WorkspaceItemInfo import com.android.launcher3.pm.UserCache import com.android.launcher3.shortcuts.ShortcutKey -import com.android.launcher3.util.ComponentKey import com.android.launcher3.util.PackageManagerHelper import com.android.launcher3.util.PackageUserKey import com.android.launcher3.util.UserIconInfo @@ -89,8 +87,6 @@ class WorkspaceItemProcessorExtraTest { private var mKeyToPinnedShortcutsMap: MutableMap = mutableMapOf() private var mInstallingPkgs: HashMap = hashMapOf() private var mAllDeepShortcuts: MutableList = mutableListOf() - private var mWidgetProvidersMap: MutableMap = - mutableMapOf() private var mPendingPackages: MutableSet = mutableSetOf() private lateinit var itemProcessorUnderTest: WorkspaceItemProcessor @@ -141,7 +137,6 @@ class WorkspaceItemProcessorExtraTest { mKeyToPinnedShortcutsMap = mutableMapOf() mInstallingPkgs = hashMapOf() mAllDeepShortcuts = mutableListOf() - mWidgetProvidersMap = mutableMapOf() mIconRequestInfos = mutableListOf() mPendingPackages = mutableSetOf() } @@ -186,8 +181,7 @@ class WorkspaceItemProcessorExtraTest { mInstallingPkgs[packageUserKey] = PackageInstaller.SessionInfo() // When - itemProcessorUnderTest = - createWorkspaceItemProcessorUnderTest(widgetProvidersMap = mWidgetProvidersMap) + itemProcessorUnderTest = createWorkspaceItemProcessorUnderTest() itemProcessorUnderTest.processItem() // Then @@ -251,8 +245,7 @@ class WorkspaceItemProcessorExtraTest { mock().apply { whenever(inflateAppWidget(any())).thenReturn(inflationResult) } - itemProcessorUnderTest = - createWorkspaceItemProcessorUnderTest(widgetProvidersMap = mWidgetProvidersMap) + itemProcessorUnderTest = createWorkspaceItemProcessorUnderTest() // When itemProcessorUnderTest.processItem() @@ -270,7 +263,6 @@ class WorkspaceItemProcessorExtraTest { shortcutKeyToPinnedShortcuts: Map = mKeyToPinnedShortcutsMap, context: Context = mockContext, bgDataModel: BgDataModel = mockBgDataModel, - widgetProvidersMap: MutableMap = mWidgetProvidersMap, widgetInflater: WidgetInflater = mockWidgetInflater, pmHelper: PackageManagerHelper = mockPmHelper, iconRequestInfos: MutableList> = mIconRequestInfos, @@ -288,7 +280,6 @@ class WorkspaceItemProcessorExtraTest { userManagerState = userManagerState, launcherApps = launcherApps, bgDataModel = bgDataModel, - widgetProvidersMap = widgetProvidersMap, widgetInflater = widgetInflater, pmHelper = pmHelper, unlockedUsers = unlockedUsers,