PrintSpooler crash if printer is reported to have only one color mode.

bug:11993304

Change-Id: I4932cf1b5b9b89f0cbf97011782ede037e7d814c
(cherry picked from commit b0d97b87ba5f23ee867f381a4e5916ebb2e5f762)
diff --git a/core/java/android/print/PrinterCapabilitiesInfo.java b/core/java/android/print/PrinterCapabilitiesInfo.java
index b615600..806a89d 100644
--- a/core/java/android/print/PrinterCapabilitiesInfo.java
+++ b/core/java/android/print/PrinterCapabilitiesInfo.java
@@ -475,6 +475,12 @@
          * @param colorModes The color mode bit mask.
          * @param defaultColorMode The default color mode.
          * @return This builder.
+         * <p>
+         * <strong>Note:</strong> On platform version 19 (Kitkat) specifying
+         * only PrintAttributes#COLOR_MODE_MONOCHROME leads to a print spooler
+         * crash. Hence, you should declare either both color modes or
+         * PrintAttributes#COLOR_MODE_COLOR.
+         * </p>
          *
          * @throws IllegalArgumentException If color modes contains an invalid
          *         mode bit or if the default color mode is invalid.
diff --git a/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java b/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
index c1c7a4e..f6008d4 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
@@ -2190,12 +2190,16 @@
                         // Select the old color mode - nothing really changed.
                         setColorModeSpinnerSelectionNoCallback(oldColorModeNewIndex);
                     } else {
-                        final int selectedColorModeIndex = Integer.numberOfTrailingZeros(
-                                    (colorModes & defaultAttributes.getColorMode()));
-                        setColorModeSpinnerSelectionNoCallback(selectedColorModeIndex);
-                        mCurrPrintAttributes.setColorMode(mColorModeSpinnerAdapter
-                                .getItem(selectedColorModeIndex).value);
-                        someAttributeSelectionChanged = true;
+                        final int selectedColorMode = colorModes & defaultAttributes.getColorMode();
+                        final int itemCount = mColorModeSpinnerAdapter.getCount();
+                        for (int i = 0; i < itemCount; i++) {
+                            SpinnerItem<Integer> item = mColorModeSpinnerAdapter.getItem(i);
+                            if (selectedColorMode == item.value) {
+                                setColorModeSpinnerSelectionNoCallback(i);
+                                mCurrPrintAttributes.setColorMode(selectedColorMode);
+                                someAttributeSelectionChanged = true;
+                            }
+                        }
                     }
                 }
                 mColorModeSpinner.setEnabled(true);