Use size to inflate nav bar buttons instead of rotation

For a landscape or close-to-square device, the button layout of
nav bar was abnormal since System UI assumes the nav bar position
would change if device rotated.
However, for default landscape device, nav bar will be always on
the buttom.

This patch resolve the assumption by using nav bar
direction (horizon/vertical) instead of rotation.
Also renaming the layout for consistency.

Test: atest SystemUITests
Test: atest NavigationBarFlaterViewTest
Test: manual - rotate the phone and comapre to my Pixel2
Test: manual - test the peproduce steps and the layout of nav bar buttons is normal
Fix: 124428114

Change-Id: I23444bcb2a44555e7116f6140a29147c5a03ed69
This commit is contained in:
Charles Chen
2019-02-19 14:18:19 +08:00
parent aa7b682699
commit aa94d95bfc
7 changed files with 191 additions and 124 deletions

View File

@@ -1,44 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:systemui="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/nav_buttons"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ends_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:clipChildren="false" />
<LinearLayout
android:id="@+id/center_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:clipChildren="false" />
</FrameLayout>
</FrameLayout>

View File

@@ -20,8 +20,8 @@
android:layout_width="match_parent"
android:layout_height="@dimen/navigation_bar_size">
<include android:id="@+id/rot0" layout="@layout/navigation_layout" />
<include android:id="@+id/horizontal" layout="@layout/navigation_layout" />
<include android:id="@+id/rot90" layout="@layout/navigation_layout_rot90" />
<include android:id="@+id/vertical" layout="@layout/navigation_layout_vertical" />
</com.android.systemui.tuner.PreviewNavInflater>

View File

@@ -22,7 +22,8 @@
android:layout_marginStart="@dimen/rounded_corner_content_padding"
android:layout_marginEnd="@dimen/rounded_corner_content_padding"
android:paddingStart="@dimen/nav_content_padding"
android:paddingEnd="@dimen/nav_content_padding">
android:paddingEnd="@dimen/nav_content_padding"
android:id="@+id/horizontal">
<com.android.systemui.statusbar.phone.NearestTouchFrame
android:id="@+id/nav_buttons"

View File

@@ -22,7 +22,8 @@
android:layout_marginTop="@dimen/rounded_corner_content_padding"
android:layout_marginBottom="@dimen/rounded_corner_content_padding"
android:paddingTop="@dimen/nav_content_padding"
android:paddingBottom="@dimen/nav_content_padding">
android:paddingBottom="@dimen/nav_content_padding"
android:id="@+id/vertical">
<com.android.systemui.statusbar.phone.NearestTouchFrame
android:id="@+id/nav_buttons"

View File

