Fix alarms with negative or very large wakup times.

When the wakeup time is negative, the kernel /dev/alarm driver
never triggers the alarm.  This can cause alarms to back up in the
priority queue since an alarm at the head with a negative wakup time
will never be triggered.  Now we use 0 as the wakup time which causes
an immediate triggering.

When the wakeup time is very large, it is possible for a numeric
overflow to occur when converting the timestamp from milliseconds
since epoch to nanoseconds.  This has been fixed by avoiding the
intermediate conversion in the JNI call so that overflow cannot
occur.

Bug: b/2558820
Change-Id: I4f5b4646a04090cc749a9fc5d3982a68402954ef
diff --git a/services/java/com/android/server/AlarmManagerService.java b/services/java/com/android/server/AlarmManagerService.java
index 1ffb427..e088417 100644
--- a/services/java/com/android/server/AlarmManagerService.java
+++ b/services/java/com/android/server/AlarmManagerService.java
@@ -400,7 +400,7 @@
     }
     
     public long timeToNextAlarm() {
-        long nextAlarm = 0xfffffffffffffffl;
+        long nextAlarm = Long.MAX_VALUE;
         synchronized (mLock) {
             for (int i=AlarmManager.RTC_WAKEUP;
                     i<=AlarmManager.ELAPSED_REALTIME; i++) {
@@ -420,7 +420,18 @@
     {
         if (mDescriptor != -1)
         {
-            set(mDescriptor, alarm.type, (alarm.when * 1000 * 1000));
+            // The kernel never triggers alarms with negative wakeup times
+            // so we ensure they are positive.
+            long alarmSeconds, alarmNanoseconds;
+            if (alarm.when < 0) {
+                alarmSeconds = 0;
+                alarmNanoseconds = 0;
+            } else {
+                alarmSeconds = alarm.when / 1000;
+                alarmNanoseconds = (alarm.when % 1000) * 1000 * 1000;
+            }
+            
+            set(mDescriptor, alarm.type, alarmSeconds, alarmNanoseconds);
         }
         else
         {
@@ -499,7 +510,7 @@
     
     private native int init();
     private native void close(int fd);
-    private native void set(int fd, int type, long nanoseconds);
+    private native void set(int fd, int type, long seconds, long nanoseconds);
     private native int waitForAlarm(int fd);
     private native int setKernelTimezone(int fd, int minuteswest);