Support deployment on Android 5

- There was no permission screen before Android 6, so there is no
  adb flag to grant anything;
- After installing the viSer app, expect the Privacy Impact screen
  to show up, and handle it accordingly.

Change-Id: I8841f6337f4cfdc272e2a2d9bd7dcfbdc17729d1
diff --git a/deploy.py b/deploy.py
index ba2b8b8..fd7d6ff 100755
--- a/deploy.py
+++ b/deploy.py
@@ -231,14 +231,18 @@
     adb('uninstall', package, serial=serial)
 
 
-def install_apk(serial, filename, prebuilts_dir):
+def install_apk(dut, filename, prebuilts_dir):
     """Install apk from prebuilts_dir on device.
 
     Raises:
         DeviceCommandError: If the install command failed.
     """
     path = os.path.join(prebuilts_dir, filename)
-    adb('install', '-g', '-r', path, serial=serial)
+    command = ['install', '-r']
+    if dut.sdk >= 23:
+        # From Marshmallow onwards, adb has a flag to grant default permissions
+        command.append('-g')
+    adb(*command, path, serial=dut.serial)
 
 
 def configure_wifi_networks(dut, networks):
@@ -347,7 +351,7 @@
     # Install the smartviser apps (starting with the proxy app)
     for app in [proxy_apk] + PREBUILT_APKS:
         print('Installing `{}`…'.format(app))
-        install_apk(dut.serial, app, prebuilts_dir)
+        install_apk(dut, app, prebuilts_dir)
 
 
 # Grant the permissions through the UI
@@ -368,6 +372,19 @@
         .child(text=signin_label, className='android.widget.Button') \
         .click()
 
+    # Handle Privacy Impact
+    if dut.sdk <= 22:
+        start_button = dut(resourceId='com.fairphone.privacyimpact:id/start_the_app_button')
+        while not start_button.exists:
+            prompt = dut(resourceId='com.fairphone.privacyimpact:id/privacy_got_it')
+            if prompt.exists:
+                prompt.click()
+                dut(resourceId='com.fairphone.privacyimpact:id/notifications_got_it').click()
+            else:
+                print('Waiting for the Privacy Impact screen…')
+                time.sleep(1)
+        start_button.click()
+
 def configure_sms(dut):
     # TODO wait for the connection to be established and time-out
     prompt = dut(resourceId='android:id/content') \