am 4fc2ea84: Merge "Ensure time picker dialog fits on small screens" into lmp-mr1-dev

* commit '4fc2ea8402b62001b225bf4e1a335c4cbf3b65ea':
  Ensure time picker dialog fits on small screens
diff --git a/core/java/android/content/res/ColorStateList.java b/core/java/android/content/res/ColorStateList.java
index 3c290f7..68a39d3 100644
--- a/core/java/android/content/res/ColorStateList.java
+++ b/core/java/android/content/res/ColorStateList.java
@@ -353,7 +353,7 @@
         for (int i = 0; i < inputStates.length; i++) {
             final int[] inputState = inputStates[i];
             for (int j = 0; j < inputState.length; j++) {
-                if (inputState[i] == state) {
+                if (inputState[j] == state) {
                     return colorStateList;
                 }
             }
diff --git a/core/java/android/widget/TimePickerClockDelegate.java b/core/java/android/widget/TimePickerClockDelegate.java
index 7d01321..d61b6fc 100644
--- a/core/java/android/widget/TimePickerClockDelegate.java
+++ b/core/java/android/widget/TimePickerClockDelegate.java
@@ -23,7 +23,6 @@
 import android.content.res.TypedArray;
 import android.os.Parcel;
 import android.os.Parcelable;
-import android.text.TextUtils;
 import android.text.format.DateFormat;
 import android.text.format.DateUtils;
 import android.util.AttributeSet;
@@ -100,9 +99,7 @@
     private int mPmKeyCode;
 
     // Accessibility strings.
-    private String mHourPickerDescription;
     private String mSelectHours;
-    private String mMinutePickerDescription;
     private String mSelectMinutes;
 
     // Most recent time announcement values for accessibility.
@@ -122,9 +119,7 @@
                 Context.LAYOUT_INFLATER_SERVICE);
         final Resources res = mContext.getResources();
 
-        mHourPickerDescription = res.getString(R.string.hour_picker_description);
         mSelectHours = res.getString(R.string.select_hours);
-        mMinutePickerDescription = res.getString(R.string.minute_picker_description);
         mSelectMinutes = res.getString(R.string.select_minutes);
 
         String[] amPmStrings = TimePickerSpinnerDelegate.getAmPmStrings(context);
@@ -153,6 +148,11 @@
             mMinuteView.setTextAppearance(context, headerTimeTextAppearance);
         }
 
+        // Now that we have text appearances out of the way, make sure the hour
+        // and minute views are correctly sized.
+        mHourView.setMinWidth(computeStableWidth(mHourView, 24));
+        mMinuteView.setMinWidth(computeStableWidth(mMinuteView, 60));
+
         // TODO: This can be removed once we support themed color state lists.
         final int headerSelectedTextColor = a.getColor(
                 R.styleable.TimePicker_headerSelectedTextColor,
@@ -206,6 +206,23 @@
         initialize(currentHour, currentMinute, false /* 12h */, HOUR_INDEX);
     }
 
+    private int computeStableWidth(TextView v, int maxNumber) {
+        int maxWidth = 0;
+
+        for (int i = 0; i < maxNumber; i++) {
+            final String text = String.format("%02d", i);
+            v.setText(text);
+            v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
+
+            final int width = v.getMeasuredWidth();
+            if (width > maxWidth) {
+                maxWidth = width;
+            }
+        }
+
+        return maxWidth;
+    }
+
     private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) {
         mInitialHourOfDay = hourOfDay;
         mInitialMinute = minute;
@@ -242,48 +259,21 @@
         setCurrentItemShowing(index, false, true);
     }
 
