Revert "Remove usages of Android's OnSystemUiVisibilityChangeListener (#30931)" (flutter/engine#32217)

This reverts commit b077eda4ca.
This commit is contained in:
Camille Simon
2022-03-23 10:47:15 -07:00
committed by GitHub
parent 3088a62e29
commit 5b4339f41a
2 changed files with 13 additions and 155 deletions

View File

@@ -20,8 +20,6 @@ import androidx.activity.OnBackPressedDispatcherOwner;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import io.flutter.Log;
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
@@ -40,9 +38,6 @@ public class PlatformPlugin {
private int mEnabledOverlays;
private static final String TAG = "PlatformPlugin";
@VisibleForTesting View.OnSystemUiVisibilityChangeListener insetsListenerLegacy;
@VisibleForTesting androidx.core.view.OnApplyWindowInsetsListener insetsListener;
/**
* The {@link PlatformPlugin} generally has default behaviors implemented for platform
* functionalities requested by the Flutter framework. However, functionalities exposed through
@@ -145,6 +140,7 @@ public class PlatformPlugin {
@NonNull PlatformPluginDelegate delegate) {
this.activity = activity;
this.platformChannel = platformChannel;
this.platformChannel.setPlatformMessageHandler(mPlatformMessageHandler);
this.platformPluginDelegate = delegate;
mEnabledOverlays = DEFAULT_SYSTEM_UI;
@@ -220,56 +216,28 @@ public class PlatformPlugin {
}
}
private void setSystemChromeChangeListenerLegacy() {
private void setSystemChromeChangeListener() {
// Set up a listener to notify the framework when the system ui has changed.
View decorView = activity.getWindow().getDecorView();
insetsListenerLegacy =
decorView.setOnSystemUiVisibilityChangeListener(
new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// The system bars are visible. Make any desired adjustments to
// your UI, such as showing the action bar or other navigational
// controls. Another common action is to set a timer to dismiss
// the system bars and restore the fullscreen mode that was
// previously enabled.
platformChannel.systemChromeChanged(false);
} else {
// The system bars are NOT visible. Make any desired adjustments
// to your UI, such as hiding the action bar or other
// navigational controls.
platformChannel.systemChromeChanged(true);
}
}
};
decorView.setOnSystemUiVisibilityChangeListener(insetsListenerLegacy);
}
private void setSystemChromeChangeListener() {
// Set up a listener to notify the framework when the system ui has changed.
// The Android API for overlays/insets as of API 30 provides backwards compatibility
// up through API 20, so legacy code with equivalent behavior is still used for API 19.
if (Build.VERSION.SDK_INT <= 19) {
setSystemChromeChangeListenerLegacy();
} else {
View decorView = activity.getWindow().getDecorView();
insetsListener =
new androidx.core.view.OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(
View view, WindowInsetsCompat windowInsets) {
System.out.println(windowInsets.getInsets(WindowInsetsCompat.Type.statusBars()));
if (windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())
|| windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())) {
// The system bars are visible. Make any desired adjustments to
// your UI, such as showing the action bar or other navigational
// controls. Another common action is to set a timer to dismiss
// the system bars and restore the fullscreen mode that was
// previously enabled.
platformChannel.systemChromeChanged(false);
} else {
// The system bars are NOT visible. Make any desired adjustments
// to your UI, such as hiding the action bar or other
// navigational controls.
platformChannel.systemChromeChanged(true);
}
return ViewCompat.onApplyWindowInsets(view, windowInsets);
}
};
ViewCompat.setOnApplyWindowInsetsListener(decorView, insetsListener);
}
});
}
private void setSystemChromeEnabledSystemUIMode(PlatformChannel.SystemUiMode systemUiMode) {

View File

@@ -7,7 +7,6 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
@@ -19,16 +18,12 @@ import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Insets;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import androidx.activity.OnBackPressedCallback;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
@@ -40,7 +35,6 @@ import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@@ -373,110 +367,6 @@ public class PlatformPluginTest {
}
}
@Config(sdk = 19)
@Test
public void verifySystemChromeChangeListenerLegacyWithSystemBarsInvisible() {
View fakeDecorView = mock(View.class);
Activity fakeActivity = mock(Activity.class);
Window fakeWindow = mock(Window.class);
when(fakeActivity.getWindow()).thenReturn(fakeWindow);
when(fakeWindow.getDecorView()).thenReturn(fakeDecorView);
PlatformChannel fakePlatformChannel = mock(PlatformChannel.class);
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);
platformPlugin.mPlatformMessageHandler.setSystemUiChangeListener();
platformPlugin.insetsListenerLegacy.onSystemUiVisibilityChange(View.SYSTEM_UI_FLAG_FULLSCREEN);
verify(fakePlatformChannel).systemChromeChanged(true);
}
@Config(sdk = 19)
@Test
public void verifySystemChromeChangeListenerLegacyWithSystemBarsVisible() {
View fakeDecorView = mock(View.class);
Activity fakeActivity = mock(Activity.class);
Window fakeWindow = mock(Window.class);
when(fakeActivity.getWindow()).thenReturn(fakeWindow);
when(fakeWindow.getDecorView()).thenReturn(fakeDecorView);
PlatformChannel fakePlatformChannel = mock(PlatformChannel.class);
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);
platformPlugin.mPlatformMessageHandler.setSystemUiChangeListener();
platformPlugin.insetsListenerLegacy.onSystemUiVisibilityChange(~View.SYSTEM_UI_FLAG_FULLSCREEN);
verify(fakePlatformChannel).systemChromeChanged(false);
}
@Config(sdk = 30)
@Test
public void verifySystemChromeChangeListenerWithSystemBarsInvisible() {
View fakeDecorView = mock(View.class);
Activity fakeActivity = mock(Activity.class);
Window fakeWindow = mock(Window.class);
when(fakeActivity.getWindow()).thenReturn(fakeWindow);
when(fakeWindow.getDecorView()).thenReturn(fakeDecorView);
PlatformChannel fakePlatformChannel = mock(PlatformChannel.class);
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);
platformPlugin.mPlatformMessageHandler.setSystemUiChangeListener();
WindowInsets.Builder builder = new WindowInsets.Builder();
builder.setInsets(WindowInsetsCompat.Type.systemBars(), Insets.of(0, 0, 0, 0));
builder.setVisible(WindowInsetsCompat.Type.systemBars(), false);
WindowInsets systemBarsInvisibleInsets = builder.build();
try (MockedStatic<ViewCompat> mocked = mockStatic(ViewCompat.class)) {
mocked
.when(
() ->
ViewCompat.onApplyWindowInsets(
fakeDecorView,
WindowInsetsCompat.toWindowInsetsCompat(systemBarsInvisibleInsets)))
.thenReturn(null);
platformPlugin.insetsListener.onApplyWindowInsets(
fakeDecorView, WindowInsetsCompat.toWindowInsetsCompat(systemBarsInvisibleInsets));
verify(fakePlatformChannel).systemChromeChanged(true);
}
}
@Config(sdk = 30)
@Test
public void verifySystemChromeChangeListenerWithSystemBarsVisible() {
View fakeDecorView = mock(View.class);
Activity fakeActivity = mock(Activity.class);
Window fakeWindow = mock(Window.class);
when(fakeActivity.getWindow()).thenReturn(fakeWindow);
when(fakeWindow.getDecorView()).thenReturn(fakeDecorView);
PlatformChannel fakePlatformChannel = mock(PlatformChannel.class);
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);
platformPlugin.mPlatformMessageHandler.setSystemUiChangeListener();
WindowInsets.Builder builder = new WindowInsets.Builder();
builder.setInsets(WindowInsetsCompat.Type.systemBars(), Insets.of(0, 0, 0, 0));
builder.setVisible(WindowInsetsCompat.Type.systemBars(), true);
WindowInsets systemBarsVisibleInsets = builder.build();
try (MockedStatic<ViewCompat> mocked = mockStatic(ViewCompat.class)) {
mocked
.when(
() ->
ViewCompat.onApplyWindowInsets(
fakeDecorView,
WindowInsetsCompat.toWindowInsetsCompat(systemBarsVisibleInsets)))
.thenReturn(null);
platformPlugin.insetsListener.onApplyWindowInsets(
fakeDecorView, WindowInsetsCompat.toWindowInsetsCompat(systemBarsVisibleInsets));
verify(fakePlatformChannel).systemChromeChanged(false);
}
}
@Config(sdk = 28)
@Test
public void doNotEnableEdgeToEdgeOnOlderSdk() {