Merge changes I1539580e,I8452e799 into pi-dev

* changes:
  Volume dialog mulit-streams have 8dp padding
  Check if alarm and now times are in schedule
diff --git a/core/java/android/service/notification/ScheduleCalendar.java b/core/java/android/service/notification/ScheduleCalendar.java
index 0128710..8b7946c 100644
--- a/core/java/android/service/notification/ScheduleCalendar.java
+++ b/core/java/android/service/notification/ScheduleCalendar.java
@@ -135,6 +135,24 @@
     }
 
     /**
+     * @param alarm milliseconds since Epoch
+     * @param now milliseconds since Epoch
+     * @return true if alarm and now is within the schedule, else false
+     */
+    public boolean isAlarmInSchedule(long alarm, long now) {
+        if (mSchedule == null || mDays.size() == 0) return false;
+        final long start = getTime(alarm, mSchedule.startHour, mSchedule.startMinute);
+        long end = getTime(alarm, mSchedule.endHour, mSchedule.endMinute);
+        if (end <= start) {
+            end = addDays(end, 1);
+        }
+        return (isInSchedule(-1, alarm, start, end)
+                && isInSchedule(-1, now, start, end))
+                || (isInSchedule(0, alarm, start, end)
+                && isInSchedule(0, now, start, end));
+    }
+
+    /**
      * @param time milliseconds since Epoch
      * @return true if should exit at time for next alarm, else false
      */
@@ -145,7 +163,7 @@
         return mSchedule.exitAtAlarm
                 && mSchedule.nextAlarm != 0
                 && time >= mSchedule.nextAlarm
-                && isInSchedule(mSchedule.nextAlarm);
+                && isAlarmInSchedule(mSchedule.nextAlarm, time);
     }
 
     private boolean isInSchedule(int daysOffset, long time, long start, long end) {
diff --git a/packages/SystemUI/res/layout/volume_dialog.xml b/packages/SystemUI/res/layout/volume_dialog.xml
index f6c2eeb..c70e829 100644
--- a/packages/SystemUI/res/layout/volume_dialog.xml
+++ b/packages/SystemUI/res/layout/volume_dialog.xml
@@ -53,7 +53,11 @@
                 android:layout_gravity="center"
                 android:soundEffectsEnabled="false" />
 
-            <include layout="@layout/volume_dnd_icon"/>
+            <include layout="@layout/volume_dnd_icon"
+                     android:layout_width="match_parent"
+                     android:layout_height="wrap_content"
+                     android:layout_marginRight="@dimen/volume_dialog_stream_padding"
+                     android:layout_marginTop="6dp"/>
         </FrameLayout>
 
         <LinearLayout
@@ -71,7 +75,9 @@
                 android:layout_height="wrap_content"
                 android:minWidth="@dimen/volume_dialog_panel_width"
                 android:gravity="center"
-                android:orientation="horizontal" >
+                android:orientation="horizontal"
+                android:paddingRight="@dimen/volume_dialog_stream_padding"
+                android:paddingLeft="@dimen/volume_dialog_stream_padding">
                     <!-- volume rows added and removed here! :-) -->
             </LinearLayout>
             <FrameLayout
diff --git a/packages/SystemUI/res/layout/volume_dnd_icon.xml b/packages/SystemUI/res/layout/volume_dnd_icon.xml
index ac235b7..037d143 100644
--- a/packages/SystemUI/res/layout/volume_dnd_icon.xml
+++ b/packages/SystemUI/res/layout/volume_dnd_icon.xml
@@ -17,14 +17,13 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/dnd_icon"
     android:layout_width="match_parent"
-    android:layout_height="wrap_content">
+    android:layout_height="wrap_content"
+    android:layout_marginTop="6dp">
 
     <ImageView
         android:layout_width="14dp"
         android:layout_height="14dp"
-        android:layout_marginTop="6dp"
-        android:layout_marginRight="6dp"
         android:layout_gravity="right|top"
         android:src="@drawable/ic_dnd"
         android:tint="?android:attr/textColorTertiary"/>
