Search in sources :

Example 71 with BridgeContext

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

the class BridgeInflater method inflate.

@Override
public View inflate(int resource, ViewGroup root) {
    Context context = getContext();
    if (context instanceof BridgeContext) {
        BridgeContext bridgeContext = (BridgeContext) context;
        ResourceValue value = null;
        Pair<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource);
        if (layoutInfo != null) {
            value = bridgeContext.getRenderResources().getFrameworkResource(ResourceType.LAYOUT, layoutInfo.getSecond());
        } else {
            layoutInfo = mProjectCallback.resolveResourceId(resource);
            if (layoutInfo != null) {
                value = bridgeContext.getRenderResources().getProjectResource(ResourceType.LAYOUT, layoutInfo.getSecond());
            }
        }
        if (value != null) {
            File f = new File(value.getValue());
            if (f.isFile()) {
                try {
                    XmlPullParser parser = ParserFactory.create(f);
                    BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(parser, bridgeContext, false);
                    return inflate(bridgeParser, root);
                } catch (Exception e) {
                    Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + f.getAbsolutePath(), e, null);
                    return null;
                }
            }
        }
    }
    return null;
}
Also used : Context(android.content.Context) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) XmlPullParser(org.xmlpull.v1.XmlPullParser) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser) InflateException(android.view.InflateException)

Example 72 with BridgeContext

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

the class Bitmap_Delegate method createBitmap.

/**
     * Creates and returns a {@link Bitmap} initialized with the given file content.
     *
     * @param input the file from which to read the bitmap content
     * @param density the density associated with the bitmap
     *
     * @see Bitmap#isPremultiplied()
     * @see Bitmap#isMutable()
     * @see Bitmap#getDensity()
     */
private static Bitmap createBitmap(File input, Set<BitmapCreateFlags> createFlags, Density density) throws IOException {
    // create a delegate with the content of the file.
    BufferedImage image = ImageIO.read(input);
    if (image == null && input.exists()) {
        // There was a problem decoding the image, or the decoder isn't registered. Webp maybe.
        // Replace with a broken image icon.
        BridgeContext currentContext = RenderAction.getCurrentContext();
        if (currentContext != null) {
            RenderResources resources = currentContext.getRenderResources();
            ResourceValue broken = resources.getFrameworkResource(ResourceType.DRAWABLE, "ic_menu_report_image");
            File brokenFile = new File(broken.getValue());
            if (brokenFile.exists()) {
                image = ImageIO.read(brokenFile);
            }
        }
    }
    Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ARGB_8888);
    return createBitmap(delegate, createFlags, density.getDpiValue());
}
Also used : RenderResources(com.android.ide.common.rendering.api.RenderResources) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 73 with BridgeContext

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

the class Resources_Theme_Delegate method resolveStyle.

@Nullable
private static StyleResourceValue resolveStyle(int nativeResid) {
    if (nativeResid == 0) {
        return null;
    }
    BridgeContext context = RenderSessionImpl.getCurrentContext();
    ResourceReference theme = context.resolveId(nativeResid);
    if (theme.isFramework()) {
        return (StyleResourceValue) context.getRenderResources().getFrameworkResource(ResourceType.STYLE, theme.getName());
    } else {
        return (StyleResourceValue) context.getRenderResources().getProjectResource(ResourceType.STYLE, theme.getName());
    }
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) Nullable(android.annotation.Nullable)

Example 74 with BridgeContext

use of com.android.layoutlib.bridge.android.BridgeContext 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)

Example 75 with BridgeContext

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

the class RenderSessionImpl method animate.

/**
     * Animate an object
     * <p>
     * {@link #acquire(long)} must have been called before this.
     *
     * @throws IllegalStateException if the current context is different than the one owned by
     *      the scene, or if {@link #acquire(long)} was not called.
     *
     * @see RenderSession#animate(Object, String, boolean, IAnimationListener)
     */
public Result animate(Object targetObject, String animationName, boolean isFrameworkAnimation, IAnimationListener listener) {
    checkLock();
    BridgeContext context = getContext();
    // find the animation file.
    ResourceValue animationResource;
    int animationId = 0;
    if (isFrameworkAnimation) {
        animationResource = context.getRenderResources().getFrameworkResource(ResourceType.ANIMATOR, animationName);
        if (animationResource != null) {
            animationId = Bridge.getResourceId(ResourceType.ANIMATOR, animationName);
        }
    } else {
        animationResource = context.getRenderResources().getProjectResource(ResourceType.ANIMATOR, animationName);
        if (animationResource != null) {
            animationId = context.getLayoutlibCallback().getResourceId(ResourceType.ANIMATOR, animationName);
        }
    }
    if (animationResource != null) {
        try {
            Animator anim = AnimatorInflater.loadAnimator(context, animationId);
            if (anim != null) {
                anim.setTarget(targetObject);
                new PlayAnimationThread(anim, this, animationName, listener).start();
                return SUCCESS.createResult();
            }
        } catch (Exception 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);
        }
    }
    return ERROR_ANIM_NOT_FOUND.createResult();
}
Also used : Animator(android.animation.Animator) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext)

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