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