Search in sources :

Example 66 with BridgeContext

use of com.android.layoutlib.bridge.android.BridgeContext in project android_frameworks_base by ParanoidAndroid.

the class RenderAction method init.

/**
     * Initializes and acquires the scene, creating various Android objects such as context,
     * inflater, and parser.
     *
     * @param timeout the time to wait if another rendering is happening.
     *
     * @return whether the scene was prepared
     *
     * @see #acquire(long)
     * @see #release()
     */
public Result init(long timeout) {
    // acquire the lock. if the result is null, lock was just acquired, otherwise, return
    // the result.
    Result result = acquireLock(timeout);
    if (result != null) {
        return result;
    }
    HardwareConfig hardwareConfig = mParams.getHardwareConfig();
    // setup the display Metrics.
    DisplayMetrics metrics = new DisplayMetrics();
    metrics.densityDpi = metrics.noncompatDensityDpi = hardwareConfig.getDensity().getDpiValue();
    metrics.density = metrics.noncompatDensity = metrics.densityDpi / (float) DisplayMetrics.DENSITY_DEFAULT;
    metrics.scaledDensity = metrics.noncompatScaledDensity = metrics.density;
    metrics.widthPixels = metrics.noncompatWidthPixels = hardwareConfig.getScreenWidth();
    metrics.heightPixels = metrics.noncompatHeightPixels = hardwareConfig.getScreenHeight();
    metrics.xdpi = metrics.noncompatXdpi = hardwareConfig.getXdpi();
    metrics.ydpi = metrics.noncompatYdpi = hardwareConfig.getYdpi();
    RenderResources resources = mParams.getResources();
    // build the context
    mContext = new BridgeContext(mParams.getProjectKey(), metrics, resources, mParams.getProjectCallback(), getConfiguration(), mParams.getTargetSdkVersion());
    setUp();
    return SUCCESS.createResult();
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) RenderResources(com.android.ide.common.rendering.api.RenderResources) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) DisplayMetrics(android.util.DisplayMetrics) Result(com.android.ide.common.rendering.api.Result)

Example 67 with BridgeContext

use of com.android.layoutlib.bridge.android.BridgeContext in project android_frameworks_base by ParanoidAndroid.

the class RenderDrawable method render.

public Result render() {
    checkLock();
    try {
        // get the drawable resource value
        DrawableParams params = getParams();
        HardwareConfig hardwareConfig = params.getHardwareConfig();
        ResourceValue drawableResource = params.getDrawable();
        // resolve it
        BridgeContext context = getContext();
        drawableResource = context.getRenderResources().resolveResValue(drawableResource);
        if (drawableResource == null || drawableResource.getResourceType() != ResourceType.DRAWABLE) {
            return Status.ERROR_NOT_A_DRAWABLE.createResult();
        }
        // create a simple FrameLayout
        FrameLayout content = new FrameLayout(context);
        // get the actual Drawable object to draw
        Drawable d = ResourceHelper.getDrawable(drawableResource, context);
        content.setBackground(d);
        // set the AttachInfo on the root view.
        AttachInfo_Accessor.setAttachInfo(content);
        // measure
        int w = hardwareConfig.getScreenWidth();
        int h = hardwareConfig.getScreenHeight();
        int w_spec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY);
        int h_spec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
        content.measure(w_spec, h_spec);
        // now do the layout.
        content.layout(0, 0, w, h);
        // preDraw setup
        AttachInfo_Accessor.dispatchOnPreDraw(content);
        // draw into a new image
        BufferedImage image = getImage(w, h);
        // create an Android bitmap around the BufferedImage
        Bitmap bitmap = Bitmap_Delegate.createBitmap(image, true, /*isMutable*/
        hardwareConfig.getDensity());
        // create a Canvas around the Android bitmap
        Canvas canvas = new Canvas(bitmap);
        canvas.setDensity(hardwareConfig.getDensity().getDpiValue());
        // and draw
        content.draw(canvas);
        return Status.SUCCESS.createResult(image);
    } catch (IOException e) {
        return ERROR_UNKNOWN.createResult(e.getMessage(), e);
    }
}
Also used : Bitmap(android.graphics.Bitmap) HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) FrameLayout(android.widget.FrameLayout) Canvas(android.graphics.Canvas) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Example 68 with BridgeContext

