Search in sources :

Example 11 with AndroidModuleInfo

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

the class RenderErrorContributor method reportMissingSizeAttributes.

private void reportMissingSizeAttributes(@NotNull final RenderLogger logger, @NotNull RenderTask renderTask) {
    Module module = logger.getModule();
    if (module == null) {
        return;
    }
    Project project = module.getProject();
    if (logger.isMissingSize()) {
        HtmlBuilder builder = new HtmlBuilder();
        // Emit hyperlink about missing attributes; the action will operate on all of them
        builder.addBold("NOTE: One or more layouts are missing the layout_width or layout_height attributes. " + "These are required in most layouts.").newline();
        final ResourceResolver resourceResolver = renderTask.getResourceResolver();
        XmlFile psiFile = renderTask.getPsiFile();
        if (psiFile == null) {
            LOG.error("PsiFile is missing in RenderTask used in RenderErrorPanel!");
            return;
        }
        AddMissingAttributesFix fix = new AddMissingAttributesFix(project, psiFile, resourceResolver);
        List<XmlTag> missing = fix.findViewsMissingSizes();
        // See whether we should offer match_parent instead of fill_parent
        AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(module);
        final String fill = moduleInfo == null || moduleInfo.getBuildSdkVersion() == null || moduleInfo.getBuildSdkVersion().getApiLevel() >= 8 ? VALUE_MATCH_PARENT : VALUE_FILL_PARENT;
        for (final XmlTag tag : missing) {
            ApplicationManager.getApplication().runReadAction(() -> {
                boolean missingWidth = !AddMissingAttributesFix.definesWidth(tag, resourceResolver);
                boolean missingHeight = !AddMissingAttributesFix.definesHeight(tag, resourceResolver);
                assert missingWidth || missingHeight;
                String id = tag.getAttributeValue(ATTR_ID);
                if (id == null || id.length() == 0) {
                    id = '<' + tag.getName() + '>';
                } else {
                    id = '"' + stripIdPrefix(id) + '"';
                }
                if (missingWidth) {
                    reportMissingSize(builder, logger, fill, tag, id, ATTR_LAYOUT_WIDTH);
                }
                if (missingHeight) {
                    reportMissingSize(builder, logger, fill, tag, id, ATTR_LAYOUT_HEIGHT);
                }
            });
        }
        builder.newline().add("Or: ").addLink("Automatically add all missing attributes", myLinkManager.createCommandLink(fix)).newline().newline().newline();
        addIssue().setSeverity(HighlightSeverity.ERROR).setSummary("One or more layouts are missing the layout_width or layout_height attributes").setHtmlContent(builder).build();
    }
}
Also used : AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) HtmlBuilder(com.android.utils.HtmlBuilder) ResourceResolver(com.android.ide.common.resources.ResourceResolver) Module(com.intellij.openapi.module.Module) XmlTag(com.intellij.psi.xml.XmlTag)

Example 12 with AndroidModuleInfo

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

the class GraphicsLayoutRenderer method create.

