mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-12 21:01:32 +08:00
优化代码
This commit is contained in:
parent
043dacaeb7
commit
8b9b54f390
@ -1,26 +0,0 @@
|
||||
package com.stardust.autojs.apkbuilder;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumentation test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() throws Exception {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
||||
|
||||
assertEquals("com.stardust.autojs.apkbuilder.test", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
@ -14,24 +14,20 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
/**
|
||||
* 负责将内置apk解压到指定目录
|
||||
*
|
||||
* Created by Stardust on 2017/10/23.
|
||||
*/
|
||||
public class ApkUnpackUtil {
|
||||
|
||||
public class ApkPackager {
|
||||
private final InputStream mApkInputStream;
|
||||
private final String mWorkspacePath;
|
||||
|
||||
private InputStream mApkInputStream;
|
||||
private String mWorkspacePath;
|
||||
|
||||
public ApkPackager(InputStream apkInputStream, String workspacePath) {
|
||||
public ApkUnpackUtil(InputStream apkInputStream, String workspacePath) {
|
||||
mApkInputStream = apkInputStream;
|
||||
mWorkspacePath = workspacePath;
|
||||
}
|
||||
|
||||
public ApkPackager(String apkPath, String workspacePath) throws FileNotFoundException {
|
||||
mApkInputStream = new FileInputStream(apkPath);
|
||||
mWorkspacePath = workspacePath;
|
||||
}
|
||||
|
||||
public void unzip() throws IOException {
|
||||
try (ZipInputStream zis = new ZipInputStream(mApkInputStream)) {
|
||||
for (ZipEntry e = zis.getNextEntry(); e != null; e = zis.getNextEntry()) {
|
||||
@ -39,19 +35,15 @@ public class ApkPackager {
|
||||
if (!e.isDirectory() && !TextUtils.isEmpty(name)) {
|
||||
File file = new File(mWorkspacePath, name);
|
||||
System.out.println(file);
|
||||
file.getParentFile().mkdirs();
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
StreamUtils.write(zis, fos);
|
||||
fos.close();
|
||||
if (file.getParentFile() != null && file.getParentFile().mkdirs()) {
|
||||
try (FileOutputStream fos = new FileOutputStream(file)) {
|
||||
StreamUtils.write(zis, fos);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println("file or empty:" + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanWorkspace() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +1,5 @@
|
||||
package com.stardust.autojs.apkbuilder;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -11,7 +9,6 @@ import java.io.OutputStream;
|
||||
import pxb.android.StringItem;
|
||||
import pxb.android.axml.AxmlReader;
|
||||
import pxb.android.axml.AxmlWriter;
|
||||
import pxb.android.axml.NodeVisitor;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/10/23.
|
||||
@ -55,7 +52,7 @@ public class ManifestEditor {
|
||||
|
||||
public ManifestEditor commit() throws IOException {
|
||||
try {
|
||||
AxmlWriter writer = new MutableAxmlWriter();
|
||||
AxmlWriter writer = new MutableAxmlWriter(this);
|
||||
AxmlReader reader = new AxmlReader(IOUtils.readFully(mManifestInputStream, mManifestInputStream.available()));
|
||||
reader.accept(writer);
|
||||
mManifestData = writer.toByteArray();
|
||||
@ -101,48 +98,4 @@ public class ManifestEditor {
|
||||
}
|
||||
|
||||
|
||||
public class MutableAxmlWriter extends AxmlWriter {
|
||||
public class MutableNodeImpl extends AxmlWriter.NodeImpl {
|
||||
private boolean ignore;
|
||||
private final String name;
|
||||
|
||||
MutableNodeImpl(String ns, String name) {
|
||||
super(ns, name);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttr(AxmlWriter.Attr a) {
|
||||
if ("uses-permission".equals(this.name) && "name".equals(a.name.data) && a.value instanceof StringItem) {
|
||||
if (ManifestEditor.this.filterPermission(((StringItem) a.value).data)) {
|
||||
ignore = true;
|
||||
}
|
||||
}
|
||||
ManifestEditor.this.onAttr(a);
|
||||
super.onAttr(a);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NodeVisitor child(String ns, String name) {
|
||||
Log.d("MutableAxmlWriter", "child: " + ns + " name: " + name);
|
||||
NodeImpl child = new MutableNodeImpl(ns, name);
|
||||
this.children.add(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
public boolean isIgnore() {
|
||||
return ignore;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeVisitor child(String ns, String name) {
|
||||
NodeImpl first = new MutableNodeImpl(ns, name);
|
||||
this.firsts.add(first);
|
||||
return first;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,63 @@
|
||||
package com.stardust.autojs.apkbuilder;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import pxb.android.StringItem;
|
||||
import pxb.android.axml.AxmlWriter;
|
||||
import pxb.android.axml.NodeVisitor;
|
||||
|
||||
/**
|
||||
* AndroidManifest文件修改,用于修改xml元素
|
||||
*
|
||||
* @author TonyJiangWJ
|
||||
* @since 2023/8/16
|
||||
*/
|
||||
public class MutableAxmlWriter extends AxmlWriter {
|
||||
private final ManifestEditor manifestEditor;
|
||||
|
||||
public MutableAxmlWriter(ManifestEditor manifestEditor) {
|
||||
this.manifestEditor = manifestEditor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeVisitor child(String ns, String name) {
|
||||
AxmlWriter.NodeImpl first = new MutableAxmlWriter.MutableNodeImpl(ns, name);
|
||||
this.firsts.add(first);
|
||||
return first;
|
||||
}
|
||||
|
||||
public class MutableNodeImpl extends AxmlWriter.NodeImpl {
|
||||
private boolean ignore;
|
||||
private final String name;
|
||||
|
||||
MutableNodeImpl(String ns, String name) {
|
||||
super(ns, name);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttr(AxmlWriter.Attr a) {
|
||||
if ("uses-permission".equals(this.name) && "name".equals(a.name.data) && a.value instanceof StringItem) {
|
||||
if (manifestEditor.filterPermission(((StringItem) a.value).data)) {
|
||||
ignore = true;
|
||||
}
|
||||
}
|
||||
manifestEditor.onAttr(a);
|
||||
super.onAttr(a);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NodeVisitor child(String ns, String name) {
|
||||
Log.d("MutableAxmlWriter", "child: " + ns + " name: " + name);
|
||||
AxmlWriter.NodeImpl child = new MutableAxmlWriter.MutableNodeImpl(ns, name);
|
||||
this.children.add(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
public boolean isIgnore() {
|
||||
return ignore;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ package pxb.android.axml;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.stardust.autojs.apkbuilder.ManifestEditor;
|
||||
import com.stardust.autojs.apkbuilder.MutableAxmlWriter;
|
||||
|
||||
import pxb.android.StringItem;
|
||||
import pxb.android.StringItems;
|
||||
@ -96,8 +96,8 @@ public class AxmlWriter extends AxmlVisitor {
|
||||
int size = 0;
|
||||
|
||||
for (NodeImpl first : firsts) {
|
||||
if (first instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
|
||||
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
|
||||
if (first instanceof MutableAxmlWriter.MutableNodeImpl) {
|
||||
if (((MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
|
||||
Log.d(TAG, "prepare: first is ignore: " + first.name);
|
||||
continue;
|
||||
}
|
||||
@ -173,8 +173,8 @@ public class AxmlWriter extends AxmlVisitor {
|
||||
}
|
||||
|
||||
for (NodeImpl first : firsts) {
|
||||
if (first instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
|
||||
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
|
||||
if (first instanceof MutableAxmlWriter.MutableNodeImpl) {
|
||||
if (((MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
|
||||
Log.d(TAG, "toByteArray: first is ignore: " + first.name);
|
||||
continue;
|
||||
}
|
||||
@ -359,8 +359,8 @@ public class AxmlWriter extends AxmlVisitor {
|
||||
int size = 24 + 36 + attrs.size() * 20;// 24 for end tag,36+x*20 for
|
||||
// start tag
|
||||
for (NodeImpl child : children) {
|
||||
if (child instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
|
||||
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
|
||||
if (child instanceof MutableAxmlWriter.MutableNodeImpl) {
|
||||
if (((MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
|
||||
Log.d(TAG, "prepare: child is ignore: " + child.name);
|
||||
continue;
|
||||
}
|
||||
@ -421,8 +421,8 @@ public class AxmlWriter extends AxmlVisitor {
|
||||
|
||||
// children
|
||||
for (NodeImpl child : children) {
|
||||
if (child instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
|
||||
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
|
||||
if (child instanceof MutableAxmlWriter.MutableNodeImpl) {
|
||||
if (((MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
|
||||
Log.d(TAG, "white: child is ignore: " + child.name);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ import com.android.apksig.ApkSigner;
|
||||
import com.android.apksigner.PasswordRetriever;
|
||||
import com.android.apksigner.SignerParams;
|
||||
import com.stardust.app.GlobalAppContext;
|
||||
import com.stardust.autojs.apkbuilder.ApkPackager;
|
||||
import com.stardust.autojs.apkbuilder.ApkUnpackUtil;
|
||||
import com.stardust.autojs.apkbuilder.ManifestEditor;
|
||||
import com.stardust.autojs.apkbuilder.util.StreamUtils;
|
||||
import com.stardust.autojs.project.BuildInfo;
|
||||
@ -197,7 +197,7 @@ public class ApkBuilder {
|
||||
}
|
||||
|
||||
private ProgressCallback mProgressCallback;
|
||||
private ApkPackager mApkPackager;
|
||||
private ApkUnpackUtil mApkUnpackUtil;
|
||||
private String mArscPackageName;
|
||||
private ManifestEditor mManifestEditor;
|
||||
private String mWorkspacePath;
|
||||
@ -210,7 +210,7 @@ public class ApkBuilder {
|
||||
public ApkBuilder(InputStream apkInputStream, File outApkFile, String workspacePath) {
|
||||
mWorkspacePath = workspacePath;
|
||||
mOutApkFile = outApkFile;
|
||||
mApkPackager = new ApkPackager(apkInputStream, mWorkspacePath);
|
||||
mApkUnpackUtil = new ApkUnpackUtil(apkInputStream, mWorkspacePath);
|
||||
PFiles.ensureDir(outApkFile.getPath());
|
||||
}
|
||||
|
||||
@ -224,7 +224,7 @@ public class ApkBuilder {
|
||||
GlobalAppContext.post(() -> mProgressCallback.onPrepare(ApkBuilder.this));
|
||||
}
|
||||
(new File(mWorkspacePath)).mkdirs();
|
||||
mApkPackager.unzip();
|
||||
mApkUnpackUtil.unzip();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user