package io.flutter.view;

import android.os.Build;
import android.os.Parcel;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityRecord;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/* JADX INFO: loaded from: classes.dex */
public final class k {

    @Nullable
    private final Field childNodeIdsField;

    @Nullable
    private final Method getChildId;

    @Nullable
    private final Method getParentNodeId;

    @Nullable
    private final Method getRecordSourceNodeId;

    @Nullable
    private final Method getSourceNodeId;

    @Nullable
    private final Method longArrayGetIndex;

    /* JADX WARN: Multi-variable type inference failed */
    public k() throws NoSuchMethodException {
        Method method;
        Method method2;
        Method method3;
        Field field;
        Method method4;
        Method method5;
        Method method6 = null;
        try {
            method = AccessibilityNodeInfo.class.getMethod("getSourceNodeId", new Class[0]);
        } catch (NoSuchMethodException unused) {
            method = null;
        }
        try {
            method2 = AccessibilityRecord.class.getMethod("getSourceNodeId", new Class[0]);
        } catch (NoSuchMethodException unused2) {
            method2 = null;
        }
        if (Build.VERSION.SDK_INT > 26) {
            try {
                Field declaredField = AccessibilityNodeInfo.class.getDeclaredField("mChildNodeIds");
                declaredField.setAccessible(true);
                method4 = Class.forName("android.util.LongArray").getMethod("get", Integer.TYPE);
                field = declaredField;
                method3 = null;
            } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | NullPointerException unused3) {
                method3 = null;
                field = null;
                method4 = field;
            }
            this.getSourceNodeId = method;
            this.getParentNodeId = method6;
            this.getRecordSourceNodeId = method2;
            this.getChildId = method3;
            this.childNodeIdsField = field;
            this.longArrayGetIndex = method4;
        }
        try {
            method5 = AccessibilityNodeInfo.class.getMethod("getParentNodeId", new Class[0]);
        } catch (NoSuchMethodException unused4) {
            method5 = null;
        }
        try {
            method3 = AccessibilityNodeInfo.class.getMethod("getChildId", Integer.TYPE);
            field = null;
        } catch (NoSuchMethodException unused5) {
            method3 = null;
            field = null;
        }
        method6 = method5;
        method4 = field;
        this.getSourceNodeId = method;
        this.getParentNodeId = method6;
        this.getRecordSourceNodeId = method2;
        this.getChildId = method3;
        this.childNodeIdsField = field;
        this.longArrayGetIndex = method4;
    }

    public static boolean e(int i10, long j10) {
        return (j10 & (1 << i10)) != 0;
    }

    /* JADX INFO: Access modifiers changed from: private */
    @Nullable
    public Long getChildId(@NonNull AccessibilityNodeInfo accessibilityNodeInfo, int i10) {
        Method method = this.getChildId;
        if (method == null && (this.childNodeIdsField == null || this.longArrayGetIndex == null)) {
            return null;
        }
        try {
            return method != null ? (Long) method.invoke(accessibilityNodeInfo, Integer.valueOf(i10)) : Long.valueOf(((Long) this.longArrayGetIndex.invoke(this.childNodeIdsField.get(accessibilityNodeInfo), Integer.valueOf(i10))).longValue());
        } catch (ArrayIndexOutOfBoundsException | IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    @Nullable
    public Long getParentNodeId(@NonNull AccessibilityNodeInfo accessibilityNodeInfo) {
        Method method = this.getParentNodeId;
        if (method != null) {
            try {
                return Long.valueOf(((Long) method.invoke(accessibilityNodeInfo, new Object[0])).longValue());
            } catch (IllegalAccessException | InvocationTargetException unused) {
            }
        }
        return yoinkParentIdFromParcel(accessibilityNodeInfo);
    }

    /* JADX INFO: Access modifiers changed from: private */
    @Nullable
    public Long getRecordSourceNodeId(@NonNull AccessibilityRecord accessibilityRecord) {
        Method method = this.getRecordSourceNodeId;
        if (method == null) {
            return null;
        }
        try {
            return (Long) method.invoke(accessibilityRecord, new Object[0]);
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    @Nullable
    public Long getSourceNodeId(@NonNull AccessibilityNodeInfo accessibilityNodeInfo) {
        Method method = this.getSourceNodeId;
        if (method == null) {
            return null;
        }
        try {
            return (Long) method.invoke(accessibilityNodeInfo, new Object[0]);
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    @Nullable
    private static Long yoinkParentIdFromParcel(AccessibilityNodeInfo accessibilityNodeInfo) {
        if (Build.VERSION.SDK_INT < 26) {
            return null;
        }
        AccessibilityNodeInfo accessibilityNodeInfoObtain = AccessibilityNodeInfo.obtain(accessibilityNodeInfo);
        Parcel parcelObtain = Parcel.obtain();
        parcelObtain.setDataPosition(0);
        accessibilityNodeInfoObtain.writeToParcel(parcelObtain, 0);
        parcelObtain.setDataPosition(0);
        long j10 = parcelObtain.readLong();
        if (e(0, j10)) {
            parcelObtain.readInt();
        }
        if (e(1, j10)) {
            parcelObtain.readLong();
        }
        if (e(2, j10)) {
            parcelObtain.readInt();
        }
        Long lValueOf = e(3, j10) ? Long.valueOf(parcelObtain.readLong()) : null;
        parcelObtain.recycle();
        return lValueOf;
    }
}
