Search in sources :

Example 1 with AndroidModel

use of com.android.tools.idea.model.AndroidModel in project android by JetBrains.

the class AddAndroidActivityPath method getDirectories.

private Map<String, Object> getDirectories() {
    Map<String, Object> templateParameters = Maps.newHashMap();
    Module module = getModule();
    assert module != null;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    assert facet != null;
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform != null) {
        templateParameters.put(ATTR_BUILD_API, platform.getTarget().getVersion().getFeatureLevel());
        templateParameters.put(ATTR_BUILD_API_STRING, getBuildApiString(platform.getTarget().getVersion()));
    }
    // Read minSdkVersion and package from manifest and/or build.gradle files
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(facet);
    AndroidModel androidModel = facet.getAndroidModel();
    SourceProvider sourceProvider1 = myState.get(KEY_SOURCE_PROVIDER);
    if (sourceProvider1 != null && androidModel != null) {
        String packageName = myState.get(KEY_PACKAGE_NAME);
        assert packageName != null;
        templateParameters.putAll(selectSourceProvider(sourceProvider1, androidModel, module, packageName));
    }
    AndroidVersion minSdkVersion = moduleInfo.getMinSdkVersion();
    String minSdkName = minSdkVersion.getApiString();
    templateParameters.put(ATTR_MIN_API, minSdkName);
    templateParameters.put(ATTR_TARGET_API, moduleInfo.getTargetSdkVersion().getApiLevel());
    templateParameters.put(ATTR_MIN_API_LEVEL, minSdkVersion.getFeatureLevel());
    templateParameters.put(ATTR_IS_LIBRARY_MODULE, facet.isLibraryProject());
    try {
        templateParameters.put(ATTR_DEBUG_KEYSTORE_SHA1, KeystoreUtils.sha1(getDebugKeystore(facet)));
    } catch (Exception e) {
        LOG.info("Could not compute SHA1 hash of debug keystore.", e);
        templateParameters.put(ATTR_DEBUG_KEYSTORE_SHA1, "");
    }
    @SuppressWarnings("deprecation") String projectLocation = NewModuleWizardState.ATTR_PROJECT_LOCATION;
    Project project = getProject();
    assert project != null;
    templateParameters.put(projectLocation, project.getBasePath());
    // We're really interested in the directory name on disk, not the module name. These will be different if you give a module the same
    // name as its containing project.
    String moduleName = new File(module.getModuleFilePath()).getParentFile().getName();
    templateParameters.put(FormFactorUtils.ATTR_MODULE_NAME, moduleName);
    return templateParameters;
}
Also used : IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) SourceProvider(com.android.builder.model.SourceProvider) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) AndroidVersion(com.android.sdklib.AndroidVersion) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) AndroidModel(com.android.tools.idea.model.AndroidModel) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 2 with AndroidModel

use of com.android.tools.idea.model.AndroidModel in project android by JetBrains.

the class AndroidXmlSpellcheckingStrategy method inEnglish.

/**
   * @return {@code true} if the given element is in an XML file that is in an English resource. Manifest files are considered to be in
   * English, as are resources in base folders (unless a locale is explicitly defined on the root element).
   */
