Merge "Page range validation edge case handling." into klp-dev
diff --git a/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java b/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
index 05b0b69..48d7be0 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
@@ -77,6 +77,8 @@
 import android.widget.Spinner;
 import android.widget.TextView;
 
+import libcore.io.IoUtils;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -92,8 +94,6 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import libcore.io.IoUtils;
-
 /**
  * Activity for configuring a print job.
  */
@@ -144,7 +144,8 @@
             "(?=[]\\[+&|!(){}^\"~*?:\\\\])");
 
     private static final Pattern PATTERN_PAGE_RANGE = Pattern.compile(
-            "([0-9]+[\\s]*[\\-]?[\\s]*[0-9]*[\\s]*[,]?[\\s]*)+");
+            "[\\s]*[0-9]*[\\s]*[\\-]?[\\s]*[0-9]*[\\s]*?(([,])"
+            + "[\\s]*[0-9]*[\\s]*[\\-]?[\\s]*[0-9]*[\\s]*|[\\s]*)+");
 
     public static final PageRange[] ALL_PAGES_ARRAY = new PageRange[] {PageRange.ALL_PAGES};
 
@@ -1076,9 +1077,13 @@
                     return;
                 }
 
+                // The range
                 Matcher matcher = PATTERN_DIGITS.matcher(text);
                 while (matcher.find()) {
-                    String numericString = text.substring(matcher.start(), matcher.end());
+                    String numericString = text.substring(matcher.start(), matcher.end()).trim();
+                    if (TextUtils.isEmpty(numericString)) {
+                        continue;
+                    }
                     final int pageIndex = Integer.parseInt(numericString);
                     if (pageIndex < 1 || pageIndex > mDocument.info.getPageCount()) {
                         mPageRangeEditText.setError("");
@@ -1506,18 +1511,21 @@
 
                 while (mStringCommaSplitter.hasNext()) {
                     String range = mStringCommaSplitter.next().trim();
+                    if (TextUtils.isEmpty(range)) {
+                        continue;
+                    }
                     final int dashIndex = range.indexOf('-');
                     final int fromIndex;
                     final int toIndex;
 
                     if (dashIndex > 0) {
-                        fromIndex = Integer.parseInt(range.substring(0, dashIndex)) - 1;
+                        fromIndex = Integer.parseInt(range.substring(0, dashIndex).trim()) - 1;
                         // It is possible that the dash is at the end since the input
                         // verification can has to allow the user to keep entering if
                         // this would lead to a valid input. So we handle this.
                         toIndex = (dashIndex < range.length() - 1)
                                 ? Integer.parseInt(range.substring(dashIndex + 1,
-                                        range.length())) - 1 : fromIndex;
+                                        range.length()).trim()) - 1 : fromIndex;
                     } else {
                         fromIndex = toIndex = Integer.parseInt(range) - 1;
                     }