From ccb71776c64c48f87158f851a0a4b4dda563e630 Mon Sep 17 00:00:00 2001 From: Chihhang Chuang Date: Thu, 20 May 2021 01:50:03 +0800 Subject: [PATCH] Apply the current system window insets to launcher preview For wallpaper picker, the position of app icon on the wallpaper preview would match to the launcher after applying the wallpaper. Wallpaper picker: https://screenshot.googleplex.com/JZazpgZLgzacZMZ.png Launcher: https://screenshot.googleplex.com/9uTTQwdmcnFZ7Vi.png Gesture mode: https://screenshot.googleplex.com/3YaKbBerYgtHBFb.png 3-button mode: https://screenshot.googleplex.com/7sA5nE3GmbuKf62.png Fixes: 188539072 Test: Manually Change-Id: I553000e6ae646207eb94fa33e97d00a3990043c8 --- .../graphics/LauncherPreviewRenderer.java | 20 +++++++++++++++---- .../graphics/PreviewSurfaceRenderer.java | 6 ++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java index 5f014db1ea..2a1aec84a3 100644 --- a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java +++ b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java @@ -45,6 +45,8 @@ import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.WindowInsets; +import android.view.WindowManager; import android.widget.TextClock; import com.android.launcher3.BubbleTextView; @@ -56,6 +58,7 @@ import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.R; +import com.android.launcher3.Utilities; import com.android.launcher3.WorkspaceLayoutManager; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.folder.FolderIcon; @@ -205,10 +208,19 @@ public class LauncherPreviewRenderer extends ContextWrapper mIdp = idp; mDp = idp.getDeviceProfile(context).copy(context); - // TODO: get correct insets once display cutout API is available. - mInsets = new Rect(); - mInsets.left = mInsets.right = (mDp.widthPx - mDp.availableWidthPx) / 2; - mInsets.top = mInsets.bottom = (mDp.heightPx - mDp.availableHeightPx) / 2; + if (Utilities.ATLEAST_R) { + WindowInsets currentWindowInsets = context.getSystemService(WindowManager.class) + .getCurrentWindowMetrics().getWindowInsets(); + mInsets = new Rect( + currentWindowInsets.getSystemWindowInsetLeft(), + currentWindowInsets.getSystemWindowInsetTop(), + currentWindowInsets.getSystemWindowInsetRight(), + currentWindowInsets.getSystemWindowInsetBottom()); + } else { + mInsets = new Rect(); + mInsets.left = mInsets.right = (mDp.widthPx - mDp.availableWidthPx) / 2; + mInsets.top = mInsets.bottom = (mDp.heightPx - mDp.availableHeightPx) / 2; + } mDp.updateInsets(mInsets); BaseIconFactory iconFactory = diff --git a/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java b/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java index 8c39eaeddb..a8c3d15bde 100644 --- a/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java +++ b/src/com/android/launcher3/graphics/PreviewSurfaceRenderer.java @@ -32,6 +32,7 @@ import android.view.Display; import android.view.SurfaceControlViewHost; import android.view.SurfaceControlViewHost.SurfacePackage; import android.view.View; +import android.view.WindowManager.LayoutParams; import android.view.animation.AccelerateDecelerateInterpolator; import androidx.annotation.UiThread; @@ -41,6 +42,7 @@ import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherSettings; import com.android.launcher3.R; +import com.android.launcher3.Utilities; import com.android.launcher3.graphics.LauncherPreviewRenderer.PreviewContext; import com.android.launcher3.model.BgDataModel; import com.android.launcher3.model.GridSizeMigrationTask; @@ -139,6 +141,10 @@ public class PreviewSurfaceRenderer { 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,