Search in sources :

Example 1 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class ResourceNotificationManagerTest method test.

public void test() {
    @Language("XML") String xml;
    // Setup sample project: a strings file, and a couple of layout file
    xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<FrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + "    android:layout_width=\"match_parent\"\n" + "    android:layout_height=\"match_parent\">\n" + "    <!-- My comment -->\n" + "    <TextView " + "        android:layout_width=\"match_parent\"\n" + "        android:layout_height=\"match_parent\"\n" + "        android:text=\"@string/hello\" />\n" + "</FrameLayout>";
    final XmlFile layout1 = (XmlFile) myFixture.addFileToProject("res/layout/my_layout1.xml", xml);
    @SuppressWarnings("ConstantConditions") VirtualFile resourceDir = layout1.getParent().getParent().getVirtualFile();
    assertNotNull(resourceDir);
    xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + "    android:layout_width=\"match_parent\"\n" + "    android:layout_height=\"match_parent\" />\n";
    final XmlFile layout2 = (XmlFile) myFixture.addFileToProject("res/layout/my_layout2.xml", xml);
    xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "    <string name=\"hello\">Hello</string>\n" + "\n" + "    <!-- Base application theme. -->\n" + "    <style name=\"AppTheme\" parent=\"Theme.AppCompat.Light.DarkActionBar\">\n" + "        <!-- Customize your theme here. -->\n" + "        <item name=\"android:colorBackground\">#ff0000</item>\n" + "    </style>" + "</resources>";
    final XmlFile values1 = (XmlFile) myFixture.addFileToProject("res/values/my_values1.xml", xml);
    xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "    \n" + "</resources>";
    myFixture.addFileToProject("res/values/colors.xml", xml);
    final Configuration configuration1 = myFacet.getConfigurationManager().getConfiguration(layout1.getVirtualFile());
    final ResourceNotificationManager manager = ResourceNotificationManager.getInstance(getProject());
    // Listener 1: Listens for changes in layout 1
    final Ref<Boolean> called1 = new Ref<>(false);
    final Ref<Set<Reason>> calledValue1 = new Ref<>();
    ResourceChangeListener listener1 = new ResourceChangeListener() {

        @Override
        public void resourcesChanged(@NotNull Set<Reason> reason) {
            called1.set(true);
            calledValue1.set(reason);
        }
    };
    // Listener 2: Only listens for general changes in the module
    final Ref<Boolean> called2 = new Ref<>(false);
    final Ref<Set<Reason>> calledValue2 = new Ref<>();
    ResourceChangeListener listener2 = new ResourceChangeListener() {

        @Override
        public void resourcesChanged(@NotNull Set<Reason> reason) {
            called2.set(true);
            calledValue2.set(reason);
        }
    };
    manager.addListener(listener1, myFacet, layout1, configuration1);
    manager.addListener(listener2, myFacet, null, null);
    // Make sure that when we're modifying multiple files, with complicated
    // edits (that trigger full file rescans), we handle that scenario correctly.
    clear(called1, calledValue1, called2, calledValue2);
    // There's actually some special optimizations done via PsiResourceItem#recomputeValue
    // to only mark the resource repository changed if the value has actually been looked
    // up. This allows us to not recompute layout if you're editing some string that
    // hasn't actually been looked up and rendered in a layout. In order to make sure
    // that that optimization doesn't kick in here, we need to look up the value of
    // the resource item first:
    //noinspection ConstantConditions
    assertEquals("#ff0000", configuration1.getResourceResolver().getStyle("AppTheme", false).getItem("colorBackground", true).getValue());
    AndroidResourceUtil.createValueResource(getProject(), resourceDir, "color2", ResourceType.COLOR, "colors.xml", Collections.singletonList("values"), "#fa2395");
    ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
    clear(called1, calledValue1, called2, calledValue2);
    @SuppressWarnings("ConstantConditions") final XmlTag tag = values1.getDocument().getRootTag().getSubTags()[1].getSubTags()[0];
    assertEquals("item", tag.getName());
    WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {

        @Override
        public void run() {
            tag.getValue().setEscapedText("@color/color2");
        }
    });
    ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
    // First check: Modify the layout by changing @string/hello to @string/hello_world
    // and verify that our listeners are called.
    ResourceVersion version1 = manager.getCurrentVersion(myFacet, layout1, configuration1);
    addText(layout1, "@string/hello^", "_world");
    ensureCalled(called1, calledValue1, called2, calledValue2, Reason.EDIT);
    ResourceVersion version2 = manager.getCurrentVersion(myFacet, layout1, configuration1);
    assertFalse(version1.toString(), version1.equals(version2));
    // Next check: Modify a <string> value definition in a values file
    // and check that those changes are flagged too
    clear(called1, calledValue1, called2, calledValue2);
    ResourceVersion version3 = manager.getCurrentVersion(myFacet, layout1, configuration1);
    addText(values1, "name=\"hello^\"", "_world");
    ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
    ResourceVersion version4 = manager.getCurrentVersion(myFacet, layout1, configuration1);
    assertFalse(version4.toString(), version3.equals(version4));
    // Next check: Modify content in a comment and verify that no changes are fired
    clear(called1, calledValue1, called2, calledValue2);
    addText(layout1, "My ^comment", "new ");
    ensureNotCalled(called1, called2);
    // Check that editing text in a layout file has no effect
    clear(called1, calledValue1, called2, calledValue2);
    addText(layout1, " ^ <TextView", "abc");
    ensureNotCalled(called1, called2);
    // Make sure that's true for replacements too
    replaceText(layout1, "^abc", "abc".length(), "def");
    ensureNotCalled(called1, called2);
    // ...and for deletions
    removeText(layout1, "^def", "def".length());
    ensureNotCalled(called1, called2);
    // Check that editing text in a *values file* -does- have an effect
    // Read the value first to ensure that we trigger it as a read (see comment above for previous
    // resource resolver lookup)
    //noinspection ConstantConditions
    assertEquals("Hello", configuration1.getResourceResolver().findResValue("@string/hello_world", false).getValue());
    addText(values1, "Hello^</string>", " World");
    ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
    // Check that recreating AppResourceRepository object doesn't affect the ResourceNotificationManager
    clear(called1, calledValue1, called2, calledValue2);
    myFacet.refreshResources();
    AndroidResourceUtil.createValueResource(getProject(), resourceDir, "color4", ResourceType.COLOR, "colors.xml", Collections.singletonList("values"), "#ff2300");
    ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
    // Finally check that once we remove the listeners there are no more notifications
    manager.removeListener(listener1, myFacet, layout1, configuration1);
    manager.removeListener(listener2, myFacet, layout2, configuration1);
    clear(called1, calledValue1, called2, calledValue2);
    addText(layout1, "@string/hello_world^", "2");
    ensureNotCalled(called1, called2);
