Search in sources :

Example 1 with ArchiveNode

use of io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode in project android-bundle-support by lizhangqu.

the class ApkFileByFileDiffParser method createTreeNode.

@VisibleForTesting
@NonNull
static DefaultMutableTreeNode createTreeNode(ArchiveNode oldFile, ArchiveNode newFile, Map<String, Long> pathsToDiffSize) throws IOException {
    if (oldFile == null && newFile == null) {
        throw new IllegalArgumentException("Both old and new files are null");
    }
    DefaultMutableTreeNode node = new DefaultMutableTreeNode();
    long oldSize = 0;
    long newSize = 0;
    long patchSize = 0;
    HashSet<String> childrenInOldFile = new HashSet<>();
    final ArchiveEntry data = oldFile == null ? newFile.getData() : oldFile.getData();
    final String name = data.getPath().getFileName() != null ? data.getPath().getFileName().toString() : data.getArchive().getPath().getFileName().toString();
    if (oldFile != null) {
        if (!oldFile.getChildren().isEmpty()) {
            for (ArchiveNode oldChild : oldFile.getChildren()) {
                ArchiveNode newChild = null;
                if (newFile != null) {
                    for (ArchiveNode archiveNode : newFile.getChildren()) {
                        if (archiveNode.getData().getPath().getFileName().toString().equals(oldChild.getData().getPath().getFileName().toString())) {
                            newChild = archiveNode;
                            break;
                        }
                    }
                }
                childrenInOldFile.add(oldChild.getData().getPath().getFileName().toString());
                DefaultMutableTreeNode childNode = createTreeNode(oldChild, newChild, pathsToDiffSize);
                node.add(childNode);
                ApkDiffEntry entry = (ApkDiffEntry) childNode.getUserObject();
                oldSize += entry.getOldSize();
                newSize += entry.getNewSize();
                patchSize += entry.getSize();
            }
            if (Files.size(oldFile.getData().getPath()) > 0) {
                // This is probably a zip inside the apk, and we should use it's size
                oldSize = Files.size(oldFile.getData().getPath());
            } else if (oldFile.getParent() == null) {
                oldSize = Files.size(oldFile.getData().getArchive().getPath());
            }
        } else {
            oldSize += Files.size(oldFile.getData().getPath());
        }
    }
    if (newFile != null) {
        if (!newFile.getChildren().isEmpty()) {
            for (ArchiveNode newChild : newFile.getChildren()) {
                if (childrenInOldFile.contains(newChild.getData().getPath().getFileName().toString())) {
                    continue;
                }
                DefaultMutableTreeNode childNode = createTreeNode(null, newChild, pathsToDiffSize);
                node.add(childNode);
                ApkDiffEntry entry = (ApkDiffEntry) childNode.getUserObject();
                oldSize += entry.getOldSize();
                newSize += entry.getNewSize();
                patchSize += entry.getSize();
            }
            if (Files.size(newFile.getData().getPath()) > 0) {
                // This is probably a zip inside the apk, and we should use it's size
                newSize = Files.size(newFile.getData().getPath());
                String relPath = newFile.getData().getArchive().getContentRoot().relativize(newFile.getData().getPath()).toString();
                if (pathsToDiffSize.containsKey(relPath)) {
                    patchSize = pathsToDiffSize.get(relPath);
                }
            } else if (newFile.getParent() == null) {
                newSize = Files.size(newFile.getData().getArchive().getPath());
            }
        } else {
            newSize += Files.size(newFile.getData().getPath());
            String relPath = newFile.getData().getArchive().getContentRoot().relativize(newFile.getData().getPath()).toString();
            if (pathsToDiffSize.containsKey(relPath)) {
                patchSize = pathsToDiffSize.get(relPath);
            }
        }
    }
    node.setUserObject(new ApkFileByFileEntry(name, oldFile, newFile, oldSize, newSize, patchSize));
    ApkEntry.sort(node);
    return node;
}
Also used : ArchiveNode(io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArchiveEntry(io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveEntry) HashSet(java.util.HashSet) VisibleForTesting(com.android.annotations.VisibleForTesting) NonNull(com.android.annotations.NonNull)

Example 2 with ArchiveNode

use of io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode in project android-bundle-support by lizhangqu.

the class ApkFileByFileDiffParser method createTreeNode.

@NonNull
public static DefaultMutableTreeNode createTreeNode(@NonNull ArchiveContext oldFile, @NonNull ArchiveContext newFile) throws IOException, InterruptedException {
    ArchiveNode oldRoot = ArchiveTreeStructure.create(oldFile);
    ArchiveNode newRoot = ArchiveTreeStructure.create(newFile);
    PatchExplainer explainer = new PatchExplainer(new DeflateCompressor(), new BsDiffDeltaGenerator());
    Map<String, Long> pathsToDiffSize = new HashMap<>();
    List<EntryExplanation> explanationList = explainer.explainPatch(oldFile.getArchive().getPath().toFile(), newFile.getArchive().getPath().toFile());
    for (EntryExplanation explanation : explanationList) {
        String path = new String(explanation.getPath().getData(), "UTF8");
        pathsToDiffSize.put(path, explanation.getCompressedSizeInPatch());
    }
    return createTreeNode(oldRoot, newRoot, pathsToDiffSize);
}
Also used : EntryExplanation(com.google.archivepatcher.explainer.EntryExplanation) ArchiveNode(io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode) DeflateCompressor(com.google.archivepatcher.shared.DeflateCompressor) HashMap(java.util.HashMap) BsDiffDeltaGenerator(com.google.archivepatcher.generator.bsdiff.BsDiffDeltaGenerator) PatchExplainer(com.google.archivepatcher.explainer.PatchExplainer) NonNull(com.android.annotations.NonNull)

Example 3 with ArchiveNode

use of io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode in project android-bundle-support by lizhangqu.

the class ApkDiffParser method createTreeNode.

@NonNull
private static DefaultMutableTreeNode createTreeNode(@Nullable ArchiveNode oldFile, @Nullable ArchiveNode newFile) throws IOException {
    if (oldFile == null && newFile == null) {
        throw new IllegalArgumentException("Both old and new files are null");
    }
    DefaultMutableTreeNode node = new DefaultMutableTreeNode();
    long oldSize = 0;
    long newSize = 0;
    HashSet<String> childrenInOldFile = new HashSet<>();
    final ArchiveEntry data = oldFile == null ? newFile.getData() : oldFile.getData();
    final String name = data.getPath().getFileName() != null ? data.getPath().getFileName().toString() : data.getArchive().getPath().getFileName().toString();
    if (oldFile != null) {
        if (!oldFile.getChildren().isEmpty()) {
            for (ArchiveNode oldChild : oldFile.getChildren()) {
                ArchiveNode newChild = null;
                if (newFile != null) {
                    for (ArchiveNode archiveNode : newFile.getChildren()) {
                        if (archiveNode.getData().getPath().getFileName().toString().equals(oldChild.getData().getPath().getFileName().toString())) {
                            newChild = archiveNode;
                            break;
                        }
                    }
                }
                childrenInOldFile.add(oldChild.getData().getPath().getFileName().toString());
                DefaultMutableTreeNode childNode = createTreeNode(oldChild, newChild);
                node.add(childNode);
                ApkDiffEntry entry = (ApkDiffEntry) childNode.getUserObject();
                oldSize += entry.getOldSize();
                newSize += entry.getNewSize();
            }
            if (Files.size(oldFile.getData().getPath()) > 0) {
                // This is probably a zip inside the apk, and we should use it's size
                oldSize = Files.size(oldFile.getData().getPath());
            } else if (oldFile.getParent() == null) {
                oldSize = Files.size(oldFile.getData().getArchive().getPath());
            }
        } else {
            oldSize += Files.size(oldFile.getData().getPath());
        }
    }
    if (newFile != null) {
        if (!newFile.getChildren().isEmpty()) {
            for (ArchiveNode newChild : newFile.getChildren()) {
                if (childrenInOldFile.contains(newChild.getData().getPath().getFileName().toString())) {
                    continue;
                }
                DefaultMutableTreeNode childNode = createTreeNode(null, newChild);
                node.add(childNode);
                ApkDiffEntry entry = (ApkDiffEntry) childNode.getUserObject();
                oldSize += entry.getOldSize();
                newSize += entry.getNewSize();
            }
            if (Files.size(newFile.getData().getPath()) > 0) {
                // This is probably a zip inside the apk, and we should use it's size
                newSize = Files.size(newFile.getData().getPath());
            } else if (newFile.getParent() == null) {
                newSize = Files.size(newFile.getData().getArchive().getPath());
            }
        } else {
            newSize += Files.size(newFile.getData().getPath());
        }
    }
    node.setUserObject(new ApkDiffEntry(name, oldFile, newFile, oldSize, newSize));
    ApkEntry.sort(node);
    return node;
}
Also used : ArchiveNode(io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArchiveEntry(io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveEntry) HashSet(java.util.HashSet) NonNull(com.android.annotations.NonNull)

Example 4 with ArchiveNode

use of io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode in project android-bundle-support by lizhangqu.

the class ApkDiffParser method createTreeNode.

@NonNull
public static DefaultMutableTreeNode createTreeNode(@NonNull ArchiveContext oldFile, @NonNull ArchiveContext newFile) throws IOException {
    ArchiveNode oldRoot = ArchiveTreeStructure.create(oldFile);
    ArchiveNode newRoot = ArchiveTreeStructure.create(newFile);
    return createTreeNode(oldRoot, newRoot);
}
Also used : ArchiveNode(io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode) NonNull(com.android.annotations.NonNull)

Aggregations

NonNull (com.android.annotations.NonNull)4 ArchiveNode (io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveNode)4 ArchiveEntry (io.github.lizhangqu.intellij.android.bundle.analyzer.ArchiveEntry)2 HashSet (java.util.HashSet)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 EntryExplanation (com.google.archivepatcher.explainer.EntryExplanation)1 PatchExplainer (com.google.archivepatcher.explainer.PatchExplainer)1 BsDiffDeltaGenerator (com.google.archivepatcher.generator.bsdiff.BsDiffDeltaGenerator)1 DeflateCompressor (com.google.archivepatcher.shared.DeflateCompressor)1 HashMap (java.util.HashMap)1