Home | History | Annotate | Download | only in bridge
      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 
     17 package com.android.layoutlib.bridge;
     18 
     19 import com.android.ide.common.rendering.api.IAnimationListener;
     20 import com.android.ide.common.rendering.api.ILayoutPullParser;
     21 import com.android.ide.common.rendering.api.RenderParams;
     22 import com.android.ide.common.rendering.api.RenderSession;
     23 import com.android.ide.common.rendering.api.Result;
     24 import com.android.ide.common.rendering.api.ViewInfo;
     25 import com.android.layoutlib.bridge.impl.RenderSessionImpl;
     26 
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 
     30 import java.awt.image.BufferedImage;
     31 import java.util.List;
     32 import 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  */
     41 public 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 Map<String, String> getDefaultProperties(Object viewObject) {
     68         return mSession.getDefaultProperties(viewObject);
     69     }
     70 
     71     @Override
     72     public Result getProperty(Object objectView, String propertyName) {
     73         // pass
     74         return super.getProperty(objectView, propertyName);
     75     }
     76 
     77     @Override
     78     public Result setProperty(Object objectView, String propertyName, String propertyValue) {
     79         // pass
     80         return super.setProperty(objectView, propertyName, propertyValue);
     81     }
     82 
     83     @Override
     84     public Result render(long timeout) {
     85         try {
     86             Bridge.prepareThread();
     87             mLastResult = mSession.acquire(timeout);
     88             if (mLastResult.isSuccess()) {
     89                 mLastResult = mSession.render(false /*freshRender*/);
     90             }
     91         } finally {
     92             mSession.release();
     93             Bridge.cleanupThread();
     94         }
     95 
     96         return mLastResult;
     97     }
     98 
     99     @Override
    100     public Result animate(Object targetObject, String animationName,
    101             boolean isFrameworkAnimation, IAnimationListener listener) {
    102         try {
    103             Bridge.prepareThread();
    104             mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
    105             if (mLastResult.isSuccess()) {
    106                 mLastResult = mSession.animate(targetObject, animationName, isFrameworkAnimation,
    107                         listener);
    108             }
    109         } finally {
    110             mSession.release();
    111             Bridge.cleanupThread();
    112         }
    113 
    114         return mLastResult;
    115     }
    116 
    117     @Override
    118     public Result insertChild(Object parentView, ILayoutPullParser childXml, int index,
    119             IAnimationListener listener) {
    120         if (parentView instanceof ViewGroup == false) {
    121             throw new IllegalArgumentException("parentView is not a ViewGroup");
    122         }
    123 
    124         try {
    125             Bridge.prepareThread();
    126             mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
    127             if (mLastResult.isSuccess()) {
    128                 mLastResult = mSession.insertChild((ViewGroup) parentView, childXml, index,
    129                         listener);
    130             }
    131         } finally {
    132             mSession.release();
    133             Bridge.cleanupThread();
    134         }
    135 
    136         return mLastResult;
    137     }
    138 
    139 
    140     @Override
    141     public Result moveChild(Object parentView, Object childView, int index,
    142             Map<String, String> layoutParams, IAnimationListener listener) {
    143         if (parentView instanceof ViewGroup == false) {
    144             throw new IllegalArgumentException("parentView is not a ViewGroup");
    145         }
    146         if (childView instanceof View == false) {
    147             throw new IllegalArgumentException("childView is not a View");
    148         }
    149 
    150         try {
    151             Bridge.prepareThread();
    152             mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
    153             if (mLastResult.isSuccess()) {
    154                 mLastResult = mSession.moveChild((ViewGroup) parentView, (View) childView, index,
    155                         layoutParams, listener);
    156             }
    157         } finally {
    158             mSession.release();
    159             Bridge.cleanupThread();
    160         }
    161 
    162         return mLastResult;
    163     }
    164 
    165     @Override
    166     public Result removeChild(Object childView, IAnimationListener listener) {
    167         if (childView instanceof View == false) {
    168             throw new IllegalArgumentException("childView is not a View");
    169         }
    170 
    171         try {
    172             Bridge.prepareThread();
    173             mLastResult = mSession.acquire(RenderParams.DEFAULT_TIMEOUT);
    174             if (mLastResult.isSuccess()) {
    175                 mLastResult = mSession.removeChild((View) childView, listener);
    176             }
    177         } finally {
    178             mSession.release();
    179             Bridge.cleanupThread();
    180         }
    181 
    182         return mLastResult;
    183     }
    184 
    185     @Override
    186     public void dispose() {
    187     }
    188 
    189     /*package*/ BridgeRenderSession(RenderSessionImpl scene, Result lastResult) {
    190         mSession = scene;
    191         if (scene != null) {
    192             mSession.setScene(this);
    193         }
    194         mLastResult = lastResult;
    195     }
    196 }
    197