// TODO: Check that editing a partial URL doesn't re-render
// Check module dependency triggers!
// TODO: Test that remove and replace editing also works as expected
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Set(java.util.Set) EnumSet(java.util.EnumSet) XmlFile(com.intellij.psi.xml.XmlFile) Configuration(com.android.tools.idea.configurations.Configuration) ResourceVersion(com.android.tools.idea.res.ResourceNotificationManager.ResourceVersion) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) Language(org.intellij.lang.annotations.Language) ResourceChangeListener(com.android.tools.idea.res.ResourceNotificationManager.ResourceChangeListener) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class ConstraintUtilities method updateWidgetFromComponent.

/**
   * Update the constraint widget with the component information (coming from XML)
   *
   * @param scene           the scene of components. Can be null.
   * @param constraintModel the constraint model we are working with
   * @param widget          constraint widget
   * @param component       the model component
   * @return true if need to save the xml
   */
static boolean updateWidgetFromComponent(@Nullable Scene scene, @NotNull ConstraintModel constraintModel, @Nullable ConstraintWidget widget, @Nullable NlComponent component) {
    if (component == null || widget == null) {
        return false;
    }
    if (!(widget instanceof Guideline)) {
        widget.setVisibility(component.getAndroidViewVisibility());
    }
    widget.setDebugName(component.getId());
    Insets padding = component.getPadding(true);
    WidgetsScene widgetsScene = constraintModel.getScene();
    if (scene != null) {
        // If the scene exists, use the bounds from it instead of from the NlComponent.
        // This gives us animation on layout changes.
        // Note: this is temporary, once the Scene interaction / painting is fully done we'll switch to it.
        long time = System.currentTimeMillis();
        SceneComponent sceneComponent = scene.getSceneComponent(component);
        if (sceneComponent != null) {
            widget.setDrawOrigin(sceneComponent.getDrawX(time), sceneComponent.getDrawY(time));
            int w = sceneComponent.getDrawWidth(time);
            int h = sceneComponent.getDrawHeight(time);
            widget.setDimension(w, h);
            return false;
        }
    }
    if (widget instanceof ConstraintWidgetContainer) {
        int paddingLeft = constraintModel.pxToDp(padding.left);
        int paddingTop = constraintModel.pxToDp(padding.top);
        int paddingRight = constraintModel.pxToDp(padding.right);
        int paddingBottom = constraintModel.pxToDp(padding.bottom);
        ((ConstraintWidgetContainer) widget).setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        widget.setDimension(constraintModel.pxToDp(component.w) - paddingLeft - paddingRight, constraintModel.pxToDp(component.h) - paddingTop - paddingBottom);
    } else {
        widget.setDimension(constraintModel.pxToDp(component.w), constraintModel.pxToDp(component.h));
    }
    String absoluteWidth = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_WIDTH);
    if (absoluteWidth != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        int size = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteWidth, configuration);
        size = constraintModel.pxToDp(size);
        widget.setWidth(size);
    }
    String absoluteHeight = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_HEIGHT);
    if (absoluteHeight != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        int size = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteHeight, configuration);
        size = constraintModel.pxToDp(size);
        widget.setHeight(size);
    }
    widget.setMinWidth(constraintModel.pxToDp(component.getMinimumWidth()));
    widget.setMinHeight(constraintModel.pxToDp(component.getMinimumHeight()));
    NlComponent parent = component.getParent();
    NlModel model = component.getModel();
    if (parent != null) {
        ConstraintWidget parentWidget = widgetsScene.getWidget(parent);
        if (parentWidget instanceof WidgetContainer) {
            WidgetContainer parentContainerWidget = (WidgetContainer) parentWidget;
            if (widget.getParent() != parentContainerWidget) {
                parentContainerWidget.add(widget);
            }
        }
    }
    // First set the origin of the widget
    int x = constraintModel.pxToDp(component.x);
    int y = constraintModel.pxToDp(component.y);
    if (widget instanceof ConstraintWidgetContainer) {
        x += constraintModel.pxToDp(padding.left);
        y += constraintModel.pxToDp(padding.top);
    }
    WidgetContainer parentContainer = (WidgetContainer) widget.getParent();
    if (parentContainer != null) {
        if (!(parentContainer instanceof ConstraintWidgetContainer)) {
            x = constraintModel.pxToDp(component.x - component.getParent().x);
            y = constraintModel.pxToDp(component.y - component.getParent().y);
        } else {
            x -= parentContainer.getDrawX();
            y -= parentContainer.getDrawY();
        }
    }
    String absoluteX = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_X);
    if (absoluteX != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        int position = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteX, configuration);
        x = constraintModel.pxToDp(position);
    }
    String absoluteY = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_Y);
    if (absoluteY != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        int position = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteY, configuration);
        y = constraintModel.pxToDp(position);
    }
    if (scene == null) {
        if (widget.getX() != x || widget.getY() != y) {
            widget.setOrigin(x, y);
            widget.forceUpdateDrawPosition();
        }
    }
    boolean overrideDimension = false;
    // FIXME: need to agree on the correct magic value for this rather than simply using zero.
    String layout_width = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_WIDTH);
    if (component.w == 0 || getLayoutDimensionDpValue(component, layout_width) == 0) {
        widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
    } else if (layout_width != null && layout_width.equalsIgnoreCase(SdkConstants.VALUE_WRAP_CONTENT)) {
        if (APPLY_MINIMUM_SIZE && widget.getWidth() < MINIMUM_SIZE && widget instanceof WidgetContainer && ((WidgetContainer) widget).getChildren().size() == 0) {
            widget.setWidth(MINIMUM_SIZE);
            widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
            overrideDimension = true;
        } else {
            widget.setWrapWidth(widget.getWidth());
            widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.WRAP_CONTENT);
        }
    } else if (layout_width != null && layout_width.equalsIgnoreCase(SdkConstants.VALUE_MATCH_PARENT)) {
        if (isWidgetInsideConstraintLayout(widget)) {
            if (widget.getAnchor(ConstraintAnchor.Type.LEFT).isConnected() && widget.getAnchor(ConstraintAnchor.Type.RIGHT).isConnected()) {
                widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
            } else {
                widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
                widget.setWidth(MINIMUM_SIZE_EXPAND);
                int height = widget.getHeight();
                ConstraintWidget.DimensionBehaviour verticalBehaviour = widget.getVerticalDimensionBehaviour();
                if (height <= 1 && widget instanceof WidgetContainer) {
                    widget.setHeight(MINIMUM_SIZE_EXPAND);
                }
                ArrayList<ConstraintWidget> widgets = new ArrayList<>();
                widgets.add(widget);
                Scout.arrangeWidgets(Scout.Arrange.ExpandHorizontally, widgets, true);
                widget.setHeight(height);
                widget.setVerticalDimensionBehaviour(verticalBehaviour);
                overrideDimension = true;
            }
        }
    } else {
        widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
    }
    String layout_height = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_HEIGHT);
    if (component.h == 0 || getLayoutDimensionDpValue(component, layout_height) == 0) {
        widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
    } else if (layout_height != null && layout_height.equalsIgnoreCase(SdkConstants.VALUE_WRAP_CONTENT)) {
        if (APPLY_MINIMUM_SIZE && widget.getHeight() < MINIMUM_SIZE && widget instanceof WidgetContainer && ((WidgetContainer) widget).getChildren().size() == 0) {
            widget.setHeight(MINIMUM_SIZE);
            widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
            overrideDimension = true;
        } else {
            widget.setWrapHeight(widget.getHeight());
            widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.WRAP_CONTENT);
        }
    } else if (layout_height != null && layout_height.equalsIgnoreCase(SdkConstants.VALUE_MATCH_PARENT)) {
        if (isWidgetInsideConstraintLayout(widget)) {
            if ((widget.getAnchor(ConstraintAnchor.Type.TOP).isConnected() && widget.getAnchor(ConstraintAnchor.Type.BOTTOM).isConnected())) {
                widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
            } else {
                widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
                widget.setHeight(MINIMUM_SIZE_EXPAND);
                int width = widget.getWidth();
                ConstraintWidget.DimensionBehaviour horizontalBehaviour = widget.getHorizontalDimensionBehaviour();
                if (width <= 1 && widget instanceof WidgetContainer) {
                    widget.setWidth(MINIMUM_SIZE_EXPAND);
                }
                ArrayList<ConstraintWidget> widgets = new ArrayList<>();
                widgets.add(widget);
                Scout.arrangeWidgets(Scout.Arrange.ExpandVertically, widgets, true);
                widget.setWidth(width);
                widget.setHorizontalDimensionBehaviour(horizontalBehaviour);
                overrideDimension = true;
            }
        }
    } else {
        widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
    }
    widget.setBaselineDistance(constraintModel.pxToDp(component.getBaseline()));
    widget.resetAnchors();
    String left1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_LEFT_TO_LEFT_OF);
    String left2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_LEFT_TO_RIGHT_OF);
    String right1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_RIGHT_TO_LEFT_OF);
    String right2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_RIGHT_TO_RIGHT_OF);
    String top1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_TOP_TO_TOP_OF);
    String top2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_TOP_TO_BOTTOM_OF);
    String bottom1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BOTTOM_TO_TOP_OF);
    String bottom2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BOTTOM_TO_BOTTOM_OF);
    String baseline = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BASELINE_TO_BASELINE_OF);
    String ratio = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_DIMENSION_RATIO);
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    companion.getWidgetProperties().clear();
    companion.getWidgetProperties().setDimensionRatio(ratio);
    setMarginType(ConstraintAnchor.Type.LEFT, component, widget);
    setMarginType(ConstraintAnchor.Type.RIGHT, component, widget);
    setMarginType(ConstraintAnchor.Type.TOP, component, widget);
    setMarginType(ConstraintAnchor.Type.BOTTOM, component, widget);
    setTarget(model, widgetsScene, left1, widget, ConstraintAnchor.Type.LEFT, ConstraintAnchor.Type.LEFT);
    setStartMargin(left1, component, widget);
    setTarget(model, widgetsScene, left2, widget, ConstraintAnchor.Type.LEFT, ConstraintAnchor.Type.RIGHT);
    setStartMargin(left2, component, widget);
    setTarget(model, widgetsScene, right1, widget, ConstraintAnchor.Type.RIGHT, ConstraintAnchor.Type.LEFT);
    setEndMargin(right1, component, widget);
    setTarget(model, widgetsScene, right2, widget, ConstraintAnchor.Type.RIGHT, ConstraintAnchor.Type.RIGHT);
    setEndMargin(right2, component, widget);
    setTarget(model, widgetsScene, top1, widget, ConstraintAnchor.Type.TOP, ConstraintAnchor.Type.TOP);
    setTopMargin(top1, component, widget);
    setTarget(model, widgetsScene, top2, widget, ConstraintAnchor.Type.TOP, ConstraintAnchor.Type.BOTTOM);
    setTopMargin(top2, component, widget);
    setTarget(model, widgetsScene, bottom1, widget, ConstraintAnchor.Type.BOTTOM, ConstraintAnchor.Type.TOP);
    setBottomMargin(bottom1, component, widget);
    setTarget(model, widgetsScene, bottom2, widget, ConstraintAnchor.Type.BOTTOM, ConstraintAnchor.Type.BOTTOM);
    setBottomMargin(bottom2, component, widget);
    setTarget(model, widgetsScene, baseline, widget, ConstraintAnchor.Type.BASELINE, ConstraintAnchor.Type.BASELINE);
    setBias(SdkConstants.ATTR_LAYOUT_HORIZONTAL_BIAS, component, widget);
    setBias(SdkConstants.ATTR_LAYOUT_VERTICAL_BIAS, component, widget);
    setDimensionRatio(SdkConstants.ATTR_LAYOUT_DIMENSION_RATIO, component, widget);
    setChainStyle(SdkConstants.ATTR_LAYOUT_HORIZONTAL_CHAIN_STYLE, component, widget);
    setChainStyle(SdkConstants.ATTR_LAYOUT_VERTICAL_CHAIN_STYLE, component, widget);
    setChainWeight(SdkConstants.ATTR_LAYOUT_HORIZONTAL_WEIGHT, component, widget);
    setChainWeight(SdkConstants.ATTR_LAYOUT_VERTICAL_WEIGHT, component, widget);
    if (widget instanceof Guideline) {
        Guideline guideline = (Guideline) widget;
        setGuideline(component, guideline);
    }
    // Update text decorator
    WidgetDecorator decorator = companion.getWidgetDecorator(WidgetDecorator.BLUEPRINT_STYLE);
    if (decorator != null && decorator instanceof TextWidget) {
        TextWidget textWidget = (TextWidget) decorator;
        textWidget.setText(getResolvedText(component));
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        Integer size = null;
        if (resourceResolver != null) {
            String textSize = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_SIZE);
            if (textSize != null) {
                size = ViewEditor.resolveDimensionPixelSize(resourceResolver, textSize, configuration);
            }
        }
        if (size == null) {
            // With the specified string, this method cannot return null
            //noinspection ConstantConditions
            size = ViewEditor.resolveDimensionPixelSize(resourceResolver, "15sp", configuration);
        }
        String alignment = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_ALIGNMENT);
        textWidget.setTextAlignment((alignment == null) ? TextWidget.TEXT_ALIGNMENT_VIEW_START : alignmentMap.get(alignment));
        String single = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_SINGLE_LINE);
        textWidget.setSingleLine(Boolean.parseBoolean(single));
        // Cannot be null, see previous condition
        //noinspection ConstantConditions
        textWidget.setTextSize(constraintModel.pxToDp(size));
    }
    // if true, need to update the XML
    return overrideDimension;
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) ArrayList(java.util.ArrayList) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator) WidgetsScene(com.android.tools.sherpa.structure.WidgetsScene) TextWidget(com.android.tools.sherpa.drawing.decorator.TextWidget) SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent) ResourceResolver(com.android.ide.common.resources.ResourceResolver)

