From 9da2670f6df85bce85ea0e3e4fa4de5a594d4966 Mon Sep 17 00:00:00 2001 From: Peter Fong Date: Wed, 26 Jun 2019 13:24:37 -0700 Subject: Refactored out shell permission adoption routine --- .../mobly/snippet/bundled/WifiManagerSnippet.java | 39 ++++++++++++++-------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java index f14f379..442db0a 100644 --- a/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java +++ b/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java @@ -64,24 +64,17 @@ public class WifiManagerSnippet implements Snippet { private final JsonSerializer mJsonSerializer = new JsonSerializer(); private volatile boolean mIsScanResultAvailable = false; - public WifiManagerSnippet() throws WifiManagerSnippetException { + public WifiManagerSnippet() throws Throwable { mContext = InstrumentationRegistry.getInstrumentation().getContext(); mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + + // Starting in Android Q (29), additional restrictions are added for wifi operation. See + // below Android Q privacy changes for additional details. + // https://developer.android.com/preview/privacy/camera-connectivity if (mContext.getApplicationContext().getApplicationInfo().targetSdkVersion >= 29) { - UiAutomation uia = InstrumentationRegistry.getInstrumentation().getUiAutomation(); - uia.adoptShellPermissionIdentity(); - try { - Class cls = Class.forName("android.app.UiAutomation"); - Method destroyMethod = cls.getDeclaredMethod("destroy"); - destroyMethod.invoke(uia); - } catch (NoSuchMethodException - | IllegalAccessException - | ClassNotFoundException - | InvocationTargetException e) { - throw new WifiManagerSnippetException("Failed to cleaup Ui Automation", e); - } + adoptShellPermissionIdentity(); } } @@ -405,6 +398,26 @@ public class WifiManagerSnippet implements Snippet { @Override public void shutdown() {} + /** + * Elevate permission by adopting shell's identity. + * + * @throws Throwable + */ + private void adoptShellPermissionIdentity() throws Throwable { + UiAutomation uia = InstrumentationRegistry.getInstrumentation().getUiAutomation(); + uia.adoptShellPermissionIdentity(); + try { + Class cls = Class.forName("android.app.UiAutomation"); + Method destroyMethod = cls.getDeclaredMethod("destroy"); + destroyMethod.invoke(uia); + } catch (NoSuchMethodException + | IllegalAccessException + | ClassNotFoundException + | InvocationTargetException e) { + throw new WifiManagerSnippetException("Failed to cleaup Ui Automation", e); + } + } + private class WifiScanReceiver extends BroadcastReceiver { @Override -- cgit v1.2.3