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

Commit 9223b673 authored by Xavier Ducrohet's avatar Xavier Ducrohet
Browse files

Layoutlib: support for editing embedded layouts.

When Resources.getLayout(int) is called to return a parser
for an embedded layout, this queries the current parser for
a custom parser (Eclipse will provide one on top of the current
XML model being edited)

Change-Id: Ia9e837358f67daed0a835e1b3f4f50c0516ceee9
parent 7370ab5c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.graphics;

import com.android.layoutlib.api.ILayoutLog;
import com.android.layoutlib.bridge.impl.DelegateManager;
import com.android.layoutlib.bridge.impl.Stack;

import android.graphics.Paint_Delegate.FontInfo;
import android.text.TextUtils;
@@ -32,7 +33,6 @@ import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Stack;


/**
+20 −1
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import com.android.layoutlib.api.IResourceValue;
import com.android.layoutlib.api.IStyleResourceValue;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.BridgeConstants;
import com.android.layoutlib.bridge.impl.Stack;
import com.android.layoutlib.bridge.impl.TempResourceValue;

import android.app.Activity;
@@ -65,7 +66,6 @@ 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;

@@ -191,14 +191,33 @@ public final class BridgeContext extends Activity {
        return mDefaultPropMaps.get(key);
    }

    /**
     * Adds a parser to the stack.
     * @param parser the parser to add.
     */
    public void pushParser(BridgeXmlBlockParser parser) {
        mParserStack.push(parser);
    }

    /**
     * Removes the parser at the top of the stack
     */
    public void popParser() {
        mParserStack.pop();
    }

    /**
     * Returns the current parser at the top the of the stack.
     * @return a parser or null.
     */
    public BridgeXmlBlockParser getCurrentParser() {
        return mParserStack.peek();
    }

    /**
     * Returns the previous parser.
     * @return a parser or null if there isn't any previous parser
     */
    public BridgeXmlBlockParser getPreviousParser() {
        if (mParserStack.size() < 2) {
            return null;
+27 −14
Original line number Diff line number Diff line
@@ -220,24 +220,37 @@ public final class BridgeResources extends Resources {
        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            XmlPullParser parser = null;

            try {
                // check if the current parser can provide us with a custom parser.
                BridgeXmlBlockParser currentParser = mContext.getCurrentParser();
                if (currentParser != null) {
                    parser = currentParser.getParser(value.getName());
                }

                // create a new one manually if needed.
                if (parser == null) {
                    File xml = new File(value.getValue());
                    if (xml.isFile()) {
                        // we need to create a pull parser around the layout XML file, and then
                        // give that to our XmlBlockParser
                try {
                    KXmlParser parser = new KXmlParser();
                        parser = new KXmlParser();
                        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
                        parser.setInput(new FileReader(xml));
                    }
                }

                if (parser != null) {
                    return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]);
                }
            } catch (XmlPullParserException e) {
                mContext.getLogger().error(e);

                // we'll return null below.
            } catch (FileNotFoundException e) {
                // this shouldn't happen since we check above.
            }
            }

        }

        // id was not found or not resolved. Throw a NotFoundException.
+12 −2
Original line number Diff line number Diff line
@@ -56,13 +56,23 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
        mPlatformFile = platformFile;
        mAttrib = new BridgeXmlPullAttributes(parser, context, mPlatformFile);

        if (mContext != null) {
            mContext.pushParser(this);
        }
    }

    public boolean isPlatformFile() {
        return mPlatformFile;
    }

    public IXmlPullParser getParser(String layoutName) {
        if (mParser instanceof IXmlPullParser) {
            return ((IXmlPullParser)mParser).getParser(layoutName);
        }

        return null;
    }

    public Object getViewKey() {
        if (mParser instanceof IXmlPullParser) {
            return ((IXmlPullParser)mParser).getViewKey();
@@ -238,7 +248,7 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
        }
        int ev = mParser.next();

        if (ev == END_TAG && mParser.getDepth() == 1) {
        if (ev == END_TAG && mParser.getDepth() == 1 && mContext != null) {
            // done with parser remove it from the context stack.
            mContext.popParser();
        }
+70 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.layoutlib.bridge.impl;

import java.util.ArrayList;

/**
 * Custom Stack implementation on top of an {@link ArrayList} instead of
 * using {@link java.util.Stack} which is on top of a vector.
 *
 * @param <T>
 */
public class Stack<T> extends ArrayList<T> {

    private static final long serialVersionUID = 1L;

    public Stack() {
        super();
    }

    public Stack(int size) {
        super(size);
    }

    /**
     * Pushes the given object to the stack
     * @param object the object to push
     */
    public void push(T object) {
        add(object);
    }

    /**
     * Remove the object at the top of the stack and returns it.
     * @return the removed object or null if the stack was empty.
     */
    public T pop() {
        if (size() > 0) {
            return remove(size() - 1);
        }

        return null;
    }

    /**
     * Returns the object at the top of the stack.
     * @return the object at the top or null if the stack is empty.
     */
    public T peek() {
        if (size() > 0) {
            return get(size() - 1);
        }

        return null;
    }
}