private static Spanned removeTrailingNewlines(Spanned text) {
    int trailingNewlineCharacterCount = 0;
    for (int i = text.length() - 1; i >= 0; i--) {
      char c = text.charAt(i);
      if ((c == '\n') || (c == '\r')) {
        trailingNewlineCharacterCount++;
      } else {
        break;
      }
    }
    if (trailingNewlineCharacterCount == 0) {
      return text;
    }

    return new SpannedString(
        text.subSequence(0, text.length() - trailingNewlineCharacterCount));
  }
Ensures that the text ends with two newline characters, appending newline characters only
when necessary.
java/com/google/android/apps/authenticator/util/Utilities.java