Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8db5038

Browse files
authored
Remove usages of deprecated setSystemUiVisibility() (#29493)
1 parent 7e50462 commit 8db5038

File tree

5 files changed

+218
-83
lines changed

5 files changed

+218
-83
lines changed

shell/platform/android/io/flutter/app/FlutterActivityDelegate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import android.view.ViewGroup;
2525
import android.view.Window;
2626
import android.view.WindowManager.LayoutParams;
27+
import androidx.core.view.WindowCompat;
2728
import io.flutter.Log;
2829
import io.flutter.plugin.common.PluginRegistry;
29-
import io.flutter.plugin.platform.PlatformPlugin;
3030
import io.flutter.util.Preconditions;
3131
import io.flutter.view.FlutterMain;
3232
import io.flutter.view.FlutterNativeView;
@@ -141,7 +141,7 @@ public void onCreate(Bundle savedInstanceState) {
141141
Window window = activity.getWindow();
142142
window.addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
143143
window.setStatusBarColor(0x40000000);
144-
window.getDecorView().setSystemUiVisibility(PlatformPlugin.DEFAULT_SYSTEM_UI);
144+
WindowCompat.setDecorFitsSystemWindows(window, false);
145145
}
146146

147147
String[] args = getArgsFromIntent(activity.getIntent());

shell/platform/android/io/flutter/embedding/android/FlutterActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import androidx.annotation.Nullable;
3939
import androidx.annotation.VisibleForTesting;
4040
import androidx.core.content.res.ResourcesCompat;
41+
import androidx.core.view.WindowCompat;
4142
import androidx.lifecycle.Lifecycle;
4243
import androidx.lifecycle.LifecycleOwner;
4344
import androidx.lifecycle.LifecycleRegistry;
@@ -581,7 +582,7 @@ private void configureStatusBarForFullscreenFlutterExperience() {
581582
Window window = getWindow();
582583
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
583584
window.setStatusBarColor(0x40000000);
584-
window.getDecorView().setSystemUiVisibility(PlatformPlugin.DEFAULT_SYSTEM_UI);
585+
WindowCompat.setDecorFitsSystemWindows(window, false);
585586
}
586587
}
587588

shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
import androidx.annotation.Nullable;
3939
import androidx.annotation.VisibleForTesting;
4040
import androidx.core.content.res.ResourcesCompat;
41+
import androidx.core.view.WindowCompat;
4142
import androidx.fragment.app.FragmentActivity;
4243
import androidx.fragment.app.FragmentManager;
4344
import io.flutter.Log;
4445
import io.flutter.embedding.android.FlutterActivityLaunchConfigs.BackgroundMode;
4546
import io.flutter.embedding.engine.FlutterEngine;
4647
import io.flutter.embedding.engine.FlutterShellArgs;
4748
import io.flutter.embedding.engine.plugins.util.GeneratedPluginRegister;
48-
import io.flutter.plugin.platform.PlatformPlugin;
4949

