Skip to content

Commit e842b26

Browse files
committed
优化代码
1 parent 868d84a commit e842b26

File tree

6 files changed

+88
-106
lines changed

6 files changed

+88
-106
lines changed

apkbuilder/src/androidTest/java/com/stardust/autojs/apkbuilder/ExampleInstrumentedTest.java

-26
This file was deleted.

apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkPackager.java apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkUnpackUtil.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,36 @@
1414
import java.util.zip.ZipInputStream;
1515

1616
/**
17+
* 负责将内置apk解压到指定目录
18+
*
1719
* Created by Stardust on 2017/10/23.
1820
*/
21+
public class ApkUnpackUtil {
1922

20-
public class ApkPackager {
23+
private final InputStream mApkInputStream;
24+
private final String mWorkspacePath;
2125

22-
private InputStream mApkInputStream;
23-
private String mWorkspacePath;
24-
25-
public ApkPackager(InputStream apkInputStream, String workspacePath) {
26+
public ApkUnpackUtil(InputStream apkInputStream, String workspacePath) {
2627
mApkInputStream = apkInputStream;
2728
mWorkspacePath = workspacePath;
2829
}
2930

30-
public ApkPackager(String apkPath, String workspacePath) throws FileNotFoundException {
31-
mApkInputStream = new FileInputStream(apkPath);
32-
mWorkspacePath = workspacePath;
33-
}
34-
3531
public void unzip() throws IOException {
3632
try (ZipInputStream zis = new ZipInputStream(mApkInputStream)) {
3733
for (ZipEntry e = zis.getNextEntry(); e != null; e = zis.getNextEntry()) {
3834
String name = e.getName();
3935
if (!e.isDirectory() && !TextUtils.isEmpty(name)) {
4036
File file = new File(mWorkspacePath, name);
4137
System.out.println(file);
42-
file.getParentFile().mkdirs();
43-
FileOutputStream fos = new FileOutputStream(file);
44-
StreamUtils.write(zis, fos);
45-
fos.close();
38+
if (file.getParentFile() != null && file.getParentFile().mkdirs()) {
39+
try (FileOutputStream fos = new FileOutputStream(file)) {
40+
StreamUtils.write(zis, fos);
41+
}
42+
}
4643
} else {
4744
System.out.println("file or empty:" + name);
4845
}
4946
}
5047
}
5148
}
52-
53-
public void cleanWorkspace() {
54-
55-
}
56-
5749
}

apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ManifestEditor.java

+1-48
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.stardust.autojs.apkbuilder;
22

3-
import android.util.Log;
4-
53
import org.apache.commons.io.IOUtils;
64

75
import java.io.IOException;
@@ -11,7 +9,6 @@
119
import pxb.android.StringItem;
1210
import pxb.android.axml.AxmlReader;
1311
import pxb.android.axml.AxmlWriter;
14-
import pxb.android.axml.NodeVisitor;
1512

1613
/**
1714
* Created by Stardust on 2017/10/23.
@@ -55,7 +52,7 @@ public ManifestEditor setPackageName(String packageName) {
5552

5653
public ManifestEditor commit() throws IOException {
5754
try {
58-
AxmlWriter writer = new MutableAxmlWriter();
55+
AxmlWriter writer = new MutableAxmlWriter(this);
5956
AxmlReader reader = new AxmlReader(IOUtils.readFully(mManifestInputStream, mManifestInputStream.available()));
6057
reader.accept(writer);
6158
mManifestData = writer.toByteArray();
@@ -101,48 +98,4 @@ public boolean filterPermission(String permissionName) {
10198
}
10299

103100

104-
public class MutableAxmlWriter extends AxmlWriter {
105-
public class MutableNodeImpl extends AxmlWriter.NodeImpl {
106-
private boolean ignore;
107-
private final String name;
108-
109-
MutableNodeImpl(String ns, String name) {
110-
super(ns, name);
111-
this.name = name;
112-
}
113-
114-
@Override
115-
protected void onAttr(AxmlWriter.Attr a) {
116-
if ("uses-permission".equals(this.name) && "name".equals(a.name.data) && a.value instanceof StringItem) {
117-
if (ManifestEditor.this.filterPermission(((StringItem) a.value).data)) {
118-
ignore = true;
119-
}
120-
}
121-
ManifestEditor.this.onAttr(a);
122-
super.onAttr(a);
123-
}
124-
125-
126-
@Override
127-
public NodeVisitor child(String ns, String name) {
128-
Log.d("MutableAxmlWriter", "child: " + ns + " name: " + name);
129-
NodeImpl child = new MutableNodeImpl(ns, name);
130-
this.children.add(child);
131-
return child;
132-
}
133-
134-
public boolean isIgnore() {
135-
return ignore;
136-
}
137-
}
138-
139-
@Override
140-
public NodeVisitor child(String ns, String name) {
141-
NodeImpl first = new MutableNodeImpl(ns, name);
142-
this.firsts.add(first);
143-
return first;
144-
}
145-
}
146-
147-
148101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.stardust.autojs.apkbuilder;
2+
3+
import android.util.Log;
4+
5+
import pxb.android.StringItem;
6+
import pxb.android.axml.AxmlWriter;
7+
import pxb.android.axml.NodeVisitor;
8+
9+
/**
10+
* AndroidManifest文件修改,用于修改xml元素
11+
*
12+
* @author TonyJiangWJ
13+
* @since 2023/8/16
14+
*/
15+
public class MutableAxmlWriter extends AxmlWriter {
16+
private final ManifestEditor manifestEditor;
17+
18+
public MutableAxmlWriter(ManifestEditor manifestEditor) {
19+
this.manifestEditor = manifestEditor;
20+
}
21+
22+
@Override
23+
public NodeVisitor child(String ns, String name) {
24+
AxmlWriter.NodeImpl first = new MutableAxmlWriter.MutableNodeImpl(ns, name);
25+
this.firsts.add(first);
26+
return first;
27+
}
28+
29+
public class MutableNodeImpl extends AxmlWriter.NodeImpl {
30+
private boolean ignore;
31+
private final String name;
32+
33+
MutableNodeImpl(String ns, String name) {
34+
super(ns, name);
35+
this.name = name;
36+
}
37+
38+
@Override
39+
protected void onAttr(AxmlWriter.Attr a) {
40+
if ("uses-permission".equals(this.name) && "name".equals(a.name.data) && a.value instanceof StringItem) {
41+
if (manifestEditor.filterPermission(((StringItem) a.value).data)) {
42+
ignore = true;
43+
}
44+
}
45+
manifestEditor.onAttr(a);
46+
super.onAttr(a);
47+
}
48+
49+
50+
@Override
51+
public NodeVisitor child(String ns, String name) {
52+
Log.d("MutableAxmlWriter", "child: " + ns + " name: " + name);
53+
AxmlWriter.NodeImpl child = new MutableAxmlWriter.MutableNodeImpl(ns, name);
54+
this.children.add(child);
55+
return child;
56+
}
57+
58+
public boolean isIgnore() {
59+
return ignore;
60+
}
61+
}
62+
}
63+

apkbuilder/src/main/java/pxb/android/axml/AxmlWriter.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import android.util.Log;
1919

20-
import com.stardust.autojs.apkbuilder.ManifestEditor;
20+
import com.stardust.autojs.apkbuilder.MutableAxmlWriter;
2121

2222
import pxb.android.StringItem;
2323
import pxb.android.StringItems;
@@ -96,8 +96,8 @@ private int prepare() throws IOException {
9696
int size = 0;
9797

9898
for (NodeImpl first : firsts) {
99-
if (first instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
100-
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
99+
if (first instanceof MutableAxmlWriter.MutableNodeImpl) {
100+
if (((MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
101101
Log.d(TAG, "prepare: first is ignore: " + first.name);
102102
continue;
103103
}
@@ -173,8 +173,8 @@ public byte[] toByteArray() throws IOException {
173173
}
174174

175175
for (NodeImpl first : firsts) {
176-
if (first instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
177-
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
176+
if (first instanceof MutableAxmlWriter.MutableNodeImpl) {
177+
if (((MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
178178
Log.d(TAG, "toByteArray: first is ignore: " + first.name);
179179
continue;
180180
}
@@ -359,8 +359,8 @@ public int prepare(AxmlWriter axmlWriter) {
359359
int size = 24 + 36 + attrs.size() * 20;// 24 for end tag,36+x*20 for
360360
// start tag
361361
for (NodeImpl child : children) {
362-
if (child instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
363-
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
362+
if (child instanceof MutableAxmlWriter.MutableNodeImpl) {
363+
if (((MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
364364
Log.d(TAG, "prepare: child is ignore: " + child.name);
365365
continue;
366366
}
@@ -421,8 +421,8 @@ void write(ByteBuffer out) throws IOException {
421421

422422
// children
423423
for (NodeImpl child : children) {
424-
if (child instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
425-
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
424+
if (child instanceof MutableAxmlWriter.MutableNodeImpl) {
425+
if (((MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
426426
Log.d(TAG, "white: child is ignore: " + child.name);
427427
continue;
428428
}

app/src/main/java/org/autojs/autojs/build/ApkBuilder.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.android.apksigner.PasswordRetriever;
99
import com.android.apksigner.SignerParams;
1010
import com.stardust.app.GlobalAppContext;
11-
import com.stardust.autojs.apkbuilder.ApkPackager;
11+
import com.stardust.autojs.apkbuilder.ApkUnpackUtil;
1212
import com.stardust.autojs.apkbuilder.ManifestEditor;
1313
import com.stardust.autojs.apkbuilder.util.StreamUtils;
1414
import com.stardust.autojs.project.BuildInfo;
@@ -197,7 +197,7 @@ public void setEnabledPermission(Set<String> enabledPermission) {
197197
}
198198

199199
private ProgressCallback mProgressCallback;
200-
private ApkPackager mApkPackager;
200+
private ApkUnpackUtil mApkUnpackUtil;
201201
private String mArscPackageName;
202202
private ManifestEditor mManifestEditor;
203203
private String mWorkspacePath;
@@ -210,7 +210,7 @@ public void setEnabledPermission(Set<String> enabledPermission) {
210210
public ApkBuilder(InputStream apkInputStream, File outApkFile, String workspacePath) {
211211
mWorkspacePath = workspacePath;
212212
mOutApkFile = outApkFile;
213-
mApkPackager = new ApkPackager(apkInputStream, mWorkspacePath);
213+
mApkUnpackUtil = new ApkUnpackUtil(apkInputStream, mWorkspacePath);
214214
PFiles.ensureDir(outApkFile.getPath());
215215
}
216216

@@ -224,7 +224,7 @@ public ApkBuilder prepare() throws IOException {
224224
GlobalAppContext.post(() -> mProgressCallback.onPrepare(ApkBuilder.this));
225225
}
226226
(new File(mWorkspacePath)).mkdirs();
227-
mApkPackager.unzip();
227+
mApkUnpackUtil.unzip();
228228
return this;
229229
}
230230

0 commit comments

Comments
 (0)