package androidx.appcompat.view.menu;

import android.content.DialogInterface;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.b;
import androidx.appcompat.view.menu.o;

/* JADX INFO: compiled from: MenuDialogHelper.java */
/* JADX INFO: loaded from: classes.dex */
class i implements DialogInterface.OnKeyListener, DialogInterface.OnClickListener, DialogInterface.OnDismissListener, o.a {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private h f245b;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private androidx.appcompat.app.b f246f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    f f247g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private o.a f248h;

    public i(h hVar) {
        this.f245b = hVar;
    }

    @Override // androidx.appcompat.view.menu.o.a
    public void a(h hVar, boolean z) {
        if (z || hVar == this.f245b) {
            c();
        }
        o.a aVar = this.f248h;
        if (aVar != null) {
            aVar.a(hVar, z);
        }
    }

    @Override // androidx.appcompat.view.menu.o.a
    public boolean b(h hVar) {
        o.a aVar = this.f248h;
        if (aVar != null) {
            return aVar.b(hVar);
        }
        return false;
    }

    public void c() {
        androidx.appcompat.app.b bVar = this.f246f;
        if (bVar != null) {
            bVar.dismiss();
        }
    }

    public void d(IBinder iBinder) {
        h hVar = this.f245b;
        b.a aVar = new b.a(hVar.u());
        f fVar = new f(aVar.b(), c.a.g.abc_list_menu_item_layout);
        this.f247g = fVar;
        fVar.k(this);
        this.f245b.b(this.f247g);
        aVar.c(this.f247g.b(), this);
        View viewY = hVar.y();
        if (viewY != null) {
            aVar.d(viewY);
        } else {
            aVar.e(hVar.w());
            aVar.g(hVar.x());
        }
        aVar.f(this);
        androidx.appcompat.app.b bVarA = aVar.a();
        this.f246f = bVarA;
        bVarA.setOnDismissListener(this);
        WindowManager.LayoutParams attributes = this.f246f.getWindow().getAttributes();
        attributes.type = 1003;
        if (iBinder != null) {
            attributes.token = iBinder;
        }
        attributes.flags |= 131072;
        this.f246f.show();
    }

    @Override // android.content.DialogInterface.OnClickListener
    public void onClick(DialogInterface dialogInterface, int i2) {
        this.f245b.L((j) this.f247g.b().getItem(i2), 0);
    }

    @Override // android.content.DialogInterface.OnDismissListener
    public void onDismiss(DialogInterface dialogInterface) {
        this.f247g.a(this.f245b, true);
    }

    @Override // android.content.DialogInterface.OnKeyListener
    public boolean onKey(DialogInterface dialogInterface, int i2, KeyEvent keyEvent) {
        Window window;
        View decorView;
        KeyEvent.DispatcherState keyDispatcherState;
        View decorView2;
        KeyEvent.DispatcherState keyDispatcherState2;
        if (i2 == 82 || i2 == 4) {
            if (keyEvent.getAction() == 0 && keyEvent.getRepeatCount() == 0) {
                Window window2 = this.f246f.getWindow();
                if (window2 != null && (decorView2 = window2.getDecorView()) != null && (keyDispatcherState2 = decorView2.getKeyDispatcherState()) != null) {
                    keyDispatcherState2.startTracking(keyEvent, this);
                    return true;
                }
            } else if (keyEvent.getAction() == 1 && !keyEvent.isCanceled() && (window = this.f246f.getWindow()) != null && (decorView = window.getDecorView()) != null && (keyDispatcherState = decorView.getKeyDispatcherState()) != null && keyDispatcherState.isTracking(keyEvent)) {
                this.f245b.e(true);
                dialogInterface.dismiss();
                return true;
            }
        }
        return this.f245b.performShortcut(i2, keyEvent, 0);
    }
}
