Set the back button sysUiVisibility as well

Changes to window sysUiVisibility is not picked up after the window
is laid out, so add a call to set the decor view's sysUiVisibility as
well.

Test: ./gradlew connectedAndroidTest
Bug: 72840994
Change-Id: I6f0744599f46adf1840738f226b4394e5b7ed1df
This commit is contained in:
Maurice Lam 2018-02-01 18:01:33 -08:00
parent f2c8c89ad6
commit c9c5c4431a
3 changed files with 32 additions and 7 deletions

View file

@ -24,6 +24,7 @@ import android.content.res.TypedArray;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.support.annotation.RequiresPermission;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
@ -183,12 +184,25 @@ public class SystemBarHelper {
}
}
/**
* Sets whether the back button on the software navigation bar is visible. This only works if
* you have the STATUS_BAR permission. Otherwise framework will filter out this flag and this
* method call will not have any effect.
*
* <p>IMPORTANT: Do not assume that users have no way to go back when the back button is hidden.
* Many devices have physical back buttons, and accessibility services like TalkBack may have
* gestures mapped to back. Please use onBackPressed, onKeyDown, or other similar ways to
* make sure back button events are still handled (or ignored) properly.
*/
@RequiresPermission("android.permission.STATUS_BAR")
public static void setBackButtonVisible(final Window window, final boolean visible) {
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
if (visible) {
removeVisibilityFlag(window, STATUS_BAR_DISABLE_BACK);
removeImmersiveFlagsFromDecorView(window, STATUS_BAR_DISABLE_BACK);
} else {
addVisibilityFlag(window, STATUS_BAR_DISABLE_BACK);
addImmersiveFlagsToDecorView(window, STATUS_BAR_DISABLE_BACK);
}
}
}
@ -217,7 +231,7 @@ public class SystemBarHelper {
* {@link View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} only takes effect when it is added to a view
* instead of the window.
*/
@TargetApi(VERSION_CODES.LOLLIPOP)
@TargetApi(VERSION_CODES.HONEYCOMB)
private static void addImmersiveFlagsToDecorView(final Window window, final int vis) {
getDecorView(window, new OnDecorViewInstalledListener() {
@Override
@ -227,7 +241,7 @@ public class SystemBarHelper {
});
}
@TargetApi(VERSION_CODES.LOLLIPOP)
@TargetApi(VERSION_CODES.HONEYCOMB)
private static void removeImmersiveFlagsFromDecorView(final Window window, final int vis) {
getDecorView(window, new OnDecorViewInstalledListener() {
@Override

View file

@ -29,6 +29,7 @@ android.sourceSets {
androidTestImplementation 'com.android.support.test:runner:1.0.0'
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
androidTestImplementation 'com.google.truth:truth:0.31'
androidTestImplementation 'junit:junit:4.+'
androidTestImplementation 'org.mockito:mockito-core:1.9.5'
}

View file

@ -16,6 +16,8 @@
package com.android.setupwizardlib.test;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import android.annotation.SuppressLint;
@ -194,11 +196,15 @@ public class SystemBarHelperTest {
@UiThreadTest
@Test
public void testSetBackButtonVisibleTrue() {
final Window window = createWindowWithSystemUiVisibility(0x456);
final Window window = createWindowWithSystemUiVisibility(STATUS_BAR_DISABLE_BACK | 0x456);
SystemBarHelper.setBackButtonVisible(window, true);
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
assertEquals("View visibility should be 0x456", 0x456,
window.getAttributes().systemUiVisibility);
assertThat(window.getAttributes().systemUiVisibility)
.named("window sysUiVisibility")
.isEqualTo(0x456);
assertThat(window.getDecorView().getSystemUiVisibility())
.named("decor view sysUiVisibility")
.isEqualTo(0x456);
}
}
@ -208,8 +214,12 @@ public class SystemBarHelperTest {
final Window window = createWindowWithSystemUiVisibility(0x456);
SystemBarHelper.setBackButtonVisible(window, false);
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
assertEquals("STATUS_BAR_DISABLE_BACK should be added to systemUiVisibility",
0x456 | STATUS_BAR_DISABLE_BACK, window.getAttributes().systemUiVisibility);
assertThat(window.getAttributes().systemUiVisibility)
.named("window sysUiVisibility")
.isEqualTo(0x456 | STATUS_BAR_DISABLE_BACK);
assertThat(window.getDecorView().getSystemUiVisibility())
.named("decor view sysUiVisibility")
.isEqualTo(0x456 | STATUS_BAR_DISABLE_BACK);
}
}