package com.liulishuo.filedownloader.util; import android.app.ActivityManager; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; import android.os.Environment; import android.os.PowerManager; import android.os.Process; import android.os.StatFs; import android.text.TextUtils; import com.liulishuo.filedownloader.connection.FileDownloadConnection; import com.liulishuo.filedownloader.download.CustomComponentHolder; import com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException; import com.liulishuo.filedownloader.exception.FileDownloadSecurityException; import com.liulishuo.filedownloader.model.FileDownloadModel; import com.liulishuo.filedownloader.stream.FileDownloadOutputStream; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; /* loaded from: classes.dex */ public class FileDownloadUtils { private static int a = 65536; private static long b = 2000; private static String c; private static Boolean d; private static final Pattern e = Pattern.compile("attachment;\\s*filename\\*\\s*=\\s*\"*([^\"]*)'\\S*'([^\"]*)\"*"); private static final Pattern f = Pattern.compile("attachment;\\s*filename\\s*=\\s*\"*([^\"\\n]*)\"*"); public static void a(int i) throws IllegalAccessException { if (!c(FileDownloadHelper.a())) { 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'."); } a = i; } public static String b() { return !TextUtils.isEmpty(c) ? c : FileDownloadHelper.a().getExternalCacheDir() == null ? Environment.getDownloadCacheDirectory().getAbsolutePath() : FileDownloadHelper.a().getExternalCacheDir().getAbsolutePath(); } public static int c() { return a; } public static long d() { return b; } public static boolean e() { ConnectivityManager connectivityManager = (ConnectivityManager) FileDownloadHelper.a().getSystemService("connectivity"); if (connectivityManager == null) { FileDownloadLog.e(FileDownloadUtils.class, "failed to get connectivity manager!", new Object[0]); return true; } NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo == null || activeNetworkInfo.getType() != 1; } public static String f(String str) { return m(str); } public static String g(String str) { return b(b(), f(str)); } public static long h(String str) { return Build.VERSION.SDK_INT >= 18 ? new StatFs(str).getAvailableBytes() : r0.getAvailableBlocks() * r0.getBlockSize(); } public static String i(String str) { int length = str.length(); int i = (File.separatorChar == '\\' && length > 2 && str.charAt(1) == ':') ? 2 : 0; int lastIndexOf = str.lastIndexOf(File.separatorChar); int i2 = (lastIndexOf != -1 || i <= 0) ? lastIndexOf : 2; if (i2 == -1) { return null; } char charAt = str.charAt(length - 1); char c2 = File.separatorChar; if (charAt == c2) { return null; } return (str.indexOf(c2) == i2 && str.charAt(i) == File.separatorChar) ? str.substring(0, i2 + 1) : str.substring(0, i2); } public static String j(String str) { return a("%s.temp", str); } public static String k(String str) { return "FileDownloader-" + str; } public static boolean l(String str) { return true; } public static String m(String str) { try { byte[] digest = MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(digest.length * 2); for (byte b2 : digest) { int i = b2 & 255; if (i < 16) { sb.append("0"); } sb.append(Integer.toHexString(i)); } return sb.toString(); } catch (UnsupportedEncodingException e2) { throw new RuntimeException("Huh, UTF-8 should be supported?", e2); } catch (NoSuchAlgorithmException e3) { throw new RuntimeException("Huh, MD5 should be supported?", e3); } } public static String n(String str) { Matcher matcher; if (str == null) { return null; } try { matcher = e.matcher(str); } catch (UnsupportedEncodingException | IllegalStateException unused) { } if (matcher.find()) { return URLDecoder.decode(matcher.group(2), matcher.group(1)); } Matcher matcher2 = f.matcher(str); if (matcher2.find()) { return matcher2.group(1); } return null; } public static long o(String str) { if (str != null && str.length() != 0) { try { Matcher matcher = Pattern.compile("bytes (\\d+)-(\\d+)/\\d+").matcher(str); if (matcher.find()) { return (Long.parseLong(matcher.group(2)) - Long.parseLong(matcher.group(1))) + 1; } } catch (Exception e2) { FileDownloadLog.a(FileDownloadUtils.class, e2, "parse content length from content range error", new Object[0]); } } return -1L; } public static long p(String str) { if (str == null) { return -1L; } String[] split = str.split("/"); if (split.length >= 2) { try { return Long.parseLong(split[1]); } catch (NumberFormatException unused) { FileDownloadLog.e(FileDownloadUtils.class, "parse instance length failed with %s", str); } } return -1L; } public static int c(String str, String str2) { return CustomComponentHolder.i().c().a(str, str2, false); } public static void d(Context context) { File a2 = a(context); try { a2.getParentFile().mkdirs(); a2.createNewFile(); } catch (IOException e2) { e2.printStackTrace(); } } public static void a(long j) throws IllegalAccessException { if (c(FileDownloadHelper.a())) { b = 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'."); } public static boolean c(Context context) { Boolean bool = d; if (bool != null) { return bool.booleanValue(); } boolean z = false; if (!FileDownloadProperties.a().d) { int myPid = Process.myPid(); ActivityManager activityManager = (ActivityManager) context.getSystemService("activity"); if (activityManager == null) { FileDownloadLog.e(FileDownloadUtils.class, "fail to get the activity manager!", new Object[0]); return false; } List runningAppProcesses = activityManager.getRunningAppProcesses(); if (runningAppProcesses != null && !runningAppProcesses.isEmpty()) { Iterator it = runningAppProcesses.iterator(); while (true) { if (!it.hasNext()) { break; } ActivityManager.RunningAppProcessInfo next = it.next(); if (next.pid == myPid) { z = next.processName.endsWith(":filedownloader"); break; } } } else { FileDownloadLog.e(FileDownloadUtils.class, "The running app process info list from ActivityManager is null or empty, maybe current App is not running.", new Object[0]); return false; } } else { z = true; } d = Boolean.valueOf(z); return d.booleanValue(); } public static String b(String str, String str2) { if (str2 == null) { throw new IllegalStateException("can't generate real path, the file name is null"); } if (str != null) { return a("%s%s%s", str, File.separator, str2); } throw new IllegalStateException("can't generate real path, the directory is null"); } private static String d(FileDownloadConnection fileDownloadConnection) { return fileDownloadConnection.a("Content-Range"); } public static void e(String str) { if (str != null) { File file = new File(str); if (file.exists()) { file.delete(); } } } public static int a(String str, String str2, boolean z) { return CustomComponentHolder.i().c().a(str, str2, z); } public static void d(String str) { if (str != null) { File file = new File(str); if (file.exists()) { file.delete(); } } } public static String a(String str, Object... objArr) { return String.format(Locale.ENGLISH, str, objArr); } public static long b(String str) { if (str == null) { return -1L; } try { return Long.parseLong(str); } catch (NumberFormatException unused) { return -1L; } } public static boolean e(Context context) { return Build.VERSION.SDK_INT >= 26 && !b(context); } public static File a(Context context) { return new File(context.getFilesDir().getAbsolutePath() + File.separator + "filedownloader", ".old_file_converted"); } public static String b(int i, FileDownloadConnection fileDownloadConnection) { if (fileDownloadConnection != null) { String a2 = fileDownloadConnection.a("Etag"); if (FileDownloadLog.a) { FileDownloadLog.a(FileDownloadUtils.class, "etag find %s for task(%d)", a2, Integer.valueOf(i)); } return a2; } throw new RuntimeException("connection is null when findEtag"); } 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 b(str, str2); } public static boolean a(String str) { return FileDownloadHelper.a().checkCallingOrSelfPermission(str) == 0; } public static long a(int i, FileDownloadConnection fileDownloadConnection) { long b2 = b(fileDownloadConnection.a("Content-Length")); String a2 = fileDownloadConnection.a("Transfer-Encoding"); if (b2 >= 0) { return b2; } if (a2 != null && a2.equals("chunked")) { return -1L; } if (FileDownloadProperties.a().c) { if (!FileDownloadLog.a) { return -1L; } FileDownloadLog.a(FileDownloadUtils.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 -1L; } 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"); } public static long b(FileDownloadConnection fileDownloadConnection) { long c2 = c(fileDownloadConnection); if (c2 < 0) { FileDownloadLog.e(FileDownloadUtils.class, "don't get instance length fromContent-Range header", new Object[0]); c2 = -1; } if (c2 == 0 && FileDownloadProperties.a().h) { return -1L; } return c2; } private static boolean b(Context context) { List runningAppProcesses; PowerManager powerManager; ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService("activity"); if (activityManager == null || (runningAppProcesses = activityManager.getRunningAppProcesses()) == null || (powerManager = (PowerManager) context.getSystemService("power")) == null) { return false; } if (Build.VERSION.SDK_INT > 19) { if (!powerManager.isInteractive()) { return false; } } else if (!powerManager.isScreenOn()) { return false; } String packageName = context.getApplicationContext().getPackageName(); for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { if (runningAppProcessInfo.processName.equals(packageName) && runningAppProcessInfo.importance == 100) { return true; } } return false; } public static boolean c(int i, FileDownloadConnection fileDownloadConnection) { if (i == 206 || i == 1) { return true; } return "bytes".equals(fileDownloadConnection.a("Accept-Ranges")); } public static long c(FileDownloadConnection fileDownloadConnection) { return p(d(fileDownloadConnection)); } public static FileDownloadOutputStream c(String str) throws IOException { if (!TextUtils.isEmpty(str)) { if (l(str)) { File file = new File(str); if (file.exists() && file.isDirectory()) { throw new RuntimeException(a("found invalid internal destination path[%s], & path is directory[%B]", str, Boolean.valueOf(file.isDirectory()))); } if (!file.exists() && !file.createNewFile()) { throw new IOException(a("create new file error %s", file.getAbsolutePath())); } return CustomComponentHolder.i().a(file); } throw new RuntimeException(a("found invalid internal destination filename %s", str)); } throw new RuntimeException("found invalid internal destination path, empty"); } public static long a(FileDownloadConnection fileDownloadConnection) { long o = o(d(fileDownloadConnection)); if (o < 0) { return -1L; } return o; } public static String a(FileDownloadConnection fileDownloadConnection, String str) throws FileDownloadSecurityException { String n = n(fileDownloadConnection.a("Content-Disposition")); if (TextUtils.isEmpty(n)) { return f(str); } if (n.contains("../")) { throw new FileDownloadSecurityException(a("The filename [%s] from the response is not allowable, because it contains '../', which can raise the directory traversal vulnerability", n)); } return n; } public static boolean a(int i, FileDownloadModel fileDownloadModel) { return a(i, fileDownloadModel, (Boolean) null); } public static boolean a(int i, FileDownloadModel fileDownloadModel, Boolean bool) { if (fileDownloadModel == null) { if (FileDownloadLog.a) { FileDownloadLog.a(FileDownloadUtils.class, "can't continue %d model == null", Integer.valueOf(i)); } return false; } if (fileDownloadModel.getTempFilePath() == null) { if (FileDownloadLog.a) { FileDownloadLog.a(FileDownloadUtils.class, "can't continue %d temp path == null", Integer.valueOf(i)); } return false; } return a(i, fileDownloadModel, fileDownloadModel.getTempFilePath(), bool); } public static boolean a(int i, FileDownloadModel fileDownloadModel, String str, Boolean bool) { if (str == null) { if (!FileDownloadLog.a) { return false; } FileDownloadLog.a(FileDownloadUtils.class, "can't continue %d path = null", Integer.valueOf(i)); return false; } File file = new File(str); boolean exists = file.exists(); boolean isDirectory = file.isDirectory(); if (exists && !isDirectory) { long length = file.length(); long soFar = fileDownloadModel.getSoFar(); if (fileDownloadModel.getConnectionCount() <= 1 && soFar == 0) { if (!FileDownloadLog.a) { return false; } FileDownloadLog.a(FileDownloadUtils.class, "can't continue %d the downloaded-record is zero.", Integer.valueOf(i)); return false; } long total = fileDownloadModel.getTotal(); if (length >= soFar && (total == -1 || (length <= total && soFar < total))) { if (bool == null || bool.booleanValue() || total != length) { return true; } if (!FileDownloadLog.a) { return false; } FileDownloadLog.a(FileDownloadUtils.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; } if (!FileDownloadLog.a) { return false; } FileDownloadLog.a(FileDownloadUtils.class, "can't continue %d dirty data fileLength[%d] sofar[%d] total[%d]", Integer.valueOf(i), Long.valueOf(length), Long.valueOf(soFar), Long.valueOf(total)); return false; } if (!FileDownloadLog.a) { return false; } FileDownloadLog.a(FileDownloadUtils.class, "can't continue %d file not suit, exists[%B], directory[%B]", Integer.valueOf(i), Boolean.valueOf(exists), Boolean.valueOf(isDirectory)); return false; } public static void a(String str, String str2) { e(str2); d(str); } public static boolean a(long j, long j2) { return j > ((long) c()) && j2 > d(); } public static String a() { return a("FileDownloader/%s", "1.7.5"); } }