Search in sources :

Example 81 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidPropertyFilesUpdater method updateTargetProperty.

private static void updateTargetProperty(@NotNull AndroidFacet facet, @NotNull final PropertiesFile propertiesFile, @NotNull List<Runnable> changes) {
    final Project project = facet.getModule().getProject();
    final IAndroidTarget androidTarget = facet.getConfiguration().getAndroidTarget();
    if (androidTarget != null) {
        final String targetPropertyValue = androidTarget.hashString();
        final IProperty property = propertiesFile.findPropertyByKey(AndroidUtils.ANDROID_TARGET_PROPERTY);
        if (property == null) {
            changes.add(new Runnable() {

                @Override
                public void run() {
                    propertiesFile.addProperty(createProperty(project, targetPropertyValue));
                }
            });
        } else {
            if (!Comparing.equal(property.getValue(), targetPropertyValue)) {
                final PsiElement element = property.getPsiElement();
                if (element != null) {
                    changes.add(new Runnable() {

                        @Override
                        public void run() {
                            element.replace(createProperty(project, targetPropertyValue).getPsiElement());
                        }
                    });
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) IProperty(com.intellij.lang.properties.IProperty) IAndroidTarget(com.android.sdklib.IAndroidTarget) PsiElement(com.intellij.psi.PsiElement)

Example 82 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidThemePreviewToolWindowManager method getBestConfiguration.

@Nullable
private static /*if there is no available configuration that would select the passed file*/
Configuration getBestConfiguration(@Nullable PsiFile psiFile) {
    Module module = psiFile != null ? ModuleUtilCore.findModuleForPsiElement(psiFile) : null;
    if (module == null) {
        return null;
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return null;
    }
    VirtualFile virtualFile = psiFile.getVirtualFile();
    ConfigurationManager manager = facet.getConfigurationManager();
    List<VirtualFile> variations = ResourceHelper.getResourceVariations(virtualFile, false);
    if (variations.isEmpty()) {
        return manager.getConfiguration(virtualFile);
    }
    // There is more than one resource folder available so make sure we select a configuration that only matches the current file.
    Collection<FolderConfiguration> incompatible = Collections2.transform(variations, new Function<VirtualFile, FolderConfiguration>() {

        @Nullable
        @Override
        public FolderConfiguration apply(VirtualFile input) {
            assert input != null;
            return ResourceHelper.getFolderConfiguration(input);
        }
    });
    FolderConfiguration selectedFileFolderConfiguration = ResourceHelper.getFolderConfiguration(psiFile);
    if (selectedFileFolderConfiguration == null) {
        // This folder probably has invalid qualifiers or they are in the wrong order
        return null;
    }
    RestrictedConfiguration restrictedConfiguration = RestrictedConfiguration.restrict(selectedFileFolderConfiguration, incompatible);
    if (restrictedConfiguration == null) {
        // Unable to create a configuration that only matches this file
        return null;
    }
    FolderConfiguration restricted = restrictedConfiguration.getAny();
    Configuration newConfiguration = Configuration.create(manager, virtualFile, null, restricted);
    VersionQualifier newVersionQualifier = restricted.getVersionQualifier();
    if (newVersionQualifier != null) {
        IAndroidTarget realTarget = manager.getHighestApiTarget() != null ? manager.getHighestApiTarget() : manager.getTarget();
        assert realTarget != null;
        newConfiguration.setTarget(new CompatibilityRenderTarget(realTarget, newVersionQualifier.getVersion(), null));
    }
    return newConfiguration;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Configuration(com.android.tools.idea.configurations.Configuration) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IAndroidTarget(com.android.sdklib.IAndroidTarget) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) Module(com.intellij.openapi.module.Module) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 83 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class ResolutionUtils method getAttributeDefinition.

@Nullable
public static AttributeDefinition getAttributeDefinition(@NotNull Module module, @Nullable Configuration configuration, @NotNull String name) {
    AttributeDefinitions definitions;
    if (name.startsWith(ANDROID_NS_NAME_PREFIX)) {
        IAndroidTarget target;
        if (configuration == null) {
            AndroidFacet facet = AndroidFacet.getInstance(module);
            assert facet != null;
            // same as getHighestApiTarget();
            target = facet.getConfigurationManager().getDefaultTarget();
        } else {
            target = configuration.getRealTarget();
        }
        assert target != null;
        AndroidTargetData androidTargetData = AndroidTargetData.getTargetData(target, module);
        assert androidTargetData != null;
        definitions = androidTargetData.getAllAttrDefs(module.getProject());
    } else {
        AndroidFacet facet = AndroidFacet.getInstance(module);
        assert facet != null : String.format("Module %s is not an Android module", module.getName());
        definitions = facet.getLocalResourceManager().getAttributeDefinitions();
    }
    if (definitions == null) {
        return null;
    }
    return definitions.getAttrDefByName(getNameFromQualifiedName(name));
}
Also used : AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData) Nullable(org.jetbrains.annotations.Nullable)

Example 84 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class ResourceFolderRepository method bitmapUpdated.

/**
   * Called when a bitmap has been changed/deleted. In that case we need to clear out any caches for that
   * image held by layout lib.
   */
private void bitmapUpdated() {
    ConfigurationManager configurationManager = myFacet.getConfigurationManager(false);
    if (configurationManager != null) {
        IAndroidTarget target = configurationManager.getTarget();
        if (target != null) {
            Module module = myFacet.getModule();
            AndroidTargetData targetData = AndroidTargetData.getTargetData(target, module);
            if (targetData != null) {
                targetData.clearLayoutBitmapCache(module);
            }
        }
    }
}
Also used : IAndroidTarget(com.android.sdklib.IAndroidTarget) Module(com.intellij.openapi.module.Module) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData)

Example 85 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class TemplateUtils method getKnownVersions.

/**
   * Returns a list of known API names
   *
   * @return a list of string API names, starting from 1 and up through the
   * maximum known versions (with no gaps)
   */
@NotNull
public static String[] getKnownVersions() {
    final AndroidSdkData sdkData = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    assert sdkData != null;
    int max = SdkVersionInfo.HIGHEST_KNOWN_STABLE_API;
    IAndroidTarget[] targets = sdkData.getTargets();
    SparseArray<IAndroidTarget> apiTargets = null;
    for (IAndroidTarget target : targets) {
        if (target.isPlatform()) {
            AndroidVersion version = target.getVersion();
            if (!version.isPreview()) {
                int apiLevel = version.getApiLevel();
                max = Math.max(max, apiLevel);
                if (apiLevel > SdkVersionInfo.HIGHEST_KNOWN_API) {
                    if (apiTargets == null) {
                        apiTargets = new SparseArray<IAndroidTarget>();
                    }
                    apiTargets.put(apiLevel, target);
                }
            }
        }
    }
    String[] versions = new String[max];
    for (int api = 1; api <= max; api++) {
        String name = SdkVersionInfo.getAndroidName(api);
        // noinspection ConstantConditions
        if (name == null) {
            if (apiTargets != null) {
                IAndroidTarget target = apiTargets.get(api);
                if (target != null) {
                    name = AndroidSdkUtils.getTargetLabel(target);
                }
            }
            if (name == null) {
                name = String.format("API %1$d", api);
            }
        }
        versions[api - 1] = name;
    }
    return versions;
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidVersion(com.android.sdklib.AndroidVersion) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IAndroidTarget (com.android.sdklib.IAndroidTarget)105 Nullable (org.jetbrains.annotations.Nullable)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Module (com.intellij.openapi.module.Module)17 NotNull (org.jetbrains.annotations.NotNull)16 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)15 Sdk (com.intellij.openapi.projectRoots.Sdk)14 File (java.io.File)13 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)13 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)13 AndroidVersion (com.android.sdklib.AndroidVersion)11 Device (com.android.sdklib.devices.Device)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)9 State (com.android.sdklib.devices.State)8 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)8 AndroidTargetData (org.jetbrains.android.sdk.AndroidTargetData)8 Configuration (com.android.tools.idea.configurations.Configuration)7 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)7 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)7 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)6