Support deployment to Android 7 devices

- Adapt the UI actions;
- Take into account the new naming scheme between the flavours;
- Change the way the proxy app is found based on the new naming scheme
  characterised by the Android SDK version and the build flavour.

Change-Id: I3493771355a9af61aed90043707a95513f2be90f
diff --git a/deploy.py b/deploy.py
index 47bcf55..1dc9692 100755
--- a/deploy.py
+++ b/deploy.py
@@ -17,14 +17,12 @@
 
 PREBUILTS_PATH = '../../vendor/smartviser/viser/prebuilts/apk'
 
-PREBUILT_PROXY_APKS = {
-    'gms': 'com.lunarlabs.panda.proxy.apk',
-    'sibon': 'com.lunarlabs.panda.proxy-sibon.apk',
-}
+PREBUILT_PROXY_APK_PATTERN = (
+    'com.lunarlabs.panda.proxy-latest-sdk{sdk}-{flavour}.apk')
 
 PREBUILT_APKS = [
-    'com.smartviser.demogame.apk',
-    'com.lunarlabs.panda.apk',
+    'com.smartviser.demogame-latest.apk',
+    'com.lunarlabs.panda-latest.apk',
 ]
 
 FAIRPHONE_WIFI_NETWORKS = {
@@ -155,17 +153,21 @@
         'true' if always else 'false'), serial=serial)
 
 
-def unlock(serial):
+def unlock(dut):
     """Wake-up the device and unlock it.
 
     Raises:
         DeviceCommandError: If the underlying adb commands failed.
     """
-    adb('shell', 'input keyevent KEYCODE_POWER', serial=serial)
+    if not dut.info['screenOn']:
+        adb('shell', 'input keyevent KEYCODE_POWER', serial=dut.serial)
+        time.sleep(1)
+    # The KEYCODE_MENU input is enough to unlock a "swipe up to unlock"
+    # lockscreen on Android 6, but unfortunately not Android 7. So we use a
+    # swipe up (that depends on the screen resolution) instead.
+    adb('shell', 'input touchscreen swipe 930 880 930 380', serial=dut.serial)
     time.sleep(1)
-    adb('shell', 'input keyevent KEYCODE_MENU', serial=serial)
-    time.sleep(1)
-    adb('shell', 'input keyevent KEYCODE_HOME', serial=serial)
+    adb('shell', 'input keyevent KEYCODE_HOME', serial=dut.serial)
 
 
 def getprop(serial, key):
@@ -200,7 +202,9 @@
         True if device runs GMS, false otherwise.
     :raise DeviceCommandError: If the underlying adb command failed.
     """
-    return getprop(serial, 'ro.build.id').startswith('FP2-gms-')
+    return (
+        getprop(serial, 'ro.build.id').startswith('FP2-gms-')
+        or getprop(serial, 'ro.build.version.incremental').startswith('gms-'))
 
 
 def uninstall_apk(serial, filename, prebuilts_dir):
@@ -316,27 +320,34 @@
     dut.press.back()
 
 
+def get_proxy_apk(android_sdk, flavour):
+    if android_sdk >= 24:
+        return PREBUILT_PROXY_APK_PATTERN.format(sdk=24, flavour=flavour)
+    else:
+        return PREBUILT_PROXY_APK_PATTERN.format(sdk=19, flavour=flavour)
+
 # Prepare the DUT
-def prepare_dut(serial, scenarios_dir, data_dir, prebuilts_dir):
-    flavour = 'gms' if is_gms_device(serial) else 'sibon'
+def prepare_dut(dut, scenarios_dir, data_dir, prebuilts_dir):
+    flavour = 'gms' if is_gms_device(dut.serial) else 'sibon'
+    proxy_apk = get_proxy_apk(dut.sdk, flavour)
 
     # Uninstall the smartviser apps
-    for app in PREBUILT_APKS + [PREBUILT_PROXY_APKS[flavour]]:
+    for app in PREBUILT_APKS + [proxy_apk]:
         print('Uninstalling `{}`…'.format(app))
-        uninstall_apk(serial, app, prebuilts_dir)
+        uninstall_apk(dut.serial, app, prebuilts_dir)
 
     # Copy the scenarios
     print('Pushing scenarios from `{}`…'.format(scenarios_dir))
-    adb('push', scenarios_dir, '/sdcard/viser', serial=serial)
+    adb('push', scenarios_dir, '/sdcard/viser', serial=dut.serial)
 
     # Copy the scenarios data
     print('Pushing scenarios data from `{}`…'.format(data_dir))
-    adb('push', data_dir, '/sdcard/viser/data', serial=serial)
+    adb('push', data_dir, '/sdcard/viser/data', serial=dut.serial)
 
     # Install the smartviser apps (starting with the proxy app)
-    for app in [PREBUILT_PROXY_APKS[flavour]] + PREBUILT_APKS:
+    for app in [proxy_apk] + PREBUILT_APKS:
         print('Installing `{}`…'.format(app))
-        install_apk(serial, app, prebuilts_dir)
+        install_apk(dut.serial, app, prebuilts_dir)
 
 
 # Grant the permissions through the UI
@@ -365,8 +376,9 @@
         .set_text(VWS_CREDENTIALS['password'])
 
     # Sign in
+    signin_label = 'SIGN IN' if dut.sdk >= 24 else 'Sign in'
     dut(resourceId='android:id/content') \
-        .child(text='Sign in', className='android.widget.Button') \
+        .child(text=signin_label, className='android.widget.Button') \
         .click()
 
 def configure_sms(dut):
@@ -430,18 +442,24 @@
         dut = Device(serial)
         # Work around the not-so-easy Device class
         dut.serial = serial
+        # Cache the Android SDK version (dut.info fetches system properties)
+        dut.sdk = dut.info['sdkInt']
 
         try:
             # Make sure the screen stays on - we're going to use UI automation
             force_awake(serial)
-            unlock(serial)
+            unlock(dut)
 
             # Configure common Fairphone Wi-Fi networks
-            configure_wifi_networks(dut, FAIRPHONE_WIFI_NETWORKS)
+            if dut.sdk < 24:
+                configure_wifi_networks(dut, FAIRPHONE_WIFI_NETWORKS)
+            else:
+                print('Uh oh, the device is running Android SDK {} on which we '
+                    'do not deploy Wi-Fi networks yet.'.format(dut.sdk))
 
             # Push the scenarios, their data, and install the apps
             prepare_dut(
-                serial, '../scenarios', '../scenarios-data', PREBUILTS_PATH)
+                dut, '../scenarios', '../scenarios-data', PREBUILTS_PATH)
 
             # Start the viser app
             adb(