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;
}
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;
}
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;
}
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());
}
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);
}
Aggregations