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