-    private int computeMaxWidthOfNumbers(int max) {
-        TextView tempView = new TextView(mContext);
-        tempView.setTextAppearance(mContext, R.style.TextAppearance_Material_TimePicker_TimeLabel);
-        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
-                ViewGroup.LayoutParams.WRAP_CONTENT);
-        tempView.setLayoutParams(lp);
-        int maxWidth = 0;
-        for (int minutes = 0; minutes < max; minutes++) {
-            final String text = String.format("%02d", minutes);
-            tempView.setText(text);
-            tempView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
-            maxWidth = Math.max(maxWidth, tempView.getMeasuredWidth());
-        }
-        return maxWidth;
-    }
-
     private void updateHeaderAmPm() {
         if (mIs24HourView) {
             mAmPmLayout.setVisibility(View.GONE);
         } else {
-            final String bestDateTimePattern = DateFormat.getBestDateTimePattern(
-                    mCurrentLocale, "hm");
-            boolean amPmOnLeft = bestDateTimePattern.startsWith("a");
-            if (TextUtils.getLayoutDirectionFromLocale(mCurrentLocale) ==
-                    View.LAYOUT_DIRECTION_RTL) {
-                amPmOnLeft = !amPmOnLeft;
+            // Ensure that AM/PM layout is in the correct position.
+            final String dateTimePattern = DateFormat.getBestDateTimePattern(mCurrentLocale, "hm");
+            final boolean amPmAtStart = dateTimePattern.startsWith("a");
+            final ViewGroup parent = (ViewGroup) mAmPmLayout.getParent();
+            final int targetIndex = amPmAtStart ? 0 : parent.getChildCount() - 1;
+            final int currentIndex = parent.indexOfChild(mAmPmLayout);
+            if (targetIndex != currentIndex) {
+                parent.removeView(mAmPmLayout);
+                parent.addView(mAmPmLayout, targetIndex);
             }
 
-            final ViewGroup.MarginLayoutParams params =
-                    (ViewGroup.MarginLayoutParams) mAmPmLayout.getLayoutParams();
-
-            if (amPmOnLeft) {
-                params.leftMargin = 0;
-                params.rightMargin = computeMaxWidthOfNumbers(12 /* for hours */);
-            } else {
-                params.leftMargin = computeMaxWidthOfNumbers(60 /* for minutes */);
-                params.rightMargin = 0;
-            }
-
-            mAmPmLayout.setLayoutParams(params);
-            mAmPmLayout.setVisibility(View.VISIBLE);
-
             updateAmPmLabelStates(mInitialHourOfDay < 12 ? AM : PM);
         }
     }
diff --git a/core/res/res/layout-land/time_picker_holo.xml b/core/res/res/layout-land/time_picker_holo.xml
index ce90a5b..f6923ee 100644
--- a/core/res/res/layout-land/time_picker_holo.xml
+++ b/core/res/res/layout-land/time_picker_holo.xml
@@ -20,11 +20,7 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:orientation="horizontal"
-    android:layout_marginLeft="@dimen/timepicker_minimum_margin_sides"
-    android:layout_marginRight="@dimen/timepicker_minimum_margin_sides"
-    android:layout_marginTop="@dimen/timepicker_minimum_margin_top_bottom"
-    android:layout_marginBottom="@dimen/timepicker_minimum_margin_top_bottom">
+    android:orientation="horizontal">
     <FrameLayout
         android:layout_width="0dp"
         android:layout_height="match_parent"
diff --git a/core/res/res/layout/time_header_label.xml b/core/res/res/layout/time_header_label.xml
index efb3628..b1625c7 100644
--- a/core/res/res/layout/time_header_label.xml
+++ b/core/res/res/layout/time_header_label.xml
@@ -17,46 +17,50 @@
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/time_header"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
-    <RelativeLayout
-        android:layout_width="match_parent"
-        android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:padding="@dimen/timepicker_separator_padding"
+    android:gravity="center">
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:gravity="bottom"
+        android:orientation="horizontal">
+
         <TextView
             android:id="@+id/hours"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_toLeftOf="@+id/separator"
-            android:layout_centerVertical="true" />
+            android:gravity="end" />
+
         <TextView
             android:id="@+id/separator"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:paddingLeft="@dimen/timepicker_separator_padding"
-            android:paddingRight="@dimen/timepicker_separator_padding"
-            android:layout_centerInParent="true"
+            android:layout_marginLeft="@dimen/timepicker_separator_padding"
+            android:layout_marginRight="@dimen/timepicker_separator_padding"
             android:importantForAccessibility="no" />
+
         <TextView
             android:id="@+id/minutes"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_toRightOf="@+id/separator"
-            android:layout_centerVertical="true" />
+            android:gravity="start" />
+
         <LinearLayout
             android:id="@+id/ampm_layout"
-            android:layout_alignBaseline="@+id/minutes"
-            android:layout_toEndOf="@+id/separator"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:baselineAlignedChildIndex="1"
-            android:orientation="vertical">
+            android:orientation="vertical"
+            android:baselineAlignedChildIndex="1">
             <CheckedTextView
                 android:id="@+id/am_label"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:paddingStart="@dimen/timepicker_ampm_horizontal_padding"
-                android:paddingTop="@dimen/timepicker_ampm_vertical_padding"
                 android:paddingEnd="@dimen/timepicker_ampm_horizontal_padding"
-                android:paddingBottom="@dimen/timepicker_am_bottom_padding"
+                android:paddingTop="@dimen/timepicker_ampm_vertical_padding"
                 android:lines="1"
                 android:ellipsize="none" />
             <CheckedTextView
@@ -64,11 +68,10 @@
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:paddingStart="@dimen/timepicker_ampm_horizontal_padding"
-                android:paddingTop="@dimen/timepicker_pm_top_padding"
                 android:paddingEnd="@dimen/timepicker_ampm_horizontal_padding"
-                android:paddingBottom="@dimen/timepicker_ampm_vertical_padding"
+                android:paddingTop="@dimen/timepicker_pm_top_padding"
                 android:lines="1"
                 android:ellipsize="none" />
         </LinearLayout>
-    </RelativeLayout>
+    </LinearLayout>
 </FrameLayout>
diff --git a/core/res/res/layout/time_picker_holo.xml b/core/res/res/layout/time_picker_holo.xml
index 08d2211..d04fbb6 100644
--- a/core/res/res/layout/time_picker_holo.xml
+++ b/core/res/res/layout/time_picker_holo.xml
@@ -18,7 +18,7 @@
 -->
 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="wrap_content"
+    android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
     <include
diff --git a/core/res/res/values/dimens.xml b/core/res/res/values/dimens.xml
index 13e3be5..b4e033c 100644
--- a/core/res/res/values/dimens.xml
+++ b/core/res/res/values/dimens.xml
@@ -372,8 +372,7 @@
     <dimen name="timepicker_ampm_label_size">16sp</dimen>
     <dimen name="timepicker_ampm_horizontal_padding">12dp</dimen>
     <dimen name="timepicker_ampm_vertical_padding">16dp</dimen>
-    <dimen name="timepicker_am_bottom_padding">1dp</dimen>
-    <dimen name="timepicker_pm_top_padding">2dp</dimen>
+    <dimen name="timepicker_pm_top_padding">3dp</dimen>
     <dimen name="timepicker_separator_padding">4dp</dimen>
     <dimen name="timepicker_header_height">96dp</dimen>
     <dimen name="timepicker_minimum_margin_sides">48dp</dimen>