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

import X.C26058fU;
import X.TSt;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.util.Log;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.google.android.play.core.d.a;
import com.google.android.play.core.d.d;
import com.google.android.play.core.d.q;
import com.google.android.play.core.e.ab;
import com.google.android.play.core.e.z;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.List;
import java.util.concurrent.Executor;

public final class o implements ab {
    public final Context LIZ;
    public final p LIZIZ;
    public final d LIZJ;
    public final Executor LIZLLL;
    public final q LJ;

    static {
        Covode.recordClassIndex(44296);
    }

    public o(Context context, Executor executor, p pVar, d dVar, q qVar) {
        this.LIZ = context;
        this.LIZJ = dVar;
        this.LIZIZ = pVar;
        this.LIZLLL = executor;
        this.LJ = qVar;
    }

    public static String LIZ(Intent intent, String str) {
        try {
            return intent.getStringExtra(str);
        } catch (Exception unused) {
            return null;
        }
    }

    public static int LIZ(String str, String str2, Throwable th) {
        TSt.LIZ(th, str + " : " + str2, C26058fU.LIZ());
        return Log.e(str, str2, th);
    }

    public static int LIZ(String str, String str2) {
        TSt.LIZ(str + " : " + str2, C26058fU.LIZ());
        return Log.e(str, str2);
    }

    public final void LIZ(List<Intent> list, z zVar) {
        if (a.LIZ.get() != null) {
            this.LIZLLL.execute(new n(this, list, zVar));
            return;
        }
        throw new IllegalStateException("Ingestion should only be called in SplitCompat mode.");
    }

    public final Integer LIZ(List<Intent> list) {
        FileLock fileLock;
        FileOutputStream fileOutputStream;
        MethodCollector.i(18516);
        try {
            FileChannel channel = new RandomAccessFile(new File(this.LIZJ.LIZLLL(), "lock.tmp"), "rw").getChannel();
            Integer num = null;
            try {
                fileLock = channel.tryLock();
            } catch (OverlappingFileLockException unused) {
                fileLock = null;
            }
            if (fileLock != null) {
                int i = -11;
                try {
                    for (Intent next : list) {
                        String LIZ2 = LIZ(next, "split_id");
                        AssetFileDescriptor openAssetFileDescriptor = this.LIZ.getContentResolver().openAssetFileDescriptor(next.getData(), "r");
                        File LIZ3 = d.LIZ(this.LIZJ.LIZIZ(), d.LIZLLL(LIZ2));
                        if (!LIZ3.exists() || LIZ3.length() == openAssetFileDescriptor.getLength()) {
                            if (!LIZ3.exists()) {
                            }
                        }
                        if (!this.LIZJ.LIZ(LIZ2).exists()) {
                            BufferedInputStream bufferedInputStream = new BufferedInputStream(openAssetFileDescriptor.createInputStream());
                            try {
                                fileOutputStream = new FileOutputStream(LIZ3);
                                byte[] bArr = new byte[4096];
                                while (true) {
                                    int read = bufferedInputStream.read(bArr);
                                    if (read <= 0) {
                                        break;
                                    }
                                    fileOutputStream.write(bArr, 0, read);
                                }
                                fileOutputStream.close();
                                bufferedInputStream.close();
                            } catch (Throwable th) {
                                bufferedInputStream.close();
                                MethodCollector.o(18516);
                                throw th;
                            }
                        }
                    }
                    try {
                        if (!this.LIZIZ.LIZ()) {
                            LIZ("SplitCompat", "Split verification failed.");
                        } else {
                            i = 0;
                        }
                    } catch (Exception e) {
                        LIZ("SplitCompat", "Error verifying splits.", e);
                    } catch (Throwable th2) {
                        if (channel != null) {
                            channel.close();
                        }
                        MethodCollector.o(18516);
                        throw th2;
                    }
                } catch (Exception e2) {
                    LIZ("SplitCompat", "Error copying splits.", e2);
                    i = -13;
                } catch (Throwable th3) {
                    bf.LIZ(th, th3);
                }
                num = Integer.valueOf(i);
                fileLock.release();
            }
            if (channel != null) {
                channel.close();
            }
            MethodCollector.o(18516);
            return num;
            MethodCollector.o(18516);
            throw th;
        } catch (Exception e3) {
            LIZ("SplitCompat", "Error locking files.", e3);
            MethodCollector.o(18516);
            return -13;
        } catch (Throwable th4) {
            bf.LIZ(th2, th4);
        }
    }
}
