Search in sources :

Example 6 with AndroidModuleInfo

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

the class VaryingConfiguration method getTarget.

@Override
@Nullable
public IAndroidTarget getTarget() {
    if (isOverridingTarget()) {
        return super.getTarget();
    }
    IAndroidTarget target = myParent.getTarget();
    if (isAlternatingTarget() && target != null) {
        ConfigurationManager manager = getConfigurationManager();
        IAndroidTarget[] targets = manager.getTargets();
        if (targets.length > 0) {
            // Pick a different target: if you're showing the most recent render target,
            // then pick the lowest supported target, and vice versa
            IAndroidTarget mostRecent = manager.getHighestApiTarget();
            if (target.equals(mostRecent)) {
                // Find oldest supported
                AndroidModuleInfo info = AndroidModuleInfo.get(manager.getModule());
                if (info != null) {
                    int minSdkVersion = info.getMinSdkVersion().getFeatureLevel();
                    for (IAndroidTarget t : targets) {
                        if (t.getVersion().getFeatureLevel() >= minSdkVersion && ConfigurationManager.isLayoutLibTarget(t)) {
                            target = t;
                            break;
                        }
                    }
                }
            } else {
                target = mostRecent;
            }
        }
    }
    return target;
}
Also used : AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with AndroidModuleInfo

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

the class AndroidLintConvertToWebpInspection method getQuickFixes.

@Override
@NotNull
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    return new AndroidLintQuickFix[] { new DefaultLintQuickFix("Convert images to WebP...") {

        @Override
        public boolean isApplicable(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.ContextType contextType) {
            return AndroidFacet.getInstance(startElement) != null;
        }

        @Override
        public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
            AndroidFacet facet = AndroidFacet.getInstance(startElement);
            if (facet != null) {
                AndroidModuleInfo info = AndroidModuleInfo.get(facet);
                int minSdkVersion = info.getMinSdkVersion().getApiLevel();
                List<VirtualFile> folders = facet.getResourceFolderManager().getFolders();
                ConvertToWebpAction action = new ConvertToWebpAction();
                action.perform(startElement.getProject(), minSdkVersion, folders.toArray(VirtualFile.EMPTY_ARRAY));
            }
        }
    } };
}
Also used : AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ConvertToWebpAction(com.android.tools.idea.rendering.webp.ConvertToWebpAction) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with AndroidModuleInfo

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

the class InferSupportAnnotationsAction method checkModules.

// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
    Set<Module> modulesWithoutAnnotations = new HashSet<>();
    Set<Module> modulesWithLowVersion = new HashSet<>();
    for (Module module : modules.keySet()) {
        AndroidModuleInfo info = AndroidModuleInfo.get(module);
        if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
            modulesWithLowVersion.add(module);
        }
        GradleBuildModel buildModel = GradleBuildModel.get(module);
        if (buildModel == null) {
            Logger.getInstance(InferSupportAnnotationsAction.class).warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
            continue;
        }
        boolean dependencyFound = false;
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
                String notation = dependency.compactNotation().value();
                if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
                    dependencyFound = true;
                    break;
                }
            }
        }
        if (!dependencyFound) {
            modulesWithoutAnnotations.add(module);
        }
    }
    if (!modulesWithLowVersion.isEmpty()) {
        Messages.showErrorDialog(project, String.format("Infer Support Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Support Annotations");
        return false;
    }
    if (modulesWithoutAnnotations.isEmpty()) {
        return true;
    }
    String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
    int count = modulesWithoutAnnotations.size();
    String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
    if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
        LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
        try {
            new WriteCommandAction(project, ADD_DEPENDENCY) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    RepositoryUrlManager manager = RepositoryUrlManager.get();
                    String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
                    for (Module module : modulesWithoutAnnotations) {
                        addDependency(module, annotationsLibraryCoordinate);
                    }
                    GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
                    GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {

                        @Override
                        public void syncSucceeded(@NotNull Project project) {
                            restartAnalysis(project, scope);
                        }
                    });
                }
            }.execute();
        } finally {
            action.finish();
        }
    }
    return false;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) LocalHistoryAction(com.intellij.history.LocalHistoryAction) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) Module(com.intellij.openapi.module.Module)

