From b56ea7e9e3401288bdedfcf60bcc04f237f8f4c7 Mon Sep 17 00:00:00 2001 From: Jason Chiu Date: Fri, 22 Jan 2021 13:40:08 +0800 Subject: [PATCH] Adjust homepage styles and layouts - Fine tune the header text size and position - Fix the header overlapping problem on the homepage and collapsing toolbar pages when configuration changes by adding android:id. - New AppBarScrollingViewBehavior for the homepage to avoid manually offsetting the IA - Correct the formula of counting search box height Test: robotest, manual Fixes: 177967923 Fixes: 177968564 Change-Id: Id3ed44f4296fa2a19d6890b009d3881a5c140bbd --- res/layout/search_bar.xml | 2 +- .../settings_collapsing_base_layout.xml | 1 + res/layout/settings_homepage_container.xml | 3 +- res/values/dimens.xml | 3 +- res/values/styles.xml | 2 +- .../homepage/SettingsHomepageActivity.java | 34 +++-------- .../HomepageAppBarScrollingViewBehavior.java | 56 ++++++++++++++++++ .../SettingsHomepageActivityTest.java | 27 --------- ...mepageAppBarScrollingViewBehaviorTest.java | 58 +++++++++++++++++++ 9 files changed, 129 insertions(+), 57 deletions(-) create mode 100644 src/com/android/settings/widget/HomepageAppBarScrollingViewBehavior.java create mode 100644 tests/robotests/src/com/android/settings/widget/HomepageAppBarScrollingViewBehaviorTest.java diff --git a/res/layout/search_bar.xml b/res/layout/search_bar.xml index c4dd15dc243..75b43f003d1 100644 --- a/res/layout/search_bar.xml +++ b/res/layout/search_bar.xml @@ -23,7 +23,7 @@ android:layout_marginTop="@dimen/search_bar_margin" android:layout_marginStart="@dimen/search_bar_margin" android:layout_marginEnd="@dimen/search_bar_margin" - android:layout_marginBottom="16dp" + android:layout_marginBottom="@dimen/search_bar_margin_bottom" app:layout_scrollFlags="scroll|enterAlways"> diff --git a/res/layout/settings_homepage_container.xml b/res/layout/settings_homepage_container.xml index fe119c6d1e6..d65d1401761 100644 --- a/res/layout/settings_homepage_container.xml +++ b/res/layout/settings_homepage_container.xml @@ -27,7 +27,7 @@ android:id="@+id/main_content_scrollable_container" android:layout_width="match_parent" android:layout_height="match_parent" - app:layout_behavior="com.android.settings.widget.FloatingAppBarScrollingViewBehavior"> + app:layout_behavior="com.android.settings.widget.HomepageAppBarScrollingViewBehavior"> 24dp + 16dp 48dp 16sp 2dp @@ -138,7 +139,7 @@ @dimen/search_bar_height - 224dp + 232dp 24dp 8dp 16dp diff --git a/res/values/styles.xml b/res/values/styles.xml index cdf1995a2f3..c9b33d36614 100644 --- a/res/values/styles.xml +++ b/res/values/styles.xml @@ -791,7 +791,7 @@ diff --git a/src/com/android/settings/homepage/SettingsHomepageActivity.java b/src/com/android/settings/homepage/SettingsHomepageActivity.java index 5eb9d9468db..22594411ce9 100644 --- a/src/com/android/settings/homepage/SettingsHomepageActivity.java +++ b/src/com/android/settings/homepage/SettingsHomepageActivity.java @@ -27,7 +27,6 @@ import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.Toolbar; -import androidx.annotation.VisibleForTesting; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; @@ -43,7 +42,6 @@ import com.android.settings.overlay.FeatureFactory; public class SettingsHomepageActivity extends FragmentActivity { private static final String TAG = "SettingsHomepageActivity"; - private int mSearchBoxHeight; @Override protected void onCreate(Bundle savedInstanceState) { @@ -56,7 +54,7 @@ public class SettingsHomepageActivity extends FragmentActivity { final View appBar = findViewById(R.id.app_bar_container); appBar.setMinimumHeight(getSearchBoxHeight()); - setDefaultHomepageContainerPaddingTop(); + initHomepageContainer(); final Toolbar toolbar = findViewById(R.id.search_action_bar); FeatureFactory.getFactory(this).getSearchFeatureProvider() @@ -89,8 +87,6 @@ public class SettingsHomepageActivity extends FragmentActivity { try { showFragment(fragment.newInstance(), R.id.contextual_suggestion_content); - setHomepageContainerTopOffset(getResources() - .getDimensionPixelSize(R.dimen.suggestion_height)); } catch (IllegalAccessException | InstantiationException e) { Log.w(TAG, "Cannot show fragment", e); } @@ -109,33 +105,19 @@ public class SettingsHomepageActivity extends FragmentActivity { fragmentTransaction.commit(); } - @VisibleForTesting - void setHomepageContainerTopOffset(int offset) { + private void initHomepageContainer() { final View view = findViewById(R.id.homepage_container); - final int paddingTop = getSearchBoxHeight() + offset; - view.setPadding(0 /* left */, paddingTop, 0 /* right */, 0 /* bottom */); - // Prevent inner RecyclerView gets focus and invokes scrolling. view.setFocusableInTouchMode(true); view.requestFocus(); } - @VisibleForTesting - void setDefaultHomepageContainerPaddingTop() { - setHomepageContainerTopOffset(0); - } - - @VisibleForTesting - int getSearchBoxHeight() { - if (mSearchBoxHeight != 0) { - return mSearchBoxHeight; - } - + private int getSearchBoxHeight() { final int searchBarHeight = getResources().getDimensionPixelSize(R.dimen.search_bar_height); - final int searchBarMargin = getResources().getDimensionPixelSize(R.dimen.search_bar_margin); - - // The height of search box is the height of search bar(48dp) + top/bottom margins(24dp) - mSearchBoxHeight = searchBarHeight + searchBarMargin * 2; - return mSearchBoxHeight; + final int searchBarMarginTop = getResources().getDimensionPixelSize( + R.dimen.search_bar_margin); + final int searchBarMarginBottom = getResources().getDimensionPixelSize( + R.dimen.search_bar_margin_bottom); + return searchBarHeight + searchBarMarginTop + searchBarMarginBottom; } } diff --git a/src/com/android/settings/widget/HomepageAppBarScrollingViewBehavior.java b/src/com/android/settings/widget/HomepageAppBarScrollingViewBehavior.java new file mode 100644 index 00000000000..c8dbc65834a --- /dev/null +++ b/src/com/android/settings/widget/HomepageAppBarScrollingViewBehavior.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2021 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.settings.widget; + +import android.content.Context; +import android.graphics.Color; +import android.util.AttributeSet; +import android.view.View; + +import androidx.annotation.VisibleForTesting; +import androidx.coordinatorlayout.widget.CoordinatorLayout; + +import com.google.android.material.appbar.AppBarLayout; + +/** + * This scrolling view behavior will set the background of the {@link AppBarLayout} as + * transparent and without the elevation. + */ +public class HomepageAppBarScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior { + private boolean mInitialized; + + public HomepageAppBarScrollingViewBehavior(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { + boolean changed = super.onDependentViewChanged(parent, child, dependency); + if (!mInitialized && dependency instanceof AppBarLayout) { + mInitialized = true; + AppBarLayout appBarLayout = (AppBarLayout) dependency; + setAppBarLayoutTransparent(appBarLayout); + } + return changed; + } + + @VisibleForTesting + void setAppBarLayoutTransparent(AppBarLayout appBarLayout) { + appBarLayout.setBackgroundColor(Color.TRANSPARENT); + appBarLayout.setTargetElevation(0); + } +} diff --git a/tests/robotests/src/com/android/settings/homepage/SettingsHomepageActivityTest.java b/tests/robotests/src/com/android/settings/homepage/SettingsHomepageActivityTest.java index 4c7b4b5829a..bae4432aedc 100644 --- a/tests/robotests/src/com/android/settings/homepage/SettingsHomepageActivityTest.java +++ b/tests/robotests/src/com/android/settings/homepage/SettingsHomepageActivityTest.java @@ -26,7 +26,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.os.Build; -import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.FrameLayout; @@ -54,32 +53,6 @@ public class SettingsHomepageActivityTest { MockitoAnnotations.initMocks(this); } - @Test - public void setDefaultHomepageContainerPaddingTop_shouldSetSearchBoxHeight() { - final SettingsHomepageActivity activity = Robolectric.buildActivity( - SettingsHomepageActivity.class).create().get(); - final View view = activity.findViewById(R.id.homepage_container); - - activity.setDefaultHomepageContainerPaddingTop(); - - final int actualPaddingTop = view.getPaddingTop(); - assertThat(actualPaddingTop).isEqualTo(activity.getSearchBoxHeight()); - } - - @Test - public void setHomepageContainerTopOffset_shouldBeSetPaddingTop() { - final SettingsHomepageActivity activity = Robolectric.buildActivity( - SettingsHomepageActivity.class).create().get(); - final View view = activity.findViewById(R.id.homepage_container); - final int offset = activity.getResources().getDimensionPixelSize( - R.dimen.suggestion_height); - - activity.setHomepageContainerTopOffset(offset); - - final int actualPaddingTop = view.getPaddingTop(); - assertThat(actualPaddingTop).isEqualTo(activity.getSearchBoxHeight() + offset); - } - @Test public void launch_shouldHaveAnimationForIaFragment() { final SettingsHomepageActivity activity = Robolectric.buildActivity( diff --git a/tests/robotests/src/com/android/settings/widget/HomepageAppBarScrollingViewBehaviorTest.java b/tests/robotests/src/com/android/settings/widget/HomepageAppBarScrollingViewBehaviorTest.java new file mode 100644 index 00000000000..6be4c137c1f --- /dev/null +++ b/tests/robotests/src/com/android/settings/widget/HomepageAppBarScrollingViewBehaviorTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2021 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.settings.widget; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; + +import com.android.settings.R; + +import com.google.android.material.appbar.AppBarLayout; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class HomepageAppBarScrollingViewBehaviorTest { + + private HomepageAppBarScrollingViewBehavior mScrollingViewBehavior; + private Context mContext; + + @Before + public void setUp() { + mContext = RuntimeEnvironment.application; + mScrollingViewBehavior = new HomepageAppBarScrollingViewBehavior(mContext, + Robolectric.buildAttributeSet().build()); + } + + @Test + public void setAppBarLayoutTransparent_backgroundDefaultAsWhite_shouldBeTransparent() { + mContext.setTheme(R.style.Theme_Settings_Home); + final AppBarLayout appBarLayout = new AppBarLayout(mContext); + appBarLayout.setBackgroundColor(Color.WHITE); + mScrollingViewBehavior.setAppBarLayoutTransparent(appBarLayout); + assertThat(((ColorDrawable) appBarLayout.getBackground()).getColor()).isEqualTo( + Color.TRANSPARENT); + } +}