-</FrameLayout>
+</FrameLayout>
\ No newline at end of file
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index ad74725..f3fe297 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -296,6 +296,8 @@
 
     <dimen name="volume_dialog_panel_transparent_padding">20dp</dimen>
 
+    <dimen name="volume_dialog_stream_padding">8dp</dimen>
+
     <!-- the amount the volume panel should be offset at the end from the view next to it (or
     the screen edge, in portrait-->
     <dimen name="volume_dialog_base_margin">8dp</dimen>
diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java
index b0e40fc..6b322c7 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java
@@ -695,26 +695,6 @@
     private void enableVolumeRowViewsH(VolumeRow row, boolean enable) {
         boolean showDndIcon = !enable;
         row.dndIcon.setVisibility(showDndIcon ? VISIBLE : GONE);
-
-        if (showDndIcon && getNumVisibleRows() == 1) {
-            row.dndIcon.setLayoutParams(new FrameLayout.LayoutParams(
-                    mContext.getResources().getDimensionPixelSize(
-                            R.dimen.volume_dialog_panel_width),
-                    FrameLayout.LayoutParams.WRAP_CONTENT));
-        } else if (row.view.getVisibility() == VISIBLE) {
-            row.dndIcon.setLayoutParams(new FrameLayout.LayoutParams(
-                    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
-        }
-    }
-
-    private int getNumVisibleRows() {
-        int count = 0;
-        for (int i = 0; i < mRows.size(); i++) {
-            if (mRows.get(i).view.getVisibility() == VISIBLE) {
-                count++;
-            }
-        }
-        return count;
     }
 
     /**
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ScheduleCalendarTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ScheduleCalendarTest.java
index 1be1643..942a07ac 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ScheduleCalendarTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ScheduleCalendarTest.java
@@ -206,15 +206,14 @@
         assertTrue(mScheduleCalendar.shouldExitForAlarm(1000));
     }
 
-    @Ignore
     @Test
     public void testShouldExitForAlarm_oldAlarm() {
         // Cal: today 2:15pm
-        Calendar cal = new GregorianCalendar();
-        cal.set(Calendar.HOUR_OF_DAY, 14);
-        cal.set(Calendar.MINUTE, 15);
-        cal.set(Calendar.SECOND, 0);
-        cal.set(Calendar.MILLISECOND, 0);
+        Calendar now = new GregorianCalendar();
+        now.set(Calendar.HOUR_OF_DAY, 14);
+        now.set(Calendar.MINUTE, 15);
+        now.set(Calendar.SECOND, 0);
+        now.set(Calendar.MILLISECOND, 0);
 
         // ScheduleInfo: today 12:16pm  - today 3:15pm
         mScheduleInfo.days = new int[] {getTodayDay()};
@@ -226,10 +225,45 @@
         mScheduleInfo.nextAlarm = 1000; // very old alarm
 
         mScheduleCalendar.setSchedule(mScheduleInfo);
-        assertTrue(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
+        assertTrue(mScheduleCalendar.isInSchedule(now.getTimeInMillis()));
 
         // don't exit for an alarm if it's an old alarm
-        assertFalse(mScheduleCalendar.shouldExitForAlarm(1000));
+        assertFalse(mScheduleCalendar.shouldExitForAlarm(now.getTimeInMillis()));
+    }
+
+    @Test
+    public void testShouldExitForAlarm_oldAlarmInSchedule() {
+        // calNow: day 2 at 9pm
+        Calendar calNow = new GregorianCalendar();
+        calNow.set(Calendar.HOUR_OF_DAY, 21);
+        calNow.set(Calendar.MINUTE, 0);
+        calNow.set(Calendar.SECOND, 0);
+        calNow.set(Calendar.MILLISECOND, 0);
+        calNow.add(Calendar.DATE, 1); // add a day
+
+        // calAlarm: day 2 at 5am
+        Calendar calAlarm = new GregorianCalendar();
+        calAlarm.set(Calendar.HOUR_OF_DAY, 5);
+        calAlarm.set(Calendar.MINUTE, 0);
+        calAlarm.set(Calendar.SECOND, 0);
+        calAlarm.set(Calendar.MILLISECOND, 0);
+        calAlarm.add(Calendar.DATE, 1); // add a day
+
+        // ScheduleInfo: day 1, day 2: 9pm-7am
+        mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay() + 1};
+        mScheduleInfo.startHour = 21;
+        mScheduleInfo.endHour = 7;
+        mScheduleInfo.startMinute = 0;
+        mScheduleInfo.endMinute = 0;
+        mScheduleInfo.exitAtAlarm = true;
+        mScheduleInfo.nextAlarm = calAlarm.getTimeInMillis(); // old alarm (5am day 2)
+
+        mScheduleCalendar.setSchedule(mScheduleInfo);
+        assertTrue(mScheduleCalendar.isInSchedule(calNow.getTimeInMillis()));
+        assertTrue(mScheduleCalendar.isInSchedule(calAlarm.getTimeInMillis()));
+
+        // don't exit for an alarm if it's an old alarm
+        assertFalse(mScheduleCalendar.shouldExitForAlarm(calNow.getTimeInMillis()));
     }
 
     @Test
@@ -369,6 +403,64 @@
         assertFalse(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
     }
 
+    @Test
+    public void testIsAlarmInSchedule_alarmAndNowInSchedule_sameScheduleTrigger() {
+        Calendar alarm = new GregorianCalendar();
+        alarm.set(Calendar.HOUR_OF_DAY, 23);
+        alarm.set(Calendar.MINUTE, 15);
+        alarm.set(Calendar.SECOND, 0);
+        alarm.set(Calendar.MILLISECOND, 0);
+
+        Calendar now = new GregorianCalendar();
+        now.set(Calendar.HOUR_OF_DAY, 2);
+        now.set(Calendar.MINUTE, 15);
+        now.set(Calendar.SECOND, 0);
+        now.set(Calendar.MILLISECOND, 0);
+        now.add(Calendar.DATE, 1); // add a day
+
+        mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay() + 1};
+        mScheduleInfo.startHour = 22;
+        mScheduleInfo.startMinute = 15;
+        mScheduleInfo.endHour = 3;
+        mScheduleInfo.endMinute = 15;
+        mScheduleCalendar.setSchedule(mScheduleInfo);
+
+        assertTrue(mScheduleCalendar.isInSchedule(alarm.getTimeInMillis()));
+        assertTrue(mScheduleCalendar.isInSchedule(now.getTimeInMillis()));
+        assertTrue(mScheduleCalendar.isAlarmInSchedule(alarm.getTimeInMillis(),
+                now.getTimeInMillis()));
+    }
+
+    @Test
+    public void testIsAlarmInSchedule_alarmAndNowInSchedule_differentScheduleTrigger() {
+        Calendar alarm = new GregorianCalendar();
+        alarm.set(Calendar.HOUR_OF_DAY, 23);
+        alarm.set(Calendar.MINUTE, 15);
+        alarm.set(Calendar.SECOND, 0);
+        alarm.set(Calendar.MILLISECOND, 0);
+
+        Calendar now = new GregorianCalendar();
+        now.set(Calendar.HOUR_OF_DAY, 23);
+        now.set(Calendar.MINUTE, 15);
+        now.set(Calendar.SECOND, 0);
+        now.set(Calendar.MILLISECOND, 0);
+        now.add(Calendar.DATE, 1); // add a day
+
+        mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay() + 1};
+        mScheduleInfo.startHour = 22;
+        mScheduleInfo.startMinute = 15;
+        mScheduleInfo.endHour = 3;
+        mScheduleInfo.endMinute = 15;
+        mScheduleCalendar.setSchedule(mScheduleInfo);
+
+        // even though both alarm and now are in schedule, they are not in the same part of
+        // the schedule (alarm is in schedule for the previous day's schedule compared to now)
+        assertTrue(mScheduleCalendar.isInSchedule(alarm.getTimeInMillis()));
+        assertTrue(mScheduleCalendar.isInSchedule(now.getTimeInMillis()));
+        assertFalse(mScheduleCalendar.isAlarmInSchedule(alarm.getTimeInMillis(),
+                now.getTimeInMillis()));
+    }
+
     private int getTodayDay() {
         return new GregorianCalendar().get(Calendar.DAY_OF_WEEK);
     }