package xyz.stream.download.execute;

import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.Proxy;
import java.security.SecureRandom;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import xf.c;
import xyz.stream.download.DownloadConstant;
import xyz.stream.download.db.DbHolder;
import xyz.stream.download.info.DownloadInfo;
import xyz.stream.download.info.FileInfo;

/* JADX INFO: loaded from: classes3.dex */
public class DownloadTask implements Runnable {
    public static final String TAG = "Download";

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f36078a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final DownloadInfo f36079b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final FileInfo f36080c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final DbHolder f36081d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f36082e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f36083f = 0;

    public DownloadTask(Context context, DownloadInfo downloadInfo, DbHolder dbHolder) {
        long downloadLocation;
        this.f36078a = context;
        this.f36079b = downloadInfo;
        this.f36081d = dbHolder;
        FileInfo fileInfo = new FileInfo();
        this.f36080c = fileInfo;
        fileInfo.setId(downloadInfo.getUniqueId());
        fileInfo.setDownloadUrl(downloadInfo.getUrl());
        fileInfo.setFilePath(downloadInfo.getFile().getAbsolutePath());
        Objects.toString(fileInfo);
        FileInfo fileInfo2 = dbHolder.getFileInfo(downloadInfo.getUniqueId());
        long j10 = 0;
        if (fileInfo2 != null) {
            downloadLocation = fileInfo2.getDownloadLocation();
            long size = fileInfo2.getSize();
            if (downloadLocation == 0) {
                if (downloadInfo.getFile().exists()) {
                    downloadInfo.getFile().delete();
                }
            } else if (!downloadInfo.getFile().exists()) {
                Objects.toString(downloadInfo.getFile());
                dbHolder.deleteFileInfo(downloadInfo.getUniqueId());
            }
            j10 = size;
            fileInfo.setSize(j10);
            fileInfo.setDownloadLocation(downloadLocation);
            Objects.toString(fileInfo);
        }
        if (downloadInfo.getFile().exists()) {
            downloadInfo.getFile().delete();
        }
        downloadLocation = 0;
        fileInfo.setSize(j10);
        fileInfo.setDownloadLocation(downloadLocation);
        Objects.toString(fileInfo);
    }