@@ -23,18 +23,15 @@ import android.graphics.drawable.Icon;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.Display;
import android.view.Display.Mode;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Space;
import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.Dependency;
import com.android.systemui.R;
import com.android.systemui.plugins.PluginListener;
@@ -83,21 +80,21 @@ public class NavigationBarInflaterView extends FrameLayout
private static final String WEIGHT_CENTERED_SUFFIX = "WC";
private final List<NavBarButtonProvider> mPlugins = new ArrayList<>();
private final Display mDisplay;
protected LayoutInflater mLayoutInflater;
protected LayoutInflater mLandscapeInflater;
protected FrameLayout mRot0;
protected FrameLayout mRot90;
private boolean isRot0Landscape;
protected FrameLayout mHorizontal;
protected FrameLayout mVertical;
private SparseArray<ButtonDispatcher> mButtonDispatchers;
@VisibleForTesting
SparseArray<ButtonDispatcher> mButtonDispatchers;
private String mCurrentLayout;
private View mLastPortrait;
private View mLastLandscape;
private boolean mIsVertical;
private boolean mAlternativeOrder;
private boolean mUsingCustomLayout;
@@ -106,14 +103,11 @@ public class NavigationBarInflaterView extends FrameLayout
public NavigationBarInflaterView(Context context, AttributeSet attrs) {
super(context, attrs);
createInflaters();
mDisplay = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Mode displayMode = mDisplay.getMode();
isRot0Landscape = displayMode.getPhysicalWidth() > displayMode.getPhysicalHeight();
mOverviewProxyService = Dependency.get(OverviewProxyService.class);
}
private void createInflaters() {
@VisibleForTesting
void createInflaters() {
mLayoutInflater = LayoutInflater.from(mContext);
Configuration landscape = new Configuration();
landscape.setTo(mContext.getResources().getConfiguration());
@@ -131,13 +125,12 @@ public class NavigationBarInflaterView extends FrameLayout
private void inflateChildren() {
removeAllViews();
mRot0 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, this, false);
mRot0.setId(R.id.rot0);
addView(mRot0);
mRot90 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_rot90, this,
false);
mRot90.setId(R.id.rot90);
addView(mRot90);
mHorizontal = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout,
this /* root */, false /* attachToRoot */);
addView(mHorizontal);
mVertical = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_vertical,
this /* root */, false /* attachToRoot */);
addView(mVertical);
updateAlternativeOrder();
}
@@ -197,12 +190,9 @@ public class NavigationBarInflaterView extends FrameLayout
}
}
public void updateButtonDispatchersCurrentView() {
void updateButtonDispatchersCurrentView() {
if (mButtonDispatchers != null) {
final int rotation = mDisplay.getRotation();
final boolean portrait = rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180;
final View view = portrait ? mRot0 : mRot90;
View view = mIsVertical ? mVertical : mHorizontal;
for (int i = 0; i < mButtonDispatchers.size(); i++) {
final ButtonDispatcher dispatcher = mButtonDispatchers.valueAt(i);
dispatcher.setCurrentView(view);
@@ -210,7 +200,13 @@ public class NavigationBarInflaterView extends FrameLayout
}
}
public void setAlternativeOrder(boolean alternativeOrder) {
void setVertical(boolean vertical) {
if (vertical != mIsVertical) {
mIsVertical = vertical;
}
}
void setAlternativeOrder(boolean alternativeOrder) {
if (alternativeOrder != mAlternativeOrder) {
mAlternativeOrder = alternativeOrder;
updateAlternativeOrder();
@@ -218,10 +214,10 @@ public class NavigationBarInflaterView extends FrameLayout
}
private void updateAlternativeOrder() {
updateAlternativeOrder(mRot0.findViewById(R.id.ends_group));
updateAlternativeOrder(mRot0.findViewById(R.id.center_group));
updateAlternativeOrder(mRot90.findViewById(R.id.ends_group));
updateAlternativeOrder(mRot90.findViewById(R.id.center_group));
updateAlternativeOrder(mHorizontal.findViewById(R.id.ends_group));
updateAlternativeOrder(mHorizontal.findViewById(R.id.center_group));
updateAlternativeOrder(mVertical.findViewById(R.id.ends_group));
updateAlternativeOrder(mVertical.findViewById(R.id.center_group));
}
private void updateAlternativeOrder(View v) {
@@ -231,10 +227,10 @@ public class NavigationBarInflaterView extends FrameLayout
}
private void initiallyFill(ButtonDispatcher buttonDispatcher) {
addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
addAll(buttonDispatcher, mHorizontal.findViewById(R.id.ends_group));
addAll(buttonDispatcher, mHorizontal.findViewById(R.id.center_group));
addAll(buttonDispatcher, mVertical.findViewById(R.id.ends_group));
addAll(buttonDispatcher, mVertical.findViewById(R.id.center_group));
}
private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
@@ -266,17 +262,23 @@ public class NavigationBarInflaterView extends FrameLayout
String[] center = sets[1].split(BUTTON_SEPARATOR);
String[] end = sets[2].split(BUTTON_SEPARATOR);
// Inflate these in start to end order or accessibility traversal will be messed up.
inflateButtons(start, mRot0.findViewById(R.id.ends_group), isRot0Landscape, true);
inflateButtons(start, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, true);
inflateButtons(start, mHorizontal.findViewById(R.id.ends_group),
false /* landscape */, true /* start */);
inflateButtons(start, mVertical.findViewById(R.id.ends_group),
true /* landscape */, true /* start */);
inflateButtons(center, mRot0.findViewById(R.id.center_group), isRot0Landscape, false);
inflateButtons(center, mRot90.findViewById(R.id.center_group), !isRot0Landscape, false);
inflateButtons(center, mHorizontal.findViewById(R.id.center_group),
false /* landscape */, false /* start */);
inflateButtons(center, mVertical.findViewById(R.id.center_group),
true /* landscape */, false /* start */);
addGravitySpacer(mRot0.findViewById(R.id.ends_group));
addGravitySpacer(mRot90.findViewById(R.id.ends_group));
addGravitySpacer(mHorizontal.findViewById(R.id.ends_group));
addGravitySpacer(mVertical.findViewById(R.id.ends_group));
inflateButtons(end, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
inflateButtons(end, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
inflateButtons(end, mHorizontal.findViewById(R.id.ends_group),
false /* landscape */, false /* start */);
inflateButtons(end, mVertical.findViewById(R.id.ends_group),
true /* landscape */, false /* start */);
updateButtonDispatchersCurrentView();
}
@@ -469,8 +471,8 @@ public class NavigationBarInflaterView extends FrameLayout
mButtonDispatchers.valueAt(i).clear();
}
}
clearAllChildren(mRot0.findViewById(R.id.nav_buttons));
clearAllChildren(mRot90.findViewById(R.id.nav_buttons));
clearAllChildren(mHorizontal.findViewById(R.id.nav_buttons));
clearAllChildren(mVertical.findViewById(R.id.nav_buttons));
}
private void clearAllChildren(ViewGroup group) {

View File

@@ -115,9 +115,11 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
final static boolean ALTERNATE_CAR_MODE_UI = false;
View mCurrentView = null;
View[] mRotatedViews = new View[4];
private View mVertical;
private View mHorizontal;
boolean mVertical;
/** Indicates that navigation bar is vertical. */
private boolean mIsVertical;
private int mCurrentRotation = -1;
boolean mLongClickableAccessibilityButton;
@@ -344,7 +346,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
public NavigationBarView(Context context, AttributeSet attrs) {
super(context, attrs);
mVertical = false;
mIsVertical = false;
mLongClickableAccessibilityButton = false;
// Set up the context group of buttons
@@ -464,7 +466,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
public void setOnVerticalChangedListener(OnVerticalChangedListener onVerticalChangedListener) {
mOnVerticalChangedListener = onVerticalChangedListener;
notifyVerticalChangedListener(mVertical);
notifyVerticalChangedListener(mIsVertical);
}
@Override
@@ -540,10 +542,6 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
return mCurrentView;
}
public View[] getAllViews() {
return mRotatedViews;
}
public ButtonDispatcher getRecentsButton() {
return mButtonDispatchers.get(R.id.recent_apps);
}
@@ -646,7 +644,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
// Animate the back button's rotation to the new degrees and only in portrait move up the
// back button to line up with the other buttons
float targetY = !mOverviewProxyService.shouldShowSwipeUpUI() && !mVertical && useAltBack
float targetY = !mOverviewProxyService.shouldShowSwipeUpUI() && !mIsVertical && useAltBack
? - getResources().getDimension(R.dimen.navbar_back_button_ime_offset)
: 0;
ObjectAnimator navBarAnimator = ObjectAnimator.ofPropertyValuesHolder(drawable,
@@ -658,7 +656,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
}
private void orientHomeButton(KeyButtonDrawable drawable) {
drawable.setRotation(mVertical ? 90 : 0);
drawable.setRotation(mIsVertical ? 90 : 0);
}
private KeyButtonDrawable chooseNavigationIconDrawable(@DrawableRes int icon,
@@ -934,7 +932,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
getImeSwitchButton().setOnClickListener(mImeSwitcherClickListener);
DockedStackExistsListener.register(mDockedListener);
updateRotatedViews();
updateOrientationViews();
reloadNavIcons();
}
@@ -998,34 +996,35 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
view.setTranslationY(posY);
}
private void updateRotatedViews() {
mRotatedViews[Surface.ROTATION_0] =
mRotatedViews[Surface.ROTATION_180] = findViewById(R.id.rot0);
mRotatedViews[Surface.ROTATION_270] =
mRotatedViews[Surface.ROTATION_90] = findViewById(R.id.rot90);
private void updateOrientationViews() {
mHorizontal = findViewById(R.id.horizontal);
mVertical = findViewById(R.id.vertical);
updateCurrentView();
}
public boolean needsReorient(int rotation) {
boolean needsReorient(int rotation) {
return mCurrentRotation != rotation;
}
private void updateCurrentView() {
final int rot = getContextDisplay().getRotation();
for (int i=0; i<4; i++) {
mRotatedViews[i].setVisibility(View.GONE);
}
mCurrentView = mRotatedViews[rot];
resetViews();
mCurrentView = mIsVertical ? mVertical : mHorizontal;
mCurrentView.setVisibility(View.VISIBLE);
mNavigationInflaterView.setAlternativeOrder(rot == Surface.ROTATION_90);
mNavigationInflaterView.setVertical(mIsVertical);
mCurrentRotation = getContextDisplay().getRotation();
mNavigationInflaterView.setAlternativeOrder(mCurrentRotation == Surface.ROTATION_90);
mNavigationInflaterView.updateButtonDispatchersCurrentView();
updateLayoutTransitionsEnabled();
mCurrentRotation = rot;
}
private void resetViews() {
mHorizontal.setVisibility(View.GONE);
mVertical.setVisibility(View.GONE);
}
private void updateRecentsIcon() {
mDockedIcon.setRotation(mDockedStackExists && mVertical ? 90 : 0);
mDockedIcon.setRotation(mDockedStackExists && mIsVertical ? 90 : 0);
getRecentsButton().setImageDrawable(mDockedStackExists ? mDockedIcon : mRecentIcon);
mBarTransitions.reapplyDarkIntensity();
}
@@ -1047,7 +1046,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
}
public boolean isVertical() {
return mVertical;
return mIsVertical;
}
public void reorient() {
@@ -1071,7 +1070,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
updateTaskSwitchHelper();
updateNavButtonIcons();
getHomeButton().setVertical(mVertical);
getHomeButton().setVertical(mIsVertical);
}
private void updateTaskSwitchHelper() {
@@ -1104,9 +1103,12 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
"onSizeChanged: (%dx%d) old: (%dx%d)", w, h, oldw, oldh));
final boolean newVertical = w > 0 && h > w;
if (newVertical != mVertical) {
mVertical = newVertical;
//Log.v(TAG, String.format("onSizeChanged: h=%d, w=%d, vert=%s", h, w, mVertical?"y":"n"));
if (newVertical != mIsVertical) {
mIsVertical = newVertical;
if (DEBUG) {
Log.d(TAG, String.format("onSizeChanged: h=%d, w=%d, vert=%s", h, w,
mIsVertical ? "y" : "n"));
}
reorient();
notifyVerticalChangedListener(newVertical);
}
@@ -1311,7 +1313,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener<Nav
pw.println(String.format(" disabled=0x%08x vertical=%s menu=%s darkIntensity=%.2f",
mDisabledFlags,
mVertical ? "true" : "false",
mIsVertical ? "true" : "false",
getMenuButton().isVisible() ? "true" : "false",
getLightTransitionsController().getCurrentDarkIntensity()));

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.statusbar.phone;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import android.support.test.filters.SmallTest;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper.RunWithLooper;
import android.util.SparseArray;
import android.view.View;
import android.widget.FrameLayout;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.statusbar.CommandQueue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/** atest NavigationBarInflaterViewTest */
@RunWith(AndroidTestingRunner.class)
@RunWithLooper
@SmallTest
public class NavigationBarInflaterViewTest extends SysuiTestCase {
private NavigationBarInflaterView mNavBarInflaterView;
private static final int BUTTON_ID = 0;
@Before
public void setUp() {
mContext.putComponent(CommandQueue.class, mock(CommandQueue.class));
mNavBarInflaterView = spy(new NavigationBarInflaterView(mContext, null));
doNothing().when(mNavBarInflaterView).createInflaters();
mNavBarInflaterView.mButtonDispatchers = new SparseArray<>(1);
mNavBarInflaterView.mButtonDispatchers.put(BUTTON_ID, new ButtonDispatcher(BUTTON_ID));
initializeViews();
}
private void initializeViews() {
mNavBarInflaterView.mVertical = mock(FrameLayout.class);
mNavBarInflaterView.mHorizontal = mock(FrameLayout.class);
initializeLayout(mNavBarInflaterView.mVertical);
initializeLayout(mNavBarInflaterView.mHorizontal);
}
private void initializeLayout(FrameLayout layout) {
View verticalChildView = mock(View.class);
verticalChildView.setId(BUTTON_ID);
doReturn(layout).when(verticalChildView).getParent();
doReturn(verticalChildView).when(layout).findViewById(BUTTON_ID);
}
@After
public void tearDown() {
mNavBarInflaterView = null;
}
@Test
public void testUpdateButtonDispatchersCurrentView_isVerticalTrue() {
mNavBarInflaterView.setVertical(true);
mNavBarInflaterView.updateButtonDispatchersCurrentView();
ButtonDispatcher button = mNavBarInflaterView.mButtonDispatchers.get(BUTTON_ID);
assertEquals("Buttons need to be set to vertical layout",
mNavBarInflaterView.mVertical.getId(),
((View) button.getCurrentView().getParent()).getId());
}
@Test
public void testUpdateButtonDispatchersCurrentView_isVerticalFalse() {
mNavBarInflaterView.setVertical(false);
mNavBarInflaterView.updateButtonDispatchersCurrentView();
ButtonDispatcher button = mNavBarInflaterView.mButtonDispatchers.get(BUTTON_ID);
assertEquals("Buttons need to be set to horizon layout",
mNavBarInflaterView.mHorizontal.getId(),
((View) button.getCurrentView().getParent()).getId());
}
}