From 4a76315805812fe4c74e64c900c4d4458935904e Mon Sep 17 00:00:00 2001 From: Brian Isganitis Date: Mon, 21 Jun 2021 16:07:09 -0400 Subject: [PATCH] Use wallpaper colors for widgets in wallpaper change preview Test: Widgets should adapt to new wallpaper colors in launcher preview. Bug: 191499358 Change-Id: I3fb76b6036cb909771b789eac15742df78c2c742 --- lint-baseline-launcher3.xml | 11 ++++++++ .../graphics/LauncherPreviewRenderer.java | 28 +++++++++++++++---- .../graphics/PreviewSurfaceRenderer.java | 2 +- .../launcher3/widget/LocalColorExtractor.java | 8 ++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lint-baseline-launcher3.xml b/lint-baseline-launcher3.xml index 469ad942d9..e77c88926d 100644 --- a/lint-baseline-launcher3.xml +++ b/lint-baseline-launcher3.xml @@ -573,4 +573,15 @@ column="42"/> + + + + diff --git a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java index a27d5c8e06..cfb38e5d6e 100644 --- a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java +++ b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java @@ -26,6 +26,7 @@ import static com.android.launcher3.model.ModelUtils.sortWorkspaceItemsSpatially import android.annotation.TargetApi; import android.app.Fragment; +import android.app.WallpaperColors; import android.appwidget.AppWidgetHost; import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetProviderInfo; @@ -42,6 +43,7 @@ import android.os.Handler; import android.os.Looper; import android.os.Process; import android.util.AttributeSet; +import android.util.SparseIntArray; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; @@ -87,6 +89,7 @@ import com.android.launcher3.views.BaseDragLayer; import com.android.launcher3.widget.BaseLauncherAppWidgetHostView; import com.android.launcher3.widget.LauncherAppWidgetHost; import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; +import com.android.launcher3.widget.LocalColorExtractor; import com.android.launcher3.widget.NavigableAppWidgetHostView; import com.android.launcher3.widget.custom.CustomWidgetManager; @@ -206,8 +209,12 @@ public class LauncherPreviewRenderer extends ContextWrapper private final Hotseat mHotseat; private final CellLayout mWorkspace; private final AppWidgetHost mAppWidgetHost; + private final SparseIntArray mWallpaperColorResources; + + public LauncherPreviewRenderer(Context context, + InvariantDeviceProfile idp, + WallpaperColors wallpaperColors) { - public LauncherPreviewRenderer(Context context, InvariantDeviceProfile idp) { super(context); mUiHandler = new Handler(Looper.getMainLooper()); mContext = context; @@ -260,9 +267,16 @@ public class LauncherPreviewRenderer extends ContextWrapper mDp.workspacePadding.right + mDp.cellLayoutPaddingLeftRightPx, mDp.workspacePadding.bottom); - mAppWidgetHost = FeatureFlags.WIDGETS_IN_LAUNCHER_PREVIEW.get() - ? new LauncherPreviewAppWidgetHost(context) - : null; + if (FeatureFlags.WIDGETS_IN_LAUNCHER_PREVIEW.get()) { + mAppWidgetHost = new LauncherPreviewAppWidgetHost(context); + mWallpaperColorResources = wallpaperColors != null + ? LocalColorExtractor.newInstance(context) + .generateColorsOverride(wallpaperColors) + : null; + } else { + mAppWidgetHost = null; + mWallpaperColorResources = null; + } } /** Populate preview and render it. */ @@ -507,10 +521,12 @@ public class LauncherPreviewRenderer extends ContextWrapper } } - private static class LauncherPreviewAppWidgetHostView extends BaseLauncherAppWidgetHostView { - + private class LauncherPreviewAppWidgetHostView extends BaseLauncherAppWidgetHostView { private LauncherPreviewAppWidgetHostView(Context context) { super(context); + if (Utilities.ATLEAST_S && mWallpaperColorResources != null) { + setColorResources(mWallpaperColorResources); + } } @Override diff --git a/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java b/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java index a8c3d15bde..df493599ed 100644 --- a/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java +++ b/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java @@ -209,7 +209,7 @@ public class PreviewSurfaceRenderer { if (mDestroyed) { return; } - View view = new LauncherPreviewRenderer(inflationContext, mIdp) + View view = new LauncherPreviewRenderer(inflationContext, mIdp, mWallpaperColors) .getRenderedView(dataModel, widgetProviderInfoMap); // This aspect scales the view to fit in the surface and centers it final float scale = Math.min(mWidth / (float) view.getMeasuredWidth(), diff --git a/src/com/android/launcher3/widget/LocalColorExtractor.java b/src/com/android/launcher3/widget/LocalColorExtractor.java index 8ae6b2e435..23d9e151f7 100644 --- a/src/com/android/launcher3/widget/LocalColorExtractor.java +++ b/src/com/android/launcher3/widget/LocalColorExtractor.java @@ -75,6 +75,14 @@ public class LocalColorExtractor implements ResourceBasedOverride { */ public void applyColorsOverride(Context base, WallpaperColors colors) { } + /** + * Generates color resource overrides from {@link WallpaperColors}. + */ + @Nullable + public SparseIntArray generateColorsOverride(WallpaperColors colors) { + return null; + } + /** * Takes a view and returns its rect that can be used by the wallpaper local color extractor. *