+ // Create a blue foreground color span.
+ ForegroundColorSpan blueColorSpan;
+
+ // Set the blue color span according to the theme. The deprecated `getColor()` must be used until the minimum API >= 23.
+ if (darkTheme) {
+ blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
+ } else {
+ blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
+ }
+
+ // Remove the incorrect lint error that `.equals` might produce a NullPointerException.
+ assert domainString != null;
+
+ // Formet the domain string and issued to CName colors.
+ if (domainString.equals(issuedToCName)) { // `domainString` and `issuedToCName` 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(issuedToCName.startsWith("*.")){ // `issuedToCName` begins with a wildcard.
+ // Remove the initial `*.`.
+ String baseCertificateDomain = issuedToCName.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);
+ }
+
+ // Set the IP addresses, issued to, and issued by spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
+ ipAddressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length(), ipAddressesStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);