private int getApiVersion(Element usesSdk, String attribute, int defaultApiLevel) { String valueString = null; if (usesSdk.hasAttributeNS(NS_RESOURCES, attribute)) { valueString = usesSdk.getAttributeNS(NS_RESOURCES, attribute); } if (valueString != null) { int apiLevel = -1; try { apiLevel = Integer.valueOf(valueString); } catch (NumberFormatException e) { // Handle codename if (Sdk.getCurrent() != null) { IAndroidTarget target = Sdk.getCurrent().getTargetFromHashString("android-" + valueString); // $NON-NLS-1$ if (target != null) { // codename future API level is current api + 1 apiLevel = target.getVersion().getApiLevel() + 1; } } if (usesSdk.getTagName().equals(ATTRIBUTE_MIN_SDK_VERSION)) { mMinSdkName = valueString; } } return apiLevel; } return defaultApiLevel; }
// The default attributes must be determined dynamically since whether // we use match_parent or fill_parent depends on the API level of the // project @Override String getDefaultAttrs(IProject project, String root) { Sdk currentSdk = Sdk.getCurrent(); String fill = VALUE_FILL_PARENT; if (currentSdk != null) { IAndroidTarget target = currentSdk.getTarget(project); // fill_parent was renamed match_parent in API level 8 if (target != null && target.getVersion().getApiLevel() >= 8) { fill = VALUE_MATCH_PARENT; } } // Only set "vertical" orientation of LinearLayouts by default; // for GridLayouts for example we want to rely on the real default // of the layout String size = String.format( "android:layout_width=\"%1$s\"\n" //$NON-NLS-1$ + "android:layout_height=\"%2$s\"", //$NON-NLS-1$ fill, fill); if (LINEAR_LAYOUT.equals(root)) { return "android:orientation=\"vertical\"\n" + size; // $NON-NLS-1$ } else { return size; } }
/** * Validates the fields, displays errors and warnings. Enables the finish button if there are no * errors. */ private void validatePage() { String error = null; String warning = null; // -- validate type TypeInfo type = mValues.type; if (error == null) { if (type == null) { error = "One of the types must be selected (e.g. layout, values, etc.)"; } } // -- validate project if (mValues.project == null) { error = "Please select an Android project."; } // -- validate type API level if (error == null) { IAndroidTarget target = Sdk.getCurrent().getTarget(mValues.project); int currentApiLevel = 1; if (target != null) { currentApiLevel = target.getVersion().getApiLevel(); } assert type != null; if (type.getTargetApiLevel() > currentApiLevel) { error = "The API level of the selected type (e.g. AppWidget, etc.) is not " + "compatible with the API level of the project."; } } // -- validate filename if (error == null) { String fileName = mValues.getFileName(); assert type != null; ResourceFolderType folderType = type.getResFolderType(); error = ResourceNameValidator.create(true, folderType).isValid(fileName); } // -- validate destination file doesn't exist if (error == null) { IFile file = mValues.getDestinationFile(); if (file != null && file.exists()) { warning = "The destination file already exists"; } } // -- update UI & enable finish if there's no error setPageComplete(error == null); if (error != null) { setMessage(error, IMessageProvider.ERROR); } else if (warning != null) { setMessage(warning, IMessageProvider.WARNING); } else { setErrorMessage(null); setMessage(null); } }
/** Displays the list of available Targets (Platforms and Add-ons) */ private void displayTargetList() { mSdkLog.printf("Available Android targets:\n"); int index = 1; for (IAndroidTarget target : mSdkManager.getTargets()) { mSdkLog.printf("id: %1$d or \"%2$s\"\n", index, target.hashString()); mSdkLog.printf(" Name: %s\n", target.getName()); if (target.isPlatform()) { mSdkLog.printf(" Type: Platform\n"); mSdkLog.printf(" API level: %s\n", target.getVersion().getApiString()); mSdkLog.printf(" Revision: %d\n", target.getRevision()); } else { mSdkLog.printf(" Type: Add-On\n"); mSdkLog.printf(" Vendor: %s\n", target.getVendor()); mSdkLog.printf(" Revision: %d\n", target.getRevision()); if (target.getDescription() != null) { mSdkLog.printf(" Description: %s\n", target.getDescription()); } mSdkLog.printf( " Based on Android %s (API level %s)\n", target.getVersionName(), target.getVersion().getApiString()); // display the optional libraries. IOptionalLibrary[] libraries = target.getOptionalLibraries(); if (libraries != null) { mSdkLog.printf(" Libraries:\n"); for (IOptionalLibrary library : libraries) { mSdkLog.printf(" * %1$s (%2$s)\n", library.getName(), library.getJarName()); mSdkLog.printf(String.format(" %1$s\n", library.getDescription())); } } } // get the target skins displaySkinList(target, " Skins: "); if (target.getUsbVendorId() != IAndroidTarget.NO_USB_ID) { mSdkLog.printf( " Adds USB support for devices (Vendor: 0x%04X)\n", target.getUsbVendorId()); } index++; } }
public void createTestProject() { IAndroidTarget target = null; IAndroidTarget[] targets = getSdk().getTargets(); for (IAndroidTarget t : targets) { if (t.getVersion().getApiLevel() >= TARGET_API_LEVEL) { target = t; break; } } assertNotNull(target); }
/** Returns the string used to represent this qualifier in the folder name. */ @Override public String getFolderSegment(IAndroidTarget target) { if (mValue != null) { if (target == null) { // Default behavior (when target==null) is qualifier is supported return mValue.getValue(); } AndroidVersion version = target.getVersion(); if (version.getApiLevel() >= 4 || (version.getApiLevel() == 3 && "Donut".equals(version.getCodename()))) { return mValue.getValue(); } } return ""; //$NON-NLS-1$ }
/** * Add the available types in the type combobox, based on whether they are available for the * current SDK. * * <p>A type is available either if: - if mProject is null, API level 1 is considered valid - if * mProject is !null, the project->target->API must be >= to the type's API level. */ private void updateAvailableTypes() { IProject project = mValues.project; IAndroidTarget target = project != null ? Sdk.getCurrent().getTarget(project) : null; int currentApiLevel = 1; if (target != null) { currentApiLevel = target.getVersion().getApiLevel(); } List<String> items = new ArrayList<String>(sTypes.length); List<TypeInfo> types = new ArrayList<TypeInfo>(sTypes.length); for (int i = 0, n = sTypes.length; i < n; i++) { TypeInfo type = sTypes[i]; if (type.getTargetApiLevel() <= currentApiLevel) { items.add(type.getUiName()); types.add(type); } } mTypeCombo.setItems(items.toArray(new String[items.size()])); mTypeCombo.setData(types.toArray(new TypeInfo[types.size()])); }
/** * Computes the minimum SDK and target SDK versions for the project * * @param project the project to look up the versions for * @return a pair of (minimum SDK, target SDK) versions, never null */ @NonNull public static Pair<Integer, Integer> computeSdkVersions(IProject project) { int mMinSdkVersion = 1; int mTargetSdkVersion = 1; IAbstractFile manifestFile = AndroidManifest.getManifest(new IFolderWrapper(project)); if (manifestFile != null) { try { Object value = AndroidManifest.getMinSdkVersion(manifestFile); mMinSdkVersion = 1; // Default case if missing if (value instanceof Integer) { mMinSdkVersion = ((Integer) value).intValue(); } else if (value instanceof String) { // handle codename, only if we can resolve it. if (Sdk.getCurrent() != null) { IAndroidTarget target = Sdk.getCurrent().getTargetFromHashString("android-" + value); // $NON-NLS-1$ if (target != null) { // codename future API level is current api + 1 mMinSdkVersion = target.getVersion().getApiLevel() + 1; } } } Integer i = AndroidManifest.getTargetSdkVersion(manifestFile); if (i == null) { mTargetSdkVersion = mMinSdkVersion; } else { mTargetSdkVersion = i.intValue(); } } catch (XPathExpressionException e) { // do nothing we'll use 1 below. } catch (StreamException e) { // do nothing we'll use 1 below. } } return Pair.of(mMinSdkVersion, mTargetSdkVersion); }
/** * Returns the default theme for this project, by looking at the manifest default theme * registration, target SDK, rendering target, etc. * * @param renderingTarget the rendering target use to render the theme, or null * @param screenSize the screen size to obtain a default theme for, or null if unknown * @return the theme to use for this project, never null */ @NonNull public String getDefaultTheme(IAndroidTarget renderingTarget, ScreenSize screenSize) { sync(); if (mManifestTheme != null) { return mManifestTheme; } int renderingTargetSdk = mTargetSdk; if (renderingTarget != null) { renderingTargetSdk = renderingTarget.getVersion().getApiLevel(); } int apiLevel = Math.min(mTargetSdk, renderingTargetSdk); // For now this theme works only on XLARGE screens. When it works for all sizes, // add that new apiLevel to this check. if (apiLevel >= 11 && screenSize == ScreenSize.XLARGE || apiLevel >= 14) { return PREFIX_ANDROID_STYLE + "Theme.Holo"; // $NON-NLS-1$ } else { return PREFIX_ANDROID_STYLE + "Theme"; // $NON-NLS-1$ } }
protected IProject createProject(String name) { IAndroidTarget target = null; IAndroidTarget[] targets = getSdk().getTargets(); for (IAndroidTarget t : targets) { if (!t.isPlatform()) { continue; } if (t.getVersion().getApiLevel() >= TARGET_API_LEVEL) { target = t; break; } } assertNotNull(target); IRunnableContext context = new IRunnableContext() { @Override public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException { runnable.run(new NullProgressMonitor()); } }; NewProjectWizardState state = new NewProjectWizardState(Mode.ANY); state.projectName = name; state.target = target; state.packageName = TEST_PROJECT_PACKAGE; state.activityName = name; state.applicationName = name; state.createActivity = false; state.useDefaultLocation = true; if (getMinSdk() != -1) { state.minSdk = Integer.toString(getMinSdk()); } NewProjectCreator creator = new NewProjectCreator(state, context); creator.createAndroidProjects(); return validateProjectExists(name); }
/** Computes the changes this wizard will make */ @NonNull List<Change> computeChanges() { if (project == null) { return Collections.emptyList(); } ManifestInfo manifest = ManifestInfo.get(project); parameters.put(ATTR_PACKAGE_NAME, manifest.getPackage()); parameters.put(ATTR_MIN_API, manifest.getMinSdkVersion()); parameters.put(ATTR_MIN_API_LEVEL, manifest.getMinSdkName()); parameters.put(ATTR_TARGET_API, manifest.getTargetSdkVersion()); IAndroidTarget target = Sdk.getCurrent().getTarget(project); int buildApi; if (target != null) { buildApi = target.getVersion().getApiLevel(); } else { buildApi = manifest.getTargetSdkVersion(); } parameters.put(NewProjectWizard.ATTR_BUILD_API, buildApi); return getTemplateHandler().render(project, parameters); }
/** * Displays the list of available AVDs for the given AvdManager. * * @param avdManager */ public void displayAvdList(AvdManager avdManager) { mSdkLog.printf("Available Android Virtual Devices:\n"); AvdInfo[] avds = avdManager.getValidAvds(); for (int index = 0; index < avds.length; index++) { AvdInfo info = avds[index]; if (index > 0) { mSdkLog.printf("---------\n"); } mSdkLog.printf(" Name: %s\n", info.getName()); mSdkLog.printf(" Path: %s\n", info.getPath()); // get the target of the AVD IAndroidTarget target = info.getTarget(); if (target.isPlatform()) { mSdkLog.printf( " Target: %s (API level %s)\n", target.getName(), target.getVersion().getApiString()); } else { mSdkLog.printf(" Target: %s (%s)\n", target.getName(), target.getVendor()); mSdkLog.printf( " Based on Android %s (API level %s)\n", target.getVersionName(), target.getVersion().getApiString()); } // display some extra values. Map<String, String> properties = info.getProperties(); if (properties != null) { String skin = properties.get(AvdManager.AVD_INI_SKIN_NAME); if (skin != null) { mSdkLog.printf(" Skin: %s\n", skin); } String sdcard = properties.get(AvdManager.AVD_INI_SDCARD_SIZE); if (sdcard == null) { sdcard = properties.get(AvdManager.AVD_INI_SDCARD_PATH); } if (sdcard != null) { mSdkLog.printf(" Sdcard: %s\n", sdcard); } String snapshot = properties.get(AvdManager.AVD_INI_SNAPSHOT_PRESENT); if (snapshot != null) { mSdkLog.printf("Snapshot: %s\n", snapshot); } } } // Are there some unused AVDs? AvdInfo[] badAvds = avdManager.getBrokenAvds(); if (badAvds.length == 0) { return; } mSdkLog.printf("\nThe following Android Virtual Devices could not be loaded:\n"); boolean needSeparator = false; for (AvdInfo info : badAvds) { if (needSeparator) { mSdkLog.printf("---------\n"); } mSdkLog.printf(" Name: %s\n", info.getName() == null ? "--" : info.getName()); mSdkLog.printf(" Path: %s\n", info.getPath() == null ? "--" : info.getPath()); String error = info.getErrorMessage(); mSdkLog.printf(" Error: %s\n", error == null ? "Uknown error" : error); needSeparator = true; } }
@Override public AndroidVersion getVersion() { return myWrapee.getVersion(); }
@Override public boolean setupSdkPaths(@NotNull Sdk sdk, @NotNull SdkModel sdkModel) { final List<String> javaSdks = Lists.newArrayList(); final Sdk[] sdks = sdkModel.getSdks(); for (Sdk jdk : sdks) { if (Jdks.isApplicableJdk(jdk)) { javaSdks.add(jdk.getName()); } } if (javaSdks.isEmpty()) { Messages.showErrorDialog( AndroidBundle.message("no.jdk.for.android.found.error"), "No Java SDK Found"); return false; } MessageBuildingSdkLog log = new MessageBuildingSdkLog(); AndroidSdkData sdkData = getSdkData(sdk); if (sdkData == null) { String errorMessage = !log.getErrorMessage().isEmpty() ? log.getErrorMessage() : AndroidBundle.message("cannot.parse.sdk.error"); Messages.showErrorDialog(errorMessage, "SDK Parsing Error"); return false; } IAndroidTarget[] targets = sdkData.getTargets(); if (targets.length == 0) { if (Messages.showOkCancelDialog( AndroidBundle.message("no.android.targets.error"), CommonBundle.getErrorTitle(), "Open SDK Manager", Messages.CANCEL_BUTTON, Messages.getErrorIcon()) == Messages.OK) { RunAndroidSdkManagerAction.runSpecificSdkManager(null, sdkData.getLocation()); } return false; } String[] targetNames = new String[targets.length]; String newestPlatform = null; AndroidVersion version = null; for (int i = 0; i < targets.length; i++) { IAndroidTarget target = targets[i]; String targetName = getTargetPresentableName(target); targetNames[i] = targetName; if (target.isPlatform() && (version == null || target.getVersion().compareTo(version) > 0)) { newestPlatform = targetName; version = target.getVersion(); } } AndroidNewSdkDialog dialog = new AndroidNewSdkDialog( null, javaSdks, javaSdks.get(0), Arrays.asList(targetNames), newestPlatform != null ? newestPlatform : targetNames[0]); if (!dialog.showAndGet()) { return false; } String name = javaSdks.get(dialog.getSelectedJavaSdkIndex()); Sdk jdk = sdkModel.findSdk(name); IAndroidTarget target = targets[dialog.getSelectedTargetIndex()]; String sdkName = chooseNameForNewLibrary(target); setUpSdk(sdk, sdkName, sdks, target, jdk, true); return true; }
public static final boolean checkTargetNumber(IAndroidTarget target, int targetNumber) { return target.getVersion().getApiLevel() == targetNumber; }
@Override public boolean setupSdkPaths(Sdk sdk, SdkModel sdkModel) { final List<String> javaSdks = new ArrayList<String>(); final Sdk[] sdks = sdkModel.getSdks(); for (Sdk jdk : sdks) { if (AndroidSdkUtils.isApplicableJdk(jdk)) { javaSdks.add(jdk.getName()); } } if (javaSdks.isEmpty()) { Messages.showErrorDialog( AndroidBundle.message("no.jdk.for.android.found.error"), "No Java SDK Found"); return false; } int choice = Messages.showChooseDialog( "Please select Java SDK", "Select Internal Java Platform", ArrayUtil.toStringArray(javaSdks), javaSdks.get(0), Messages.getQuestionIcon()); if (choice == -1) { return false; } final String name = javaSdks.get(choice); final Sdk jdk = sdkModel.findSdk(name); MessageBuildingSdkLog log = new MessageBuildingSdkLog(); AndroidSdkData sdkData = AndroidSdkData.parse(sdk.getHomePath(), log); if (sdkData == null) { String errorMessage = log.getErrorMessage().length() > 0 ? log.getErrorMessage() : AndroidBundle.message("cannot.parse.sdk.error"); Messages.showErrorDialog(errorMessage, "SDK Parsing Error"); return false; } IAndroidTarget[] targets = sdkData.getTargets(); if (targets.length == 0) { Messages.showErrorDialog( AndroidBundle.message("no.android.targets.error"), CommonBundle.getErrorTitle()); return false; } String[] targetNames = new String[targets.length]; String newestPlatform = null; AndroidVersion version = null; for (int i = 0; i < targets.length; i++) { IAndroidTarget target = targets[i]; String targetName = AndroidSdkUtils.getTargetPresentableName(target); targetNames[i] = targetName; if (target.isPlatform() && (version == null || target.getVersion().compareTo(version) > 0)) { newestPlatform = targetName; version = target.getVersion(); } } choice = Messages.showChooseDialog( "Select build target", "Create New Android SDK", targetNames, newestPlatform != null ? newestPlatform : targetNames[0], Messages.getQuestionIcon()); if (choice == -1) { return false; } AndroidSdkUtils.setUpSdk(sdk, jdk, sdks, targets[choice], true); return true; }