FileDocCategorySizeDatePackage
BridgeInflater.javaAPI DocAndroid 5.1 API10478Thu Mar 12 22:22:44 GMT 2015android.view

BridgeInflater

public final class BridgeInflater extends LayoutInflater
Custom implementation of {@link LayoutInflater} to handle custom views.

Fields Summary
private final com.android.ide.common.rendering.api.IProjectCallback
mProjectCallback
private boolean
mIsInMerge
private com.android.ide.common.rendering.api.ResourceReference
mResourceReference
private static final String[]
sClassPrefixList
List of class prefixes which are tried first by default.

This should match the list in com.android.internal.policy.impl.PhoneLayoutInflater.

Constructors Summary
protected BridgeInflater(LayoutInflater original, android.content.Context newContext)


         
        super(original, newContext);
        mProjectCallback = null;
    
public BridgeInflater(android.content.Context context, com.android.ide.common.rendering.api.IProjectCallback projectCallback)
Instantiate a new BridgeInflater with an {@link IProjectCallback} object.

param
context The Android application context.
param
projectCallback the {@link IProjectCallback} object.

        super(context);
        mProjectCallback = projectCallback;
        mConstructorArgs[0] = context;
    
Methods Summary
public LayoutInflatercloneInContext(android.content.Context newContext)

        return new BridgeInflater(this, newContext);
    
public ViewcreateViewFromTag(View parent, java.lang.String name, android.util.AttributeSet attrs, boolean inheritContext)

        View view = null;
        try {
            view = super.createViewFromTag(parent, name, attrs, inheritContext);
        } catch (InflateException e) {
            // try to load the class from using the custom view loader
            try {
                view = loadCustomView(name, attrs);
            } catch (Exception e2) {
                // Wrap the real exception in an InflateException so that the calling
                // method can deal with it.
                InflateException exception = new InflateException();
                if (e2.getClass().equals(ClassNotFoundException.class) == false) {
                    exception.initCause(e2);
                } else {
                    exception.initCause(e);
                }
                throw exception;
            }
        }

        setupViewInContext(view, attrs);

        return view;
    
static java.lang.ObjectgetViewKeyFromParser(android.util.AttributeSet attrs, com.android.layoutlib.bridge.android.BridgeContext bc, com.android.ide.common.rendering.api.ResourceReference resourceReference, boolean isInMerge)


        if (!(attrs instanceof BridgeXmlBlockParser)) {
            return null;
        }
        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 are 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 = isInMerge ? 2 : 1;
                if (currentDepth == testDepth) {
                    viewKey = previousParser.getViewCookie();
                    // if we are in a merge, wrap the cookie in a MergeCookie.
                    if (viewKey != null && isInMerge) {
                        viewKey = new MergeCookie(viewKey);
                    }
                }
            } else if (resourceReference != 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 = resourceReference;
            }
        }

        return viewKey;
    
public Viewinflate(int resource, ViewGroup root)

        Context context = getContext();
        while (context instanceof ContextThemeWrapper) {
            context = ((ContextThemeWrapper) context).getBaseContext();
        }
        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 /*data*/);

                        return null;
                    }
                }
            }
        }
        return null;
    
private ViewloadCustomView(java.lang.String name, android.util.AttributeSet attrs)

        if (mProjectCallback != null) {
            // first get the classname in case it's not the node name
            if (name.equals("view")) {
                name = attrs.getAttributeValue(null, "class");
            }

            mConstructorArgs[1] = attrs;

            Object customView = mProjectCallback.loadView(name, mConstructorSignature,
                    mConstructorArgs);

            if (customView instanceof View) {
                return (View)customView;
            }
        }

        return null;
    
public ViewonCreateView(java.lang.String name, android.util.AttributeSet attrs)

        View view = null;

        try {
            // First try to find a class using the default Android prefixes
            for (String prefix : sClassPrefixList) {
                try {
                    view = createView(name, prefix, attrs);
                    if (view != null) {
                        break;
                    }
                } catch (ClassNotFoundException e) {
                    // Ignore. We'll try again using the base class below.
                }
            }

            // Next try using the parent loader. This will most likely only work for
            // fully-qualified class names.
            try {
                if (view == null) {
                    view = super.onCreateView(name, attrs);
                }
            } catch (ClassNotFoundException e) {
                // Ignore. We'll try again using the custom view loader below.
            }

            // Finally try again using the custom view loader
            try {
                if (view == null) {
                    view = loadCustomView(name, attrs);
                }
            } catch (ClassNotFoundException e) {
                // If the class was not found, we throw the exception directly, because this
                // method is already expected to throw it.
                throw e;
            }
        } catch (Exception e) {
            // Wrap the real exception in a ClassNotFoundException, so that the calling method
            // can deal with it.
            ClassNotFoundException exception = new ClassNotFoundException("onCreateView", e);
            throw exception;
        }

        setupViewInContext(view, attrs);

        return view;
    
public voidsetIsInMerge(boolean isInMerge)

        mIsInMerge = isInMerge;
    
public voidsetResourceReference(com.android.ide.common.rendering.api.ResourceReference reference)

        mResourceReference = reference;
    
private voidsetupViewInContext(View view, android.util.AttributeSet attrs)

        Context context = getContext();
        while (context instanceof ContextThemeWrapper) {
            context = ((ContextThemeWrapper) context).getBaseContext();
        }
        if (context instanceof BridgeContext) {
            BridgeContext bc = (BridgeContext) context;
            // get the view key
            Object viewKey = getViewKeyFromParser(attrs, bc, mResourceReference, mIsInMerge);
            if (viewKey != null) {
                bc.addViewKey(view, viewKey);
            }
        }