jimu-decompiled/sources/com/google/android/material/appbar/CollapsingToolbarLayout.java
2025-05-13 19:24:51 +02:00

746 lines
25 KiB
Java

package com.google.android.material.appbar;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.math.MathUtils;
import androidx.core.util.ObjectsCompat;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.material.R$id;
import com.google.android.material.R$style;
import com.google.android.material.R$styleable;
import com.google.android.material.animation.AnimationUtils;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.internal.CollapsingTextHelper;
import com.google.android.material.internal.DescendantOffsetUtils;
import com.google.android.material.internal.ThemeEnforcement;
/* loaded from: classes.dex */
public class CollapsingToolbarLayout extends FrameLayout {
private boolean a;
private int b;
private Toolbar c;
private View d;
private View e;
private int f;
private int g;
private int h;
private int i;
private final Rect j;
final CollapsingTextHelper k;
private boolean l;
private boolean m;
private Drawable n;
Drawable o;
private int p;
private boolean q;
private ValueAnimator r;
private long s;
private int t;
private AppBarLayout.OnOffsetChangedListener u;
int v;
WindowInsetsCompat x;
private class OffsetUpdateListener implements AppBarLayout.OnOffsetChangedListener {
OffsetUpdateListener() {
}
@Override // com.google.android.material.appbar.AppBarLayout.BaseOnOffsetChangedListener
public void a(AppBarLayout appBarLayout, int i) {
CollapsingToolbarLayout collapsingToolbarLayout = CollapsingToolbarLayout.this;
collapsingToolbarLayout.v = i;
WindowInsetsCompat windowInsetsCompat = collapsingToolbarLayout.x;
int e = windowInsetsCompat != null ? windowInsetsCompat.e() : 0;
int childCount = CollapsingToolbarLayout.this.getChildCount();
for (int i2 = 0; i2 < childCount; i2++) {
View childAt = CollapsingToolbarLayout.this.getChildAt(i2);
LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();
ViewOffsetHelper d = CollapsingToolbarLayout.d(childAt);
int i3 = layoutParams.a;
if (i3 == 1) {
d.b(MathUtils.a(-i, 0, CollapsingToolbarLayout.this.a(childAt)));
} else if (i3 == 2) {
d.b(Math.round((-i) * layoutParams.b));
}
}
CollapsingToolbarLayout.this.a();
CollapsingToolbarLayout collapsingToolbarLayout2 = CollapsingToolbarLayout.this;
if (collapsingToolbarLayout2.o != null && e > 0) {
ViewCompat.A(collapsingToolbarLayout2);
}
CollapsingToolbarLayout.this.k.b(Math.abs(i) / ((CollapsingToolbarLayout.this.getHeight() - ViewCompat.l(CollapsingToolbarLayout.this)) - e));
}
}
public CollapsingToolbarLayout(Context context) {
this(context, null);
}
private void b() {
if (this.a) {
Toolbar toolbar = null;
this.c = null;
this.d = null;
int i = this.b;
if (i != -1) {
this.c = (Toolbar) findViewById(i);
Toolbar toolbar2 = this.c;
if (toolbar2 != null) {
this.d = b(toolbar2);
}
}
if (this.c == null) {
int childCount = getChildCount();
int i2 = 0;
while (true) {
if (i2 >= childCount) {
break;
}
View childAt = getChildAt(i2);
if (childAt instanceof Toolbar) {
toolbar = (Toolbar) childAt;
break;
}
i2++;
}
this.c = toolbar;
}
d();
this.a = false;
}
}
private static int c(View view) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if (!(layoutParams instanceof ViewGroup.MarginLayoutParams)) {
return view.getHeight();
}
ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
return view.getHeight() + marginLayoutParams.topMargin + marginLayoutParams.bottomMargin;
}
private void d() {
View view;
if (!this.l && (view = this.e) != null) {
ViewParent parent = view.getParent();
if (parent instanceof ViewGroup) {
((ViewGroup) parent).removeView(this.e);
}
}
if (!this.l || this.c == null) {
return;
}
if (this.e == null) {
this.e = new View(getContext());
}
if (this.e.getParent() == null) {
this.c.addView(this.e, -1, -1);
}
}
private boolean e(View view) {
View view2 = this.d;
if (view2 == null || view2 == this) {
if (view == this.c) {
return true;
}
} else if (view == view2) {
return true;
}
return false;
}
WindowInsetsCompat a(WindowInsetsCompat windowInsetsCompat) {
WindowInsetsCompat windowInsetsCompat2 = ViewCompat.h(this) ? windowInsetsCompat : null;
if (!ObjectsCompat.a(this.x, windowInsetsCompat2)) {
this.x = windowInsetsCompat2;
requestLayout();
}
return windowInsetsCompat.a();
}
@Override // android.widget.FrameLayout, android.view.ViewGroup
protected boolean checkLayoutParams(ViewGroup.LayoutParams layoutParams) {
return layoutParams instanceof LayoutParams;
}
@Override // android.view.View
public void draw(Canvas canvas) {
Drawable drawable;
super.draw(canvas);
b();
if (this.c == null && (drawable = this.n) != null && this.p > 0) {
drawable.mutate().setAlpha(this.p);
this.n.draw(canvas);
}
if (this.l && this.m) {
this.k.a(canvas);
}
if (this.o == null || this.p <= 0) {
return;
}
WindowInsetsCompat windowInsetsCompat = this.x;
int e = windowInsetsCompat != null ? windowInsetsCompat.e() : 0;
if (e > 0) {
this.o.setBounds(0, -this.v, getWidth(), e - this.v);
this.o.mutate().setAlpha(this.p);
this.o.draw(canvas);
}
}
@Override // android.view.ViewGroup
protected boolean drawChild(Canvas canvas, View view, long j) {
boolean z;
if (this.n == null || this.p <= 0 || !e(view)) {
z = false;
} else {
this.n.mutate().setAlpha(this.p);
this.n.draw(canvas);
z = true;
}
return super.drawChild(canvas, view, j) || z;
}
@Override // android.view.ViewGroup, android.view.View
protected void drawableStateChanged() {
super.drawableStateChanged();
int[] drawableState = getDrawableState();
Drawable drawable = this.o;
boolean z = false;
if (drawable != null && drawable.isStateful()) {
z = false | drawable.setState(drawableState);
}
Drawable drawable2 = this.n;
if (drawable2 != null && drawable2.isStateful()) {
z |= drawable2.setState(drawableState);
}
CollapsingTextHelper collapsingTextHelper = this.k;
if (collapsingTextHelper != null) {
z |= collapsingTextHelper.a(drawableState);
}
if (z) {
invalidate();
}
}
public int getCollapsedTitleGravity() {
return this.k.c();
}
public Typeface getCollapsedTitleTypeface() {
return this.k.e();
}
public Drawable getContentScrim() {
return this.n;
}
public int getExpandedTitleGravity() {
return this.k.g();
}
public int getExpandedTitleMarginBottom() {
return this.i;
}
public int getExpandedTitleMarginEnd() {
return this.h;
}
public int getExpandedTitleMarginStart() {
return this.f;
}
public int getExpandedTitleMarginTop() {
return this.g;
}
public Typeface getExpandedTitleTypeface() {
return this.k.h();
}
int getScrimAlpha() {
return this.p;
}
public long getScrimAnimationDuration() {
return this.s;
}
public int getScrimVisibleHeightTrigger() {
int i = this.t;
if (i >= 0) {
return i;
}
WindowInsetsCompat windowInsetsCompat = this.x;
int e = windowInsetsCompat != null ? windowInsetsCompat.e() : 0;
int l = ViewCompat.l(this);
return l > 0 ? Math.min((l * 2) + e, getHeight()) : getHeight() / 3;
}
public Drawable getStatusBarScrim() {
return this.o;
}
public CharSequence getTitle() {
if (this.l) {
return this.k.j();
}
return null;
}
@Override // android.view.ViewGroup, android.view.View
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Object parent = getParent();
if (parent instanceof AppBarLayout) {
ViewCompat.a(this, ViewCompat.h((View) parent));
if (this.u == null) {
this.u = new OffsetUpdateListener();
}
((AppBarLayout) parent).a(this.u);
ViewCompat.B(this);
}
}
@Override // android.view.ViewGroup, android.view.View
protected void onDetachedFromWindow() {
ViewParent parent = getParent();
AppBarLayout.OnOffsetChangedListener onOffsetChangedListener = this.u;
if (onOffsetChangedListener != null && (parent instanceof AppBarLayout)) {
((AppBarLayout) parent).b(onOffsetChangedListener);
}
super.onDetachedFromWindow();
}
@Override // android.widget.FrameLayout, android.view.ViewGroup, android.view.View
protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
View view;
super.onLayout(z, i, i2, i3, i4);
WindowInsetsCompat windowInsetsCompat = this.x;
if (windowInsetsCompat != null) {
int e = windowInsetsCompat.e();
int childCount = getChildCount();
for (int i5 = 0; i5 < childCount; i5++) {
View childAt = getChildAt(i5);
if (!ViewCompat.h(childAt) && childAt.getTop() < e) {
ViewCompat.d(childAt, e);
}
}
}
if (this.l && (view = this.e) != null) {
this.m = ViewCompat.w(view) && this.e.getVisibility() == 0;
if (this.m) {
boolean z2 = ViewCompat.k(this) == 1;
View view2 = this.d;
if (view2 == null) {
view2 = this.c;
}
int a = a(view2);
DescendantOffsetUtils.a(this, this.e, this.j);
this.k.a(this.j.left + (z2 ? this.c.getTitleMarginEnd() : this.c.getTitleMarginStart()), this.j.top + a + this.c.getTitleMarginTop(), this.j.right + (z2 ? this.c.getTitleMarginStart() : this.c.getTitleMarginEnd()), (this.j.bottom + a) - this.c.getTitleMarginBottom());
this.k.b(z2 ? this.h : this.f, this.j.top + this.g, (i3 - i) - (z2 ? this.f : this.h), (i4 - i2) - this.i);
this.k.m();
}
}
int childCount2 = getChildCount();
for (int i6 = 0; i6 < childCount2; i6++) {
d(getChildAt(i6)).c();
}
if (this.c != null) {
if (this.l && TextUtils.isEmpty(this.k.j())) {
setTitle(this.c.getTitle());
}
View view3 = this.d;
if (view3 == null || view3 == this) {
setMinimumHeight(c(this.c));
} else {
setMinimumHeight(c(view3));
}
}
a();
}
@Override // android.widget.FrameLayout, android.view.View
protected void onMeasure(int i, int i2) {
b();
super.onMeasure(i, i2);
int mode = View.MeasureSpec.getMode(i2);
WindowInsetsCompat windowInsetsCompat = this.x;
int e = windowInsetsCompat != null ? windowInsetsCompat.e() : 0;
if (mode != 0 || e <= 0) {
return;
}
super.onMeasure(i, View.MeasureSpec.makeMeasureSpec(getMeasuredHeight() + e, 1073741824));
}
@Override // android.view.View
protected void onSizeChanged(int i, int i2, int i3, int i4) {
super.onSizeChanged(i, i2, i3, i4);
Drawable drawable = this.n;
if (drawable != null) {
drawable.setBounds(0, 0, i, i2);
}
}
public void setCollapsedTitleGravity(int i) {
this.k.b(i);
}
public void setCollapsedTitleTextAppearance(int i) {
this.k.a(i);
}
public void setCollapsedTitleTextColor(int i) {
setCollapsedTitleTextColor(ColorStateList.valueOf(i));
}
public void setCollapsedTitleTypeface(Typeface typeface) {
this.k.a(typeface);
}
public void setContentScrim(Drawable drawable) {
Drawable drawable2 = this.n;
if (drawable2 != drawable) {
if (drawable2 != null) {
drawable2.setCallback(null);
}
this.n = drawable != null ? drawable.mutate() : null;
Drawable drawable3 = this.n;
if (drawable3 != null) {
drawable3.setBounds(0, 0, getWidth(), getHeight());
this.n.setCallback(this);
this.n.setAlpha(this.p);
}
ViewCompat.A(this);
}
}
public void setContentScrimColor(int i) {
setContentScrim(new ColorDrawable(i));
}
public void setContentScrimResource(int i) {
setContentScrim(ContextCompat.c(getContext(), i));
}
public void setExpandedTitleColor(int i) {
setExpandedTitleTextColor(ColorStateList.valueOf(i));
}
public void setExpandedTitleGravity(int i) {
this.k.d(i);
}
public void setExpandedTitleMarginBottom(int i) {
this.i = i;
requestLayout();
}
public void setExpandedTitleMarginEnd(int i) {
this.h = i;
requestLayout();
}
public void setExpandedTitleMarginStart(int i) {
this.f = i;
requestLayout();
}
public void setExpandedTitleMarginTop(int i) {
this.g = i;
requestLayout();
}
public void setExpandedTitleTextAppearance(int i) {
this.k.c(i);
}
public void setExpandedTitleTextColor(ColorStateList colorStateList) {
this.k.b(colorStateList);
}
public void setExpandedTitleTypeface(Typeface typeface) {
this.k.b(typeface);
}
void setScrimAlpha(int i) {
Toolbar toolbar;
if (i != this.p) {
if (this.n != null && (toolbar = this.c) != null) {
ViewCompat.A(toolbar);
}
this.p = i;
ViewCompat.A(this);
}
}
public void setScrimAnimationDuration(long j) {
this.s = j;
}
public void setScrimVisibleHeightTrigger(int i) {
if (this.t != i) {
this.t = i;
a();
}
}
public void setScrimsShown(boolean z) {
a(z, ViewCompat.x(this) && !isInEditMode());
}
public void setStatusBarScrim(Drawable drawable) {
Drawable drawable2 = this.o;
if (drawable2 != drawable) {
if (drawable2 != null) {
drawable2.setCallback(null);
}
this.o = drawable != null ? drawable.mutate() : null;
Drawable drawable3 = this.o;
if (drawable3 != null) {
if (drawable3.isStateful()) {
this.o.setState(getDrawableState());
}
DrawableCompat.a(this.o, ViewCompat.k(this));
this.o.setVisible(getVisibility() == 0, false);
this.o.setCallback(this);
this.o.setAlpha(this.p);
}
ViewCompat.A(this);
}
}
public void setStatusBarScrimColor(int i) {
setStatusBarScrim(new ColorDrawable(i));
}
public void setStatusBarScrimResource(int i) {
setStatusBarScrim(ContextCompat.c(getContext(), i));
}
public void setTitle(CharSequence charSequence) {
this.k.a(charSequence);
c();
}
public void setTitleEnabled(boolean z) {
if (z != this.l) {
this.l = z;
c();
d();
requestLayout();
}
}
@Override // android.view.View
public void setVisibility(int i) {
super.setVisibility(i);
boolean z = i == 0;
Drawable drawable = this.o;
if (drawable != null && drawable.isVisible() != z) {
this.o.setVisible(z, false);
}
Drawable drawable2 = this.n;
if (drawable2 == null || drawable2.isVisible() == z) {
return;
}
this.n.setVisible(z, false);
}
@Override // android.view.View
protected boolean verifyDrawable(Drawable drawable) {
return super.verifyDrawable(drawable) || drawable == this.n || drawable == this.o;
}
public CollapsingToolbarLayout(Context context, AttributeSet attributeSet) {
this(context, attributeSet, 0);
}
public void setCollapsedTitleTextColor(ColorStateList colorStateList) {
this.k.a(colorStateList);
}
public CollapsingToolbarLayout(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
this.a = true;
this.j = new Rect();
this.t = -1;
this.k = new CollapsingTextHelper(this);
this.k.b(AnimationUtils.e);
TypedArray c = ThemeEnforcement.c(context, attributeSet, R$styleable.CollapsingToolbarLayout, i, R$style.Widget_Design_CollapsingToolbar, new int[0]);
this.k.d(c.getInt(R$styleable.CollapsingToolbarLayout_expandedTitleGravity, 8388691));
this.k.b(c.getInt(R$styleable.CollapsingToolbarLayout_collapsedTitleGravity, 8388627));
int dimensionPixelSize = c.getDimensionPixelSize(R$styleable.CollapsingToolbarLayout_expandedTitleMargin, 0);
this.i = dimensionPixelSize;
this.h = dimensionPixelSize;
this.g = dimensionPixelSize;
this.f = dimensionPixelSize;
if (c.hasValue(R$styleable.CollapsingToolbarLayout_expandedTitleMarginStart)) {
this.f = c.getDimensionPixelSize(R$styleable.CollapsingToolbarLayout_expandedTitleMarginStart, 0);
}
if (c.hasValue(R$styleable.CollapsingToolbarLayout_expandedTitleMarginEnd)) {
this.h = c.getDimensionPixelSize(R$styleable.CollapsingToolbarLayout_expandedTitleMarginEnd, 0);
}
if (c.hasValue(R$styleable.CollapsingToolbarLayout_expandedTitleMarginTop)) {
this.g = c.getDimensionPixelSize(R$styleable.CollapsingToolbarLayout_expandedTitleMarginTop, 0);
}
if (c.hasValue(R$styleable.CollapsingToolbarLayout_expandedTitleMarginBottom)) {
this.i = c.getDimensionPixelSize(R$styleable.CollapsingToolbarLayout_expandedTitleMarginBottom, 0);
}
this.l = c.getBoolean(R$styleable.CollapsingToolbarLayout_titleEnabled, true);
setTitle(c.getText(R$styleable.CollapsingToolbarLayout_title));
this.k.c(R$style.TextAppearance_Design_CollapsingToolbar_Expanded);
this.k.a(androidx.appcompat.R$style.TextAppearance_AppCompat_Widget_ActionBar_Title);
if (c.hasValue(R$styleable.CollapsingToolbarLayout_expandedTitleTextAppearance)) {
this.k.c(c.getResourceId(R$styleable.CollapsingToolbarLayout_expandedTitleTextAppearance, 0));
}
if (c.hasValue(R$styleable.CollapsingToolbarLayout_collapsedTitleTextAppearance)) {
this.k.a(c.getResourceId(R$styleable.CollapsingToolbarLayout_collapsedTitleTextAppearance, 0));
}
this.t = c.getDimensionPixelSize(R$styleable.CollapsingToolbarLayout_scrimVisibleHeightTrigger, -1);
this.s = c.getInt(R$styleable.CollapsingToolbarLayout_scrimAnimationDuration, 600);
setContentScrim(c.getDrawable(R$styleable.CollapsingToolbarLayout_contentScrim));
setStatusBarScrim(c.getDrawable(R$styleable.CollapsingToolbarLayout_statusBarScrim));
this.b = c.getResourceId(R$styleable.CollapsingToolbarLayout_toolbarId, -1);
c.recycle();
setWillNotDraw(false);
ViewCompat.a(this, new OnApplyWindowInsetsListener() { // from class: com.google.android.material.appbar.CollapsingToolbarLayout.1
@Override // androidx.core.view.OnApplyWindowInsetsListener
public WindowInsetsCompat a(View view, WindowInsetsCompat windowInsetsCompat) {
return CollapsingToolbarLayout.this.a(windowInsetsCompat);
}
});
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // android.widget.FrameLayout, android.view.ViewGroup
public LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(-1, -1);
}
@Override // android.widget.FrameLayout, android.view.ViewGroup
public FrameLayout.LayoutParams generateLayoutParams(AttributeSet attributeSet) {
return new LayoutParams(getContext(), attributeSet);
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // android.widget.FrameLayout, android.view.ViewGroup
public FrameLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams layoutParams) {
return new LayoutParams(layoutParams);
}
private void c() {
setContentDescription(getTitle());
}
public void a(boolean z, boolean z2) {
if (this.q != z) {
if (z2) {
a(z ? 255 : 0);
} else {
setScrimAlpha(z ? 255 : 0);
}
this.q = z;
}
}
public static class LayoutParams extends FrameLayout.LayoutParams {
int a;
float b;
public LayoutParams(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
this.a = 0;
this.b = 0.5f;
TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R$styleable.CollapsingToolbarLayout_Layout);
this.a = obtainStyledAttributes.getInt(R$styleable.CollapsingToolbarLayout_Layout_layout_collapseMode, 0);
a(obtainStyledAttributes.getFloat(R$styleable.CollapsingToolbarLayout_Layout_layout_collapseParallaxMultiplier, 0.5f));
obtainStyledAttributes.recycle();
}
public void a(float f) {
this.b = f;
}
public LayoutParams(int i, int i2) {
super(i, i2);
this.a = 0;
this.b = 0.5f;
}
public LayoutParams(ViewGroup.LayoutParams layoutParams) {
super(layoutParams);
this.a = 0;
this.b = 0.5f;
}
}
private void a(int i) {
b();
ValueAnimator valueAnimator = this.r;
if (valueAnimator == null) {
this.r = new ValueAnimator();
this.r.setDuration(this.s);
this.r.setInterpolator(i > this.p ? AnimationUtils.c : AnimationUtils.d);
this.r.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { // from class: com.google.android.material.appbar.CollapsingToolbarLayout.2
@Override // android.animation.ValueAnimator.AnimatorUpdateListener
public void onAnimationUpdate(ValueAnimator valueAnimator2) {
CollapsingToolbarLayout.this.setScrimAlpha(((Integer) valueAnimator2.getAnimatedValue()).intValue());
}
});
} else if (valueAnimator.isRunning()) {
this.r.cancel();
}
this.r.setIntValues(this.p, i);
this.r.start();
}
static ViewOffsetHelper d(View view) {
ViewOffsetHelper viewOffsetHelper = (ViewOffsetHelper) view.getTag(R$id.view_offset_helper);
if (viewOffsetHelper != null) {
return viewOffsetHelper;
}
ViewOffsetHelper viewOffsetHelper2 = new ViewOffsetHelper(view);
view.setTag(R$id.view_offset_helper, viewOffsetHelper2);
return viewOffsetHelper2;
}
private View b(View view) {
for (ViewParent parent = view.getParent(); parent != this && parent != null; parent = parent.getParent()) {
if (parent instanceof View) {
view = parent;
}
}
return view;
}
final void a() {
if (this.n == null && this.o == null) {
return;
}
setScrimsShown(getHeight() + this.v < getScrimVisibleHeightTrigger());
}
final int a(View view) {
return ((getHeight() - d(view).a()) - view.getHeight()) - ((FrameLayout.LayoutParams) ((LayoutParams) view.getLayoutParams())).bottomMargin;
}
}