| 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 com.android.SdkConstants; |
| 20 |
import com.android.annotations.NonNull; |
| 21 |
import com.android.annotations.Nullable; |
| 22 |
import com.android.resources.Density; |
| 23 |
import com.android.resources.ResourceFolderType; |
| 24 |
import com.android.resources.ScreenOrientation; |
| 25 |
import com.google.common.base.Objects; |
| 26 |
import com.google.common.base.Splitter; |
| 27 |
import com.google.common.collect.Iterators; |
| 28 |
|
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.Collections; |
| 31 |
import java.util.Iterator; |
| 32 |
import java.util.List; |
| 33 |
import java.util.Locale; |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Represents the configuration for Resource Folders. All the properties have a default |
| 38 |
* value which means that the property is not set. |
| 39 |
*/ |
| 40 |
public final class FolderConfiguration implements Comparable<FolderConfiguration> { |
| 41 |
|
| 42 |
@NonNull |
| 43 |
private static final ResourceQualifier[] DEFAULT_QUALIFIERS; |
| 44 |
|
| 45 |
/** Splitter which can be used to split qualifiers */ |
| 46 |
public static final Splitter QUALIFIER_SPLITTER = Splitter.on('-'); |
| 47 |
|
| 48 |
|
| 49 |
private final ResourceQualifier[] mQualifiers; |
| 50 |
|
| 51 |
private static final int INDEX_COUNTRY_CODE = 0; |
| 52 |
private static final int INDEX_NETWORK_CODE = 1; |
| 53 |
private static final int INDEX_LOCALE = 2; |
| 54 |
private static final int INDEX_LAYOUT_DIR = 3; |
| 55 |
private static final int INDEX_SMALLEST_SCREEN_WIDTH = 4; |
| 56 |
private static final int INDEX_SCREEN_WIDTH = 5; |
| 57 |
private static final int INDEX_SCREEN_HEIGHT = 6; |
| 58 |
private static final int INDEX_SCREEN_LAYOUT_SIZE = 7; |
| 59 |
private static final int INDEX_SCREEN_RATIO = 8; |
| 60 |
private static final int INDEX_SCREEN_ROUND = 9; |
| 61 |
private static final int INDEX_SCREEN_ORIENTATION = 10; |
| 62 |
private static final int INDEX_UI_MODE = 11; |
| 63 |
private static final int INDEX_NIGHT_MODE = 12; |
| 64 |
private static final int INDEX_PIXEL_DENSITY = 13; |
| 65 |
private static final int INDEX_TOUCH_TYPE = 14; |
| 66 |
private static final int INDEX_KEYBOARD_STATE = 15; |
| 67 |
private static final int INDEX_TEXT_INPUT_METHOD = 16; |
| 68 |
private static final int INDEX_NAVIGATION_STATE = 17; |
| 69 |
private static final int INDEX_NAVIGATION_METHOD = 18; |
| 70 |
private static final int INDEX_SCREEN_DIMENSION = 19; |
| 71 |
private static final int INDEX_VERSION = 20; |
| 72 |
private static final int INDEX_COUNT = 21; |
| 73 |
|
| 74 |
private static final ResourceQualifier[] NULL_QUALIFIERS = new ResourceQualifier[INDEX_COUNT]; |
| 75 |
|
| 76 |
|
| 77 |
static { |
| 78 |
// get the default qualifiers. |
| 79 |
FolderConfiguration defaultConfig = new FolderConfiguration(); |
| 80 |
defaultConfig.createDefault(); |
| 81 |
DEFAULT_QUALIFIERS = defaultConfig.mQualifiers; |
| 82 |
for (int i = 0; i < DEFAULT_QUALIFIERS.length; i++) { |
| 83 |
NULL_QUALIFIERS[i] = DEFAULT_QUALIFIERS[i].getNullQualifier(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public FolderConfiguration() { |
| 88 |
this(NULL_QUALIFIERS); |
| 89 |
} |
| 90 |
|
| 91 |
private FolderConfiguration(@NonNull ResourceQualifier[] qualifiers) { |
| 92 |
mQualifiers = new ResourceQualifier[INDEX_COUNT]; |
| 93 |
System.arraycopy(qualifiers, 0, mQualifiers, 0, INDEX_COUNT); |
| 94 |
} |
| 95 |
/** |
| 96 |
* Creates a {@link FolderConfiguration} matching the folder segments. |
| 97 |
* @param folderSegments The segments of the folder name. The first segments should contain |
| 98 |
* the name of the folder |
| 99 |
* @return a FolderConfiguration object, or null if the folder name isn't valid.. |
| 100 |
*/ |
| 101 |
@Nullable |
| 102 |
public static FolderConfiguration getConfig(@NonNull String[] folderSegments) { |
| 103 |
Iterator<String> iterator = Iterators.forArray(folderSegments); |
| 104 |
if (iterator.hasNext()) { |
| 105 |
// Skip the first segment: it should be just the base folder, such as "values" or |
| 106 |
// "layout" |
| 107 |
iterator.next(); |
| 108 |
} |
| 109 |
|
| 110 |
return getConfigFromQualifiers(iterator); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Creates a {@link FolderConfiguration} matching the folder segments. |
| 115 |
* @param folderSegments The segments of the folder name. The first segments should contain |
| 116 |
* the name of the folder |
| 117 |
* @return a FolderConfiguration object, or null if the folder name isn't valid.. |
| 118 |
* @see FolderConfiguration#getConfig(String[]) |
| 119 |
*/ |
| 120 |
@Nullable |
| 121 |
public static FolderConfiguration getConfig(@NonNull Iterable<String> folderSegments) { |
| 122 |
Iterator<String> iterator = folderSegments.iterator(); |
| 123 |
if (iterator.hasNext()) { |
| 124 |
// Skip the first segment: it should be just the base folder, such as "values" or |
| 125 |
// "layout" |
| 126 |
iterator.next(); |
| 127 |
} |
| 128 |
|
| 129 |
return getConfigFromQualifiers(iterator); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Creates a {@link FolderConfiguration} matching the qualifiers. |
| 134 |
* |
| 135 |
* @param qualifiers the qualifiers. |
| 136 |
* |
| 137 |
* @return a FolderConfiguration object, or null if the folder name isn't valid.. |
| 138 |
*/ |
| 139 |
@Nullable |
| 140 |
public static FolderConfiguration getConfigFromQualifiers( |
| 141 |
@NonNull Iterable<String> qualifiers) { |
| 142 |
return getConfigFromQualifiers(qualifiers.iterator()); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Creates a {@link FolderConfiguration} matching the qualifiers. |
| 147 |
* |
| 148 |
* @param qualifiers An iterator on the qualifiers. |
| 149 |
* |
| 150 |
* @return a FolderConfiguration object, or null if the folder name isn't valid.. |
| 151 |
*/ |
| 152 |
@Nullable |
| 153 |
public static FolderConfiguration getConfigFromQualifiers( |
| 154 |
@NonNull Iterator<String> qualifiers) { |
| 155 |
FolderConfiguration config = new FolderConfiguration(); |
| 156 |
|
| 157 |
// we are going to loop through the segments, and match them with the first |
| 158 |
// available qualifier. If the segment doesn't match we try with the next qualifier. |
| 159 |
// Because the order of the qualifier is fixed, we do not reset the first qualifier |
| 160 |
// after each successful segment. |
| 161 |
// If we run out of qualifier before processing all the segments, we fail. |
| 162 |
|
| 163 |
int qualifierIndex = 0; |
| 164 |
int qualifierCount = DEFAULT_QUALIFIERS.length; |
| 165 |
|
| 166 |
/* |
| 167 |
Process a series of qualifiers and parse them into a set of ResourceQualifiers |
| 168 |
in the new folder configuration. |
| 169 |
|
| 170 |
The basic loop is as follows: |
| 171 |
|
| 172 |
while (qualifiers.hasNext()) { |
| 173 |
String seg = qualifiers.next(); |
| 174 |
|
| 175 |
while (qualifierIndex < qualifierCount && |
| 176 |
!DEFAULT_QUALIFIERS[qualifierIndex].checkAndSet(seg, config)) { |
| 177 |
qualifierIndex++; |
| 178 |
} |
| 179 |
|
| 180 |
// if we reached the end of the qualifier we didn't find a matching qualifier. |
| 181 |
if (qualifierIndex == qualifierCount) { |
| 182 |
return null; |
| 183 |
} else { |
| 184 |
qualifierIndex++; // already processed this one |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
In other words, we process through the iterable, one segment at a time, and |
| 189 |
for that segment, we iterate up through the qualifiers and ask each one if |
| 190 |
they can handle it (via checkAndSet); if they can, we are done with that segment |
| 191 |
*and* that qualifier, so next time through the segment loop we won't keep retrying |
| 192 |
the same qualifiers. So, we are basically iterating through two lists (segments |
| 193 |
and qualifiers) at the same time). |
| 194 |
|
| 195 |
However, locales are a special exception to this: we want to combine *two* segments |
| 196 |
into a single qualifier when you specify both a language and a region. |
| 197 |
E.g. for "en-rUS-ldltr" we want a single LocaleQualifier holding both "en" and "rUS" |
| 198 |
and then a LayoutDirectionQualifier for ldltr. |
| 199 |
|
| 200 |
Therefore, we've unrolled the above loop: we process all identifiers up to |
| 201 |
the locale qualifier index. |
| 202 |
|
| 203 |
Then, at the locale qualifier index, IF we get a match, we don't increment |
| 204 |
the qualifierIndex: instead, we fetch the next segment, and if it matches |
| 205 |
as a region, we augment the locale qualifier and continue -- otherwise, we |
| 206 |
bail and process the next segment as usual. |
| 207 |
|
| 208 |
And then we finally iterate through the remaining qualifiers and segments; this |
| 209 |
is basically the first loop again, iterating from the post-locale qualifier |
| 210 |
up to the end. |
| 211 |
*/ |
| 212 |
|
| 213 |
if (!qualifiers.hasNext()) { |
| 214 |
return config; |
| 215 |
} |
| 216 |
|
| 217 |
while (qualifiers.hasNext()) { |
| 218 |
String seg = qualifiers.next(); |
| 219 |
if (seg.isEmpty()) { |
| 220 |
return null; // Not a valid folder configuration |
| 221 |
} |
| 222 |
|
| 223 |
// TODO: Perform case normalization later (on a per qualifier basis) |
| 224 |
seg = seg.toLowerCase(Locale.US); // no-op if string is already in lower case |
| 225 |
|
| 226 |
while (qualifierIndex < INDEX_LOCALE && |
| 227 |
!DEFAULT_QUALIFIERS[qualifierIndex].checkAndSet(seg, config)) { |
| 228 |
qualifierIndex++; |
| 229 |
} |
| 230 |
|
| 231 |
// if we reached the end of the qualifier we didn't find a matching qualifier. |
| 232 |
if (qualifierIndex == INDEX_LOCALE) { |
| 233 |
// Ready for locale matching now; that requires some special |
| 234 |
// casing described below |
| 235 |
|
| 236 |
boolean handle = true; |
| 237 |
// Don't need to lowercase; qualifier will normalize case on its own |
| 238 |
if (DEFAULT_QUALIFIERS[qualifierIndex].checkAndSet(seg, config)) { |
| 239 |
qualifierIndex++; |
| 240 |
if (qualifiers.hasNext()) { |
| 241 |
seg = qualifiers.next(); |
| 242 |
if (seg.isEmpty()) { |
| 243 |
return null; // Not a valid folder configuration |
| 244 |
} |
| 245 |
// Is the next qualifier a region? If so, amend the existing |
| 246 |
// LocaleQualifier |
| 247 |
if (LocaleQualifier.isRegionSegment(seg)) { |
| 248 |
LocaleQualifier localeQualifier = config.getLocaleQualifier(); |
| 249 |
assert localeQualifier != null; // because checkAndSet returned true above |
| 250 |
localeQualifier.setRegionSegment(seg); |
| 251 |
handle = false; |
| 252 |
} else { |
| 253 |
// No, not a region, so perform normal processing |
| 254 |
seg = seg.toLowerCase(Locale.US); // no-op if string is already in lower case |
| 255 |
} |
| 256 |
} else { |
| 257 |
return config; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if (handle) { |
| 262 |
while (qualifierIndex < qualifierCount && |
| 263 |
!DEFAULT_QUALIFIERS[qualifierIndex].checkAndSet(seg, config)) { |
| 264 |
qualifierIndex++; |
| 265 |
} |
| 266 |
|
| 267 |
// if we reached the end of the qualifier we didn't find a matching qualifier. |
| 268 |
if (qualifierIndex == qualifierCount) { |
| 269 |
// Ready for locale matching now; that requires some special |
| 270 |
// casing described below |
| 271 |
return null; |
| 272 |
} else { |
| 273 |
qualifierIndex++; // already processed this one |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
break; |
| 278 |
} else { |
| 279 |
qualifierIndex++; // already processed this one |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// Same loop as above, but we continue from after the locales |
| 284 |
while (qualifiers.hasNext()) { |
| 285 |
String seg = qualifiers.next(); |
| 286 |
if (seg.isEmpty()) { |
| 287 |
return null; // Not a valid folder configuration |
| 288 |
} |
| 289 |
|
| 290 |
seg = seg.toLowerCase(Locale.US); // no-op if string is already in lower case |
| 291 |
|
| 292 |
while (qualifierIndex < qualifierCount && |
| 293 |
!DEFAULT_QUALIFIERS[qualifierIndex].checkAndSet(seg, config)) { |
| 294 |
qualifierIndex++; |
| 295 |
} |
| 296 |
|
| 297 |
// if we reached the end of the qualifier we didn't find a matching qualifier. |
| 298 |
if (qualifierIndex == qualifierCount) { |
| 299 |
return null; |
| 300 |
} else { |
| 301 |
qualifierIndex++; // already processed this one |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
return config; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Creates a {@link FolderConfiguration} matching the given folder name. |
| 310 |
* |
| 311 |
* @param folderName the folder name |
| 312 |
* @return a FolderConfiguration object, or null if the folder name isn't valid.. |
| 313 |
*/ |
| 314 |
@Nullable |
| 315 |
public static FolderConfiguration getConfigForFolder(@NonNull String folderName) { |
| 316 |
return getConfig(QUALIFIER_SPLITTER.split(folderName)); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Creates a copy of the given {@link FolderConfiguration}, that can be modified without |
| 321 |
* affecting the original. |
| 322 |
*/ |
| 323 |
@NonNull |
| 324 |
public static FolderConfiguration copyOf(@NonNull FolderConfiguration original) { |
| 325 |
return new FolderConfiguration(original.mQualifiers); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Creates a {@link FolderConfiguration} matching the given qualifier string |
| 330 |
* (just the qualifiers; e.g. for a folder like "values-en-rUS" this would be "en-rUS"). |
| 331 |
* |
| 332 |
* @param qualifierString the qualifier string |
| 333 |
* @return a FolderConfiguration object, or null if the qualifier string isn't valid.. |
| 334 |
*/ |
| 335 |
@Nullable |
| 336 |
public static FolderConfiguration getConfigForQualifierString(@NonNull String qualifierString) { |
| 337 |
if (qualifierString.isEmpty()) { |
| 338 |
return new FolderConfiguration(); |
| 339 |
} else { |
| 340 |
return getConfigFromQualifiers(QUALIFIER_SPLITTER.split(qualifierString)); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Returns the number of {@link ResourceQualifier} that make up a Folder configuration. |
| 346 |
*/ |
| 347 |
public static int getQualifierCount() { |
| 348 |
return INDEX_COUNT; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Sets the config from the qualifiers of a given <var>config</var>. |
| 353 |
* <p>This is equivalent to <code>set(config, false)</code> |
| 354 |
* @param config the configuration to set |
| 355 |
* |
| 356 |
* @see #set(FolderConfiguration, boolean) |
| 357 |
*/ |
| 358 |
public void set(@Nullable FolderConfiguration config) { |
| 359 |
set(config, false /*nonFakeValuesOnly*/); |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Sets the config from the qualifiers of a given <var>config</var>. |
| 364 |
* @param config the configuration to set |
| 365 |
* @param nonFakeValuesOnly if set to true this ignore qualifiers for which the |
| 366 |
* current value is a fake value. |
| 367 |
* |
| 368 |
* @see ResourceQualifier#hasFakeValue() |
| 369 |
*/ |
| 370 |
public void set(@Nullable FolderConfiguration config, boolean nonFakeValuesOnly) { |
| 371 |
if (config != null) { |
| 372 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 373 |
ResourceQualifier q = config.mQualifiers[i]; |
| 374 |
if (!nonFakeValuesOnly || q == null || !q.hasFakeValue()) { |
| 375 |
mQualifiers[i] = q; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Reset the config. |
| 383 |
* <p>This makes qualifiers at all indices <code>null</code>. |
| 384 |
*/ |
| 385 |
public void reset() { |
| 386 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 387 |
mQualifiers[i] = NULL_QUALIFIERS[i]; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Removes the qualifiers from the receiver if they are present (and valid) |
| 393 |
* in the given configuration. |
| 394 |
*/ |
| 395 |
public void substract(@NonNull FolderConfiguration config) { |
| 396 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 397 |
if (ResourceQualifier.isValid(config.mQualifiers[i])) { |
| 398 |
mQualifiers[i] = NULL_QUALIFIERS[i]; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Adds the non-qualifiers from the given config. |
| 405 |
* Qualifiers that are null in the given config do not change in the receiver. |
| 406 |
*/ |
| 407 |
public void add(@NonNull FolderConfiguration config) { |
| 408 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 409 |
if (config.mQualifiers[i] != NULL_QUALIFIERS[i]) { |
| 410 |
mQualifiers[i] = config.mQualifiers[i]; |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Returns the first invalid qualifier, or <code>null</code> if they are all valid (or if none |
| 417 |
* exists). |
| 418 |
*/ |
| 419 |
@Nullable |
| 420 |
public ResourceQualifier getInvalidQualifier() { |
| 421 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 422 |
if (mQualifiers[i] != null && !mQualifiers[i].isValid()) { |
| 423 |
return mQualifiers[i]; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
// all allocated qualifiers are valid, we return null. |
| 428 |
return null; |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Adds a qualifier to the {@link FolderConfiguration} |
| 433 |
* @param qualifier the {@link ResourceQualifier} to add. |
| 434 |
*/ |
| 435 |
public void addQualifier(@Nullable ResourceQualifier qualifier) { |
| 436 |
if (qualifier instanceof CountryCodeQualifier) { |
| 437 |
mQualifiers[INDEX_COUNTRY_CODE] = qualifier; |
| 438 |
|
| 439 |
} else if (qualifier instanceof NetworkCodeQualifier) { |
| 440 |
mQualifiers[INDEX_NETWORK_CODE] = qualifier; |
| 441 |
|
| 442 |
} else if (qualifier instanceof LocaleQualifier) { |
| 443 |
mQualifiers[INDEX_LOCALE] = qualifier; |
| 444 |
|
| 445 |
} else if (qualifier instanceof LayoutDirectionQualifier) { |
| 446 |
mQualifiers[INDEX_LAYOUT_DIR] = qualifier; |
| 447 |
|
| 448 |
} else if (qualifier instanceof SmallestScreenWidthQualifier) { |
| 449 |
mQualifiers[INDEX_SMALLEST_SCREEN_WIDTH] = qualifier; |
| 450 |
|
| 451 |
} else if (qualifier instanceof ScreenWidthQualifier) { |
| 452 |
mQualifiers[INDEX_SCREEN_WIDTH] = qualifier; |
| 453 |
|
| 454 |
} else if (qualifier instanceof ScreenHeightQualifier) { |
| 455 |
mQualifiers[INDEX_SCREEN_HEIGHT] = qualifier; |
| 456 |
|
| 457 |
} else if (qualifier instanceof ScreenSizeQualifier) { |
| 458 |
mQualifiers[INDEX_SCREEN_LAYOUT_SIZE] = qualifier; |
| 459 |
|
| 460 |
} else if (qualifier instanceof ScreenRatioQualifier) { |
| 461 |
mQualifiers[INDEX_SCREEN_RATIO] = qualifier; |
| 462 |
|
| 463 |
} else if (qualifier instanceof ScreenRoundQualifier) { |
| 464 |
mQualifiers[INDEX_SCREEN_ROUND] = qualifier; |
| 465 |
|
| 466 |
} else if (qualifier instanceof ScreenOrientationQualifier) { |
| 467 |
mQualifiers[INDEX_SCREEN_ORIENTATION] = qualifier; |
| 468 |
|
| 469 |
} else if (qualifier instanceof UiModeQualifier) { |
| 470 |
mQualifiers[INDEX_UI_MODE] = qualifier; |
| 471 |
|
| 472 |
} else if (qualifier instanceof NightModeQualifier) { |
| 473 |
mQualifiers[INDEX_NIGHT_MODE] = qualifier; |
| 474 |
|
| 475 |
} else if (qualifier instanceof DensityQualifier) { |
| 476 |
mQualifiers[INDEX_PIXEL_DENSITY] = qualifier; |
| 477 |
|
| 478 |
} else if (qualifier instanceof TouchScreenQualifier) { |
| 479 |
mQualifiers[INDEX_TOUCH_TYPE] = qualifier; |
| 480 |
|
| 481 |
} else if (qualifier instanceof KeyboardStateQualifier) { |
| 482 |
mQualifiers[INDEX_KEYBOARD_STATE] = qualifier; |
| 483 |
|
| 484 |
} else if (qualifier instanceof TextInputMethodQualifier) { |
| 485 |
mQualifiers[INDEX_TEXT_INPUT_METHOD] = qualifier; |
| 486 |
|
| 487 |
} else if (qualifier instanceof NavigationStateQualifier) { |
| 488 |
mQualifiers[INDEX_NAVIGATION_STATE] = qualifier; |
| 489 |
|
| 490 |
} else if (qualifier instanceof NavigationMethodQualifier) { |
| 491 |
mQualifiers[INDEX_NAVIGATION_METHOD] = qualifier; |
| 492 |
|
| 493 |
} else if (qualifier instanceof ScreenDimensionQualifier) { |
| 494 |
mQualifiers[INDEX_SCREEN_DIMENSION] = qualifier; |
| 495 |
|
| 496 |
} else if (qualifier instanceof VersionQualifier) { |
| 497 |
mQualifiers[INDEX_VERSION] = qualifier; |
| 498 |
|
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Removes a given qualifier from the {@link FolderConfiguration}. |
| 504 |
* @param qualifier the {@link ResourceQualifier} to remove. |
| 505 |
*/ |
| 506 |
public void removeQualifier(@NonNull ResourceQualifier qualifier) { |
| 507 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 508 |
if (mQualifiers[i] == qualifier) { |
| 509 |
mQualifiers[i] = NULL_QUALIFIERS[i]; |
| 510 |
return; |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Returns a qualifier by its index. The total number of qualifiers can be accessed by |
| 517 |
* {@link #getQualifierCount()}. |
| 518 |
* @param index the index of the qualifier to return. |
| 519 |
* @return the qualifier or null if there are none at the index. |
| 520 |
*/ |
| 521 |
@Nullable |
| 522 |
public ResourceQualifier getQualifier(int index) { |
| 523 |
return mQualifiers[index]; |
| 524 |
} |
| 525 |
|
| 526 |
public void setCountryCodeQualifier(CountryCodeQualifier qualifier) { |
| 527 |
mQualifiers[INDEX_COUNTRY_CODE] = qualifier == null ? NULL_QUALIFIERS[INDEX_COUNTRY_CODE] |
| 528 |
: qualifier; |
| 529 |
} |
| 530 |
|
| 531 |
@Nullable |
| 532 |
public CountryCodeQualifier getCountryCodeQualifier() { |
| 533 |
return (CountryCodeQualifier)mQualifiers[INDEX_COUNTRY_CODE]; |
| 534 |
} |
| 535 |
|
| 536 |
public void setNetworkCodeQualifier(NetworkCodeQualifier qualifier) { |
| 537 |
mQualifiers[INDEX_NETWORK_CODE] = qualifier == null ? NULL_QUALIFIERS[INDEX_NETWORK_CODE] |
| 538 |
: qualifier; |
| 539 |
} |
| 540 |
|
| 541 |
@Nullable |
| 542 |
public NetworkCodeQualifier getNetworkCodeQualifier() { |
| 543 |
return (NetworkCodeQualifier)mQualifiers[INDEX_NETWORK_CODE]; |
| 544 |
} |
| 545 |
|
| 546 |
public void setLocaleQualifier(LocaleQualifier qualifier) { |
| 547 |
mQualifiers[INDEX_LOCALE] = qualifier == null ? NULL_QUALIFIERS[INDEX_LOCALE] |
| 548 |
: qualifier; |
| 549 |
} |
| 550 |
|
| 551 |
@Nullable |
| 552 |
public LocaleQualifier getLocaleQualifier() { |
| 553 |
return (LocaleQualifier)mQualifiers[INDEX_LOCALE]; |
| 554 |
} |
| 555 |
|
| 556 |
public void setLayoutDirectionQualifier(LayoutDirectionQualifier qualifier) { |
| 557 |
mQualifiers[INDEX_LAYOUT_DIR] = qualifier == null ? NULL_QUALIFIERS[INDEX_LAYOUT_DIR] |
| 558 |
: qualifier; |
| 559 |
} |
| 560 |
|
| 561 |
@Nullable |
| 562 |
public LayoutDirectionQualifier getLayoutDirectionQualifier() { |
| 563 |
return (LayoutDirectionQualifier)mQualifiers[INDEX_LAYOUT_DIR]; |
| 564 |
} |
| 565 |
|
| 566 |
public void setSmallestScreenWidthQualifier(SmallestScreenWidthQualifier qualifier) { |
| 567 |
mQualifiers[INDEX_SMALLEST_SCREEN_WIDTH] = qualifier == null ? NULL_QUALIFIERS |
| 568 |
[INDEX_SMALLEST_SCREEN_WIDTH] |
| 569 |
: qualifier; |
| 570 |
} |
| 571 |
|
| 572 |
@Nullable |
| 573 |
public SmallestScreenWidthQualifier getSmallestScreenWidthQualifier() { |
| 574 |
return (SmallestScreenWidthQualifier) mQualifiers[INDEX_SMALLEST_SCREEN_WIDTH]; |
| 575 |
} |
| 576 |
|
| 577 |
public void setScreenWidthQualifier(ScreenWidthQualifier qualifier) { |
| 578 |
mQualifiers[INDEX_SCREEN_WIDTH] = qualifier == null ? NULL_QUALIFIERS[INDEX_SCREEN_WIDTH] |
| 579 |
: qualifier; |
| 580 |
} |
| 581 |
|
| 582 |
@Nullable |
| 583 |
public ScreenWidthQualifier getScreenWidthQualifier() { |
| 584 |
return (ScreenWidthQualifier) mQualifiers[INDEX_SCREEN_WIDTH]; |
| 585 |
} |
| 586 |
|
| 587 |
public void setScreenHeightQualifier(ScreenHeightQualifier qualifier) { |
| 588 |
mQualifiers[INDEX_SCREEN_HEIGHT] = qualifier == null ? NULL_QUALIFIERS[INDEX_SCREEN_HEIGHT] |
| 589 |
: qualifier; |
| 590 |
} |
| 591 |
|
| 592 |
@Nullable |
| 593 |
public ScreenHeightQualifier getScreenHeightQualifier() { |
| 594 |
return (ScreenHeightQualifier) mQualifiers[INDEX_SCREEN_HEIGHT]; |
| 595 |
} |
| 596 |
|
| 597 |
public void setScreenSizeQualifier(ScreenSizeQualifier qualifier) { |
| 598 |
mQualifiers[INDEX_SCREEN_LAYOUT_SIZE] = qualifier == null |
| 599 |
? NULL_QUALIFIERS[INDEX_SCREEN_LAYOUT_SIZE] : qualifier; |
| 600 |
} |
| 601 |
|
| 602 |
@Nullable |
| 603 |
public ScreenSizeQualifier getScreenSizeQualifier() { |
| 604 |
return (ScreenSizeQualifier)mQualifiers[INDEX_SCREEN_LAYOUT_SIZE]; |
| 605 |
} |
| 606 |
|
| 607 |
public void setScreenRatioQualifier(ScreenRatioQualifier qualifier) { |
| 608 |
mQualifiers[INDEX_SCREEN_RATIO] = qualifier == null ? NULL_QUALIFIERS[INDEX_SCREEN_RATIO] |
| 609 |
: qualifier; |
| 610 |
} |
| 611 |
|
| 612 |
@Nullable |
| 613 |
public ScreenRatioQualifier getScreenRatioQualifier() { |
| 614 |
return (ScreenRatioQualifier)mQualifiers[INDEX_SCREEN_RATIO]; |
| 615 |
} |
| 616 |
|
| 617 |
public void setScreenRoundQualifier(ScreenRoundQualifier qualifier) { |
| 618 |
mQualifiers[INDEX_SCREEN_ROUND] = qualifier == null ? NULL_QUALIFIERS[INDEX_SCREEN_ROUND] |
| 619 |
: qualifier; |
| 620 |
} |
| 621 |
|
| 622 |
@Nullable |
| 623 |
public ScreenRoundQualifier getScreenRoundQualifier() { |
| 624 |
return (ScreenRoundQualifier)mQualifiers[INDEX_SCREEN_ROUND]; |
| 625 |
} |
| 626 |
|
| 627 |
public void setScreenOrientationQualifier(ScreenOrientationQualifier qualifier) { |
| 628 |
mQualifiers[INDEX_SCREEN_ORIENTATION] = qualifier == null |
| 629 |
? NULL_QUALIFIERS[INDEX_SCREEN_ORIENTATION] : qualifier; |
| 630 |
} |
| 631 |
|
| 632 |
@Nullable |
| 633 |
public ScreenOrientationQualifier getScreenOrientationQualifier() { |
| 634 |
return (ScreenOrientationQualifier)mQualifiers[INDEX_SCREEN_ORIENTATION]; |
| 635 |
} |
| 636 |
|
| 637 |
public void setUiModeQualifier(UiModeQualifier qualifier) { |
| 638 |
mQualifiers[INDEX_UI_MODE] = qualifier == null ? NULL_QUALIFIERS[INDEX_UI_MODE] |
| 639 |
: qualifier; |
| 640 |
} |
| 641 |
|
| 642 |
@Nullable |
| 643 |
public UiModeQualifier getUiModeQualifier() { |
| 644 |
return (UiModeQualifier)mQualifiers[INDEX_UI_MODE]; |
| 645 |
} |
| 646 |
|
| 647 |
public void setNightModeQualifier(NightModeQualifier qualifier) { |
| 648 |
mQualifiers[INDEX_NIGHT_MODE] = qualifier == null ? NULL_QUALIFIERS[INDEX_NIGHT_MODE] |
| 649 |
: qualifier; |
| 650 |
} |
| 651 |
|
| 652 |
@Nullable |
| 653 |
public NightModeQualifier getNightModeQualifier() { |
| 654 |
return (NightModeQualifier)mQualifiers[INDEX_NIGHT_MODE]; |
| 655 |
} |
| 656 |
|
| 657 |
public void setDensityQualifier(DensityQualifier qualifier) { |
| 658 |
mQualifiers[INDEX_PIXEL_DENSITY] = qualifier == null ? NULL_QUALIFIERS[INDEX_PIXEL_DENSITY] |
| 659 |
: qualifier; |
| 660 |
} |
| 661 |
|
| 662 |
@Nullable |
| 663 |
public DensityQualifier getDensityQualifier() { |
| 664 |
return (DensityQualifier)mQualifiers[INDEX_PIXEL_DENSITY]; |
| 665 |
} |
| 666 |
|
| 667 |
public void setTouchTypeQualifier(TouchScreenQualifier qualifier) { |
| 668 |
mQualifiers[INDEX_TOUCH_TYPE] = qualifier == null ? NULL_QUALIFIERS[INDEX_TOUCH_TYPE] |
| 669 |
: qualifier; |
| 670 |
} |
| 671 |
|
| 672 |
@Nullable |
| 673 |
public TouchScreenQualifier getTouchTypeQualifier() { |
| 674 |
return (TouchScreenQualifier)mQualifiers[INDEX_TOUCH_TYPE]; |
| 675 |
} |
| 676 |
|
| 677 |
public void setKeyboardStateQualifier(KeyboardStateQualifier qualifier) { |
| 678 |
mQualifiers[INDEX_KEYBOARD_STATE] = qualifier == null ? NULL_QUALIFIERS[INDEX_KEYBOARD_STATE] |
| 679 |
: qualifier; |
| 680 |
} |
| 681 |
|
| 682 |
@Nullable |
| 683 |
public KeyboardStateQualifier getKeyboardStateQualifier() { |
| 684 |
return (KeyboardStateQualifier)mQualifiers[INDEX_KEYBOARD_STATE]; |
| 685 |
} |
| 686 |
|
| 687 |
public void setTextInputMethodQualifier(TextInputMethodQualifier qualifier) { |
| 688 |
mQualifiers[INDEX_TEXT_INPUT_METHOD] = qualifier == null ? NULL_QUALIFIERS[INDEX_TEXT_INPUT_METHOD] |
| 689 |
: qualifier; |
| 690 |
} |
| 691 |
|
| 692 |
@Nullable |
| 693 |
public TextInputMethodQualifier getTextInputMethodQualifier() { |
| 694 |
return (TextInputMethodQualifier)mQualifiers[INDEX_TEXT_INPUT_METHOD]; |
| 695 |
} |
| 696 |
|
| 697 |
public void setNavigationStateQualifier(NavigationStateQualifier qualifier) { |
| 698 |
mQualifiers[INDEX_NAVIGATION_STATE] = qualifier == null |
| 699 |
? NULL_QUALIFIERS[INDEX_NAVIGATION_STATE] : qualifier; |
| 700 |
} |
| 701 |
|
| 702 |
@Nullable |
| 703 |
public NavigationStateQualifier getNavigationStateQualifier() { |
| 704 |
return (NavigationStateQualifier)mQualifiers[INDEX_NAVIGATION_STATE]; |
| 705 |
} |
| 706 |
|
| 707 |
public void setNavigationMethodQualifier(NavigationMethodQualifier qualifier) { |
| 708 |
mQualifiers[INDEX_NAVIGATION_METHOD] = qualifier == null |
| 709 |
? NULL_QUALIFIERS[INDEX_NAVIGATION_METHOD] : qualifier; |
| 710 |
} |
| 711 |
|
| 712 |
@Nullable |
| 713 |
public NavigationMethodQualifier getNavigationMethodQualifier() { |
| 714 |
return (NavigationMethodQualifier)mQualifiers[INDEX_NAVIGATION_METHOD]; |
| 715 |
} |
| 716 |
|
| 717 |
public void setScreenDimensionQualifier(ScreenDimensionQualifier qualifier) { |
| 718 |
mQualifiers[INDEX_SCREEN_DIMENSION] = qualifier == null |
| 719 |
? NULL_QUALIFIERS[INDEX_SCREEN_DIMENSION] : qualifier; |
| 720 |
} |
| 721 |
|
| 722 |
@Nullable |
| 723 |
public ScreenDimensionQualifier getScreenDimensionQualifier() { |
| 724 |
return (ScreenDimensionQualifier)mQualifiers[INDEX_SCREEN_DIMENSION]; |
| 725 |
} |
| 726 |
|
| 727 |
public void setVersionQualifier(VersionQualifier qualifier) { |
| 728 |
mQualifiers[INDEX_VERSION] = qualifier == null ? NULL_QUALIFIERS[INDEX_VERSION] |
| 729 |
: qualifier; |
| 730 |
} |
| 731 |
|
| 732 |
@Nullable |
| 733 |
public VersionQualifier getVersionQualifier() { |
| 734 |
return (VersionQualifier)mQualifiers[INDEX_VERSION]; |
| 735 |
} |
| 736 |
|
| 737 |
/** |
| 738 |
* Normalize a folder configuration based on the API level of its qualifiers |
| 739 |
*/ |
| 740 |
public void normalize() { |
| 741 |
int minSdk = 1; |
| 742 |
for (int i = 0; i < mQualifiers.length; i++) { |
| 743 |
ResourceQualifier qualifier = mQualifiers[i]; |
| 744 |
if (qualifier != NULL_QUALIFIERS[i]) { |
| 745 |
int min = qualifier.since(); |
| 746 |
if (min > minSdk) { |
| 747 |
minSdk = min; |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
if (minSdk == 1) { |
| 753 |
return; |
| 754 |
} |
| 755 |
|
| 756 |
if (mQualifiers[INDEX_VERSION] == NULL_QUALIFIERS[INDEX_VERSION] || |
| 757 |
((VersionQualifier)mQualifiers[INDEX_VERSION]).getVersion() < minSdk) { |
| 758 |
mQualifiers[INDEX_VERSION] = new VersionQualifier(minSdk); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
/** |
| 763 |
* Updates the {@link SmallestScreenWidthQualifier}, {@link ScreenWidthQualifier}, and |
| 764 |
* {@link ScreenHeightQualifier} based on the (required) values of |
| 765 |
* {@link ScreenDimensionQualifier} {@link DensityQualifier}, and |
| 766 |
* {@link ScreenOrientationQualifier}. |
| 767 |
* |
| 768 |
* Also the density cannot be {@link Density#NODPI} as it's not valid on a device. |
| 769 |
*/ |
| 770 |
public void updateScreenWidthAndHeight() { |
| 771 |
|
| 772 |
ResourceQualifier sizeQ = mQualifiers[INDEX_SCREEN_DIMENSION]; |
| 773 |
ResourceQualifier densityQ = mQualifiers[INDEX_PIXEL_DENSITY]; |
| 774 |
ResourceQualifier orientQ = mQualifiers[INDEX_SCREEN_ORIENTATION]; |
| 775 |
|
| 776 |
if (sizeQ != NULL_QUALIFIERS[INDEX_SCREEN_DIMENSION] |
| 777 |
&& densityQ != NULL_QUALIFIERS[INDEX_PIXEL_DENSITY] |
| 778 |
&& orientQ != NULL_QUALIFIERS[INDEX_SCREEN_ORIENTATION]) { |
| 779 |
Density density = ((DensityQualifier) densityQ).getValue(); |
| 780 |
if (density == Density.NODPI || density == Density.ANYDPI) { |
| 781 |
return; |
| 782 |
} |
| 783 |
|
| 784 |
ScreenOrientation orientation = ((ScreenOrientationQualifier) orientQ).getValue(); |
| 785 |
|
| 786 |
int size1 = ((ScreenDimensionQualifier) sizeQ).getValue1(); |
| 787 |
int size2 = ((ScreenDimensionQualifier) sizeQ).getValue2(); |
| 788 |
|
| 789 |
// make sure size1 is the biggest (should be the case, but make sure) |
| 790 |
if (size1 < size2) { |
| 791 |
int a = size1; |
| 792 |
size1 = size2; |
| 793 |
size2 = a; |
| 794 |
} |
| 795 |
|
| 796 |
// compute the dp. round them up since we want -w480dp to match a 480.5dp screen |
| 797 |
int dp1 = (int) Math.ceil(size1 * Density.DEFAULT_DENSITY / density.getDpiValue()); |
| 798 |
int dp2 = (int) Math.ceil(size2 * Density.DEFAULT_DENSITY / density.getDpiValue()); |
| 799 |
|
| 800 |
setSmallestScreenWidthQualifier(new SmallestScreenWidthQualifier(dp2)); |
| 801 |
|
| 802 |
switch (orientation) { |
| 803 |
case PORTRAIT: |
| 804 |
setScreenWidthQualifier(new ScreenWidthQualifier(dp2)); |
| 805 |
setScreenHeightQualifier(new ScreenHeightQualifier(dp1)); |
| 806 |
break; |
| 807 |
case LANDSCAPE: |
| 808 |
setScreenWidthQualifier(new ScreenWidthQualifier(dp1)); |
| 809 |
setScreenHeightQualifier(new ScreenHeightQualifier(dp2)); |
| 810 |
break; |
| 811 |
case SQUARE: |
| 812 |
setScreenWidthQualifier(new ScreenWidthQualifier(dp2)); |
| 813 |
setScreenHeightQualifier(new ScreenHeightQualifier(dp2)); |
| 814 |
break; |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
/** |
| 820 |
* Returns whether an object is equals to the receiver. |
| 821 |
*/ |
| 822 |
@Override |
| 823 |
public boolean equals(Object obj) { |
| 824 |
if (obj == this) { |
| 825 |
return true; |
| 826 |
} |
| 827 |
|
| 828 |
if (obj instanceof FolderConfiguration) { |
| 829 |
FolderConfiguration fc = (FolderConfiguration)obj; |
| 830 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 831 |
ResourceQualifier qualifier = mQualifiers[i]; |
| 832 |
ResourceQualifier fcQualifier = fc.mQualifiers[i]; |
| 833 |
if (!Objects.equal(qualifier, fcQualifier)) { |
| 834 |
return false; |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
return true; |
| 839 |
} |
| 840 |
|
| 841 |
return false; |
| 842 |
} |
| 843 |
|
| 844 |
@Override |
| 845 |
public int hashCode() { |
| 846 |
return toString().hashCode(); |
| 847 |
} |
| 848 |
|
| 849 |
/** |
| 850 |
* Returns whether the Configuration has only default values. |
| 851 |
*/ |
| 852 |
public boolean isDefault() { |
| 853 |
for (int i = 0; i < mQualifiers.length; i++) { |
| 854 |
ResourceQualifier irq = mQualifiers[i]; |
| 855 |
if (irq != NULL_QUALIFIERS[i]) { |
| 856 |
return false; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
return true; |
| 861 |
} |
| 862 |
|
| 863 |
/** |
| 864 |
* Returns the name of a folder with the configuration. |
| 865 |
*/ |
| 866 |
@NonNull |
| 867 |
public String getFolderName(@NonNull ResourceFolderType folder) { |
| 868 |
StringBuilder result = new StringBuilder(folder.getName()); |
| 869 |
|
| 870 |
for (int i = 0; i < mQualifiers.length; i++) { |
| 871 |
ResourceQualifier qualifier = mQualifiers[i]; |
| 872 |
if (qualifier != NULL_QUALIFIERS[i]) { |
| 873 |
String segment = qualifier.getFolderSegment(); |
| 874 |
if (segment != null && !segment.isEmpty()) { |
| 875 |
result.append(SdkConstants.RES_QUALIFIER_SEP); |
| 876 |
result.append(segment); |
| 877 |
} |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
return result.toString(); |
| 882 |
} |
| 883 |
|
| 884 |
/** |
| 885 |
* Returns the folder configuration as a unique key |
| 886 |
*/ |
| 887 |
@NonNull |
| 888 |
public String getUniqueKey() { |
| 889 |
StringBuilder result = new StringBuilder(100); |
| 890 |
|
| 891 |
for (int i = 0; i < mQualifiers.length; i++) { |
| 892 |
ResourceQualifier qualifier = mQualifiers[i]; |
| 893 |
if (qualifier != NULL_QUALIFIERS[i]) { |
| 894 |
String segment = qualifier.getFolderSegment(); |
| 895 |
if (segment != null && !segment.isEmpty()) { |
| 896 |
result.append(SdkConstants.RES_QUALIFIER_SEP); |
| 897 |
result.append(segment); |
| 898 |
} |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
return result.toString(); |
| 903 |
} |
| 904 |
|
| 905 |
/** |
| 906 |
* Returns the folder configuration as a qualifier string, e.g. |
| 907 |
* for the folder values-en-rUS this returns "en-rUS". For the |
| 908 |
* default configuration it returns "". |
| 909 |
*/ |
| 910 |
@NonNull |
| 911 |
public String getQualifierString() { |
| 912 |
StringBuilder result = new StringBuilder(40); |
| 913 |
|
| 914 |
for (int i = 0; i < mQualifiers.length; i++) { |
| 915 |
ResourceQualifier qualifier = mQualifiers[i]; |
| 916 |
if (qualifier != NULL_QUALIFIERS[i]) { |
| 917 |
String segment = qualifier.getFolderSegment(); |
| 918 |
if (segment != null && !segment.isEmpty()) { |
| 919 |
if (result.length() > 0) { |
| 920 |
result.append(SdkConstants.RES_QUALIFIER_SEP); |
| 921 |
} |
| 922 |
result.append(segment); |
| 923 |
} |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
return result.toString(); |
| 928 |
} |
| 929 |
|
| 930 |
/** |
| 931 |
* Returns {@link #toDisplayString()}. |
| 932 |
*/ |
| 933 |
@NonNull |
| 934 |
@Override |
| 935 |
public String toString() { |
| 936 |
return toDisplayString(); |
| 937 |
} |
| 938 |
|
| 939 |
/** |
| 940 |
* Returns a string valid for display purpose. |
| 941 |
*/ |
| 942 |
@NonNull |
| 943 |
public String toDisplayString() { |
| 944 |
if (isDefault()) { |
| 945 |
return "default"; |
| 946 |
} |
| 947 |
|
| 948 |
StringBuilder result = null; |
| 949 |
int index = 0; |
| 950 |
|
| 951 |
while (index < INDEX_COUNT) { |
| 952 |
ResourceQualifier qualifier = mQualifiers[index]; |
| 953 |
if (qualifier != NULL_QUALIFIERS[index++]) { |
| 954 |
if (result == null) { |
| 955 |
result = new StringBuilder(); |
| 956 |
} else { |
| 957 |
result.append(", "); //$NON-NLS-1$ |
| 958 |
} |
| 959 |
result.append(qualifier.getLongDisplayValue()); |
| 960 |
|
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
return result == null ? "" : result.toString(); |
| 965 |
} |
| 966 |
|
| 967 |
/** |
| 968 |
* Returns a string for display purposes which uses only the short names of the qualifiers |
| 969 |
*/ |
| 970 |
@NonNull |
| 971 |
public String toShortDisplayString() { |
| 972 |
if (isDefault()) { |
| 973 |
return "default"; |
| 974 |
} |
| 975 |
|
| 976 |
StringBuilder result = new StringBuilder(100); |
| 977 |
int index = 0; |
| 978 |
|
| 979 |
while (index < INDEX_COUNT) { |
| 980 |
ResourceQualifier qualifier = mQualifiers[index]; |
| 981 |
if (qualifier != NULL_QUALIFIERS[index++]) { |
| 982 |
if (result.length() > 0) { |
| 983 |
result.append(','); |
| 984 |
} |
| 985 |
result.append(qualifier.getShortDisplayValue()); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
return result.toString(); |
| 990 |
} |
| 991 |
|
| 992 |
@Override |
| 993 |
public int compareTo(@NonNull FolderConfiguration folderConfig) { |
| 994 |
// default are always at the top. |
| 995 |
if (isDefault()) { |
| 996 |
if (folderConfig.isDefault()) { |
| 997 |
return 0; |
| 998 |
} |
| 999 |
return -1; |
| 1000 |
} |
| 1001 |
|
| 1002 |
// now we compare the qualifiers |
| 1003 |
for (int i = 0 ; i < INDEX_COUNT; i++) { |
| 1004 |
ResourceQualifier qualifier1 = mQualifiers[i]; |
| 1005 |
ResourceQualifier qualifier2 = folderConfig.mQualifiers[i]; |
| 1006 |
if (Objects.equal(qualifier1, qualifier2)) { |
| 1007 |
continue; |
| 1008 |
} |
| 1009 |
if (qualifier1 != null && qualifier2 != null) { |
| 1010 |
return qualifier1.compareTo(qualifier2); |
| 1011 |
} |
| 1012 |
return qualifier1 == NULL_QUALIFIERS[i] ? -1 : 1; |
| 1013 |
} |
| 1014 |
|
| 1015 |
// if we arrive here, all the qualifier matches |
| 1016 |
return 0; |
| 1017 |
} |
| 1018 |
|
| 1019 |
/** |
| 1020 |
* Returns the best matching {@link Configurable} for this configuration. |
| 1021 |
* |
| 1022 |
* @param configurables the list of {@link Configurable} to choose from. |
| 1023 |
* |
| 1024 |
* @return an item from the given list of {@link Configurable} or null. |
| 1025 |
* |
| 1026 |
* See http://d.android.com/guide/topics/resources/resources-i18n.html#best-match |
| 1027 |
*/ |
| 1028 |
@Nullable |
| 1029 |
public <T extends Configurable> T findMatchingConfigurable(@Nullable List<T> configurables) { |
| 1030 |
// Because we skip qualifiers where reference configuration doesn't have a valid qualifier, |
| 1031 |
// we can end up with more than one match. In this case, we just take the first one. |
| 1032 |
List<T> matches = findMatchingConfigurables(configurables); |
| 1033 |
return matches.isEmpty() ? null : matches.get(0); |
| 1034 |
} |
| 1035 |
|
| 1036 |
/** |
| 1037 |
* Tries to eliminate as many {@link Configurable}s as possible. It skips the |
| 1038 |
* {@link ResourceQualifier} if it's not valid and assumes that all resources match it. |
| 1039 |
* |
| 1040 |
* @param configurables the list of {@code Configurable} to choose from. |
| 1041 |
* |
| 1042 |
* @return a list of items from the above list. This may be empty. |
| 1043 |
*/ |
| 1044 |
@NonNull |
| 1045 |
public <T extends Configurable> List<T> findMatchingConfigurables( |
| 1046 |
@Nullable List<T> configurables) { |
| 1047 |
if (configurables == null) { |
| 1048 |
return Collections.emptyList(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
// |
| 1052 |
// 1: eliminate resources that contradict the reference configuration |
| 1053 |
// 2: pick next qualifier type |
| 1054 |
// 3: check if any resources use this qualifier, if no, back to 2, else move on to 4. |
| 1055 |
// 4: eliminate resources that don't use this qualifier. |
| 1056 |
// 5: if more than one resource left, go back to 2. |
| 1057 |
// |
| 1058 |
// The precedence of the qualifiers is more important than the number of qualifiers that |
| 1059 |
// exactly match the device. |
| 1060 |
|
| 1061 |
// 1: eliminate resources that contradict |
| 1062 |
ArrayList<T> matchingConfigurables = new ArrayList<T>(); |
| 1063 |
for (T res : configurables) { |
| 1064 |
final FolderConfiguration configuration = res.getConfiguration(); |
| 1065 |
if (configuration != null && configuration.isMatchFor(this)) { |
| 1066 |
matchingConfigurables.add(res); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
// if there is at most one match, just take it |
| 1071 |
if (matchingConfigurables.size() < 2) { |
| 1072 |
return matchingConfigurables; |
| 1073 |
} |
| 1074 |
|
| 1075 |
// 2. Loop on the qualifiers, and eliminate matches |
| 1076 |
final int count = getQualifierCount(); |
| 1077 |
for (int q = 0; q < count; q++) { |
| 1078 |
// look to see if one configurable has this qualifier. |
| 1079 |
// At the same time also record the best match value for the qualifier (if applicable). |
| 1080 |
|
| 1081 |
// The reference value, to find the best match. |
| 1082 |
// Note that this qualifier could be null. In which case any qualifier found in the |
| 1083 |
// possible match, will all be considered best match. |
| 1084 |
ResourceQualifier referenceQualifier = getQualifier(q); |
| 1085 |
|
| 1086 |
// If referenceQualifier is null, we don't eliminate resources based on it. |
| 1087 |
if (referenceQualifier == NULL_QUALIFIERS[q] || referenceQualifier == null) { |
| 1088 |
continue; |
| 1089 |
} |
| 1090 |
|
| 1091 |
boolean found = false; |
| 1092 |
ResourceQualifier bestMatch = null; // this is to store the best match. |
| 1093 |
for (T configurable : matchingConfigurables) { |
| 1094 |
ResourceQualifier qualifier = configurable.getConfiguration().getQualifier(q); |
| 1095 |
if (qualifier != null) { |
| 1096 |
// set the flag. |
| 1097 |
found = true; |
| 1098 |
|
| 1099 |
// Now check for a best match. If the reference qualifier is null , |
| 1100 |
// any qualifier is a "best" match (we don't need to record all of them. |
| 1101 |
// Instead the non compatible ones are removed below) |
| 1102 |
if (qualifier.isBetterMatchThan(bestMatch, referenceQualifier)) { |
| 1103 |
bestMatch = qualifier; |
| 1104 |
} |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
// 4. If a configurable has a qualifier at the current index, remove all the ones that |
| 1109 |
// do not have one, or whose qualifier value does not equal the best match found above |
| 1110 |
// unless there's no reference qualifier, in which case they are all considered |
| 1111 |
// "best" match. |
| 1112 |
if (found) { |
| 1113 |
for (int i = 0; i < matchingConfigurables.size(); ) { |
| 1114 |
T configurable = matchingConfigurables.get(i); |
| 1115 |
FolderConfiguration configuration = configurable.getConfiguration(); |
| 1116 |
ResourceQualifier qualifier = configuration.getQualifier(q); |
| 1117 |
|
| 1118 |
if (qualifier == null) { |
| 1119 |
// this resource has no qualifier of this type: rejected. |
| 1120 |
matchingConfigurables.remove(configurable); |
| 1121 |
} else if (bestMatch != null && !bestMatch.equals(qualifier)) { |
| 1122 |
// there's a reference qualifier and there is a better match for it than |
| 1123 |
// this resource, so we reject it. |
| 1124 |
matchingConfigurables.remove(configurable); |
| 1125 |
} else { |
| 1126 |
// looks like we keep this resource, move on to the next one. |
| 1127 |
//noinspection AssignmentToForLoopParameter |
| 1128 |
i++; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
// at this point we may have run out of matching resources before going |
| 1133 |
// through all the qualifiers. |
| 1134 |
if (matchingConfigurables.size() < 2) { |
| 1135 |
break; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
// We've exhausted all the qualifiers. If we still have matching ones left, return all. |
| 1141 |
return matchingConfigurables; |
| 1142 |
} |
| 1143 |
|
| 1144 |
/** |
| 1145 |
* Returns whether the configuration is a match for the given reference config. |
| 1146 |
* <p>A match means that, for each qualifier of this config |
| 1147 |
* <ul> |
| 1148 |
* <li>The reference config has no value set |
| 1149 |
* <li>or, the qualifier of the reference config is a match. Depending on the qualifier type |
| 1150 |
* this does not mean the same exact value.</li> |
| 1151 |
* </ul> |
| 1152 |
* @param referenceConfig The reference configuration to test against. |
| 1153 |
* @return true if the configuration matches. |
| 1154 |
*/ |
| 1155 |
public boolean isMatchFor(@Nullable FolderConfiguration referenceConfig) { |
| 1156 |
if (referenceConfig == null) { |
| 1157 |
return false; |
| 1158 |
} |
| 1159 |
|
| 1160 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 1161 |
ResourceQualifier testQualifier = mQualifiers[i]; |
| 1162 |
ResourceQualifier referenceQualifier = referenceConfig.mQualifiers[i]; |
| 1163 |
|
| 1164 |
// it's only a non match if both qualifiers are non-null, and they don't match. |
| 1165 |
if (testQualifier != null && testQualifier != testQualifier.getNullQualifier() && |
| 1166 |
referenceQualifier != null && referenceQualifier != referenceQualifier.getNullQualifier() && |
| 1167 |
!testQualifier.isMatchFor(referenceQualifier)) { |
| 1168 |
return false; |
| 1169 |
} |
| 1170 |
} |
| 1171 |
|
| 1172 |
return true; |
| 1173 |
} |
| 1174 |
|
| 1175 |
/** |
| 1176 |
* Returns the index of the first non null {@link ResourceQualifier} starting at index |
| 1177 |
* <var>startIndex</var> |
| 1178 |
* @return -1 if no qualifier was found. |
| 1179 |
*/ |
| 1180 |
public int getHighestPriorityQualifier(int startIndex) { |
| 1181 |
for (int i = startIndex ; i < INDEX_COUNT ; i++) { |
| 1182 |
if (mQualifiers[i] != NULL_QUALIFIERS[i]) { |
| 1183 |
return i; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
return -1; |
| 1188 |
} |
| 1189 |
|
| 1190 |
/** |
| 1191 |
* Create default qualifiers. |
| 1192 |
* <p>This creates qualifiers with no values for all indices. |
| 1193 |
*/ |
| 1194 |
public void createDefault() { |
| 1195 |
mQualifiers[INDEX_COUNTRY_CODE] = new CountryCodeQualifier(); |
| 1196 |
mQualifiers[INDEX_NETWORK_CODE] = new NetworkCodeQualifier(); |
| 1197 |
mQualifiers[INDEX_LOCALE] = new LocaleQualifier(); |
| 1198 |
mQualifiers[INDEX_LAYOUT_DIR] = new LayoutDirectionQualifier(); |
| 1199 |
mQualifiers[INDEX_SMALLEST_SCREEN_WIDTH] = new SmallestScreenWidthQualifier(); |
| 1200 |
mQualifiers[INDEX_SCREEN_WIDTH] = new ScreenWidthQualifier(); |
| 1201 |
mQualifiers[INDEX_SCREEN_HEIGHT] = new ScreenHeightQualifier(); |
| 1202 |
mQualifiers[INDEX_SCREEN_LAYOUT_SIZE] = new ScreenSizeQualifier(); |
| 1203 |
mQualifiers[INDEX_SCREEN_RATIO] = new ScreenRatioQualifier(); |
| 1204 |
mQualifiers[INDEX_SCREEN_ROUND] = new ScreenRoundQualifier(); |
| 1205 |
mQualifiers[INDEX_SCREEN_ORIENTATION] = new ScreenOrientationQualifier(); |
| 1206 |
mQualifiers[INDEX_UI_MODE] = new UiModeQualifier(); |
| 1207 |
mQualifiers[INDEX_NIGHT_MODE] = new NightModeQualifier(); |
| 1208 |
mQualifiers[INDEX_PIXEL_DENSITY] = new DensityQualifier(); |
| 1209 |
mQualifiers[INDEX_TOUCH_TYPE] = new TouchScreenQualifier(); |
| 1210 |
mQualifiers[INDEX_KEYBOARD_STATE] = new KeyboardStateQualifier(); |
| 1211 |
mQualifiers[INDEX_TEXT_INPUT_METHOD] = new TextInputMethodQualifier(); |
| 1212 |
mQualifiers[INDEX_NAVIGATION_STATE] = new NavigationStateQualifier(); |
| 1213 |
mQualifiers[INDEX_NAVIGATION_METHOD] = new NavigationMethodQualifier(); |
| 1214 |
mQualifiers[INDEX_SCREEN_DIMENSION] = new ScreenDimensionQualifier(); |
| 1215 |
mQualifiers[INDEX_VERSION] = new VersionQualifier(); |
| 1216 |
} |
| 1217 |
|
| 1218 |
/** |
| 1219 |
* Returns an array of all the non null qualifiers. |
| 1220 |
*/ |
| 1221 |
@NonNull |
| 1222 |
public ResourceQualifier[] getQualifiers() { |
| 1223 |
int count = 0; |
| 1224 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 1225 |
if (mQualifiers[i] != null && mQualifiers[i] != NULL_QUALIFIERS[i]) { |
| 1226 |
count++; |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
ResourceQualifier[] array = new ResourceQualifier[count]; |
| 1231 |
int index = 0; |
| 1232 |
for (int i = 0 ; i < INDEX_COUNT ; i++) { |
| 1233 |
if (mQualifiers[i] != null && mQualifiers[i] != NULL_QUALIFIERS[i]) { |
| 1234 |
array[index++] = mQualifiers[i]; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
|
| 1238 |
return array; |
| 1239 |
} |
| 1240 |
} |