Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public interface StartsActivity extends ExecutesMethod {
* </pre>
*
* @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())
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,21 +18,52 @@ public interface SupportsNetworkStateManagement extends ExecutesMethod {
* Toggles Wifi on and off.
*/
default void toggleWifi() {
CommandExecutionHelper.execute(this, toggleWifiCommand());
try {
Map<String, Object> 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<String, Object> 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<String, Object> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
}
}

/**
Expand All @@ -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<String, Object> args = ImmutableMap.<String, Object>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()));
}
}
}