Skip to content

Commit d96ca60

Browse files
New version: v1.0.0.
1 parent ae5cea4 commit d96ca60

File tree

9 files changed

+371
-155
lines changed

9 files changed

+371
-155
lines changed

pom.xml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.mobilebox.appium.repl</groupId>
55
<artifactId>appium-repl</artifactId>
6-
<version>0.2.0-BETA1</version>
6+
<version>1.0.0</version>
77
<name>Appium REPL</name>
88
<description>Simple Java REPL (Read-eval-print Loop) for controlling mobile apps through Appium</description>
99

@@ -26,13 +26,13 @@
2626
<dependency>
2727
<groupId>io.appium</groupId>
2828
<artifactId>java-client</artifactId>
29-
<version>5.0.0-BETA2</version>
29+
<version>6.0.0-BETA2</version>
3030
</dependency>
3131

3232
<dependency>
3333
<groupId>com.javarepl</groupId>
3434
<artifactId>javarepl</artifactId>
35-
<version>1.0.1</version>
35+
<version>428</version>
3636
</dependency>
3737

3838
<dependency>
@@ -44,7 +44,7 @@
4444
<dependency>
4545
<groupId>com.github.vidstige</groupId>
4646
<artifactId>jadb</artifactId>
47-
<version>94ebf38</version>
47+
<version>v1.0.1</version>
4848
</dependency>
4949

5050
<dependency>
@@ -64,7 +64,7 @@
6464
<dependency>
6565
<groupId>org.aeonbits.owner</groupId>
6666
<artifactId>owner</artifactId>
67-
<version>1.0.8</version>
67+
<version>1.0.9</version>
6868
</dependency>
6969

7070
<dependency>
@@ -79,6 +79,19 @@
7979
<version>1.0-SNAPSHOT</version>
8080
</dependency>
8181

82+
<dependency>
83+
<groupId>org.zeroturnaround</groupId>
84+
<artifactId>zt-zip</artifactId>
85+
<version>1.9</version>
86+
<type>jar</type>
87+
</dependency>
88+
89+
<dependency>
90+
<groupId>com.cedarsoftware</groupId>
91+
<artifactId>json-io</artifactId>
92+
<version>4.5.0</version>
93+
</dependency>
94+
8295
</dependencies>
8396

8497
<build>

src/main/java/com/mobilebox/repl/Appium.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static com.mobilebox.repl.commands.CommandsDoc.printCommands;
44
import static com.mobilebox.repl.commands.CommandsDoc.SEPARATOR;
55
import static com.mobilebox.repl.misc.Utils.console;
6+
import static java.lang.System.getProperty;
7+
68
import javarepl.Main;
79

810
import com.mobilebox.repl.commands.AndroidCommands;
@@ -52,23 +54,33 @@ public static void exit() {
5254
}
5355

