X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fhelpers%2FUrlHelper.kt;h=44a2be8cc5c4238890aae134fc25c0abe4cc3f20;hb=dc36dea65a4eb37496fb1ecbbd9f8e1906f50216;hp=a2e586e64e0274d4a026d9854d213c6179ae9b47;hpb=6790b16072ffa307636462579ed1476668a57e6e;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/UrlHelper.kt b/app/src/main/java/com/stoutner/privacybrowser/helpers/UrlHelper.kt index a2e586e6..44a2be8c 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/helpers/UrlHelper.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/UrlHelper.kt @@ -1,5 +1,5 @@ /* - * Copyright 2020-2022 Soren Stoutner . + * Copyright 2020-2023 Soren Stoutner . * * This file is part of Privacy Browser Android . * @@ -36,7 +36,6 @@ import java.text.NumberFormat object UrlHelper { // Content dispositions can contain other text besides the file name, and they can be in any order. // Elements are separated by semicolons. Sometimes the file names are contained in quotes. - @JvmStatic fun getFileName(context: Context, contentDispositionString: String?, contentTypeString: String?, urlString: String): String { // Define a file name string. var fileNameString: String @@ -196,72 +195,6 @@ object UrlHelper { return Pair(fileNameString, formattedFileSize) } - fun getSize(context: Context, url: URL, userAgent: String, cookiesEnabled: Boolean): String { - // Initialize the formatted file size string. - var formattedFileSize = context.getString(R.string.unknown_size) - - // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch exceptions. - try { - // Instantiate the proxy helper. - val proxyHelper = ProxyHelper() - - // Get the current proxy. - val proxy = proxyHelper.getCurrentProxy(context) - - // Open a connection to the URL. No data is actually sent at this point. - val httpUrlConnection = url.openConnection(proxy) as HttpURLConnection - - // Add the user agent to the header property. - httpUrlConnection.setRequestProperty("User-Agent", userAgent) - - // Add the cookies if they are enabled. - if (cookiesEnabled) { - // Get the cookies for the current domain. - val cookiesString = CookieManager.getInstance().getCookie(url.toString()) - - // Only add the cookies if they are not null. - if (cookiesString != null) { - // Add the cookies to the header property. - httpUrlConnection.setRequestProperty("Cookie", cookiesString) - } - } - - // The actual network request is in a `try` bracket so that `disconnect()` is run in the `finally` section even if an error is encountered in the main block. - try { - // Get the status code. This initiates a network connection. - val responseCode = httpUrlConnection.responseCode - - // Check the response code. - if (responseCode >= 400) { // The response code is an error message. - // Set the formatted file size to indicate a bad URL. - formattedFileSize = context.getString(R.string.invalid_url) - } else { // The response code is not an error message. - // Get the content length header. - val contentLengthString = httpUrlConnection.getHeaderField("Content-Length") - - // Only process the content length string if it isn't null. - if (contentLengthString != null) { - // Convert the content length string to a long. - val fileSize = contentLengthString.toLong() - - // Format the file size. - formattedFileSize = NumberFormat.getInstance().format(fileSize) + " " + context.getString(R.string.bytes) - } - } - } finally { - // Disconnect the HTTP URL connection. - httpUrlConnection.disconnect() - } - } catch (exception: Exception) { - // Set the formatted file size to indicate a bad URL. - formattedFileSize = context.getString(R.string.invalid_url) - } - - // Return the formatted file size. - return formattedFileSize - } - - @JvmStatic fun highlightSyntax(urlEditText: EditText, initialGrayColorSpan: ForegroundColorSpan, finalGrayColorSpan: ForegroundColorSpan, redColorSpan: ForegroundColorSpan) { // Get the URL string. val urlString: String = urlEditText.text.toString()