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].

<!-- Links -->
[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
This commit is contained in:
jesswrd
2024-09-25 17:04:00 -04:00
committed by GitHub
parent ad894bd2f0
commit 21ddf3e6e3
5 changed files with 86 additions and 20 deletions

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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(

View File

@@ -109,6 +109,11 @@ public class WindowManagerHandlerTest {
@SuppressWarnings("Unchecked cast")
Consumer<Boolean> mockListener = (Consumer<Boolean>) 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();

View File

@@ -1,5 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<issues format="6" by="lint 8.1.0 [10406996] " type="baseline" client="" dependencies="true" name="" variant="all" version="8.1.0 [10406996] ">
<issues format="6" by="lint 8.3.0 [11479570] " type="baseline" client="" dependencies="true" name="" variant="all" version="8.3.0 [11479570] ">
<issue
id="DeprecatedSinceApi"
message="This method is deprecated as of API level 30"
errorLine1=" zeroSides = calculateShouldZeroSides();"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java"
line="759"
column="21"/>
</issue>
<issue
id="HardcodedDebugMode"
@@ -19,7 +30,7 @@
errorLine2=" ~~~~~~~~~~~~">
<location
file="../../../flutter/shell/platform/android/io/flutter/view/FlutterView.java"
line="445"
line="474"
column="18"/>
</issue>
@@ -30,7 +41,7 @@
errorLine2=" ~~~~~~~~~~~~">
<location
file="../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java"
line="928"
line="896"
column="18"/>
</issue>