Search in sources :

Example 31 with SessionParams

use of com.android.ide.common.rendering.api.SessionParams in project platform_frameworks_base by android.

the class Main method testVectorDrawable.

/**
     * Test a vector drawable that uses trimStart and trimEnd. It also tests all the primitives
     * for vector drawables (lines, moves and cubic and quadratic curves).
     */
@Test
public void testVectorDrawable() throws ClassNotFoundException {
    // Create the layout pull parser.
    LayoutPullParser parser = createLayoutPullParser("vector_drawable.xml");
    // Create LayoutLibCallback.
    LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger());
    layoutLibCallback.initResources();
    SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback, "Theme.Material.NoActionBar.Fullscreen", false, RenderingMode.V_SCROLL, 22);
    renderAndVerify(params, "vector_drawable.png", TimeUnit.SECONDS.toNanos(2));
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) LayoutLibTestCallback(com.android.layoutlib.bridge.intensive.setup.LayoutLibTestCallback) LayoutPullParser(com.android.layoutlib.bridge.intensive.setup.LayoutPullParser) Test(org.junit.Test)

Example 32 with SessionParams

use of com.android.ide.common.rendering.api.SessionParams in project platform_frameworks_base by android.

the class Main method getSessionParams.

/**
     * Uses Theme.Material and Target sdk version as 22.
     */
private SessionParams getSessionParams(LayoutPullParser layoutParser, ConfigGenerator configGenerator, LayoutLibTestCallback layoutLibCallback, String themeName, boolean isProjectTheme, RenderingMode renderingMode, int targetSdk) {
    FolderConfiguration config = configGenerator.getFolderConfig();
    ResourceResolver resourceResolver = ResourceResolver.create(sProjectResources.getConfiguredResources(config), sFrameworkRepo.getConfiguredResources(config), themeName, isProjectTheme);
    SessionParams sessionParams = new SessionParams(layoutParser, renderingMode, null, /*used for caching*/
    configGenerator.getHardwareConfig(), resourceResolver, layoutLibCallback, 0, targetSdk, getLayoutLog());
    sessionParams.setFlag(RenderParamsFlags.FLAG_DO_NOT_RENDER_ON_CREATE, true);
    return sessionParams;
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) ResourceResolver(com.android.ide.common.resources.ResourceResolver) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 33 with SessionParams

use of com.android.ide.common.rendering.api.SessionParams in project platform_frameworks_base by android.

the class Main method testGetResourceNameVariants.

@Test
public void testGetResourceNameVariants() throws Exception {
    // Setup
    SessionParams params = createSessionParams("", ConfigGenerator.NEXUS_4);
    AssetManager assetManager = AssetManager.getSystem();
    DisplayMetrics metrics = new DisplayMetrics();
    Configuration configuration = RenderAction.getConfiguration(params);
    Resources resources = new Resources(assetManager, metrics, configuration);
    resources.mLayoutlibCallback = params.getLayoutlibCallback();
    resources.mContext = new BridgeContext(params.getProjectKey(), metrics, params.getResources(), params.getAssets(), params.getLayoutlibCallback(), configuration, params.getTargetSdkVersion(), params.isRtlSupported());
    // Test
    assertEquals("android:style/ButtonBar", resources.getResourceName(android.R.style.ButtonBar));
    assertEquals("android", resources.getResourcePackageName(android.R.style.ButtonBar));
    assertEquals("ButtonBar", resources.getResourceEntryName(android.R.style.ButtonBar));
    assertEquals("style", resources.getResourceTypeName(android.R.style.ButtonBar));
    int id = resources.mLayoutlibCallback.getResourceId(ResourceType.STRING, "app_name");
    assertEquals("com.android.layoutlib.test.myapplication:string/app_name", resources.getResourceName(id));
    assertEquals("com.android.layoutlib.test.myapplication", resources.getResourcePackageName(id));
    assertEquals("string", resources.getResourceTypeName(id));
    assertEquals("app_name", resources.getResourceEntryName(id));
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) AssetManager(android.content.res.AssetManager) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(android.content.res.Configuration) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) FrameworkResources(com.android.ide.common.resources.FrameworkResources) Resources(android.content.res.Resources) DisplayMetrics(android.util.DisplayMetrics) Test(org.junit.Test)

