| 1 |
/* |
| 2 |
* Copyright (C) 2007 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 java.util.regex.Matcher; |
| 20 |
import java.util.regex.Pattern; |
| 21 |
|
| 22 |
/** |
| 23 |
* Resource Qualifier for Screen Dimension. |
| 24 |
*/ |
| 25 |
public final class ScreenDimensionQualifier extends ResourceQualifier { |
| 26 |
/** Default screen size value. This means the property is not set */ |
| 27 |
static final int DEFAULT_SIZE = -1; |
| 28 |
|
| 29 |
private static final Pattern sDimensionPattern = Pattern.compile( |
| 30 |
"^(\\d+)x(\\d+)$"); //$NON-NLS-1$ |
| 31 |
|
| 32 |
public static final String NAME = "Screen Dimension"; |
| 33 |
|
| 34 |
/** Screen size 1 value. This is not size X or Y because the folder name always |
| 35 |
* contains the biggest size first. So if the qualifier is 400x200, size 1 will always be |
| 36 |
* 400 but that will be X in landscape and Y in portrait. |
| 37 |
* Default value is <code>DEFAULT_SIZE</code> */ |
| 38 |
private int mValue1 = DEFAULT_SIZE; |
| 39 |
|
| 40 |
/** Screen size 2 value. This is not size X or Y because the folder name always |
| 41 |
* contains the biggest size first. So if the qualifier is 400x200, size 2 will always be |
| 42 |
* 200 but that will be Y in landscape and X in portrait. |
| 43 |
* Default value is <code>DEFAULT_SIZE</code> */ |
| 44 |
private int mValue2 = DEFAULT_SIZE; |
| 45 |
|
| 46 |
public ScreenDimensionQualifier() { |
| 47 |
// pass |
| 48 |
} |
| 49 |
|
| 50 |
public ScreenDimensionQualifier(int value1, int value2) { |
| 51 |
mValue1 = value1; |
| 52 |
mValue2 = value2; |
| 53 |
} |
| 54 |
|
| 55 |
public int getValue1() { |
| 56 |
return mValue1; |
| 57 |
} |
| 58 |
|
| 59 |
public int getValue2() { |
| 60 |
return mValue2; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public String getName() { |
| 65 |
return NAME; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public String getShortName() { |
| 70 |
return "Dimension"; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public int since() { |
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public boolean deprecated() { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public boolean isValid() { |
| 85 |
return mValue1 != DEFAULT_SIZE && mValue2 != DEFAULT_SIZE; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public boolean hasFakeValue() { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public boolean checkAndSet(String value, FolderConfiguration config) { |
| 95 |
Matcher m = sDimensionPattern.matcher(value); |
| 96 |
if (m.matches()) { |
| 97 |
String d1 = m.group(1); |
| 98 |
String d2 = m.group(2); |
| 99 |
|
| 100 |
ScreenDimensionQualifier qualifier = getQualifier(d1, d2); |
| 101 |
if (qualifier != null) { |
| 102 |
config.setScreenDimensionQualifier(qualifier); |
| 103 |
return true; |
| 104 |
} |
| 105 |
} |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public boolean equals(Object qualifier) { |
| 111 |
if (qualifier instanceof ScreenDimensionQualifier) { |
| 112 |
ScreenDimensionQualifier q = (ScreenDimensionQualifier)qualifier; |
| 113 |
return (mValue1 == q.mValue1 && mValue2 == q.mValue2); |
| 114 |
} |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public int hashCode() { |
| 121 |
return toString().hashCode(); |
| 122 |
} |
| 123 |
|
| 124 |
public static ScreenDimensionQualifier getQualifier(String size1, String size2) { |
| 125 |
try { |
| 126 |
int s1 = Integer.parseInt(size1); |
| 127 |
int s2 = Integer.parseInt(size2); |
| 128 |
|
| 129 |
ScreenDimensionQualifier qualifier = new ScreenDimensionQualifier(); |
| 130 |
|
| 131 |
if (s1 > s2) { |
| 132 |
qualifier.mValue1 = s1; |
| 133 |
qualifier.mValue2 = s2; |
| 134 |
} else { |
| 135 |
qualifier.mValue1 = s2; |
| 136 |
qualifier.mValue2 = s1; |
| 137 |
} |
| 138 |
|
| 139 |
return qualifier; |
| 140 |
} catch (NumberFormatException e) { |
| 141 |
// looks like the string we extracted wasn't a valid number. |
| 142 |
} |
| 143 |
|
| 144 |
return null; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the string used to represent this qualifier in the folder name. |
| 149 |
*/ |
| 150 |
@Override |
| 151 |
public String getFolderSegment() { |
| 152 |
return String.format("%1$dx%2$d", mValue1, mValue2); //$NON-NLS-1$ |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public String getShortDisplayValue() { |
| 157 |
if (isValid()) { |
| 158 |
return String.format("%1$dx%2$d", mValue1, mValue2); |
| 159 |
} |
| 160 |
|
| 161 |
return ""; //$NON-NLS-1$ |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public String getLongDisplayValue() { |
| 166 |
if (isValid()) { |
| 167 |
return String.format("Screen resolution %1$dx%2$d", mValue1, mValue2); |
| 168 |
} |
| 169 |
|
| 170 |
return ""; //$NON-NLS-1$ |
| 171 |
} |
| 172 |
} |