diff options
author | Xavier Ducrohet <xav@android.com> | 2010-12-15 16:08:57 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2010-12-21 10:27:22 -0800 |
commit | 95b17a5e62eeeb7b38ef668508df43a1ee9e0880 (patch) | |
tree | ca77258cf072446e38d2a68acab77aac8dd3a73e /layoutlib_api/src/com/android/ide/common/rendering/api | |
parent | 77e0fdebe08d39f550544571989649533d043223 (diff) | |
download | sdk-95b17a5e62eeeb7b38ef668508df43a1ee9e0880.zip sdk-95b17a5e62eeeb7b38ef668508df43a1ee9e0880.tar.gz sdk-95b17a5e62eeeb7b38ef668508df43a1ee9e0880.tar.bz2 |
LayoutLib API refactoring
Change-Id: I40abba4c4f786755c2a1c0e70df4d7bc08e2bcde
Diffstat (limited to 'layoutlib_api/src/com/android/ide/common/rendering/api')
15 files changed, 1469 insertions, 0 deletions
diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/Bridge.java b/layoutlib_api/src/com/android/ide/common/rendering/api/Bridge.java new file mode 100644 index 0000000..b2c2109 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/Bridge.java @@ -0,0 +1,93 @@ +/* + * 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.ide.common.rendering.api; + + +import java.io.File; +import java.util.EnumSet; +import java.util.Map; + +/** + * Entry point of the Layout Library. Extensions of this class provide a method to compute + * and render a layout. + */ +public abstract class Bridge { + + public final static int API_CURRENT = 5; + + /** + * Returns the API level of the layout library. + * <p/> + * While no methods will ever be removed, some may become deprecated, and some new ones + * will appear. + * <p/>All Layout libraries based on {@link Bridge} return at minimum an API level of 5. + */ + public abstract int getApiLevel(); + + /** + * Returns an {@link EnumSet} of the supported {@link Capability}. + * @return an {@link EnumSet} with the supported capabilities. + * + */ + public EnumSet<Capability> getCapabilities() { + return EnumSet.noneOf(Capability.class); + } + + /** + * Initializes the Bridge object. + * + * @param fontLocation the location of the fonts. + * @param enumValueMap map attrName => { map enumFlagName => Integer value }. + * @return true if success. + */ + public boolean init(File fontLocation, Map<String, Map<String, Integer>> enumValueMap) { + return false; + } + + /** + * Prepares the layoutlib to unloaded. + */ + public boolean dispose() { + return false; + } + + /** + * Starts a layout session by inflating and rendering it. The method returns a + * {@link RenderSession} on which further actions can be taken. + * + * @return a new {@link RenderSession} object that contains the result of the scene creation and + * first rendering. + */ + public RenderSession createSession(Params params) { + return null; + } + + /** + * Clears the resource cache for a specific project. + * <p/>This cache contains bitmaps and nine patches that are loaded from the disk and reused + * until this method is called. + * <p/>The cache is not configuration dependent and should only be cleared when a + * resource changes (at this time only bitmaps and 9 patches go into the cache). + * <p/> + * The project key provided must be similar to the one passed in {@link Params}. + * + * @param projectKey the key for the project. + */ + public void clearCaches(Object projectKey) { + + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/Capability.java b/layoutlib_api/src/com/android/ide/common/rendering/api/Capability.java new file mode 100644 index 0000000..abbab45 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/Capability.java @@ -0,0 +1,47 @@ +/* + * 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.ide.common.rendering.api; + +/** + * Enum describing the layout bridge capabilities. + * + */ +public enum Capability { + /** Ability to render at full size, as required by the layout, and unbound by the screen */ + UNBOUND_RENDERING, + /** Ability to override the background of the rendering with transparency using + * {@link SceneParams#setCustomBackgroundColor(int)} */ + TRANSPARENCY, + /** Ability to call {@link LayoutScene#render()} and {@link LayoutScene#render(long)}. */ + RENDER, + /** + * Ability to control embedded layout parsers through {@link IXmlPullParser#getParser(String)} + */ + EMBEDDED_LAYOUT, + /** Ability to call<br> + * {@link LayoutScene#insertChild(Object, IXmlPullParser, int, com.android.layoutlib.api.LayoutScene.IAnimationListener)}<br> + * {@link LayoutScene#moveChild(Object, Object, int, java.util.Map, com.android.layoutlib.api.LayoutScene.IAnimationListener)}<br> + * {@link LayoutScene#removeChild(Object, com.android.layoutlib.api.LayoutScene.IAnimationListener)}<br> + * {@link LayoutScene#setProperty(Object, String, String)} + * */ + VIEW_MANIPULATION, + /** Ability to call<br> + * {@link LayoutScene#animate(Object, String, boolean, com.android.layoutlib.api.LayoutScene.IAnimationListener)} + * <p>If the bridge also supports {@link #VIEW_MANIPULATION} then those methods can use + * an {@link com.android.layoutlib.api.LayoutScene.IAnimationListener}, otherwise they won't. */ + ANIMATE; +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/DensityBasedResourceValue.java b/layoutlib_api/src/com/android/ide/common/rendering/api/DensityBasedResourceValue.java new file mode 100644 index 0000000..d190b62 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/DensityBasedResourceValue.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2008 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.ide.common.rendering.api; + +import com.android.layoutlib.api.IDensityBasedResourceValue; + +@SuppressWarnings("deprecation") +public class DensityBasedResourceValue extends ResourceValue implements IDensityBasedResourceValue { + + private ResourceDensity mDensity; + + public DensityBasedResourceValue(String type, String name, String value, + ResourceDensity density, boolean isFramework) { + super(type, name, value, isFramework); + mDensity = density; + } + + /** + * Returns the density for which this resource is configured. + * @return the density. + */ + public ResourceDensity getResourceDensity() { + return mDensity; + } + + /** Legacy method, do not call + * @deprecated use {@link #getResourceDensity()} instead. + */ + public Density getDensity() { + return Density.getEnum(mDensity.getDpi()); + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/IAnimationListener.java b/layoutlib_api/src/com/android/ide/common/rendering/api/IAnimationListener.java new file mode 100644 index 0000000..81a2320 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/IAnimationListener.java @@ -0,0 +1,48 @@ +/* + * 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.ide.common.rendering.api; + + +import java.awt.image.BufferedImage; + +public interface IAnimationListener { + /** + * Called when a new animation frame is available for display. + * + * <p>The {@link RenderSession} object is provided as a convenience. It should be queried + * for the image through {@link RenderSession#getImage()}. + * + * <p>If no {@link IImageFactory} is used, then each new animation frame will be rendered + * in its own new {@link BufferedImage} object. However if an image factory is used, and it + * always re-use the same object, then the image is only guaranteed to be valid during + * this method call. As soon as this method return the image content will be overridden + * with new drawing. + * + */ + void onNewFrame(RenderSession scene); + + /** + * Called when the animation is done playing. + */ + void done(Result result); + + /** + * Return true to cancel the animation. + */ + boolean isCanceled(); + +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/IImageFactory.java b/layoutlib_api/src/com/android/ide/common/rendering/api/IImageFactory.java new file mode 100644 index 0000000..c05c7c6 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/IImageFactory.java @@ -0,0 +1,42 @@ +/* + * 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.ide.common.rendering.api; + + +import java.awt.image.BufferedImage; + +/** + * Image Factory Interface. + * + * An Image factory's task is to create the {@link BufferedImage} into which the scene will be + * rendered. The goal is to let the layoutlib caller create an image that's optimized for its use + * case. + * + * If no factory is passed in {@link Params#setImageFactory(IImageFactory)}, then a default + * {@link BufferedImage} of type {@link BufferedImage#TYPE_INT_ARGB} is created. + * + */ +public interface IImageFactory { + + /** + * Creates a buffered image with the given size + * @param width the width of the image + * @param height the height of the image + * @return a new (or reused) BufferedImage of the given size. + */ + BufferedImage getImage(int width, int height); +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/ILayoutPullParser.java b/layoutlib_api/src/com/android/ide/common/rendering/api/ILayoutPullParser.java new file mode 100644 index 0000000..4b033d9 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/ILayoutPullParser.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2008 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.ide.common.rendering.api; + +import org.xmlpull.v1.XmlPullParser; + +/** + * Extended version of {@link XmlPullParser} to use with + * {@link Bridge#createScene(SceneParams)} + */ +public interface ILayoutPullParser extends XmlPullParser { + + /** + * Returns a cookie for the current XML node. + * <p/>This cookie will be passed back in the {@link ViewInfo} objects, allowing association + * of a particular XML node with its result from the layout computation. + * + * @see ViewInfo#getCookie() + */ + Object getViewCookie(); + + /** + * Returns a custom parser for the layout of the given name. + * @param layoutName the name of the layout. + * @return returns a custom parser or null if no custom parsers are needed. + * + * @since 5 + */ + ILayoutPullParser getParser(String layoutName); +} + diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/IProjectCallback.java b/layoutlib_api/src/com/android/ide/common/rendering/api/IProjectCallback.java new file mode 100644 index 0000000..67e082e --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/IProjectCallback.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2008 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.ide.common.rendering.api; + +/** + * Callback for project information needed by the Layout Library. + * Classes implementing this interface provide methods giving access to some project data, like + * resource resolution, namespace information, and instantiation of custom view. + */ +public interface IProjectCallback { + + /** + * Loads a custom view with the given constructor signature and arguments. + * @param name The fully qualified name of the class. + * @param constructorSignature The signature of the class to use + * @param constructorArgs The arguments to use on the constructor + * @return A newly instantiated android.view.View object. + * @throws ClassNotFoundException + * @throws Exception + */ + @SuppressWarnings("unchecked") + Object loadView(String name, Class[] constructorSignature, Object[] constructorArgs) + throws ClassNotFoundException, Exception; + + /** + * Returns the namespace of the application. + * <p/>This lets the Layout Lib load custom attributes for custom views. + */ + String getNamespace(); + + /** + * Resolves the id of a resource Id. + * <p/>The resource id is the value of a <code>R.<type>.<name></code>, and + * this method will return both the type and name of the resource. + * @param id the Id to resolve. + * @return an array of 2 strings containing the resource name and type, or null if the id + * does not match any resource. + */ + String[] resolveResourceValue(int id); + + /** + * Resolves the id of a resource Id of type int[] + * <p/>The resource id is the value of a R.styleable.<name>, and this method will + * return the name of the resource. + * @param id the Id to resolve. + * @return the name of the resource or <code>null</code> if not found. + */ + String resolveResourceValue(int[] id); + + /** + * Returns the id of a resource. + * <p/>The provided type and name must match an existing constant defined as + * <code>R.<type>.<name></code>. + * @param type the type of the resource + * @param name the name of the resource + * @return an Integer containing the resource Id, or <code>null</code> if not found. + */ + Integer getResourceValue(String type, String name); + +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/LayoutLog.java b/layoutlib_api/src/com/android/ide/common/rendering/api/LayoutLog.java new file mode 100644 index 0000000..a735f81 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/LayoutLog.java @@ -0,0 +1,42 @@ +/* + * 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.ide.common.rendering.api; + +public class LayoutLog { + + public void error(String tag, String message) { + } + + public void error(String tag, Throwable t) { + } + + public void warning(String tag, String message) { + } + + /** + * Logs an error message and a {@link Throwable}. + * @param message the message to log. + * @param throwable the {@link Throwable} to log. + */ + public void error(String tag, String message, Throwable throwable) { + + } + + public void fidelityWarning(String tag, String message, Throwable throwable) { + + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/Params.java b/layoutlib_api/src/com/android/ide/common/rendering/api/Params.java new file mode 100644 index 0000000..59e790e --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/Params.java @@ -0,0 +1,228 @@ +/* + * 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.ide.common.rendering.api; + +import java.util.Map; + +public class Params { + + public final static long DEFAULT_TIMEOUT = 250; //ms + + public static enum RenderingMode { + NORMAL(false, false), + V_SCROLL(false, true), + H_SCROLL(true, false), + FULL_EXPAND(true, true); + + private final boolean mHorizExpand; + private final boolean mVertExpand; + + private RenderingMode(boolean horizExpand, boolean vertExpand) { + mHorizExpand = horizExpand; + mVertExpand = vertExpand; + } + + public boolean isHorizExpand() { + return mHorizExpand; + } + + public boolean isVertExpand() { + return mVertExpand; + } + } + + private ILayoutPullParser mLayoutDescription; + private Object mProjectKey; + private int mScreenWidth; + private int mScreenHeight; + private RenderingMode mRenderingMode; + private int mDensity; + private float mXdpi; + private float mYdpi; + private String mThemeName; + private boolean mIsProjectTheme; + private Map<String, Map<String, ResourceValue>> mProjectResources; + private Map<String, Map<String, ResourceValue>> mFrameworkResources; + private IProjectCallback mProjectCallback; + private LayoutLog mLog; + + private boolean mCustomBackgroundEnabled; + private int mCustomBackgroundColor; + private long mTimeout; + + private IImageFactory mImageFactory = null; + + /** + * + * @param layoutDescription the {@link ILayoutPullParser} letting the LayoutLib Bridge visit the + * layout file. + * @param projectKey An Object identifying the project. This is used for the cache mechanism. + * @param screenWidth the screen width + * @param screenHeight the screen height + * @param renderingMode The rendering mode. + * @param density the density factor for the screen. + * @param xdpi the screen actual dpi in X + * @param ydpi the screen actual dpi in Y + * @param themeName The name of the theme to use. + * @param isProjectTheme true if the theme is a project theme, false if it is a framework theme. + * @param projectResources the resources of the project. The map contains (String, map) pairs + * where the string is the type of the resource reference used in the layout file, and the + * map contains (String, {@link ResourceValue}) pairs where the key is the resource name, + * and the value is the resource value. + * @param frameworkResources the framework resources. The map contains (String, map) pairs + * where the string is the type of the resource reference used in the layout file, and the map + * contains (String, {@link ResourceValue}) pairs where the key is the resource name, and the + * value is the resource value. + * @param projectCallback The {@link IProjectCallback} object to get information from + * the project. + * @param log the object responsible for displaying warning/errors to the user. + */ + public Params(ILayoutPullParser layoutDescription, + Object projectKey, + int screenWidth, int screenHeight, RenderingMode renderingMode, + int density, float xdpi, float ydpi, + String themeName, boolean isProjectTheme, + Map<String, Map<String, ResourceValue>> projectResources, + Map<String, Map<String, ResourceValue>> frameworkResources, + IProjectCallback projectCallback, LayoutLog log) { + mLayoutDescription = layoutDescription; + mProjectKey = projectKey; + mScreenWidth = screenWidth; + mScreenHeight = screenHeight; + mRenderingMode = renderingMode; + mDensity = density; + mXdpi = xdpi; + mYdpi = ydpi; + mThemeName = themeName; + mIsProjectTheme = isProjectTheme; + mProjectResources = projectResources; + mFrameworkResources = frameworkResources; + mProjectCallback = projectCallback; + mLog = log; + mCustomBackgroundEnabled = false; + mTimeout = DEFAULT_TIMEOUT; + } + + /** + * Copy constructor. + */ + public Params(Params params) { + mLayoutDescription = params.mLayoutDescription; + mProjectKey = params.mProjectKey; + mScreenWidth = params.mScreenWidth; + mScreenHeight = params.mScreenHeight; + mRenderingMode = params.mRenderingMode; + mDensity = params.mDensity; + mXdpi = params.mXdpi; + mYdpi = params.mYdpi; + mThemeName = params.mThemeName; + mIsProjectTheme = params.mIsProjectTheme; + mProjectResources = params.mProjectResources; + mFrameworkResources = params.mFrameworkResources; + mProjectCallback = params.mProjectCallback; + mLog = params.mLog; + mCustomBackgroundEnabled = params.mCustomBackgroundEnabled; + mCustomBackgroundColor = params.mCustomBackgroundColor; + mTimeout = params.mTimeout; + mImageFactory = params.mImageFactory; + } + + public void setOverrideBgColor(int color) { + mCustomBackgroundEnabled = true; + mCustomBackgroundColor = color; + } + + public void setTimeout(long timeout) { + mTimeout = timeout; + } + + public void setImageFactory(IImageFactory imageFactory) { + mImageFactory = imageFactory; + } + + public ILayoutPullParser getLayoutDescription() { + return mLayoutDescription; + } + + public Object getProjectKey() { + return mProjectKey; + } + + public int getScreenWidth() { + return mScreenWidth; + } + + public int getScreenHeight() { + return mScreenHeight; + } + + public RenderingMode getRenderingMode() { + return mRenderingMode; + } + + public int getDensity() { + return mDensity; + } + + public float getXdpi() { + return mXdpi; + } + + public float getYdpi() { + return mYdpi; + } + + public String getThemeName() { + return mThemeName; + } + + public boolean isProjectTheme() { + return mIsProjectTheme; + } + + public Map<String, Map<String, ResourceValue>> getProjectResources() { + return mProjectResources; + } + + public Map<String, Map<String, ResourceValue>> getFrameworkResources() { + return mFrameworkResources; + } + + public IProjectCallback getProjectCallback() { + return mProjectCallback; + } + + public LayoutLog getLog() { + return mLog; + } + + public boolean isBgColorOverridden() { + return mCustomBackgroundEnabled; + } + + public int getOverrideBgColor() { + return mCustomBackgroundColor; + } + + public long getTimeout() { + return mTimeout; + } + + public IImageFactory getImageFactory() { + return mImageFactory; + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/RenderSession.java b/layoutlib_api/src/com/android/ide/common/rendering/api/RenderSession.java new file mode 100644 index 0000000..12a28de --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/RenderSession.java @@ -0,0 +1,256 @@ +/* + * 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.ide.common.rendering.api; + +import static com.android.ide.common.rendering.api.Result.Status.NOT_IMPLEMENTED; + +import com.android.ide.common.rendering.api.Result.Status; + +import java.awt.image.BufferedImage; +import java.util.Map; + +/** + * An object allowing interaction with an Android layout. + * + * This is returned by {@link Bridge#createScene(Params)}. + * and can then be used for subsequent actions on the layout. + * + * @since 5 + * + */ +public class RenderSession { + + /** + * Returns the last operation result. + */ + public Result getResult() { + return NOT_IMPLEMENTED.createResult(); + } + + /** + * Returns the {@link ViewInfo} object for the top level view. + * <p> + * + * This is reset to a new instance every time {@link #render()} is called and can be + * <code>null</code> if the call failed (and the method returned a {@link Result} with + * {@link Status#ERROR_UNKNOWN} or {@link Status#NOT_IMPLEMENTED}. + * <p/> + * This can be safely modified by the caller. + */ + public ViewInfo getRootView() { + return null; + } + + /** + * Returns the rendering of the full layout. + * <p> + * This is reset to a new instance every time {@link #render()} is called and can be + * <code>null</code> if the call failed (and the method returned a {@link Result} with + * {@link Status#ERROR_UNKNOWN} or {@link Status#NOT_IMPLEMENTED}. + * <p/> + * This can be safely modified by the caller. + */ + public BufferedImage getImage() { + return null; + } + + + /** + * Returns a map of (XML attribute name, attribute value) containing only default attribute + * values, for the given view Object. + * @param viewObject the view object. + * @return a map of the default property values or null. + */ + public Map<String, String> getDefaultProperties(Object viewObject) { + return null; + } + + /** + * Re-renders the layout as-is. + * In case of success, this should be followed by calls to {@link #getRootView()} and + * {@link #getImage()} to access the result of the rendering. + * + * This is equivalent to calling <code>render(SceneParams.DEFAULT_TIMEOUT)</code> + * + * @return a {@link Result} indicating the status of the action. + */ + public Result render() { + return render(Params.DEFAULT_TIMEOUT); + } + + /** + * Re-renders the layout as-is, with a given timeout in case other renderings are being done. + * In case of success, this should be followed by calls to {@link #getRootView()} and + * {@link #getImage()} to access the result of the rendering. + * + * The {@link Bridge} is only able to inflate or render one layout at a time. There + * is an internal lock object whenever such an action occurs. The timeout parameter is used + * when attempting to acquire the lock. If the timeout expires, the method will return + * {@link Status#ERROR_TIMEOUT}. + * + * @param timeout timeout for the rendering, in milliseconds. + * + * @return a {@link Result} indicating the status of the action. + */ + public Result render(long timeout) { + return NOT_IMPLEMENTED.createResult(); + } + + /** + * Sets the value of a given property on a given object. + * <p/> + * This does nothing more than change the property. To render the scene in its new state, a + * call to {@link #render()} is required. + * <p/> + * Any amount of actions can be taken on the scene before {@link #render()} is called. + * + * @param objectView + * @param propertyName + * @param propertyValue + * + * @return a {@link Result} indicating the status of the action. + * + * @throws IllegalArgumentException if the view object is not an android.view.View + */ + public Result setProperty(Object objectView, String propertyName, String propertyValue) { + return NOT_IMPLEMENTED.createResult(); + } + + /** + * returns the value of a given property on a given object. + * <p/> + * This returns a {@link Result} object. If the operation of querying the object for its + * property was successful (check {@link Result#isSuccess()}), then the property value + * is set in the result and can be accessed through {@link Result#getData()}. + * + * @param objectView + * @param propertyName + * + * @return a {@link Result} indicating the status of the action. + * + * @throws IllegalArgumentException if the view object is not an android.view.View + */ + public Result getProperty(Object objectView, String propertyName) { + return NOT_IMPLEMENTED.createResult(); + } + + /** + * Inserts a new child in a ViewGroup object, and renders the result. + * <p/> + * The child is first inflated and then added to its new parent, at the given <var>index<var> + * position. If the <var>index</var> is -1 then the child is added at the end of the parent. + * <p/> + * If an animation listener is passed then the rendering is done asynchronously and the + * result is sent to the listener. + * If the listener is null, then the rendering is done synchronously. + * <p/> + * The child stays in the view hierarchy after the rendering is done. To remove it call + * {@link #removeChild(Object, IAnimationListener)} + * <p/> + * The returned {@link Result} object will contain the android.view.View object for + * the newly inflated child. It is accessible through {@link Result#getData()}. + * + * @param parentView the parent View object to receive the new child. + * @param childXml an {@link ILayoutPullParser} containing the content of the new child, + * including ViewGroup.LayoutParams attributes. + * @param index the index at which position to add the new child into the parent. -1 means at + * the end. + * @param listener an optional {@link IAnimationListener}. + * + * @return a {@link Result} indicating the status of the action. + */ + public Result insertChild(Object parentView, ILayoutPullParser childXml, int index, + IAnimationListener listener) { + return NOT_IMPLEMENTED.createResult(); + } + + /** + * Move a new child to a different ViewGroup object. + * <p/> + * The child is first removed from its current parent, and then added to its new parent, at the + * given <var>index<var> position. In case the <var>parentView</var> is the current parent of + * <var>childView</var> then the index must be the value with the <var>childView</var> removed + * from its parent. If the <var>index</var> is -1 then the child is added at the end of + * the parent. + * <p/> + * If an animation listener is passed then the rendering is done asynchronously and the + * result is sent to the listener. + * If the listener is null, then the rendering is done synchronously. + * <p/> + * The child stays in the view hierarchy after the rendering is done. To remove it call + * {@link #removeChild(Object, IAnimationListener)} + * <p/> + * The returned {@link Result} object will contain the android.view.ViewGroup.LayoutParams + * object created from the <var>layoutParams</var> map if it was non <code>null</code>. + * + * @param parentView the parent View object to receive the child. Can be the current parent + * already. + * @param childView the view to move. + * @param index the index at which position to add the new child into the parent. -1 means at + * the end. + * @param layoutParams an optional map of new ViewGroup.LayoutParams attribute. If non null, + * then the current layout params of the view will be removed and a new one will + * be inflated and set with the content of the map. + * @param listener an optional {@link IAnimationListener}. + * + * @return a {@link Result} indicating the status of the action. + */ + public Result moveChild(Object parentView, Object childView, int index, + Map<String, String> layoutParams, IAnimationListener listener) { + return NOT_IMPLEMENTED.createResult(); + } + + /** + * Removes a child from a ViewGroup object. + * <p/> + * This does nothing more than change the layout. To render the scene in its new state, a + * call to {@link #render()} is required. + * <p/> + * Any amount of actions can be taken on the scene before {@link #render()} is called. + * + * @param childView the view object to remove from its parent + * @param listener an optional {@link IAnimationListener}. + * + * @return a {@link Result} indicating the status of the action. + */ + public Result removeChild(Object childView, IAnimationListener listener) { + return NOT_IMPLEMENTED.createResult(); + } + + /** + * Starts playing an given animation on a given object. + * <p/> + * The animation playback is asynchronous and the rendered frame is sent vi the + * <var>listener</var>. + * + * @param targetObject the view object to animate + * @param animationName the name of the animation (res/anim) to play. + * @param listener the listener callback. + * + * @return a {@link Result} indicating the status of the action. + */ + public Result animate(Object targetObject, String animationName, + boolean isFrameworkAnimation, IAnimationListener listener) { + return NOT_IMPLEMENTED.createResult(); + } + + /** + * Discards the layout. No more actions can be called on this object. + */ + public void dispose() { + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/ResourceDensity.java b/layoutlib_api/src/com/android/ide/common/rendering/api/ResourceDensity.java new file mode 100644 index 0000000..ca92eae --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/ResourceDensity.java @@ -0,0 +1,59 @@ +/* + * 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.ide.common.rendering.api; + +/** + * Enum representing the density class of Android resources. + */ +public enum ResourceDensity { + XHIGH(320), + HIGH(240), + MEDIUM(160), + LOW(120), + NODPI(0); + + public final static int DEFAULT_DENSITY = 160; + + private final int mDpi; + + ResourceDensity(int dpi) { + mDpi = dpi; + } + + /** + * Returns the dot-per-inch value associated with the density. + * @return the dpi value. + */ + public int getDpi() { + return mDpi; + } + + /** + * Returns the enum matching the given dpi. + * @param dpi The dpi + * @return the enum for the dpi or null if no match was found. + */ + public static ResourceDensity getEnum(int dpi) { + for (ResourceDensity d : values()) { + if (d.mDpi == dpi) { + return d; + } + } + + return null; + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/ResourceValue.java b/layoutlib_api/src/com/android/ide/common/rendering/api/ResourceValue.java new file mode 100644 index 0000000..f59f3c3 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/ResourceValue.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2008 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.ide.common.rendering.api; + +import com.android.layoutlib.api.IResourceValue; + +/** + * Represents an android resource with a name and a string value. + */ +@SuppressWarnings("deprecation") +public class ResourceValue implements IResourceValue { + private final String mType; + private final String mName; + private String mValue = null; + private final boolean mIsFramwork; + + public ResourceValue(String type, String name, boolean isFramwork) { + mType = type; + mName = name; + mIsFramwork = isFramwork; + } + + public ResourceValue(String type, String name, String value, boolean isFramework) { + mType = type; + mName = name; + mValue = value; + mIsFramwork = isFramework; + } + + /** + * Returns the type of the resource. For instance "drawable", "color", etc... + */ + public String getType() { + return mType; + } + + /** + * Returns the name of the resource, as defined in the XML. + */ + public final String getName() { + return mName; + } + + /** + * Returns the value of the resource, as defined in the XML. This can be <code>null</code> + */ + public final String getValue() { + return mValue; + } + + /** + * Returns whether the resource is a framework resource (<code>true</code>) or a project + * resource (<code>false</false>). + */ + public final boolean isFramework() { + return mIsFramwork; + } + + + /** + * Sets the value of the resource. + * @param value the new value + */ + public void setValue(String value) { + mValue = value; + } + + /** + * Sets the value from another resource. + * @param value the resource value + */ + public void replaceWith(ResourceValue value) { + mValue = value.mValue; + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/Result.java b/layoutlib_api/src/com/android/ide/common/rendering/api/Result.java new file mode 100644 index 0000000..172a7e7 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/Result.java @@ -0,0 +1,187 @@ +/* + * 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.ide.common.rendering.api; + +/** + * Scene result class. This is an immutable class. + * <p/> + * This cannot be allocated directly, instead use + * {@link Status#createResult()}, + * {@link Status#createResult(String, Throwable)}, + * {@link Status#createResult(String)} + * {@link Status#createResult(Object)} + */ +public class Result { + + private final Status mStatus; + private final String mErrorMessage; + private final Throwable mThrowable; + private Object mData; + + /** + * Scene Status enum. + * <p/>This indicates the status of all scene actions. + */ + public enum Status { + SUCCESS, + NOT_IMPLEMENTED, + ERROR_TIMEOUT, + ERROR_LOCK_INTERRUPTED, + ERROR_INFLATION, + ERROR_VIEWGROUP_NO_CHILDREN, + ERROR_NOT_INFLATED, + ERROR_RENDER, + ERROR_ANIM_NOT_FOUND, + ERROR_UNKNOWN; + + private Result mResult; + + /** + * Returns a {@link Result} object with this status. + * @return an instance of SceneResult; + */ + public Result createResult() { + // don't want to get generic error that way. + assert this != ERROR_UNKNOWN; + + if (mResult == null) { + mResult = new Result(this); + } + + return mResult; + } + + /** + * Returns a {@link Result} object with this status, and the given data. + * @return an instance of SceneResult; + * + * @see Result#getData() + */ + public Result createResult(Object data) { + Result res = createResult(); + + if (data != null) { + res = res.getCopyWithData(data); + } + + return res; + } + + /** + * Returns a {@link #ERROR_UNKNOWN} result with the given message and throwable + * @param errorMessage the error message + * @param throwable the throwable + * @return an instance of SceneResult. + */ + public Result createResult(String errorMessage, Throwable throwable) { + return new Result(this, errorMessage, throwable); + } + + /** + * Returns a {@link #ERROR_UNKNOWN} result with the given message + * @param errorMessage the error message + * @return an instance of SceneResult. + */ + public Result createResult(String errorMessage) { + return new Result(this, errorMessage, null /*throwable*/); + } + } + + /** + * Creates a {@link Result} object with the given SceneStatus. + * + * @param status the status. Must not be null. + */ + private Result(Status status) { + this(status, null, null); + } + + /** + * Creates a {@link Result} object with the given SceneStatus, and the given message + * and {@link Throwable} + * + * @param status the status. Must not be null. + * @param errorMessage an optional error message. + * @param t an optional exception. + */ + private Result(Status status, String errorMessage, Throwable t) { + assert status != null; + mStatus = status; + mErrorMessage = errorMessage; + mThrowable = t; + } + + private Result(Result result) { + mStatus = result.mStatus; + mErrorMessage = result.mErrorMessage; + mThrowable = result.mThrowable; + } + + /** + * Returns a copy of the current result with the added (or replaced) given data + * @param data the data bundle + * + * @return returns a new SceneResult instance. + */ + public Result getCopyWithData(Object data) { + Result r = new Result(this); + r.mData = data; + return r; + } + + + /** + * Returns whether the status is successful. + * <p> + * This is the same as calling <code>getStatus() == SceneStatus.SUCCESS</code> + * @return <code>true</code> if the status is successful. + */ + public boolean isSuccess() { + return mStatus == Status.SUCCESS; + } + + /** + * Returns the status. This is never null. + */ + public Status getStatus() { + return mStatus; + } + + /** + * Returns the error message. This is only non-null when {@link #getStatus()} returns + * {@link Status#ERROR_UNKNOWN} + */ + public String getErrorMessage() { + return mErrorMessage; + } + + /** + * Returns the exception. This is only non-null when {@link #getStatus()} returns + * {@link Status#ERROR_UNKNOWN} + */ + public Throwable getException() { + return mThrowable; + } + + /** + * Returns the optional data bundle stored in the result object. + * @return the data bundle or <code>null</code> if none have been set. + */ + public Object getData() { + return mData; + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/StyleResourceValue.java b/layoutlib_api/src/com/android/ide/common/rendering/api/StyleResourceValue.java new file mode 100644 index 0000000..a9f499f --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/StyleResourceValue.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2008 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.ide.common.rendering.api; + +import com.android.layoutlib.api.IResourceValue; +import com.android.layoutlib.api.IStyleResourceValue; + +import java.util.HashMap; + +/** + * Represents an android style resources with a name and a list of children {@link ResourceValue}. + */ +@SuppressWarnings("deprecation") +public final class StyleResourceValue extends ResourceValue implements IStyleResourceValue { + + private String mParentStyle = null; + private HashMap<String, ResourceValue> mItems = new HashMap<String, ResourceValue>(); + + public StyleResourceValue(String type, String name, boolean isFramework) { + super(type, name, isFramework); + } + + public StyleResourceValue(String type, String name, String parentStyle, boolean isFramework) { + super(type, name, isFramework); + mParentStyle = parentStyle; + } + + /** + * Returns the parent style name or <code>null</code> if unknown. + */ + public String getParentStyle() { + return mParentStyle; + } + + /** + * Finds a value in the list by name + * @param name the name of the resource + */ + public ResourceValue findValue(String name) { + return mItems.get(name); + } + + public void addValue(ResourceValue value) { + mItems.put(value.getName(), value); + } + + @Override + public void replaceWith(ResourceValue value) { + assert value instanceof StyleResourceValue; + super.replaceWith(value); + + if (value instanceof StyleResourceValue) { + mItems.clear(); + mItems.putAll(((StyleResourceValue)value).mItems); + } + } + + /** + * Legacy method. + * @deprecated use {@link #getValue()} + */ + public IResourceValue findItem(String name) { + return mItems.get(name); + } + +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/ViewInfo.java b/layoutlib_api/src/com/android/ide/common/rendering/api/ViewInfo.java new file mode 100644 index 0000000..2c0c829 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/ViewInfo.java @@ -0,0 +1,133 @@ +/* + * 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.ide.common.rendering.api; + +import java.util.Collections; +import java.util.List; + +/** + * Layout information for a specific view object + */ +public class ViewInfo { + + private final Object mCookie; + private final String mName; + private final int mLeft; + private final int mRight; + private final int mTop; + private final int mBottom; + private List<ViewInfo> mChildren = Collections.emptyList(); + private final Object mViewObject; + private final Object mLayoutParamsObject; + + public ViewInfo(String name, Object cookie, int left, int top, int right, int bottom) { + this(name, cookie, left, top, right, bottom, null /*viewObject*/, + null /*layoutParamsObject*/); + } + + public ViewInfo(String name, Object cookie, int left, int top, int right, int bottom, + Object viewObject, Object layoutParamsObject) { + mName = name; + mCookie = cookie; + mLeft = left; + mRight = right; + mTop = top; + mBottom = bottom; + mViewObject = viewObject; + mLayoutParamsObject = layoutParamsObject; + } + + /** + * Sets the list of children {@link ViewInfo}. + */ + public void setChildren(List<ViewInfo> children) { + if (children != null) { + mChildren = Collections.unmodifiableList(children); + } else { + mChildren = Collections.emptyList(); + } + } + + /** + * Returns the list of children views. This is never null, but can be empty. + */ + public List<ViewInfo> getChildren() { + return mChildren; + } + + /** + * Returns the cookie associated with the XML node. Can be null. + * + * @see ILayoutPullParser#getViewKey() + */ + public Object getCookie() { + return mCookie; + } + + /** + * Returns the class name of the view object. Can be null. + */ + public String getClassName() { + return mName; + } + + /** + * Returns the left of the view bounds, relative to the view parent bounds. + */ + public int getLeft() { + return mLeft; + } + + /** + * Returns the top of the view bounds, relative to the view parent bounds. + */ + public int getTop() { + return mTop; + } + + /** + * Returns the right of the view bounds, relative to the view parent bounds. + */ + public int getRight() { + return mRight; + } + + /** + * Returns the bottom of the view bounds, relative to the view parent bounds. + */ + public int getBottom() { + return mBottom; + } + + /** + * Returns the actual android.view.View (or child class) object. This can be used + * to query the object properties that are not in the XML and not in the map returned + * by {@link #getDefaultPropertyValues()}. + */ + public Object getViewObject() { + return mViewObject; + } + + /** + * Returns the actual android.view.ViewGroup$LayoutParams (or child class) object. + * This can be used to query the object properties that are not in the XML and not in + * the map returned by {@link #getDefaultPropertyValues()}. + */ + public Object getLayoutParamsObject() { + return mLayoutParamsObject; + } +} |