Example 34 with SessionParams

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

the class RenderSessionImpl method render.

/**
     * Renders the scene.
     * <p>
     * {@link #acquire(long)} must have been called before this.
     *
     * @param freshRender whether the render is a new one and should erase the existing bitmap (in
     *      the case where bitmaps are reused). This is typically needed when not playing
     *      animations.)
     *
     * @throws IllegalStateException if the current context is different than the one owned by
     *      the scene, or if {@link #acquire(long)} was not called.
     *
     * @see RenderParams#getRenderingMode()
     * @see RenderSession#render(long)
     */
public Result render(boolean freshRender) {
    checkLock();
    SessionParams params = getParams();
    try {
        if (mViewRoot == null) {
            return ERROR_NOT_INFLATED.createResult();
        }
        RenderingMode renderingMode = params.getRenderingMode();
        HardwareConfig hardwareConfig = params.getHardwareConfig();
        // only do the screen measure when needed.
        boolean newRenderSize = false;
        if (mMeasuredScreenWidth == -1) {
            newRenderSize = true;
            mMeasuredScreenWidth = hardwareConfig.getScreenWidth();
            mMeasuredScreenHeight = hardwareConfig.getScreenHeight();
            if (renderingMode != RenderingMode.NORMAL) {
                int widthMeasureSpecMode = renderingMode.isHorizExpand() ? // this lets us know the actual needed size
                MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY;
                int heightMeasureSpecMode = renderingMode.isVertExpand() ? // this lets us know the actual needed size
                MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY;
                // We used to compare the measured size of the content to the screen size but
                // this does not work anymore due to the 2 following issues:
                // - If the content is in a decor (system bar, title/action bar), the root view
                //   will not resize even with the UNSPECIFIED because of the embedded layout.
                // - If there is no decor, but a dialog frame, then the dialog padding prevents
                //   comparing the size of the content to the screen frame (as it would not
                //   take into account the dialog padding).
                // The solution is to first get the content size in a normal rendering, inside
                // the decor or the dialog padding.
                // Then measure only the content with UNSPECIFIED to see the size difference
                // and apply this to the screen size.
                // first measure the full layout, with EXACTLY to get the size of the
                // content as it is inside the decor/dialog
                Pair<Integer, Integer> exactMeasure = measureView(mViewRoot, mContentRoot.getChildAt(0), mMeasuredScreenWidth, MeasureSpec.EXACTLY, mMeasuredScreenHeight, MeasureSpec.EXACTLY);
                // now measure the content only using UNSPECIFIED (where applicable, based on
                // the rendering mode). This will give us the size the content needs.
                Pair<Integer, Integer> result = measureView(mContentRoot, mContentRoot.getChildAt(0), mMeasuredScreenWidth, widthMeasureSpecMode, mMeasuredScreenHeight, heightMeasureSpecMode);
                // now look at the difference and add what is needed.
                if (renderingMode.isHorizExpand()) {
                    int measuredWidth = exactMeasure.getFirst();
                    int neededWidth = result.getFirst();
                    if (neededWidth > measuredWidth) {
                        mMeasuredScreenWidth += neededWidth - measuredWidth;
                    }
                }
                if (renderingMode.isVertExpand()) {
                    int measuredHeight = exactMeasure.getSecond();
                    int neededHeight = result.getSecond();
                    if (neededHeight > measuredHeight) {
                        mMeasuredScreenHeight += neededHeight - measuredHeight;
                    }
                }
            }
        }
        // measure again with the size we need
        // This must always be done before the call to layout
        measureView(mViewRoot, null, /*measuredView*/
        mMeasuredScreenWidth, MeasureSpec.EXACTLY, mMeasuredScreenHeight, MeasureSpec.EXACTLY);
        // now do the layout.
        mViewRoot.layout(0, 0, mMeasuredScreenWidth, mMeasuredScreenHeight);
        if (params.isLayoutOnly()) {
            // delete the canvas and image to reset them on the next full rendering
            mImage = null;
            mCanvas = null;
        } else {
            AttachInfo_Accessor.dispatchOnPreDraw(mViewRoot);
            // draw the views
            // create the BufferedImage into which the layout will be rendered.
            boolean newImage = false;
            if (newRenderSize || mCanvas == null) {
                if (params.getImageFactory() != null) {
                    mImage = params.getImageFactory().getImage(mMeasuredScreenWidth, mMeasuredScreenHeight);
                } else {
                    mImage = new BufferedImage(mMeasuredScreenWidth, mMeasuredScreenHeight, BufferedImage.TYPE_INT_ARGB);
                    newImage = true;
                }
                if (params.isBgColorOverridden()) {
                    // since we override the content, it's the same as if it was a new image.
                    newImage = true;
                    Graphics2D gc = mImage.createGraphics();
                    gc.setColor(new Color(params.getOverrideBgColor(), true));
                    gc.setComposite(AlphaComposite.Src);
                    gc.fillRect(0, 0, mMeasuredScreenWidth, mMeasuredScreenHeight);
                    gc.dispose();
                }
                // create an Android bitmap around the BufferedImage
                Bitmap bitmap = Bitmap_Delegate.createBitmap(mImage, true, /*isMutable*/
                hardwareConfig.getDensity());
                // create a Canvas around the Android bitmap
                mCanvas = new Canvas(bitmap);
                mCanvas.setDensity(hardwareConfig.getDensity().getDpiValue());
            }
            if (freshRender && newImage == false) {
                Graphics2D gc = mImage.createGraphics();
                gc.setComposite(AlphaComposite.Src);
                gc.setColor(new Color(0x00000000, true));
                gc.fillRect(0, 0, mMeasuredScreenWidth, mMeasuredScreenHeight);
                // done
                gc.dispose();
            }
            mViewRoot.draw(mCanvas);
        }
        mViewInfoList = startVisitingViews(mViewRoot, 0, params.getExtendedViewInfoMode());
        // success!
        return SUCCESS.createResult();
    } catch (Throwable e) {
        // get the real cause of the exception.
        Throwable t = e;
        while (t.getCause() != null) {
            t = t.getCause();
        }
        return ERROR_UNKNOWN.createResult(t.getMessage(), t);
    }
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) Color(java.awt.Color) Canvas(android.graphics.Canvas) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Bitmap(android.graphics.Bitmap) RenderingMode(com.android.ide.common.rendering.api.SessionParams.RenderingMode)

