Update AYTF to stop formatting if the plus sign is entered and it is not
the first character.

Bug: 5110153
Change-Id: I6ce9f613488dc4b66b2ff9249d7b4cca50f6fa35
diff --git a/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java b/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
index e6bd36e..e175eda 100644
--- a/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
+++ b/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
@@ -279,9 +279,9 @@
     if (rememberPosition) {
       originalPosition = accruedInput.length();
     }
-    // We do formatting on-the-fly only when each character entered is either a plus sign or a
-    // digit.
-    if (!PhoneNumberUtil.VALID_START_CHAR_PATTERN.matcher(Character.toString(nextChar)).matches()) {
+    // We do formatting on-the-fly only when each character entered is either a digit, or a plus 
+    // sign (accepted at the start of the number only).
+    if (!isDigitOrLeadingPlusSign(nextChar)) {
       ableToFormat = false;
     }
     if (!ableToFormat) {
@@ -341,6 +341,12 @@
     }
   }
 
+  private boolean isDigitOrLeadingPlusSign(char nextChar) {
+    return Character.isDigit(nextChar) ||
+        (accruedInput.length() == 1 &&
+         PhoneNumberUtil.PLUS_CHARS_PATTERN.matcher(Character.toString(nextChar)).matches());
+  }
+
   String attemptToFormatAccruedDigits() {
     for (NumberFormat numFormat : possibleFormats) {
       Matcher m = regexCache.getPatternForRegex(numFormat.getPattern()).matcher(nationalNumber);
diff --git a/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
index da57a13..bb2bf38 100644
--- a/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
+++ b/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
@@ -202,7 +202,7 @@
       Arrays.toString(ALPHA_MAPPINGS.keySet().toArray()).replaceAll("[, \\[\\]]", "") +
       Arrays.toString(ALPHA_MAPPINGS.keySet().toArray()).toLowerCase().replaceAll("[, \\[\\]]", "");
   static final String PLUS_CHARS = "+\uFF0B";
-  private static final Pattern PLUS_CHARS_PATTERN = Pattern.compile("[" + PLUS_CHARS + "]+");
+  static final Pattern PLUS_CHARS_PATTERN = Pattern.compile("[" + PLUS_CHARS + "]+");
   private static final Pattern SEPARATOR_PATTERN = Pattern.compile("[" + VALID_PUNCTUATION + "]+");
   private static final Pattern CAPTURING_DIGIT_PATTERN = Pattern.compile("(" + DIGITS + ")");
 
@@ -213,7 +213,7 @@
   // not include other punctuation, as this will be stripped later during parsing and is of no
   // information value when parsing a number.
   private static final String VALID_START_CHAR = "[" + PLUS_CHARS + DIGITS + "]";
-  static final Pattern VALID_START_CHAR_PATTERN = Pattern.compile(VALID_START_CHAR);
+  private static final Pattern VALID_START_CHAR_PATTERN = Pattern.compile(VALID_START_CHAR);
 
   // Regular expression of characters typically used to start a second phone number for the purposes
   // of parsing. This allows us to strip off parts of the number that are actually the start of