From 1e4e50149b03f7d396844450aed7601a4ece386a Mon Sep 17 00:00:00 2001 From: Sebastian Franco Date: Sat, 5 Aug 2023 20:47:44 -0700 Subject: [PATCH] Reorder code so we can use different displays on the preview render The existing code almost have the avility to use different display for the preview by providing a different display id, this change just make sure the InvariantDeviceProfile is created after we create a new context with the desired display and we need to use the default display for the SurfaceControlViewHost Test: Manual testing, changing the display id on FixedWidthDisplayRatioFrameLayout Bug: 292152331 Change-Id: I450d94a9a3f1a6a7b0c2b0722a11cf335122de05 --- .../graphics/LauncherPreviewRenderer.java | 33 +++++--- .../graphics/PreviewSurfaceRenderer.java | 75 ++++++++++--------- 2 files changed, 64 insertions(+), 44 deletions(-) diff --git a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java index 68106c4d52..ae44f0a498 100644 --- a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java +++ b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java @@ -45,12 +45,11 @@ import android.util.Size; import android.util.SparseArray; import android.util.SparseIntArray; import android.view.ContextThemeWrapper; +import android.view.Display; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; -import android.view.WindowInsets; -import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.TextClock; @@ -94,6 +93,7 @@ import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.IntSet; import com.android.launcher3.util.MainThreadInitializedObject.SandboxContext; +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; @@ -206,14 +206,7 @@ public class LauncherPreviewRenderer extends ContextWrapper } else { mDpOrig = mDp; } - - WindowInsets currentWindowInsets = context.getSystemService(WindowManager.class) - .getCurrentWindowMetrics().getWindowInsets(); - mInsets = new Rect( - currentWindowInsets.getSystemWindowInsetLeft(), - currentWindowInsets.getSystemWindowInsetTop(), - currentWindowInsets.getSystemWindowInsetRight(), - mDp.isTaskbarPresent ? 0 : currentWindowInsets.getSystemWindowInsetBottom()); + mInsets = getInsets(context); mDp.updateInsets(mInsets); mHomeElementInflater = LayoutInflater.from( @@ -265,6 +258,26 @@ public class LauncherPreviewRenderer extends ContextWrapper mAppWidgetHost = new LauncherPreviewAppWidgetHost(context); } + /** + * 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) { diff --git a/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java b/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java index e89c0c5d11..aebcdd4aae 100644 --- a/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java +++ b/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java @@ -16,6 +16,8 @@ package com.android.launcher3.graphics; +import static android.view.Display.DEFAULT_DISPLAY; + import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; @@ -80,11 +82,12 @@ public class PreviewSurfaceRenderer { private static final String KEY_DISPLAY_ID = "display_id"; private static final String KEY_COLORS = "wallpaper_colors"; - private final Context mContext; - private final InvariantDeviceProfile mIdp; + private Context mContext; private final IBinder mHostToken; private final int mWidth; private final int mHeight; + private String mGridName; + private final Display mDisplay; private final WallpaperColors mWallpaperColors; private final RunnableList mOnDestroyCallbacks = new RunnableList(); @@ -97,15 +100,13 @@ public class PreviewSurfaceRenderer { public PreviewSurfaceRenderer(Context context, Bundle bundle) throws Exception { mContext = context; - - String gridName = bundle.getString("name"); + mGridName = bundle.getString("name"); bundle.remove("name"); - if (gridName == null) { - gridName = InvariantDeviceProfile.getCurrentGridName(context); + if (mGridName == null) { + mGridName = InvariantDeviceProfile.getCurrentGridName(context); } mWallpaperColors = bundle.getParcelable(KEY_COLORS); mHideQsb = bundle.getBoolean(GridCustomizationsProvider.KEY_HIDE_BOTTOM_ROW); - mIdp = new InvariantDeviceProfile(context, gridName); mHostToken = bundle.getBinder(KEY_HOST_TOKEN); mWidth = bundle.getInt(KEY_VIEW_WIDTH); @@ -113,9 +114,9 @@ public class PreviewSurfaceRenderer { mDisplay = context.getSystemService(DisplayManager.class) .getDisplay(bundle.getInt(KEY_DISPLAY_ID)); - mSurfaceControlViewHost = MAIN_EXECUTOR - .submit(() -> new SurfaceControlViewHost(mContext, mDisplay, mHostToken)) - .get(5, TimeUnit.SECONDS); + mSurfaceControlViewHost = MAIN_EXECUTOR.submit(() -> new SurfaceControlViewHost(mContext, + context.getSystemService(DisplayManager.class).getDisplay(DEFAULT_DISPLAY), + mHostToken)).get(5, TimeUnit.SECONDS); mOnDestroyCallbacks.add(mSurfaceControlViewHost::release); } @@ -195,28 +196,33 @@ public class PreviewSurfaceRenderer { } } + /*** + * Generates a new context overriding the theme color and the display size without affecting the + * main application context + */ + private Context getPreviewContext() { + Context context = mContext.createDisplayContext(mDisplay); + if (mWallpaperColors == null) { + return new ContextThemeWrapper(context, + Themes.getActivityThemeRes(context)); + } + if (Utilities.ATLEAST_R) { + context = context.createWindowContext( + LayoutParams.TYPE_APPLICATION_OVERLAY, null); + } + LocalColorExtractor.newInstance(context) + .applyColorsOverride(context, mWallpaperColors); + return new ContextThemeWrapper(context, + Themes.getActivityThemeRes(context, mWallpaperColors.getColorHints())); + } + @WorkerThread private void loadModelData() { - final Context inflationContext; - if (mWallpaperColors != null) { - // Create a themed context, without affecting the main application context - Context context = mContext.createDisplayContext(mDisplay); - if (Utilities.ATLEAST_R) { - context = context.createWindowContext( - LayoutParams.TYPE_APPLICATION_OVERLAY, null); - } - LocalColorExtractor.newInstance(mContext) - .applyColorsOverride(context, mWallpaperColors); - inflationContext = new ContextThemeWrapper(context, - Themes.getActivityThemeRes(context, mWallpaperColors.getColorHints())); - } else { - inflationContext = new ContextThemeWrapper(mContext, - Themes.getActivityThemeRes(mContext)); - } - - if (GridSizeMigrationUtil.needsToMigrate(inflationContext, mIdp)) { + final Context inflationContext = getPreviewContext(); + final InvariantDeviceProfile idp = new InvariantDeviceProfile(inflationContext, mGridName); + if (GridSizeMigrationUtil.needsToMigrate(inflationContext, idp)) { // Start the migration - PreviewContext previewContext = new PreviewContext(inflationContext, mIdp); + PreviewContext previewContext = new PreviewContext(inflationContext, idp); // Copy existing data to preview DB LauncherDbUtils.copyTable(LauncherAppState.getInstance(mContext) .getModel().getModelDbController().getDb(), @@ -239,7 +245,7 @@ public class PreviewSurfaceRenderer { @Override public void run() { - DeviceProfile deviceProfile = mIdp.getDeviceProfile(previewContext); + DeviceProfile deviceProfile = idp.getDeviceProfile(previewContext); String query = LauncherSettings.Favorites.SCREEN + " = " + Workspace.FIRST_SCREEN_ID + " or " + LauncherSettings.Favorites.CONTAINER + " = " @@ -254,7 +260,8 @@ public class PreviewSurfaceRenderer { getLoadedLauncherWidgetInfo(previewContext.getBaseContext()); MAIN_EXECUTOR.execute(() -> { - renderView(previewContext, mBgDataModel, mWidgetProvidersMap, spanInfo); + renderView(previewContext, mBgDataModel, mWidgetProvidersMap, spanInfo, + idp); mOnDestroyCallbacks.add(previewContext::onDestroy); }); } @@ -263,7 +270,7 @@ public class PreviewSurfaceRenderer { LauncherAppState.getInstance(inflationContext).getModel().loadAsync(dataModel -> { if (dataModel != null) { MAIN_EXECUTOR.execute(() -> renderView(inflationContext, dataModel, null, - null)); + null, idp)); } else { Log.e(TAG, "Model loading failed"); } @@ -274,11 +281,11 @@ public class PreviewSurfaceRenderer { @UiThread private void renderView(Context inflationContext, BgDataModel dataModel, Map widgetProviderInfoMap, - @Nullable final SparseArray launcherWidgetSpanInfo) { + @Nullable final SparseArray launcherWidgetSpanInfo, InvariantDeviceProfile idp) { if (mDestroyed) { return; } - mRenderer = new LauncherPreviewRenderer(inflationContext, mIdp, + mRenderer = new LauncherPreviewRenderer(inflationContext, idp, mWallpaperColors, launcherWidgetSpanInfo); mRenderer.hideBottomRow(mHideQsb); View view = mRenderer.getRenderedView(dataModel, widgetProviderInfoMap);