5050
/**
5151
* A Flutter {@code Activity} that is based upon {@link FragmentActivity}.
@@ -493,7 +493,7 @@ private void configureStatusBarForFullscreenFlutterExperience() {
493493
Window window = getWindow();
494494
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
495495
window.setStatusBarColor(0x40000000);
496-
window.getDecorView().setSystemUiVisibility(PlatformPlugin.DEFAULT_SYSTEM_UI);
496+
WindowCompat.setDecorFitsSystemWindows(window, false);
497497
}
498498
}
499499

shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import androidx.annotation.NonNull;
2121
import androidx.annotation.Nullable;
2222
import androidx.annotation.VisibleForTesting;
23+
import androidx.core.view.WindowCompat;
24+
import androidx.core.view.WindowInsetsCompat;
2325
import androidx.core.view.WindowInsetsControllerCompat;
2426
import io.flutter.Log;
2527
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
@@ -28,14 +30,13 @@
2830

2931
/** Android implementation of the platform plugin. */
3032
public class PlatformPlugin {
31-
public static final int DEFAULT_SYSTEM_UI =
32-
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
3333

3434
private final Activity activity;
3535
private final PlatformChannel platformChannel;
3636
private final PlatformPluginDelegate platformPluginDelegate;
3737
private PlatformChannel.SystemChromeStyle currentTheme;
38-
private int mEnabledOverlays;
38+
private PlatformChannel.SystemUiMode currentSystemUiMode;
39+
private List<PlatformChannel.SystemUiOverlay> currentOverlays;
3940
private static final String TAG = "PlatformPlugin";
4041

4142
/**
@@ -141,7 +142,7 @@ public PlatformPlugin(
141142
this.platformChannel.setPlatformMessageHandler(mPlatformMessageHandler);
142143
this.platformPluginDelegate = delegate;
143144

144-
mEnabledOverlays = DEFAULT_SYSTEM_UI;
145+
currentSystemUiMode = PlatformChannel.SystemUiMode.EDGE_TO_EDGE;
145146
}
146147

147148
/**
@@ -240,105 +241,98 @@ public void onSystemUiVisibilityChange(int visibility) {
240241
private void setSystemChromeEnabledSystemUIMode(PlatformChannel.SystemUiMode systemUiMode) {
241242
int enabledOverlays;
242243

243-
if (systemUiMode == PlatformChannel.SystemUiMode.LEAN_BACK
244-
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
244+
Window window = activity.getWindow();
245+
View view = window.getDecorView();
246+
WindowInsetsControllerCompat windowInsetsControllerCompat =
247+
new WindowInsetsControllerCompat(window, view);
248+
249+
if (systemUiMode == PlatformChannel.SystemUiMode.LEAN_BACK) {
245250
// LEAN BACK
246-
// Available starting at SDK 16
251+
// Available starting at SDK 20, due to the backwards compatibility provided by the
252+
// WindowInsetsControllerCompat class for setting the behavior of system bars.
247253
// Should not show overlays, tap to reveal overlays, needs onChange callback
248254
// When the overlays come in on tap, the app does not receive the gesture and does not know
249255
// the system overlay has changed. The overlays cannot be dismissed, so adding the callback
250256
// support will allow users to restore the system ui and dismiss the overlays.
251257
// Not compatible with top/bottom overlays enabled.
252-
enabledOverlays =
253-
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
254-
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
255-
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
256-
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
257-
| View.SYSTEM_UI_FLAG_FULLSCREEN;
258-
} else if (systemUiMode == PlatformChannel.SystemUiMode.IMMERSIVE
259-
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
258+
windowInsetsControllerCompat.setSystemBarsBehavior(
259+
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH);
260+
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.systemBars());
261+
WindowCompat.setDecorFitsSystemWindows(window, false);
262+
} else if (systemUiMode == PlatformChannel.SystemUiMode.IMMERSIVE) {
260263
// IMMERSIVE
261-
// Available starting at 19
264+
// Available starting at SDK 20, due to the backwards compatibility provided by the
265+
// WindowInsetsControllerCompat class for setting the behavior of system bars.
262266
// Should not show overlays, swipe from edges to reveal overlays, needs onChange callback
263267
// When the overlays come in on swipe, the app does not receive the gesture and does not know
264268
// the system overlay has changed. The overlays cannot be dismissed, so adding callback
265269
// support will allow users to restore the system ui and dismiss the overlays.
266270
// Not compatible with top/bottom overlays enabled.
267-
enabledOverlays =
268-
View.SYSTEM_UI_FLAG_IMMERSIVE
269-
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
270-
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
271-
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
272-
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
273-
| View.SYSTEM_UI_FLAG_FULLSCREEN;
274-
} else if (systemUiMode == PlatformChannel.SystemUiMode.IMMERSIVE_STICKY
275-
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
271+
windowInsetsControllerCompat.setSystemBarsBehavior(
272+
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE);
273+
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.systemBars());
274+
WindowCompat.setDecorFitsSystemWindows(window, false);
275+
} else if (systemUiMode == PlatformChannel.SystemUiMode.IMMERSIVE_STICKY) {
276276
// STICKY IMMERSIVE
277-
// Available starting at 19
277+
// Available starting at SDK 20, due to the backwards compatibility provided by the
278+
// WindowInsetsControllerCompat class for setting the behavior of system bars.
278279
// Should not show overlays, swipe from edges to reveal overlays. The app will also receive
279280
// the swipe gesture. The overlays cannot be dismissed, so adding callback support will
280281
// allow users to restore the system ui and dismiss the overlays.
281282
// Not compatible with top/bottom overlays enabled.
282-
enabledOverlays =
283-
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
284-
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
285-
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
286-
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
287-
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
288-
| View.SYSTEM_UI_FLAG_FULLSCREEN;
283+
windowInsetsControllerCompat.setSystemBarsBehavior(
284+
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
285+
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.systemBars());
286+
WindowCompat.setDecorFitsSystemWindows(window, false);
289287
} else if (systemUiMode == PlatformChannel.SystemUiMode.EDGE_TO_EDGE
290288
&& Build.VERSION.SDK_INT >= 29) {
291289
// EDGE TO EDGE
292-
// Available starting at 29
293-
// SDK 29 and up will apply a translucent body scrim behind 2/3 button navigation bars
290+
// Available starting at SDK 29. See issue for context:
291+
// https://github.com/flutter/flutter/issues/89774.
292+
// Will apply a translucent body scrim behind 2/3 button navigation bars
294293
// to ensure contrast with buttons on the nav and status bars, unless the contrast is not
295294
// enforced in the overlay styling.
296-
enabledOverlays =
297-
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
298-
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
299-
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
295+
WindowCompat.setDecorFitsSystemWindows(window, false);
300296
} else {
301297
// When none of the conditions are matched, return without updating the system UI overlays.
302298
return;
303299
}
304-
305-
mEnabledOverlays = enabledOverlays;
306-
updateSystemUiOverlays();
300+
currentSystemUiMode = systemUiMode;
307301
}
308302

309303
private void setSystemChromeEnabledSystemUIOverlays(
310304
List<PlatformChannel.SystemUiOverlay> overlaysToShow) {
305+
Window window = activity.getWindow();
306+
View view = window.getDecorView();
307+
WindowInsetsControllerCompat windowInsetsControllerCompat =
308+
new WindowInsetsControllerCompat(window, view);
309+
311310
// Start by assuming we want to hide all system overlays (like an immersive
312311
// game).
313-
int enabledOverlays =
314-
DEFAULT_SYSTEM_UI
315-
| View.SYSTEM_UI_FLAG_FULLSCREEN
316-
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
317-
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
318-
319-
// The SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag was introduced in API 19, so we
320-
// apply it
321-
// if desired, and if the current Android version is 19 or greater.
322-
if (overlaysToShow.size() == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
323-
enabledOverlays |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
312+
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.systemBars());
313+
WindowCompat.setDecorFitsSystemWindows(window, false);
314+
315+
// We apply sticky immersive mode if desired. Available starting at SDK 20.
316+
if (overlaysToShow.size() == 0) {
317+
currentSystemUiMode = PlatformChannel.SystemUiMode.IMMERSIVE_STICKY;
318+
319+
windowInsetsControllerCompat.setSystemBarsBehavior(
320+
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
324321
}
325322

326323
// Re-add any desired system overlays.
327324
for (int i = 0; i < overlaysToShow.size(); ++i) {
328325
PlatformChannel.SystemUiOverlay overlayToShow = overlaysToShow.get(i);
329326
switch (overlayToShow) {
330327
case TOP_OVERLAYS:
331-
enabledOverlays &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
328+
windowInsetsControllerCompat.show(WindowInsetsCompat.Type.statusBars());
332329
break;
333330
case BOTTOM_OVERLAYS:
334-
enabledOverlays &= ~View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
335-
enabledOverlays &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
331+
windowInsetsControllerCompat.show(WindowInsetsCompat.Type.navigationBars());
336332
break;
337333
}
338334
}
339-
340-
mEnabledOverlays = enabledOverlays;
341-
updateSystemUiOverlays();
335+
currentOverlays = overlaysToShow;
342336
}
343337

344338
/**
@@ -350,8 +344,11 @@ private void setSystemChromeEnabledSystemUIOverlays(
350344
* PlatformPlugin}.
351345
*/
352346
public void updateSystemUiOverlays() {
353-
activity.getWindow().getDecorView().setSystemUiVisibility(mEnabledOverlays);
354-
if (currentTheme != null) {
347+
setSystemChromeEnabledSystemUIMode(currentSystemUiMode);
348+
349+
if (currentOverlays != null) {
350+
setSystemChromeEnabledSystemUIOverlays(currentOverlays);
351+
} else if (currentTheme != null) {
355352
setSystemChromeSystemUIOverlayStyle(currentTheme);
356353
}
357354
}

0 commit comments

Comments
 (0)