Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 29374ed5 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Update the collapsing toolbar layout" into sc-dev am: b0e554dc

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14747454

Change-Id: I88c199607f93ceddae5f1651d575ba717fee9f25
parents 5596fd92 b0e554dc
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -16,10 +16,12 @@
-->
<resources>
    <!-- Collapsing toolbar layout dimensions -->
    <dimen name="toolbar_one_line_height">226dp</dimen>
    <dimen name="toolbar_two_lines_height">270dp</dimen>
    <dimen name="toolbar_three_lines_height">314dp</dimen>
    <dimen name="toolbar_one_line_height">216dp</dimen>
    <dimen name="toolbar_two_lines_height">260dp</dimen>
    <dimen name="toolbar_three_lines_height">304dp</dimen>
    <dimen name="scrim_visible_height_trigger">174dp</dimen>
    <dimen name="scrim_visible_height_trigger_two_lines">218dp</dimen>
    <dimen name="scrim_visible_height_trigger_three_lines">262dp</dimen>
    <dimen name="expanded_title_margin_start">24dp</dimen>
    <dimen name="expanded_title_margin_end">24dp</dimen>
</resources>
 No newline at end of file
+9 −30
Original line number Diff line number Diff line
@@ -26,12 +26,9 @@ import androidx.annotation.Nullable;

import com.google.android.material.appbar.CollapsingToolbarLayout;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * A customized version of CollapsingToolbarLayout that can apply different font size based on
 * the line count of its title.
 * A customized version of CollapsingToolbarLayout that can apply different font size based on the
 * line count of its title.
 */
public class AdjustableToolbarLayout extends CollapsingToolbarLayout {

@@ -59,43 +56,25 @@ public class AdjustableToolbarLayout extends CollapsingToolbarLayout {
            public void onLayoutChange(View v, int left, int top, int right, int bottom,
                    int oldLeft, int oldTop, int oldRight, int oldBottom) {
                v.removeOnLayoutChangeListener(this);
                final int count = getLineCountWithReflection();
                final int count = getLineCount();
                if (count > TOOLBAR_MAX_LINE_NUMBER) {
                    final ViewGroup.LayoutParams lp = getLayoutParams();
                    lp.height = getResources()
                            .getDimensionPixelSize(R.dimen.toolbar_three_lines_height);
                    setScrimVisibleHeightTrigger(
                            getResources().getDimensionPixelSize(
                                    R.dimen.scrim_visible_height_trigger_three_lines));
                    setLayoutParams(lp);
                } else if (count == TOOLBAR_MAX_LINE_NUMBER) {
                    final ViewGroup.LayoutParams lp = getLayoutParams();
                    lp.height = getResources()
                            .getDimensionPixelSize(R.dimen.toolbar_two_lines_height);
                    setScrimVisibleHeightTrigger(
                            getResources().getDimensionPixelSize(
                                    R.dimen.scrim_visible_height_trigger_two_lines));
                    setLayoutParams(lp);
                }
            }
        });
    }

    /**
     * Returns a number of title line count for CollapsingToolbarLayout so that facilitates the
     * determination to apply what kind of font size. Since the title of CollapsingToolbarLayout is
     * drawn in a canvas and the text process is wrapped in a CollapsingTextHelper, the way we used
     * here is to get the line count from the CollapsingTextHelper via Java Reflection.
     */
    private int getLineCountWithReflection() {
        try {
            final Field textHelperField =
                    this.getClass().getSuperclass().getDeclaredField("collapsingTextHelper");
            textHelperField.setAccessible(true);
            final Object textHelperObj = textHelperField.get(this);

            final Field layoutField = textHelperObj.getClass().getDeclaredField("textLayout");
            layoutField.setAccessible(true);
            final Object layoutObj = layoutField.get(textHelperObj);

            final Method method = layoutObj.getClass().getDeclaredMethod("getLineCount");
            return (int) method.invoke(layoutObj);
        } catch (Exception e) {
            return 0;
        }
    }
}