aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/configuration/NestedConfiguration.java
blob: 50778e2f13f7d9ec70cd770b6a503b46c3c11bcc (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 * 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 com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.resources.NightMode;
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.Objects;

/**
 * An {@linkplain NestedConfiguration} is a {@link Configuration} which inherits
 * all of its values from a different configuration, except for one or more
 * attributes where it overrides a custom value.
 * <p>
 * Unlike a {@link VaryingConfiguration}, a {@linkplain NestedConfiguration}
 * will always return the same overridden value, regardless of the inherited
 * value.
 * <p>
 * For example, an {@linkplain NestedConfiguration} may fix the locale to always
 * be "en", but otherwise inherit everything else.
 */
public class NestedConfiguration extends Configuration {
    /** The configuration we are inheriting non-overridden values from */
    protected Configuration mParent;

    /** Bitmask of attributes to be overridden in this configuration */
    private int mOverride;

    /**
     * Constructs a new {@linkplain NestedConfiguration}.
     * Construct via
     *
     * @param chooser the associated chooser
     * @param configuration the configuration to inherit from
     */
    protected NestedConfiguration(
            @NonNull ConfigurationChooser chooser,
            @NonNull Configuration configuration) {
        super(chooser);
        mParent = configuration;

        mFullConfig.set(mParent.mFullConfig);
        if (mParent.getEditedConfig() != null) {
            mEditedConfig = new FolderConfiguration();
            mEditedConfig.set(mParent.mEditedConfig);
        }
    }

    /**
     * Returns the override flags for this configuration. Corresponds to
     * the {@code CFG_} flags in {@link ConfigurationClient}.
     *
     * @return the bitmask
     */
    public int getOverrideFlags() {
        return mOverride;
    }

    /**
     * Creates a new {@linkplain NestedConfiguration} that has the same overriding
     * attributes as the given other {@linkplain NestedConfiguration}, and gets
     * its values from the given {@linkplain Configuration}.
     *
     * @param other the configuration to copy overrides from
     * @param values the configuration to copy values from
     * @param parent the parent to tie the configuration to for inheriting values
     * @return a new configuration
     */
    @NonNull
    public static NestedConfiguration create(
            @NonNull NestedConfiguration other,
            @NonNull Configuration values,
            @NonNull Configuration parent) {
        NestedConfiguration configuration =
                new NestedConfiguration(other.mConfigChooser, parent);
        initFrom(configuration, other, values, true /*sync*/);
        return configuration;
    }

    /**
     * Initializes a new {@linkplain NestedConfiguration} with the overriding
     * attributes as the given other {@linkplain NestedConfiguration}, and gets
     * its values from the given {@linkplain Configuration}.
     *
     * @param configuration the configuration to initialize
     * @param other the configuration to copy overrides from
     * @param values the configuration to copy values from
     * @param sync if true, sync the folder configuration from
     */
    protected static void initFrom(NestedConfiguration configuration,
            NestedConfiguration other, Configuration values, boolean sync) {
        configuration.mOverride = other.mOverride;
        configuration.setDisplayName(values.getDisplayName());
        configuration.setActivity(values.getActivity());

        if (configuration.isOverridingLocale()) {
            configuration.setLocale(values.getLocale(), true);
        }
        if (configuration.isOverridingTarget()) {
            configuration.setTarget(values.getTarget(), true);
        }
        if (configuration.isOverridingDevice()) {
            configuration.setDevice(values.getDevice(), true);
        }
        if (configuration.isOverridingDeviceState()) {
            configuration.setDeviceState(values.getDeviceState(), true);
        }
        if (configuration.isOverridingNightMode()) {
            configuration.setNightMode(values.getNightMode(), true);
        }
        if (configuration.isOverridingUiMode()) {
            configuration.setUiMode(values.getUiMode(), true);
        }
        if (sync) {
            configuration.syncFolderConfig();
        }
    }

    /**
     * Sets the parent configuration that this configuration is inheriting from.
     *
     * @param parent the parent configuration
     */
    public void setParent(@NonNull Configuration parent) {
        mParent = parent;
    }

    /**
     * Creates a new {@linkplain Configuration} which inherits values from the
     * given parent {@linkplain Configuration}, possibly overriding some as
     * well.
     *
     * @param chooser the associated chooser
     * @param parent the configuration to inherit values from
     * @return a new configuration
     */
    @NonNull
    public static NestedConfiguration create(@NonNull ConfigurationChooser chooser,
            @NonNull Configuration parent) {
        return new NestedConfiguration(chooser, parent);
    }

    @Override
    @Nullable
    public String getTheme() {
        // Never overridden: this is a static attribute of a layout, not something which
        // varies by configuration or at runtime
        return mParent.getTheme();
    }

    @Override
    public void setTheme(String theme) {
        // Never overridden
        mParent.setTheme(theme);
    }

    /**
     * Sets whether the locale should be overridden by this configuration
     *
     * @param override if true, override the inherited value
     */
    public void setOverrideLocale(boolean override) {
        mOverride |= CFG_LOCALE;
    }

    /**
     * Returns true if the locale is overridden
     *
     * @return true if the locale is overridden
     */
    public final boolean isOverridingLocale() {
        return (mOverride & CFG_LOCALE) != 0;
    }

    @Override
    @NonNull
    public Locale getLocale() {
        if (isOverridingLocale()) {
            return super.getLocale();
        } else {
            return mParent.getLocale();
        }
    }

    @Override
    public void setLocale(@NonNull Locale locale, boolean skipSync) {
        if (isOverridingLocale()) {
            super.setLocale(locale, skipSync);
        } else {
            mParent.setLocale(locale, skipSync);
        }
    }

    /**
     * Sets whether the rendering target should be overridden by this configuration
     *
     * @param override if true, override the inherited value
     */
    public void setOverrideTarget(boolean override) {
        mOverride |= CFG_TARGET;
    }

    /**
     * Returns true if the target is overridden
     *
     * @return true if the target is overridden
     */
    public final boolean isOverridingTarget() {
        return (mOverride & CFG_TARGET) != 0;
    }

    @Override
    @Nullable
    public IAndroidTarget getTarget() {
        if (isOverridingTarget()) {
            return super.getTarget();
        } else {
            return mParent.getTarget();
        }
    }

    @Override
    public void setTarget(IAndroidTarget target, boolean skipSync) {
        if (isOverridingTarget()) {
            super.setTarget(target, skipSync);
        } else {
            mParent.setTarget(target, skipSync);
        }
    }

    /**
     * Sets whether the device should be overridden by this configuration
     *
     * @param override if true, override the inherited value
     */
    public void setOverrideDevice(boolean override) {
        mOverride |= CFG_DEVICE;
    }

    /**
     * Returns true if the device is overridden
     *
     * @return true if the device is overridden
     */
    public final boolean isOverridingDevice() {
        return (mOverride & CFG_DEVICE) != 0;
    }

    @Override
    @Nullable
    public Device getDevice() {
        if (isOverridingDevice()) {
            return super.getDevice();
        } else {
            return mParent.getDevice();
        }
    }

    @Override
    public void setDevice(Device device, boolean skipSync) {
        if (isOverridingDevice()) {
            super.setDevice(device, skipSync);
        } else {
            mParent.setDevice(device, skipSync);
        }
    }

    /**
     * Sets whether the device state should be overridden by this configuration
     *
     * @param override if true, override the inherited value
     */
    public void setOverrideDeviceState(boolean override) {
        mOverride |= CFG_DEVICE_STATE;
    }

    /**
     * Returns true if the device state is overridden
     *
     * @return true if the device state is overridden
     */
    public final boolean isOverridingDeviceState() {
        return (mOverride & CFG_DEVICE_STATE) != 0;
    }

    @Override
    @Nullable
    public State getDeviceState() {
        if (isOverridingDeviceState()) {
            return super.getDeviceState();
        } else {
            State state = mParent.getDeviceState();
            if (isOverridingDevice()) {
                // If the device differs, I need to look up a suitable equivalent state
                // on our device
                if (state != null) {
                    Device device = super.getDevice();
                    if (device != null) {
                        return device.getState(state.getName());
                    }
                }
            }

            return state;
        }
    }

    @Override
    public void setDeviceState(State state, boolean skipSync) {
        if (isOverridingDeviceState()) {
            super.setDeviceState(state, skipSync);
        } else {
            if (isOverridingDevice()) {
                Device device = super.getDevice();
                if (device != null) {
                    State equivalentState = device.getState(state.getName());
                    if (equivalentState != null) {
                        state = equivalentState;
                    }
                }
            }
            mParent.setDeviceState(state, skipSync);
        }
    }

    /**
     * Sets whether the night mode should be overridden by this configuration
     *
     * @param override if true, override the inherited value
     */
    public void setOverrideNightMode(boolean override) {
        mOverride |= CFG_NIGHT_MODE;
    }

    /**
     * Returns true if the night mode is overridden
     *
     * @return true if the night mode is overridden
     */
    public final boolean isOverridingNightMode() {
        return (mOverride & CFG_NIGHT_MODE) != 0;
    }

    @Override
    @NonNull
    public NightMode getNightMode() {
        if (isOverridingNightMode()) {
            return super.getNightMode();
        } else {
            return mParent.getNightMode();
        }
    }

    @Override
    public void setNightMode(@NonNull NightMode night, boolean skipSync) {
        if (isOverridingNightMode()) {
            super.setNightMode(night, skipSync);
        } else {
            mParent.setNightMode(night, skipSync);
        }
    }

    /**
     * Sets whether the UI mode should be overridden by this configuration
     *
     * @param override if true, override the inherited value
     */
    public void setOverrideUiMode(boolean override) {
        mOverride |= CFG_UI_MODE;
    }

    /**
     * Returns true if the UI mode is overridden
     *
     * @return true if the UI mode is overridden
     */
    public final boolean isOverridingUiMode() {
        return (mOverride & CFG_UI_MODE) != 0;
    }

    @Override
    @NonNull
    public UiMode getUiMode() {
        if (isOverridingUiMode()) {
            return super.getUiMode();
        } else {
            return mParent.getUiMode();
        }
    }

    @Override
    public void setUiMode(@NonNull UiMode uiMode, boolean skipSync) {
        if (isOverridingUiMode()) {
            super.setUiMode(uiMode, skipSync);
        } else {
            mParent.setUiMode(uiMode, skipSync);
        }
    }

    /**
     * Returns the configuration this {@linkplain NestedConfiguration} is
     * inheriting from
     *
     * @return the configuration this configuration is inheriting from
     */
    @NonNull
    public Configuration getParent() {
        return mParent;
    }

    @Override
    @Nullable
    public String getActivity() {
        return mParent.getActivity();
    }

    @Override
    public void setActivity(String activity) {
        super.setActivity(activity);
    }

    /**
     * Returns a computed display name (ignoring the value stored by
     * {@link #setDisplayName(String)}) by looking at the override flags
     * and picking a suitable name.
     *
     * @return a suitable display name
     */
    @Nullable
    public String computeDisplayName() {
        return computeDisplayName(mOverride, this);
    }

    /**
     * Computes a display name for the given configuration, using the given
     * override flags (which correspond to the {@code CFG_} constants in
     * {@link ConfigurationClient}
     *
     * @param flags the override bitmask
     * @param configuration the configuration to fetch values from
     * @return a suitable display name
     */
    @Nullable
    public static String computeDisplayName(int flags, @NonNull Configuration configuration) {
        if ((flags & CFG_LOCALE) != 0) {
            return ConfigurationChooser.getLocaleLabel(configuration.mConfigChooser,
                    configuration.getLocale(), false);
        }

        if ((flags & CFG_TARGET) != 0) {
            return ConfigurationChooser.getRenderingTargetLabel(configuration.getTarget(), false);
        }

        if ((flags & CFG_DEVICE) != 0) {
            return ConfigurationChooser.getDeviceLabel(configuration.getDevice(), true);
        }

        if ((flags & CFG_DEVICE_STATE) != 0) {
            State deviceState = configuration.getDeviceState();
            if (deviceState != null) {
                return deviceState.getName();
            }
        }

        if ((flags & CFG_NIGHT_MODE) != 0) {
            return configuration.getNightMode().getLongDisplayValue();
        }

        if ((flags & CFG_UI_MODE) != 0) {
            configuration.getUiMode().getLongDisplayValue();
        }

        return null;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this.getClass())
                .add("parent", mParent.getDisplayName())          //$NON-NLS-1$
                .add("display", getDisplayName())                 //$NON-NLS-1$
                .add("overrideLocale", isOverridingLocale())           //$NON-NLS-1$
                .add("overrideTarget", isOverridingTarget())           //$NON-NLS-1$
                .add("overrideDevice", isOverridingDevice())           //$NON-NLS-1$
                .add("overrideDeviceState", isOverridingDeviceState()) //$NON-NLS-1$
                .add("persistent", toPersistentString())          //$NON-NLS-1$
                .toString();
    }
}