BridgeRenderSession.java revision 54ffb4358c3d6f2f0f55e4b39f524375f6ffb12f
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 measure(long timeout) { 80 try { 81 Bridge.prepareThread(); 82 mLastResult = mSession.acquire(timeout); 83 if (mLastResult.isSuccess()) { 84 mSession.invalidateRenderingSize(); 85 mLastResult = mSession.measure(); 86 } 87 } finally { 88 mSession.release(); 89 Bridge.cleanupThread(); 90 } 91 92 return mLastResult; 93 } 94 95 @Override 96 public Result render(long timeout, boolean forceMeasure) { 97 try { 98 Bridge.prepareThread(); 99 mLastResult = mSession.acquire(timeout); 100 if (mLastResult.isSuccess()) { 101 if (forceMeasure) { 102 mSession.invalidateRenderingSize(); 103 } 104 mLastResult = mSession.render(false /*freshRender*/); 105 } 106 } finally { 107 mSession.release(); 108 Bridge.cleanupThread(); 109 } 110 111 return mLastResult; 112 } 113 114 @Override 115 public Result animate(Object targetObject, String animationName, 116 boolean isFrameworkAnimation, IAnimationListener listener) { 117 try { 118 Bridge.prepareThread(); 119 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 120 if (mLastResult.isSuccess()) { 121 mLastResult = mSession.animate(targetObject, animationName, isFrameworkAnimation, 122 listener); 123 } 124 } finally { 125 mSession.release(); 126 Bridge.cleanupThread(); 127 } 128 129 return mLastResult; 130 } 131 132 @Override 133 public Result insertChild(Object parentView, ILayoutPullParser childXml, int index, 134 IAnimationListener listener) { 135 if (parentView instanceof ViewGroup == false) { 136 throw new IllegalArgumentException("parentView is not a ViewGroup"); 137 } 138 139 try { 140 Bridge.prepareThread(); 141 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 142 if (mLastResult.isSuccess()) { 143 mLastResult = mSession.insertChild((ViewGroup) parentView, childXml, index, 144 listener); 145 } 146 } finally { 147 mSession.release(); 148 Bridge.cleanupThread(); 149 } 150 151 return mLastResult; 152 } 153 154 155 @Override 156 public Result moveChild(Object parentView, Object childView, int index, 157 Map<String, String> layoutParams, IAnimationListener listener) { 158 if (parentView instanceof ViewGroup == false) { 159 throw new IllegalArgumentException("parentView is not a ViewGroup"); 160 } 161 if (childView instanceof View == false) { 162 throw new IllegalArgumentException("childView is not a View"); 163 } 164 165 try { 166 Bridge.prepareThread(); 167 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 168 if (mLastResult.isSuccess()) { 169 mLastResult = mSession.moveChild((ViewGroup) parentView, (View) childView, index, 170 layoutParams, listener); 171 } 172 } finally { 173 mSession.release(); 174 Bridge.cleanupThread(); 175 } 176 177 return mLastResult; 178 } 179 180 @Override 181 public Result removeChild(Object childView, IAnimationListener listener) { 182 if (childView instanceof View == false) { 183 throw new IllegalArgumentException("childView is not a View"); 184 } 185 186 try { 187 Bridge.prepareThread(); 188 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 189 if (mLastResult.isSuccess()) { 190 mLastResult = mSession.removeChild((View) childView, listener); 191 } 192 } finally { 193 mSession.release(); 194 Bridge.cleanupThread(); 195 } 196 197 return mLastResult; 198 } 199 200 @Override 201 public void setSystemTimeNanos(long nanos) { 202 System_Delegate.setNanosTime(nanos); 203 } 204 205 @Override 206 public void setSystemBootTimeNanos(long nanos) { 207 System_Delegate.setBootTimeNanos(nanos); 208 } 209 210 @Override 211 public void setElapsedFrameTimeNanos(long nanos) { 212 if (mSession != null) { 213 mSession.setElapsedFrameTimeNanos(nanos); 214 } 215 } 216 217 @Override 218 public void dispose() { 219 if (mSession != null) { 220 mSession.dispose(); 221 } 222 } 223 224 /*package*/ BridgeRenderSession(RenderSessionImpl scene, Result lastResult) { 225 mSession = scene; 226 if (scene != null) { 227 mSession.setScene(this); 228 } 229 mLastResult = lastResult; 230 } 231} 232