| 1 |
/* |
| 2 |
* Copyright (C) 2010 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.android.ide.common.resources.configuration; |
| 18 |
|
| 19 |
import com.android.annotations.NonNull; |
| 20 |
import com.android.annotations.Nullable; |
| 21 |
import com.android.resources.ResourceEnum; |
| 22 |
import com.android.resources.UiMode; |
| 23 |
|
| 24 |
/** |
| 25 |
* Resource Qualifier for UI Mode. |
| 26 |
*/ |
| 27 |
public final class UiModeQualifier extends EnumBasedResourceQualifier { |
| 28 |
|
| 29 |
public static final String NAME = "UI Mode"; |
| 30 |
|
| 31 |
private UiMode mValue; |
| 32 |
|
| 33 |
public UiModeQualifier() { |
| 34 |
// pass |
| 35 |
} |
| 36 |
|
| 37 |
public UiModeQualifier(UiMode value) { |
| 38 |
mValue = value; |
| 39 |
} |
| 40 |
|
| 41 |
public UiMode getValue() { |
| 42 |
return mValue; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
ResourceEnum getEnumValue() { |
| 47 |
return mValue; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public String getName() { |
| 52 |
return NAME; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public String getShortName() { |
| 57 |
return NAME; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public int since() { |
| 62 |
if (mValue != null) { |
| 63 |
return mValue.since(); |
| 64 |
} |
| 65 |
return 8; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean checkAndSet(String value, FolderConfiguration config) { |
| 70 |
UiMode mode = UiMode.getEnum(value); |
| 71 |
if (mode != null) { |
| 72 |
UiModeQualifier qualifier = new UiModeQualifier(mode); |
| 73 |
config.setUiModeQualifier(qualifier); |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public boolean isMatchFor(ResourceQualifier qualifier) { |
| 82 |
// only normal is a match for all UI mode, because it's not an actual mode. |
| 83 |
if (mValue == UiMode.NORMAL) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
// others must be an exact match |
| 88 |
return ((UiModeQualifier)qualifier).mValue == mValue; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public boolean isBetterMatchThan(@Nullable ResourceQualifier compareTo, |
| 93 |
@NonNull ResourceQualifier reference) { |
| 94 |
if (compareTo == null) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
UiModeQualifier compareQualifier = (UiModeQualifier) compareTo; |
| 99 |
UiModeQualifier referenceQualifier = (UiModeQualifier) reference; |
| 100 |
|
| 101 |
if (compareQualifier.getValue() == referenceQualifier.getValue()) { |
| 102 |
// what we have is already the best possible match (exact match) |
| 103 |
return false; |
| 104 |
} else if (mValue == referenceQualifier.mValue) { |
| 105 |
// got new exact value, this is the best! |
| 106 |
return true; |
| 107 |
} else if (mValue == UiMode.NORMAL) { |
| 108 |
// else "normal" can be a match in case there's no exact match |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
return false; |
| 113 |
} |
| 114 |
} |