Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 55acd60b authored by Xavier Ducrohet's avatar Xavier Ducrohet
Browse files

Layoutlib: support viewkey in include nodes.

When an include tag is parsed, the custom bridge
code never has access to both parsers at the same
time.
The child parser is created out of an in (representing
the layout id), and the code inflating the content of
the include layout doesn't see the parent parser either.

This changeset adds a parser stack in the BridgeContext
in order to allow access to the parent parser when setting
the viewkey. This is only used if the current parser depth
is 1 (top node), as we only want to set the include
node to the top node of the included layout.

Change-Id: I2ac3b72a0c84a269d9019f44f98cbc0b615ab959
parent 60899fac
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import java.io.InputStream;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Stack;
import java.util.TreeMap;
import java.util.Map.Entry;

@@ -99,6 +100,8 @@ public final class BridgeContext extends Activity {
    private final ILayoutLog mLogger;
    private BridgeContentResolver mContentResolver;

    private final Stack<BridgeXmlBlockParser> mParserStack = new Stack<BridgeXmlBlockParser>();

    /**
     * @param projectKey An Object identifying the project. This is used for the cache mechanism.
     * @param metrics the {@link DisplayMetrics}.
@@ -188,6 +191,21 @@ public final class BridgeContext extends Activity {
        return mDefaultPropMaps.get(key);
    }

    public void pushParser(BridgeXmlBlockParser parser) {
        mParserStack.push(parser);
    }

    public void popParser() {
        mParserStack.pop();
    }

    public BridgeXmlBlockParser getPreviousParser() {
        if (mParserStack.size() < 2) {
            return null;
        }
        return mParserStack.get(mParserStack.size() - 2);
    }

    // ------------- Activity Methods

    @Override
+38 −25
Original line number Diff line number Diff line
@@ -214,7 +214,20 @@ public final class BridgeInflater extends LayoutInflater {
        if (getContext() instanceof BridgeContext) {
            BridgeContext bc = (BridgeContext) getContext();
            if (attrs instanceof BridgeXmlBlockParser) {
                Object viewKey = ((BridgeXmlBlockParser) attrs).getViewKey();
                BridgeXmlBlockParser parser = (BridgeXmlBlockParser) attrs;

                // get the view key
                Object viewKey = parser.getViewKey();

                // if there's no view key and the depth is 1 (ie this is the first tag),
                // look for a previous parser in the context, and check if this one has a viewkey.
                if (viewKey == null && parser.getDepth() == 1) {
                    BridgeXmlBlockParser previousParser = bc.getPreviousParser();
                    if (previousParser != null) {
                        viewKey = previousParser.getViewKey();
                    }
                }

                if (viewKey != null) {
                    bc.addViewKey(view, viewKey);
                }
+13 −19
Original line number Diff line number Diff line
@@ -40,10 +40,9 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
    private XmlPullAttributes mAttrib;

    private boolean mStarted = false;
    private boolean mDecNextDepth = false;
    private int mDepth = 0;
    private int mEventType = START_DOCUMENT;
    private final boolean mPlatformFile;
    private final BridgeContext mContext;

    /**
     * Builds a {@link BridgeXmlBlockParser}.
@@ -53,8 +52,11 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
     */
    public BridgeXmlBlockParser(XmlPullParser parser, BridgeContext context, boolean platformFile) {
        mParser = parser;
        mContext = context;
        mPlatformFile = platformFile;
        mAttrib = new BridgeXmlPullAttributes(parser, context, mPlatformFile);

        mContext.pushParser(this);
    }

    public boolean isPlatformFile() {
@@ -69,7 +71,6 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
        return null;
    }

    
    // ------- XmlResourceParser implementation

    public void setFeature(String name, boolean state)
@@ -145,7 +146,7 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
    }

    public int getDepth() {
        return mDepth;
        return mParser.getDepth();
    }

    public String getText() {
@@ -236,17 +237,10 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
            return START_DOCUMENT;
        }
        int ev = mParser.next();
        if (mDecNextDepth) {
            mDepth--;
            mDecNextDepth = false;
        }
        switch (ev) {
        case START_TAG:
            mDepth++;
            break;
        case END_TAG:
            mDecNextDepth = true;
            break;

        if (ev == END_TAG && mParser.getDepth() == 1) {
            // done with parser remove it from the context stack.
            mContext.popParser();
        }
        mEventType = ev;
        return ev;