5456
@CommandRef(desc = "Prints all commands available for Android and iOS.")
55-
public static void commands_appium() {
57+
public static void appium() {
5658
printCommands(AppiumCommands.class);
5759
}
5860

5961
@CommandRef(desc = "Prints all commands available especifc for Android.")
60-
public static void commands_android() {
62+
public static void android() {
6163
printCommands(AndroidCommands.class);
6264
}
6365

6466
@CommandRef(desc = "Prints all commands available for iOS.")
65-
public static void commands_ios() {
67+
public static void ios() {
6668
printCommands(IOSCommands.class);
6769
}
6870

6971
@CommandRef(desc = "Prints all commands available for Android Device.")
70-
public static void commands_android_device() {
72+
public static void android_device() {
7173
printCommands(AndroidDeviceCommands.class);
7274
}
7375

76+
@CommandRef(desc = "Prints the user home directory.")
77+
public static void user_home() {
78+
System.out.println(getProperty("user.home"));
79+
}
80+
81+
@CommandRef(desc = "Prints my name")
82+
public static void my_name() {
83+
System.out.println("My Name \n it's a joke, a bad one...");
84+
}
85+
7486
}
Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
package com.mobilebox.repl.app;
22

33
import static com.google.common.base.Strings.nullToEmpty;
4+
import static java.util.UUID.randomUUID;
5+
import static org.zeroturnaround.zip.ZipUtil.containsEntry;
6+
import static org.zeroturnaround.zip.ZipUtil.unpackEntry;
47

8+
import java.awt.image.BufferedImage;
9+
import java.io.ByteArrayOutputStream;
510
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.IOException;
13+
import java.nio.charset.StandardCharsets;
14+
import java.util.ArrayList;
15+
import java.util.Base64;
16+
import java.util.List;
617
import java.util.Locale;
18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
720

8-
import com.mobilebox.repl.exceptions.CommandsException;
21+
import javax.imageio.ImageIO;
22+
23+
import com.jcabi.xml.XML;
24+
import com.jcabi.xml.XMLDocument;
925

1026
import net.dongliu.apk.parser.ApkParser;
1127
import net.dongliu.apk.parser.bean.ApkMeta;
@@ -23,7 +39,7 @@ public class APKInspector {
2339
* @return An {@link ApkData} instance.
2440
* @throws CommandsException
2541
*/
26-
public ApkData inspect(final String appPath) throws CommandsException {
42+
public ApkData inspect(final String appPath) {
2743
File apkFile = new File(appPath);
2844
ApkData app = new ApkData();
2945

@@ -32,22 +48,92 @@ public ApkData inspect(final String appPath) throws CommandsException {
3248
ApkParser parser = new ApkParser(appPath);
3349
parser.setPreferredLocale(Locale.getDefault());
3450
ApkMeta data = parser.getApkMeta();
35-
51+
3652
app.setPackageName(nullToEmpty(data.getPackageName()))
37-
.setLabel(nullToEmpty(data.getLabel()))
38-
.setVersionName(nullToEmpty(data.getVersionName()))
39-
.setMaxSdkVersion(nullToEmpty(data.getMaxSdkVersion()))
40-
.setMinSdkVersion(nullToEmpty(data.getMinSdkVersion()))
41-
.setTargetSdkVersion(nullToEmpty(data.getTargetSdkVersion()))
42-
.setUsesFeature(data.getUsesFeatures())
43-
.setUserPermissions(data.getUsesPermissions());
53+
.setLabel(nullToEmpty(data.getLabel()))
54+
.setVersionName(nullToEmpty(data.getVersionName()))
55+
.setMaxSdkVersion(nullToEmpty(data.getMaxSdkVersion()))
56+
.setMinSdkVersion(nullToEmpty(data.getMinSdkVersion()))
57+
.setTargetSdkVersion(nullToEmpty(data.getTargetSdkVersion()))
58+
.setUsesFeature(data.getUsesFeatures()).setUserPermissions(data.getUsesPermissions())
59+
.setActivities(getActivities(parser.getManifestXml()))
60+
.setIcon(icon2Base64(appPath, data.getIcon()));
4461

4562
} catch (Exception e) {
46-
throw new CommandsException("Error: " + e.getMessage());
63+
throw new RuntimeException("Error: " + e.getMessage());
4764
}
4865
}
4966

5067
return app;
5168
}
5269

70+
/**
71+
* Return information about activities in the package in activities.
72+
*
73+
* @param manifest The app manifest.
74+
* @return List of activities
75+
*/
76+
private List<String> getActivities(final String manifest) {
77+
List<String> activities = new ArrayList<String>();
78+
Pattern p = Pattern.compile("android:name=\"(.*?)\"");
79+
80+
XML xml = new XMLDocument(manifest);
81+
XML je = new XMLDocument(xml.toString());
82+
List<XML> name = je.nodes("//activity");
83+
84+
for (XML node : name) {
85+
Matcher m = p.matcher(node.toString());
86+
if (m.find()) {
87+
activities.add(m.group(1));
88+
}
89+
}
90+
return activities;
91+
}
92+
93+
/**
94+
* Retrieve the icon associated with an application. If it has not defined an icon, the default
95+
* app icon is returned.
96+
*
97+
* @param appPath The aplication under test path.
98+
* @iconPath The path to the icon on the APK file.
99+
* @return The image of the icon, or the default application icon if it could not be found in
100+
* base64.
101+
* @throws MoneyException
102+
*/
103+
public String icon2Base64(final String appPath, final String iconPath) {
104+
String icon =
105+
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEwAACxMBAJqcGAAABRZJREFUeJztm89vVFUUxz/3vJm22FakZVoTRFaGUliI8QcsXICu5EcTE2ICf4BGggYTaBDoy0AFiyILjZq6xehKEH+wsZi40BohkShWlmpIoENLkZZOmTfnuGjHFKaFDn1vXgnzWU0mZ84595tz37333DdQoUKFChXuX9xUXzb62RZn8rbD1oLURxPaXsvsq3o/Gt8zp0iARj/bIuZ6QeZHG1rzZtJ2eX/ym2jj3J4iARZ23DjucG1mdtyTxMuX0tIfZsBUR84Kn1V1WDyezaSrfw0zRinIrV+Mlz1EMfjJGHwgInWY+7rhTV0UVZw7USRAYc5HOXiAyy6xHfQkuEVeIv9Vyre6KONNxxQClIm0C3Q0+ZLCb8BK9MZnbDKv3GnEJwAw8I5cU5dYr6oXcbK+sSU4Uu4cYhUA4Era/Y3nNig6Ko5tqY4b28oZP3YBAAbSVac9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HUVuPKW/FPrEk8a9gZwRtHRMP3PholcfjGz1wdc4unBA3JhSsNUR86ivpEthajzue/3ARUB4k4gbooEKLy20uxrU/nTuZnCS1SqOhxVjCIBxMb7Z3kLuuMUoWmXNlsimHEP8G4p/svMnuxSwfUiMjd2c6pDiq2aWRusdIoqYKCz5rxiqwz7Ms63uFR12MyOz7wHWKFChQql8x+anVpT1E4upAAAAABJRU5ErkJggg==";
106+
File apkFile = new File(appPath);
107+
if (apkFile.exists()) {
108+
if (containsEntry(apkFile, iconPath)) {
109+
String tempIcon = randomUUID().toString() + ".png";
110+
File icono = new File(tempIcon);
111+
unpackEntry(apkFile, iconPath, icono);
112+
icon = imageToBase64(icono.getAbsolutePath());
113+
icono.delete();
114+
}
115+
}
116+
return icon;
117+
}
118+
119+
/**
120+
* Image to Base64 converter
121+
*
122+
* @param imagePath - The image path.
123+
* @return The image as base64 data.
124+
* @throws MonkeyException
125+
*/
126+
private String imageToBase64(final String imagePath) {
127+
final ByteArrayOutputStream os = new ByteArrayOutputStream();
128+
try {
129+
FileInputStream inputStream = new FileInputStream(imagePath);
130+
BufferedImage inputImage = ImageIO.read(inputStream);
131+
ImageIO.write(inputImage, "png", Base64.getEncoder().wrap(os));
132+
return os.toString(StandardCharsets.ISO_8859_1.name());
133+
} catch (final IOException e) {
134+
throw new RuntimeException(
135+
"We have an error with pseudo image to 64 route converter: " + e.getMessage());
136+
}
137+
}
138+
53139
}

src/main/java/com/mobilebox/repl/app/ApkData.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,25 @@
1111
public class ApkData {
1212

1313
private String packageName;
14-
1514
private String label;
16-
1715
private String icon;
18-
1916
private String versionName;
20-
2117
private String minSdkVersion;
22-
2318
private String targetSdkVersion;
24-
2519
private String maxSdkVersion;
26-
2720
private List<UseFeature> usesFeature;
28-
2921
private List<String> userPermissions;
30-
3122
private List<String> activities;
3223

33-
3424
public String getPackageName() {
3525
return packageName;
3626
}
3727

38-
3928
public ApkData setPackageName(String packageName) {
4029
this.packageName = packageName;
4130
return this;
4231
}
4332

44-
4533
public String getLabel() {
4634
return label;
4735
}
@@ -55,18 +43,15 @@ public String getIcon() {
5543
return icon;
5644
}
5745

58-
5946
public ApkData setIcon(String icon) {
6047
this.icon = icon;
6148
return this;
6249
}
6350

64-
6551
public String getVersionName() {
6652
return versionName;
6753
}
6854

69-
7055
public ApkData setVersionName(String versionName) {
7156
this.versionName = versionName;
7257
return this;
@@ -76,35 +61,29 @@ public String getMinSdkVersion() {
7661
return minSdkVersion;
7762
}
7863

79-
8064
public ApkData setMinSdkVersion(String minSdkVersion) {
8165
this.minSdkVersion = minSdkVersion;
8266
return this;
8367
}
8468

85-
8669
public String getTargetSdkVersion() {
8770
return targetSdkVersion;
8871
}
8972

90-
9173
public ApkData setTargetSdkVersion(String targetSdkVersion) {
9274
this.targetSdkVersion = targetSdkVersion;
9375
return this;
9476
}
9577

96-
9778
public String getMaxSdkVersion() {
9879
return maxSdkVersion;
9980
}
10081

101-
10282
public ApkData setMaxSdkVersion(String maxSdkVersion) {
10383
this.maxSdkVersion = maxSdkVersion;
10484
return this;
10585
}
10686

107-
10887
public String toJson() {
10988
return new Gson().toJson(this);
11089
}
@@ -113,7 +92,6 @@ public List<UseFeature> getUsesFeature() {
11392
return usesFeature;
11493
}
11594

116-
11795
public ApkData setUsesFeature(List<UseFeature> usesFeature) {
11896
this.usesFeature = usesFeature;
11997
return this;
@@ -146,6 +124,7 @@ public void toConsole() {
146124
console("---> Max SDK version: " + maxSdkVersion);
147125
console("---> Uses Feature: " + usesFeature);
148126
console("---> Uses Permissions: " + userPermissions);
127+
console("---> Activities: " + activities);
149128
}
150129

151130
}

0 commit comments

Comments
 (0)