aboutsummaryrefslogtreecommitdiff
path: root/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/ConfigGenerator.java
blob: 684cf12c4ed696da52b410601d3c6fdd68fb266d (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (C) 2014 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.android.layoutlib.bridge.intensive.setup;

import com.android.ide.common.rendering.api.HardwareConfig;
import com.android.ide.common.resources.configuration.CountryCodeQualifier;
import com.android.ide.common.resources.configuration.DensityQualifier;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.ide.common.resources.configuration.KeyboardStateQualifier;
import com.android.ide.common.resources.configuration.LayoutDirectionQualifier;
import com.android.ide.common.resources.configuration.LocaleQualifier;
import com.android.ide.common.resources.configuration.NavigationMethodQualifier;
import com.android.ide.common.resources.configuration.NetworkCodeQualifier;
import com.android.ide.common.resources.configuration.NightModeQualifier;
import com.android.ide.common.resources.configuration.ScreenDimensionQualifier;
import com.android.ide.common.resources.configuration.ScreenOrientationQualifier;
import com.android.ide.common.resources.configuration.ScreenRatioQualifier;
import com.android.ide.common.resources.configuration.ScreenSizeQualifier;
import com.android.ide.common.resources.configuration.TextInputMethodQualifier;
import com.android.ide.common.resources.configuration.TouchScreenQualifier;
import com.android.ide.common.resources.configuration.UiModeQualifier;
import com.android.ide.common.resources.configuration.VersionQualifier;
import com.android.resources.Density;
import com.android.resources.Keyboard;
import com.android.resources.KeyboardState;
import com.android.resources.Navigation;
import com.android.resources.NightMode;
import com.android.resources.ScreenOrientation;
import com.android.resources.ScreenRatio;
import com.android.resources.ScreenSize;
import com.android.resources.TouchScreen;
import com.android.resources.UiMode;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import com.google.android.collect.Maps;

/**
 * Provides {@link FolderConfiguration} and {@link HardwareConfig} for various devices. Also
 * provides utility methods to parse build.prop and attrs.xml to generate the appropriate maps.
 */
@SuppressWarnings("UnusedDeclaration") // For the pre-configured nexus generators.
public class ConfigGenerator {

    public static final ConfigGenerator NEXUS_4 = new ConfigGenerator();

    public static final ConfigGenerator NEXUS_5 = new ConfigGenerator()
                                                        .setScreenHeight(1920)
                                                        .setScreenWidth(1080)
                                                        .setXdpi(445)
                                                        .setYdpi(445)
                                                        .setOrientation(ScreenOrientation.PORTRAIT)
                                                        .setDensity(Density.XXHIGH)
                                                        .setRatio(ScreenRatio.NOTLONG)
                                                        .setSize(ScreenSize.NORMAL)
                                                        .setKeyboard(Keyboard.NOKEY)
                                                        .setTouchScreen(TouchScreen.FINGER)
                                                        .setKeyboardState(KeyboardState.SOFT)
                                                        .setSoftButtons(true)
                                                        .setNavigation(Navigation.NONAV);

    public static final ConfigGenerator NEXUS_7 = new ConfigGenerator()
                                                        .setScreenHeight(1920)
                                                        .setScreenWidth(1200)
                                                        .setXdpi(323)
                                                        .setYdpi(323)
                                                        .setOrientation(ScreenOrientation.PORTRAIT)
                                                        .setDensity(Density.XHIGH)
                                                        .setRatio(ScreenRatio.NOTLONG)
                                                        .setSize(ScreenSize.LARGE)
                                                        .setKeyboard(Keyboard.NOKEY)
                                                        .setTouchScreen(TouchScreen.FINGER)
                                                        .setKeyboardState(KeyboardState.SOFT)
                                                        .setSoftButtons(true)
                                                        .setNavigation(Navigation.NONAV);

    public static final ConfigGenerator NEXUS_10 = new ConfigGenerator()
                                                        .setScreenHeight(1600)
                                                        .setScreenWidth(2560)
                                                        .setXdpi(300)
                                                        .setYdpi(300)
                                                        .setOrientation(ScreenOrientation.LANDSCAPE)
                                                        .setDensity(Density.XHIGH)
                                                        .setRatio(ScreenRatio.NOTLONG)
                                                        .setSize(ScreenSize.XLARGE)
                                                        .setKeyboard(Keyboard.NOKEY)
                                                        .setTouchScreen(TouchScreen.FINGER)
                                                        .setKeyboardState(KeyboardState.SOFT)
                                                        .setSoftButtons(true)
                                                        .setNavigation(Navigation.NONAV);

    public static final ConfigGenerator NEXUS_5_LAND = new ConfigGenerator()
                                                        .setScreenHeight(1080)
                                                        .setScreenWidth(1920)
                                                        .setXdpi(445)
                                                        .setYdpi(445)
                                                        .setOrientation(ScreenOrientation.LANDSCAPE)
                                                        .setDensity(Density.XXHIGH)
                                                        .setRatio(ScreenRatio.NOTLONG)
                                                        .setSize(ScreenSize.NORMAL)
                                                        .setKeyboard(Keyboard.NOKEY)
                                                        .setTouchScreen(TouchScreen.FINGER)
                                                        .setKeyboardState(KeyboardState.SOFT)
                                                        .setSoftButtons(true)
                                                        .setNavigation(Navigation.NONAV);

    public static final ConfigGenerator NEXUS_7_2012 = new ConfigGenerator()
                                                        .setScreenHeight(1280)
                                                        .setScreenWidth(800)
                                                        .setXdpi(195)
                                                        .setYdpi(200)
                                                        .setOrientation(ScreenOrientation.PORTRAIT)
                                                        .setDensity(Density.TV)
                                                        .setRatio(ScreenRatio.NOTLONG)
                                                        .setSize(ScreenSize.LARGE)
                                                        .setKeyboard(Keyboard.NOKEY)
                                                        .setTouchScreen(TouchScreen.FINGER)
                                                        .setKeyboardState(KeyboardState.SOFT)
                                                        .setSoftButtons(true)
                                                        .setNavigation(Navigation.NONAV);

    private static final String TAG_ATTR = "attr";
    private static final String TAG_ENUM = "enum";
    private static final String TAG_FLAG = "flag";
    private static final String ATTR_NAME = "name";
    private static final String ATTR_VALUE = "value";

    // Device Configuration. Defaults are for a Nexus 4 device.
    private int mScreenHeight = 1280;
    private int mScreenWidth = 768;
    private int mXdpi = 320;
    private int mYdpi = 320;
    private ScreenOrientation mOrientation = ScreenOrientation.PORTRAIT;
    private Density mDensity = Density.XHIGH;
    private ScreenRatio mRatio = ScreenRatio.NOTLONG;
    private ScreenSize mSize = ScreenSize.NORMAL;
    private Keyboard mKeyboard = Keyboard.NOKEY;
    private TouchScreen mTouchScreen = TouchScreen.FINGER;
    private KeyboardState mKeyboardState = KeyboardState.SOFT;
    private boolean mSoftButtons = true;
    private Navigation mNavigation = Navigation.NONAV;

    public FolderConfiguration getFolderConfig() {
        FolderConfiguration config = FolderConfiguration.createDefault();
        config.setDensityQualifier(new DensityQualifier(mDensity));
        config.setNavigationMethodQualifier(new NavigationMethodQualifier(mNavigation));
        if (mScreenWidth > mScreenHeight) {
            config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenWidth,
                    mScreenHeight));
        } else {
            config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenHeight,
                    mScreenWidth));
        }
        config.setScreenRatioQualifier(new ScreenRatioQualifier(mRatio));
        config.setScreenSizeQualifier(new ScreenSizeQualifier(mSize));
        config.setTextInputMethodQualifier(new TextInputMethodQualifier(mKeyboard));
        config.setTouchTypeQualifier(new TouchScreenQualifier(mTouchScreen));
        config.setKeyboardStateQualifier(new KeyboardStateQualifier(mKeyboardState));
        config.setScreenOrientationQualifier(new ScreenOrientationQualifier(mOrientation));

        config.updateScreenWidthAndHeight();

        // some default qualifiers.
        config.setUiModeQualifier(new UiModeQualifier(UiMode.NORMAL));
        config.setNightModeQualifier(new NightModeQualifier(NightMode.NOTNIGHT));
        config.setCountryCodeQualifier(new CountryCodeQualifier());
        config.setLayoutDirectionQualifier(new LayoutDirectionQualifier());
        config.setNetworkCodeQualifier(new NetworkCodeQualifier());
        config.setLocaleQualifier(new LocaleQualifier());
        config.setVersionQualifier(new VersionQualifier());
        return config;
    }

    public HardwareConfig getHardwareConfig() {
        return new HardwareConfig(mScreenWidth, mScreenHeight, mDensity, mXdpi, mYdpi, mSize,
                mOrientation, null, mSoftButtons);
    }

    public static Map<String, String> loadProperties(File path) {
        Properties p = new Properties();
        Map<String, String> map = Maps.newHashMap();
        try {
            p.load(new FileInputStream(path));
            for (String key : p.stringPropertyNames()) {
                map.put(key, p.getProperty(key));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    public static Map<String, Map<String, Integer>> getEnumMap(File path) {
        Map<String, Map<String, Integer>> map = Maps.newHashMap();
        try {
            XmlPullParser xmlPullParser = XmlPullParserFactory.newInstance().newPullParser();
            xmlPullParser.setInput(new FileInputStream(path), null);
            int eventType = xmlPullParser.getEventType();
            String attr = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if (TAG_ATTR.equals(xmlPullParser.getName())) {
                        attr = xmlPullParser.getAttributeValue(null, ATTR_NAME);
                    } else if (TAG_ENUM.equals(xmlPullParser.getName())
                            || TAG_FLAG.equals(xmlPullParser.getName())) {
                        String name = xmlPullParser.getAttributeValue(null, ATTR_NAME);
                        String value = xmlPullParser.getAttributeValue(null, ATTR_VALUE);
                        // Integer.decode cannot handle "ffffffff", see JDK issue 6624867
                        int i = (int) (long) Long.decode(value);
                        assert attr != null;
                        Map<String, Integer> attributeMap = map.get(attr);
                        if (attributeMap == null) {
                            attributeMap = Maps.newHashMap();
                            map.put(attr, attributeMap);
                        }
                        attributeMap.put(name, i);
                    }
                } else if (eventType == XmlPullParser.END_TAG) {
                    if (TAG_ATTR.equals(xmlPullParser.getName())) {
                        attr = null;
                    }
                }
                eventType = xmlPullParser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    // Methods to set the configuration values.

    public ConfigGenerator setScreenHeight(int height) {
        mScreenHeight = height;
        return this;
    }

    public ConfigGenerator setScreenWidth(int width) {
        mScreenWidth = width;
        return this;
    }

    public ConfigGenerator setXdpi(int xdpi) {
        mXdpi = xdpi;
        return this;
    }

    public ConfigGenerator setYdpi(int ydpi) {
        mYdpi = ydpi;
        return this;
    }

    public ConfigGenerator setOrientation(ScreenOrientation orientation) {
        mOrientation = orientation;
        return this;
    }

    public ConfigGenerator setDensity(Density density) {
        mDensity = density;
        return this;
    }

    public ConfigGenerator setRatio(ScreenRatio ratio) {
        mRatio = ratio;
        return this;
    }

    public ConfigGenerator setSize(ScreenSize size) {
        mSize = size;
        return this;
    }

    public ConfigGenerator setKeyboard(Keyboard keyboard) {
        mKeyboard = keyboard;
        return this;
    }

    public ConfigGenerator setTouchScreen(TouchScreen touchScreen) {
        mTouchScreen = touchScreen;
        return this;
    }

    public ConfigGenerator setKeyboardState(KeyboardState state) {
        mKeyboardState = state;
        return this;
    }

    public ConfigGenerator setSoftButtons(boolean softButtons) {
        mSoftButtons = softButtons;
        return this;
    }

    public ConfigGenerator setNavigation(Navigation navigation) {
        mNavigation = navigation;
        return this;
    }
}