package com.google.android.play.core.b;

import X.0HH;
import X.3GY;
import X.AnonymousClass9EH;
import X.C30738n2;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ComponentInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Pair;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;

public final class an {
    static {
        Covode.recordClassIndex(44237);
    }

    public static <T> am<T> LIZ(Object obj, String str, Class<T> cls) {
        return new am<>(obj, LIZ(obj, str), cls);
    }

    public static <R, P0> R LIZ(Class cls, Class<R> cls2, Class<P0> cls3, P0 p0) {
        try {
            return cls2.cast(LIZ(LIZ((Class<?>) cls, "isDexOptNeeded", (Class<?>[]) new Class[]{cls3}), (Object) null, new Object[]{p0}));
        } catch (Exception e) {
            throw new ao(0HH.LIZ("Failed to invoke static method %s on type %s", new Object[]{"isDexOptNeeded", cls}), e);
        }
    }

    public static <R, P0, P1> R LIZ(Class cls, Class<R> cls2, Class<P0> cls3, P0 p0, Class<P1> cls4, P1 p1) {
        try {
            return cls2.cast(LIZ(LIZ((Class<?>) cls, "optimizedPathFor", (Class<?>[]) new Class[]{cls3, cls4}), (Object) null, new Object[]{p0, p1}));
        } catch (Exception e) {
            throw new ao(0HH.LIZ("Failed to invoke static method %s on type %s", new Object[]{"optimizedPathFor", cls}), e);
        }
    }

    public static <R, P0> R LIZ(Object obj, String str, Class<R> cls, Class<P0> cls2, P0 p0) {
        try {
            return cls.cast(LIZ(LIZ(obj, str, (Class<?>[]) new Class[]{cls2}), obj, new Object[]{p0}));
        } catch (Exception e) {
            throw new ao(0HH.LIZ("Failed to invoke method %s on an object of type %s", new Object[]{str, obj.getClass()}), e);
        }
    }

    public static <R, P0, P1, P2> R LIZ(Object obj, String str, Class<R> cls, Class<P0> cls2, P0 p0, Class<P1> cls3, P1 p1, Class<P2> cls4, P2 p2) {
        try {
            return cls.cast(LIZ(LIZ(obj, str, (Class<?>[]) new Class[]{cls2, cls3, cls4}), obj, new Object[]{p0, p1, p2}));
        } catch (Exception e) {
            throw new ao(0HH.LIZ("Failed to invoke method %s on an object of type %s", new Object[]{str, obj.getClass()}), e);
        }
    }

    public static Object LIZ(Method method, Object obj, Object[] objArr) {
        3GY.LIZ(110000);
        Method method2 = method;
        Pair LIZ = 3GY.LIZ((Object) null, method2, new Object[]{obj, objArr}, 110000, "java.lang.Object", true, 1117413940, 2);
        return ((Boolean) LIZ.first).booleanValue() ? LIZ.second : method2.invoke(obj, objArr);
    }

    public static Field LIZ(Object obj, String str) {
        Class cls = obj.getClass();
        while (cls != null) {
            try {
                Field declaredField = cls.getDeclaredField(str);
                if (!declaredField.isAccessible()) {
                    declaredField.setAccessible(true);
                }
                return declaredField;
            } catch (NoSuchFieldException unused) {
                cls = cls.getSuperclass();
            }
        }
        throw new ao(0HH.LIZ("Failed to find a field named %s on an object of instance %s", new Object[]{str, obj.getClass().getName()}));
    }

    public static Method LIZ(Class<?> cls, String str, Class<?>... clsArr) {
        Class<?> cls2 = cls;
        while (cls2 != null) {
            try {
                Method declaredMethod = cls2.getDeclaredMethod(str, clsArr);
                if (!declaredMethod.isAccessible()) {
                    declaredMethod.setAccessible(true);
                }
                return declaredMethod;
            } catch (NoSuchMethodException unused) {
                cls2 = cls2.getSuperclass();
            }
        }
        throw new ao(0HH.LIZ("Could not find a method named %s with parameters %s in type %s", new Object[]{str, Arrays.asList(clsArr), cls}));
    }

    public static Method LIZ(Object obj, String str, Class<?>... clsArr) {
        return LIZ(obj.getClass(), str, clsArr);
    }

