diff --git a/src/main/java/io/appium/java_client/android/StartsActivity.java b/src/main/java/io/appium/java_client/android/StartsActivity.java index ea4c04ce5..f01d50998 100644 --- a/src/main/java/io/appium/java_client/android/StartsActivity.java +++ b/src/main/java/io/appium/java_client/android/StartsActivity.java @@ -40,13 +40,16 @@ public interface StartsActivity extends ExecutesMethod { * * * @param activity The {@link Activity} object + * @deprecated Use 'mobile: startActivity' extension instead */ + @Deprecated default void startActivity(Activity activity) { CommandExecutionHelper.execute(this, - startActivityCommand(activity.getAppPackage(), activity.getAppActivity(), - activity.getAppWaitPackage(), activity.getAppWaitActivity(), - activity.getIntentAction(), activity.getIntentCategory(), activity.getIntentFlags(), - activity.getOptionalIntentArguments(), activity.isStopApp())); + startActivityCommand(activity.getAppPackage(), activity.getAppActivity(), + activity.getAppWaitPackage(), activity.getAppWaitActivity(), + activity.getIntentAction(), activity.getIntentCategory(), activity.getIntentFlags(), + activity.getOptionalIntentArguments(), activity.isStopApp()) + ); } /** diff --git a/src/main/java/io/appium/java_client/android/SupportsNetworkStateManagement.java b/src/main/java/io/appium/java_client/android/SupportsNetworkStateManagement.java index 9100fbb69..9fdbd23c9 100644 --- a/src/main/java/io/appium/java_client/android/SupportsNetworkStateManagement.java +++ b/src/main/java/io/appium/java_client/android/SupportsNetworkStateManagement.java @@ -1,8 +1,13 @@ package io.appium.java_client.android; +import com.google.common.collect.ImmutableMap; import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; +import org.openqa.selenium.UnsupportedCommandException; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleAirplaneCommand; import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleDataCommand; import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleWifiCommand; @@ -13,21 +18,52 @@ public interface SupportsNetworkStateManagement extends ExecutesMethod { * Toggles Wifi on and off. */ default void toggleWifi() { - CommandExecutionHelper.execute(this, toggleWifiCommand()); + try { + Map result = checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: getConnectivity") + ); + CommandExecutionHelper.executeScript(this, "mobile: setConnectivity", ImmutableMap.of( + "wifi", !((Boolean) result.get("wifi")) + )); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + CommandExecutionHelper.execute(this, toggleWifiCommand()); + } } /** - * Toggle Airplane mode and this works on OS 6.0 and lesser - * and does not work on OS 7.0 and greater + * Toggle Airplane mode and this works on Android versions below + * 6 and above 10. */ default void toggleAirplaneMode() { - CommandExecutionHelper.execute(this, toggleAirplaneCommand()); + try { + Map result = checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: getConnectivity") + ); + CommandExecutionHelper.executeScript(this, "mobile: setConnectivity", ImmutableMap.of( + "airplaneMode", !((Boolean) result.get("airplaneMode")) + )); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + CommandExecutionHelper.execute(this, toggleAirplaneCommand()); + } } /** - * Toggle Mobile Data and this works on Emulator and rooted device. + * Toggle Mobile Data and this works on Emulators and real devices + * running Android version above 10. */ default void toggleData() { - CommandExecutionHelper.execute(this, toggleDataCommand()); + try { + Map result = checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: getConnectivity") + ); + CommandExecutionHelper.executeScript(this, "mobile: setConnectivity", ImmutableMap.of( + "data", !((Boolean) result.get("data")) + )); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + CommandExecutionHelper.execute(this, toggleDataCommand()); + } } } diff --git a/src/main/java/io/appium/java_client/android/nativekey/PressesKey.java b/src/main/java/io/appium/java_client/android/nativekey/PressesKey.java index 6c9f36212..9584ff517 100644 --- a/src/main/java/io/appium/java_client/android/nativekey/PressesKey.java +++ b/src/main/java/io/appium/java_client/android/nativekey/PressesKey.java @@ -16,10 +16,13 @@ package io.appium.java_client.android.nativekey; +import com.google.common.collect.ImmutableMap; import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; +import org.openqa.selenium.UnsupportedCommandException; import java.util.AbstractMap; +import java.util.Map; import static io.appium.java_client.MobileCommand.LONG_PRESS_KEY_CODE; import static io.appium.java_client.MobileCommand.PRESS_KEY_CODE; @@ -32,8 +35,13 @@ public interface PressesKey extends ExecutesMethod { * @param keyEvent The generated native key event */ default void pressKey(KeyEvent keyEvent) { - CommandExecutionHelper.execute(this, - new AbstractMap.SimpleEntry<>(PRESS_KEY_CODE, keyEvent.build())); + try { + CommandExecutionHelper.executeScript(this, "mobile: pressKey", keyEvent.build()); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + CommandExecutionHelper.execute(this, + new AbstractMap.SimpleEntry<>(PRESS_KEY_CODE, keyEvent.build())); + } } /** @@ -42,7 +50,16 @@ default void pressKey(KeyEvent keyEvent) { * @param keyEvent The generated native key event */ default void longPressKey(KeyEvent keyEvent) { - CommandExecutionHelper.execute(this, - new AbstractMap.SimpleEntry<>(LONG_PRESS_KEY_CODE, keyEvent.build())); + try { + Map args = ImmutableMap.builder() + .putAll(keyEvent.build()) + .put("isLongPress", true) + .build(); + CommandExecutionHelper.executeScript(this, "mobile: pressKey", args); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + CommandExecutionHelper.execute(this, + new AbstractMap.SimpleEntry<>(LONG_PRESS_KEY_CODE, keyEvent.build())); + } } }