Force the device to stay awake while we configure it

We need the display to be on to issue UI commands. Instead of trying
to wake up the device whenever we want it, force the display to stay
awake for the whole session.

Change-Id: I42a2d3f21c52ca25fca8d0efc4c769dd40070802
diff --git a/deploy.py b/deploy.py
index 7a8b5d9..9e7976a 100755
--- a/deploy.py
+++ b/deploy.py
@@ -71,6 +71,21 @@
         universal_newlines=True)
 
 
+def force_awake(serial, always=True):
+    """Force the device to stay awake."""
+    return adb('shell', 'svc power stayon {}'.format(
+        'true' if always else 'false'), serial=serial)
+
+
+def unlock(serial):
+    """Wake-up the device and unlock it."""
+    adb('shell', 'input keyevent KEYCODE_POWER', serial=serial)
+    time.sleep(1)
+    adb('shell', 'input keyevent KEYCODE_MENU', serial=serial)
+    time.sleep(1)
+    adb('shell', 'input keyevent KEYCODE_HOME', serial=serial)
+
+
 def getprop(serial, key):
     """Get system property of device.
 
@@ -277,6 +292,10 @@
 
         dut = Device(serial)
 
+        # Make sure the screen stays on - we're going to use UI automation
+        force_awake(serial)
+        unlock(serial)
+
         # Push the scenarios, their data, and install the apps
         prepare_dut(serial, '../scenarios', '../scenarios-data', PREBUILTS_PATH)
 
@@ -297,6 +316,9 @@
 
         configure_settings(dut)
 
+        # Leave the device alone now
+        force_awake(serial, always=False)
+
 
 if __name__ == '__main__':
     deploy()