Home | History | Annotate | Download | only in render
      1 /*
      2  * Copyright (C) 2011 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.example.android.render;
     18 
     19 import com.android.ide.common.rendering.api.RenderSession;
     20 import com.android.ide.common.rendering.api.Result;
     21 import com.android.ide.common.rendering.api.ViewInfo;
     22 import com.android.ide.common.resources.ResourceItem;
     23 import com.android.ide.common.resources.ResourceRepository;
     24 import com.android.ide.common.resources.ResourceResolver;
     25 import com.android.ide.common.resources.configuration.FolderConfiguration;
     26 import com.android.io.FolderWrapper;
     27 import com.android.resources.Density;
     28 import com.android.resources.Keyboard;
     29 import com.android.resources.KeyboardState;
     30 import com.android.resources.Navigation;
     31 import com.android.resources.NavigationState;
     32 import com.android.resources.ScreenOrientation;
     33 import com.android.resources.ScreenRatio;
     34 import com.android.resources.ScreenSize;
     35 import com.android.resources.TouchScreen;
     36 
     37 import org.xmlpull.v1.XmlPullParserException;
     38 
     39 import java.awt.image.BufferedImage;
     40 import java.io.File;
     41 import java.io.FileNotFoundException;
     42 import java.io.IOException;
     43 import java.util.List;
     44 
     45 import javax.imageio.ImageIO;
     46 
     47 /**
     48  * Sample code showing how to use the different API used to achieve a layout rendering.
     49  * This requires the following jar: layoutlib_api.jar, common.jar, ide_common.jar, sdklib.jar (altho
     50  * we should get rid of this one) and a full SDK (or at leas the platform component).
     51  *
     52  */
     53 public class Main {
     54 
     55     // path to the SDK and the project to render
     56     private final static String SDK = "...<insert>...";
     57     private final static String PROJECT = "...<insert>...";
     58 
     59     /**
     60      * @param args
     61      */
     62     public static void main(String[] args) {
     63         // load the factory for a given platform
     64         File f = new File(SDK + "/platforms/android-3.1");
     65         RenderServiceFactory factory = RenderServiceFactory.create(f);
     66 
     67         if (factory == null) {
     68             System.err.println("Failed to load platform rendering library");
     69             System.exit(1);
     70         }
     71 
     72         // load the project resources
     73         ResourceRepository projectRes = new ResourceRepository(false /*isFramework*/) {
     74 
     75             @Override
     76             protected ResourceItem createResourceItem(String name) {
     77                 return new ResourceItem(name);
     78             }
     79         };
     80         try {
     81             projectRes.loadResources(new FolderWrapper(PROJECT + "/res"));
     82         } catch (IOException e) {
     83             e.printStackTrace();
     84             System.exit(1);
     85         }
     86 
     87         // create the rendering config
     88         FolderConfiguration config = RenderServiceFactory.createConfig(
     89                 1280, 800, // size 1 and 2. order doesn't matter.
     90                            // Orientation will drive which is w and h
     91                 ScreenSize.XLARGE,
     92                 ScreenRatio.LONG,
     93                 ScreenOrientation.LANDSCAPE,
     94                 Density.MEDIUM,
     95                 TouchScreen.FINGER,
     96                 KeyboardState.SOFT,
     97                 Keyboard.QWERTY,
     98                 NavigationState.EXPOSED,
     99                 Navigation.NONAV,
    100                 12); // api level
    101 
    102         // create the resource resolver once for the given config.
    103         ResourceResolver resources = factory.createResourceResolver(
    104                 config, projectRes,
    105                 "Theme", false /*isProjectTheme*/);
    106 
    107         // create the render service
    108         RenderService renderService = factory.createService(
    109                 resources, config, new ProjectCallback());
    110 
    111         try {
    112             RenderSession session = renderService
    113                     .setLog(new StdOutLogger())
    114                     .setAppInfo("foo", "icon") // optional
    115                     .createRenderSession("main" /*layoutName*/);
    116 
    117             // get the status of the render
    118             Result result = session.getResult();
    119             if (result.isSuccess() == false) {
    120                 System.err.println(result.getErrorMessage());
    121                 System.exit(1);
    122             }
    123 
    124             // get the image and save it somewhere.
    125             BufferedImage image = session.getImage();
    126             ImageIO.write(image, "png", new File("/path/to/test.png"));
    127 
    128             // read the views
    129             displayViewObjects(session.getRootViews());
    130 
    131             return;
    132         } catch (FileNotFoundException e) {
    133             e.printStackTrace();
    134         } catch (XmlPullParserException e) {
    135             e.printStackTrace();
    136         } catch (IOException e) {
    137             e.printStackTrace();
    138         }
    139 
    140         System.exit(1);
    141     }
    142 
    143     private static void displayViewObjects(List<ViewInfo> rootViews) {
    144         for (ViewInfo info : rootViews) {
    145             displayView(info, "");
    146         }
    147     }
    148 
    149     private static void displayView(ViewInfo info, String indent) {
    150         // display info data
    151         System.out.println(indent + info.getClassName() +
    152                 " [" + info.getLeft() + ", " + info.getTop() + ", " +
    153                 info.getRight() + ", " + info.getBottom() + "]");
    154 
    155         // display the children
    156         List<ViewInfo> children = info.getChildren();
    157         if (children != null) {
    158             indent += "\t";
    159             for (ViewInfo child : children) {
    160                 displayView(child, indent);
    161             }
    162         }
    163     }
    164 }
    165