![JAR search and dependency download from the Maven repository](/logo.png)
com.android.sdklib.internal.repository.LocalSdkParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdklib Show documentation
Show all versions of sdklib Show documentation
A library to parse and download the Android SDK.
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.sdklib.internal.repository;
import com.android.SdkConstants;
import com.android.annotations.NonNull;
import com.android.io.FileWrapper;
import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.ISystemImage;
import com.android.sdklib.ISystemImage.LocationType;
import com.android.sdklib.SdkManager;
import com.android.sdklib.internal.androidTarget.PlatformTarget;
import com.android.sdklib.internal.project.ProjectProperties;
import com.android.sdklib.internal.repository.packages.AddonPackage;
import com.android.sdklib.internal.repository.packages.BuildToolPackage;
import com.android.sdklib.internal.repository.packages.DocPackage;
import com.android.sdklib.internal.repository.packages.ExtraPackage;
import com.android.sdklib.internal.repository.packages.Package;
import com.android.sdklib.internal.repository.packages.PlatformPackage;
import com.android.sdklib.internal.repository.packages.PlatformToolPackage;
import com.android.sdklib.internal.repository.packages.SamplePackage;
import com.android.sdklib.internal.repository.packages.SourcePackage;
import com.android.sdklib.internal.repository.packages.SystemImagePackage;
import com.android.sdklib.internal.repository.packages.ToolPackage;
import com.android.sdklib.io.FileOp;
import com.android.sdklib.repository.AddonManifestIniProps;
import com.android.sdklib.repository.descriptors.PkgType;
import com.android.utils.ILogger;
import com.android.utils.Pair;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* Scans a local SDK to find which packages are currently installed.
*/
public class LocalSdkParser {
private Package[] mPackages;
/** Parse all SDK folders. */
public static final int PARSE_ALL = PkgType.PKG_ALL_INT;
/** Parse the SDK/tools folder. */
public static final int PARSE_TOOLS = PkgType.PKG_TOOLS.getIntValue();
/** Parse the SDK/platform-tools folder */
public static final int PARSE_PLATFORM_TOOLS = PkgType.PKG_PLATFORM_TOOLS.getIntValue();
/** Parse the SDK/docs folder. */
public static final int PARSE_DOCS = PkgType.PKG_DOC.getIntValue();
/**
* Equivalent to parsing the SDK/platforms folder but does so
* by using the valid targets loaded by the {@link SdkManager}.
* Parsing the platforms also parses the SDK/system-images folder.
*/
public static final int PARSE_PLATFORMS = PkgType.PKG_PLATFORM.getIntValue();
/**
* Equivalent to parsing the SDK/addons folder but does so
* by using the valid targets loaded by the {@link SdkManager}.
*/
public static final int PARSE_ADDONS = PkgType.PKG_ADDON.getIntValue();
/** Parse the SDK/samples folder.
* Note: this will not detect samples located in the SDK/extras packages. */
public static final int PARSE_SAMPLES = PkgType.PKG_SAMPLE.getIntValue();
/** Parse the SDK/sources folder. */
public static final int PARSE_SOURCES = PkgType.PKG_SOURCE.getIntValue();
/** Parse the SDK/extras folder. */
public static final int PARSE_EXTRAS = PkgType.PKG_EXTRA.getIntValue();
/** Parse the SDK/build-tools folder. */
public static final int PARSE_BUILD_TOOLS = PkgType.PKG_BUILD_TOOLS.getIntValue();
public LocalSdkParser() {
// pass
}
/**
* Returns the packages found by the last call to {@link #parseSdk}.
*
* This returns initially returns null.
* Once the parseSdk() method has been called, this returns a possibly empty but non-null array.
*/
public Package[] getPackages() {
return mPackages;
}
/**
* Clear the internal packages list. After this call, {@link #getPackages()} will return
* null till {@link #parseSdk} is called.
*/
public void clearPackages() {
mPackages = null;
}
/**
* Scan the give SDK to find all the packages already installed at this location.
*
* Store the packages internally. You can use {@link #getPackages()} to retrieve them
* at any time later.
*
* Equivalent to calling {@code parseSdk(..., PARSE_ALL, ...); }
*
* @param osSdkRoot The path to the SDK folder, typically {@code sdkManager.getLocation()}.
* @param sdkManager An existing SDK manager to list current platforms and addons.
* @param monitor A monitor to track progress. Cannot be null.
* @return The packages found. Can be retrieved later using {@link #getPackages()}.
*/
@NonNull
public Package[] parseSdk(
@NonNull String osSdkRoot,
@NonNull SdkManager sdkManager,
@NonNull ITaskMonitor monitor) {
return parseSdk(osSdkRoot, sdkManager, PARSE_ALL, monitor);
}
/**
* Scan the give SDK to find all the packages already installed at this location.
*
* Store the packages internally. You can use {@link #getPackages()} to retrieve them
* at any time later.
*
* @param osSdkRoot The path to the SDK folder, typically {@code sdkManager.getLocation()}.
* @param sdkManager An existing SDK manager to list current platforms and addons.
* @param parseFilter Either {@link #PARSE_ALL} or an ORed combination of the other
* {@code PARSE_} constants to indicate what should be parsed.
* @param monitor A monitor to track progress. Cannot be null.
* @return The packages found. Can be retrieved later using {@link #getPackages()}.
*/
@NonNull
public Package[] parseSdk(
@NonNull String osSdkRoot,
@NonNull SdkManager sdkManager,
int parseFilter,
@NonNull ITaskMonitor monitor) {
ArrayList packages = new ArrayList();
HashSet visited = new HashSet();
monitor.setProgressMax(11);
File dir = null;
Package pkg = null;
if ((parseFilter & PARSE_DOCS) != 0) {
dir = new File(osSdkRoot, SdkConstants.FD_DOCS);
pkg = scanDoc(dir, monitor);
if (pkg != null) {
packages.add(pkg);
visited.add(dir);
}
}
monitor.incProgress(1);
if ((parseFilter & PARSE_TOOLS) != 0) {
dir = new File(osSdkRoot, SdkConstants.FD_TOOLS);
pkg = scanTools(dir, monitor);
if (pkg != null) {
packages.add(pkg);
visited.add(dir);
}
}
monitor.incProgress(1);
if ((parseFilter & PARSE_PLATFORM_TOOLS) != 0) {
dir = new File(osSdkRoot, SdkConstants.FD_PLATFORM_TOOLS);
pkg = scanPlatformTools(dir, monitor);
if (pkg != null) {
packages.add(pkg);
visited.add(dir);
}
}
monitor.incProgress(1);
if ((parseFilter & PARSE_BUILD_TOOLS) != 0) {
scanBuildTools(sdkManager, visited, packages, monitor);
}
monitor.incProgress(1);
// for platforms, add-ons and samples, rely on the SdkManager parser
if ((parseFilter & (PARSE_ADDONS | PARSE_PLATFORMS)) != 0) {
File samplesRoot = new File(osSdkRoot, SdkConstants.FD_SAMPLES);
for(IAndroidTarget target : sdkManager.getTargets()) {
Properties props = parseProperties(new File(target.getLocation(),
SdkConstants.FN_SOURCE_PROP));
try {
pkg = null;
if (target.isPlatform() && (parseFilter & PARSE_PLATFORMS) != 0) {
pkg = PlatformPackage.create(target, props);
if (samplesRoot.isDirectory()) {
// Get the samples dir for a platform if it is located in the new
// root /samples dir. We purposely ignore "old" samples that are
// located under the platform dir.
File samplesDir = new File(target.getPath(IAndroidTarget.SAMPLES));
if (samplesDir.exists() &&
samplesDir.getParentFile().equals(samplesRoot)) {
Properties samplesProps = parseProperties(
new File(samplesDir, SdkConstants.FN_SOURCE_PROP));
if (samplesProps != null) {
Package pkg2 = SamplePackage.create(target, samplesProps);
packages.add(pkg2);
}
visited.add(samplesDir);
}
}
} else if ((parseFilter & PARSE_ADDONS) != 0) {
pkg = AddonPackage.create(target, props);
}
if (pkg != null) {
for (ISystemImage systemImage : target.getSystemImages()) {
if (systemImage.getLocationType() == LocationType.IN_SYSTEM_IMAGE) {
File siDir = systemImage.getLocation();
if (siDir.isDirectory()) {
Properties siProps = parseProperties(
new File(siDir, SdkConstants.FN_SOURCE_PROP));
Package pkg2 = new SystemImagePackage(
target.getVersion(),
0 /*rev*/, // use the one from siProps
systemImage.getAbiType(),
siProps,
siDir.getAbsolutePath());
packages.add(pkg2);
visited.add(siDir);
}
}
}
}
} catch (Exception e) {
monitor.error(e, null);
}
if (pkg != null) {
packages.add(pkg);
visited.add(new File(target.getLocation()));
}
}
}
monitor.incProgress(1);
if ((parseFilter & PARSE_PLATFORMS) != 0) {
scanMissingSystemImages(sdkManager, visited, packages, monitor);
}
monitor.incProgress(1);
if ((parseFilter & PARSE_ADDONS) != 0) {
scanMissingAddons(sdkManager, visited, packages, monitor);
}
monitor.incProgress(1);
if ((parseFilter & PARSE_SAMPLES) != 0) {
scanMissingSamples(sdkManager, visited, packages, monitor);
}
monitor.incProgress(1);
if ((parseFilter & PARSE_EXTRAS) != 0) {
scanExtras(sdkManager, visited, packages, monitor);
}
monitor.incProgress(1);
if ((parseFilter & PARSE_EXTRAS) != 0) {
scanExtrasDirectory(osSdkRoot, visited, packages, monitor);
}
monitor.incProgress(1);
if ((parseFilter & PARSE_SOURCES) != 0) {
scanSources(sdkManager, visited, packages, monitor);
}
monitor.incProgress(1);
Collections.sort(packages);
mPackages = packages.toArray(new Package[packages.size()]);
return mPackages;
}
/**
* Find any directory in the /extras/vendors/path folders for extra packages.
* This isn't a recursive search.
*/
private void scanExtras(SdkManager sdkManager,
HashSet visited,
ArrayList packages,
ILogger log) {
File root = new File(sdkManager.getLocation(), SdkConstants.FD_EXTRAS);
for (File vendor : listFilesNonNull(root)) {
if (vendor.isDirectory()) {
scanExtrasDirectory(vendor.getAbsolutePath(), visited, packages, log);
}
}
}
/**
* Find any other directory in the given "root" directory that hasn't been visited yet
* and assume they contain extra packages. This is not a recursive search.
*/
private void scanExtrasDirectory(String extrasRoot,
HashSet visited,
ArrayList packages,
ILogger log) {
File root = new File(extrasRoot);
for (File dir : listFilesNonNull(root)) {
if (dir.isDirectory() && !visited.contains(dir)) {
Properties props = parseProperties(new File(dir, SdkConstants.FN_SOURCE_PROP));
if (props != null) {
try {
Package pkg = ExtraPackage.create(
null, //source
props, //properties
null, //vendor
dir.getName(), //path
0, //revision
null, //license
null, //description
null, //descUrl
dir.getPath() //archiveOsPath
);
packages.add(pkg);
visited.add(dir);
} catch (Exception e) {
log.error(e, null);
}
}
}
}
}
/**
* Find any other sub-directories under the /samples root that hasn't been visited yet
* and assume they contain sample packages. This is not a recursive search.
*
* The use case is to find samples dirs under /samples when their target isn't loaded.
*/
private void scanMissingSamples(SdkManager sdkManager,
HashSet visited,
ArrayList packages,
ILogger log) {
File root = new File(sdkManager.getLocation());
root = new File(root, SdkConstants.FD_SAMPLES);
for (File dir : listFilesNonNull(root)) {
if (dir.isDirectory() && !visited.contains(dir)) {
Properties props = parseProperties(new File(dir, SdkConstants.FN_SOURCE_PROP));
if (props != null) {
try {
Package pkg = SamplePackage.create(dir.getAbsolutePath(), props);
packages.add(pkg);
visited.add(dir);
} catch (Exception e) {
log.error(e, null);
}
}
}
}
}
/**
* The sdk manager only lists valid addons. However here we also want to find "broken"
* addons, i.e. addons that failed to load for some reason.
*
* Find any other sub-directories under the /add-ons root that hasn't been visited yet
* and assume they contain broken addons.
*/
private void scanMissingAddons(SdkManager sdkManager,
HashSet visited,
ArrayList packages,
ILogger log) {
File addons = new File(new File(sdkManager.getLocation()), SdkConstants.FD_ADDONS);
for (File dir : listFilesNonNull(addons)) {
if (dir.isDirectory() && !visited.contains(dir)) {
Pair
© 2015 - 2025 Weber Informatics LLC | Privacy Policy