package com.kwai.filedownloader.e; import android.app.ActivityManager; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Environment; import android.text.TextUtils; import com.bytedance.sdk.openadsdk.downloadnew.core.TTDownloadField; import com.kwad.components.offline.api.BuildConfig; import com.kwad.sdk.utils.ao; import com.kwai.filedownloader.c.c; import com.kwai.filedownloader.d.a; import com.kwai.filedownloader.exception.FileDownloadGiveUpRetryException; import com.kwai.filedownloader.kwai.b; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import kotlin.UByte; public class f { private static int bKB = 65536; private static long bKC = 2000; private static String bKD; private static Boolean bKE; private static Boolean bKF; private static final Pattern bKG = Pattern.compile("attachment;\\s*filename\\s*=\\s*\"([^\"]*)\""); public static String a(int i, b bVar) { if (bVar != null) { String o = bVar.o("Etag"); if (d.bKs) { d.e(f.class, "etag find %s for task(%d)", o, Integer.valueOf(i)); } return o; } throw new RuntimeException("connection is null when findEtag"); } public static String a(b bVar, String str) { String go = go(bVar.o("Content-Disposition")); if (TextUtils.isEmpty(go)) { go = gm(str); } return go.replaceAll("\\/", "_"); } public static String a(String str, boolean z, String str2) { if (str == null) { return null; } if (!z) { return str; } if (str2 == null) { return null; } return aq(str, str2); } private static boolean a(int i, c cVar, Boolean bool) { if (cVar == null) { if (d.bKs) { d.e(f.class, "can't continue %d model == null", Integer.valueOf(i)); } return false; } else if (cVar.acC() != null) { return a(i, cVar, cVar.acC(), null); } else { if (d.bKs) { d.e(f.class, "can't continue %d temp path == null", Integer.valueOf(i)); } return false; } } public static boolean a(int i, c cVar, String str, Boolean bool) { if (str != null) { File file = new File(str); boolean exists = file.exists(); boolean isDirectory = file.isDirectory(); if (exists && !isDirectory) { long length = file.length(); long adk = cVar.adk(); if (cVar.adm() > 1 || adk != 0) { long total = cVar.getTotal(); if (length < adk || (total != -1 && (length > total || adk >= total))) { if (!d.bKs) { return false; } d.e(f.class, "can't continue %d dirty data fileLength[%d] sofar[%d] total[%d]", Integer.valueOf(i), Long.valueOf(length), Long.valueOf(adk), Long.valueOf(total)); return false; } else if (bool == null || bool.booleanValue() || total != length) { return true; } else { if (!d.bKs) { return false; } d.e(f.class, "can't continue %d, because of the output stream doesn't support seek, but the task has already pre-allocated, so we only can download it from the very beginning.", Integer.valueOf(i)); return false; } } else if (!d.bKs) { return false; } else { d.e(f.class, "can't continue %d the downloaded-record is zero.", Integer.valueOf(i)); return false; } } else if (!d.bKs) { return false; } else { d.e(f.class, "can't continue %d file not suit, exists[%B], directory[%B]", Integer.valueOf(i), Boolean.valueOf(exists), Boolean.valueOf(isDirectory)); return false; } } else if (!d.bKs) { return false; } else { d.e(f.class, "can't continue %d path = null", Integer.valueOf(i)); return false; } } public static void aQ(long j) { if (em(c.adH())) { bKC = j; return; } throw new IllegalAccessException("This value is used in the :filedownloader process, so set this value in your process is without effect. You can add 'process.non-separate=true' in 'filedownloader.properties' to share the main process to FileDownloadService. Or you can configure this value in 'filedownloader.properties' by 'download.min-progress-time'."); } private static int adL() { return bKB; } private static long adM() { return bKC; } private static String adN() { if (!TextUtils.isEmpty(bKD)) { return bKD; } return (c.adH().getExternalCacheDir() == null ? Environment.getDownloadCacheDirectory() : c.adH().getExternalCacheDir()).getAbsolutePath(); } public static boolean adO() { ConnectivityManager connectivityManager = (ConnectivityManager) c.adH().getSystemService("connectivity"); if (connectivityManager == null) { d.f(f.class, "failed to get connectivity manager!", new Object[0]); return true; } NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo == null || activeNetworkInfo.getType() != 1; } public static String adP() { return j("FileDownloader/%s", BuildConfig.VERSION_NAME); } private static String aq(String str, String str2) { if (str2 == null) { throw new IllegalStateException("can't generate real path, the file name is null"); } else if (str != null) { return j("%s%s%s", str, File.separator, str2); } else { throw new IllegalStateException("can't generate real path, the directory is null"); } } public static int ar(String str, String str2) { return com.kwai.filedownloader.download.b.aco().acp().l(str, str2, false); } public static void as(String str, String str2) { gu(str2); gv(str); } public static long b(int i, b bVar) { long gs = gs(bVar.o("Content-Length")); String o = bVar.o("Transfer-Encoding"); if (gs >= 0) { return gs; } if (!(o != null && o.equals("chunked"))) { if (!e.adJ().bKv) { throw new FileDownloadGiveUpRetryException("can't know the size of the download file, and its Transfer-Encoding is not Chunked either.\nyou can ignore such exception by add http.lenient=true to the filedownloader.properties"); } else if (d.bKs) { d.e(f.class, "%d response header is not legal but HTTP lenient is true, so handle as the case of transfer encoding chunk", Integer.valueOf(i)); } } return -1; } public static boolean b(int i, c cVar) { return a(i, cVar, (Boolean) null); } public static boolean em(Context context) { boolean z; Boolean bool = bKE; if (bool != null) { return bool.booleanValue(); } if (e.adJ().bKw) { z = true; } else if (((ActivityManager) context.getSystemService(TTDownloadField.TT_ACTIVITY)) == null) { d.f(f.class, "fail to get the activity manager!", new Object[0]); return false; } else { z = ao.getProcessName(context).endsWith(":filedownloader"); } Boolean valueOf = Boolean.valueOf(z); bKE = valueOf; return valueOf.booleanValue(); } public static void en(Context context) { File eo = eo(context); try { eo.getParentFile().mkdirs(); eo.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } private static File eo(Context context) { return new File(context.getFilesDir().getAbsolutePath() + File.separator + "filedownloader", ".old_file_converted"); } public static void fi(int i) { if (em(c.adH())) { bKB = i; return; } throw new IllegalAccessException("This value is used in the :filedownloader process, so set this value in your process is without effect. You can add 'process.non-separate=true' in 'filedownloader.properties' to share the main process to FileDownloadService. Or you can configure this value in 'filedownloader.properties' by 'download.min-progress-step'."); } public static String fo(String str) { try { byte[] digest = MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(digest.length * 2); for (byte b : digest) { int i = b & UByte.MAX_VALUE; if (i < 16) { sb.append("0"); } sb.append(Integer.toHexString(i)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Huh, MD5 should be supported?", e); } catch (UnsupportedEncodingException e2) { throw new RuntimeException("Huh, UTF-8 should be supported?", e2); } } public static String gl(String str) { return aq(adN(), gm(str)); } private static String gm(String str) { return fo(str); } public static String gn(String str) { return j("%s.temp", str); } public static String go(String str) { if (str == null) { return null; } try { Matcher matcher = bKG.matcher(str); if (matcher.find()) { return matcher.group(1); } } catch (IllegalStateException unused) { } return null; } public static String gp(String str) { int length = str.length(); int i = 2; int i2 = (File.separatorChar == '\\' && length > 2 && str.charAt(1) == ':') ? 2 : 0; int lastIndexOf = str.lastIndexOf(File.separatorChar); if (lastIndexOf != -1 || i2 <= 0) { i = lastIndexOf; } if (i == -1 || str.charAt(length - 1) == File.separatorChar) { return null; } return (str.indexOf(File.separatorChar) == i && str.charAt(i2) == File.separatorChar) ? str.substring(0, i + 1) : str.substring(0, i); } public static String gq(String str) { return "FileDownloader-" + str; } public static boolean gr(String str) { return c.adH().checkCallingOrSelfPermission(str) == 0; } private static long gs(String str) { if (str == null) { return -1; } try { return Long.parseLong(str); } catch (NumberFormatException unused) { return -1; } } public static a gt(String str) { if (!TextUtils.isEmpty(str)) { File file = new File(str); if (file.exists() && file.isDirectory()) { file.delete(); if (!file.createNewFile()) { throw new RuntimeException(j("found invalid internal destination path[%s], & path is directory[%B]", str, Boolean.valueOf(file.isDirectory()))); } } if (file.exists() || file.createNewFile()) { return com.kwai.filedownloader.download.b.aco().al(file); } throw new IOException(j("create new file error %s", file.getAbsolutePath())); } throw new RuntimeException("found invalid internal destination path, empty"); } private static void gu(String str) { if (str != null) { File file = new File(str); if (file.exists()) { file.delete(); } } } public static void gv(String str) { if (str != null) { File file = new File(str); if (file.exists()) { file.delete(); } } } public static String j(String str, Object... objArr) { return String.format(Locale.ENGLISH, str, objArr); } public static int l(String str, String str2, boolean z) { return com.kwai.filedownloader.download.b.aco().acp().l(str, str2, z); } public static boolean s(long j, long j2) { return j > ((long) adL()) && j2 > adM(); } }