- // Highlight the beginning of the URL.
- if (urlString.startsWith("http://")) { // Highlight the protocol of connections that are not encrypted.
- urlEditText.getText().setSpan(redColorSpan, 0, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
- } else if (urlString.startsWith("https://")) { // De-emphasize the protocol of connections that are encrypted.
- urlEditText.getText().setSpan(initialGrayColorSpan, 0, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
- }
-
- // Get the index of the `/` immediately after the domain name.
- int endOfDomainName = urlString.indexOf("/", (urlString.indexOf("//") + 2));
-
- // De-emphasize the text after the domain name.
- if (endOfDomainName > 0) {
- urlEditText.getText().setSpan(finalGrayColorSpan, endOfDomainName, urlString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
- }
- }
-
- // The first `String` declares the parameters. The `Void` does not declare progress units. The last `String` contains the results.
- // `StaticFieldLeaks` are suppressed so that Android Studio doesn't complain about running an AsyncTask in a non-static context.
- @SuppressLint("StaticFieldLeak")
- private class GetSource extends AsyncTask<String, Void, String> {
- // The class variables pass information from `doInBackground()` to `onPostExecute()`.
- SpannableStringBuilder responseMessageBuilder;
- SpannableStringBuilder requestHeadersBuilder;
- SpannableStringBuilder responseHeadersBuilder;
-
- // `onPreExecute()` operates on the UI thread.
- @Override
- protected void onPreExecute() {
- // Get a handle for the progress bar.
- ProgressBar progressBar = findViewById(R.id.progress_bar);
-
- // Make the progress bar visible.
- progressBar.setVisibility(View.VISIBLE);
-
- // Set the progress bar to be indeterminate.
- progressBar.setIndeterminate(true);
- }
-
- @Override
- protected String doInBackground(String... formattedUrlString) {
- // Initialize the response body `String`.
- String responseBodyString = "";
-
- // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch `IOExceptions`.
- try {
- // Get the current URL from the main activity.
- URL url = new URL(formattedUrlString[0]);
-
- // Open a connection to the URL. No data is actually sent at this point.
- HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
-
- // Instantiate the variables necessary to build the request headers.
- requestHeadersBuilder = new SpannableStringBuilder();
- int oldRequestHeadersBuilderLength;
- int newRequestHeadersBuilderLength;
-
-
- // Set the `Host` header property.
- httpUrlConnection.setRequestProperty("Host", url.getHost());
-
- // Add the `Host` header to the string builder and format the text.
- if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart.
- requestHeadersBuilder.append("Host", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- } else { // Older versions not so much.
- oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.append("Host");
- newRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- requestHeadersBuilder.append(": ");
- requestHeadersBuilder.append(url.getHost());
-
-
- // Set the `Connection` header property.
- httpUrlConnection.setRequestProperty("Connection", "keep-alive");
-
- // Add the `Connection` header to the string builder and format the text.
- requestHeadersBuilder.append(System.getProperty("line.separator"));
- if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart.
- requestHeadersBuilder.append("Connection", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- } else { // Older versions not so much.
- oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.append("Connection");
- newRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- requestHeadersBuilder.append(": keep-alive");
-
-
- // Get the current `User-Agent` string.
- String userAgentString = MainWebViewActivity.appliedUserAgentString;
-
- // Set the `User-Agent` header property.
- httpUrlConnection.setRequestProperty("User-Agent", userAgentString);
-
- // Add the `User-Agent` header to the string builder and format the text.
- requestHeadersBuilder.append(System.getProperty("line.separator"));
- if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart.
- requestHeadersBuilder.append("User-Agent", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- } else { // Older versions not so much.
- oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.append("User-Agent");
- newRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- requestHeadersBuilder.append(": ");
- requestHeadersBuilder.append(userAgentString);
-
-
- // Set the `Upgrade-Insecure-Requests` header property.
- httpUrlConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
-
- // Add the `Upgrade-Insecure-Requests` header to the string builder and format the text.
- requestHeadersBuilder.append(System.getProperty("line.separator"));
- if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart.
- requestHeadersBuilder.append("Upgrade-Insecure-Requests", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- } else { // Older versions not so much.
- oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.append("Upgrade-Insecure_Requests");
- newRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- requestHeadersBuilder.append(": 1");
-
-
- // Set the `x-requested-with` header property.
- httpUrlConnection.setRequestProperty("x-requested-with", "");
-
- // Add the `x-requested-with` header to the string builder and format the text.
- requestHeadersBuilder.append(System.getProperty("line.separator"));
- if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart.
- requestHeadersBuilder.append("x-requested-with", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- } else { // Older versions not so much.
- oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.append("x-requested-with");
- newRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- requestHeadersBuilder.append(": ");
-
-
- // Get a handle for the shared preferences.
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
-
- // Only populate `Do Not Track` if it is enabled.
- if (sharedPreferences.getBoolean("do_not_track", false)) {
- // Set the `dnt` header property.
- httpUrlConnection.setRequestProperty("dnt", "1");
-
- // Add the `dnt` header to the string builder and format the text.
- requestHeadersBuilder.append(System.getProperty("line.separator"));
- if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart.
- requestHeadersBuilder.append("dnt", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- } else { // Older versions not so much.
- oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.append("dnt");
- newRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- requestHeadersBuilder.append(": 1");
- }
-
-
- // Set the `Accept` header property.
- httpUrlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
-
- // Add the `Accept` header to the string builder and format the text.
- requestHeadersBuilder.append(System.getProperty("line.separator"));
- if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart.
- requestHeadersBuilder.append("Accept", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- } else { // Older versions not so much.
- oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.append("Accept");
- newRequestHeadersBuilderLength = requestHeadersBuilder.length();
- requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- requestHeadersBuilder.append(": ");
- requestHeadersBuilder.append("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
-
-
- // Instantiate a locale string.
- String localeString;
-
- // Populate the locale string.
- if (Build.VERSION.SDK_INT >= 24) { // SDK >= 24 has a list of locales.
- // Get the list of locales.
- LocaleList localeList = getResources().getConfiguration().getLocales();
-
- // Initialize a string builder to extract the locales from the list.
- StringBuilder localesStringBuilder = new StringBuilder();
-
- // Initialize a `q` value, which is used by `WebView` to indicate the order of importance of the languages.
- int q = 10;
-
- // Populate the string builder with the contents of the locales list.
- for (int i = 0; i < localeList.size(); i++) {
- // Append a comma if there is already an item in the string builder.
- if (i > 0) {
- localesStringBuilder.append(",");
- }
-
- // Get the indicated locale from the list.
- localesStringBuilder.append(localeList.get(i));
-
- // If not the first locale, append `;q=0.i`, which drops by .1 for each removal from the main locale.
- if (q < 10) {
- localesStringBuilder.append(";q=0.");
- localesStringBuilder.append(q);
- }
-
- // Decrement `q`.
- q--;
- }