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.annotation.NonNull; 30import android.annotation.Nullable; 31import android.view.View; 32import android.view.ViewGroup; 33 34import java.awt.image.BufferedImage; 35import java.util.Collections; 36import java.util.List; 37import java.util.Map; 38 39/** 40 * An implementation of {@link RenderSession}. 41 * 42 * This is a pretty basic class that does almost nothing. All of the work is done in 43 * {@link RenderSessionImpl}. 44 * 45 */ 46public class BridgeRenderSession extends RenderSession { 47 48 @Nullable 49 private final RenderSessionImpl mSession; 50 @NonNull 51 private Result mLastResult; 52 53 @Override 54 public Result getResult() { 55 return mLastResult; 56 } 57 58 @Override 59 public BufferedImage getImage() { 60 return mSession != null ? mSession.getImage() : 61 new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); 62 } 63 64 @Override 65 public boolean isAlphaChannelImage() { 66 return mSession != null && mSession.isAlphaChannelImage(); 67 } 68 69 @Override 70 public List<ViewInfo> getRootViews() { 71 return mSession != null ? mSession.getViewInfos() : Collections.emptyList(); 72 } 73 74 @Override 75 public List<ViewInfo> getSystemRootViews() { 76 return mSession != null ? mSession.getSystemViewInfos() : Collections.emptyList(); 77 } 78 79 @Override 80 public Map<Object, PropertiesMap> getDefaultProperties() { 81 return mSession != null ? mSession.getDefaultProperties() : Collections.emptyMap(); 82 } 83 84 @Override 85 public Result measure(long timeout) { 86 if (mSession != null) { 87 try { 88 Bridge.prepareThread(); 89 mLastResult = mSession.acquire(timeout); 90 if (mLastResult.isSuccess()) { 91 mSession.invalidateRenderingSize(); 92 mLastResult = mSession.measure(); 93 } 94 } finally { 95 mSession.release(); 96 Bridge.cleanupThread(); 97 } 98 } 99 100 return mLastResult; 101 } 102 103 @Override 104 public Result render(long timeout, boolean forceMeasure) { 105 if (mSession != null) { 106 try { 107 Bridge.prepareThread(); 108 mLastResult = mSession.acquire(timeout); 109 if (mLastResult.isSuccess()) { 110 if (forceMeasure) { 111 mSession.invalidateRenderingSize(); 112 } 113 mLastResult = mSession.render(false /*freshRender*/); 114 } 115 } finally { 116 mSession.release(); 117 Bridge.cleanupThread(); 118 } 119 } 120 121 return mLastResult; 122 } 123 124 @Override 125 public Result animate(Object targetObject, String animationName, 126 boolean isFrameworkAnimation, IAnimationListener listener) { 127 if (mSession != null) { 128 try { 129 Bridge.prepareThread(); 130 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 131 if (mLastResult.isSuccess()) { 132 mLastResult = mSession.animate(targetObject, animationName, isFrameworkAnimation, 133 listener); 134 } 135 } finally { 136 mSession.release(); 137 Bridge.cleanupThread(); 138 } 139 } 140 141 return mLastResult; 142 } 143 144 @Override 145 public Result insertChild(Object parentView, ILayoutPullParser childXml, int index, 146 IAnimationListener listener) { 147 if (!(parentView instanceof ViewGroup)) { 148 throw new IllegalArgumentException("parentView is not a ViewGroup"); 149 } 150 151 if (mSession != null) { 152 try { 153 Bridge.prepareThread(); 154 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 155 if (mLastResult.isSuccess()) { 156 mLastResult = 157 mSession.insertChild((ViewGroup) parentView, childXml, index, listener); 158 } 159 } finally { 160 mSession.release(); 161 Bridge.cleanupThread(); 162 } 163 } 164 165 return mLastResult; 166 } 167 168 169 @Override 170 public Result moveChild(Object parentView, Object childView, int index, 171 Map<String, String> layoutParams, IAnimationListener listener) { 172 if (!(parentView instanceof ViewGroup)) { 173 throw new IllegalArgumentException("parentView is not a ViewGroup"); 174 } 175 if (!(childView instanceof View)) { 176 throw new IllegalArgumentException("childView is not a View"); 177 } 178 179 if (mSession != null) { 180 try { 181 Bridge.prepareThread(); 182 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 183 if (mLastResult.isSuccess()) { 184 mLastResult = mSession.moveChild((ViewGroup) parentView, (View) childView, index, 185 layoutParams, listener); 186 } 187 } finally { 188 mSession.release(); 189 Bridge.cleanupThread(); 190 } 191 } 192 193 return mLastResult; 194 } 195 196 @Override 197 public Result removeChild(Object childView, IAnimationListener listener) { 198 if (!(childView instanceof View)) { 199 throw new IllegalArgumentException("childView is not a View"); 200 } 201 202 if (mSession != null) { 203 try { 204 Bridge.prepareThread(); 205 mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT); 206 if (mLastResult.isSuccess()) { 207 mLastResult = mSession.removeChild((View) childView, listener); 208 } 209 } finally { 210 mSession.release(); 211 Bridge.cleanupThread(); 212 } 213 } 214 215 return mLastResult; 216 } 217 218 @Override 219 public void setSystemTimeNanos(long nanos) { 220 System_Delegate.setNanosTime(nanos); 221 } 222 223 @Override 224 public void setSystemBootTimeNanos(long nanos) { 225 System_Delegate.setBootTimeNanos(nanos); 226 } 227 228 @Override 229 public void setElapsedFrameTimeNanos(long nanos) { 230 if (mSession != null) { 231 mSession.setElapsedFrameTimeNanos(nanos); 232 } 233 } 234 235 @Override 236 public void dispose() { 237 if (mSession != null) { 238 mSession.dispose(); 239 } 240 } 241 242 /*package*/ BridgeRenderSession(@Nullable RenderSessionImpl scene, @NonNull Result lastResult) { 243 mSession = scene; 244 if (scene != null) { 245 mSession.setScene(this); 246 } 247 mLastResult = lastResult; 248 } 249} 250