aboutsummaryrefslogtreecommitdiff
path: root/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java')
-rw-r--r--bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java97
1 files changed, 64 insertions, 33 deletions
diff --git a/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java b/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
index ae5e401edc..9ed5d17531 100644
--- a/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
+++ b/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
@@ -50,7 +50,6 @@ import com.android.tools.idea.validator.ValidatorHierarchy;
import com.android.tools.idea.validator.hierarchy.CustomHierarchyHelper;
import com.android.tools.layoutlib.annotations.NotNull;
-import android.animation.AnimationHandler;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
@@ -92,7 +91,10 @@ import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import static android.os._Original_Build.VERSION.SDK_INT;
import static com.android.ide.common.rendering.api.Result.Status.ERROR_INFLATION;
import static com.android.ide.common.rendering.api.Result.Status.ERROR_NOT_INFLATED;
import static com.android.ide.common.rendering.api.Result.Status.ERROR_UNKNOWN;
@@ -109,6 +111,10 @@ import static com.android.layoutlib.common.util.ReflectionUtils.isInstanceOf;
public class RenderSessionImpl extends RenderAction<SessionParams> {
private static final Canvas NOP_CANVAS = new NopCanvas();
+ private static final String SIMULATED_SDK_TOO_HIGH =
+ String.format("The current rendering only supports APIs up to %d. You may encounter " +
+ "crashes if using with higher APIs. To avoid, you can set a lower API for " +
+ "your previews.", SDK_INT);
// scene state
private RenderSession mScene;
@@ -198,7 +204,7 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
}
/**
- * Measures the the current layout if needed (see {@link #invalidateRenderingSize}).
+ * Measures the current layout if needed (see {@link #invalidateRenderingSize}).
*/
private void measureLayout(@NonNull SessionParams params) {
// only do the screen measure when needed.
@@ -310,6 +316,13 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
SessionParams params = getParams();
BridgeContext context = getContext();
+ int simulatedVersion = params.getSimulatedPlatformVersion();
+ sSimulatedSdk = simulatedVersion > 0 ? simulatedVersion : SDK_INT;
+ if (sSimulatedSdk > SDK_INT) {
+ Bridge.getLog().fidelityWarning(ILayoutLog.TAG_UNSUPPORTED, SIMULATED_SDK_TOO_HIGH,
+ null, null, null);
+ }
+
if (Bridge.isLocaleRtl(params.getLocale())) {
if (!params.isRtlSupported()) {
Bridge.getLog().warning(ILayoutLog.TAG_RTL_NOT_ENABLED,
@@ -362,14 +375,8 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
mViewRoot.getViewRootImpl().mTmpFrames.displayFrame.set(mViewRoot.getLeft(),
mViewRoot.getTop(), mViewRoot.getRight(), mViewRoot.getBottom());
- ViewRootImpl rootImpl = AttachInfo_Accessor.getRootView(mViewRoot);
- if (rootImpl != null) {
- ViewRootImpl_Accessor.setChild(rootImpl, mViewRoot);
- }
-
mSystemViewInfoList =
- visitAllChildren(mViewRoot, 0, 0, params.getExtendedViewInfoMode(),
- false);
+ visitAllChildren(mViewRoot, 0, 0, params, false);
return SUCCESS.createResult();
} catch (PostInflateException e) {
@@ -480,6 +487,13 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
SessionParams params = getParams();
+ int simulatedVersion = params.getSimulatedPlatformVersion();
+ sSimulatedSdk = simulatedVersion > 0 ? simulatedVersion : SDK_INT;
+ if (sSimulatedSdk > SDK_INT) {
+ Bridge.getLog().fidelityWarning(ILayoutLog.TAG_UNSUPPORTED, SIMULATED_SDK_TOO_HIGH,
+ null, null, null);
+ }
+
try {
if (mViewRoot == null) {
return ERROR_NOT_INFLATED.createResult();
@@ -570,8 +584,12 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
}
mSystemViewInfoList =
- visitAllChildren(mViewRoot, 0, 0, params.getExtendedViewInfoMode(),
- false);
+ visitAllChildren(mViewRoot, 0, 0, params, false);
+
+ Consumer<BufferedImage> imageTransformation = getParams().getImageTransformation();
+ if (imageTransformation != null) {
+ imageTransformation.accept(mImage);
+ }
boolean enableLayoutValidation = Boolean.TRUE.equals(params.getFlag(RenderParamsFlags.FLAG_ENABLE_LAYOUT_VALIDATOR));
boolean enableLayoutValidationImageCheck = Boolean.TRUE.equals(
@@ -853,15 +871,16 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
*
* @return {@code ViewInfo} containing the bounds of the view and it children otherwise.
*/
- private ViewInfo visit(View view, int hOffset, int vOffset, boolean setExtendedInfo,
+ private ViewInfo visit(View view, int hOffset, int vOffset, SessionParams params,
boolean isContentFrame) {
- ViewInfo result = createViewInfo(view, hOffset, vOffset, setExtendedInfo, isContentFrame);
+ ViewInfo result = createViewInfo(view, hOffset, vOffset, params.getExtendedViewInfoMode(),
+ isContentFrame);
if (view instanceof ViewGroup) {
ViewGroup group = ((ViewGroup) view);
result.setChildren(visitAllChildren(group, isContentFrame ? 0 : hOffset,
isContentFrame ? 0 : vOffset,
- setExtendedInfo, isContentFrame));
+ params, isContentFrame));
}
return result;
}
@@ -880,7 +899,7 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
* part of the system decor.
*/
private List<ViewInfo> visitAllChildren(ViewGroup viewGroup, int hOffset, int vOffset,
- boolean setExtendedInfo, boolean isContentFrame) {
+ SessionParams params, boolean isContentFrame) {
if (viewGroup == null) {
return null;
}
@@ -896,8 +915,7 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
List<ViewInfo> childrenWithOffset = new ArrayList<>(childCount);
for (int i = 0; i < childCount; i++) {
ViewInfo[] childViewInfo =
- visitContentRoot(viewGroup.getChildAt(i), hOffset, vOffset,
- setExtendedInfo);
+ visitContentRoot(viewGroup.getChildAt(i), hOffset, vOffset, params);
childrenWithoutOffset.add(childViewInfo[0]);
childrenWithOffset.add(childViewInfo[1]);
}
@@ -906,7 +924,7 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
} else {
List<ViewInfo> children = new ArrayList<>(childCount);
for (int i = 0; i < childCount; i++) {
- children.add(visit(viewGroup.getChildAt(i), hOffset, vOffset, setExtendedInfo,
+ children.add(visit(viewGroup.getChildAt(i), hOffset, vOffset, params,
isContentFrame));
}
return children;
@@ -920,26 +938,32 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
* get the right bounds if the {@code ViewInfo} hierarchy is accessed from
* {@code mViewInfoList}. When the hierarchy is accessed via {@code mSystemViewInfoList}, the
* offset is not needed.
+ * If a custom parser was passed inside the {@link SessionParams} argument, this will be used
+ * to generate the {@link ViewInfo}s. Otherwise, {@link RenderSessionImpl#visitAllChildren}
+ * will be used.
*
* @return an array of length two, with ViewInfo at index 0 is without offset and ViewInfo at
* index 1 is with the offset.
*/
@NonNull
- private ViewInfo[] visitContentRoot(View view, int hOffset, int vOffset,
- boolean setExtendedInfo) {
+ private ViewInfo[] visitContentRoot(View view, int hOffset, int vOffset, SessionParams params) {
ViewInfo[] result = new ViewInfo[2];
if (view == null) {
return result;
}
+ boolean setExtendedInfo = params.getExtendedViewInfoMode();
result[0] = createViewInfo(view, 0, 0, setExtendedInfo, true);
result[1] = createViewInfo(view, hOffset, vOffset, setExtendedInfo, true);
- if (view instanceof ViewGroup) {
- List<ViewInfo> children =
- visitAllChildren((ViewGroup) view, 0, 0, setExtendedInfo, true);
- result[0].setChildren(children);
- result[1].setChildren(children);
+ Function<Object, List<ViewInfo>> customParser = params.getCustomContentHierarchyParser();
+ List<ViewInfo> children = null;
+ if (customParser != null) {
+ children = customParser.apply(view);
+ } else if (view instanceof ViewGroup) {
+ children = visitAllChildren((ViewGroup) view, 0, 0, params, true);
}
+ result[0].setChildren(children);
+ result[1].setChildren(children);
return result;
}
@@ -975,13 +999,13 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
shiftY + view.getTop(),
shiftX + view.getRight(),
shiftY + view.getBottom(),
- view, view.getLayoutParams());
+ view, null, view.getLayoutParams());
} else {
// We are part of the system decor.
SystemViewInfo r = new SystemViewInfo(view.getClass().getName(),
getViewKey(view),
view.getLeft(), view.getTop(), view.getRight(),
- view.getBottom(), view, view.getLayoutParams());
+ view.getBottom(), view, null, view.getLayoutParams());
result = r;
// We currently mark three kinds of views:
// 1. Menus in the Action Bar
@@ -1184,6 +1208,19 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
}
}
+ @Override
+ public void release() {
+ super.release();
+ if (mViewRoot == null) {
+ return;
+ }
+ ViewRootImpl viewRootImpl = mViewRoot.getViewRootImpl();
+ if (viewRootImpl == null) {
+ return;
+ }
+ ViewRootImpl_Accessor.detachFromWindow(viewRootImpl);
+ }
+
private void disposeImageSurface() {
if (mCanvas != null) {
mCanvas.release();
@@ -1198,12 +1235,6 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
mImage = null;
// detachFromWindow might create Handler callbacks, thus before Handler_Delegate.dispose
AttachInfo_Accessor.detachFromWindow(mViewRoot);
- AnimationHandler animationHandler = AnimationHandler.sAnimatorHandler.get();
- if (animationHandler != null) {
- animationHandler.mDelayedCallbackStartTime.clear();
- animationHandler.mAnimationCallbacks.clear();
- animationHandler.mCommitCallbacks.clear();
- }
getContext().getSessionInteractiveData().dispose();
if (mViewInfoList != null) {
mViewInfoList.clear();