    public static void LIZ(ax axVar, InputStream inputStream, OutputStream outputStream, long j) {
        MethodCollector.i(18801);
        byte[] bArr = new byte[16384];
        DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(inputStream, 4096));
        int readInt = dataInputStream.readInt();
        if (readInt != -771763713) {
            String valueOf = String.valueOf(0HH.LIZ("%x", new Object[]{Integer.valueOf(readInt)}));
            aw awVar = new aw(valueOf.length() != 0 ? "Unexpected magic=".concat(valueOf) : new String("Unexpected magic="));
            MethodCollector.o(18801);
            throw awVar;
        }
        int read = dataInputStream.read();
        if (read == 4) {
            long j2 = 0;
            while (true) {
                long j3 = j - j2;
                OutputStream outputStream2 = outputStream;
                try {
                    int read2 = dataInputStream.read();
                    if (read2 == -1) {
                        IOException iOException = new IOException("Patch file overrun");
                        MethodCollector.o(18801);
                        throw iOException;
                    } else if (read2 != 0) {
                        ax axVar2 = axVar;
                        switch (read2) {
                            case 247:
                                read2 = dataInputStream.readUnsignedShort();
                                LIZ(bArr, dataInputStream, outputStream2, read2, j3);
                                break;
                            case 248:
                                read2 = dataInputStream.readInt();
                                LIZ(bArr, dataInputStream, outputStream2, read2, j3);
                                break;
                            case 249:
                                long readUnsignedShort = (long) dataInputStream.readUnsignedShort();
                                read2 = dataInputStream.read();
                                if (read2 != -1) {
                                    LIZ(bArr, axVar2, outputStream2, readUnsignedShort, read2, j3);
                                    break;
                                } else {
                                    IOException iOException2 = new IOException("Unexpected end of patch");
                                    MethodCollector.o(18801);
                                    throw iOException2;
                                }
                            case 250:
                                read2 = dataInputStream.readUnsignedShort();
                                LIZ(bArr, axVar2, outputStream2, (long) dataInputStream.readUnsignedShort(), read2, j3);
                                break;
                            case 251:
                                read2 = dataInputStream.readInt();
                                LIZ(bArr, axVar2, outputStream2, (long) dataInputStream.readUnsignedShort(), read2, j3);
                                break;
                            case 252:
                                long readInt2 = (long) dataInputStream.readInt();
                                read2 = dataInputStream.read();
                                if (read2 != -1) {
                                    LIZ(bArr, axVar2, outputStream2, readInt2, read2, j3);
                                    break;
                                } else {
                                    IOException iOException3 = new IOException("Unexpected end of patch");
                                    MethodCollector.o(18801);
                                    throw iOException3;
                                }
                            case 253:
                                read2 = dataInputStream.readUnsignedShort();
                                LIZ(bArr, axVar2, outputStream2, (long) dataInputStream.readInt(), read2, j3);
                                break;
                            case 254:
                                read2 = dataInputStream.readInt();
                                LIZ(bArr, axVar2, outputStream2, (long) dataInputStream.readInt(), read2, j3);
                                break;
                            case 255:
                                long readLong = dataInputStream.readLong();
                                read2 = dataInputStream.readInt();
                                LIZ(bArr, axVar2, outputStream2, readLong, read2, j3);
                                break;
                            default:
                                LIZ(bArr, dataInputStream, outputStream2, read2, j3);
                                break;
                        }
                        j2 += (long) read2;
                    } else {
                        return;
                    }
                } finally {
                    outputStream2.flush();
                    MethodCollector.o(18801);
                }
            }
        } else {
            StringBuilder sb = new StringBuilder(30);
            sb.append("Unexpected version=");
            sb.append(read);
            aw awVar2 = new aw(sb.toString());
            MethodCollector.o(18801);
            throw awVar2;
        }
    }

    public static <T> void LIZ(T t) {
        Objects.requireNonNull(t, "Cannot return null from a non-@Nullable @Provides method");
    }

    public static <T> void LIZ(T t, Class<T> cls) {
        if (t == null) {
            throw new IllegalStateException(String.valueOf(cls.getCanonicalName()).concat(" must be set"));
        }
    }

    public static void LIZ(byte[] bArr, ax axVar, OutputStream outputStream, long j, int i, long j2) {
        InputStream LIZIZ;
        int i2 = i;
        MethodCollector.i(18725);
        if (i2 >= 0) {
            long j3 = j;
            if (j3 >= 0) {
                long j4 = (long) i2;
                if (j4 <= j2) {
                    try {
                        LIZIZ = new ay(axVar, j3, j4).LIZIZ();
                        while (i2 > 0) {
                            int min = Math.min(i2, 16384);
                            int i3 = 0;
                            while (i3 < min) {
                                int read = LIZIZ.read(bArr, i3, min - i3);
                                if (read != -1) {
                                    i3 += read;
                                } else {
                                    IOException iOException = new IOException("truncated input stream");
                                    MethodCollector.o(18725);
                                    throw iOException;
                                }
                            }
                            outputStream.write(bArr, 0, min);
                            i2 -= min;
                        }
                        LIZIZ.close();
                        MethodCollector.o(18725);
                        return;
                    } catch (EOFException e) {
                        IOException iOException2 = new IOException("patch underrun", e);
                        MethodCollector.o(18725);
                        throw iOException2;
                    } catch (Throwable th) {
                        bf.LIZ(th, th);
                    }
                } else {
                    IOException iOException3 = new IOException("Output length overrun");
                    MethodCollector.o(18725);
                    throw iOException3;
                }
            } else {
                IOException iOException4 = new IOException("inputOffset negative");
                MethodCollector.o(18725);
                throw iOException4;
            }
        } else {
            IOException iOException5 = new IOException("copyLength negative");
            MethodCollector.o(18725);
            throw iOException5;
        }
        MethodCollector.o(18725);
        throw th;
    }

    public static void LIZ(byte[] bArr, DataInputStream dataInputStream, OutputStream outputStream, int i, long j) {
        MethodCollector.i(18659);
        if (i < 0) {
            IOException iOException = new IOException("copyLength negative");
            MethodCollector.o(18659);
            throw iOException;
        } else if (((long) i) <= j) {
            while (i > 0) {
                try {
                    int min = Math.min(i, 16384);
                    dataInputStream.readFully(bArr, 0, min);
                    outputStream.write(bArr, 0, min);
                    i -= min;
                } catch (EOFException unused) {
                    IOException iOException2 = new IOException("patch underrun");
                    MethodCollector.o(18659);
                    throw iOException2;
                }
            }
            MethodCollector.o(18659);
        } else {
            IOException iOException3 = new IOException("Output length overrun");
            MethodCollector.o(18659);
            throw iOException3;
        }
    }

    public static <T> am LIZIZ(Object obj, String str, Class<T> cls) {
        return new am(obj, LIZ(obj, str), cls, (byte) 0);
    }

    public static void LIZ(PackageManager packageManager, ComponentName componentName, int i) {
        PackageInfo packageInfo;
        int componentEnabledSetting = packageManager.getComponentEnabledSetting(componentName);
        if (componentEnabledSetting != 1) {
            if (componentEnabledSetting != 2) {
                String packageName = componentName.getPackageName();
                String className = componentName.getClassName();
                int i2 = i | 512;
                try {
                    Context LIZ = AnonymousClass9EH.LJJIFFI.LIZ();
                    if (C30738n2.LJIIIZ && TextUtils.equals(packageName, LIZ.getPackageName()) && i2 == 128) {
                        if (C30738n2.LIZIZ == null) {
                            C30738n2.LIZIZ = packageManager.getPackageInfo(packageName, i2);
                        }
                        packageInfo = C30738n2.LIZIZ;
                    } else if (C30738n2.LJIIIZ && TextUtils.equals(packageName, LIZ.getPackageName()) && i2 == 0) {
                        if (C30738n2.LIZLLL == null) {
                            C30738n2.LIZLLL = packageManager.getPackageInfo(packageName, i2);
                        }
                        packageInfo = C30738n2.LIZLLL;
                    } else if (C30738n2.LJIIIZ && TextUtils.equals(packageName, LIZ.getPackageName()) && i2 == 64) {
                        if (C30738n2.LJ == null) {
                            C30738n2.LJ = packageManager.getPackageInfo(packageName, i2);
                        }
                        packageInfo = C30738n2.LJ;
                    } else if (!C30738n2.LJIIIZ || !TextUtils.equals(packageName, LIZ.getPackageName()) || i2 != 8) {
                        packageInfo = packageManager.getPackageInfo(packageName, i2);
                    } else {
                        if (C30738n2.LJFF == null) {
                            C30738n2.LJFF = packageManager.getPackageInfo(packageName, i2);
                        }
                        packageInfo = C30738n2.LJFF;
                    }
                    ComponentInfo[][] componentInfoArr = {packageInfo.activities, packageInfo.services, packageInfo.providers};
                    int i3 = 0;
                    while (true) {
                        ComponentInfo[] componentInfoArr2 = componentInfoArr[i3];
                        if (componentInfoArr2 != null) {
                            int length = componentInfoArr2.length;
                            int i4 = 0;
                            while (true) {
                                if (i4 >= length) {
                                    break;
                                }
                                ComponentInfo componentInfo = componentInfoArr2[i4];
                                if (!componentInfo.name.equals(className)) {
                                    i4++;
                                } else if (componentInfo.isEnabled()) {
                                    return;
                                }
                            }
                        }
                        i3++;
                        if (i3 >= 3) {
                            break;
                        }
                    }
                } catch (PackageManager.NameNotFoundException unused) {
                }
            }
            packageManager.setComponentEnabledSetting(componentName, 1, 1);
        }
    }
}
