use of com.android.annotations.VisibleForTesting in project android by JetBrains.
the class SourceToGradleModuleStep method checkPath.
@NotNull
@VisibleForTesting
PathValidationResult checkPath(@NotNull String path) {
if (Strings.isNullOrEmpty(path)) {
return PathValidationResult.ofType(EMPTY_PATH);
}
VirtualFile vFile = VfsUtil.findFileByIoFile(new File(path), false);
if (vFile == null || !vFile.exists()) {
return PathValidationResult.ofType(DOES_NOT_EXIST);
} else if (isProjectOrModule(vFile)) {
return PathValidationResult.ofType(IS_PROJECT_OR_MODULE);
}
ModuleImporter importer = ModuleImporter.importerForLocation(getModel().getContext(), vFile);
if (!importer.isValid()) {
return PathValidationResult.ofType(NOT_ADT_OR_GRADLE);
}
Collection<ModuleToImport> modules = ApplicationManager.getApplication().runReadAction((Computable<Collection<ModuleToImport>>) () -> {
try {
return importer.findModules(vFile);
} catch (IOException e) {
Logger.getInstance(SourceToGradleModuleStep.class).error(e);
return null;
}
});
if (modules == null) {
return PathValidationResult.ofType(INTERNAL_ERROR);
}
Set<String> missingSourceModuleNames = Sets.newTreeSet();
for (ModuleToImport module : modules) {
if (module.location == null || !module.location.exists()) {
missingSourceModuleNames.add(module.name);
}
}
if (!missingSourceModuleNames.isEmpty()) {
return new PathValidationResult(MISSING_SUBPROJECTS, vFile, importer, modules, missingSourceModuleNames);
}
return new PathValidationResult(OK, vFile, importer, modules, null);
}
use of com.android.annotations.VisibleForTesting in project android by JetBrains.
the class AvdWizardUtils method emulatorSupportsWebp.
@VisibleForTesting
static boolean emulatorSupportsWebp(@NotNull AndroidSdkHandler sdkHandler) {
ProgressIndicator log = new StudioLoggerProgressIndicator(AvdWizardUtils.class);
LocalPackage sdkPackage = sdkHandler.getLocalPackage(FD_EMULATOR, log);
if (sdkPackage == null) {
sdkPackage = sdkHandler.getLocalPackage(FD_TOOLS, log);
}
if (sdkPackage != null) {
Revision version = sdkPackage.getVersion();
// >= 25.2.3?
if (version.getMajor() > 25 || version.getMajor() == 25 && (version.getMinor() > 2 || version.getMinor() == 2 && version.getMicro() >= 3)) {
return true;
}
}
return false;
}
use of com.android.annotations.VisibleForTesting in project android by JetBrains.
the class AvdWizardUtils method convertWebpSkinToPng.
/**
* Copies a skin folder from the internal device data folder over to the SDK skin folder, rewriting
* the webp files to PNG, and rewriting the layout file to reference webp instead.
*
* @param fop the file operation to use to conduct I/O
* @param dest the destination folder to write the skin files to
* @param resourcePath the source folder to read skin files from
* @throws IOException if there's a problem
*/
@VisibleForTesting
static void convertWebpSkinToPng(@NotNull FileOp fop, @NotNull File dest, @NotNull File resourcePath) throws IOException {
File[] files = fop.listFiles(resourcePath);
Map<String, String> renameMap = Maps.newHashMap();
File skinFile = null;
for (File src : files) {
String name = src.getName();
if (name.equals(FN_SKIN_LAYOUT)) {
skinFile = src;
continue;
}
if (name.endsWith(DOT_WEBP)) {
// Convert WEBP to PNG until emulator supports it
try (InputStream inputStream = new BufferedInputStream(fop.newFileInputStream(src))) {
BufferedImage icon = ImageIO.read(inputStream);
if (icon != null) {
File target = new File(dest, name.substring(0, name.length() - DOT_WEBP.length()) + DOT_PNG);
try (BufferedOutputStream outputStream = new BufferedOutputStream(fop.newFileOutputStream(target))) {
ImageIO.write(icon, "PNG", outputStream);
renameMap.put(name, target.getName());
continue;
}
}
}
}
// Normal copy: either the file is not a webp or skin file (for example, some skins such as the
// wear ones are not in webp format), or it's a webp file where we couldn't
// (a) decode the webp file (for example if there's a problem loading the native library doing webp
// decoding, or (b) there was an I/O error writing the PNG file. In that case we'll leave the file in
// webp format (future emulators support it.)
File target = new File(dest, name);
if (fop.isFile(src) && !fop.exists(target)) {
fop.copyFile(src, target);
}
}
if (skinFile != null) {
// Replace skin paths
try (InputStream inputStream = new BufferedInputStream(fop.newFileInputStream(skinFile))) {
File target = new File(dest, skinFile.getName());
try (BufferedOutputStream outputStream = new BufferedOutputStream(fop.newFileOutputStream(target))) {
byte[] bytes = ByteStreams.toByteArray(inputStream);
String layout = new String(bytes, Charsets.UTF_8);
for (Map.Entry<String, String> entry : renameMap.entrySet()) {
layout = layout.replace(entry.getKey(), entry.getValue());
}
outputStream.write(layout.getBytes(Charsets.UTF_8));
}
}
}
}
use of com.android.annotations.VisibleForTesting in project android by JetBrains.
the class ApkDiffParser method createTreeNode.
@VisibleForTesting
@NotNull
static DefaultMutableTreeNode createTreeNode(@Nullable VirtualFile oldFile, @Nullable VirtualFile newFile) {
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 String name = oldFile == null ? newFile.getName() : oldFile.getName();
if (oldFile != null) {
if (StringUtil.equals(oldFile.getExtension(), SdkConstants.EXT_ZIP)) {
VirtualFile zipRoot = ApkFileSystem.getInstance().extractAndGetContentRoot(oldFile);
if (zipRoot != null) {
oldFile = zipRoot;
}
}
if (oldFile.isDirectory()) {
//noinspection UnsafeVfsRecursion (no symlinks inside an APK)
for (VirtualFile oldChild : oldFile.getChildren()) {
VirtualFile newChild = newFile == null ? null : newFile.findChild(oldChild.getName());
childrenInOldFile.add(oldChild.getName());
DefaultMutableTreeNode childNode = createTreeNode(oldChild, newChild);
node.add(childNode);
ApkDiffEntry entry = (ApkDiffEntry) childNode.getUserObject();
oldSize += entry.getOldSize();
newSize += entry.getNewSize();
}
if (oldFile.getLength() > 0) {
// This is probably a zip inside the apk, and we should use it's size
oldSize = oldFile.getLength();
}
} else {
oldSize += oldFile.getLength();
}
}
if (newFile != null) {
if (StringUtil.equals(newFile.getExtension(), SdkConstants.EXT_ZIP)) {
VirtualFile zipRoot = ApkFileSystem.getInstance().extractAndGetContentRoot(newFile);
if (zipRoot != null) {
newFile = zipRoot;
}
}
if (newFile.isDirectory()) {
//noinspection UnsafeVfsRecursion (no symlinks inside an APK)
for (VirtualFile newChild : newFile.getChildren()) {
if (childrenInOldFile.contains(newChild.getName())) {
continue;
}
DefaultMutableTreeNode childNode = createTreeNode(null, newChild);
node.add(childNode);
ApkDiffEntry entry = (ApkDiffEntry) childNode.getUserObject();
oldSize += entry.getOldSize();
newSize += entry.getNewSize();
}
if (newFile.getLength() > 0) {
// This is probably a zip inside the apk, and we should use it's size
newSize = newFile.getLength();
}
} else {
newSize += newFile.getLength();
}
}
node.setUserObject(new ApkDiffEntry(name, oldFile, newFile, oldSize, newSize));
ApkParser.sort(node);
return node;
}
use of com.android.annotations.VisibleForTesting in project android by JetBrains.
the class AvdDisplayList method getResolution.
/**
* @return the resolution of a given AVD as a string of the format [width]x[height] - [density]
* (e.g. 1200x1920 - xhdpi) or "Unknown Resolution" if the AVD does not define a resolution.
*/
@VisibleForTesting
static String getResolution(@NotNull AvdInfo info) {
DeviceManagerConnection deviceManager = DeviceManagerConnection.getDefaultDeviceManagerConnection();
Device device = deviceManager.getDevice(info.getDeviceName(), info.getDeviceManufacturer());
Dimension res = null;
Density density = null;
if (device != null) {
res = device.getScreenSize(device.getDefaultState().getOrientation());
density = device.getDefaultHardware().getScreen().getPixelDensity();
}
String resolution;
String densityString = density == null ? "Unknown Density" : density.getResourceValue();
if (res != null) {
resolution = String.format(Locale.getDefault(), "%1$d × %2$d: %3$s", res.width, res.height, densityString);
} else {
resolution = "Unknown Resolution";
}
return resolution;
}
Aggregations