+ // Create a red `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
+ @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
+
+ // Create a blue `ForegroundColorSpan`.
+ ForegroundColorSpan blueColorSpan;
+
+ // Set `blueColorSpan` according to the theme. We have to use the deprecated `getColor()` until API >= 23.
+ if (MainWebViewActivity.darkTheme) {
+ //noinspection deprecation
+ blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
+ } else {
+ //noinspection deprecation
+ blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
+ }
+
+ // Formet the `domainString` and `issuedToCName` colors.
+ if (domainString.equals(issuedToCNameString)) { // `domainString` and `issuedToCNameString` match.
+ // Set the strings to be blue.
+ domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ } else if(issuedToCNameString.startsWith("*.")){ // `issuedToCNameString` begins with a wildcard.
+ // Remove the initial `*.`.
+ String baseCertificateDomain = issuedToCNameString.substring(2);
+
+ // Setup a copy of `domainString` to test subdomains.
+ String domainStringSubdomain = domainString;
+
+ // Initialize `domainNamesMatch`.
+ boolean domainNamesMatch = false;
+
+ // Check all the subdomains in `domainStringSubdomain` against `baseCertificateDomain`.
+ while (!domainNamesMatch && domainStringSubdomain.contains(".")) { // Stop checking if we know that `domainNamesMatch` is `true` or if we run out of `.`.
+ // Test the `domainStringSubdomain` against `baseCertificateDomain`.
+ if (domainStringSubdomain.equals(baseCertificateDomain)) {
+ domainNamesMatch = true;
+ }
+
+ // Strip out the lowest subdomain of `certificateCommonNameSubdomain`.
+ domainStringSubdomain = domainStringSubdomain.substring(domainStringSubdomain.indexOf(".") + 1);
+ }
+
+ // Format the domain and issued to Common Name according to `domainNamesMatch`.
+ if (domainNamesMatch) { // `domainString` is a subdomain of the wildcard `issuedToCNameString`.
+ // Set the strings to be blue.
+ domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ } else { // `domainString` is not a subdomain of the wildcard `issuedToCNameString`.
+ // Set the string to be red.
+ domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ }
+ } else { // The strings do not match and `issuedToCNameString` does not begin with a wildcard.
+ // Set the strings to be red.
+ domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ }
+
+ // Setup the issued to and issued by spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.