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 {
    private h b;
    private androidx.appcompat.app.b d;
    f e;
    private o.a f;

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

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

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

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

    public void d(IBinder iBinder) {
        h hVar = this.b;
        b.a aVar = new b.a(hVar.u());
        f fVar = new f(aVar.b(), g.a.g.abc_list_menu_item_layout);
        this.e = fVar;
        fVar.k(this);
        this.b.b(this.e);
        aVar.c(this.e.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.d = bVarA;
        bVarA.setOnDismissListener(this);
        WindowManager.LayoutParams attributes = this.d.getWindow().getAttributes();
        attributes.type = 1003;
        if (iBinder != null) {
            attributes.token = iBinder;
        }
        attributes.flags |= 131072;
        this.d.show();
    }

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

    @Override // android.content.DialogInterface.OnDismissListener
    public void onDismiss(DialogInterface dialogInterface) {
        this.e.a(this.b, 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.d.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.d.getWindow()) != null && (decorView = window.getDecorView()) != null && (keyDispatcherState = decorView.getKeyDispatcherState()) != null && keyDispatcherState.isTracking(keyEvent)) {
                this.b.e(true);
                dialogInterface.dismiss();
                return true;
            }
        }
        return this.b.performShortcut(i2, keyEvent, 0);
    }
}