    /* JADX WARN: Removed duplicated region for block: B:123:0x0275  */
    /* JADX WARN: Removed duplicated region for block: B:132:0x0287 A[Catch: all -> 0x02ac, TRY_ENTER, TryCatch #3 {all -> 0x02ac, blocks: (B:120:0x026a, B:132:0x0287, B:134:0x0297, B:147:0x02b1), top: B:164:0x026a }] */
    /* JADX WARN: Removed duplicated region for block: B:158:0x02cc A[Catch: IOException -> 0x02c8, TRY_LEAVE, TryCatch #7 {IOException -> 0x02c8, blocks: (B:154:0x02c4, B:158:0x02cc), top: B:167:0x02c4 }] */
    /* JADX WARN: Removed duplicated region for block: B:167:0x02c4 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:210:? A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a() throws Throwable {
        DownloadTask downloadTask;
        Throwable th;
        RandomAccessFile randomAccessFile;
        InputStream inputStream;
        Throwable th2;
        String url;
        OkHttpClient okHttpClient;
        TrustManager[] trustManagerArr;
        Response responseExecute;
        long j10;
        InputStream inputStreamByteStream;
        InputStream inputStream2;
        DbHolder dbHolder = this.f36081d;
        FileInfo fileInfo = this.f36080c;
        fileInfo.setDownloadStatus(43);
        Intent intent = new Intent();
        DownloadInfo downloadInfo = this.f36079b;
        intent.setAction(downloadInfo.getAction());
        intent.putExtra(DownloadConstant.EXTRA_INTENT_DOWNLOAD, fileInfo);
        Context context = this.f36078a;
        context.sendBroadcast(intent);
        try {
            try {
                try {
                    url = downloadInfo.getUrl();
                    okHttpClient = new OkHttpClient();
                    trustManagerArr = new TrustManager[]{new c()};
                } catch (Throwable th3) {
                    th = th3;
                }
                try {
                    try {
                        SSLContext sSLContext = SSLContext.getInstance("SSL");
                        sSLContext.init(null, trustManagerArr, new SecureRandom());
                        SSLSocketFactory socketFactory = sSLContext.getSocketFactory();
                        OkHttpClient.Builder builderProxy = okHttpClient.newBuilder().cache(null).proxy(Proxy.NO_PROXY);
                        TimeUnit timeUnit = TimeUnit.MINUTES;
                        okHttpClient = builderProxy.callTimeout(5L, timeUnit).connectTimeout(5L, timeUnit).readTimeout(5L, timeUnit).writeTimeout(5L, timeUnit).sslSocketFactory(socketFactory, (X509TrustManager) trustManagerArr[0]).hostnameVerifier(new HostnameVerifier(0) { // from class: xf.b
                            @Override // javax.net.ssl.HostnameVerifier
                            public final boolean verify(String str, SSLSession sSLSession) {
                                return true;
                            }
                        }).connectionPool(new ConnectionPool(32, 5L, timeUnit)).build();
                    } catch (Exception e10) {
                        e10.printStackTrace();
                    }
                    responseExecute = okHttpClient.newCall(new Request.Builder().url(url).head().build()).execute();
                } catch (Throwable th4) {
                    th2 = th4;
                    randomAccessFile = null;
                    inputStream = null;
                    th = th2;
                    if (randomAccessFile != null) {
                    }
                    if (inputStream == null) {
                    }
                }
                if (responseExecute.code() != 200 && responseExecute.code() != 206) {
                    fileInfo.setDownloadStatus(47);
                    context.sendBroadcast(intent);
                    return;
                }
                if (responseExecute.header(RtspHeaders.CONTENT_LENGTH) != null) {
                    String strHeader = responseExecute.header(RtspHeaders.CONTENT_LENGTH);
                    Objects.requireNonNull(strHeader);
                    j10 = Long.parseLong(strHeader);
                } else {
                    j10 = 0;
                }
                try {
                    responseExecute.close();
                    if (j10 <= 0) {
                        try {
                            if (downloadInfo.getFile().exists()) {
                                downloadInfo.getFile().delete();
                            }
                            dbHolder.deleteFileInfo(downloadInfo.getUniqueId());
                            fileInfo.setDownloadStatus(47);
                            context.sendBroadcast(intent);
                            return;
                        } catch (Exception e11) {
                            e = e11;
                            randomAccessFile = null;
                            inputStream = null;
                            downloadTask = this;
                            try {
                                e.printStackTrace();
                                if (fileInfo.getDownloadStatus() != 46) {
                                }
                            } catch (Throwable th5) {
                                th2 = th5;
                            }
                        } catch (Throwable th6) {
                            th2 = th6;
                            randomAccessFile = null;
                            inputStream = null;
                        }
                    } else {
                        fileInfo.setSize(j10);
                        RandomAccessFile randomAccessFile2 = new RandomAccessFile(downloadInfo.getFile(), "rwd");
                        try {
                            try {
                                Response responseExecute2 = okHttpClient.newCall(new Request.Builder().url(url).addHeader(RtspHeaders.RANGE, "bytes=" + fileInfo.getDownloadLocation() + "-").build()).execute();
                                if (responseExecute2.body() != null) {
                                    try {
                                        inputStreamByteStream = responseExecute2.body().byteStream();
                                    } catch (Exception e12) {
                                        e = e12;
                                        inputStream = null;
                                        downloadTask = this;
                                        randomAccessFile = randomAccessFile2;
                                        e.printStackTrace();
                                        if (fileInfo.getDownloadStatus() != 46) {
                                        }
                                    }
                                } else {
                                    inputStreamByteStream = null;
                                }
                                if (inputStreamByteStream == null) {
                                    try {
                                        fileInfo.setDownloadStatus(47);
                                        context.sendBroadcast(intent);
                                        try {
                                            randomAccessFile2.close();
                                            if (inputStreamByteStream != null) {
                                                inputStreamByteStream.close();
                                                return;
                                            }
                                            return;
                                        } catch (IOException e13) {
                                            e13.printStackTrace();
                                            return;
                                        }
                                    } catch (Exception e14) {
                                        e = e14;
                                        downloadTask = this;
                                        inputStream = inputStreamByteStream;
                                        randomAccessFile = randomAccessFile2;
                                        e.printStackTrace();
                                        if (fileInfo.getDownloadStatus() != 46) {
                                        }
                                    } catch (Throwable th7) {
                                        th2 = th7;
                                        inputStream = inputStreamByteStream;
                                        randomAccessFile = randomAccessFile2;
                                        th = th2;
                                        if (randomAccessFile != null) {
                                        }
                                        if (inputStream == null) {
                                        }
                                    }
                                } else {
                                    try {
                                        byte[] bArr = new byte[8192];
                                        randomAccessFile2.seek(fileInfo.getDownloadLocation());
                                        long jUptimeMillis = SystemClock.uptimeMillis();
                                        while (true) {
                                            int i10 = inputStreamByteStream.read(bArr);
                                            if (i10 == -1) {
                                                downloadTask = this;
                                                inputStream2 = inputStreamByteStream;
                                                fileInfo.setDownloadStatus(46);
                                                dbHolder.saveFile(fileInfo);
                                                context.sendBroadcast(intent);
                                                randomAccessFile2.close();
                                                inputStream2.close();
                                                return;
                                            }
                                            downloadTask = this;
                                            try {
                                                if (downloadTask.f36082e) {
                                                    try {
                                                        fileInfo.setDownloadStatus(45);
                                                        downloadTask.f36082e = false;
                                                        dbHolder.saveFile(fileInfo);
                                                        context.sendBroadcast(intent);
                                                        okHttpClient.dispatcher().cancelAll();
                                                        responseExecute2.close();
                                                        randomAccessFile2.close();
                                                        inputStreamByteStream.close();
                                                        try {
                                                            randomAccessFile2.close();
                                                            inputStreamByteStream.close();
                                                            return;
                                                        } catch (IOException e15) {
                                                            e15.printStackTrace();
                                                            return;
                                                        }
                                                    } catch (Exception e16) {
                                                        e = e16;
                                                        inputStream = inputStreamByteStream;
                                                        randomAccessFile = randomAccessFile2;
                                                        e.printStackTrace();
                                                        if (fileInfo.getDownloadStatus() != 46) {
                                                        }
                                                    } catch (Throwable th8) {
                                                        th2 = th8;
                                                        inputStream = inputStreamByteStream;
                                                        randomAccessFile = randomAccessFile2;
                                                        th = th2;
                                                        if (randomAccessFile != null) {
                                                        }
                                                        if (inputStream == null) {
                                                        }
                                                    }
                                                } else {
                                                    randomAccessFile2.write(bArr, 0, i10);
                                                    inputStream2 = inputStreamByteStream;
                                                    try {
                                                        fileInfo.setDownloadLocation(fileInfo.getDownloadLocation() + ((long) i10));
                                                        fileInfo.setDownloadStatus(44);
                                                        if (SystemClock.uptimeMillis() - jUptimeMillis >= 1000) {
                                                            jUptimeMillis = SystemClock.uptimeMillis();
                                                            dbHolder.saveFile(fileInfo);
                                                            context.sendBroadcast(intent);
                                                        }
                                                        inputStreamByteStream = inputStream2;
                                                    } catch (Exception e17) {
                                                        e = e17;
                                                        randomAccessFile = randomAccessFile2;
                                                        inputStream = inputStream2;
                                                        e.printStackTrace();
                                                        if (fileInfo.getDownloadStatus() != 46) {
                                                        }
                                                    } catch (Throwable th9) {
                                                        th2 = th9;
                                                        randomAccessFile = randomAccessFile2;
                                                        inputStream = inputStream2;
                                                        th = th2;
                                                        if (randomAccessFile != null) {
                                                        }
                                                        if (inputStream == null) {
                                                        }
                                                    }
                                                }
                                            } catch (Exception e18) {
                                                e = e18;
                                                inputStream2 = inputStreamByteStream;
                                                randomAccessFile = randomAccessFile2;
                                                inputStream = inputStream2;
                                                e.printStackTrace();
                                                if (fileInfo.getDownloadStatus() != 46) {
                                                }
                                            } catch (Throwable th10) {
                                                th2 = th10;
                                                inputStream2 = inputStreamByteStream;
                                                randomAccessFile = randomAccessFile2;
                                                inputStream = inputStream2;
                                                th = th2;
                                                if (randomAccessFile != null) {
                                                }
                                                if (inputStream == null) {
                                                }
                                            }
                                        }
                                    } catch (Exception e19) {
                                        e = e19;
                                        downloadTask = this;
                                    } catch (Throwable th11) {
                                        th2 = th11;
                                    }
                                }
                            } catch (Throwable th12) {
                                th2 = th12;
                                randomAccessFile = randomAccessFile2;
                                inputStream = null;
                            }
                        } catch (Exception e20) {
                            e = e20;
                            downloadTask = this;
                            randomAccessFile = randomAccessFile2;
                            inputStream = null;
                            e.printStackTrace();
                            if (fileInfo.getDownloadStatus() != 46) {
                                if (randomAccessFile != null) {
                                    try {
                                        randomAccessFile.close();
                                    } catch (IOException e21) {
                                        e21.printStackTrace();
                                        return;
                                    }
                                }
                                if (inputStream != null) {
                                    inputStream.close();
                                    return;
                                }
                                return;
                            }
                            e.getMessage();
                            dbHolder.saveFile(fileInfo);
                            int i11 = downloadTask.f36083f + 1;
                            downloadTask.f36083f = i11;
                            if (i11 >= 20) {
                                fileInfo.setDownloadStatus(47);
                                context.sendBroadcast(intent);
                                if (randomAccessFile != null) {
                                    randomAccessFile.close();
                                }
                                if (inputStream != null) {
                                    inputStream.close();
                                    return;
                                }
                                return;
                            }
                            a();
                            if (randomAccessFile != null) {
                                try {
                                    randomAccessFile.close();
                                } catch (IOException e22) {
                                    e22.printStackTrace();
                                    return;
                                }
                            }
                            if (inputStream != null) {
                                inputStream.close();
                                return;
                            }
                            return;
                        }
                        th = th2;
                    }
                } catch (Exception e23) {
                    e = e23;
                    downloadTask = this;
                    randomAccessFile = null;
                    inputStream = null;
                    e.printStackTrace();
                    if (fileInfo.getDownloadStatus() != 46) {
                    }
                } catch (Throwable th13) {
                    th = th13;
                }
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException e24) {
                        e24.printStackTrace();
                        throw th;
                    }
                }
                if (inputStream == null) {
                    throw th;
                }
                inputStream.close();
                throw th;
                th = th;
                randomAccessFile = null;
                inputStream = null;
                if (randomAccessFile != null) {
                }
                if (inputStream == null) {
                }
            } catch (Exception e25) {
                e = e25;
                downloadTask = this;
            }
        } catch (IOException e26) {
            e26.printStackTrace();
        }
    }

    public DownloadInfo getDownLoadInfo() {
        return this.f36079b;
    }

    public FileInfo getFileInfo() {
        return this.f36080c;
    }

    public int getStatus() {
        FileInfo fileInfo = this.f36080c;
        if (fileInfo != null) {
            return fileInfo.getDownloadStatus();
        }
        return 47;
    }

    public void pause() {
        this.f36082e = true;
    }

    @Override // java.lang.Runnable
    public void run() throws Throwable {
        a();
    }

    public void sendBroadcast(Intent intent) {
        this.f36078a.sendBroadcast(intent);
    }

    public void setFileStatus(int i10) {
        this.f36080c.setDownloadStatus(i10);
    }
}
