Fix handling of Privacy Impact screen on Android 5

Globally disable the Privacy Impact popup, as automating clicking
through the impact screens is error-prone.

Issue: INFRA-164
Issue: INFRA-30
Change-Id: I6789083aa7dbf146c99d19ab37a2ffb04362df6d
diff --git a/deploy.py b/deploy.py
index fd7d6ff..b29c5dd 100755
--- a/deploy.py
+++ b/deploy.py
@@ -324,6 +324,23 @@
     dut.press.back()
 
 
+def disable_privacy_impact_popup(dut):
+    """Disable Privacy Impact popup on Android 5.
+
+    This simplifies UI automation. Disabling the feature globally is more robust
+    than clicking through the the Privacy Impact screen per app.
+    """
+    print('Disabling the Privacy Impact screen…')
+    adb('shell',
+        ('am start -a android.intent.action.MAIN '
+            'com.fairphone.privacyimpact/.PrivacyImpactPreferenceActivity'),
+        serial=dut.serial
+    )
+    disable_privacy_impact_checkbox = dut(className='android.widget.CheckBox')
+    if not disable_privacy_impact_checkbox.checked:
+        disable_privacy_impact_checkbox.click()
+
+
 def get_proxy_apk(android_sdk, flavour):
     if android_sdk >= 24:
         return PREBUILT_PROXY_APK_PATTERN.format(sdk=24, flavour=flavour)
@@ -372,19 +389,6 @@
         .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') \
@@ -461,6 +465,10 @@
                 print('Uh oh, the device is running Android SDK {} on which we '
                     'do not deploy Wi-Fi networks yet.'.format(dut.sdk))
 
+            # Disable Privacy Impact popup on Android 5.
+            if dut.sdk <= 22:
+                disable_privacy_impact_popup(dut)
+
             # Push the scenarios, their data, and install the apps
             prepare_dut(
                 dut, '../scenarios', '../scenarios-data', PREBUILTS_PATH)