private static boolean inEnglish(PsiElement element) {
    XmlFile file = PsiTreeUtil.getParentOfType(element, XmlFile.class);
    if (file != null) {
        String name = file.getName();
        if (name.equals(ANDROID_MANIFEST_XML)) {
            return true;
        } else if (name.equals("generated.xml")) {
            // Android Studio Workaround for issue https://code.google.com/p/android/issues/detail?id=76715
            // If this a generated file like this:
            //   ${project}/${module}/build/generated/res/generated/{test?}/${flavors}/${build-type}/values/generated.xml
            // ? If so, skip it.
            AndroidFacet facet = AndroidFacet.getInstance(file);
            VirtualFile virtualFile = file.getVirtualFile();
            if (facet != null && facet.requiresAndroidModel() && virtualFile != null) {
                AndroidModel androidModel = facet.getAndroidModel();
                if (androidModel != null && androidModel.isGenerated(virtualFile)) {
                    return false;
                }
            }
        } else if (isLintConfigElement(element)) {
            // lint config file: should not be spell checked
            return false;
        }
        PsiDirectory dir = file.getParent();
        if (dir != null) {
            String locale = LintUtils.getLocaleAndRegion(dir.getName());
            if (locale == null) {
                locale = getToolsLocale(file);
            }
            return locale == null || locale.startsWith("en") || locale.equals("b+en") || locale.startsWith("b+en+");
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) AndroidModel(com.android.tools.idea.model.AndroidModel) PsiDirectory(com.intellij.psi.PsiDirectory) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 3 with AndroidModel

use of com.android.tools.idea.model.AndroidModel in project android by JetBrains.

the class ModuleClassLoader method getExternalJars.

@Override
protected List<URL> getExternalJars() {
    final Module module = myModuleReference.get();
    if (module == null) {
        return Collections.emptyList();
    }
    final List<URL> result = new ArrayList<>();
    if (ThemeEditorProvider.THEME_EDITOR_ENABLE) {
        URL customWidgetsUrl = ThemeEditorUtils.getCustomWidgetsJarUrl();
        if (customWidgetsUrl != null) {
            result.add(customWidgetsUrl);
        }
    }
    List<VirtualFile> externalLibraries;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet != null && facet.requiresAndroidModel() && facet.getAndroidModel() != null) {
        AndroidModel androidModel = facet.getAndroidModel();
        externalLibraries = androidModel.getClassJarProvider().getModuleExternalLibraries(module);
    } else {
        externalLibraries = AndroidRootUtil.getExternalLibraries(module);
    }
    for (VirtualFile libFile : externalLibraries) {
        if (EXT_JAR.equals(libFile.getExtension())) {
            final File file = new File(libFile.getPath());
            if (file.exists()) {
                try {
                    result.add(SdkUtils.fileToUrl(file));
                    File aarDir = file.getParentFile();
                    if (aarDir != null && (aarDir.getPath().endsWith(DOT_AAR) || aarDir.getPath().contains(EXPLODED_AAR))) {
                        if (aarDir.getPath().contains(EXPLODED_AAR)) {
                            if (aarDir.getPath().endsWith(LIBS_FOLDER)) {
                                // Some libraries recently started packaging jars inside a sub libs folder inside jars
                                aarDir = aarDir.getParentFile();
                            }
                            // Gradle plugin version 1.2.x and later has classes in aar-dir/jars/
                            if (aarDir.getPath().endsWith(FD_JARS)) {
                                aarDir = aarDir.getParentFile();
                            }
                        }
                        AppResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
                        if (appResources != null) {
                            ResourceClassRegistry.get(module.getProject()).addAarLibrary(appResources, aarDir);
                        }
                    } else if (aarDir != null) {
                        // Build cache? We need to compute the package name in a slightly different way
                        File parentFile = aarDir.getParentFile();
                        if (parentFile != null) {
                            File manifest = new File(parentFile, ANDROID_MANIFEST_XML);
                            if (manifest.exists()) {
                                AppResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
                                if (appResources != null) {
                                    FileResourceRepository repository = appResources.findRepositoryFor(parentFile);
                                    if (repository != null) {
                                        ResourceClassRegistry registry = ResourceClassRegistry.get(module.getProject());
                                        registry.addLibrary(appResources, registry.getAarPackage(parentFile));
                                    }
                                }
                            }
                        }
                    }
                } catch (MalformedURLException e) {
                    LOG.error(e);
                }
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MalformedURLException(java.net.MalformedURLException) FileResourceRepository(com.android.tools.idea.res.FileResourceRepository) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceClassRegistry(com.android.tools.idea.res.ResourceClassRegistry) URL(java.net.URL) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AndroidModel(com.android.tools.idea.model.AndroidModel) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 4 with AndroidModel

use of com.android.tools.idea.model.AndroidModel in project android by JetBrains.

the class LintIdeProject method createModuleProject.

/** Creates a new module project */
@Nullable
private static LintModuleProject createModuleProject(@NonNull LintClient client, @NonNull Module module) {
    AndroidFacet facet = AndroidFacet.getInstance(module);
    File dir = getLintProjectDirectory(module, facet);
    if (dir == null)
        return null;
    LintModuleProject project = null;
    if (facet == null) {
        project = new LintModuleProject(client, dir, dir, module);
        AndroidFacet f = findAndroidFacetInProject(module.getProject());
        if (f != null) {
            project.gradleProject = f.requiresAndroidModel();
        }
    } else if (facet.requiresAndroidModel()) {
        AndroidModel androidModel = facet.getAndroidModel();
        if (androidModel instanceof AndroidModuleModel) {
            project = new LintGradleProject(client, dir, dir, facet, (AndroidModuleModel) androidModel);
        } else {
            project = new LintAndroidModelProject(client, dir, dir, facet, androidModel);
        }
    } else {
        project = new LintAndroidProject(client, dir, dir, facet);
    }
    if (project != null) {
        client.registerProject(dir, project);
    }
    return project;
}
Also used : AndroidModel(com.android.tools.idea.model.AndroidModel) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with AndroidModel

use of com.android.tools.idea.model.AndroidModel in project kotlin by JetBrains.

the class IntellijLintUtils method getResourceDirectories.

/** Returns the resource directories to use for the given module */
@NotNull
public static List<File> getResourceDirectories(@NotNull AndroidFacet facet) {
    if (facet.requiresAndroidModel()) {
        AndroidModel androidModel = facet.getAndroidModel();
        if (androidModel != null) {
            List<File> resDirectories = new ArrayList<File>();
            List<SourceProvider> sourceProviders = androidModel.getActiveSourceProviders();
            for (SourceProvider provider : sourceProviders) {
                for (File file : provider.getResDirectories()) {
                    if (file.isDirectory()) {
                        resDirectories.add(file);
                    }
                }
            }
            return resDirectories;
        }
    }
    return new ArrayList<File>(facet.getMainSourceProvider().getResDirectories());
}
Also used : AndroidModel(com.android.tools.idea.model.AndroidModel) ArrayList(java.util.ArrayList) SourceProvider(com.android.builder.model.SourceProvider) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AndroidModel (com.android.tools.idea.model.AndroidModel)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)7 File (java.io.File)6 SourceProvider (com.android.builder.model.SourceProvider)3 Module (com.intellij.openapi.module.Module)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 ArrayList (java.util.ArrayList)2 ProductFlavorContainer (com.android.builder.model.ProductFlavorContainer)1 AndroidVersion (com.android.sdklib.AndroidVersion)1 AndroidGradleModel (com.android.tools.idea.gradle.AndroidGradleModel)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 FileResourceRepository (com.android.tools.idea.res.FileResourceRepository)1 ResourceClassRegistry (com.android.tools.idea.res.ResourceClassRegistry)1 Project (com.intellij.openapi.project.Project)1 CompilerModuleExtension (com.intellij.openapi.roots.CompilerModuleExtension)1 VirtualFileManager (com.intellij.openapi.vfs.VirtualFileManager)1