121373fa4SWill Schurman package host.exp.exponent.utils 221373fa4SWill Schurman 321373fa4SWill Schurman import android.content.Intent 421373fa4SWill Schurman import android.net.Uri 521373fa4SWill Schurman import android.provider.Settings 621373fa4SWill Schurman import android.widget.Switch 721373fa4SWill Schurman import androidx.test.InstrumentationRegistry 821373fa4SWill Schurman import androidx.test.uiautomator.* 921373fa4SWill Schurman 1021373fa4SWill Schurman private const val LAUNCH_TIMEOUT = 5000 1121373fa4SWill Schurman 1221373fa4SWill Schurman object DeviceUtils { allowDrawingOverOtherAppsnull1321373fa4SWill Schurman fun allowDrawingOverOtherApps(uiDevice: UiDevice) { 1421373fa4SWill Schurman // Start from the home screen 1521373fa4SWill Schurman uiDevice.pressHome() 1621373fa4SWill Schurman 1721373fa4SWill Schurman // Wait for launcher 1821373fa4SWill Schurman val launcherPackage = uiDevice.launcherPackageName 1921373fa4SWill Schurman uiDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT.toLong()) 2021373fa4SWill Schurman 2121373fa4SWill Schurman val context = InstrumentationRegistry.getContext() 2221373fa4SWill Schurman // Enable draw over other apps if necessary 23*d211fd84SWojciech Dróżdż if (!Settings.canDrawOverlays(context)) { 2421373fa4SWill Schurman // Open settings 2521373fa4SWill Schurman val intent = 2621373fa4SWill Schurman Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:host.exp.exponent")) 2721373fa4SWill Schurman intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 2821373fa4SWill Schurman intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) 2921373fa4SWill Schurman context.startActivity(intent) 3021373fa4SWill Schurman 3121373fa4SWill Schurman // Wait for the app to appear 3221373fa4SWill Schurman uiDevice.wait( 3321373fa4SWill Schurman Until.hasObject(By.textContains("Permit drawing over other apps")), 3421373fa4SWill Schurman LAUNCH_TIMEOUT.toLong() 3521373fa4SWill Schurman ) 3621373fa4SWill Schurman val switchObject = uiDevice.findObject(UiSelector().className(Switch::class.java.name)) 3721373fa4SWill Schurman try { 3821373fa4SWill Schurman if (!switchObject.isChecked) { 3921373fa4SWill Schurman switchObject.click() 4021373fa4SWill Schurman } 4121373fa4SWill Schurman } catch (e: UiObjectNotFoundException) { 4221373fa4SWill Schurman e.printStackTrace() 4321373fa4SWill Schurman } 4421373fa4SWill Schurman } 4521373fa4SWill Schurman } 4621373fa4SWill Schurman } 47