X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FViewSourceActivity.java;h=e4899ea329669f741c3b6355558b7e3f4bceba6e;hp=34f8f3bd7b49ddff912075fd26a3337b0fe62488;hb=b82022327701273b1b56419e8d6042895c0bc7b9;hpb=8ee786ff2f0c11ebb7abcd4d51e7013c5c5a5845 diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/ViewSourceActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/ViewSourceActivity.java index 34f8f3bd..e4899ea3 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/ViewSourceActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/ViewSourceActivity.java @@ -1,5 +1,5 @@ /* - * Copyright © 2017-2018 Soren Stoutner . + * Copyright © 2017-2020 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -19,45 +19,45 @@ package com.stoutner.privacybrowser.activities; -import android.annotation.SuppressLint; import android.app.Activity; -import android.app.DialogFragment; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.Typeface; -import android.os.AsyncTask; +import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.os.LocaleList; import android.preference.PreferenceManager; -import android.support.v4.app.NavUtils; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; -import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.ForegroundColorSpan; -import android.text.style.StyleSpan; +import android.util.TypedValue; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; -import android.webkit.CookieManager; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.app.NavUtils; +import androidx.fragment.app.DialogFragment; +import androidx.lifecycle.ViewModelProvider; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; + +import com.google.android.material.snackbar.Snackbar; import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.dialogs.AboutViewSourceDialog; +import com.stoutner.privacybrowser.helpers.ProxyHelper; +import com.stoutner.privacybrowser.viewmodelfactories.WebViewSourceFactory; +import com.stoutner.privacybrowser.viewmodels.WebViewSource; -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; +import java.net.Proxy; import java.util.Locale; public class ViewSourceActivity extends AppCompatActivity { @@ -71,62 +71,88 @@ public class ViewSourceActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + + // Get the screenshot preference. + boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false); + // Disable screenshots if not allowed. - if (!MainWebViewActivity.allowScreenshots) { + if (!allowScreenshots) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); } // Set the theme. - if (MainWebViewActivity.darkTheme) { - setTheme(R.style.PrivacyBrowserDark); - } else { - setTheme(R.style.PrivacyBrowserLight); - } + setTheme(R.style.PrivacyBrowser); // Run the default commands. super.onCreate(savedInstanceState); + // Get the launching intent + Intent intent = getIntent(); + + // Get the information from the intent. + String userAgent = intent.getStringExtra("user_agent"); + String currentUrl = intent.getStringExtra("current_url"); + + // Remove the incorrect lint warning below that the user agent might be null. + assert userAgent != null; + // Store a handle for the current activity. activity = this; // Set the content view. setContentView(R.layout.view_source_coordinatorlayout); - // `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21. - Toolbar viewSourceAppBar = findViewById(R.id.view_source_toolbar); - setSupportActionBar(viewSourceAppBar); + // Get a handle for the toolbar. + Toolbar toolbar = findViewById(R.id.view_source_toolbar); - // Setup the app bar. - final ActionBar appBar = getSupportActionBar(); + // Set the support action bar. + setSupportActionBar(toolbar); - // Remove the incorrect warning in Android Studio that appBar might be null. - assert appBar != null; + // Get a handle for the action bar. + final ActionBar actionBar = getSupportActionBar(); - // Add the custom layout to the app bar. - appBar.setCustomView(R.layout.view_source_app_bar); - appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); + // Remove the incorrect lint warning that the action bar might be null. + assert actionBar != null; - // Get a handle for the url text box. - EditText urlEditText = findViewById(R.id.url_edittext); + // Add the custom layout to the action bar. + actionBar.setCustomView(R.layout.view_source_app_bar); + actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); - // Get the formatted URL string from the main activity. - String formattedUrlString = MainWebViewActivity.formattedUrlString; + // Get handles for the views. + EditText urlEditText = findViewById(R.id.url_edittext); + TextView requestHeadersTextView = findViewById(R.id.request_headers); + TextView responseMessageTextView = findViewById(R.id.response_message); + TextView responseHeadersTextView = findViewById(R.id.response_headers); + TextView responseBodyTextView = findViewById(R.id.response_body); + ProgressBar progressBar = findViewById(R.id.progress_bar); + SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.view_source_swiperefreshlayout); // Populate the URL text box. - urlEditText.setText(formattedUrlString); + urlEditText.setText(currentUrl); - // Initialize the foreground color spans for highlighting the URLs. We have to use the deprecated `getColor()` until API >= 23. - redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700)); + // Initialize the gray foreground color spans for highlighting the URLs. The deprecated `getResources()` must be used until API >= 23. initialGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500)); finalGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500)); + // Get the current theme status. + int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; + + // Set the red color span according to the theme. + if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { + redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700)); + } else { + redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_900)); + } + // Apply text highlighting to the URL. highlightUrlText(); // Get a handle for the input method manager, which is used to hide the keyboard. InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - // Let Android Studio know that we aren't worried about the input method manager being null. + // Remove the lint warning that the input method manager might be null. assert inputMethodManager != null; // Remove the formatting from the URL when the user is editing the text. @@ -140,11 +166,152 @@ public class ViewSourceActivity extends AppCompatActivity { // Hide the soft keyboard. inputMethodManager.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0); + // Move to the beginning of the string. + urlEditText.setSelection(0); + // Reapply the highlighting. highlightUrlText(); + } + }); + + // Set the refresh color scheme according to the theme. + if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { + swipeRefreshLayout.setColorSchemeResources(R.color.blue_700); + } else { + swipeRefreshLayout.setColorSchemeResources(R.color.violet_500); + } + + // Initialize a color background typed value. + TypedValue colorBackgroundTypedValue = new TypedValue(); + + // Get the color background from the theme. + getTheme().resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true); + + // Get the color background int from the typed value. + int colorBackgroundInt = colorBackgroundTypedValue.data; + + // Set the swipe refresh background color. + swipeRefreshLayout.setProgressBackgroundColorSchemeColor(colorBackgroundInt); + + // Get the Do Not Track status. + boolean doNotTrack = sharedPreferences.getBoolean("do_not_track", false); + + // 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 locale from the list. + Locale locale = localeList.get(i); + // Add the locale to the string. `locale` by default displays as `en_US`, but WebView uses the `en-US` format. + localesStringBuilder.append(locale.getLanguage()); + localesStringBuilder.append("-"); + localesStringBuilder.append(locale.getCountry()); + // If not the first locale, append `;q=0.x`, which drops by .1 for each removal from the main locale until q=0.1. + if (q < 10) { + localesStringBuilder.append(";q=0."); + localesStringBuilder.append(q); + } + + // Decrement `q` if it is greater than 1. + if (q > 1) { + q--; + } + + // Add a second entry for the language only portion of the locale. + localesStringBuilder.append(","); + localesStringBuilder.append(locale.getLanguage()); + + // Append `1;q=0.x`, which drops by .1 for each removal form the main locale until q=0.1. + localesStringBuilder.append(";q=0."); + localesStringBuilder.append(q); + + // Decrement `q` if it is greater than 1. + if (q > 1) { + q--; + } } + + // Store the populated string builder in the locale string. + localeString = localesStringBuilder.toString(); + } else { // SDK < 24 only has a primary locale. + // Store the locale in the locale string. + localeString = Locale.getDefault().toString(); + } + + // Instantiate the proxy helper. + ProxyHelper proxyHelper = new ProxyHelper(); + + // Get the current proxy. + Proxy proxy = proxyHelper.getCurrentProxy(this); + + // Make the progress bar visible. + progressBar.setVisibility(View.VISIBLE); + + // Set the progress bar to be indeterminate. + progressBar.setIndeterminate(true); + + // Instantiate the WebView source factory. + ViewModelProvider.Factory webViewSourceFactory = new WebViewSourceFactory(currentUrl, userAgent, doNotTrack, localeString, proxy, MainWebViewActivity.executorService); + + // Instantiate the WebView source view model class. + final WebViewSource webViewSource = new ViewModelProvider(this, webViewSourceFactory).get(WebViewSource.class); + + // Create a source observer. + webViewSource.observeSource().observe(this, sourceStringArray -> { + // Populate the text views. This can take a long time, and freezes the user interface, if the response body is particularly large. + requestHeadersTextView.setText(sourceStringArray[0]); + responseMessageTextView.setText(sourceStringArray[1]); + responseHeadersTextView.setText(sourceStringArray[2]); + responseBodyTextView.setText(sourceStringArray[3]); + + // Hide the progress bar. + progressBar.setIndeterminate(false); + progressBar.setVisibility(View.GONE); + + //Stop the swipe to refresh indicator if it is running + swipeRefreshLayout.setRefreshing(false); + }); + + // Create an error observer. + webViewSource.observeErrors().observe(this, errorString -> { + // Display an error snackbar if the string is not `""`. + if (!errorString.equals("")) { + Snackbar.make(swipeRefreshLayout, errorString, Snackbar.LENGTH_LONG).show(); + } + }); + + // Implement swipe to refresh. + swipeRefreshLayout.setOnRefreshListener(() -> { + // Make the progress bar visible. + progressBar.setVisibility(View.VISIBLE); + + // Set the progress bar to be indeterminate. + progressBar.setIndeterminate(true); + + // Get the URL. + String urlString = urlEditText.getText().toString(); + + // Get the updated source. + webViewSource.updateSource(urlString); }); // Set the go button on the keyboard to request new source data. @@ -157,8 +324,17 @@ public class ViewSourceActivity extends AppCompatActivity { // Remove the focus from the URL box. urlEditText.clearFocus(); - // Get new source data for the current URL. - new GetSource().execute(urlEditText.getText().toString()); + // Make the progress bar visible. + progressBar.setVisibility(View.VISIBLE); + + // Set the progress bar to be indeterminate. + progressBar.setIndeterminate(true); + + // Get the URL. + String urlString = urlEditText.getText().toString(); + + // Get the updated source. + webViewSource.updateSource(urlString); // Consume the key press. return true; @@ -167,14 +343,11 @@ public class ViewSourceActivity extends AppCompatActivity { return false; } }); - - // Get the source as an `AsyncTask`. - new GetSource().execute(formattedUrlString); } @Override public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. + // Inflate the menu. This adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.view_source_options_menu, menu); // Display the menu. @@ -182,12 +355,12 @@ public class ViewSourceActivity extends AppCompatActivity { } @Override - public boolean onOptionsItemSelected(MenuItem menuItem) { + public boolean onOptionsItemSelected(@NonNull MenuItem menuItem) { // Get a handle for the about alert dialog. DialogFragment aboutDialogFragment = new AboutViewSourceDialog(); // Show the about alert dialog. - aboutDialogFragment.show(getFragmentManager(), getString(R.string.about)); + aboutDialogFragment.show(getSupportFragmentManager(), getString(R.string.about)); // Consume the event. return true; @@ -202,393 +375,60 @@ public class ViewSourceActivity extends AppCompatActivity { // Get a handle for the URL EditText. EditText urlEditText = findViewById(R.id.url_edittext); - // Get the URL. + // Get the URL string. String urlString = urlEditText.getText().toString(); - // 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 { - // 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--; - } + // Highlight the URL according to the protocol. + if (urlString.startsWith("file://")) { // This is a file URL. + // De-emphasize only the protocol. + urlEditText.getText().setSpan(initialGrayColorSpan, 0, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE); + } else if (urlString.startsWith("content://")) { + // De-emphasize only the protocol. + urlEditText.getText().setSpan(initialGrayColorSpan, 0, 10, Spanned.SPAN_INCLUSIVE_INCLUSIVE); + } else { // This is a web URL. + // Get the index of the `/` immediately after the domain name. + int endOfDomainName = urlString.indexOf("/", (urlString.indexOf("//") + 2)); + + // Create a base URL string. + String baseUrl; + + // Get the base URL. + if (endOfDomainName > 0) { // There is at least one character after the base URL. + // Get the base URL. + baseUrl = urlString.substring(0, endOfDomainName); + } else { // There are no characters after the base URL. + // Set the base URL to be the entire URL string. + baseUrl = urlString; + } - // Store the populated string builder in the locale string. - localeString = localesStringBuilder.toString(); - } else { // SDK < 24 only has a primary locale. - // Store the locale in the locale string. - localeString = Locale.getDefault().toString(); - } + // Get the index of the last `.` in the domain. + int lastDotIndex = baseUrl.lastIndexOf("."); - // Set the `Accept-Language` header property. - httpUrlConnection.setRequestProperty("Accept-Language", localeString); - - // Add the `Accept-Language` 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-Language", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } else { // Older versions not so much. - oldRequestHeadersBuilderLength = requestHeadersBuilder.length(); - requestHeadersBuilder.append("Accept-Language"); - newRequestHeadersBuilderLength = requestHeadersBuilder.length(); - requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } - requestHeadersBuilder.append(": "); - requestHeadersBuilder.append(localeString); - - - // Get the cookies for the current domain. - String cookiesString = CookieManager.getInstance().getCookie(url.toString()); - - // Only process the cookies if they are not null. - if (cookiesString != null) { - // Set the `Cookie` header property. - httpUrlConnection.setRequestProperty("Cookie", cookiesString); - - // Add the `Cookie` 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("Cookie", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } else { // Older versions not so much. - oldRequestHeadersBuilderLength = requestHeadersBuilder.length(); - requestHeadersBuilder.append("Cookie"); - newRequestHeadersBuilderLength = requestHeadersBuilder.length(); - requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } - requestHeadersBuilder.append(": "); - requestHeadersBuilder.append(cookiesString); - } + // Get the index of the penultimate `.` in the domain. + int penultimateDotIndex = baseUrl.lastIndexOf(".", lastDotIndex - 1); + // Markup 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); - // `HttpUrlConnection` sets `Accept-Encoding` to be `gzip` by default. If the property is manually set, than `HttpUrlConnection` does not process the decoding. - // Add the `Accept-Encoding` 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-Encoding", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } else { // Older versions not so much. - oldRequestHeadersBuilderLength = requestHeadersBuilder.length(); - requestHeadersBuilder.append("Accept-Encoding"); - newRequestHeadersBuilderLength = requestHeadersBuilder.length(); - requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + // De-emphasize subdomains. + if (penultimateDotIndex > 0) { // There is more than one subdomain in the domain name. + urlEditText.getText().setSpan(initialGrayColorSpan, 7, penultimateDotIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE); } - requestHeadersBuilder.append(": gzip"); - - - // 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 { - // Initialize the string builders. - responseMessageBuilder = new SpannableStringBuilder(); - responseHeadersBuilder = new SpannableStringBuilder(); - - // Get the response code, which causes the connection to the server to be made. - int responseCode = httpUrlConnection.getResponseCode(); - - // Populate the response message string builder. - if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart. - responseMessageBuilder.append(String.valueOf(responseCode), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } else { // Older versions not so much. - responseMessageBuilder.append(String.valueOf(responseCode)); - int newLength = responseMessageBuilder.length(); - responseMessageBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, newLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } - responseMessageBuilder.append(": "); - responseMessageBuilder.append(httpUrlConnection.getResponseMessage()); - - // Initialize the iteration variable. - int i = 0; - - // Iterate through the received header fields. - while (httpUrlConnection.getHeaderField(i) != null) { - // Add a new line if there is already information in the string builder. - if (i > 0) { - responseHeadersBuilder.append(System.getProperty("line.separator")); - } - - // Add the header to the string builder and format the text. - if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart. - responseHeadersBuilder.append(httpUrlConnection.getHeaderFieldKey(i), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } else { // Older versions not so much. - int oldLength = responseHeadersBuilder.length(); - responseHeadersBuilder.append(httpUrlConnection.getHeaderFieldKey(i)); - int newLength = responseHeadersBuilder.length(); - responseHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldLength + 1, newLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } - responseHeadersBuilder.append(": "); - responseHeadersBuilder.append(httpUrlConnection.getHeaderField(i)); - - // Increment the iteration variable. - i++; - } - - // Instantiate an input stream for the response body. - InputStream inputStream; - - // Get the correct input stream based on the response code. - if (responseCode == 404) { // Get the error stream. - inputStream = new BufferedInputStream(httpUrlConnection.getErrorStream()); - } else { // Get the response body stream. - inputStream = new BufferedInputStream(httpUrlConnection.getInputStream()); - } - - // Initialize the byte array output stream and the conversion buffer byte array. - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - byte[] conversionBufferByteArray = new byte[1024]; - - // Instantiate the variable to track the buffer length. - int bufferLength; - - try { - // Attempt to read data from the input stream and store it in the conversion buffer byte array. Also store the amount of data transferred in the buffer length variable. - while ((bufferLength = inputStream.read(conversionBufferByteArray)) > 0) { // Proceed while the amount of data stored in the buffer is > 0. - // Write the contents of the conversion buffer to the byte array output stream. - byteArrayOutputStream.write(conversionBufferByteArray, 0, bufferLength); - } - } catch (IOException e) { - e.printStackTrace(); - } - - // Close the input stream. - inputStream.close(); - - // Populate the response body string with the contents of the byte array output stream. - responseBodyString = byteArrayOutputStream.toString(); - } finally { - // Disconnect `httpUrlConnection`. - httpUrlConnection.disconnect(); + } else if (urlString.startsWith("https://")) { // De-emphasize the protocol of connections that are encrypted. + if (penultimateDotIndex > 0) { // There is more than one subdomain in the domain name. + // De-emphasize the protocol and the additional subdomains. + urlEditText.getText().setSpan(initialGrayColorSpan, 0, penultimateDotIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE); + } else { // There is only one subdomain in the domain name. + // De-emphasize only the protocol. + urlEditText.getText().setSpan(initialGrayColorSpan, 0, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE); } - } catch (IOException e) { - e.printStackTrace(); } - // Return the response body string as the result. - return responseBodyString; - } - - // `onPostExecute()` operates on the UI thread. - @Override - protected void onPostExecute(String responseBodyString){ - // Get handles for the text views. - TextView requestHeadersTextView = findViewById(R.id.request_headers); - TextView responseMessageTextView = findViewById(R.id.response_message); - TextView responseHeadersTextView = findViewById(R.id.response_headers); - TextView responseBodyTextView = findViewById(R.id.response_body); - ProgressBar progressBar = findViewById(R.id.progress_bar); - - // Populate the text views. - requestHeadersTextView.setText(requestHeadersBuilder); - responseMessageTextView.setText(responseMessageBuilder); - responseHeadersTextView.setText(responseHeadersBuilder); - responseBodyTextView.setText(responseBodyString); - - // Hide the progress bar. - progressBar.setIndeterminate(false); - progressBar.setVisibility(View.GONE); + // De-emphasize the text after the domain name. + if (endOfDomainName > 0) { + urlEditText.getText().setSpan(finalGrayColorSpan, endOfDomainName, urlString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); + } } } } \ No newline at end of file