aboutsummaryrefslogtreecommitdiffstats
path: root/layoutlib_api/sample/src/com/example/android/render/Main.java
blob: 93ee8a902702ffa30e323e62fbcf1e4963bba36d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.render;

import com.android.ide.common.rendering.api.RenderSession;
import com.android.ide.common.rendering.api.Result;
import com.android.ide.common.rendering.api.ViewInfo;
import com.android.ide.common.resources.ResourceItem;
import com.android.ide.common.resources.ResourceRepository;
import com.android.ide.common.resources.ResourceResolver;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.io.FolderWrapper;
import com.android.resources.Density;
import com.android.resources.Keyboard;
import com.android.resources.KeyboardState;
import com.android.resources.Navigation;
import com.android.resources.NavigationState;
import com.android.resources.ScreenOrientation;
import com.android.resources.ScreenRatio;
import com.android.resources.ScreenSize;
import com.android.resources.TouchScreen;

import org.xmlpull.v1.XmlPullParserException;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * Sample code showing how to use the different API used to achieve a layout rendering.
 * This requires the following jar: layoutlib_api.jar, common.jar, ide_common.jar, sdklib.jar (altho
 * we should get rid of this one) and a full SDK (or at leas the platform component).
 *
 */
public class Main {

    // path to the SDK and the project to render
    private final static String SDK = "...<insert>...";
    private final static String PROJECT = "...<insert>...";

    /**
     * @param args
     */
    public static void main(String[] args) {
        // load the factory for a given platform
        File f = new File(SDK + "/platforms/android-3.1");
        RenderServiceFactory factory = RenderServiceFactory.create(f);

        if (factory == null) {
            System.err.println("Failed to load platform rendering library");
            System.exit(1);
        }

        // load the project resources
        ResourceRepository projectRes = new ResourceRepository(false /*isFramework*/) {

            @Override
            protected ResourceItem createResourceItem(String name) {
                return new ResourceItem(name);
            }
        };
        try {
            projectRes.loadResources(new FolderWrapper(PROJECT + "/res"));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        // create the rendering config
        FolderConfiguration config = RenderServiceFactory.createConfig(
                1280, 800, // size 1 and 2. order doesn't matter.
                           // Orientation will drive which is w and h
                ScreenSize.XLARGE,
                ScreenRatio.LONG,
                ScreenOrientation.LANDSCAPE,
                Density.MEDIUM,
                TouchScreen.FINGER,
                KeyboardState.SOFT,
                Keyboard.QWERTY,
                NavigationState.EXPOSED,
                Navigation.NONAV,
                12); // api level

        // create the resource resolver once for the given config.
        ResourceResolver resources = factory.createResourceResolver(
                config, projectRes,
                "Theme", false /*isProjectTheme*/);

        // create the render service
        RenderService renderService = factory.createService(
                resources, config, new ProjectCallback());

        try {
            RenderSession session = renderService
                    .setLog(new StdOutLogger())
                    .setAppInfo("foo", "icon") // optional
                    .createRenderSession("main" /*layoutName*/);

            // get the status of the render
            Result result = session.getResult();
            if (result.isSuccess() == false) {
                System.err.println(result.getErrorMessage());
                System.exit(1);
            }

            // get the image and save it somewhere.
            BufferedImage image = session.getImage();
            ImageIO.write(image, "png", new File("/path/to/test.png"));

            // read the views
            displayViewObjects(session.getRootViews());

            return;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.exit(1);
    }

    private static void displayViewObjects(List<ViewInfo> rootViews) {
        for (ViewInfo info : rootViews) {
            displayView(info, "");
        }
    }

    private static void displayView(ViewInfo info, String indent) {
        // display info data
        System.out.println(indent + info.getClassName() +
                " [" + info.getLeft() + ", " + info.getTop() + ", " +
                info.getRight() + ", " + info.getBottom() + "]");

        // display the children
        List<ViewInfo> children = info.getChildren();
        if (children != null) {
            indent += "\t";
            for (ViewInfo child : children) {
                displayView(child, indent);
            }
        }
    }
}