package com.google.android.material.bottomsheet;

import a3.g0;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.s1;
import e.o0;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes2.dex */
public final class o extends o0 {
    public boolean Q;
    public m R;

    @Nullable
    private n9.g backOrchestrator;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public BottomSheetBehavior f22326f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public FrameLayout f22327g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public CoordinatorLayout f22328h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public FrameLayout f22329i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f22330j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public boolean f22331k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public boolean f22332l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public n f22333m;

    private View wrapInBottomSheet(int i10, @Nullable View view, @Nullable ViewGroup.LayoutParams layoutParams) {
        f();
        CoordinatorLayout coordinatorLayout = (CoordinatorLayout) this.f22327g.findViewById(x8.g.coordinator);
        if (i10 != 0 && view == null) {
            view = getLayoutInflater().inflate(i10, (ViewGroup) coordinatorLayout, false);
        }
        if (this.Q) {
            s1.setOnApplyWindowInsetsListener(this.f22327g, new j(this));
        }
        this.f22329i.removeAllViews();
        if (layoutParams == null) {
            this.f22329i.addView(view);
        } else {
            this.f22329i.addView(view, layoutParams);
        }
        coordinatorLayout.findViewById(x8.g.touch_outside).setOnClickListener(new k(this));
        s1.setAccessibilityDelegate(this.f22329i, new g0(this, 3));
        this.f22329i.setOnTouchListener(new l());
        return this.f22327g;
    }

    @Override // android.app.Dialog, android.content.DialogInterface
    public final void cancel() {
        if (this.f22326f == null) {
            f();
        }
        super.cancel();
    }

    public final void f() {
        if (this.f22327g == null) {
            FrameLayout frameLayout = (FrameLayout) View.inflate(getContext(), x8.i.design_bottom_sheet_dialog, null);
            this.f22327g = frameLayout;
            this.f22328h = (CoordinatorLayout) frameLayout.findViewById(x8.g.coordinator);
            FrameLayout frameLayout2 = (FrameLayout) this.f22327g.findViewById(x8.g.design_bottom_sheet);
            this.f22329i = frameLayout2;
            BottomSheetBehavior bottomSheetBehaviorB = BottomSheetBehavior.B(frameLayout2);
            this.f22326f = bottomSheetBehaviorB;
            m mVar = this.R;
            ArrayList arrayList = bottomSheetBehaviorB.f22294w0;
            if (!arrayList.contains(mVar)) {
                arrayList.add(mVar);
            }
            this.f22326f.H(this.f22330j);
            this.backOrchestrator = new n9.g(this.f22326f, this.f22329i);
        }
    }

    public final void g() {
        TypedArray typedArrayObtainStyledAttributes = getContext().getTheme().obtainStyledAttributes(new int[]{x8.c.enableEdgeToEdge});
        this.Q = typedArrayObtainStyledAttributes.getBoolean(0, false);
        typedArrayObtainStyledAttributes.recycle();
    }

    @Override // android.app.Dialog, android.view.Window.Callback
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        if (window != null) {
            boolean z10 = this.Q && Color.alpha(window.getNavigationBarColor()) < 255;
            FrameLayout frameLayout = this.f22327g;
            if (frameLayout != null) {
                frameLayout.setFitsSystemWindows(!z10);
            }
            CoordinatorLayout coordinatorLayout = this.f22328h;
            if (coordinatorLayout != null) {
                coordinatorLayout.setFitsSystemWindows(!z10);
            }
            o6.b.B(window, !z10);
            n nVar = this.f22333m;
            if (nVar != null) {
                nVar.setWindow(window);
            }
        }
        n9.g gVar = this.backOrchestrator;
        if (gVar == null) {
            return;
        }
        if (this.f22330j) {
            gVar.b(false);
        } else {
            gVar.c();
        }
    }

    @Override // e.o0, androidx.activity.r, android.app.Dialog
    public final void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Window window = getWindow();
        if (window != null) {
            window.setStatusBarColor(0);
            window.addFlags(Integer.MIN_VALUE);
            window.setLayout(-1, -1);
        }
    }

    @Override // android.app.Dialog, android.view.Window.Callback
    public final void onDetachedFromWindow() {
        n nVar = this.f22333m;
        if (nVar != null) {
            nVar.setWindow(null);
        }
        n9.g gVar = this.backOrchestrator;
        if (gVar != null) {
            gVar.c();
        }
    }

    @Override // androidx.activity.r, android.app.Dialog
    public final void onStart() {
        super.onStart();
        BottomSheetBehavior bottomSheetBehavior = this.f22326f;
        if (bottomSheetBehavior == null || bottomSheetBehavior.f22286o0 != 5) {
            return;
        }
        bottomSheetBehavior.J(4);
    }

    @Override // android.app.Dialog
    public final void setCancelable(boolean z10) {
        n9.g gVar;
        super.setCancelable(z10);
        if (this.f22330j != z10) {
            this.f22330j = z10;
            BottomSheetBehavior bottomSheetBehavior = this.f22326f;
            if (bottomSheetBehavior != null) {
                bottomSheetBehavior.H(z10);
            }
            if (getWindow() == null || (gVar = this.backOrchestrator) == null) {
                return;
            }
            if (this.f22330j) {
                gVar.b(false);
            } else {
                gVar.c();
            }
        }
    }

    @Override // android.app.Dialog
    public final void setCanceledOnTouchOutside(boolean z10) {
        super.setCanceledOnTouchOutside(z10);
        if (z10 && !this.f22330j) {
            this.f22330j = true;
        }
        this.f22331k = z10;
        this.f22332l = true;
    }

    @Override // e.o0, androidx.activity.r, android.app.Dialog
    public final void setContentView(int i10) {
        super.setContentView(wrapInBottomSheet(i10, null, null));
    }

    @Override // e.o0, androidx.activity.r, android.app.Dialog
    public final void setContentView(View view) {
        super.setContentView(wrapInBottomSheet(0, view, null));
    }

    @Override // e.o0, androidx.activity.r, android.app.Dialog
    public final void setContentView(View view, ViewGroup.LayoutParams layoutParams) {
        super.setContentView(wrapInBottomSheet(0, view, layoutParams));
    }
}
