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();
}
}
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);
}
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);
}
Aggregations