Merge "Handle the case where no session exists" into oc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
261dd2305a
@@ -26,10 +26,13 @@ import com.android.layoutlib.bridge.impl.RenderSessionImpl;
|
|||||||
import com.android.tools.layoutlib.java.System_Delegate;
|
import com.android.tools.layoutlib.java.System_Delegate;
|
||||||
import com.android.util.PropertiesMap;
|
import com.android.util.PropertiesMap;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.Nullable;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -42,7 +45,9 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class BridgeRenderSession extends RenderSession {
|
public class BridgeRenderSession extends RenderSession {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private final RenderSessionImpl mSession;
|
private final RenderSessionImpl mSession;
|
||||||
|
@NonNull
|
||||||
private Result mLastResult;
|
private Result mLastResult;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -52,41 +57,44 @@ public class BridgeRenderSession extends RenderSession {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BufferedImage getImage() {
|
public BufferedImage getImage() {
|
||||||
return mSession.getImage();
|
return mSession != null ? mSession.getImage() :
|
||||||
|
new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAlphaChannelImage() {
|
public boolean isAlphaChannelImage() {
|
||||||
return mSession.isAlphaChannelImage();
|
return mSession != null && mSession.isAlphaChannelImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ViewInfo> getRootViews() {
|
public List<ViewInfo> getRootViews() {
|
||||||
return mSession.getViewInfos();
|
return mSession != null ? mSession.getViewInfos() : Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ViewInfo> getSystemRootViews() {
|
public List<ViewInfo> getSystemRootViews() {
|
||||||
return mSession.getSystemViewInfos();
|
return mSession != null ? mSession.getSystemViewInfos() : Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<Object, PropertiesMap> getDefaultProperties() {
|
public Map<Object, PropertiesMap> getDefaultProperties() {
|
||||||
return mSession.getDefaultProperties();
|
return mSession != null ? mSession.getDefaultProperties() : Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result measure(long timeout) {
|
public Result measure(long timeout) {
|
||||||
try {
|
if (mSession != null) {
|
||||||
Bridge.prepareThread();
|
try {
|
||||||
mLastResult = mSession.acquire(timeout);
|
Bridge.prepareThread();
|
||||||
if (mLastResult.isSuccess()) {
|
mLastResult = mSession.acquire(timeout);
|
||||||
mSession.invalidateRenderingSize();
|
if (mLastResult.isSuccess()) {
|
||||||
mLastResult = mSession.measure();
|
mSession.invalidateRenderingSize();
|
||||||
|
mLastResult = mSession.measure();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
mSession.release();
|
||||||
|
Bridge.cleanupThread();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
mSession.release();
|
|
||||||
Bridge.cleanupThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mLastResult;
|
return mLastResult;
|
||||||
@@ -94,18 +102,20 @@ public class BridgeRenderSession extends RenderSession {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result render(long timeout, boolean forceMeasure) {
|
public Result render(long timeout, boolean forceMeasure) {
|
||||||
try {
|
if (mSession != null) {
|
||||||
Bridge.prepareThread();
|
try {
|
||||||
mLastResult = mSession.acquire(timeout);
|
Bridge.prepareThread();
|
||||||
if (mLastResult.isSuccess()) {
|
mLastResult = mSession.acquire(timeout);
|
||||||
if (forceMeasure) {
|
if (mLastResult.isSuccess()) {
|
||||||
mSession.invalidateRenderingSize();
|
if (forceMeasure) {
|
||||||
|
mSession.invalidateRenderingSize();
|
||||||
|
}
|
||||||
|
mLastResult = mSession.render(false /*freshRender*/);
|
||||||
}
|
}
|
||||||
mLastResult = mSession.render(false /*freshRender*/);
|
} finally {
|
||||||
|
mSession.release();
|
||||||
|
Bridge.cleanupThread();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
mSession.release();
|
|
||||||
Bridge.cleanupThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mLastResult;
|
return mLastResult;
|
||||||
@@ -114,16 +124,18 @@ public class BridgeRenderSession extends RenderSession {
|
|||||||
@Override
|
@Override
|
||||||
public Result animate(Object targetObject, String animationName,
|
public Result animate(Object targetObject, String animationName,
|
||||||
boolean isFrameworkAnimation, IAnimationListener listener) {
|
boolean isFrameworkAnimation, IAnimationListener listener) {
|
||||||
try {
|
if (mSession != null) {
|
||||||
Bridge.prepareThread();
|
try {
|
||||||
mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
|
Bridge.prepareThread();
|
||||||
if (mLastResult.isSuccess()) {
|
mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
|
||||||
mLastResult = mSession.animate(targetObject, animationName, isFrameworkAnimation,
|
if (mLastResult.isSuccess()) {
|
||||||
listener);
|
mLastResult = mSession.animate(targetObject, animationName, isFrameworkAnimation,
|
||||||
|
listener);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
mSession.release();
|
||||||
|
Bridge.cleanupThread();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
mSession.release();
|
|
||||||
Bridge.cleanupThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mLastResult;
|
return mLastResult;
|
||||||
@@ -136,16 +148,18 @@ public class BridgeRenderSession extends RenderSession {
|
|||||||
throw new IllegalArgumentException("parentView is not a ViewGroup");
|
throw new IllegalArgumentException("parentView is not a ViewGroup");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (mSession != null) {
|
||||||
Bridge.prepareThread();
|
try {
|
||||||
mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
|
Bridge.prepareThread();
|
||||||
if (mLastResult.isSuccess()) {
|
mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
|
||||||
mLastResult = mSession.insertChild((ViewGroup) parentView, childXml, index,
|
if (mLastResult.isSuccess()) {
|
||||||
listener);
|
mLastResult =
|
||||||
|
mSession.insertChild((ViewGroup) parentView, childXml, index, listener);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
mSession.release();
|
||||||
|
Bridge.cleanupThread();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
mSession.release();
|
|
||||||
Bridge.cleanupThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mLastResult;
|
return mLastResult;
|
||||||
@@ -162,16 +176,18 @@ public class BridgeRenderSession extends RenderSession {
|
|||||||
throw new IllegalArgumentException("childView is not a View");
|
throw new IllegalArgumentException("childView is not a View");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (mSession != null) {
|
||||||
Bridge.prepareThread();
|
try {
|
||||||
mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
|
Bridge.prepareThread();
|
||||||
if (mLastResult.isSuccess()) {
|
mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
|
||||||
mLastResult = mSession.moveChild((ViewGroup) parentView, (View) childView, index,
|
if (mLastResult.isSuccess()) {
|
||||||
layoutParams, listener);
|
mLastResult = mSession.moveChild((ViewGroup) parentView, (View) childView, index,
|
||||||
|
layoutParams, listener);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
mSession.release();
|
||||||
|
Bridge.cleanupThread();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
mSession.release();
|
|
||||||
Bridge.cleanupThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mLastResult;
|
return mLastResult;
|
||||||
@@ -183,15 +199,17 @@ public class BridgeRenderSession extends RenderSession {
|
|||||||
throw new IllegalArgumentException("childView is not a View");
|
throw new IllegalArgumentException("childView is not a View");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (mSession != null) {
|
||||||
Bridge.prepareThread();
|
try {
|
||||||
mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
|
Bridge.prepareThread();
|
||||||
if (mLastResult.isSuccess()) {
|
mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
|
||||||
mLastResult = mSession.removeChild((View) childView, listener);
|
if (mLastResult.isSuccess()) {
|
||||||
|
mLastResult = mSession.removeChild((View) childView, listener);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
mSession.release();
|
||||||
|
Bridge.cleanupThread();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
mSession.release();
|
|
||||||
Bridge.cleanupThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mLastResult;
|
return mLastResult;
|
||||||
@@ -221,7 +239,7 @@ public class BridgeRenderSession extends RenderSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ BridgeRenderSession(RenderSessionImpl scene, Result lastResult) {
|
/*package*/ BridgeRenderSession(@Nullable RenderSessionImpl scene, @NonNull Result lastResult) {
|
||||||
mSession = scene;
|
mSession = scene;
|
||||||
if (scene != null) {
|
if (scene != null) {
|
||||||
mSession.setScene(this);
|
mSession.setScene(this);
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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;
|
||||||
|
|
||||||
|
import com.android.ide.common.rendering.api.Result;
|
||||||
|
import com.android.ide.common.rendering.api.Result.Status;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class BridgeRenderSessionTest {
|
||||||
|
@Test
|
||||||
|
public void testNullSession() {
|
||||||
|
BridgeRenderSession renderSession = new BridgeRenderSession(null, Status.ERROR_UNKNOWN
|
||||||
|
.createResult("Test result"));
|
||||||
|
|
||||||
|
assertNotNull(renderSession.getImage());
|
||||||
|
assertNotNull(renderSession.getRootViews());
|
||||||
|
assertNotNull(renderSession.getSystemRootViews());
|
||||||
|
assertNotNull(renderSession.getDefaultProperties());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.android.layoutlib.bridge.intensive;
|
package com.android.layoutlib.bridge.intensive;
|
||||||
|
|
||||||
|
import com.android.layoutlib.bridge.BridgeRenderSessionTest;
|
||||||
import com.android.layoutlib.bridge.TestDelegates;
|
import com.android.layoutlib.bridge.TestDelegates;
|
||||||
import com.android.layoutlib.bridge.android.BridgeXmlBlockParserTest;
|
import com.android.layoutlib.bridge.android.BridgeXmlBlockParserTest;
|
||||||
import com.android.layoutlib.bridge.impl.LayoutParserWrapperTest;
|
import com.android.layoutlib.bridge.impl.LayoutParserWrapperTest;
|
||||||
@@ -34,7 +35,8 @@ import android.util.BridgeXmlPullAttributesTest;
|
|||||||
@SuiteClasses({
|
@SuiteClasses({
|
||||||
RenderTests.class, LayoutParserWrapperTest.class,
|
RenderTests.class, LayoutParserWrapperTest.class,
|
||||||
BridgeXmlBlockParserTest.class, BridgeXmlPullAttributesTest.class,
|
BridgeXmlBlockParserTest.class, BridgeXmlPullAttributesTest.class,
|
||||||
Matrix_DelegateTest.class, TestDelegates.class, PerformanceTests.class
|
Matrix_DelegateTest.class, TestDelegates.class, PerformanceTests.class,
|
||||||
|
BridgeRenderSessionTest.class
|
||||||
})
|
})
|
||||||
public class Main {
|
public class Main {
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user