@VisibleForTesting
@NotNull
static GraphicsLayoutRenderer create(@NotNull AndroidFacet facet, @NotNull AndroidPlatform platform, @NotNull Project project, @NotNull Configuration configuration, @NotNull ILayoutPullParser parser, @Nullable Color backgroundColor, @NotNull SessionParams.RenderingMode renderingMode, boolean useSecurityManager) throws InitializationException {
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(facet);
    LayoutLibrary layoutLib;
    try {
        IAndroidTarget latestTarget = configuration.getConfigurationManager().getHighestApiTarget();
        if (latestTarget == null) {
            throw new UnsupportedLayoutlibException("GraphicsLayoutRenderer requires at least layoutlib version " + MIN_LAYOUTLIB_API_VERSION);
        }
        layoutLib = platform.getSdkData().getTargetData(latestTarget).getLayoutLibrary(project);
        if (layoutLib == null) {
            throw new InitializationException("getLayoutLibrary() returned null");
        }
    } catch (RenderingException e) {
        throw new InitializationException(e);
    } catch (IOException e) {
        throw new InitializationException(e);
    }
    if (layoutLib.getApiLevel() < MIN_LAYOUTLIB_API_VERSION) {
        throw new UnsupportedLayoutlibException("GraphicsLayoutRenderer requires at least layoutlib version " + MIN_LAYOUTLIB_API_VERSION);
    }
    AppResourceRepository appResources = AppResourceRepository.getAppResources(facet, true);
    final Module module = facet.getModule();
    // Security token used to disable the security manager. Only objects that have a reference to it are allowed to disable it.
    Object credential = new Object();
    RenderLogger logger = new RenderLogger("theme_editor", module, credential);
    final ActionBarCallback actionBarCallback = new ActionBarCallback();
    // TODO: Remove LayoutlibCallback dependency.
    //noinspection ConstantConditions
    final LayoutlibCallbackImpl layoutlibCallback = new LayoutlibCallbackImpl(null, layoutLib, appResources, module, facet, logger, credential, null) {

        @Override
        public ActionBarCallback getActionBarCallback() {
            return actionBarCallback;
        }
    };
    // Load the local project R identifiers.
    boolean loadRResult = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

        @Override
        public Boolean compute() {
            // half way.
            if (module.isDisposed()) {
                return false;
            }
            layoutlibCallback.loadAndParseRClass();
            return true;
        }
    });
    if (!loadRResult) {
        throw new AlreadyDisposedException("Module was already disposed");
    }
    IAndroidTarget target = configuration.getTarget();
    if (target == null) {
        throw new InitializationException("Unable to get IAndroidTarget");
    }
    Device device = configuration.getDevice();
    assert device != null;
    HardwareConfigHelper hardwareConfigHelper = new HardwareConfigHelper(device);
    DynamicHardwareConfig hardwareConfig = new DynamicHardwareConfig(hardwareConfigHelper.getConfig());
    List<ResourceValue> resourceLookupChain = new ArrayList<ResourceValue>();
    ResourceResolver resourceResolver = ResourceResolver.copy(configuration.getResourceResolver());
    assert resourceResolver != null;
    // Create a resource resolver that will save the lookups on the passed List<>
    ResourceResolver recordingResourceResolver = resourceResolver.createRecorder(resourceLookupChain);
    final SessionParams params = new SessionParams(parser, renderingMode, module, hardwareConfig, recordingResourceResolver, layoutlibCallback, moduleInfo.getMinSdkVersion().getApiLevel(), moduleInfo.getTargetSdkVersion().getApiLevel(), logger, target instanceof CompatibilityRenderTarget ? target.getVersion().getApiLevel() : 0);
    params.setForceNoDecor();
    params.setAssetRepository(new AssetRepositoryImpl(facet));
    // The App Label needs to be not null
    params.setAppLabel("");
    if (backgroundColor != null) {
        params.setOverrideBgColor(backgroundColor.getRGB());
    }
    RenderSecurityManager mySecurityManager = useSecurityManager ? RenderSecurityManagerFactory.create(module, platform) : null;
    return new GraphicsLayoutRenderer(layoutLib, params, mySecurityManager, hardwareConfig, resourceLookupChain, credential);
}
Also used : LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) AssetRepositoryImpl(com.android.tools.idea.res.AssetRepositoryImpl) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) HardwareConfigHelper(com.android.ide.common.rendering.HardwareConfigHelper) Device(com.android.sdklib.devices.Device) RenderingException(com.android.tools.idea.layoutlib.RenderingException) IAndroidTarget(com.android.sdklib.IAndroidTarget) IOException(java.io.IOException) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) ResourceResolver(com.android.ide.common.resources.ResourceResolver) Module(com.intellij.openapi.module.Module) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with AndroidModuleInfo

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

the class AddAndroidActivityPath method init.

@Override
protected void init() {
    Module module = getModule();
    assert module != null;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    assert facet != null;
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform != null) {
        myState.put(KEY_BUILD_SDK, platform.getTarget().getVersion().getFeatureLevel());
    }
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(facet);
    AndroidVersion minSdkVersion = moduleInfo.getMinSdkVersion();
    myState.put(KEY_MIN_SDK, minSdkVersion);
    myState.put(KEY_TARGET_API, moduleInfo.getTargetSdkVersion());
    myState.put(KEY_PACKAGE_NAME, getInitialPackageName(module, facet));
    myState.put(KEY_OPEN_EDITORS, true);
    if (myTemplate == null) {
        FormFactor formFactor = getFormFactor(myTargetFolder);
        myState.put(FormFactorUtils.getMinApiLevelKey(formFactor), minSdkVersion.getApiLevel());
        myState.put(FormFactorUtils.getBuildApiLevelKey(formFactor), moduleInfo.getTargetSdkVersion().getApiLevel());
        ActivityGalleryStep galleryStep = new ActivityGalleryStep(formFactor, false, KEY_SELECTED_TEMPLATE, module, myParentDisposable);
        addStep(galleryStep);
    } else {
        TemplateMetadata templateMetadata = TemplateManager.getInstance().getTemplateMetadata(myTemplate);
        assert templateMetadata != null;
        myState.put(KEY_SELECTED_TEMPLATE, new TemplateEntry(myTemplate, templateMetadata));
    }
    SourceProvider[] sourceProviders = getSourceProviders(module, myTargetFolder);
    boolean isInstantAppModule = facet.getProjectType() == PROJECT_TYPE_ATOM;
    myState.put(IS_INSTANT_APP_KEY, isInstantAppModule);
    myParameterStep = new TemplateParameterStep2(getFormFactor(myTargetFolder), ImmutableMap.of(), myParentDisposable, KEY_PACKAGE_NAME, sourceProviders, CUSTOMIZE_ACTIVITY_TITLE);
    myAssetStudioStep = new IconStep(KEY_SELECTED_TEMPLATE, KEY_SOURCE_PROVIDER, myParentDisposable);
    addStep(myParameterStep);
    addStep(myAssetStudioStep);
}
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) Module(com.intellij.openapi.module.Module) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata)

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