BridgeRenderSession.java revision 39e540caffca2584aa6c4cb74ce42dceb24a93f7
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.layoutlib.bridge; 18 19import com.android.ide.common.rendering.api.IAnimationListener; 20import com.android.ide.common.rendering.api.ILayoutPullParser; 21import com.android.ide.common.rendering.api.RenderParams; 22import com.android.ide.common.rendering.api.RenderSession; 23import com.android.ide.common.rendering.api.Result; 24import com.android.ide.common.rendering.api.ViewInfo; 25import com.android.layoutlib.bridge.impl.RenderSessionImpl; 26import com.android.tools.layoutlib.java.System_Delegate; 27import com.android.util.PropertiesMap; 28 29import android.view.View; 30import android.view.ViewGroup; 31 32import java.awt.image.BufferedImage; 33import java.util.List; 34import java.util.Map; 35 36/** 37 * An implementation of {@link RenderSession}. 38 * 39 * This is a pretty basic class that does almost nothing. All of the work is done in 40 * {@link RenderSessionImpl}. 41 * 42 */ 43public class BridgeRenderSession extends RenderSession { 44 45 private final RenderSessionImpl mSession; 46 private Result mLastResult; 47 48 @Override 49 public Result getResult() { 50 return mLastResult; 51 } 52 53 @Override 54 public BufferedImage getImage() { 55 return mSession.getImage(); 56 } 57 58 @Override 59 public boolean isAlphaChannelImage() { 60 return mSession.isAlphaChannelImage(); 61 } 62 63 @Override 64 public List<ViewInfo> getRootViews() { 65 return mSession.getViewInfos(); 66 } 67 68 @Override 69 public List<ViewInfo> getSystemRootViews() { 70 return mSession.getSystemViewInfos(); 71 } 72 73 @Override 74 public Map<Object, PropertiesMap> getDefaultProperties() { 75 return mSession.getDefaultProperties(); 76 } 77 78 @Override 79 public Result render(long timeout, boolean forceMeasure) { 80 try { 81 Bridge.prepareThread(); 82 mLastResult = mSession.acquire(timeout); 83 if (mLastResult.isSuccess()) { 84 if (forceMeasure) { 85 mSession.invalidateRenderingSize(); 86 } 87 mLastResult = mSession.render(false /*freshRender*/); 88 } 89 } finally { 90 mSession.release(); 91 Bridge.cleanupThread(); 92 } 93 94 return mLastResult; 95 } 96 97 @Override 98 public Result animate(Object targetObject, String animationName, 99 boolean isFrameworkAnimation, IAnimationListener listener) { 100 try { 101 Bridge.prepareThread(); 102 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 103 if (mLastResult.isSuccess()) { 104 mLastResult = mSession.animate(targetObject, animationName, isFrameworkAnimation, 105 listener); 106 } 107 } finally { 108 mSession.release(); 109 Bridge.cleanupThread(); 110 } 111 112 return mLastResult; 113 } 114 115 @Override 116 public Result insertChild(Object parentView, ILayoutPullParser childXml, int index, 117 IAnimationListener listener) { 118 if (parentView instanceof ViewGroup == false) { 119 throw new IllegalArgumentException("parentView is not a ViewGroup"); 120 } 121 122 try { 123 Bridge.prepareThread(); 124 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 125 if (mLastResult.isSuccess()) { 126 mLastResult = mSession.insertChild((ViewGroup) parentView, childXml, index, 127 listener); 128 } 129 } finally { 130 mSession.release(); 131 Bridge.cleanupThread(); 132 } 133 134 return mLastResult; 135 } 136 137 138 @Override 139 public Result moveChild(Object parentView, Object childView, int index, 140 Map<String, String> layoutParams, IAnimationListener listener) { 141 if (parentView instanceof ViewGroup == false) { 142 throw new IllegalArgumentException("parentView is not a ViewGroup"); 143 } 144 if (childView instanceof View == false) { 145 throw new IllegalArgumentException("childView is not a View"); 146 } 147 148 try { 149 Bridge.prepareThread(); 150 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 151 if (mLastResult.isSuccess()) { 152 mLastResult = mSession.moveChild((ViewGroup) parentView, (View) childView, index, 153 layoutParams, listener); 154 } 155 } finally { 156 mSession.release(); 157 Bridge.cleanupThread(); 158 } 159 160 return mLastResult; 161 } 162 163 @Override 164 public Result removeChild(Object childView, IAnimationListener listener) { 165 if (childView instanceof View == false) { 166 throw new IllegalArgumentException("childView is not a View"); 167 } 168 169 try { 170 Bridge.prepareThread(); 171 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 172 if (mLastResult.isSuccess()) { 173 mLastResult = mSession.removeChild((View) childView, listener); 174 } 175 } finally { 176 mSession.release(); 177 Bridge.cleanupThread(); 178 } 179 180 return mLastResult; 181 } 182 183 @Override 184 public void setSystemTimeNanos(long nanos) { 185 System_Delegate.setNanosTime(nanos); 186 } 187 188 @Override 189 public void setSystemBootTimeNanos(long nanos) { 190 System_Delegate.setBootTimeNanos(nanos); 191 } 192 193 @Override 194 public void setElapsedFrameTimeNanos(long nanos) { 195 if (mSession != null) { 196 mSession.setElapsedFrameTimeNanos(nanos); 197 } 198 } 199 200 @Override 201 public void dispose() { 202 if (mSession != null) { 203 mSession.dispose(); 204 } 205 } 206 207 /*package*/ BridgeRenderSession(RenderSessionImpl scene, Result lastResult) { 208 mSession = scene; 209 if (scene != null) { 210 mSession.setScene(this); 211 } 212 mLastResult = lastResult; 213 } 214} 215