From 21ddf3e6e376093747b35bd821798bade9484344 Mon Sep 17 00:00:00 2001 From: jesswrd Date: Wed, 25 Sep 2024 17:04:00 -0400 Subject: [PATCH] Remove usages of WindowManager's getDefaultDisplay (flutter/engine#55002) Removed deprecated usages of WindowManager's `getDefaultDisplay()` and replaced them with DisplayManager's `getDisplay()`. Note: Decided to keep a usage of `getDefaultDisplay()`, which can be found in FlutterView.java because it is expected to be deleted as a part of V1 embedding removal. No changes were made to that file. path to FlutterView.java: shell/platform/android/io/flutter/embedding/android/FlutterView.java Fixes [#99421](https://github.com/flutter/flutter/issues/99421) ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../platform/android/io/flutter/Build.java | 3 + .../embedding/android/FlutterView.java | 24 +++++--- .../embedding/android/FlutterViewTest.java | 57 +++++++++++++++---- .../platform/WindowManagerHandlerTest.java | 5 ++ .../flutter/tools/android_lint/baseline.xml | 17 +++++- 5 files changed, 86 insertions(+), 20 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/Build.java b/engine/src/flutter/shell/platform/android/io/flutter/Build.java index 904bcc392d..09662c6dad 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/Build.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/Build.java @@ -4,10 +4,13 @@ package io.flutter; +import androidx.annotation.VisibleForTesting; + /** A replacement of utilities from android.os.Build. */ public class Build { /** For use in place of the Android Build.VERSION_CODES class. */ public static class API_LEVELS { + @VisibleForTesting public static final int FLUTTER_MIN = 21; public static final int API_21 = 21; public static final int API_22 = 22; public static final int API_23 = 23; diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java index d7c1387da8..4c6d5448ae 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java @@ -14,6 +14,7 @@ import android.content.res.Configuration; import android.database.ContentObserver; import android.graphics.Insets; import android.graphics.Rect; +import android.hardware.display.DisplayManager; import android.os.Build; import android.os.Handler; import android.os.Looper; @@ -21,6 +22,7 @@ import android.provider.Settings; import android.text.format.DateFormat; import android.util.AttributeSet; import android.util.SparseArray; +import android.view.Display; import android.view.DisplayCutout; import android.view.KeyEvent; import android.view.MotionEvent; @@ -31,7 +33,6 @@ import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.ViewStructure; import android.view.WindowInsets; -import android.view.WindowManager; import android.view.accessibility.AccessibilityManager; import android.view.accessibility.AccessibilityNodeProvider; import android.view.autofill.AutofillValue; @@ -597,26 +598,35 @@ public class FlutterView extends FrameLayout // android may decide to place the software navigation bars on the side. When the nav // bar is hidden, the reported insets should be removed to prevent extra useless space // on the sides. - private enum ZeroSides { + @VisibleForTesting + public enum ZeroSides { NONE, LEFT, RIGHT, BOTH } - private ZeroSides calculateShouldZeroSides() { + /** + * This method can be run on APIs 30 and above but its intended use is for 30 and below. + * + * @return some ZeroSides enum + */ + @androidx.annotation.DeprecatedSinceApi(api = API_LEVELS.API_30) + @VisibleForTesting + public ZeroSides calculateShouldZeroSides() { // We get both orientation and rotation because rotation is all 4 // rotations relative to default rotation while orientation is portrait // or landscape. By combining both, we can obtain a more precise measure // of the rotation. Context context = getContext(); int orientation = context.getResources().getConfiguration().orientation; - int rotation = - ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)) - .getDefaultDisplay() - .getRotation(); if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + int rotation = + ((DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE)) + .getDisplay(Display.DEFAULT_DISPLAY) + .getRotation(); + if (rotation == Surface.ROTATION_90) { return ZeroSides.RIGHT; } else if (rotation == Surface.ROTATION_270) { diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java index b4c6e040ca..dc62638fb2 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java @@ -29,6 +29,7 @@ import android.graphics.Insets; import android.graphics.Rect; import android.graphics.Region; import android.hardware.HardwareBuffer; +import android.hardware.display.DisplayManager; import android.media.Image; import android.media.Image.Plane; import android.media.ImageReader; @@ -39,13 +40,13 @@ import android.view.DisplayCutout; import android.view.Surface; import android.view.View; import android.view.WindowInsets; -import android.view.WindowManager; import android.widget.FrameLayout; import androidx.core.util.Consumer; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.window.layout.FoldingFeature; import androidx.window.layout.WindowLayoutInfo; +import io.flutter.Build.API_LEVELS; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterJNI; import io.flutter.embedding.engine.loader.FlutterLoader; @@ -438,6 +439,48 @@ public class FlutterViewTest { validateViewportMetricPadding(viewportMetricsCaptor, 100, 0, 0, 0); } + @Test + @Config(minSdk = API_LEVELS.FLUTTER_MIN, maxSdk = API_LEVELS.API_29, qualifiers = "port") + public void calculateShouldZeroSidesInPortrait() { + FlutterView flutterView = spy(new FlutterView(ctx)); + assertEquals(FlutterView.ZeroSides.NONE, flutterView.calculateShouldZeroSides()); + } + + @Test + @Config(minSdk = API_LEVELS.FLUTTER_MIN, maxSdk = API_LEVELS.API_29, qualifiers = "land") + public void calculateShouldZeroSidesInLandscapeNeutralRotation() { + FlutterView flutterView = spy(new FlutterView(ctx)); + setExpectedDisplayRotation(Surface.ROTATION_0); + assertEquals(FlutterView.ZeroSides.BOTH, flutterView.calculateShouldZeroSides()); + + setExpectedDisplayRotation(Surface.ROTATION_180); + assertEquals(FlutterView.ZeroSides.BOTH, flutterView.calculateShouldZeroSides()); + } + + @Test + @Config(minSdk = API_LEVELS.FLUTTER_MIN, maxSdk = API_LEVELS.API_29, qualifiers = "land") + public void calculateShouldZeroSidesInLandscapeRotation90() { + FlutterView flutterView = spy(new FlutterView(ctx)); + setExpectedDisplayRotation(Surface.ROTATION_90); + assertEquals(FlutterView.ZeroSides.RIGHT, flutterView.calculateShouldZeroSides()); + } + + @Test + @Config(minSdk = API_LEVELS.API_21, maxSdk = API_LEVELS.API_22, qualifiers = "land") + public void calculateShouldZeroSidesInLandscapeRotation270API22() { + FlutterView flutterView = spy(new FlutterView(ctx)); + setExpectedDisplayRotation(Surface.ROTATION_270); + assertEquals(FlutterView.ZeroSides.RIGHT, flutterView.calculateShouldZeroSides()); + } + + @Test + @Config(minSdk = API_LEVELS.API_23, maxSdk = API_LEVELS.API_29, qualifiers = "land") + public void calculateShouldZeroSidesInLandscapeRotation270API23Plus() { + FlutterView flutterView = spy(new FlutterView(ctx)); + setExpectedDisplayRotation(Surface.ROTATION_270); + assertEquals(FlutterView.ZeroSides.LEFT, flutterView.calculateShouldZeroSides()); + } + @SuppressWarnings("deprecation") // getSystemUiVisibility, getWindowSystemUiVisibility required to test pre api 30 behavior. @Test @@ -615,9 +658,6 @@ public class FlutterViewTest { public void itRegistersAndUnregistersToWindowManager() { Context context = Robolectric.setupActivity(Activity.class); FlutterView flutterView = spy(new FlutterView(context)); - ShadowDisplay display = - Shadows.shadowOf( - ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()); WindowInfoRepositoryCallbackAdapterWrapper windowInfoRepo = mock(WindowInfoRepositoryCallbackAdapterWrapper.class); // For reasoning behing using doReturn instead of when, read "Important gotcha" at @@ -646,9 +686,6 @@ public class FlutterViewTest { public void itSendsHingeDisplayFeatureToFlutter() { Context context = Robolectric.setupActivity(Activity.class); FlutterView flutterView = spy(new FlutterView(context)); - ShadowDisplay display = - Shadows.shadowOf( - ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()); when(flutterView.getContext()).thenReturn(context); WindowInfoRepositoryCallbackAdapterWrapper windowInfoRepo = mock(WindowInfoRepositoryCallbackAdapterWrapper.class); @@ -1102,10 +1139,10 @@ public class FlutterViewTest { @SuppressWarnings("deprecation") private void setExpectedDisplayRotation(int rotation) { - ShadowDisplay display = + ShadowDisplay myDisplay = Shadows.shadowOf( - ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()); - display.setRotation(rotation); + ((DisplayManager) ctx.getSystemService(Context.DISPLAY_SERVICE)).getDisplay(0)); + myDisplay.setRotation(rotation); } private void validateViewportMetricPadding( diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/WindowManagerHandlerTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/WindowManagerHandlerTest.java index 7e37231774..230172d9e6 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/WindowManagerHandlerTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/WindowManagerHandlerTest.java @@ -109,6 +109,11 @@ public class WindowManagerHandlerTest { @SuppressWarnings("Unchecked cast") Consumer mockListener = (Consumer) mock(Consumer.class); + // Windowmanager's getDefaultDisplay() function is deprecated in API 30 level 30. + // See Android docs here: + // https://developer.android.com/reference/android/view/WindowManager#getDefaultDisplay() + // We expect this behavior because this unit test expects a blind forward that includes + // deprecated function calls. See comment above for more details. windowManagerHandler.getDefaultDisplay(); verify(mockWindowManager).getDefaultDisplay(); diff --git a/engine/src/flutter/tools/android_lint/baseline.xml b/engine/src/flutter/tools/android_lint/baseline.xml index a73601b794..c3932724a8 100644 --- a/engine/src/flutter/tools/android_lint/baseline.xml +++ b/engine/src/flutter/tools/android_lint/baseline.xml @@ -1,5 +1,16 @@ - + + + + + @@ -30,7 +41,7 @@ errorLine2=" ~~~~~~~~~~~~">