Search in sources :

Example 16 with ViewInfo

use of com.android.ide.common.rendering.api.ViewInfo in project android by JetBrains.

the class AndroidPreviewPanelTest method testSimpleRender.

public void testSimpleRender() throws ParserConfigurationException, IOException, SAXException, InterruptedException {
    VirtualFile layout = myFixture.copyFileToProject("themeEditor/theme_preview_layout.xml", "res/layout/theme_preview_layout.xml");
    Configuration configuration = myFacet.getConfigurationManager().getConfiguration(layout);
    AtomicBoolean executorCalled = new AtomicBoolean(false);
    ExecutorService threadPool = Executors.newFixedThreadPool(1);
    Executor executor = (r) -> {
        // Run in a separate thread and wait for the result
        try {
            threadPool.submit(r).get(60, TimeUnit.SECONDS);
            executorCalled.set(true);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            throw new AssertionError("Unexpected exception", e);
        }
    };
    AndroidPreviewPanel.GraphicsLayoutRendererFactory graphicsFactory = (configuration1, parser, background) -> {
        Module module = configuration.getModule();
        AndroidFacet facet = AndroidFacet.getInstance(configuration.getModule());
        assertNotNull(facet);
        AndroidPlatform platform = AndroidPlatform.getInstance(module);
        assertNotNull(platform);
        return GraphicsLayoutRenderer.create(facet, platform, getProject(), configuration, parser, background, SessionParams.RenderingMode.V_SCROLL, false);
    };
    AndroidPreviewPanel panel = new AndroidPreviewPanel(configuration, executor, graphicsFactory) {

        @Override
        public void invalidateGraphicsRenderer() {
            myInvalidateRunnable.run();
        }
    };
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader("<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"" + "     android:layout_width=\"match_parent\"" + "     android:layout_height=\"match_parent\"" + "     android:background=\"#0F0\">" + "     <TextView" + "       android:layout_width=\"wrap_content\"" + "       android:layout_height=\"wrap_content\"" + "       android:text=\"Hello world\"" + "       android:background=\"#F00\"/>" + "</LinearLayout>")));
    panel.setDocument(document);
    panel.setBounds(0, 0, 400, 100);
    //noinspection UndesirableClassUsage (we don't want to use HDPI images here)
    BufferedImage output = new BufferedImage(400, 100, BufferedImage.TYPE_INT_ARGB);
    panel.paintComponent(output.getGraphics());
    // Make sure that the executor got called since we were trying to render on the UI thread.
    assertTrue(executorCalled.get());
    BufferedImage goldenImage = ImageIO.read(new File(getTestDataPath() + "/themeEditor/previewPanel/golden.png"));
    assertImageSimilar("layout", goldenImage, output, 3);
    ViewInfo textView = panel.findViewAtPoint(new Point(0, 0));
    assertEquals("android.widget.TextView", textView.getClassName());
    threadPool.shutdownNow();
    threadPool.awaitTermination(30, TimeUnit.SECONDS);
    Disposer.dispose(panel);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) InputSource(org.xml.sax.InputSource) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) BufferedImage(java.awt.image.BufferedImage) VirtualFile(com.intellij.openapi.vfs.VirtualFile) java.util.concurrent(java.util.concurrent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) IOException(java.io.IOException) File(java.io.File) ImageDiffUtil.assertImageSimilar(com.android.tools.adtui.imagediff.ImageDiffUtil.assertImageSimilar) java.awt(java.awt) SessionParams(com.android.ide.common.rendering.api.SessionParams) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) ViewInfo(com.android.ide.common.rendering.api.ViewInfo) SAXException(org.xml.sax.SAXException) Disposer(com.intellij.openapi.util.Disposer) ImageIO(javax.imageio.ImageIO) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Module(com.intellij.openapi.module.Module) AndroidTestCase(org.jetbrains.android.AndroidTestCase) Configuration(com.android.tools.idea.configurations.Configuration) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Configuration(com.android.tools.idea.configurations.Configuration) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) Document(org.w3c.dom.Document) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) BufferedImage(java.awt.image.BufferedImage) ViewInfo(com.android.ide.common.rendering.api.ViewInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringReader(java.io.StringReader) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 17 with ViewInfo

use of com.android.ide.common.rendering.api.ViewInfo in project android by JetBrains.

the class PreferenceGroupDragHandler method initDividerHeight.

private void initDividerHeight() {
    ViewInfo view = ViewInfoUtils.findListView(editor.getRootViews());
    assert view != null;
    myDividerHeight = ((ListView) view.getViewObject()).getDividerHeight();
}
Also used : ViewInfo(com.android.ide.common.rendering.api.ViewInfo)

Example 18 with ViewInfo

use of com.android.ide.common.rendering.api.ViewInfo in project android_frameworks_base by crdroidandroid.

the class RenderSessionImpl method visit.

/**
     * Visits a {@link View} and its children and generate a {@link ViewInfo} containing the
     * bounds of all the views.
     *
     * @param view the root View
     * @param offset an offset for the view bounds.
     * @param setExtendedInfo whether to set the extended view info in the {@link ViewInfo} object.
     * @param isContentFrame {@code true} if the {@code ViewInfo} to be created is part of the
     *                       content frame.
     *
     * @return {@code ViewInfo} containing the bounds of the view and it children otherwise.
     */
private ViewInfo visit(View view, int offset, boolean setExtendedInfo, boolean isContentFrame) {
    ViewInfo result = createViewInfo(view, offset, setExtendedInfo, isContentFrame);
    if (view instanceof ViewGroup) {
        ViewGroup group = ((ViewGroup) view);
        result.setChildren(visitAllChildren(group, isContentFrame ? 0 : offset, setExtendedInfo, isContentFrame));
    }
    return result;
}
Also used : ViewGroup(android.view.ViewGroup) ViewInfo(com.android.ide.common.rendering.api.ViewInfo)

Example 19 with ViewInfo

use of com.android.ide.common.rendering.api.ViewInfo in project android_frameworks_base by ParanoidAndroid.

the class RenderSessionImpl method visit.

/**
     * Visits a View and its children and generate a {@link ViewInfo} containing the
     * bounds of all the views.
     * @param view the root View
     * @param offset an offset for the view bounds.
     * @param setExtendedInfo whether to set the extended view info in the {@link ViewInfo} object.
     */
private ViewInfo visit(View view, int offset, boolean setExtendedInfo) {
    if (view == null) {
        return null;
    }
    ViewInfo result = new ViewInfo(view.getClass().getName(), getContext().getViewKey(view), view.getLeft(), view.getTop() + offset, view.getRight(), view.getBottom() + offset, view, view.getLayoutParams());
    if (setExtendedInfo) {
        MarginLayoutParams marginParams = null;
        LayoutParams params = view.getLayoutParams();
        if (params instanceof MarginLayoutParams) {
            marginParams = (MarginLayoutParams) params;
        }
        result.setExtendedInfo(view.getBaseline(), marginParams != null ? marginParams.leftMargin : 0, marginParams != null ? marginParams.topMargin : 0, marginParams != null ? marginParams.rightMargin : 0, marginParams != null ? marginParams.bottomMargin : 0);
    }
    if (view instanceof ViewGroup) {
        ViewGroup group = ((ViewGroup) view);
        result.setChildren(visitAllChildren(group, 0, /*offset*/
        setExtendedInfo));
    }
    return result;
}
Also used : MarginLayoutParams(android.view.ViewGroup.MarginLayoutParams) LayoutParams(android.view.ViewGroup.LayoutParams) ViewGroup(android.view.ViewGroup) MarginLayoutParams(android.view.ViewGroup.MarginLayoutParams) ViewInfo(com.android.ide.common.rendering.api.ViewInfo)

Example 20 with ViewInfo

use of com.android.ide.common.rendering.api.ViewInfo in project android_frameworks_base by AOSPA.

the class RenderSessionImpl method createViewInfo.

/**
     * Creates a {@link ViewInfo} for the view. The {@code ViewInfo} corresponding to the children
     * of the {@code view} are not created. Consequently, the children of {@code ViewInfo} is not
     * set.
     * @param offset an offset for the view bounds. Used only if view is part of the content frame.
     */
private ViewInfo createViewInfo(View view, int offset, boolean setExtendedInfo, boolean isContentFrame) {
    if (view == null) {
        return null;
    }
    ViewParent parent = view.getParent();
    ViewInfo result;
    if (isContentFrame) {
        // Account for parent scroll values when calculating the bounding box
        int scrollX = parent != null ? ((View) parent).getScrollX() : 0;
        int scrollY = parent != null ? ((View) parent).getScrollY() : 0;
        // The view is part of the layout added by the user. Hence,
        // the ViewCookie may be obtained only through the Context.
        result = new ViewInfo(view.getClass().getName(), getContext().getViewKey(view), -scrollX + view.getLeft(), -scrollY + view.getTop() + offset, -scrollX + view.getRight(), -scrollY + view.getBottom() + offset, view, view.getLayoutParams());
    } else {
        // We are part of the system decor.
        SystemViewInfo r = new SystemViewInfo(view.getClass().getName(), getViewKey(view), view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), view, view.getLayoutParams());
        result = r;
        // 3. The overflow popup button.
        if (view instanceof ListMenuItemView) {
            // Mark 2.
            // All menus in the popup are of type ListMenuItemView.
            r.setViewType(ViewType.ACTION_BAR_OVERFLOW_MENU);
        } else {
            // Mark 3.
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp instanceof ActionMenuView.LayoutParams && ((ActionMenuView.LayoutParams) lp).isOverflowButton) {
                r.setViewType(ViewType.ACTION_BAR_OVERFLOW);
            } else {
                // actionProviderClass.
                while (parent != mViewRoot && parent instanceof ViewGroup) {
                    if (parent instanceof ActionMenuView) {
                        r.setViewType(ViewType.ACTION_BAR_MENU);
                        break;
                    }
                    parent = parent.getParent();
                }
            }
        }
    }
    if (setExtendedInfo) {
        MarginLayoutParams marginParams = null;
        LayoutParams params = view.getLayoutParams();
        if (params instanceof MarginLayoutParams) {
            marginParams = (MarginLayoutParams) params;
        }
        result.setExtendedInfo(view.getBaseline(), marginParams != null ? marginParams.leftMargin : 0, marginParams != null ? marginParams.topMargin : 0, marginParams != null ? marginParams.rightMargin : 0, marginParams != null ? marginParams.bottomMargin : 0);
    }
    return result;
}
Also used : ListMenuItemView(com.android.internal.view.menu.ListMenuItemView) MarginLayoutParams(android.view.ViewGroup.MarginLayoutParams) LayoutParams(android.view.ViewGroup.LayoutParams) ViewParent(android.view.ViewParent) ViewGroup(android.view.ViewGroup) LayoutParams(android.view.ViewGroup.LayoutParams) ActionMenuView(android.widget.ActionMenuView) MarginLayoutParams(android.view.ViewGroup.MarginLayoutParams) ViewInfo(com.android.ide.common.rendering.api.ViewInfo)

Aggregations

ViewInfo (com.android.ide.common.rendering.api.ViewInfo)37 ViewGroup (android.view.ViewGroup)11 LayoutParams (android.view.ViewGroup.LayoutParams)6 MarginLayoutParams (android.view.ViewGroup.MarginLayoutParams)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)6 XmlTag (com.intellij.psi.xml.XmlTag)6 ViewParent (android.view.ViewParent)5 ActionMenuView (android.widget.ActionMenuView)5 ListMenuItemView (com.android.internal.view.menu.ListMenuItemView)5 LayoutLibTestCallback (com.android.layoutlib.bridge.intensive.setup.LayoutLibTestCallback)5 LayoutPullParser (com.android.layoutlib.bridge.intensive.setup.LayoutPullParser)5 NotNull (org.jetbrains.annotations.NotNull)5 Test (org.junit.Test)5 View (android.view.View)3 RenderLogger (com.android.tools.idea.rendering.RenderLogger)3 XmlFile (com.intellij.psi.xml.XmlFile)3 ListView (android.widget.ListView)2 Configuration (com.android.tools.idea.configurations.Configuration)2 RenderService (com.android.tools.idea.rendering.RenderService)2 RenderTask (com.android.tools.idea.rendering.RenderTask)2