use of com.android.layoutlib.bridge.android.BridgeContext in project android_frameworks_base by ParanoidAndroid.

the class CustomBar method getResourceValue.

private ResourceValue getResourceValue(String reference) {
    BridgeContext bridgeContext = (BridgeContext) mContext;
    RenderResources res = bridgeContext.getRenderResources();
    // find the resource
    ResourceValue value = res.findResValue(reference, false);
    // resolve it if needed
    return res.resolveResValue(value);
}
Also used : RenderResources(com.android.ide.common.rendering.api.RenderResources) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext)

Example 69 with BridgeContext

use of com.android.layoutlib.bridge.android.BridgeContext in project android_frameworks_base by ParanoidAndroid.

the class HandlerThread_Delegate method run.

// -------- Delegate methods
@LayoutlibDelegate
static /*package*/
void run(HandlerThread theThread) {
    // record the thread so that it can be quit() on clean up.
    BridgeContext context = RenderAction.getCurrentContext();
    List<HandlerThread> list = sThreads.get(context);
    if (list == null) {
        list = new ArrayList<HandlerThread>();
        sThreads.put(context, list);
    }
    list.add(theThread);
    // ---- START DEFAULT IMPLEMENTATION.
    theThread.mTid = Process.myTid();
    Looper.prepare();
    synchronized (theThread) {
        theThread.mLooper = Looper.myLooper();
        theThread.notifyAll();
    }
    Process.setThreadPriority(theThread.mPriority);
    theThread.onLooperPrepared();
    Looper.loop();
    theThread.mTid = -1;
}
Also used : BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 70 with BridgeContext

use of com.android.layoutlib.bridge.android.BridgeContext in project android_frameworks_base by ParanoidAndroid.

the class BridgeInflater method setupViewInContext.

private void setupViewInContext(View view, AttributeSet attrs) {
    if (getContext() instanceof BridgeContext) {
        BridgeContext bc = (BridgeContext) getContext();
        if (attrs instanceof BridgeXmlBlockParser) {
            BridgeXmlBlockParser parser = (BridgeXmlBlockParser) attrs;
            // get the view key
            Object viewKey = parser.getViewCookie();
            if (viewKey == null) {
                int currentDepth = parser.getDepth();
                // test whether we are in an included file or in a adapter binding view.
                BridgeXmlBlockParser previousParser = bc.getPreviousParser();
                if (previousParser != null) {
                    // looks like we inside an embedded layout.
                    // only apply the cookie of the calling node (<include>) if we are at the
                    // top level of the embedded layout. If there is a merge tag, then
                    // skip it and look for the 2nd level
                    int testDepth = mIsInMerge ? 2 : 1;
                    if (currentDepth == testDepth) {
                        viewKey = previousParser.getViewCookie();
                        // if we are in a merge, wrap the cookie in a MergeCookie.
                        if (viewKey != null && mIsInMerge) {
                            viewKey = new MergeCookie(viewKey);
                        }
                    }
                } else if (mResourceReference != null && currentDepth == 1) {
                    // else if there's a resource reference, this means we are in an adapter
                    // binding case. Set the resource ref as the view cookie only for the top
                    // level view.
                    viewKey = mResourceReference;
                }
            }
            if (viewKey != null) {
                bc.addViewKey(view, viewKey);
            }
        }
    }
}
Also used : BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) MergeCookie(com.android.ide.common.rendering.api.MergeCookie) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser)

Aggregations

BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)112 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)40 Context (android.content.Context)31 View (android.view.View)24 AdapterView (android.widget.AdapterView)24 RenderResources (com.android.ide.common.rendering.api.RenderResources)24 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)24 SessionParams (com.android.ide.common.rendering.api.SessionParams)23 ActionMenuItemView (com.android.internal.view.menu.ActionMenuItemView)20 IconMenuItemView (com.android.internal.view.menu.IconMenuItemView)20 ListMenuItemView (com.android.internal.view.menu.ListMenuItemView)20 AbsListView (android.widget.AbsListView)18 ExpandableListView (android.widget.ExpandableListView)18 ListView (android.widget.ListView)18 Result (com.android.ide.common.rendering.api.Result)18 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)17 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)16 ActionMenuView (android.widget.ActionMenuView)15 MenuView (com.android.internal.view.menu.MenuView)15 Drawable (android.graphics.drawable.Drawable)13