Example 35 with SessionParams

use of com.android.ide.common.rendering.api.SessionParams in project android_frameworks_base by DirtyUnicorns.

the class RenderSessionImpl method postInflateProcess.

/**
     * Post process on a view hierarchy that was just inflated.
     * <p/>
     * At the moment this only supports TabHost: If {@link TabHost} is detected, look for the
     * {@link TabWidget}, and the corresponding {@link FrameLayout} and make new tabs automatically
     * based on the content of the {@link FrameLayout}.
     * @param view the root view to process.
     * @param layoutlibCallback callback to the project.
     * @param skip the view and it's children are not processed.
     */
// For the use of Pair
@SuppressWarnings("deprecation")
private void postInflateProcess(View view, LayoutlibCallback layoutlibCallback, View skip) throws PostInflateException {
    if (view == skip) {
        return;
    }
    if (view instanceof TabHost) {
        setupTabHost((TabHost) view, layoutlibCallback);
    } else if (view instanceof QuickContactBadge) {
        QuickContactBadge badge = (QuickContactBadge) view;
        badge.setImageToDefault();
    } else if (view instanceof AdapterView<?>) {
        // get the view ID.
        int id = view.getId();
        BridgeContext context = getContext();
        // get a ResourceReference from the integer ID.
        ResourceReference listRef = context.resolveId(id);
        if (listRef != null) {
            SessionParams params = getParams();
            AdapterBinding binding = params.getAdapterBindings().get(listRef);
            // if there was no adapter binding, trying to get it from the call back.
            if (binding == null) {
                binding = layoutlibCallback.getAdapterBinding(listRef, context.getViewKey(view), view);
            }
            if (binding != null) {
                if (view instanceof AbsListView) {
                    if ((binding.getFooterCount() > 0 || binding.getHeaderCount() > 0) && view instanceof ListView) {
                        ListView list = (ListView) view;
                        boolean skipCallbackParser = false;
                        int count = binding.getHeaderCount();
                        for (int i = 0; i < count; i++) {
                            Pair<View, Boolean> pair = context.inflateView(binding.getHeaderAt(i), list, false, skipCallbackParser);
                            if (pair.getFirst() != null) {
                                list.addHeaderView(pair.getFirst());
                            }
                            skipCallbackParser |= pair.getSecond();
                        }
                        count = binding.getFooterCount();
                        for (int i = 0; i < count; i++) {
                            Pair<View, Boolean> pair = context.inflateView(binding.getFooterAt(i), list, false, skipCallbackParser);
                            if (pair.getFirst() != null) {
                                list.addFooterView(pair.getFirst());
                            }
                            skipCallbackParser |= pair.getSecond();
                        }
                    }
                    if (view instanceof ExpandableListView) {
                        ((ExpandableListView) view).setAdapter(new FakeExpandableAdapter(listRef, binding, layoutlibCallback));
                    } else {
                        ((AbsListView) view).setAdapter(new FakeAdapter(listRef, binding, layoutlibCallback));
                    }
                } else if (view instanceof AbsSpinner) {
                    ((AbsSpinner) view).setAdapter(new FakeAdapter(listRef, binding, layoutlibCallback));
                }
            }
        }
    } else if (view instanceof ViewGroup) {
        mInflater.postInflateProcess(view);
        ViewGroup group = (ViewGroup) view;
        final int count = group.getChildCount();
        for (int c = 0; c < count; c++) {
            View child = group.getChildAt(c);
            postInflateProcess(child, layoutlibCallback, skip);
        }
    }
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) TabHost(android.widget.TabHost) ViewGroup(android.view.ViewGroup) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) AbsListView(android.widget.AbsListView) FakeAdapter(com.android.layoutlib.bridge.impl.binding.FakeAdapter) FakeExpandableAdapter(com.android.layoutlib.bridge.impl.binding.FakeExpandableAdapter) MenuView(com.android.internal.view.menu.MenuView) View(android.view.View) AdapterView(android.widget.AdapterView) ActionMenuItemView(com.android.internal.view.menu.ActionMenuItemView) IconMenuItemView(com.android.internal.view.menu.IconMenuItemView) ListView(android.widget.ListView) ListMenuItemView(com.android.internal.view.menu.ListMenuItemView) AbsListView(android.widget.AbsListView) ActionMenuView(android.widget.ActionMenuView) ExpandableListView(android.widget.ExpandableListView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) ExpandableListView(android.widget.ExpandableListView) QuickContactBadge(android.widget.QuickContactBadge) AdapterBinding(com.android.ide.common.rendering.api.AdapterBinding) AbsSpinner(android.widget.AbsSpinner) AdapterView(android.widget.AdapterView) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) ExpandableListView(android.widget.ExpandableListView)

Aggregations

SessionParams (com.android.ide.common.rendering.api.SessionParams)54 Test (org.junit.Test)25 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)23 LayoutLibTestCallback (com.android.layoutlib.bridge.intensive.setup.LayoutLibTestCallback)20 LayoutPullParser (com.android.layoutlib.bridge.intensive.setup.LayoutPullParser)20 View (android.view.View)12 AbsListView (android.widget.AbsListView)12 AdapterView (android.widget.AdapterView)12 ExpandableListView (android.widget.ExpandableListView)12 ListView (android.widget.ListView)12 Result (com.android.ide.common.rendering.api.Result)11 ActionMenuView (android.widget.ActionMenuView)10 FrameLayout (android.widget.FrameLayout)10 LinearLayout (android.widget.LinearLayout)10 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)10 ActionMenuItemView (com.android.internal.view.menu.ActionMenuItemView)10 IconMenuItemView (com.android.internal.view.menu.IconMenuItemView)10 ListMenuItemView (com.android.internal.view.menu.ListMenuItemView)10 MenuView (com.android.internal.view.menu.MenuView)10 ViewGroup (android.view.ViewGroup)7