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