Remove usages of Android's OnSystemUiVisibilityChangeListener (flutter/engine#30931)
This commit is contained in:
@@ -20,6 +20,8 @@ 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;
|
||||
@@ -38,6 +40,9 @@ 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
|
||||
@@ -140,7 +145,6 @@ public class PlatformPlugin {
|
||||
@NonNull PlatformPluginDelegate delegate) {
|
||||
this.activity = activity;
|
||||
this.platformChannel = platformChannel;
|
||||
this.platformChannel.setPlatformMessageHandler(mPlatformMessageHandler);
|
||||
this.platformPluginDelegate = delegate;
|
||||
|
||||
mEnabledOverlays = DEFAULT_SYSTEM_UI;
|
||||
@@ -216,28 +220,56 @@ public class PlatformPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void setSystemChromeChangeListener() {
|
||||
// Set up a listener to notify the framework when the system ui has changed.
|
||||
private void setSystemChromeChangeListenerLegacy() {
|
||||
View decorView = activity.getWindow().getDecorView();
|
||||
decorView.setOnSystemUiVisibilityChangeListener(
|
||||
insetsListenerLegacy =
|
||||
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) {
|
||||
|
||||
@@ -7,6 +7,7 @@ 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;
|
||||
@@ -18,12 +19,16 @@ 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;
|
||||
@@ -35,6 +40,7 @@ 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;
|
||||
@@ -367,6 +373,110 @@ 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() {
|
||||
|
||||
Reference in New Issue
Block a user