Example 3 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class TextViewDecorator method addContent.

@Override
public void addContent(@NotNull DisplayList list, long time, @NotNull SceneContext sceneContext, @NotNull SceneComponent component) {
    Rectangle rect = new Rectangle();
    component.fillDrawRect(time, rect);
    int l = sceneContext.getSwingX(rect.x);
    int t = sceneContext.getSwingY(rect.y);
    int w = sceneContext.getSwingDimension(rect.width);
    int h = sceneContext.getSwingDimension(rect.height);
    String text = ConstraintUtilities.getResolvedText(component.getNlComponent());
    NlComponent nlc = component.getNlComponent();
    Configuration configuration = nlc.getModel().getConfiguration();
    ResourceResolver resourceResolver = configuration.getResourceResolver();
    Integer size = null;
    if (resourceResolver != null) {
        String textSize = nlc.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_SIZE);
        if (textSize != null) {
            size = ViewEditor.resolveDimensionPixelSize(resourceResolver, textSize, configuration);
        }
    }
    if (size == null) {
        // With the specified string, this method cannot return null
        //noinspection ConstantConditions
        size = ViewEditor.resolveDimensionPixelSize(resourceResolver, DEFAULT_DIM, configuration);
        // temporary
        size = (int) (0.8 * size);
    }
    String alignment = nlc.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_ALIGNMENT);
    int align = ConstraintUtilities.getAlignment(alignment);
    String single = nlc.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_SINGLE_LINE);
    boolean singleLine = Boolean.parseBoolean(single);
    int baseLineOffset = sceneContext.getSwingDimension(component.getBaseline());
    int scaleSize = sceneContext.getSwingDimension(size);
    list.add(new DrawTextRegion(l, t, w, h, baseLineOffset, text, singleLine, false, align, DrawTextRegion.TEXT_ALIGNMENT_VIEW_START, scaleSize));
}
Also used : DrawTextRegion(com.android.tools.idea.uibuilder.scene.draw.DrawTextRegion) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) Configuration(com.android.tools.idea.configurations.Configuration) ResourceResolver(com.android.ide.common.resources.ResourceResolver)

