package com.baidu.license.download.a; import android.os.Process; import android.text.TextUtils; import com.baidu.license.download.base.HttpConnectTask; import com.baidu.license.download.exception.DownloadException; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; /* compiled from: HttpConnectTaskImpl.java */ /* loaded from: classes.dex */ public final class b implements HttpConnectTask { private final String a; private final HttpConnectTask.OnConnectListener b; private volatile int c; private volatile long d; public b(String str, HttpConnectTask.OnConnectListener onConnectListener) { this.a = str; this.b = onConnectListener; } private void a(HttpURLConnection httpURLConnection, boolean z) { String headerField = httpURLConnection.getHeaderField("Content-Length"); long contentLength = (TextUtils.isEmpty(headerField) || headerField.equals("0") || headerField.equals("-1")) ? httpURLConnection.getContentLength() : Long.parseLong(headerField); if (contentLength <= 0) { String headerField2 = httpURLConnection.getHeaderField("Ohc-File-Size"); contentLength = (TextUtils.isEmpty(headerField2) || headerField2.equals("0") || headerField2.equals("-1")) ? httpURLConnection.getContentLength() : Long.parseLong(headerField2); if (contentLength <= 0) { throw new DownloadException(108, "length <= 0"); } } a(); this.c = 103; this.b.a(System.currentTimeMillis() - this.d, contentLength, z); } @Override // com.baidu.license.download.base.HttpConnectTask public final void cancel() { this.c = 107; } @Override // com.baidu.license.download.base.HttpConnectTask public final boolean i() { return this.c == 106; } @Override // com.baidu.license.download.base.HttpConnectTask public final boolean isCanceled() { return this.c == 107; } @Override // com.baidu.license.download.base.HttpConnectTask public final void pause() { this.c = 106; } @Override // java.lang.Runnable public final void run() { IOException e; ProtocolException e2; HttpURLConnection httpURLConnection; Process.setThreadPriority(10); this.c = 102; this.b.i(); try { this.d = System.currentTimeMillis(); HttpURLConnection httpURLConnection2 = null; try { try { try { httpURLConnection = (HttpURLConnection) new URL(this.a).openConnection(); } catch (ProtocolException e3) { e2 = e3; } catch (IOException e4) { e = e4; } catch (Throwable th) { th = th; if (0 != 0) { httpURLConnection2.disconnect(); } throw th; } try { httpURLConnection.setConnectTimeout(4000); httpURLConnection.setReadTimeout(4000); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setRequestProperty("Range", "bytes=0-"); int responseCode = httpURLConnection.getResponseCode(); if (responseCode == 200) { a(httpURLConnection, false); } else { if (responseCode != 206) { throw new DownloadException(108, "UnSupported response code:".concat(String.valueOf(responseCode))); } a(httpURLConnection, true); } if (httpURLConnection != null) { httpURLConnection.disconnect(); } } catch (ProtocolException e5) { e2 = e5; throw new DownloadException(108, "Protocol error", e2); } catch (IOException e6) { e = e6; throw new DownloadException(108, "IO error", e); } } catch (Throwable th2) { th = th2; } } catch (MalformedURLException e7) { throw new DownloadException(108, "Bad url.", e7); } } catch (DownloadException e8) { switch (e8.getErrorCode()) { case 106: synchronized (this.b) { this.c = 106; this.b.a(); return; } case 107: synchronized (this.b) { this.c = 107; this.b.l(); return; } case 108: synchronized (this.b) { this.c = 108; this.b.a(e8); return; } default: throw new IllegalArgumentException("Unknown state"); } } } private void a() { if (!isCanceled()) { if (i()) { throw new DownloadException(106, "Connection Paused!"); } return; } throw new DownloadException(107, "Connection Canceled!"); } }