Search in sources :

Example 26 with VisibleForTesting

use of com.android.annotations.VisibleForTesting in project android by JetBrains.

the class ImagePool method create.

@VisibleForTesting
@NotNull
ImageImpl create(final int w, final int h, final int type, @Nullable Consumer<BufferedImage> freedCallback) {
    assert !isDisposed : "ImagePool already disposed";
    EvictingQueue<SoftReference<BufferedImage>> queue = getTypeQueue(w, h, type);
    BufferedImage image;
    SoftReference<BufferedImage> imageRef;
    try {
        imageRef = queue.remove();
        while ((image = imageRef.get()) == null) {
            imageRef = queue.remove();
        }
        if (DEBUG) {
            //noinspection UseOfSystemOutOrSystemErr
            System.out.printf("Re-used image %dx%d - %d\n", w, h, type);
        }
        // Clear the image
        Graphics2D g = image.createGraphics();
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(0, 0, w, h);
        g.dispose();
    } catch (NoSuchElementException e) {
        if (DEBUG) {
            //noinspection UseOfSystemOutOrSystemErr
            System.out.printf("New image %dx%d - %d\n", w, h, type);
        }
        //noinspection UndesirableClassUsage
        image = new BufferedImage(w, h, type);
    }
    ImageImpl pooledImage = new ImageImpl(image);
    final BufferedImage imagePointer = image;
    Reference<?> reference = new FinalizablePhantomReference<Image>(pooledImage, myFinalizableReferenceQueue) {

        @Override
        public void finalizeReferent() {
            if (DEBUG) {
                //noinspection UseOfSystemOutOrSystemErr
                System.out.printf("Released image %dx%d - %d\n", w, h, type);
            }
            myReferences.remove(this);
            getTypeQueue(w, h, type).add(new SoftReference<>(imagePointer));
            if (freedCallback != null) {
                freedCallback.accept(imagePointer);
            }
        }
    };
    myReferences.add(reference);
    return pooledImage;
}
Also used : SoftReference(java.lang.ref.SoftReference) FinalizablePhantomReference(com.google.common.base.FinalizablePhantomReference) BufferedImage(java.awt.image.BufferedImage) NoSuchElementException(java.util.NoSuchElementException) VisibleForTesting(com.android.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with VisibleForTesting

use of com.android.annotations.VisibleForTesting in project android by JetBrains.

the class GradleProjectSyncData method createFrom.

@Nullable
@VisibleForTesting
static GradleProjectSyncData createFrom(@NotNull Project project) throws IOException {
    GradleProjectSyncData data = new GradleProjectSyncData();
    File rootDirPath = getBaseDirPath(project);
    Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
        GradleFacet gradleFacet = GradleFacet.getInstance(module);
        if (gradleFacet != null) {
            GradleModuleModel gradleModuleModel = gradleFacet.getGradleModuleModel();
            if (gradleModuleModel != null) {
                data.addFileChecksum(rootDirPath, gradleModuleModel.getBuildFile());
            } else {
                LOG.warn(String.format("Trying to create project data from a not initialized project '%1$s'. Abort.", project.getName()));
                return null;
            }
        }
        if (isGradleProjectModule(module)) {
            data.addFileChecksum(rootDirPath, getGradleBuildFile(module));
            data.addFileChecksum(rootDirPath, getGradleSettingsFile(rootDirPath));
            data.addFileChecksum(rootDirPath, new File(rootDirPath, FN_GRADLE_PROPERTIES));
            data.addFileChecksum(rootDirPath, new File(rootDirPath, FN_LOCAL_PROPERTIES));
            data.addFileChecksum(rootDirPath, getGradleUserSettingsFile());
        }
        NdkModuleModel ndkModuleModel = NdkModuleModel.get(module);
        if (ndkModuleModel != null) {
            for (File externalBuildFile : ndkModuleModel.getAndroidProject().getBuildFiles()) {
                data.addFileChecksum(rootDirPath, externalBuildFile);
            }
        }
    }
    GradleSyncState syncState = GradleSyncState.getInstance(project);
    data.myLastGradleSyncTimestamp = syncState.getSummary().getSyncTimestamp();
    return data;
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Projects.isGradleProjectModule(com.android.tools.idea.gradle.util.Projects.isGradleProjectModule) Module(com.intellij.openapi.module.Module) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) VisibleForTesting(com.android.annotations.VisibleForTesting) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with VisibleForTesting