Example 4 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class TextDirectionTest method test.

public void test() {
    assertSame(LEFT_TO_RIGHT, TextDirection.fromConfiguration(null));
    Configuration configuration = mock(Configuration.class);
    FolderConfiguration folderConfig = new FolderConfiguration();
    when(configuration.getFullConfig()).thenReturn(folderConfig);
    // Nothing specified
    assertSame(LEFT_TO_RIGHT, TextDirection.fromConfiguration(configuration));
    // LTR specified
    folderConfig.setLayoutDirectionQualifier(new LayoutDirectionQualifier(LayoutDirection.LTR));
    assertSame(LEFT_TO_RIGHT, TextDirection.fromConfiguration(configuration));
    // RTL specified
    folderConfig.setLayoutDirectionQualifier(new LayoutDirectionQualifier(LayoutDirection.RTL));
    assertSame(RIGHT_TO_LEFT, TextDirection.fromConfiguration(configuration));
    assertSame(SegmentType.START, LEFT_TO_RIGHT.getLeftSegment());
    assertSame(SegmentType.END, RIGHT_TO_LEFT.getLeftSegment());
    assertSame(SegmentType.END, LEFT_TO_RIGHT.getRightSegment());
    assertSame(SegmentType.START, RIGHT_TO_LEFT.getRightSegment());
    assertTrue(LEFT_TO_RIGHT.isLeftSegment(SegmentType.START));
    assertFalse(LEFT_TO_RIGHT.isLeftSegment(SegmentType.END));
    assertTrue(RIGHT_TO_LEFT.isLeftSegment(SegmentType.END));
    assertFalse(RIGHT_TO_LEFT.isLeftSegment(SegmentType.START));
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) LayoutDirectionQualifier(com.android.ide.common.resources.configuration.LayoutDirectionQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 5 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class NlPaletteTreeGridTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    myDependencyManager = mock(DependencyManager.class);
    mySurface = mock(DesignSurface.class);
    Runnable closeToolWindowCallback = mock(Runnable.class);
    myIconPreviewFactory = new IconPreviewFactory();
    myPanel = new NlPaletteTreeGrid(getProject(), myDependencyManager, closeToolWindowCallback, mySurface, myIconPreviewFactory);
    PsiFile file = myFixture.configureByText("res/layout/mine.xml", "<LinearLayout/>");
    Configuration configuration = myFacet.getConfigurationManager().getConfiguration(file.getVirtualFile());
    when(mySurface.getConfiguration()).thenReturn(configuration);
}
Also used : DesignSurface(com.android.tools.idea.uibuilder.surface.DesignSurface) Configuration(com.android.tools.idea.configurations.Configuration) PsiFile(com.intellij.psi.PsiFile)

Aggregations

Configuration (com.android.tools.idea.configurations.Configuration)95 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)23 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)21 ResourceResolver (com.android.ide.common.resources.ResourceResolver)16 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)14 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 Module (com.intellij.openapi.module.Module)9 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)8 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Device (com.android.sdklib.devices.Device)7 State (com.android.sdklib.devices.State)7 NotNull (org.jetbrains.annotations.NotNull)7 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)6 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)5 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)5 NlModel (com.android.tools.idea.uibuilder.model.NlModel)4 DesignSurface (com.android.tools.idea.uibuilder.surface.DesignSurface)4 PsiFile (com.intellij.psi.PsiFile)4 ResourceFolderType (com.android.resources.ResourceFolderType)3