aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/configuration/ConfigurationDescription.java
blob: cc503987fb3cdb0f48cd31e888fa51e66daeb18e (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.ide.eclipse.adt.internal.editors.layout.configuration;

import static com.android.SdkConstants.ANDROID_STYLE_RESOURCE_PREFIX;
import static com.android.SdkConstants.ATTR_NAME;
import static com.android.SdkConstants.ATTR_THEME;
import static com.android.SdkConstants.PREFIX_RESOURCE_REF;
import static com.android.SdkConstants.STYLE_RESOURCE_PREFIX;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.common.resources.ResourceRepository;
import com.android.ide.common.resources.configuration.DeviceConfigHelper;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.ide.common.resources.configuration.LanguageQualifier;
import com.android.ide.common.resources.configuration.RegionQualifier;
import com.android.ide.common.resources.configuration.ScreenSizeQualifier;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo;
import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo.ActivityAttributes;
import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.resources.NightMode;
import com.android.resources.ResourceFolderType;
import com.android.resources.ScreenSize;
import com.android.resources.UiMode;
import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.devices.Device;
import com.android.sdklib.devices.State;
import com.google.common.base.Splitter;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.QualifiedName;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.List;

/** A description of a configuration, used for persistence */
public class ConfigurationDescription {
    private static final String TAG_PREVIEWS = "previews";    //$NON-NLS-1$
    private static final String TAG_PREVIEW = "preview";      //$NON-NLS-1$
    private static final String ATTR_TARGET = "target";       //$NON-NLS-1$
    private static final String ATTR_CONFIG = "config";       //$NON-NLS-1$
    private static final String ATTR_LOCALE = "locale";       //$NON-NLS-1$
    private static final String ATTR_ACTIVITY = "activity";   //$NON-NLS-1$
    private static final String ATTR_DEVICE = "device";       //$NON-NLS-1$
    private static final String ATTR_STATE = "devicestate";   //$NON-NLS-1$
    private static final String ATTR_UIMODE = "ui";           //$NON-NLS-1$
    private static final String ATTR_NIGHTMODE = "night";     //$NON-NLS-1$
    private final static String SEP_LOCALE = "-";             //$NON-NLS-1$

    /**
     * Settings name for file-specific configuration preferences, such as which theme or
     * device to render the current layout with
     */
    public final static QualifiedName NAME_CONFIG_STATE =
        new QualifiedName(AdtPlugin.PLUGIN_ID, "state");//$NON-NLS-1$

    /** The project corresponding to this configuration's description */
    public final IProject project;

    /** The display name */
    public String displayName;

    /** The theme */
    public String theme;

    /** The target */
    public IAndroidTarget target;

    /** The display name */
    public FolderConfiguration folder;

    /** The locale */
    public Locale locale = Locale.ANY;

    /** The device */
    public Device device;

    /** The device state */
    public State state;

    /** The activity */
    public String activity;

    /** UI mode */
    @NonNull
    public UiMode uiMode = UiMode.NORMAL;

    /** Night mode */
    @NonNull
    public NightMode nightMode = NightMode.NOTNIGHT;

    private ConfigurationDescription(@Nullable IProject project) {
        this.project = project;
    }

    /**
     * Returns the persistent configuration description from the given file
     *
     * @param file the file to look up a description from
     * @return the description or null if never written
     */
    @Nullable
    public static String getDescription(@NonNull IFile file) {
        return AdtPlugin.getFileProperty(file, NAME_CONFIG_STATE);
    }

    /**
     * Sets the persistent configuration description data for the given file
     *
     * @param file the file to associate the description with
     * @param description the description
     */
    public static void setDescription(@NonNull IFile file, @NonNull String description) {
        AdtPlugin.setFileProperty(file, NAME_CONFIG_STATE, description);
    }

    /**
     * Creates a description from a given configuration
     *
     * @param project the project for this configuration's description
     * @param configuration the configuration to describe
     * @return a new configuration
     */
    public static ConfigurationDescription fromConfiguration(
            @Nullable IProject project,
            @NonNull Configuration configuration) {
        ConfigurationDescription description = new ConfigurationDescription(project);
        description.displayName = configuration.getDisplayName();
        description.theme = configuration.getTheme();
        description.target = configuration.getTarget();
        description.folder = new FolderConfiguration();
        description.folder.set(configuration.getFullConfig());
        description.locale = configuration.getLocale();
        description.device = configuration.getDevice();
        description.state = configuration.getDeviceState();
        description.activity = configuration.getActivity();
        return description;
    }

    /**
     * Initializes a string previously created with
     * {@link #toXml(Document)}
     *
     * @param project the project for this configuration's description
     * @param element the element to read back from
     * @param deviceList list of available devices
     * @return true if the configuration was initialized
     */
    @Nullable
    public static ConfigurationDescription fromXml(
            @Nullable IProject project,
            @NonNull Element element,
            @NonNull List<Device> deviceList) {
        ConfigurationDescription description = new ConfigurationDescription(project);

        if (!TAG_PREVIEW.equals(element.getTagName())) {
            return null;
        }

        String displayName = element.getAttribute(ATTR_NAME);
        if (!displayName.isEmpty()) {
            description.displayName = displayName;
        }

        String config = element.getAttribute(ATTR_CONFIG);
        Iterable<String> segments = Splitter.on('-').split(config);
        description.folder = FolderConfiguration.getConfig(segments);

        String theme = element.getAttribute(ATTR_THEME);
        if (!theme.isEmpty()) {
            description.theme = theme;
        }

        String targetId = element.getAttribute(ATTR_TARGET);
        if (!targetId.isEmpty()) {
            IAndroidTarget target = Configuration.stringToTarget(targetId);
            description.target = target;
        }

        String localeString = element.getAttribute(ATTR_LOCALE);
        if (!localeString.isEmpty()) {
            // Load locale. Note that this can get overwritten by the
            // project-wide settings read below.
            LanguageQualifier language = Locale.ANY_LANGUAGE;
            RegionQualifier region = Locale.ANY_REGION;
            String locales[] = localeString.split(SEP_LOCALE);
            if (locales[0].length() > 0) {
                language = new LanguageQualifier(locales[0]);
            }
            if (locales.length > 1 && locales[1].length() > 0) {
                region = new RegionQualifier(locales[1]);
            }
            description.locale = Locale.create(language, region);
        }

        String activity = element.getAttribute(ATTR_ACTIVITY);
        if (activity.isEmpty()) {
            activity = null;
        }

        String deviceString = element.getAttribute(ATTR_DEVICE);
        if (!deviceString.isEmpty()) {
            for (Device d : deviceList) {
                if (d.getName().equals(deviceString)) {
                    description.device = d;
                    String stateName = element.getAttribute(ATTR_STATE);
                    if (stateName.isEmpty() || stateName.equals("null")) {
                        description.state = Configuration.getState(d, stateName);
                    } else if (d.getAllStates().size() > 0) {
                        description.state = d.getAllStates().get(0);
                    }
                    break;
                }
            }
        }

        String uiModeString = element.getAttribute(ATTR_UIMODE);
        if (!uiModeString.isEmpty()) {
            description.uiMode = UiMode.getEnum(uiModeString);
            if (description.uiMode == null) {
                description.uiMode = UiMode.NORMAL;
            }
        }

        String nightModeString = element.getAttribute(ATTR_NIGHTMODE);
        if (!nightModeString.isEmpty()) {
            description.nightMode = NightMode.getEnum(nightModeString);
            if (description.nightMode == null) {
                description.nightMode = NightMode.NOTNIGHT;
            }
        }


        // Should I really be storing the FULL configuration? Might be trouble if
        // you bring a different device

        return description;
    }

    /**
     * Write this description into the given document as a new element.
     *
     * @param document the document to add the description to
     * @return the newly inserted element
     */
    @NonNull
    public Element toXml(Document document) {
        Element element = document.createElement(TAG_PREVIEW);

        element.setAttribute(ATTR_NAME, displayName);
        FolderConfiguration fullConfig = folder;
        String folderName = fullConfig.getFolderName(ResourceFolderType.LAYOUT);
        element.setAttribute(ATTR_CONFIG, folderName);
        if (theme != null) {
            element.setAttribute(ATTR_THEME, theme);
        }
        if (target != null) {
            element.setAttribute(ATTR_TARGET, Configuration.targetToString(target));
        }

        if (locale != null && (locale.hasLanguage() || locale.hasRegion())) {
            String value;
            if (locale.hasRegion()) {
                value = locale.language.getValue() + SEP_LOCALE + locale.region.getValue();
            } else {
                value = locale.language.getValue();
            }
            element.setAttribute(ATTR_LOCALE, value);
        }

        if (device != null) {
            element.setAttribute(ATTR_DEVICE, device.getName());
            if (state != null) {
                element.setAttribute(ATTR_STATE, state.getName());
            }
        }

        if (activity != null) {
            element.setAttribute(ATTR_ACTIVITY, activity);
        }

        if (uiMode != null && uiMode != UiMode.NORMAL) {
            element.setAttribute(ATTR_UIMODE, uiMode.getResourceValue());
        }

        if (nightMode != null && nightMode != NightMode.NOTNIGHT) {
            element.setAttribute(ATTR_NIGHTMODE, nightMode.getResourceValue());
        }

        Element parent = document.getDocumentElement();
        if (parent == null) {
            parent = document.createElement(TAG_PREVIEWS);
            document.appendChild(parent);
        }
        parent.appendChild(element);

        return element;
    }

    /** Returns the preferred theme, or null */
    @Nullable
    String computePreferredTheme() {
        if (project == null) {
            return "Theme";
        }
        ManifestInfo manifest = ManifestInfo.get(project);

        // Look up the screen size for the current state
        ScreenSize screenSize = null;
        if (device != null) {
            List<State> states = device.getAllStates();
            for (State s : states) {
                FolderConfiguration folderConfig = DeviceConfigHelper.getFolderConfig(s);
                if (folderConfig != null) {
                    ScreenSizeQualifier qualifier = folderConfig.getScreenSizeQualifier();
                    screenSize = qualifier.getValue();
                    break;
                }
            }
        }

        // Look up the default/fallback theme to use for this project (which
        // depends on the screen size when no particular theme is specified
        // in the manifest)
        String defaultTheme = manifest.getDefaultTheme(target, screenSize);

        String preferred = defaultTheme;
        if (theme == null) {
            // If we are rendering a layout in included context, pick the theme
            // from the outer layout instead

            if (activity != null) {
                ActivityAttributes attributes = manifest.getActivityAttributes(activity);
                if (attributes != null) {
                    preferred = attributes.getTheme();
                }
            }
            if (preferred == null) {
                preferred = defaultTheme;
            }
            theme = preferred;
        }

        return preferred;
    }

    private void checkThemePrefix() {
        if (theme != null && !theme.startsWith(PREFIX_RESOURCE_REF)) {
            if (theme.isEmpty()) {
                computePreferredTheme();
                return;
            }

            if (target != null) {
                Sdk sdk = Sdk.getCurrent();
                if (sdk != null) {
                    AndroidTargetData data = sdk.getTargetData(target);

                    if (data != null) {
                        ResourceRepository resources = data.getFrameworkResources();
                        if (resources != null
                            && resources.hasResourceItem(ANDROID_STYLE_RESOURCE_PREFIX + theme)) {
                            theme = ANDROID_STYLE_RESOURCE_PREFIX + theme;
                            return;
                        }
                    }
                }
            }

            theme = STYLE_RESOURCE_PREFIX + theme;
        }
    }
}