diff options
author | Tor Norbye <tnorbye@google.com> | 2012-07-27 16:43:23 -0700 |
---|---|---|
committer | android code review <noreply-gerritcodereview@google.com> | 2012-07-27 16:43:24 -0700 |
commit | 61c223a8db885680752fa3504da3bc078bce6798 (patch) | |
tree | db4a4849601605b5c4b425fbf5d8607954d33544 | |
parent | 4f5400bf86b738c51137728f708fb7cc77f88d64 (diff) | |
parent | c58247bd46d86afe244d2d9dd9160da10e0db648 (diff) | |
download | sdk-61c223a8db885680752fa3504da3bc078bce6798.zip sdk-61c223a8db885680752fa3504da3bc078bce6798.tar.gz sdk-61c223a8db885680752fa3504da3bc078bce6798.tar.bz2 |
Merge "Template support for minimum build target"
8 files changed, 107 insertions, 23 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/ActivityPage.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/ActivityPage.java index a74ffa8..4a7c2ca 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/ActivityPage.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/ActivityPage.java @@ -239,7 +239,8 @@ class ActivityPage extends WizardPage implements SelectionListener { "Select an activity type"); } else { TemplateHandler templateHandler = mValues.activityValues.getTemplateHandler(); - status = templateHandler.validateTemplate(mValues.minSdkLevel); + status = templateHandler.validateTemplate(mValues.minSdkLevel, + mValues.getBuildApi()); } } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectPage.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectPage.java index 69d3d1d..9377dc0 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectPage.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectPage.java @@ -698,7 +698,8 @@ public class NewProjectPage extends WizardPage // Validation private void validatePage() { - IStatus status = mValues.template.validateTemplate(mValues.minSdkLevel); + IStatus status = mValues.template.validateTemplate(mValues.minSdkLevel, + mValues.getBuildApi()); if (status != null && !status.isOK()) { updateDecorator(mApplicationDec, null, true); updateDecorator(mPackageDec, null, true); diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectWizard.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectWizard.java index aa7e134..824c542 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectWizard.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectWizard.java @@ -68,6 +68,7 @@ public class NewProjectWizard extends TemplateWizard { private static final String ATTR_COPY_ICONS = "copyIcons"; //$NON-NLS-1$ static final String ATTR_TARGET_API = "targetApi"; //$NON-NLS-1$ static final String ATTR_MIN_API = "minApi"; //$NON-NLS-1$ + static final String ATTR_MIN_BUILD_API = "minBuildApi"; //$NON-NLS-1$ static final String ATTR_BUILD_API = "buildApi"; //$NON-NLS-1$ static final String ATTR_REVISION = "revision"; //$NON-NLS-1$ static final String ATTR_MIN_API_LEVEL = "minApiLevel"; //$NON-NLS-1$ @@ -155,7 +156,7 @@ public class NewProjectWizard extends TemplateWizard { mTemplatePage = new NewTemplatePage(activityValues, false); addPage(mTemplatePage); } - mTemplatePage.setCustomMinSdk(mValues.minSdkLevel); + mTemplatePage.setCustomMinSdk(mValues.minSdkLevel, mValues.getBuildApi()); return mTemplatePage; } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectWizardState.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectWizardState.java index 7e8878f..5c219b7 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectWizardState.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewProjectWizardState.java @@ -95,4 +95,13 @@ public class NewProjectWizardState { /** Folder where the project should be created. */ public String projectLocation; + + /** + * Returns the build target API level + * + * @return the build target API level + */ + public int getBuildApi() { + return target != null ? target.getVersion().getApiLevel() : minSdkLevel; + } } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplatePage.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplatePage.java index aa35bda..498d42a 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplatePage.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplatePage.java @@ -16,6 +16,7 @@ package com.android.ide.eclipse.adt.internal.wizards.templates; import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_MIN_API; +import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_MIN_BUILD_API; import static com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.ATTR_DEFAULT; import static com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.ATTR_ID; import static com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.ATTR_NAME; @@ -102,6 +103,7 @@ public class NewTemplatePage extends WizardPage private final NewTemplateWizardState mValues; private final boolean mChooseProject; private int mCustomMinSdk = -1; + private int mCustomBuildApi = -1; private boolean mIgnore; private boolean mShown; private Control mFirst; @@ -131,13 +133,16 @@ public class NewTemplatePage extends WizardPage } /** - * @param minSdk a minimum SDK to use, provided chooseProject is - * false. If it is true, then the minimum SDK used for validation - * will be the one of the project + * @param minSdk a minimum SDK to use, provided chooseProject is false. If + * it is true, then the minimum SDK used for validation will be + * the one of the project + * @param buildApi the build API to use */ - void setCustomMinSdk(int minSdk) { + void setCustomMinSdk(int minSdk, int buildApi) { assert !mChooseProject; + //assert buildApi >= minSdk; mCustomMinSdk = minSdk; + mCustomBuildApi = buildApi; } @Override @@ -353,6 +358,7 @@ public class NewTemplatePage extends WizardPage int selected = 0; List<String> ids = Lists.newArrayList(); List<Integer> minSdks = Lists.newArrayList(); + List<Integer> minBuildApis = Lists.newArrayList(); List<String> labels = Lists.newArrayList(); for (int i = 0, n = options.size(); i < n; i++) { Element option = options.get(i); @@ -380,7 +386,20 @@ public class NewTemplatePage extends WizardPage minSdk = 1; } } + String minBuildApiString = option.getAttribute(ATTR_MIN_BUILD_API); + int minBuildApi = 1; + if (minBuildApiString != null && !minBuildApiString.isEmpty()) { + try { + minBuildApi = Integer.parseInt(minBuildApiString); + } catch (NumberFormatException nufe) { + // Templates aren't allowed to contain codenames, should + // always be an integer + AdtPlugin.log(nufe, null); + minBuildApi = 1; + } + } minSdks.add(minSdk); + minBuildApis.add(minBuildApi); ids.add(optionId); labels.add(optionLabel); } @@ -388,6 +407,8 @@ public class NewTemplatePage extends WizardPage parameter.control = combo; combo.setData(ATTR_ID, ids.toArray(new String[ids.size()])); combo.setData(ATTR_MIN_API, minSdks.toArray(new Integer[minSdks.size()])); + combo.setData(ATTR_MIN_BUILD_API, minBuildApis.toArray( + new Integer[minBuildApis.size()])); assert labels.size() > 0; combo.setItems(labels.toArray(new String[labels.size()])); combo.select(selected); @@ -534,9 +555,9 @@ public class NewTemplatePage extends WizardPage // ---- Validation ---- private void validatePage() { - int currentMinSdk = getMinSdk(); - int minSdk = currentMinSdk; - IStatus status = mValues.getTemplateHandler().validateTemplate(minSdk); + int minSdk = getMinSdk(); + int buildApi = getBuildApi(); + IStatus status = mValues.getTemplateHandler().validateTemplate(minSdk, buildApi); if (status == null || status.isOK()) { if (mChooseProject && mValues.project == null) { @@ -569,14 +590,29 @@ public class NewTemplatePage extends WizardPage Combo combo = (Combo) parameter.control; Integer[] optionIds = (Integer[]) combo.getData(ATTR_MIN_API); int index = combo.getSelectionIndex(); + + // Check minSdk if (index != -1 && index < optionIds.length) { Integer requiredMinSdk = optionIds[index]; - if (requiredMinSdk > currentMinSdk) { + if (requiredMinSdk > minSdk) { status = new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, String.format( "This template requires a minimum SDK version of at " + "least %1$d, and the current min version is %2$d", - requiredMinSdk, currentMinSdk)); + requiredMinSdk, minSdk)); + } + } + + // Check minimum build target + optionIds = (Integer[]) combo.getData(ATTR_MIN_BUILD_API); + if (index != -1 && index < optionIds.length) { + Integer requiredBuildApi = optionIds[index]; + if (requiredBuildApi > buildApi) { + status = new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, + String.format( + "This template requires a build target API version of at " + + "least %1$d, and the current min version is %2$d", + requiredBuildApi, buildApi)); } } } @@ -598,6 +634,10 @@ public class NewTemplatePage extends WizardPage return mChooseProject ? mValues.getMinSdk() : mCustomMinSdk; } + private int getBuildApi() { + return mChooseProject ? mValues.getBuildApi() : mCustomBuildApi; + } + private void updateDecorator(ControlDecoration decorator, IStatus status, String help) { if (help != null && !help.isEmpty()) { decorator.setDescriptionText(status != null ? status.getMessage() : help); diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplateWizardState.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplateWizardState.java index 02bbb10..9986810 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplateWizardState.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplateWizardState.java @@ -24,8 +24,6 @@ import static com.android.ide.eclipse.adt.internal.wizards.templates.NewTemplate import com.android.annotations.NonNull; import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo; -import com.android.ide.eclipse.adt.internal.sdk.Sdk; -import com.android.sdklib.IAndroidTarget; import org.eclipse.core.resources.IProject; import org.eclipse.ltk.core.refactoring.Change; @@ -65,6 +63,10 @@ public class NewTemplateWizardState { /** The minimum API level to use for this template */ public int minSdkLevel; + /** The build API level to use for this template */ +// TODO: Populate + public int buildApiLevel; + /** Location of the template being created */ private File mTemplateLocation; @@ -112,6 +114,15 @@ public class NewTemplateWizardState { return manifest.getMinSdkVersion(); } + /** Returns the min SDK version to use */ + int getBuildApi() { + if (project == null) { + return -1; + } + ManifestInfo manifest = ManifestInfo.get(project); + return manifest.getMinSdkVersion(); + } + /** Computes the changes this wizard will make */ @NonNull List<Change> computeChanges() { @@ -124,14 +135,7 @@ public class NewTemplateWizardState { 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); + parameters.put(NewProjectWizard.ATTR_BUILD_API, getBuildApi()); return getTemplateHandler().render(project, parameters); } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateHandler.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateHandler.java index 43ade9c..bdbe50d 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateHandler.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateHandler.java @@ -970,12 +970,13 @@ class TemplateHandler { /** * Validates this template to make sure it's supported * @param currentMinSdk the minimum SDK in the project, or -1 or 0 if unknown (e.g. codename) + * @param buildApi the build API, or -1 or 0 if unknown (e.g. codename) * * @return a status object with the error, or null if there is no problem */ @SuppressWarnings("cast") // In Eclipse 3.6.2 cast below is needed @Nullable - public IStatus validateTemplate(int currentMinSdk) { + public IStatus validateTemplate(int currentMinSdk, int buildApi) { TemplateMetadata template = getTemplate(); if (template == null) { return null; @@ -996,6 +997,13 @@ class TemplateHandler { "least %1$d, and the current min version is %2$d", templateMinSdk, currentMinSdk)); } + int templateMinBuildApi = template.getMinBuildApi(); + if (templateMinBuildApi > buildApi && buildApi >= 1) { + return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, + String.format("This template requires a build target API version of at " + + "least %1$d, and the current version is %2$d", + templateMinBuildApi, buildApi)); + } return null; } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateMetadata.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateMetadata.java index e4b117f..2e78093 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateMetadata.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateMetadata.java @@ -16,6 +16,7 @@ package com.android.ide.eclipse.adt.internal.wizards.templates; import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_MIN_API; +import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_MIN_BUILD_API; import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_REVISION; import static com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.ATTR_DESCRIPTION; import static com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.ATTR_FORMAT; @@ -50,6 +51,7 @@ class TemplateMetadata { private final Map<String, Parameter> mParameterMap; private List<Pair<String, Integer>> mDependencies; private Integer mMinApi; + private Integer mMinBuildApi; private Integer mRevision; TemplateMetadata(@NonNull Document document) { @@ -121,6 +123,24 @@ class TemplateMetadata { return mMinApi.intValue(); } + int getMinBuildApi() { + if (mMinBuildApi == null) { + mMinBuildApi = 1; + String api = mDocument.getDocumentElement().getAttribute(ATTR_MIN_BUILD_API); + if (api != null && !api.isEmpty()) { + try { + mMinBuildApi = Integer.parseInt(api); + } catch (NumberFormatException nufe) { + // Templates aren't allowed to contain codenames, should always be an integer + AdtPlugin.log(nufe, null); + mMinBuildApi = 1; + } + } + } + + return mMinBuildApi.intValue(); + } + public int getRevision() { if (mRevision == null) { mRevision = 1; |