Skip to content

Commit d163f8d

Browse files
feat: Switch more Android helpers to use extensions (#1898)
1 parent 6a4adb7 commit d163f8d

File tree

9 files changed

+190
-43
lines changed

9 files changed

+190
-43
lines changed

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public class MobileCommand {
112112
protected static final String TOUCH_ID_ENROLLMENT;
113113
//Android
114114
@Deprecated
115-
protected static final String CURRENT_ACTIVITY;
115+
public static final String CURRENT_ACTIVITY;
116116
@Deprecated
117117
protected static final String END_TEST_COVERAGE;
118118
@Deprecated
@@ -149,21 +149,21 @@ public class MobileCommand {
149149
@Deprecated
150150
protected static final String SET_SETTINGS;
151151
@Deprecated
152-
protected static final String GET_CURRENT_PACKAGE;
152+
public static final String GET_CURRENT_PACKAGE;
153153
@Deprecated
154-
protected static final String SEND_SMS;
154+
public static final String SEND_SMS;
155155
@Deprecated
156-
protected static final String GSM_CALL;
156+
public static final String GSM_CALL;
157157
@Deprecated
158-
protected static final String GSM_SIGNAL;
158+
public static final String GSM_SIGNAL;
159159
@Deprecated
160-
protected static final String GSM_VOICE;
160+
public static final String GSM_VOICE;
161161
@Deprecated
162-
protected static final String NETWORK_SPEED;
162+
public static final String NETWORK_SPEED;
163163
@Deprecated
164-
protected static final String POWER_CAPACITY;
164+
public static final String POWER_CAPACITY;
165165
@Deprecated
166-
protected static final String POWER_AC_STATE;
166+
public static final String POWER_AC_STATE;
167167
@Deprecated
168168
protected static final String TOGGLE_WIFI;
169169
@Deprecated

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ public AndroidDriver(URL remoteSessionAddress, String automationName) {
255255
*
256256
* @param intent intent to broadcast.
257257
* @param path path to .ec file.
258+
* @deprecated This API will be removed
258259
*/
260+
@Deprecated
259261
public void endTestCoverage(String intent, String path) {
260262
CommandExecutionHelper.execute(this, endTestCoverageCommand(intent, path));
261263
}

src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
3737
*
3838
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
3939
*/
40+
@Deprecated
4041
public static Map.Entry<String, Map<String, ?>> currentActivityCommand() {
4142
return new AbstractMap.SimpleEntry<>(CURRENT_ACTIVITY, ImmutableMap.of());
4243
}
@@ -46,6 +47,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
4647
*
4748
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
4849
*/
50+
@Deprecated
4951
public static Map.Entry<String, Map<String, ?>> currentPackageCommand() {
5052
return new AbstractMap.SimpleEntry<>(GET_CURRENT_PACKAGE, ImmutableMap.of());
5153
}
@@ -57,6 +59,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
5759
* @param path path to .ec file.
5860
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
5961
*/
62+
@Deprecated
6063
public static Map.Entry<String, Map<String, ?>> endTestCoverageCommand(String intent,
6164
String path) {
6265
String[] parameters = new String[] {"intent", "path"};
@@ -117,6 +120,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
117120
*
118121
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
119122
*/
123+
@Deprecated
120124
public static Map.Entry<String, Map<String, ?>> getDisplayDensityCommand() {
121125
return new AbstractMap.SimpleEntry<>(GET_DISPLAY_DENSITY, ImmutableMap.of());
122126
}
@@ -126,6 +130,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
126130
*
127131
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
128132
*/
133+
@Deprecated
129134
public static Map.Entry<String, Map<String, ?>> getNetworkConnectionCommand() {
130135
return new AbstractMap.SimpleEntry<>(GET_NETWORK_CONNECTION, ImmutableMap.of());
131136
}
@@ -136,6 +141,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
136141
*
137142
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
138143
*/
144+
@Deprecated
139145
public static Map.Entry<String, Map<String, ?>> getSystemBarsCommand() {
140146
return new AbstractMap.SimpleEntry<>(GET_SYSTEM_BARS, ImmutableMap.of());
141147
}
@@ -145,6 +151,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
145151
*
146152
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
147153
*/
154+
@Deprecated
148155
public static Map.Entry<String, Map<String, ?>> isLockedCommand() {
149156
return new AbstractMap.SimpleEntry<>(IS_LOCKED, ImmutableMap.of());
150157
}
@@ -155,6 +162,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
155162
* @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)
156163
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
157164
*/
165+
@Deprecated
158166
public static Map.Entry<String, Map<String, ?>> fingerPrintCommand(int fingerPrintId) {
159167
return new AbstractMap.SimpleEntry<>(FINGER_PRINT,
160168
prepareArguments("fingerprintId", fingerPrintId));
@@ -165,6 +173,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
165173
*
166174
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
167175
*/
176+
@Deprecated
168177
public static Map.Entry<String, Map<String, ?>> openNotificationsCommand() {
169178
return new AbstractMap.SimpleEntry<>(OPEN_NOTIFICATIONS, ImmutableMap.of());
170179
}
@@ -175,6 +184,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
175184
* @param bitMask The bitmask of the desired connection
176185
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
177186
*/
187+
@Deprecated
178188
public static Map.Entry<String, Map<String, ?>> setConnectionCommand(long bitMask) {
179189
String[] parameters = new String[] {"name", "parameters"};
180190
Object[] values = new Object[] {"network_connection", ImmutableMap.of("type", bitMask)};
@@ -198,6 +208,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
198208
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
199209
* @throws IllegalArgumentException when any required argument is empty
200210
*/
211+
@Deprecated
201212
public static Map.Entry<String, Map<String, ?>> startActivityCommand(String appPackage,
202213
String appActivity, String appWaitPackage, String appWaitActivity,
203214
String intentAction, String intentCategory, String intentFlags,
@@ -234,6 +245,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
234245
*
235246
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
236247
*/
248+
@Deprecated
237249
public static Map.Entry<String, Map<String, ?>> toggleLocationServicesCommand() {
238250
return new AbstractMap.SimpleEntry<>(TOGGLE_LOCATION_SERVICES, ImmutableMap.of());
239251
}
@@ -243,6 +255,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
243255
*
244256
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
245257
*/
258+
@Deprecated
246259
public static Map.Entry<String, Map<String, ?>> unlockCommand() {
247260
return new AbstractMap.SimpleEntry<>(UNLOCK, ImmutableMap.of());
248261
}
@@ -256,6 +269,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
256269
* @param value a new value
257270
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
258271
*/
272+
@Deprecated
259273
public static Map.Entry<String, Map<String, ?>> replaceElementValueCommand(
260274
RemoteWebElement remoteWebElement, String value) {
261275
String[] parameters = new String[] {"id", "value"};
@@ -275,6 +289,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
275289
*
276290
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
277291
*/
292+
@Deprecated
278293
public static Map.Entry<String, Map<String, ?>> sendSMSCommand(
279294
String phoneNumber, String message) {
280295
ImmutableMap<String, ?> parameters = ImmutableMap
@@ -294,6 +309,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
294309
*
295310
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
296311
*/
312+
@Deprecated
297313
public static Map.Entry<String, Map<String, ?>> gsmCallCommand(
298314
String phoneNumber, GsmCallActions gsmCallActions) {
299315
String[] parameters = new String[] {"phoneNumber", "action"};
@@ -309,6 +325,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
309325
*
310326
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
311327
*/
328+
@Deprecated
312329
public static Map.Entry<String, Map<String, ?>> gsmSignalStrengthCommand(
313330
GsmSignalStrength gsmSignalStrength) {
314331
return new AbstractMap.SimpleEntry<>(GSM_SIGNAL,
@@ -327,6 +344,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
327344
*
328345
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
329346
*/
347+
@Deprecated
330348
public static Map.Entry<String, Map<String, ?>> gsmVoiceCommand(
331349
GsmVoiceState gsmVoiceState) {
332350
return new AbstractMap.SimpleEntry<>(GSM_VOICE,
@@ -341,6 +359,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
341359
*
342360
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
343361
*/
362+
@Deprecated
344363
public static Map.Entry<String, Map<String, ?>> networkSpeedCommand(
345364
NetworkSpeed networkSpeed) {
346365
return new AbstractMap.SimpleEntry<>(NETWORK_SPEED,
@@ -355,6 +374,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
355374
*
356375
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
357376
*/
377+
@Deprecated
358378
public static Map.Entry<String, Map<String, ?>> powerCapacityCommand(
359379
int percent) {
360380
return new AbstractMap.SimpleEntry<>(POWER_CAPACITY,
@@ -369,6 +389,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
369389
*
370390
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
371391
*/
392+
@Deprecated
372393
public static Map.Entry<String, Map<String, ?>> powerACCommand(
373394
PowerACState powerACState) {
374395
return new AbstractMap.SimpleEntry<>(POWER_AC_STATE,
@@ -380,6 +401,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
380401
*
381402
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
382403
*/
404+
@Deprecated
383405
public static Map.Entry<String, Map<String, ?>> toggleWifiCommand() {
384406
return new AbstractMap.SimpleEntry<>(TOGGLE_WIFI, ImmutableMap.of());
385407
}
@@ -389,6 +411,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
389411
*
390412
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
391413
*/
414+
@Deprecated
392415
public static Map.Entry<String, Map<String, ?>> toggleAirplaneCommand() {
393416
return new AbstractMap.SimpleEntry<>(TOGGLE_AIRPLANE_MODE, ImmutableMap.of());
394417
}
@@ -398,6 +421,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
398421
*
399422
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
400423
*/
424+
@Deprecated
401425
public static Map.Entry<String, Map<String, ?>> toggleDataCommand() {
402426
return new AbstractMap.SimpleEntry<>(TOGGLE_DATA, ImmutableMap.of());
403427
}

src/main/java/io/appium/java_client/android/AuthenticatesByFinger.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.appium.java_client.android;
22

3+
import com.google.common.collect.ImmutableMap;
34
import io.appium.java_client.CommandExecutionHelper;
45
import io.appium.java_client.ExecutesMethod;
6+
import org.openqa.selenium.UnsupportedCommandException;
57

68
import static io.appium.java_client.android.AndroidMobileCommandHelper.fingerPrintCommand;
79

@@ -13,6 +15,13 @@ public interface AuthenticatesByFinger extends ExecutesMethod {
1315
* @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)
1416
*/
1517
default void fingerPrint(int fingerPrintId) {
16-
CommandExecutionHelper.execute(this, fingerPrintCommand(fingerPrintId));
18+
try {
19+
CommandExecutionHelper.executeScript(this, "mobile: fingerprint", ImmutableMap.of(
20+
"fingerprintId", fingerPrintId
21+
));
22+
} catch (UnsupportedCommandException e) {
23+
// TODO: Remove the fallback
24+
CommandExecutionHelper.execute(this, fingerPrintCommand(fingerPrintId));
25+
}
1726
}
1827
}
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
package io.appium.java_client.android;
22

33
import com.google.common.collect.ImmutableMap;
4+
import io.appium.java_client.CommandExecutionHelper;
45
import io.appium.java_client.ExecutesMethod;
56
import io.appium.java_client.MobileCommand;
7+
import org.openqa.selenium.UnsupportedCommandException;
68
import org.openqa.selenium.remote.RemoteWebElement;
79

810
public interface CanReplaceElementValue extends ExecutesMethod {
911
/**
10-
* Replaces element value with the given one.
12+
* Sends a text to the given element by replacing its previous content.
1113
*
1214
* @param element The destination element.
13-
* @param value The value to set.
15+
* @param value The text to enter. It could also contain Unicode characters.
16+
* If the text ends with `\\n` (the backslash must be escaped, so the
17+
* char is NOT translated into `0x0A`) then the Enter key press is going to
18+
* be emulated after it is entered (the `\\n` substring itself will be cut
19+
* off from the typed text).
1420
*/
1521
default void replaceElementValue(RemoteWebElement element, String value) {
16-
this.execute(MobileCommand.REPLACE_VALUE, ImmutableMap.of(
17-
"id", element.getId(),
18-
"value", value
19-
));
22+
try {
23+
CommandExecutionHelper.executeScript(this, "mobile: replaceValue", ImmutableMap.of(
24+
"elementId", element.getId(),
25+
"text", value
26+
));
27+
} catch (UnsupportedCommandException e) {
28+
// TODO: Remove the fallback
29+
this.execute(MobileCommand.REPLACE_VALUE, ImmutableMap.of(
30+
"id", element.getId(),
31+
"value", value
32+
));
33+
}
2034
}
2135
}

src/main/java/io/appium/java_client/android/HasAndroidDeviceDetails.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.appium.java_client.CommandExecutionHelper;
44
import io.appium.java_client.ExecutesMethod;
5+
import org.openqa.selenium.UnsupportedCommandException;
56

67
import java.util.Map;
78

@@ -16,7 +17,12 @@ public interface HasAndroidDeviceDetails extends ExecutesMethod {
1617
@return The density value in dpi
1718
*/
1819
default Long getDisplayDensity() {
19-
return CommandExecutionHelper.execute(this, getDisplayDensityCommand());
20+
try {
21+
return CommandExecutionHelper.executeScript(this, "mobile: getDisplayDensity");
22+
} catch (UnsupportedCommandException e) {
23+
// TODO: Remove the fallback
24+
return CommandExecutionHelper.execute(this, getDisplayDensityCommand());
25+
}
2026
}
2127

2228
/**
@@ -25,7 +31,12 @@ default Long getDisplayDensity() {
2531
@return The map where keys are bar types and values are mappings of bar properties.
2632
*/
2733
default Map<String, Map<String, Object>> getSystemBars() {
28-
return CommandExecutionHelper.execute(this, getSystemBarsCommand());
34+
try {
35+
return CommandExecutionHelper.executeScript(this, "mobile: getSystemBars");
36+
} catch (UnsupportedCommandException e) {
37+
// TODO: Remove the fallback
38+
return CommandExecutionHelper.execute(this, getSystemBarsCommand());
39+
}
2940
}
3041

3142
}

src/main/java/io/appium/java_client/android/StartsActivity.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616

1717
package io.appium.java_client.android;
1818

19+
import com.google.common.collect.ImmutableMap;
1920
import io.appium.java_client.CommandExecutionHelper;
2021
import io.appium.java_client.ExecutesMethod;
22+
import org.openqa.selenium.UnsupportedCommandException;
2123

22-
import static io.appium.java_client.android.AndroidMobileCommandHelper.currentActivityCommand;
23-
import static io.appium.java_client.android.AndroidMobileCommandHelper.currentPackageCommand;
24+
import javax.annotation.Nullable;
25+
26+
import java.util.AbstractMap;
27+
28+
import static io.appium.java_client.MobileCommand.CURRENT_ACTIVITY;
29+
import static io.appium.java_client.MobileCommand.GET_CURRENT_PACKAGE;
2430
import static io.appium.java_client.android.AndroidMobileCommandHelper.startActivityCommand;
2531

2632
public interface StartsActivity extends ExecutesMethod {
@@ -57,16 +63,32 @@ default void startActivity(Activity activity) {
5763
*
5864
* @return a current activity being run on the mobile device.
5965
*/
66+
@Nullable
6067
default String currentActivity() {
61-
return CommandExecutionHelper.execute(this, currentActivityCommand());
68+
try {
69+
return CommandExecutionHelper.executeScript(this, "mobile: getCurrentActivity");
70+
} catch (UnsupportedCommandException e) {
71+
// TODO: Remove the fallback
72+
return CommandExecutionHelper.execute(
73+
this, new AbstractMap.SimpleEntry<>(CURRENT_ACTIVITY, ImmutableMap.of())
74+
);
75+
}
6276
}
6377

6478
/**
6579
* Get the current package being run on the mobile device.
6680
*
6781
* @return a current package being run on the mobile device.
6882
*/
83+
@Nullable
6984
default String getCurrentPackage() {
70-
return CommandExecutionHelper.execute(this, currentPackageCommand());
85+
try {
86+
return CommandExecutionHelper.executeScript(this, "mobile: getCurrentPackage");
87+
} catch (UnsupportedCommandException e) {
88+
// TODO: Remove the fallback
89+
return CommandExecutionHelper.execute(
90+
this, new AbstractMap.SimpleEntry<>(GET_CURRENT_PACKAGE, ImmutableMap.of())
91+
);
92+
}
7193
}
7294
}

0 commit comments

Comments
 (0)