Example 9 with AndroidModuleInfo

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

the class StringResourceViewPanel method hyperlinkUpdate.

@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
    StringBuilder sb = new StringBuilder("https://translate.google.com/manager/android_studio/");
    // Application Version
    sb.append("?asVer=");
    ApplicationInfo ideInfo = ApplicationInfo.getInstance();
    // @formatter:off
    sb.append(ideInfo.getMajorVersion()).append('.').append(ideInfo.getMinorVersion()).append('.').append(ideInfo.getMicroVersion()).append('.').append(ideInfo.getPatchVersion());
    // @formatter:on
    // Package name
    MergedManifest manifest = MergedManifest.get(myFacet);
    String pkg = manifest.getPackage();
    if (pkg != null) {
        sb.append("&pkgName=");
        sb.append(pkg.replace('.', '_'));
    }
    // Application ID
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(myFacet);
    String appId = moduleInfo.getPackage();
    if (appId != null) {
        sb.append("&appId=");
        sb.append(appId.replace('.', '_'));
    }
    // Version code
    Integer versionCode = manifest.getVersionCode();
    if (versionCode != null) {
        sb.append("&apkVer=");
        sb.append(versionCode.toString());
    }
    // If we support additional IDE languages, we can send the language used in the IDE here
    //sb.append("&lang=en");
    BrowserUtil.browse(sb.toString());
}
Also used : AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) MergedManifest(com.android.tools.idea.model.MergedManifest) ApplicationInfo(com.intellij.openapi.application.ApplicationInfo)

Example 10 with AndroidModuleInfo

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

the class AndroidInferNullityAnnotationAction method checkModules.

// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
    Set<Module> modulesWithoutAnnotations = new HashSet<>();
    Set<Module> modulesWithLowVersion = new HashSet<>();
    for (Module module : modules.keySet()) {
        AndroidModuleInfo info = AndroidModuleInfo.get(module);
        if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
            modulesWithLowVersion.add(module);
        }
        GradleBuildModel buildModel = GradleBuildModel.get(module);
        if (buildModel == null) {
            LOG.warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
            continue;
        }
        boolean dependencyFound = false;
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
                String notation = dependency.compactNotation().value();
                if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
                    dependencyFound = true;
                    break;
                }
            }
        }
        if (!dependencyFound) {
            modulesWithoutAnnotations.add(module);
        }
    }
    if (!modulesWithLowVersion.isEmpty()) {
        Messages.showErrorDialog(project, String.format("Infer Nullity Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Nullity Annotations");
        return false;
    }
    if (modulesWithoutAnnotations.isEmpty()) {
        return true;
    }
    String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
    int count = modulesWithoutAnnotations.size();
    String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
    if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
        LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
        try {
            new WriteCommandAction(project, ADD_DEPENDENCY) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    RepositoryUrlManager manager = RepositoryUrlManager.get();
                    String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
                    for (Module module : modulesWithoutAnnotations) {
                        addDependency(module, annotationsLibraryCoordinate);
                    }
                    GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
                    GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {

                        @Override
                        public void syncSucceeded(@NotNull Project project) {
                            restartAnalysis(project, scope);
                        }
                    });
                }
            }.execute();
        } finally {
            action.finish();
        }
    }
    return false;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) LocalHistoryAction(com.intellij.history.LocalHistoryAction) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) Module(com.intellij.openapi.module.Module)

Aggregations

AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)13 Module (com.intellij.openapi.module.Module)8 Project (com.intellij.openapi.project.Project)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 NotNull (org.jetbrains.annotations.NotNull)4 AndroidVersion (com.android.sdklib.AndroidVersion)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)3 SourceProvider (com.android.builder.model.SourceProvider)2 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 IAndroidTarget (com.android.sdklib.IAndroidTarget)2 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)2 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)2 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)2 RepositoryUrlManager (com.android.tools.idea.templates.RepositoryUrlManager)2 LocalHistoryAction (com.intellij.history.LocalHistoryAction)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 File (java.io.File)2 IdeaSourceProvider (org.jetbrains.android.facet.IdeaSourceProvider)2