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, boolean forceMeasure) {
90        try {
91            Bridge.prepareThread();
92            mLastResult = mSession.acquire(timeout);
93            if (mLastResult.isSuccess()) {
94                if (forceMeasure) {
95                    mSession.invalidateRenderingSize();
96                }
97                mLastResult = mSession.render(false /*freshRender*/);
98            }
99        } finally {
100            mSession.release();
101            Bridge.cleanupThread();
102        }
103
104        return mLastResult;
105    }
106
107    @Override
108    public Result animate(Object targetObject, String animationName,
109            boolean isFrameworkAnimation, IAnimationListener listener) {
110        try {
111            Bridge.prepareThread();
112            mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
113            if (mLastResult.isSuccess()) {
114                mLastResult = mSession.animate(targetObject, animationName, isFrameworkAnimation,
115                        listener);
116            }
117        } finally {
118            mSession.release();
119            Bridge.cleanupThread();
120        }
121
122        return mLastResult;
123    }
124
125    @Override
126    public Result insertChild(Object parentView, ILayoutPullParser childXml, int index,
127            IAnimationListener listener) {
128        if (parentView instanceof ViewGroup == false) {
129            throw new IllegalArgumentException("parentView is not a ViewGroup");
130        }
131
132        try {
133            Bridge.prepareThread();
134            mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
135            if (mLastResult.isSuccess()) {
136                mLastResult = mSession.insertChild((ViewGroup) parentView, childXml, index,
137                        listener);
138            }
139        } finally {
140            mSession.release();
141            Bridge.cleanupThread();
142        }
143
144        return mLastResult;
145    }
146
147
148    @Override
149    public Result moveChild(Object parentView, Object childView, int index,
150            Map<String, String> layoutParams, IAnimationListener listener) {
151        if (parentView instanceof ViewGroup == false) {
152            throw new IllegalArgumentException("parentView is not a ViewGroup");
153        }
154        if (childView instanceof View == false) {
155            throw new IllegalArgumentException("childView is not a View");
156        }
157
158        try {
159            Bridge.prepareThread();
160            mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
161            if (mLastResult.isSuccess()) {
162                mLastResult = mSession.moveChild((ViewGroup) parentView, (View) childView, index,
163                        layoutParams, listener);
164            }
165        } finally {
166            mSession.release();
167            Bridge.cleanupThread();
168        }
169
170        return mLastResult;
171    }
172
173    @Override
174    public Result removeChild(Object childView, IAnimationListener listener) {
175        if (childView instanceof View == false) {
176            throw new IllegalArgumentException("childView is not a View");
177        }
178
179        try {
180            Bridge.prepareThread();
181            mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
182            if (mLastResult.isSuccess()) {
183                mLastResult = mSession.removeChild((View) childView, listener);
184            }
185        } finally {
186            mSession.release();
187            Bridge.cleanupThread();
188        }
189
190        return mLastResult;
191    }
192
193    @Override
194    public void dispose() {
195    }
196
197    /*package*/ BridgeRenderSession(RenderSessionImpl scene, Result lastResult) {
198        mSession = scene;
199        if (scene != null) {
200            mSession.setScene(this);
201        }
202        mLastResult = lastResult;
203    }
204}
205