aboutsummaryrefslogtreecommitdiffstats
path: root/ide_common
diff options
context:
space:
mode:
authorXavier Ducrohet <xav@android.com>2010-12-15 16:08:57 -0800
committerXavier Ducrohet <xav@android.com>2010-12-21 10:27:22 -0800
commit95b17a5e62eeeb7b38ef668508df43a1ee9e0880 (patch)
treeca77258cf072446e38d2a68acab77aac8dd3a73e /ide_common
parent77e0fdebe08d39f550544571989649533d043223 (diff)
downloadsdk-95b17a5e62eeeb7b38ef668508df43a1ee9e0880.zip
sdk-95b17a5e62eeeb7b38ef668508df43a1ee9e0880.tar.gz
sdk-95b17a5e62eeeb7b38ef668508df43a1ee9e0880.tar.bz2
LayoutLib API refactoring
Change-Id: I40abba4c4f786755c2a1c0e70df4d7bc08e2bcde
Diffstat (limited to 'ide_common')
-rw-r--r--ide_common/.classpath1
-rw-r--r--ide_common/Android.mk3
-rw-r--r--ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java (renamed from ide_common/src/com/android/ide/common/layoutlib/LayoutLibrary.java)122
-rw-r--r--ide_common/src/com/android/ide/common/rendering/StaticRenderSession.java (renamed from ide_common/src/com/android/ide/common/layoutlib/BasicLayoutScene.java)21
-rw-r--r--ide_common/src/com/android/ide/common/rendering/ValueResourceParser.java (renamed from ide_common/src/com/android/ide/common/layoutlib/ValueResourceParser.java)6
-rw-r--r--ide_common/src/com/android/ide/common/rendering/legacy/ILegacyCallback.java31
-rw-r--r--ide_common/src/com/android/ide/common/rendering/legacy/ILegacyPullParser.java31
7 files changed, 152 insertions, 63 deletions
diff --git a/ide_common/.classpath b/ide_common/.classpath
index 0321c43..8760b35 100644
--- a/ide_common/.classpath
+++ b/ide_common/.classpath
@@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/layoutlib_api"/>
+ <classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/kxml2/kxml2-2.3.0.jar" sourcepath="/ANDROID_SRC/dalvik/libcore/xml/src/main/java"/>
<classpathentry kind="output" path="bin"/>
</classpath>
diff --git a/ide_common/Android.mk b/ide_common/Android.mk
index 2410b49..d3b388c 100644
--- a/ide_common/Android.mk
+++ b/ide_common/Android.mk
@@ -19,7 +19,8 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-java-files-under,src)
LOCAL_JAVA_LIBRARIES := \
- layoutlib_api
+ layoutlib_api \
+ kxml2-2.3.0
LOCAL_MODULE := ide_common
diff --git a/ide_common/src/com/android/ide/common/layoutlib/LayoutLibrary.java b/ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java
index 740953e..878f9fd 100644
--- a/ide_common/src/com/android/ide/common/layoutlib/LayoutLibrary.java
+++ b/ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java
@@ -14,24 +14,29 @@
* limitations under the License.
*/
-package com.android.ide.common.layoutlib;
+package com.android.ide.common.rendering;
import com.android.ide.common.log.ILogger;
+import com.android.ide.common.rendering.api.Bridge;
+import com.android.ide.common.rendering.api.Capability;
+import com.android.ide.common.rendering.api.ILayoutPullParser;
+import com.android.ide.common.rendering.api.LayoutLog;
+import com.android.ide.common.rendering.api.Params;
+import com.android.ide.common.rendering.api.RenderSession;
+import com.android.ide.common.rendering.api.Result;
+import com.android.ide.common.rendering.api.ViewInfo;
+import com.android.ide.common.rendering.api.Params.RenderingMode;
+import com.android.ide.common.rendering.api.Result.Status;
+import com.android.ide.common.rendering.legacy.ILegacyCallback;
+import com.android.ide.common.rendering.legacy.ILegacyPullParser;
import com.android.ide.common.sdk.LoadStatus;
-import com.android.layoutlib.api.Capability;
import com.android.layoutlib.api.ILayoutBridge;
import com.android.layoutlib.api.ILayoutLog;
import com.android.layoutlib.api.ILayoutResult;
+import com.android.layoutlib.api.IProjectCallback;
import com.android.layoutlib.api.IResourceValue;
-import com.android.layoutlib.api.LayoutBridge;
-import com.android.layoutlib.api.LayoutLog;
-import com.android.layoutlib.api.LayoutScene;
-import com.android.layoutlib.api.SceneParams;
-import com.android.layoutlib.api.SceneResult;
-import com.android.layoutlib.api.ViewInfo;
+import com.android.layoutlib.api.IXmlPullParser;
import com.android.layoutlib.api.ILayoutResult.ILayoutViewInfo;
-import com.android.layoutlib.api.SceneParams.RenderingMode;
-import com.android.layoutlib.api.SceneResult.SceneStatus;
import java.io.File;
import java.lang.reflect.Constructor;
@@ -48,9 +53,16 @@ import java.util.Map;
* Use {@link #load(String, ILogger)} to load the jar file.
* <p/>
* Use the layout library with:
- * {@link #init(String, Map)}, {@link #supports(Capability)}, {@link #createScene(SceneParams)},
+ * {@link #init(String, Map)}, {@link #supports(Capability)}, {@link #createSession(Params)},
* {@link #dispose()}, {@link #clearCaches(Object)}.
*
+ * <p/>
+ * For client wanting to access both new and old (pre API level 5) layout libraries, it is
+ * important that the following interfaces be used:<br>
+ * {@link ILegacyPullParser} instead of {@link ILayoutPullParser}<br>
+ * {@link ILegacyCallback} instead of{@link com.android.ide.common.rendering.api.IProjectCallback}.
+ * <p/>
+ * These interfaces will ensure that both new and older Layout libraries can be accessed.
*/
@SuppressWarnings("deprecation")
public class LayoutLibrary {
@@ -58,7 +70,7 @@ public class LayoutLibrary {
public final static String CLASS_BRIDGE = "com.android.layoutlib.bridge.Bridge"; //$NON-NLS-1$
/** Link to the layout bridge */
- private final LayoutBridge mBridge;
+ private final Bridge mBridge;
/** Link to a ILayoutBridge in case loaded an older library */
private final ILayoutBridge mLegacyBridge;
/** Status of the layoutlib.jar loading */
@@ -106,7 +118,7 @@ public class LayoutLibrary {
LoadStatus status = LoadStatus.LOADING;
String message = null;
- LayoutBridge bridge = null;
+ Bridge bridge = null;
ILayoutBridge legacyBridge = null;
ClassLoader classLoader = null;
@@ -135,8 +147,8 @@ public class LayoutLibrary {
Constructor<?> constructor = clazz.getConstructor();
if (constructor != null) {
Object bridgeObject = constructor.newInstance();
- if (bridgeObject instanceof LayoutBridge) {
- bridge = (LayoutBridge)bridgeObject;
+ if (bridgeObject instanceof Bridge) {
+ bridge = (Bridge)bridgeObject;
} else if (bridgeObject instanceof ILayoutBridge) {
legacyBridge = (ILayoutBridge) bridgeObject;
}
@@ -160,7 +172,7 @@ public class LayoutLibrary {
// check the API, only if it's not a legacy bridge
if (bridge != null) {
int api = bridge.getApiLevel();
- if (api > LayoutBridge.API_CURRENT) {
+ if (api > Bridge.API_CURRENT) {
status = LoadStatus.FAILED;
message = "LayoutLib is too recent. Update your tool!";
}
@@ -189,7 +201,7 @@ public class LayoutLibrary {
* Returns whether the LayoutLibrary supports a given {@link Capability}.
* @return true if it supports it.
*
- * @see LayoutBridge#getCapabilities()
+ * @see Bridge#getCapabilities()
*
*/
public boolean supports(Capability capability) {
@@ -212,18 +224,18 @@ public class LayoutLibrary {
* Initializes the Layout Library object. This must be called before any other action is taken
* on the instance.
*
- * @param fontOsLocation the location of the fonts in the SDK target.
+ * @param fontLocation the location of the fonts in the SDK target.
* @param enumValueMap map attrName => { map enumFlagName => Integer value }. This is typically
* read from attrs.xml in the SDK target.
* @return true if success.
*
- * @see LayoutBridge#init(String, Map)
+ * @see Bridge#init(String, Map)
*/
- public boolean init(String fontOsLocation, Map<String, Map<String, Integer>> enumValueMap) {
+ public boolean init(File fontLocation, Map<String, Map<String, Integer>> enumValueMap) {
if (mBridge != null) {
- return mBridge.init(fontOsLocation, enumValueMap);
+ return mBridge.init(fontLocation, enumValueMap);
} else if (mLegacyBridge != null) {
- return mLegacyBridge.init(fontOsLocation, enumValueMap);
+ return mLegacyBridge.init(fontLocation.getAbsolutePath(), enumValueMap);
}
return false;
@@ -232,7 +244,7 @@ public class LayoutLibrary {
/**
* Prepares the layoutlib to unloaded.
*
- * @see LayoutBridge#dispose()
+ * @see Bridge#dispose()
*/
public boolean dispose() {
if (mBridge != null) {
@@ -244,7 +256,7 @@ public class LayoutLibrary {
/**
* Starts a layout session by inflating and rendering it. The method returns a
- * {@link LayoutScene} on which further actions can be taken.
+ * {@link RenderSession} on which further actions can be taken.
* <p/>
* Before taking further actions on the scene, it is recommended to use
* {@link #supports(Capability)} to check what the scene can do.
@@ -252,13 +264,13 @@ public class LayoutLibrary {
* @return a new {@link ILayoutScene} object that contains the result of the scene creation and
* first rendering or null if {@link #getStatus()} doesn't return {@link LoadStatus#LOADED}.
*
- * @see LayoutBridge#createScene(SceneParams)
+ * @see Bridge#createSession(Params)
*/
- public LayoutScene createScene(SceneParams params) {
+ public RenderSession createSession(Params params) {
if (mBridge != null) {
- return mBridge.createScene(params);
+ return mBridge.createSession(params);
} else if (mLegacyBridge != null) {
- return createLegacyScene(params);
+ return createLegacySession(params);
}
return null;
@@ -273,7 +285,7 @@ public class LayoutLibrary {
*
* @param projectKey the key for the project.
*
- * @see LayoutBridge#clearCaches(Object)
+ * @see Bridge#clearCaches(Object)
*/
public void clearCaches(Object projectKey) {
if (mBridge != null) {
@@ -285,7 +297,7 @@ public class LayoutLibrary {
// ------ Implementation
- private LayoutLibrary(LayoutBridge bridge, ILayoutBridge legacyBridge, ClassLoader classLoader,
+ private LayoutLibrary(Bridge bridge, ILayoutBridge legacyBridge, ClassLoader classLoader,
LoadStatus status, String message) {
mBridge = bridge;
mLegacyBridge = legacyBridge;
@@ -316,8 +328,16 @@ public class LayoutLibrary {
return apiLevel;
}
- private LayoutScene createLegacyScene(SceneParams params) {
- int apiLevel = mLegacyBridge.getApiLevel();
+ private RenderSession createLegacySession(Params params) {
+ if (params.getLayoutDescription() instanceof IXmlPullParser == false) {
+ throw new IllegalArgumentException("Parser must be of type ILegacyPullParser");
+ }
+ if (params.getProjectCallback() instanceof
+ com.android.layoutlib.api.IProjectCallback == false) {
+ throw new IllegalArgumentException("Project callback must be of type ILegacyCallback");
+ }
+
+ int apiLevel = getLegacyApiLevel();
// create a log wrapper since the older api requires a ILayoutLog
final LayoutLog log = params.getLog();
@@ -349,30 +369,32 @@ public class LayoutLibrary {
if (apiLevel == 4) {
// Final ILayoutBridge API added support for "render full height"
result = mLegacyBridge.computeLayout(
- params.getLayoutDescription(), params.getProjectKey(),
+ (IXmlPullParser) params.getLayoutDescription(),
+ params.getProjectKey(),
params.getScreenWidth(), params.getScreenHeight(),
params.getRenderingMode() == RenderingMode.FULL_EXPAND ? true : false,
params.getDensity(), params.getXdpi(), params.getYdpi(),
- params.getThemeName(), params.getIsProjectTheme(),
+ params.getThemeName(), params.isProjectTheme(),
projectMap, frameworkMap,
- params.getProjectCallback(), logWrapper);
+ (IProjectCallback) params.getProjectCallback(),
+ logWrapper);
} else if (apiLevel == 3) {
// api 3 add density support.
result = mLegacyBridge.computeLayout(
- params.getLayoutDescription(), params.getProjectKey(),
+ (IXmlPullParser) params.getLayoutDescription(), params.getProjectKey(),
params.getScreenWidth(), params.getScreenHeight(),
params.getDensity(), params.getXdpi(), params.getYdpi(),
- params.getThemeName(), params.getIsProjectTheme(),
+ params.getThemeName(), params.isProjectTheme(),
projectMap, frameworkMap,
- params.getProjectCallback(), logWrapper);
+ (IProjectCallback) params.getProjectCallback(), logWrapper);
} else if (apiLevel == 2) {
// api 2 added boolean for separation of project/framework theme
result = mLegacyBridge.computeLayout(
- params.getLayoutDescription(), params.getProjectKey(),
+ (IXmlPullParser) params.getLayoutDescription(), params.getProjectKey(),
params.getScreenWidth(), params.getScreenHeight(),
- params.getThemeName(), params.getIsProjectTheme(),
+ params.getThemeName(), params.isProjectTheme(),
projectMap, frameworkMap,
- params.getProjectCallback(), logWrapper);
+ (IProjectCallback) params.getProjectCallback(), logWrapper);
} else {
// First api with no density/dpi, and project theme boolean mixed
// into the theme name.
@@ -380,16 +402,16 @@ public class LayoutLibrary {
// change the string if it's a custom theme to make sure we can
// differentiate them
String themeName = params.getThemeName();
- if (params.getIsProjectTheme()) {
+ if (params.isProjectTheme()) {
themeName = "*" + themeName; //$NON-NLS-1$
}
result = mLegacyBridge.computeLayout(
- params.getLayoutDescription(), params.getProjectKey(),
+ (IXmlPullParser) params.getLayoutDescription(), params.getProjectKey(),
params.getScreenWidth(), params.getScreenHeight(),
themeName,
projectMap, frameworkMap,
- params.getProjectCallback(), logWrapper);
+ (IProjectCallback) params.getProjectCallback(), logWrapper);
}
// clean up that is not done by the ILayoutBridge itself
@@ -399,25 +421,25 @@ public class LayoutLibrary {
}
/**
- * Converts a {@link ILayoutResult} to a {@link LayoutScene}.
+ * Converts a {@link ILayoutResult} to a {@link RenderSession}.
*/
- private LayoutScene convertToScene(ILayoutResult result) {
+ private RenderSession convertToScene(ILayoutResult result) {
- SceneResult sceneResult;
+ Result sceneResult;
ViewInfo rootViewInfo;
if (result.getSuccess() == ILayoutResult.SUCCESS) {
- sceneResult = SceneStatus.SUCCESS.createResult();
+ sceneResult = Status.SUCCESS.createResult();
rootViewInfo = convertToViewInfo(result.getRootView());
} else {
- sceneResult = SceneStatus.ERROR_UNKNOWN.createResult(result.getErrorMessage());
+ sceneResult = Status.ERROR_UNKNOWN.createResult(result.getErrorMessage());
rootViewInfo = null;
}
// create a BasicLayoutScene. This will return the given values but return the default
// implementation for all method.
// ADT should gracefully handle the default implementations of LayoutScene
- return new BasicLayoutScene(sceneResult, rootViewInfo, result.getImage());
+ return new StaticRenderSession(sceneResult, rootViewInfo, result.getImage());
}
/**
diff --git a/ide_common/src/com/android/ide/common/layoutlib/BasicLayoutScene.java b/ide_common/src/com/android/ide/common/rendering/StaticRenderSession.java
index dfb3992..a33a497 100644
--- a/ide_common/src/com/android/ide/common/layoutlib/BasicLayoutScene.java
+++ b/ide_common/src/com/android/ide/common/rendering/StaticRenderSession.java
@@ -14,35 +14,38 @@
* limitations under the License.
*/
-package com.android.ide.common.layoutlib;
+package com.android.ide.common.rendering;
-import com.android.layoutlib.api.LayoutScene;
-import com.android.layoutlib.api.SceneResult;
-import com.android.layoutlib.api.ViewInfo;
+import com.android.ide.common.rendering.api.RenderSession;
+import com.android.ide.common.rendering.api.Result;
+import com.android.ide.common.rendering.api.ViewInfo;
import java.awt.image.BufferedImage;
/**
- * Basic LayoutScene returning a given {@link SceneResult}, {@link ViewInfo} and
+ * Static {@link RenderSession} returning a given {@link Result}, {@link ViewInfo} and
* {@link BufferedImage}.
* <p/>
* All other methods are untouched from the base implementation provided by the API.
+ * <p/>
+ * This is meant to be used as a wrapper around the static results. No further operations are
+ * possible.
*
*/
-public class BasicLayoutScene extends LayoutScene {
+public class StaticRenderSession extends RenderSession {
- private final SceneResult mResult;
+ private final Result mResult;
private final ViewInfo mRootViewInfo;
private final BufferedImage mImage;
- public BasicLayoutScene(SceneResult result, ViewInfo rootViewInfo, BufferedImage image) {
+ public StaticRenderSession(Result result, ViewInfo rootViewInfo, BufferedImage image) {
mResult = result;
mRootViewInfo = rootViewInfo;
mImage = image;
}
@Override
- public SceneResult getResult() {
+ public Result getResult() {
return mResult;
}
diff --git a/ide_common/src/com/android/ide/common/layoutlib/ValueResourceParser.java b/ide_common/src/com/android/ide/common/rendering/ValueResourceParser.java
index cc65b9d..8c56a98 100644
--- a/ide_common/src/com/android/ide/common/layoutlib/ValueResourceParser.java
+++ b/ide_common/src/com/android/ide/common/rendering/ValueResourceParser.java
@@ -14,10 +14,10 @@
* limitations under the License.
*/
-package com.android.ide.common.layoutlib;
+package com.android.ide.common.rendering;
-import com.android.layoutlib.api.ResourceValue;
-import com.android.layoutlib.api.StyleResourceValue;
+import com.android.ide.common.rendering.api.ResourceValue;
+import com.android.ide.common.rendering.api.StyleResourceValue;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
diff --git a/ide_common/src/com/android/ide/common/rendering/legacy/ILegacyCallback.java b/ide_common/src/com/android/ide/common/rendering/legacy/ILegacyCallback.java
new file mode 100644
index 0000000..000c7c5
--- /dev/null
+++ b/ide_common/src/com/android/ide/common/rendering/legacy/ILegacyCallback.java
@@ -0,0 +1,31 @@
+/*
+ * 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.legacy;
+
+import com.android.ide.common.rendering.api.IProjectCallback;
+
+/**
+ * Intermediary interface extending both old and new project call back from the layout lib API.
+ *
+ * Clients should use this instead of {@link IProjectCallback} to target both old and new
+ * Layout Libraries.
+ *
+ */
+@SuppressWarnings("deprecation")
+public interface ILegacyCallback extends com.android.ide.common.rendering.api.IProjectCallback,
+ com.android.layoutlib.api.IProjectCallback {
+}
diff --git a/ide_common/src/com/android/ide/common/rendering/legacy/ILegacyPullParser.java b/ide_common/src/com/android/ide/common/rendering/legacy/ILegacyPullParser.java
new file mode 100644
index 0000000..a71e190
--- /dev/null
+++ b/ide_common/src/com/android/ide/common/rendering/legacy/ILegacyPullParser.java
@@ -0,0 +1,31 @@
+/*
+ * 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.legacy;
+
+import com.android.ide.common.rendering.api.ILayoutPullParser;
+import com.android.layoutlib.api.IXmlPullParser;
+
+/**
+ * Intermediary interface extending both old and new project pull parsers from the layout lib API.
+ *
+ * Clients should use this instead of {@link ILayoutPullParser} or {@link IXmlPullParser}.
+ *
+ */
+@SuppressWarnings("deprecation")
+public interface ILegacyPullParser extends ILayoutPullParser, IXmlPullParser {
+
+}