use of com.android.annotations.VisibleForTesting in project android by JetBrains.

the class ApkParser method createTreeNode.

@VisibleForTesting
@NotNull
static DefaultMutableTreeNode createTreeNode(@NotNull VirtualFile file) {
    String originalName = null;
    DefaultMutableTreeNode node = new DefaultMutableTreeNode();
    long size = 0;
    if (StringUtil.equals(file.getExtension(), SdkConstants.EXT_ZIP)) {
        VirtualFile zipRoot = ApkFileSystem.getInstance().extractAndGetContentRoot(file);
        if (zipRoot != null) {
            originalName = file.getName();
            file = zipRoot;
        }
    }
    if (file.isDirectory()) {
        //noinspection UnsafeVfsRecursion (no symlinks inside an APK)
        for (VirtualFile child : file.getChildren()) {
            DefaultMutableTreeNode childNode = createTreeNode(child);
            node.add(childNode);
            size += ((ApkEntry) childNode.getUserObject()).getSize();
        }
        if (file.getLength() > 0) {
            // This is probably a zip inside the apk, and we should use it's size
            size = file.getLength();
        }
    } else {
        size = file.getLength();
    }
    node.setUserObject(new ApkEntryImpl(file, originalName, size));
    sort(node);
    return node;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) VisibleForTesting(com.android.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with VisibleForTesting

use of com.android.annotations.VisibleForTesting in project android by JetBrains.

the class ResourceFolderRepository method equalFilesItems.

// Not a super-robust comparator. Compares a subset of the repo state for testing.
// Returns false if a difference is found.
// Returns true if the repositories are roughly equivalent.
@VisibleForTesting
boolean equalFilesItems(ResourceFolderRepository other) {
    File myResourceDirFile = VfsUtilCore.virtualToIoFile(myResourceDir);
    File otherResourceDir = VfsUtilCore.virtualToIoFile(other.myResourceDir);
    if (!FileUtil.filesEqual(myResourceDirFile, otherResourceDir)) {
        return false;
    }
    if (myResourceFiles.size() != other.myResourceFiles.size()) {
        return false;
    }
    for (Map.Entry<VirtualFile, ResourceFile> fileEntry : myResourceFiles.entrySet()) {
        ResourceFile otherResFile = other.myResourceFiles.get(fileEntry.getKey());
        if (otherResFile == null) {
            return false;
        }
        if (!FileUtil.filesEqual(fileEntry.getValue().getFile(), otherResFile.getFile())) {
            return false;
        }
    }
    if (myItems.size() != other.myItems.size()) {
        return false;
    }
    for (Map.Entry<ResourceType, ListMultimap<String, ResourceItem>> entry : myItems.entrySet()) {
        ListMultimap<String, ResourceItem> ownEntries = entry.getValue();
        ListMultimap<String, ResourceItem> otherEntries = other.myItems.get(entry.getKey());
        if (otherEntries == null || otherEntries.size() != ownEntries.size()) {
            return false;
        }
        for (Map.Entry<String, ResourceItem> itemEntry : ownEntries.entries()) {
            List<ResourceItem> otherItemsList = otherEntries.get(itemEntry.getKey());
            if (otherItemsList == null) {
                return false;
            }
            final ResourceItem item = itemEntry.getValue();
            if (!ContainerUtil.exists(otherItemsList, new Condition<ResourceItem>() {

                @Override
                public boolean value(ResourceItem resourceItem) {
                    // Use #compareTo instead of #equals because #equals compares pointers of mSource.
                    if (resourceItem.compareTo(item) != 0) {
                        return false;
                    }
                    // Skip ID type resources, where the ResourceValues are not important and where blob writing doesn't preserve the value.
                    if (item.getType() != ResourceType.ID) {
                        ResourceValue resValue = item.getResourceValue(false);
                        ResourceValue otherResValue = resourceItem.getResourceValue(false);
                        if (resValue == null || otherResValue == null) {
                            if (resValue != otherResValue) {
                                return false;
                            }
                        } else {
                            String resValueStr = resValue.getValue();
                            String otherResValueStr = otherResValue.getValue();
                            if (resValueStr == null || otherResValueStr == null) {
                                if (resValueStr != otherResValueStr) {
                                    return false;
                                }
                            } else {
                                if (!resValueStr.equals(otherResValueStr)) {
                                    return false;
                                }
                            }
                        }
                    }
                    // We can only compareValueWith (compare equivalence of XML nodes) for VALUE items.
                    // For others, the XML node may be different before and after serialization.
                    ResourceFile source = item.getSource();
                    ResourceFile otherSource = resourceItem.getSource();
                    if (source != null && otherSource != null) {
                        ResourceFolderType ownFolderType = ResourceHelper.getFolderType(source);
                        ResourceFolderType otherFolderType = ResourceHelper.getFolderType(otherSource);
                        if (otherFolderType != ownFolderType) {
                            return false;
                        }
                        if (otherFolderType == VALUES) {
                            return resourceItem.compareValueWith(item);
                        }
                    }
                    return true;
                }
            })) {
                return false;
            }
        }
    }
    // Only compare the keys.
    return myDataBindingResourceFiles.keySet().equals(other.myDataBindingResourceFiles.keySet());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Condition(com.intellij.openapi.util.Condition) ResourceType(com.android.resources.ResourceType) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) VisibleForTesting(com.android.annotations.VisibleForTesting)

Example 30 with VisibleForTesting

use of com.android.annotations.VisibleForTesting in project android by JetBrains.

the class ModuleResourceRepository method updateRoots.

@VisibleForTesting
void updateRoots(List<VirtualFile> resourceDirectories) {
    // Non-folder repositories: Always kept last in the list
    List<LocalResourceRepository> other = null;
    // Compute current roots
    Map<VirtualFile, ResourceFolderRepository> map = Maps.newHashMap();
    for (LocalResourceRepository repository : myChildren) {
        if (repository instanceof ResourceFolderRepository) {
            ResourceFolderRepository folderRepository = (ResourceFolderRepository) repository;
            VirtualFile resourceDir = folderRepository.getResourceDir();
            map.put(resourceDir, folderRepository);
        } else {
            assert repository instanceof DynamicResourceValueRepository;
            if (other == null) {
                other = Lists.newArrayList();
            }
            other.add(repository);
        }
    }
    // Compute new resource directories (it's possible for just the order to differ, or
    // for resource dirs to have been added and/or removed)
    Set<VirtualFile> newDirs = Sets.newHashSet(resourceDirectories);
    List<LocalResourceRepository> resources = Lists.newArrayListWithExpectedSize(newDirs.size() + (other != null ? other.size() : 0));
    for (VirtualFile dir : resourceDirectories) {
        ResourceFolderRepository repository = map.get(dir);
        if (repository == null) {
            repository = ResourceFolderRegistry.get(myFacet, dir);
        } else {
            map.remove(dir);
        }
        resources.add(repository);
    }
    if (other != null) {
        resources.addAll(other);
    }
    if (resources.equals(myChildren)) {
        // shouldn't have created any new ones
        assert map.isEmpty();
        return;
    }
    for (ResourceFolderRepository removed : map.values()) {
        removed.removeParent(this);
    }
    setChildren(resources);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VisibleForTesting(com.android.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.android.annotations.VisibleForTesting)32 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 File (java.io.File)7 NotNull (org.jetbrains.annotations.NotNull)7 Module (com.intellij.openapi.module.Module)5 ResourceType (com.android.resources.ResourceType)3 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)3 Nullable (com.android.annotations.Nullable)2 GradleVersion (com.android.ide.common.repository.GradleVersion)2 Revision (com.android.repository.Revision)2 Device (com.android.sdklib.devices.Device)2 ModuleImporter (com.android.tools.idea.gradle.project.ModuleImporter)2 GradleFacet (com.android.tools.idea.gradle.project.facet.gradle.GradleFacet)2 NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)2 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)2 PsiFile (com.intellij.psi.PsiFile)2 BufferedImage (java.awt.image.BufferedImage)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 Nullable (org.jetbrains.annotations.Nullable)2 ApiObjectFactory (com.android.build.gradle.internal.ApiObjectFactory)1