charger: sync with the current key state on boot

If the power key was down when we booted, we would not have
gotten the down event and thus the device would not have rebooted until
the user released and pressed it again.

Change-Id: Iecb8c3dba773bce4647748715d056e8e1d77f7e0
Signed-off-by: Dima Zavin <dima@android.com>
diff --git a/charger/charger.c b/charger/charger.c
index ad0aab7..b63312c 100644
--- a/charger/charger.c
+++ b/charger/charger.c
@@ -745,30 +745,40 @@
     }
 }
 
-static void update_input_state(struct charger *charger,
-                               struct input_event *ev,
-                               int64_t now)
+static int set_key_callback(int code, int value, void *data)
 {
-    int down = !!ev->value;
+    struct charger *charger = data;
+    int64_t now = curr_time_ms();
+    int down = !!value;
 
-    if (ev->type != EV_KEY || ev->code > KEY_MAX)
-        return;
+    if (code > KEY_MAX)
+        return -1;
 
     /* only record the down even timestamp, as the amount
      * of time the key spent not being pressed is not useful */
     if (down)
-        charger->keys[ev->code].timestamp = now;
-    charger->keys[ev->code].down = down;
-    charger->keys[ev->code].pending = true;
+        charger->keys[code].timestamp = now;
+    charger->keys[code].down = down;
+    charger->keys[code].pending = true;
     if (down) {
-        LOGV("[%lld] key[%d] down\n", now, ev->code);
+        LOGV("[%lld] key[%d] down\n", now, code);
     } else {
-        int64_t duration = now - charger->keys[ev->code].timestamp;
+        int64_t duration = now - charger->keys[code].timestamp;
         int64_t secs = duration / 1000;
         int64_t msecs = duration - secs * 1000;
         LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
-            ev->code, secs, msecs);
+            code, secs, msecs);
     }
+
+    return 0;
+}
+
+static void update_input_state(struct charger *charger,
+                               struct input_event *ev)
+{
+    if (ev->type != EV_KEY)
+        return;
+    set_key_callback(ev->code, ev->value, charger);
 }
 
 static void set_next_key_check(struct charger *charger,
@@ -876,7 +886,7 @@
     ret = ev_get_input(fd, revents, &ev);
     if (ret)
         return -1;
-    update_input_state(charger, &ev, curr_time_ms());
+    update_input_state(charger, &ev);
     return 0;
 }
 
@@ -949,6 +959,8 @@
         }
     }
 
+    ev_sync_key_state(set_key_callback, charger);
+
     gr_fb_blank(true);
 
     charger->next_screen_transition = now - 1;