package com.liulishuo.filedownloader.download; import android.text.TextUtils; import com.liulishuo.filedownloader.connection.FileDownloadConnection; import com.liulishuo.filedownloader.connection.RedirectHandler; import com.liulishuo.filedownloader.download.ConnectionProfile; import com.liulishuo.filedownloader.model.FileDownloadHeader; import com.liulishuo.filedownloader.util.FileDownloadLog; import com.liulishuo.filedownloader.util.FileDownloadUtils; import java.io.IOException; import java.net.ProtocolException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /* loaded from: classes.dex */ public class ConnectTask { final int a; final String b; final FileDownloadHeader c; private ConnectionProfile d; private String e; private Map> f; private List g; static class Builder { private Integer a; private String b; private String c; private FileDownloadHeader d; private ConnectionProfile e; Builder() { } public Builder a(int i) { this.a = Integer.valueOf(i); return this; } public Builder b(String str) { this.b = str; return this; } public Builder a(String str) { this.c = str; return this; } public Builder a(FileDownloadHeader fileDownloadHeader) { this.d = fileDownloadHeader; return this; } public Builder a(ConnectionProfile connectionProfile) { this.e = connectionProfile; return this; } ConnectTask a() { ConnectionProfile connectionProfile; Integer num = this.a; if (num != null && (connectionProfile = this.e) != null && this.b != null) { return new ConnectTask(connectionProfile, num.intValue(), this.b, this.c, this.d); } throw new IllegalArgumentException(); } } private void b(FileDownloadConnection fileDownloadConnection) { HashMap> headers; FileDownloadHeader fileDownloadHeader = this.c; if (fileDownloadHeader == null || (headers = fileDownloadHeader.getHeaders()) == null) { return; } if (FileDownloadLog.a) { FileDownloadLog.d(this, "%d add outside header: %s", Integer.valueOf(this.a), headers); } for (Map.Entry> entry : headers.entrySet()) { String key = entry.getKey(); List value = entry.getValue(); if (value != null) { Iterator it = value.iterator(); while (it.hasNext()) { fileDownloadConnection.a(key, it.next()); } } } } private void c(FileDownloadConnection fileDownloadConnection) { FileDownloadHeader fileDownloadHeader = this.c; if (fileDownloadHeader == null || fileDownloadHeader.getHeaders().get("User-Agent") == null) { fileDownloadConnection.a("User-Agent", FileDownloadUtils.a()); } } void a(long j) { ConnectionProfile connectionProfile = this.d; long j2 = connectionProfile.b; if (j == j2) { FileDownloadLog.e(this, "no data download, no need to update", new Object[0]); return; } this.d = ConnectionProfile.ConnectionProfileBuild.a(connectionProfile.a, j, connectionProfile.c, connectionProfile.d - (j - j2)); if (FileDownloadLog.a) { FileDownloadLog.c(this, "after update profile:%s", this.d); } } public Map> d() { return this.f; } boolean e() { return this.d.b > 0; } private ConnectTask(ConnectionProfile connectionProfile, int i, String str, String str2, FileDownloadHeader fileDownloadHeader) { this.a = i; this.b = str; this.e = str2; this.c = fileDownloadHeader; this.d = connectionProfile; } public ConnectionProfile c() { return this.d; } FileDownloadConnection a() throws IOException, IllegalAccessException { FileDownloadConnection a = CustomComponentHolder.i().a(this.b); b(a); a(a); c(a); this.f = a.e(); if (FileDownloadLog.a) { FileDownloadLog.a(this, "<---- %s request header %s", Integer.valueOf(this.a), this.f); } a.execute(); this.g = new ArrayList(); FileDownloadConnection a2 = RedirectHandler.a(this.f, a, this.g); if (FileDownloadLog.a) { FileDownloadLog.a(this, "----> %s response header %s", Integer.valueOf(this.a), a2.b()); } return a2; } String b() { List list = this.g; if (list == null || list.isEmpty()) { return null; } return this.g.get(r0.size() - 1); } private void a(FileDownloadConnection fileDownloadConnection) throws ProtocolException { if (fileDownloadConnection.a(this.e, this.d.a)) { return; } if (!TextUtils.isEmpty(this.e)) { fileDownloadConnection.a("If-Match", this.e); } this.d.a(fileDownloadConnection); } }