Resources.java revision 4ece3d6bb18a609afcd0e82f0340b7d36ba24eea
1/* 2 * Copyright (C) 2006 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 17package android.content.res; 18 19import android.animation.Animator; 20import android.animation.StateListAnimator; 21import android.annotation.AnimRes; 22import android.annotation.AnyRes; 23import android.annotation.ArrayRes; 24import android.annotation.AttrRes; 25import android.annotation.BoolRes; 26import android.annotation.ColorInt; 27import android.annotation.ColorRes; 28import android.annotation.DimenRes; 29import android.annotation.DrawableRes; 30import android.annotation.FractionRes; 31import android.annotation.IntegerRes; 32import android.annotation.LayoutRes; 33import android.annotation.NonNull; 34import android.annotation.Nullable; 35import android.annotation.PluralsRes; 36import android.annotation.RawRes; 37import android.annotation.StringRes; 38import android.annotation.StyleRes; 39import android.annotation.StyleableRes; 40import android.annotation.XmlRes; 41import android.content.pm.ActivityInfo; 42import android.graphics.Movie; 43import android.graphics.drawable.Drawable; 44import android.graphics.drawable.Drawable.ConstantState; 45import android.graphics.drawable.DrawableInflater; 46import android.os.Build; 47import android.os.Bundle; 48import android.util.AttributeSet; 49import android.util.DisplayMetrics; 50import android.util.Log; 51import android.util.LongSparseArray; 52import android.util.Pools.SynchronizedPool; 53import android.util.TypedValue; 54import android.view.DisplayAdjustments; 55import android.view.ViewDebug; 56import android.view.ViewHierarchyEncoder; 57 58import com.android.internal.annotations.VisibleForTesting; 59import com.android.internal.util.GrowingArrayUtils; 60import com.android.internal.util.XmlUtils; 61 62import org.xmlpull.v1.XmlPullParser; 63import org.xmlpull.v1.XmlPullParserException; 64 65import java.io.IOException; 66import java.io.InputStream; 67import java.lang.ref.WeakReference; 68import java.util.ArrayList; 69 70/** 71 * Class for accessing an application's resources. This sits on top of the 72 * asset manager of the application (accessible through {@link #getAssets}) and 73 * provides a high-level API for getting typed data from the assets. 74 * 75 * <p>The Android resource system keeps track of all non-code assets associated with an 76 * application. You can use this class to access your application's resources. You can generally 77 * acquire the {@link android.content.res.Resources} instance associated with your application 78 * with {@link android.content.Context#getResources getResources()}.</p> 79 * 80 * <p>The Android SDK tools compile your application's resources into the application binary 81 * at build time. To use a resource, you must install it correctly in the source tree (inside 82 * your project's {@code res/} directory) and build your application. As part of the build 83 * process, the SDK tools generate symbols for each resource, which you can use in your application 84 * code to access the resources.</p> 85 * 86 * <p>Using application resources makes it easy to update various characteristics of your 87 * application without modifying code, and—by providing sets of alternative 88 * resources—enables you to optimize your application for a variety of device configurations 89 * (such as for different languages and screen sizes). This is an important aspect of developing 90 * Android applications that are compatible on different types of devices.</p> 91 * 92 * <p>For more information about using resources, see the documentation about <a 93 * href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>.</p> 94 */ 95public class Resources { 96 static final String TAG = "Resources"; 97 98 private static final Object sSync = new Object(); 99 100 // Used by BridgeResources in layoutlib 101 static Resources mSystem = null; 102 103 private ResourcesImpl mResourcesImpl; 104 105 // Pool of TypedArrays targeted to this Resources object. 106 final SynchronizedPool<TypedArray> mTypedArrayPool = new SynchronizedPool<>(5); 107 108 /** Used to inflate drawable objects from XML. */ 109 private DrawableInflater mDrawableInflater; 110 111 /** Lock object used to protect access to {@link #mTmpValue}. */ 112 private final Object mTmpValueLock = new Object(); 113 114 /** Single-item pool used to minimize TypedValue allocations. */ 115 private TypedValue mTmpValue = new TypedValue(); 116 117 final ClassLoader mClassLoader; 118 119 /** 120 * WeakReferences to Themes that were constructed from this Resources object. 121 * We keep track of these in case our underlying implementation is changed, in which case 122 * the Themes must also get updated ThemeImpls. 123 */ 124 private final ArrayList<WeakReference<Theme>> mThemeRefs = new ArrayList<>(); 125 126 /** 127 * Returns the most appropriate default theme for the specified target SDK version. 128 * <ul> 129 * <li>Below API 11: Gingerbread 130 * <li>APIs 12 thru 14: Holo 131 * <li>APIs 15 thru 23: Device default dark 132 * <li>APIs 24 and above: Device default light with dark action bar 133 * </ul> 134 * 135 * @param curTheme The current theme, or 0 if not specified. 136 * @param targetSdkVersion The target SDK version. 137 * @return A theme resource identifier 138 * @hide 139 */ 140 public static int selectDefaultTheme(int curTheme, int targetSdkVersion) { 141 return selectSystemTheme(curTheme, targetSdkVersion, 142 com.android.internal.R.style.Theme, 143 com.android.internal.R.style.Theme_Holo, 144 com.android.internal.R.style.Theme_DeviceDefault, 145 com.android.internal.R.style.Theme_DeviceDefault_Light_DarkActionBar); 146 } 147 148 /** @hide */ 149 public static int selectSystemTheme(int curTheme, int targetSdkVersion, int orig, int holo, 150 int dark, int deviceDefault) { 151 if (curTheme != 0) { 152 return curTheme; 153 } 154 if (targetSdkVersion < Build.VERSION_CODES.HONEYCOMB) { 155 return orig; 156 } 157 if (targetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 158 return holo; 159 } 160 if (targetSdkVersion < Build.VERSION_CODES.N) { 161 return dark; 162 } 163 return deviceDefault; 164 } 165 166 /** 167 * Return a global shared Resources object that provides access to only 168 * system resources (no application resources), and is not configured for 169 * the current screen (can not use dimension units, does not change based 170 * on orientation, etc). 171 */ 172 public static Resources getSystem() { 173 synchronized (sSync) { 174 Resources ret = mSystem; 175 if (ret == null) { 176 ret = new Resources(); 177 mSystem = ret; 178 } 179 return ret; 180 } 181 } 182 183 /** 184 * This exception is thrown by the resource APIs when a requested resource 185 * can not be found. 186 */ 187 public static class NotFoundException extends RuntimeException { 188 public NotFoundException() { 189 } 190 191 public NotFoundException(String name) { 192 super(name); 193 } 194 195 public NotFoundException(String name, Exception cause) { 196 super(name, cause); 197 } 198 } 199 200 /** 201 * Create a new Resources object on top of an existing set of assets in an 202 * AssetManager. 203 * 204 * @param assets Previously created AssetManager. 205 * @param metrics Current display metrics to consider when 206 * selecting/computing resource values. 207 * @param config Desired device configuration to consider when 208 * selecting/computing resource values (optional). 209 */ 210 public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) { 211 this(null); 212 mResourcesImpl = new ResourcesImpl(assets, metrics, config, 213 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO); 214 } 215 216 /** 217 * Creates a new Resources object with CompatibilityInfo. 218 * 219 * @param classLoader class loader for the package used to load custom 220 * resource classes, may be {@code null} to use system 221 * class loader 222 * @hide 223 */ 224 public Resources(@Nullable ClassLoader classLoader) { 225 mClassLoader = classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader; 226 } 227 228 /** 229 * Only for creating the System resources. 230 */ 231 private Resources() { 232 this(null); 233 234 final DisplayMetrics metrics = new DisplayMetrics(); 235 metrics.setToDefaults(); 236 237 final Configuration config = new Configuration(); 238 config.setToDefaults(); 239 240 mResourcesImpl = new ResourcesImpl(AssetManager.getSystem(), metrics, config, 241 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO); 242 } 243 244 /** 245 * Set the underlying implementation (containing all the resources and caches) 246 * and updates all Theme references to new implementations as well. 247 * @hide 248 */ 249 public void setImpl(ResourcesImpl impl) { 250 if (impl == mResourcesImpl) { 251 return; 252 } 253 254 mResourcesImpl = impl; 255 256 // Create new ThemeImpls that are identical to the ones we have. 257 synchronized (mThemeRefs) { 258 final int count = mThemeRefs.size(); 259 for (int i = 0; i < count; i++) { 260 WeakReference<Theme> weakThemeRef = mThemeRefs.get(i); 261 Theme theme = weakThemeRef != null ? weakThemeRef.get() : null; 262 if (theme != null) { 263 theme.setImpl(mResourcesImpl.newThemeImpl(theme.getKey())); 264 } 265 } 266 } 267 } 268 269 /** 270 * @hide 271 */ 272 public ResourcesImpl getImpl() { 273 return mResourcesImpl; 274 } 275 276 /** 277 * @hide 278 */ 279 public ClassLoader getClassLoader() { 280 return mClassLoader; 281 } 282 283 /** 284 * @return the inflater used to create drawable objects 285 * @hide Pending API finalization. 286 */ 287 public final DrawableInflater getDrawableInflater() { 288 if (mDrawableInflater == null) { 289 mDrawableInflater = new DrawableInflater(this, mClassLoader); 290 } 291 return mDrawableInflater; 292 } 293 294 /** 295 * Used by AnimatorInflater. 296 * 297 * @hide 298 */ 299 public ConfigurationBoundResourceCache<Animator> getAnimatorCache() { 300 return mResourcesImpl.getAnimatorCache(); 301 } 302 303 /** 304 * Used by AnimatorInflater. 305 * 306 * @hide 307 */ 308 public ConfigurationBoundResourceCache<StateListAnimator> getStateListAnimatorCache() { 309 return mResourcesImpl.getStateListAnimatorCache(); 310 } 311 312 /** 313 * Return the string value associated with a particular resource ID. The 314 * returned object will be a String if this is a plain string; it will be 315 * some other type of CharSequence if it is styled. 316 * {@more} 317 * 318 * @param id The desired resource identifier, as generated by the aapt 319 * tool. This integer encodes the package, type, and resource 320 * entry. The value 0 is an invalid identifier. 321 * 322 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 323 * 324 * @return CharSequence The string data associated with the resource, plus 325 * possibly styled text information. 326 */ 327 @NonNull public CharSequence getText(@StringRes int id) throws NotFoundException { 328 CharSequence res = mResourcesImpl.getAssets().getResourceText(id); 329 if (res != null) { 330 return res; 331 } 332 throw new NotFoundException("String resource ID #0x" 333 + Integer.toHexString(id)); 334 } 335 336 /** 337 * Returns the character sequence necessary for grammatically correct pluralization 338 * of the given resource ID for the given quantity. 339 * Note that the character sequence is selected based solely on grammatical necessity, 340 * and that such rules differ between languages. Do not assume you know which string 341 * will be returned for a given quantity. See 342 * <a href="{@docRoot}guide/topics/resources/string-resource.html#Plurals">String Resources</a> 343 * for more detail. 344 * 345 * @param id The desired resource identifier, as generated by the aapt 346 * tool. This integer encodes the package, type, and resource 347 * entry. The value 0 is an invalid identifier. 348 * @param quantity The number used to get the correct string for the current language's 349 * plural rules. 350 * 351 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 352 * 353 * @return CharSequence The string data associated with the resource, plus 354 * possibly styled text information. 355 */ 356 @NonNull 357 public CharSequence getQuantityText(@PluralsRes int id, int quantity) 358 throws NotFoundException { 359 return mResourcesImpl.getQuantityText(id, quantity); 360 } 361 362 /** 363 * Return the string value associated with a particular resource ID. It 364 * will be stripped of any styled text information. 365 * {@more} 366 * 367 * @param id The desired resource identifier, as generated by the aapt 368 * tool. This integer encodes the package, type, and resource 369 * entry. The value 0 is an invalid identifier. 370 * 371 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 372 * 373 * @return String The string data associated with the resource, 374 * stripped of styled text information. 375 */ 376 @NonNull 377 public String getString(@StringRes int id) throws NotFoundException { 378 return getText(id).toString(); 379 } 380 381 382 /** 383 * Return the string value associated with a particular resource ID, 384 * substituting the format arguments as defined in {@link java.util.Formatter} 385 * and {@link java.lang.String#format}. It will be stripped of any styled text 386 * information. 387 * {@more} 388 * 389 * @param id The desired resource identifier, as generated by the aapt 390 * tool. This integer encodes the package, type, and resource 391 * entry. The value 0 is an invalid identifier. 392 * 393 * @param formatArgs The format arguments that will be used for substitution. 394 * 395 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 396 * 397 * @return String The string data associated with the resource, 398 * stripped of styled text information. 399 */ 400 @NonNull 401 public String getString(@StringRes int id, Object... formatArgs) throws NotFoundException { 402 final String raw = getString(id); 403 return String.format(mResourcesImpl.getConfiguration().getLocales().get(0), raw, 404 formatArgs); 405 } 406 407 /** 408 * Formats the string necessary for grammatically correct pluralization 409 * of the given resource ID for the given quantity, using the given arguments. 410 * Note that the string is selected based solely on grammatical necessity, 411 * and that such rules differ between languages. Do not assume you know which string 412 * will be returned for a given quantity. See 413 * <a href="{@docRoot}guide/topics/resources/string-resource.html#Plurals">String Resources</a> 414 * for more detail. 415 * 416 * <p>Substitution of format arguments works as if using 417 * {@link java.util.Formatter} and {@link java.lang.String#format}. 418 * The resulting string will be stripped of any styled text information. 419 * 420 * @param id The desired resource identifier, as generated by the aapt 421 * tool. This integer encodes the package, type, and resource 422 * entry. The value 0 is an invalid identifier. 423 * @param quantity The number used to get the correct string for the current language's 424 * plural rules. 425 * @param formatArgs The format arguments that will be used for substitution. 426 * 427 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 428 * 429 * @return String The string data associated with the resource, 430 * stripped of styled text information. 431 */ 432 @NonNull 433 public String getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs) 434 throws NotFoundException { 435 String raw = getQuantityText(id, quantity).toString(); 436 return String.format(mResourcesImpl.getConfiguration().getLocales().get(0), raw, 437 formatArgs); 438 } 439 440 /** 441 * Returns the string necessary for grammatically correct pluralization 442 * of the given resource ID for the given quantity. 443 * Note that the string is selected based solely on grammatical necessity, 444 * and that such rules differ between languages. Do not assume you know which string 445 * will be returned for a given quantity. See 446 * <a href="{@docRoot}guide/topics/resources/string-resource.html#Plurals">String Resources</a> 447 * for more detail. 448 * 449 * @param id The desired resource identifier, as generated by the aapt 450 * tool. This integer encodes the package, type, and resource 451 * entry. The value 0 is an invalid identifier. 452 * @param quantity The number used to get the correct string for the current language's 453 * plural rules. 454 * 455 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 456 * 457 * @return String The string data associated with the resource, 458 * stripped of styled text information. 459 */ 460 @NonNull 461 public String getQuantityString(@PluralsRes int id, int quantity) throws NotFoundException { 462 return getQuantityText(id, quantity).toString(); 463 } 464 465 /** 466 * Return the string value associated with a particular resource ID. The 467 * returned object will be a String if this is a plain string; it will be 468 * some other type of CharSequence if it is styled. 469 * 470 * @param id The desired resource identifier, as generated by the aapt 471 * tool. This integer encodes the package, type, and resource 472 * entry. The value 0 is an invalid identifier. 473 * 474 * @param def The default CharSequence to return. 475 * 476 * @return CharSequence The string data associated with the resource, plus 477 * possibly styled text information, or def if id is 0 or not found. 478 */ 479 public CharSequence getText(@StringRes int id, CharSequence def) { 480 CharSequence res = id != 0 ? mResourcesImpl.getAssets().getResourceText(id) : null; 481 return res != null ? res : def; 482 } 483 484 /** 485 * Return the styled text array associated with a particular resource ID. 486 * 487 * @param id The desired resource identifier, as generated by the aapt 488 * tool. This integer encodes the package, type, and resource 489 * entry. The value 0 is an invalid identifier. 490 * 491 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 492 * 493 * @return The styled text array associated with the resource. 494 */ 495 @NonNull 496 public CharSequence[] getTextArray(@ArrayRes int id) throws NotFoundException { 497 CharSequence[] res = mResourcesImpl.getAssets().getResourceTextArray(id); 498 if (res != null) { 499 return res; 500 } 501 throw new NotFoundException("Text array resource ID #0x" + Integer.toHexString(id)); 502 } 503 504 /** 505 * Return the string array associated with a particular resource ID. 506 * 507 * @param id The desired resource identifier, as generated by the aapt 508 * tool. This integer encodes the package, type, and resource 509 * entry. The value 0 is an invalid identifier. 510 * 511 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 512 * 513 * @return The string array associated with the resource. 514 */ 515 @NonNull 516 public String[] getStringArray(@ArrayRes int id) 517 throws NotFoundException { 518 String[] res = mResourcesImpl.getAssets().getResourceStringArray(id); 519 if (res != null) { 520 return res; 521 } 522 throw new NotFoundException("String array resource ID #0x" + Integer.toHexString(id)); 523 } 524 525 /** 526 * Return the int array associated with a particular resource ID. 527 * 528 * @param id The desired resource identifier, as generated by the aapt 529 * tool. This integer encodes the package, type, and resource 530 * entry. The value 0 is an invalid identifier. 531 * 532 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 533 * 534 * @return The int array associated with the resource. 535 */ 536 @NonNull 537 public int[] getIntArray(@ArrayRes int id) throws NotFoundException { 538 int[] res = mResourcesImpl.getAssets().getArrayIntResource(id); 539 if (res != null) { 540 return res; 541 } 542 throw new NotFoundException("Int array resource ID #0x" + Integer.toHexString(id)); 543 } 544 545 /** 546 * Return an array of heterogeneous values. 547 * 548 * @param id The desired resource identifier, as generated by the aapt 549 * tool. This integer encodes the package, type, and resource 550 * entry. The value 0 is an invalid identifier. 551 * 552 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 553 * 554 * @return Returns a TypedArray holding an array of the array values. 555 * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} 556 * when done with it. 557 */ 558 @NonNull 559 public TypedArray obtainTypedArray(@ArrayRes int id) throws NotFoundException { 560 final ResourcesImpl impl = mResourcesImpl; 561 int len = impl.getAssets().getArraySize(id); 562 if (len < 0) { 563 throw new NotFoundException("Array resource ID #0x" + Integer.toHexString(id)); 564 } 565 566 TypedArray array = TypedArray.obtain(this, len); 567 array.mLength = impl.getAssets().retrieveArray(id, array.mData); 568 array.mIndices[0] = 0; 569 570 return array; 571 } 572 573 /** 574 * Retrieve a dimensional for a particular resource ID. Unit 575 * conversions are based on the current {@link DisplayMetrics} associated 576 * with the resources. 577 * 578 * @param id The desired resource identifier, as generated by the aapt 579 * tool. This integer encodes the package, type, and resource 580 * entry. The value 0 is an invalid identifier. 581 * 582 * @return Resource dimension value multiplied by the appropriate 583 * metric. 584 * 585 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 586 * 587 * @see #getDimensionPixelOffset 588 * @see #getDimensionPixelSize 589 */ 590 public float getDimension(@DimenRes int id) throws NotFoundException { 591 final TypedValue value = obtainTempTypedValue(); 592 try { 593 final ResourcesImpl impl = mResourcesImpl; 594 impl.getValue(id, value, true); 595 if (value.type == TypedValue.TYPE_DIMENSION) { 596 return TypedValue.complexToDimension(value.data, impl.getDisplayMetrics()); 597 } 598 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 599 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 600 } finally { 601 releaseTempTypedValue(value); 602 } 603 } 604 605 /** 606 * Retrieve a dimensional for a particular resource ID for use 607 * as an offset in raw pixels. This is the same as 608 * {@link #getDimension}, except the returned value is converted to 609 * integer pixels for you. An offset conversion involves simply 610 * truncating the base value to an integer. 611 * 612 * @param id The desired resource identifier, as generated by the aapt 613 * tool. This integer encodes the package, type, and resource 614 * entry. The value 0 is an invalid identifier. 615 * 616 * @return Resource dimension value multiplied by the appropriate 617 * metric and truncated to integer pixels. 618 * 619 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 620 * 621 * @see #getDimension 622 * @see #getDimensionPixelSize 623 */ 624 public int getDimensionPixelOffset(@DimenRes int id) throws NotFoundException { 625 final TypedValue value = obtainTempTypedValue(); 626 try { 627 final ResourcesImpl impl = mResourcesImpl; 628 impl.getValue(id, value, true); 629 if (value.type == TypedValue.TYPE_DIMENSION) { 630 return TypedValue.complexToDimensionPixelOffset(value.data, 631 impl.getDisplayMetrics()); 632 } 633 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 634 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 635 } finally { 636 releaseTempTypedValue(value); 637 } 638 } 639 640 /** 641 * Retrieve a dimensional for a particular resource ID for use 642 * as a size in raw pixels. This is the same as 643 * {@link #getDimension}, except the returned value is converted to 644 * integer pixels for use as a size. A size conversion involves 645 * rounding the base value, and ensuring that a non-zero base value 646 * is at least one pixel in size. 647 * 648 * @param id The desired resource identifier, as generated by the aapt 649 * tool. This integer encodes the package, type, and resource 650 * entry. The value 0 is an invalid identifier. 651 * 652 * @return Resource dimension value multiplied by the appropriate 653 * metric and truncated to integer pixels. 654 * 655 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 656 * 657 * @see #getDimension 658 * @see #getDimensionPixelOffset 659 */ 660 public int getDimensionPixelSize(@DimenRes int id) throws NotFoundException { 661 final TypedValue value = obtainTempTypedValue(); 662 try { 663 final ResourcesImpl impl = mResourcesImpl; 664 impl.getValue(id, value, true); 665 if (value.type == TypedValue.TYPE_DIMENSION) { 666 return TypedValue.complexToDimensionPixelSize(value.data, impl.getDisplayMetrics()); 667 } 668 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 669 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 670 } finally { 671 releaseTempTypedValue(value); 672 } 673 } 674 675 /** 676 * Retrieve a fractional unit for a particular resource ID. 677 * 678 * @param id The desired resource identifier, as generated by the aapt 679 * tool. This integer encodes the package, type, and resource 680 * entry. The value 0 is an invalid identifier. 681 * @param base The base value of this fraction. In other words, a 682 * standard fraction is multiplied by this value. 683 * @param pbase The parent base value of this fraction. In other 684 * words, a parent fraction (nn%p) is multiplied by this 685 * value. 686 * 687 * @return Attribute fractional value multiplied by the appropriate 688 * base value. 689 * 690 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 691 */ 692 public float getFraction(@FractionRes int id, int base, int pbase) { 693 final TypedValue value = obtainTempTypedValue(); 694 try { 695 mResourcesImpl.getValue(id, value, true); 696 if (value.type == TypedValue.TYPE_FRACTION) { 697 return TypedValue.complexToFraction(value.data, base, pbase); 698 } 699 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 700 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 701 } finally { 702 releaseTempTypedValue(value); 703 } 704 } 705 706 /** 707 * Return a drawable object associated with a particular resource ID. 708 * Various types of objects will be returned depending on the underlying 709 * resource -- for example, a solid color, PNG image, scalable image, etc. 710 * The Drawable API hides these implementation details. 711 * 712 * <p class="note"><strong>Note:</strong> Prior to 713 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN}, this function 714 * would not correctly retrieve the final configuration density when 715 * the resource ID passed here is an alias to another Drawable resource. 716 * This means that if the density configuration of the alias resource 717 * is different than the actual resource, the density of the returned 718 * Drawable would be incorrect, resulting in bad scaling. To work 719 * around this, you can instead manually resolve the aliased reference 720 * by using {@link #getValue(int, TypedValue, boolean)} and passing 721 * {@code true} for {@code resolveRefs}. The resulting 722 * {@link TypedValue#resourceId} value may be passed to this method.</p> 723 * 724 * <p class="note"><strong>Note:</strong> To obtain a themed drawable, use 725 * {@link android.content.Context#getDrawable(int) Context.getDrawable(int)} 726 * or {@link #getDrawable(int, Theme)} passing the desired theme.</p> 727 * 728 * @param id The desired resource identifier, as generated by the aapt 729 * tool. This integer encodes the package, type, and resource 730 * entry. The value 0 is an invalid identifier. 731 * @return Drawable An object that can be used to draw this resource. 732 * @throws NotFoundException Throws NotFoundException if the given ID does 733 * not exist. 734 * @see #getDrawable(int, Theme) 735 * @deprecated Use {@link #getDrawable(int, Theme)} instead. 736 */ 737 @Deprecated 738 public Drawable getDrawable(@DrawableRes int id) throws NotFoundException { 739 final Drawable d = getDrawable(id, null); 740 if (d != null && d.canApplyTheme()) { 741 Log.w(TAG, "Drawable " + getResourceName(id) + " has unresolved theme " 742 + "attributes! Consider using Resources.getDrawable(int, Theme) or " 743 + "Context.getDrawable(int).", new RuntimeException()); 744 } 745 return d; 746 } 747 748 /** 749 * Return a drawable object associated with a particular resource ID and 750 * styled for the specified theme. Various types of objects will be 751 * returned depending on the underlying resource -- for example, a solid 752 * color, PNG image, scalable image, etc. 753 * 754 * @param id The desired resource identifier, as generated by the aapt 755 * tool. This integer encodes the package, type, and resource 756 * entry. The value 0 is an invalid identifier. 757 * @param theme The theme used to style the drawable attributes, may be {@code null}. 758 * @return Drawable An object that can be used to draw this resource. 759 * @throws NotFoundException Throws NotFoundException if the given ID does 760 * not exist. 761 */ 762 public Drawable getDrawable(@DrawableRes int id, @Nullable Theme theme) 763 throws NotFoundException { 764 final TypedValue value = obtainTempTypedValue(); 765 try { 766 final ResourcesImpl impl = mResourcesImpl; 767 impl.getValue(id, value, true); 768 return impl.loadDrawable(this, value, id, theme, true); 769 } finally { 770 releaseTempTypedValue(value); 771 } 772 } 773 774 /** 775 * Return a drawable object associated with a particular resource ID for the 776 * given screen density in DPI. This will set the drawable's density to be 777 * the device's density multiplied by the ratio of actual drawable density 778 * to requested density. This allows the drawable to be scaled up to the 779 * correct size if needed. Various types of objects will be returned 780 * depending on the underlying resource -- for example, a solid color, PNG 781 * image, scalable image, etc. The Drawable API hides these implementation 782 * details. 783 * 784 * <p class="note"><strong>Note:</strong> To obtain a themed drawable, use 785 * {@link android.content.Context#getDrawable(int) Context.getDrawable(int)} 786 * or {@link #getDrawableForDensity(int, int, Theme)} passing the desired 787 * theme.</p> 788 * 789 * @param id The desired resource identifier, as generated by the aapt tool. 790 * This integer encodes the package, type, and resource entry. 791 * The value 0 is an invalid identifier. 792 * @param density the desired screen density indicated by the resource as 793 * found in {@link DisplayMetrics}. 794 * @return Drawable An object that can be used to draw this resource. 795 * @throws NotFoundException Throws NotFoundException if the given ID does 796 * not exist. 797 * @see #getDrawableForDensity(int, int, Theme) 798 * @deprecated Use {@link #getDrawableForDensity(int, int, Theme)} instead. 799 */ 800 @Deprecated 801 public Drawable getDrawableForDensity(@DrawableRes int id, int density) 802 throws NotFoundException { 803 return getDrawableForDensity(id, density, null); 804 } 805 806 /** 807 * Return a drawable object associated with a particular resource ID for the 808 * given screen density in DPI and styled for the specified theme. 809 * 810 * @param id The desired resource identifier, as generated by the aapt tool. 811 * This integer encodes the package, type, and resource entry. 812 * The value 0 is an invalid identifier. 813 * @param density The desired screen density indicated by the resource as 814 * found in {@link DisplayMetrics}. 815 * @param theme The theme used to style the drawable attributes, may be {@code null}. 816 * @return Drawable An object that can be used to draw this resource. 817 * @throws NotFoundException Throws NotFoundException if the given ID does 818 * not exist. 819 */ 820 public Drawable getDrawableForDensity(@DrawableRes int id, int density, @Nullable Theme theme) { 821 final TypedValue value = obtainTempTypedValue(); 822 try { 823 final ResourcesImpl impl = mResourcesImpl; 824 impl.getValueForDensity(id, density, value, true); 825 826 // If the drawable's XML lives in our current density qualifier, 827 // it's okay to use a scaled version from the cache. Otherwise, we 828 // need to actually load the drawable from XML. 829 final DisplayMetrics metrics = impl.getDisplayMetrics(); 830 final boolean useCache = value.density == metrics.densityDpi; 831 832 /* 833 * Pretend the requested density is actually the display density. If 834 * the drawable returned is not the requested density, then force it 835 * to be scaled later by dividing its density by the ratio of 836 * requested density to actual device density. Drawables that have 837 * undefined density or no density don't need to be handled here. 838 */ 839 if (value.density > 0 && value.density != TypedValue.DENSITY_NONE) { 840 if (value.density == density) { 841 value.density = metrics.densityDpi; 842 } else { 843 value.density = (value.density * metrics.densityDpi) / density; 844 } 845 } 846 return impl.loadDrawable(this, value, id, theme, useCache); 847 } finally { 848 releaseTempTypedValue(value); 849 } 850 } 851 852 @NonNull 853 Drawable loadDrawable(@NonNull TypedValue value, int id, @Nullable Theme theme) 854 throws NotFoundException { 855 return mResourcesImpl.loadDrawable(this, value, id, theme, true); 856 } 857 858 /** 859 * Return a movie object associated with the particular resource ID. 860 * @param id The desired resource identifier, as generated by the aapt 861 * tool. This integer encodes the package, type, and resource 862 * entry. The value 0 is an invalid identifier. 863 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 864 * 865 */ 866 public Movie getMovie(@RawRes int id) throws NotFoundException { 867 final InputStream is = openRawResource(id); 868 final Movie movie = Movie.decodeStream(is); 869 try { 870 is.close(); 871 } catch (IOException e) { 872 // No one cares. 873 } 874 return movie; 875 } 876 877 /** 878 * Returns a color integer associated with a particular resource ID. If the 879 * resource holds a complex {@link ColorStateList}, then the default color 880 * from the set is returned. 881 * 882 * @param id The desired resource identifier, as generated by the aapt 883 * tool. This integer encodes the package, type, and resource 884 * entry. The value 0 is an invalid identifier. 885 * 886 * @throws NotFoundException Throws NotFoundException if the given ID does 887 * not exist. 888 * 889 * @return A single color value in the form 0xAARRGGBB. 890 * @deprecated Use {@link #getColor(int, Theme)} instead. 891 */ 892 @ColorInt 893 @Deprecated 894 public int getColor(@ColorRes int id) throws NotFoundException { 895 return getColor(id, null); 896 } 897 898 /** 899 * Returns a themed color integer associated with a particular resource ID. 900 * If the resource holds a complex {@link ColorStateList}, then the default 901 * color from the set is returned. 902 * 903 * @param id The desired resource identifier, as generated by the aapt 904 * tool. This integer encodes the package, type, and resource 905 * entry. The value 0 is an invalid identifier. 906 * @param theme The theme used to style the color attributes, may be 907 * {@code null}. 908 * 909 * @throws NotFoundException Throws NotFoundException if the given ID does 910 * not exist. 911 * 912 * @return A single color value in the form 0xAARRGGBB. 913 */ 914 @ColorInt 915 public int getColor(@ColorRes int id, @Nullable Theme theme) throws NotFoundException { 916 final TypedValue value = obtainTempTypedValue(); 917 try { 918 final ResourcesImpl impl = mResourcesImpl; 919 impl.getValue(id, value, true); 920 if (value.type >= TypedValue.TYPE_FIRST_INT 921 && value.type <= TypedValue.TYPE_LAST_INT) { 922 return value.data; 923 } else if (value.type != TypedValue.TYPE_STRING) { 924 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 925 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 926 } 927 928 final ColorStateList csl = impl.loadColorStateList(this, value, id, theme); 929 return csl.getDefaultColor(); 930 } finally { 931 releaseTempTypedValue(value); 932 } 933 } 934 935 /** 936 * Returns a color state list associated with a particular resource ID. The 937 * resource may contain either a single raw color value or a complex 938 * {@link ColorStateList} holding multiple possible colors. 939 * 940 * @param id The desired resource identifier of a {@link ColorStateList}, 941 * as generated by the aapt tool. This integer encodes the 942 * package, type, and resource entry. The value 0 is an invalid 943 * identifier. 944 * 945 * @throws NotFoundException Throws NotFoundException if the given ID does 946 * not exist. 947 * 948 * @return A ColorStateList object containing either a single solid color 949 * or multiple colors that can be selected based on a state. 950 * @deprecated Use {@link #getColorStateList(int, Theme)} instead. 951 */ 952 @Nullable 953 @Deprecated 954 public ColorStateList getColorStateList(@ColorRes int id) throws NotFoundException { 955 final ColorStateList csl = getColorStateList(id, null); 956 if (csl != null && csl.canApplyTheme()) { 957 Log.w(TAG, "ColorStateList " + getResourceName(id) + " has " 958 + "unresolved theme attributes! Consider using " 959 + "Resources.getColorStateList(int, Theme) or " 960 + "Context.getColorStateList(int).", new RuntimeException()); 961 } 962 return csl; 963 } 964 965 /** 966 * Returns a themed color state list associated with a particular resource 967 * ID. The resource may contain either a single raw color value or a 968 * complex {@link ColorStateList} holding multiple possible colors. 969 * 970 * @param id The desired resource identifier of a {@link ColorStateList}, 971 * as generated by the aapt tool. This integer encodes the 972 * package, type, and resource entry. The value 0 is an invalid 973 * identifier. 974 * @param theme The theme used to style the color attributes, may be 975 * {@code null}. 976 * 977 * @throws NotFoundException Throws NotFoundException if the given ID does 978 * not exist. 979 * 980 * @return A themed ColorStateList object containing either a single solid 981 * color or multiple colors that can be selected based on a state. 982 */ 983 @Nullable 984 public ColorStateList getColorStateList(@ColorRes int id, @Nullable Theme theme) 985 throws NotFoundException { 986 final TypedValue value = obtainTempTypedValue(); 987 try { 988 final ResourcesImpl impl = mResourcesImpl; 989 impl.getValue(id, value, true); 990 return impl.loadColorStateList(this, value, id, theme); 991 } finally { 992 releaseTempTypedValue(value); 993 } 994 } 995 996 @Nullable 997 ColorStateList loadColorStateList(@NonNull TypedValue value, int id, @Nullable Theme theme) 998 throws NotFoundException { 999 return mResourcesImpl.loadColorStateList(this, value, id, theme); 1000 } 1001 1002 /** 1003 * @hide 1004 */ 1005 @Nullable 1006 public ComplexColor loadComplexColor(@NonNull TypedValue value, int id, @Nullable Theme theme) { 1007 return mResourcesImpl.loadComplexColor(this, value, id, theme); 1008 } 1009 1010 /** 1011 * Return a boolean associated with a particular resource ID. This can be 1012 * used with any integral resource value, and will return true if it is 1013 * non-zero. 1014 * 1015 * @param id The desired resource identifier, as generated by the aapt 1016 * tool. This integer encodes the package, type, and resource 1017 * entry. The value 0 is an invalid identifier. 1018 * 1019 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1020 * 1021 * @return Returns the boolean value contained in the resource. 1022 */ 1023 public boolean getBoolean(@BoolRes int id) throws NotFoundException { 1024 final TypedValue value = obtainTempTypedValue(); 1025 try { 1026 mResourcesImpl.getValue(id, value, true); 1027 if (value.type >= TypedValue.TYPE_FIRST_INT 1028 && value.type <= TypedValue.TYPE_LAST_INT) { 1029 return value.data != 0; 1030 } 1031 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 1032 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 1033 } finally { 1034 releaseTempTypedValue(value); 1035 } 1036 } 1037 1038 /** 1039 * Return an integer associated with a particular resource ID. 1040 * 1041 * @param id The desired resource identifier, as generated by the aapt 1042 * tool. This integer encodes the package, type, and resource 1043 * entry. The value 0 is an invalid identifier. 1044 * 1045 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1046 * 1047 * @return Returns the integer value contained in the resource. 1048 */ 1049 public int getInteger(@IntegerRes int id) throws NotFoundException { 1050 final TypedValue value = obtainTempTypedValue(); 1051 try { 1052 mResourcesImpl.getValue(id, value, true); 1053 if (value.type >= TypedValue.TYPE_FIRST_INT 1054 && value.type <= TypedValue.TYPE_LAST_INT) { 1055 return value.data; 1056 } 1057 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 1058 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 1059 } finally { 1060 releaseTempTypedValue(value); 1061 } 1062 } 1063 1064 /** 1065 * Retrieve a floating-point value for a particular resource ID. 1066 * 1067 * @param id The desired resource identifier, as generated by the aapt 1068 * tool. This integer encodes the package, type, and resource 1069 * entry. The value 0 is an invalid identifier. 1070 * 1071 * @return Returns the floating-point value contained in the resource. 1072 * 1073 * @throws NotFoundException Throws NotFoundException if the given ID does 1074 * not exist or is not a floating-point value. 1075 * @hide Pending API council approval. 1076 */ 1077 public float getFloat(int id) { 1078 final TypedValue value = obtainTempTypedValue(); 1079 try { 1080 mResourcesImpl.getValue(id, value, true); 1081 if (value.type == TypedValue.TYPE_FLOAT) { 1082 return value.getFloat(); 1083 } 1084 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 1085 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 1086 } finally { 1087 releaseTempTypedValue(value); 1088 } 1089 } 1090 1091 /** 1092 * Return an XmlResourceParser through which you can read a view layout 1093 * description for the given resource ID. This parser has limited 1094 * functionality -- in particular, you can't change its input, and only 1095 * the high-level events are available. 1096 * 1097 * <p>This function is really a simple wrapper for calling 1098 * {@link #getXml} with a layout resource. 1099 * 1100 * @param id The desired resource identifier, as generated by the aapt 1101 * tool. This integer encodes the package, type, and resource 1102 * entry. The value 0 is an invalid identifier. 1103 * 1104 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1105 * 1106 * @return A new parser object through which you can read 1107 * the XML data. 1108 * 1109 * @see #getXml 1110 */ 1111 public XmlResourceParser getLayout(@LayoutRes int id) throws NotFoundException { 1112 return loadXmlResourceParser(id, "layout"); 1113 } 1114 1115 /** 1116 * Return an XmlResourceParser through which you can read an animation 1117 * description for the given resource ID. This parser has limited 1118 * functionality -- in particular, you can't change its input, and only 1119 * the high-level events are available. 1120 * 1121 * <p>This function is really a simple wrapper for calling 1122 * {@link #getXml} with an animation resource. 1123 * 1124 * @param id The desired resource identifier, as generated by the aapt 1125 * tool. This integer encodes the package, type, and resource 1126 * entry. The value 0 is an invalid identifier. 1127 * 1128 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1129 * 1130 * @return A new parser object through which you can read 1131 * the XML data. 1132 * 1133 * @see #getXml 1134 */ 1135 public XmlResourceParser getAnimation(@AnimRes int id) throws NotFoundException { 1136 return loadXmlResourceParser(id, "anim"); 1137 } 1138 1139 /** 1140 * Return an XmlResourceParser through which you can read a generic XML 1141 * resource for the given resource ID. 1142 * 1143 * <p>The XmlPullParser implementation returned here has some limited 1144 * functionality. In particular, you can't change its input, and only 1145 * high-level parsing events are available (since the document was 1146 * pre-parsed for you at build time, which involved merging text and 1147 * stripping comments). 1148 * 1149 * @param id The desired resource identifier, as generated by the aapt 1150 * tool. This integer encodes the package, type, and resource 1151 * entry. The value 0 is an invalid identifier. 1152 * 1153 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1154 * 1155 * @return A new parser object through which you can read 1156 * the XML data. 1157 * 1158 * @see android.util.AttributeSet 1159 */ 1160 public XmlResourceParser getXml(@XmlRes int id) throws NotFoundException { 1161 return loadXmlResourceParser(id, "xml"); 1162 } 1163 1164 /** 1165 * Open a data stream for reading a raw resource. This can only be used 1166 * with resources whose value is the name of an asset files -- that is, it can be 1167 * used to open drawable, sound, and raw resources; it will fail on string 1168 * and color resources. 1169 * 1170 * @param id The resource identifier to open, as generated by the appt 1171 * tool. 1172 * 1173 * @return InputStream Access to the resource data. 1174 * 1175 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1176 * 1177 */ 1178 public InputStream openRawResource(@RawRes int id) throws NotFoundException { 1179 final TypedValue value = obtainTempTypedValue(); 1180 try { 1181 return openRawResource(id, value); 1182 } finally { 1183 releaseTempTypedValue(value); 1184 } 1185 } 1186 1187 /** 1188 * Returns a TypedValue suitable for temporary use. The obtained TypedValue 1189 * should be released using {@link #releaseTempTypedValue(TypedValue)}. 1190 * 1191 * @return a typed value suitable for temporary use 1192 */ 1193 private TypedValue obtainTempTypedValue() { 1194 TypedValue tmpValue = null; 1195 synchronized (mTmpValueLock) { 1196 if (mTmpValue != null) { 1197 tmpValue = mTmpValue; 1198 mTmpValue = null; 1199 } 1200 } 1201 if (tmpValue == null) { 1202 return new TypedValue(); 1203 } 1204 return tmpValue; 1205 } 1206 1207 /** 1208 * Returns a TypedValue to the pool. After calling this method, the 1209 * specified TypedValue should no longer be accessed. 1210 * 1211 * @param value the typed value to return to the pool 1212 */ 1213 private void releaseTempTypedValue(TypedValue value) { 1214 synchronized (mTmpValueLock) { 1215 if (mTmpValue == null) { 1216 mTmpValue = value; 1217 } 1218 } 1219 } 1220 1221 /** 1222 * Open a data stream for reading a raw resource. This can only be used 1223 * with resources whose value is the name of an asset file -- that is, it can be 1224 * used to open drawable, sound, and raw resources; it will fail on string 1225 * and color resources. 1226 * 1227 * @param id The resource identifier to open, as generated by the appt tool. 1228 * @param value The TypedValue object to hold the resource information. 1229 * 1230 * @return InputStream Access to the resource data. 1231 * 1232 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1233 */ 1234 public InputStream openRawResource(@RawRes int id, TypedValue value) 1235 throws NotFoundException { 1236 return mResourcesImpl.openRawResource(id, value); 1237 } 1238 1239 /** 1240 * Open a file descriptor for reading a raw resource. This can only be used 1241 * with resources whose value is the name of an asset files -- that is, it can be 1242 * used to open drawable, sound, and raw resources; it will fail on string 1243 * and color resources. 1244 * 1245 * <p>This function only works for resources that are stored in the package 1246 * as uncompressed data, which typically includes things like mp3 files 1247 * and png images. 1248 * 1249 * @param id The resource identifier to open, as generated by the appt 1250 * tool. 1251 * 1252 * @return AssetFileDescriptor A new file descriptor you can use to read 1253 * the resource. This includes the file descriptor itself, as well as the 1254 * offset and length of data where the resource appears in the file. A 1255 * null is returned if the file exists but is compressed. 1256 * 1257 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1258 * 1259 */ 1260 public AssetFileDescriptor openRawResourceFd(@RawRes int id) 1261 throws NotFoundException { 1262 final TypedValue value = obtainTempTypedValue(); 1263 try { 1264 return mResourcesImpl.openRawResourceFd(id, value); 1265 } finally { 1266 releaseTempTypedValue(value); 1267 } 1268 } 1269 1270 /** 1271 * Return the raw data associated with a particular resource ID. 1272 * 1273 * @param id The desired resource identifier, as generated by the aapt 1274 * tool. This integer encodes the package, type, and resource 1275 * entry. The value 0 is an invalid identifier. 1276 * @param outValue Object in which to place the resource data. 1277 * @param resolveRefs If true, a resource that is a reference to another 1278 * resource will be followed so that you receive the 1279 * actual final resource data. If false, the TypedValue 1280 * will be filled in with the reference itself. 1281 * 1282 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1283 * 1284 */ 1285 public void getValue(@AnyRes int id, TypedValue outValue, boolean resolveRefs) 1286 throws NotFoundException { 1287 mResourcesImpl.getValue(id, outValue, resolveRefs); 1288 } 1289 1290 /** 1291 * Get the raw value associated with a resource with associated density. 1292 * 1293 * @param id resource identifier 1294 * @param density density in DPI 1295 * @param resolveRefs If true, a resource that is a reference to another 1296 * resource will be followed so that you receive the actual final 1297 * resource data. If false, the TypedValue will be filled in with 1298 * the reference itself. 1299 * @throws NotFoundException Throws NotFoundException if the given ID does 1300 * not exist. 1301 * @see #getValue(String, TypedValue, boolean) 1302 */ 1303 public void getValueForDensity(@AnyRes int id, int density, TypedValue outValue, 1304 boolean resolveRefs) throws NotFoundException { 1305 mResourcesImpl.getValueForDensity(id, density, outValue, resolveRefs); 1306 } 1307 1308 /** 1309 * Return the raw data associated with a particular resource ID. 1310 * See getIdentifier() for information on how names are mapped to resource 1311 * IDs, and getString(int) for information on how string resources are 1312 * retrieved. 1313 * 1314 * <p>Note: use of this function is discouraged. It is much more 1315 * efficient to retrieve resources by identifier than by name. 1316 * 1317 * @param name The name of the desired resource. This is passed to 1318 * getIdentifier() with a default type of "string". 1319 * @param outValue Object in which to place the resource data. 1320 * @param resolveRefs If true, a resource that is a reference to another 1321 * resource will be followed so that you receive the 1322 * actual final resource data. If false, the TypedValue 1323 * will be filled in with the reference itself. 1324 * 1325 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1326 * 1327 */ 1328 public void getValue(String name, TypedValue outValue, boolean resolveRefs) 1329 throws NotFoundException { 1330 mResourcesImpl.getValue(name, outValue, resolveRefs); 1331 } 1332 1333 /** 1334 * This class holds the current attribute values for a particular theme. 1335 * In other words, a Theme is a set of values for resource attributes; 1336 * these are used in conjunction with {@link TypedArray} 1337 * to resolve the final value for an attribute. 1338 * 1339 * <p>The Theme's attributes come into play in two ways: (1) a styled 1340 * attribute can explicit reference a value in the theme through the 1341 * "?themeAttribute" syntax; (2) if no value has been defined for a 1342 * particular styled attribute, as a last resort we will try to find that 1343 * attribute's value in the Theme. 1344 * 1345 * <p>You will normally use the {@link #obtainStyledAttributes} APIs to 1346 * retrieve XML attributes with style and theme information applied. 1347 */ 1348 public final class Theme { 1349 private ResourcesImpl.ThemeImpl mThemeImpl; 1350 1351 private Theme() { 1352 } 1353 1354 void setImpl(ResourcesImpl.ThemeImpl impl) { 1355 mThemeImpl = impl; 1356 } 1357 1358 /** 1359 * Place new attribute values into the theme. The style resource 1360 * specified by <var>resid</var> will be retrieved from this Theme's 1361 * resources, its values placed into the Theme object. 1362 * 1363 * <p>The semantics of this function depends on the <var>force</var> 1364 * argument: If false, only values that are not already defined in 1365 * the theme will be copied from the system resource; otherwise, if 1366 * any of the style's attributes are already defined in the theme, the 1367 * current values in the theme will be overwritten. 1368 * 1369 * @param resId The resource ID of a style resource from which to 1370 * obtain attribute values. 1371 * @param force If true, values in the style resource will always be 1372 * used in the theme; otherwise, they will only be used 1373 * if not already defined in the theme. 1374 */ 1375 public void applyStyle(int resId, boolean force) { 1376 mThemeImpl.applyStyle(resId, force); 1377 } 1378 1379 /** 1380 * Set this theme to hold the same contents as the theme 1381 * <var>other</var>. If both of these themes are from the same 1382 * Resources object, they will be identical after this function 1383 * returns. If they are from different Resources, only the resources 1384 * they have in common will be set in this theme. 1385 * 1386 * @param other The existing Theme to copy from. 1387 */ 1388 public void setTo(Theme other) { 1389 mThemeImpl.setTo(other.mThemeImpl); 1390 } 1391 1392 /** 1393 * Return a TypedArray holding the values defined by 1394 * <var>Theme</var> which are listed in <var>attrs</var>. 1395 * 1396 * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done 1397 * with the array. 1398 * 1399 * @param attrs The desired attributes. 1400 * 1401 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1402 * 1403 * @return Returns a TypedArray holding an array of the attribute values. 1404 * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} 1405 * when done with it. 1406 * 1407 * @see Resources#obtainAttributes 1408 * @see #obtainStyledAttributes(int, int[]) 1409 * @see #obtainStyledAttributes(AttributeSet, int[], int, int) 1410 */ 1411 public TypedArray obtainStyledAttributes(@StyleableRes int[] attrs) { 1412 return mThemeImpl.obtainStyledAttributes(this, null, attrs, 0, 0); 1413 } 1414 1415 /** 1416 * Return a TypedArray holding the values defined by the style 1417 * resource <var>resid</var> which are listed in <var>attrs</var>. 1418 * 1419 * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done 1420 * with the array. 1421 * 1422 * @param resId The desired style resource. 1423 * @param attrs The desired attributes in the style. 1424 * 1425 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1426 * 1427 * @return Returns a TypedArray holding an array of the attribute values. 1428 * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} 1429 * when done with it. 1430 * 1431 * @see Resources#obtainAttributes 1432 * @see #obtainStyledAttributes(int[]) 1433 * @see #obtainStyledAttributes(AttributeSet, int[], int, int) 1434 */ 1435 public TypedArray obtainStyledAttributes(@StyleRes int resId, @StyleableRes int[] attrs) 1436 throws NotFoundException { 1437 return mThemeImpl.obtainStyledAttributes(this, null, attrs, 0, resId); 1438 } 1439 1440 /** 1441 * Return a TypedArray holding the attribute values in 1442 * <var>set</var> 1443 * that are listed in <var>attrs</var>. In addition, if the given 1444 * AttributeSet specifies a style class (through the "style" attribute), 1445 * that style will be applied on top of the base attributes it defines. 1446 * 1447 * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done 1448 * with the array. 1449 * 1450 * <p>When determining the final value of a particular attribute, there 1451 * are four inputs that come into play:</p> 1452 * 1453 * <ol> 1454 * <li> Any attribute values in the given AttributeSet. 1455 * <li> The style resource specified in the AttributeSet (named 1456 * "style"). 1457 * <li> The default style specified by <var>defStyleAttr</var> and 1458 * <var>defStyleRes</var> 1459 * <li> The base values in this theme. 1460 * </ol> 1461 * 1462 * <p>Each of these inputs is considered in-order, with the first listed 1463 * taking precedence over the following ones. In other words, if in the 1464 * AttributeSet you have supplied <code><Button 1465 * textColor="#ff000000"></code>, then the button's text will 1466 * <em>always</em> be black, regardless of what is specified in any of 1467 * the styles. 1468 * 1469 * @param set The base set of attribute values. May be null. 1470 * @param attrs The desired attributes to be retrieved. 1471 * @param defStyleAttr An attribute in the current theme that contains a 1472 * reference to a style resource that supplies 1473 * defaults values for the TypedArray. Can be 1474 * 0 to not look for defaults. 1475 * @param defStyleRes A resource identifier of a style resource that 1476 * supplies default values for the TypedArray, 1477 * used only if defStyleAttr is 0 or can not be found 1478 * in the theme. Can be 0 to not look for defaults. 1479 * 1480 * @return Returns a TypedArray holding an array of the attribute values. 1481 * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} 1482 * when done with it. 1483 * 1484 * @see Resources#obtainAttributes 1485 * @see #obtainStyledAttributes(int[]) 1486 * @see #obtainStyledAttributes(int, int[]) 1487 */ 1488 public TypedArray obtainStyledAttributes(AttributeSet set, 1489 @StyleableRes int[] attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) { 1490 return mThemeImpl.obtainStyledAttributes(this, set, attrs, defStyleAttr, defStyleRes); 1491 } 1492 1493 /** 1494 * Retrieve the values for a set of attributes in the Theme. The 1495 * contents of the typed array are ultimately filled in by 1496 * {@link Resources#getValue}. 1497 * 1498 * @param values The base set of attribute values, must be equal in 1499 * length to {@code attrs}. All values must be of type 1500 * {@link TypedValue#TYPE_ATTRIBUTE}. 1501 * @param attrs The desired attributes to be retrieved. 1502 * @return Returns a TypedArray holding an array of the attribute 1503 * values. Be sure to call {@link TypedArray#recycle()} 1504 * when done with it. 1505 * @hide 1506 */ 1507 @NonNull 1508 public TypedArray resolveAttributes(@NonNull int[] values, @NonNull int[] attrs) { 1509 return mThemeImpl.resolveAttributes(this, values, attrs); 1510 } 1511 1512 /** 1513 * Retrieve the value of an attribute in the Theme. The contents of 1514 * <var>outValue</var> are ultimately filled in by 1515 * {@link Resources#getValue}. 1516 * 1517 * @param resid The resource identifier of the desired theme 1518 * attribute. 1519 * @param outValue Filled in with the ultimate resource value supplied 1520 * by the attribute. 1521 * @param resolveRefs If true, resource references will be walked; if 1522 * false, <var>outValue</var> may be a 1523 * TYPE_REFERENCE. In either case, it will never 1524 * be a TYPE_ATTRIBUTE. 1525 * 1526 * @return boolean Returns true if the attribute was found and 1527 * <var>outValue</var> is valid, else false. 1528 */ 1529 public boolean resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) { 1530 return mThemeImpl.resolveAttribute(resid, outValue, resolveRefs); 1531 } 1532 1533 /** 1534 * Gets all of the attribute ids associated with this {@link Theme}. For debugging only. 1535 * 1536 * @return The int array containing attribute ids associated with this {@link Theme}. 1537 * @hide 1538 */ 1539 public int[] getAllAttributes() { 1540 return mThemeImpl.getAllAttributes(); 1541 } 1542 1543 /** 1544 * Returns the resources to which this theme belongs. 1545 * 1546 * @return Resources to which this theme belongs. 1547 */ 1548 public Resources getResources() { 1549 return Resources.this; 1550 } 1551 1552 /** 1553 * Return a drawable object associated with a particular resource ID 1554 * and styled for the Theme. 1555 * 1556 * @param id The desired resource identifier, as generated by the aapt 1557 * tool. This integer encodes the package, type, and resource 1558 * entry. The value 0 is an invalid identifier. 1559 * @return Drawable An object that can be used to draw this resource. 1560 * @throws NotFoundException Throws NotFoundException if the given ID 1561 * does not exist. 1562 */ 1563 public Drawable getDrawable(@DrawableRes int id) throws NotFoundException { 1564 return Resources.this.getDrawable(id, this); 1565 } 1566 1567 /** 1568 * Returns a bit mask of configuration changes that will impact this 1569 * theme (and thus require completely reloading it). 1570 * 1571 * @return a bit mask of configuration changes, as defined by 1572 * {@link ActivityInfo} 1573 * @see ActivityInfo 1574 */ 1575 public int getChangingConfigurations() { 1576 return mThemeImpl.getChangingConfigurations(); 1577 } 1578 1579 /** 1580 * Print contents of this theme out to the log. For debugging only. 1581 * 1582 * @param priority The log priority to use. 1583 * @param tag The log tag to use. 1584 * @param prefix Text to prefix each line printed. 1585 */ 1586 public void dump(int priority, String tag, String prefix) { 1587 mThemeImpl.dump(priority, tag, prefix); 1588 } 1589 1590 // Needed by layoutlib. 1591 /*package*/ long getNativeTheme() { 1592 return mThemeImpl.getNativeTheme(); 1593 } 1594 1595 /*package*/ int getAppliedStyleResId() { 1596 return mThemeImpl.getAppliedStyleResId(); 1597 } 1598 1599 /** 1600 * @hide 1601 */ 1602 public ThemeKey getKey() { 1603 return mThemeImpl.getKey(); 1604 } 1605 1606 private String getResourceNameFromHexString(String hexString) { 1607 return getResourceName(Integer.parseInt(hexString, 16)); 1608 } 1609 1610 /** 1611 * Parses {@link #getKey()} and returns a String array that holds pairs of 1612 * adjacent Theme data: resource name followed by whether or not it was 1613 * forced, as specified by {@link #applyStyle(int, boolean)}. 1614 * 1615 * @hide 1616 */ 1617 @ViewDebug.ExportedProperty(category = "theme", hasAdjacentMapping = true) 1618 public String[] getTheme() { 1619 return mThemeImpl.getTheme(); 1620 } 1621 1622 /** @hide */ 1623 public void encode(@NonNull ViewHierarchyEncoder encoder) { 1624 encoder.beginObject(this); 1625 final String[] properties = getTheme(); 1626 for (int i = 0; i < properties.length; i += 2) { 1627 encoder.addProperty(properties[i], properties[i+1]); 1628 } 1629 encoder.endObject(); 1630 } 1631 1632 /** 1633 * Rebases the theme against the parent Resource object's current 1634 * configuration by re-applying the styles passed to 1635 * {@link #applyStyle(int, boolean)}. 1636 * 1637 * @hide 1638 */ 1639 public void rebase() { 1640 mThemeImpl.rebase(); 1641 } 1642 } 1643 1644 static class ThemeKey implements Cloneable { 1645 int[] mResId; 1646 boolean[] mForce; 1647 int mCount; 1648 1649 private int mHashCode = 0; 1650 1651 public void append(int resId, boolean force) { 1652 if (mResId == null) { 1653 mResId = new int[4]; 1654 } 1655 1656 if (mForce == null) { 1657 mForce = new boolean[4]; 1658 } 1659 1660 mResId = GrowingArrayUtils.append(mResId, mCount, resId); 1661 mForce = GrowingArrayUtils.append(mForce, mCount, force); 1662 mCount++; 1663 1664 mHashCode = 31 * (31 * mHashCode + resId) + (force ? 1 : 0); 1665 } 1666 1667 /** 1668 * Sets up this key as a deep copy of another key. 1669 * 1670 * @param other the key to deep copy into this key 1671 */ 1672 public void setTo(ThemeKey other) { 1673 mResId = other.mResId == null ? null : other.mResId.clone(); 1674 mForce = other.mForce == null ? null : other.mForce.clone(); 1675 mCount = other.mCount; 1676 } 1677 1678 @Override 1679 public int hashCode() { 1680 return mHashCode; 1681 } 1682 1683 @Override 1684 public boolean equals(Object o) { 1685 if (this == o) { 1686 return true; 1687 } 1688 1689 if (o == null || getClass() != o.getClass() || hashCode() != o.hashCode()) { 1690 return false; 1691 } 1692 1693 final ThemeKey t = (ThemeKey) o; 1694 if (mCount != t.mCount) { 1695 return false; 1696 } 1697 1698 final int N = mCount; 1699 for (int i = 0; i < N; i++) { 1700 if (mResId[i] != t.mResId[i] || mForce[i] != t.mForce[i]) { 1701 return false; 1702 } 1703 } 1704 1705 return true; 1706 } 1707 1708 /** 1709 * @return a shallow copy of this key 1710 */ 1711 @Override 1712 public ThemeKey clone() { 1713 final ThemeKey other = new ThemeKey(); 1714 other.mResId = mResId; 1715 other.mForce = mForce; 1716 other.mCount = mCount; 1717 other.mHashCode = mHashCode; 1718 return other; 1719 } 1720 } 1721 1722 /** 1723 * Generate a new Theme object for this set of Resources. It initially 1724 * starts out empty. 1725 * 1726 * @return Theme The newly created Theme container. 1727 */ 1728 public final Theme newTheme() { 1729 Theme theme = new Theme(); 1730 theme.setImpl(mResourcesImpl.newThemeImpl()); 1731 mThemeRefs.add(new WeakReference<>(theme)); 1732 return theme; 1733 } 1734 1735 /** 1736 * Retrieve a set of basic attribute values from an AttributeSet, not 1737 * performing styling of them using a theme and/or style resources. 1738 * 1739 * @param set The current attribute values to retrieve. 1740 * @param attrs The specific attributes to be retrieved. 1741 * @return Returns a TypedArray holding an array of the attribute values. 1742 * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} 1743 * when done with it. 1744 * 1745 * @see Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 1746 */ 1747 public TypedArray obtainAttributes(AttributeSet set, int[] attrs) { 1748 int len = attrs.length; 1749 TypedArray array = TypedArray.obtain(this, len); 1750 1751 // XXX note that for now we only work with compiled XML files. 1752 // To support generic XML files we will need to manually parse 1753 // out the attributes from the XML file (applying type information 1754 // contained in the resources and such). 1755 XmlBlock.Parser parser = (XmlBlock.Parser)set; 1756 mResourcesImpl.getAssets().retrieveAttributes(parser.mParseState, attrs, 1757 array.mData, array.mIndices); 1758 1759 array.mXml = parser; 1760 1761 return array; 1762 } 1763 1764 /** 1765 * Store the newly updated configuration. 1766 */ 1767 public void updateConfiguration(Configuration config, DisplayMetrics metrics) { 1768 updateConfiguration(config, metrics, null); 1769 } 1770 1771 /** 1772 * @hide 1773 */ 1774 public void updateConfiguration(Configuration config, DisplayMetrics metrics, 1775 CompatibilityInfo compat) { 1776 mResourcesImpl.updateConfiguration(config, metrics, compat); 1777 } 1778 1779 /** 1780 * Update the system resources configuration if they have previously 1781 * been initialized. 1782 * 1783 * @hide 1784 */ 1785 public static void updateSystemConfiguration(Configuration config, DisplayMetrics metrics, 1786 CompatibilityInfo compat) { 1787 if (mSystem != null) { 1788 mSystem.updateConfiguration(config, metrics, compat); 1789 //Log.i(TAG, "Updated system resources " + mSystem 1790 // + ": " + mSystem.getConfiguration()); 1791 } 1792 } 1793 1794 /** 1795 * Return the current display metrics that are in effect for this resource 1796 * object. The returned object should be treated as read-only. 1797 * 1798 * @return The resource's current display metrics. 1799 */ 1800 public DisplayMetrics getDisplayMetrics() { 1801 return mResourcesImpl.getDisplayMetrics(); 1802 } 1803 1804 /** @hide */ 1805 public DisplayAdjustments getDisplayAdjustments() { 1806 return mResourcesImpl.getDisplayAdjustments(); 1807 } 1808 1809 /** 1810 * Return the current configuration that is in effect for this resource 1811 * object. The returned object should be treated as read-only. 1812 * 1813 * @return The resource's current configuration. 1814 */ 1815 public Configuration getConfiguration() { 1816 return mResourcesImpl.getConfiguration(); 1817 } 1818 1819 /** @hide */ 1820 public Configuration[] getSizeConfigurations() { 1821 return mResourcesImpl.getSizeConfigurations(); 1822 } 1823 1824 /** 1825 * Return the compatibility mode information for the application. 1826 * The returned object should be treated as read-only. 1827 * 1828 * @return compatibility info. 1829 * @hide 1830 */ 1831 public CompatibilityInfo getCompatibilityInfo() { 1832 return mResourcesImpl.getCompatibilityInfo(); 1833 } 1834 1835 /** 1836 * This is just for testing. 1837 * @hide 1838 */ 1839 @VisibleForTesting 1840 public void setCompatibilityInfo(CompatibilityInfo ci) { 1841 if (ci != null) { 1842 mResourcesImpl.updateConfiguration(null, null, ci); 1843 } 1844 } 1845 1846 /** 1847 * Return a resource identifier for the given resource name. A fully 1848 * qualified resource name is of the form "package:type/entry". The first 1849 * two components (package and type) are optional if defType and 1850 * defPackage, respectively, are specified here. 1851 * 1852 * <p>Note: use of this function is discouraged. It is much more 1853 * efficient to retrieve resources by identifier than by name. 1854 * 1855 * @param name The name of the desired resource. 1856 * @param defType Optional default resource type to find, if "type/" is 1857 * not included in the name. Can be null to require an 1858 * explicit type. 1859 * @param defPackage Optional default package to find, if "package:" is 1860 * not included in the name. Can be null to require an 1861 * explicit package. 1862 * 1863 * @return int The associated resource identifier. Returns 0 if no such 1864 * resource was found. (0 is not a valid resource ID.) 1865 */ 1866 public int getIdentifier(String name, String defType, String defPackage) { 1867 return mResourcesImpl.getIdentifier(name, defType, defPackage); 1868 } 1869 1870 /** 1871 * Return true if given resource identifier includes a package. 1872 * 1873 * @hide 1874 */ 1875 public static boolean resourceHasPackage(@AnyRes int resid) { 1876 return (resid >>> 24) != 0; 1877 } 1878 1879 /** 1880 * Return the full name for a given resource identifier. This name is 1881 * a single string of the form "package:type/entry". 1882 * 1883 * @param resid The resource identifier whose name is to be retrieved. 1884 * 1885 * @return A string holding the name of the resource. 1886 * 1887 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1888 * 1889 * @see #getResourcePackageName 1890 * @see #getResourceTypeName 1891 * @see #getResourceEntryName 1892 */ 1893 public String getResourceName(@AnyRes int resid) throws NotFoundException { 1894 return mResourcesImpl.getResourceName(resid); 1895 } 1896 1897 /** 1898 * Return the package name for a given resource identifier. 1899 * 1900 * @param resid The resource identifier whose package name is to be 1901 * retrieved. 1902 * 1903 * @return A string holding the package name of the resource. 1904 * 1905 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1906 * 1907 * @see #getResourceName 1908 */ 1909 public String getResourcePackageName(@AnyRes int resid) throws NotFoundException { 1910 return mResourcesImpl.getResourcePackageName(resid); 1911 } 1912 1913 /** 1914 * Return the type name for a given resource identifier. 1915 * 1916 * @param resid The resource identifier whose type name is to be 1917 * retrieved. 1918 * 1919 * @return A string holding the type name of the resource. 1920 * 1921 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1922 * 1923 * @see #getResourceName 1924 */ 1925 public String getResourceTypeName(@AnyRes int resid) throws NotFoundException { 1926 return mResourcesImpl.getResourceTypeName(resid); 1927 } 1928 1929 /** 1930 * Return the entry name for a given resource identifier. 1931 * 1932 * @param resid The resource identifier whose entry name is to be 1933 * retrieved. 1934 * 1935 * @return A string holding the entry name of the resource. 1936 * 1937 * @throws NotFoundException Throws NotFoundException if the given ID does not exist. 1938 * 1939 * @see #getResourceName 1940 */ 1941 public String getResourceEntryName(@AnyRes int resid) throws NotFoundException { 1942 return mResourcesImpl.getResourceEntryName(resid); 1943 } 1944 1945 /** 1946 * Parse a series of {@link android.R.styleable#Extra <extra>} tags from 1947 * an XML file. You call this when you are at the parent tag of the 1948 * extra tags, and it will return once all of the child tags have been parsed. 1949 * This will call {@link #parseBundleExtra} for each extra tag encountered. 1950 * 1951 * @param parser The parser from which to retrieve the extras. 1952 * @param outBundle A Bundle in which to place all parsed extras. 1953 * @throws XmlPullParserException 1954 * @throws IOException 1955 */ 1956 public void parseBundleExtras(XmlResourceParser parser, Bundle outBundle) 1957 throws XmlPullParserException, IOException { 1958 int outerDepth = parser.getDepth(); 1959 int type; 1960 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 1961 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 1962 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 1963 continue; 1964 } 1965 1966 String nodeName = parser.getName(); 1967 if (nodeName.equals("extra")) { 1968 parseBundleExtra("extra", parser, outBundle); 1969 XmlUtils.skipCurrentTag(parser); 1970 1971 } else { 1972 XmlUtils.skipCurrentTag(parser); 1973 } 1974 } 1975 } 1976 1977 /** 1978 * Parse a name/value pair out of an XML tag holding that data. The 1979 * AttributeSet must be holding the data defined by 1980 * {@link android.R.styleable#Extra}. The following value types are supported: 1981 * <ul> 1982 * <li> {@link TypedValue#TYPE_STRING}: 1983 * {@link Bundle#putCharSequence Bundle.putCharSequence()} 1984 * <li> {@link TypedValue#TYPE_INT_BOOLEAN}: 1985 * {@link Bundle#putCharSequence Bundle.putBoolean()} 1986 * <li> {@link TypedValue#TYPE_FIRST_INT}-{@link TypedValue#TYPE_LAST_INT}: 1987 * {@link Bundle#putCharSequence Bundle.putBoolean()} 1988 * <li> {@link TypedValue#TYPE_FLOAT}: 1989 * {@link Bundle#putCharSequence Bundle.putFloat()} 1990 * </ul> 1991 * 1992 * @param tagName The name of the tag these attributes come from; this is 1993 * only used for reporting error messages. 1994 * @param attrs The attributes from which to retrieve the name/value pair. 1995 * @param outBundle The Bundle in which to place the parsed value. 1996 * @throws XmlPullParserException If the attributes are not valid. 1997 */ 1998 public void parseBundleExtra(String tagName, AttributeSet attrs, 1999 Bundle outBundle) throws XmlPullParserException { 2000 TypedArray sa = obtainAttributes(attrs, 2001 com.android.internal.R.styleable.Extra); 2002 2003 String name = sa.getString( 2004 com.android.internal.R.styleable.Extra_name); 2005 if (name == null) { 2006 sa.recycle(); 2007 throw new XmlPullParserException("<" + tagName 2008 + "> requires an android:name attribute at " 2009 + attrs.getPositionDescription()); 2010 } 2011 2012 TypedValue v = sa.peekValue( 2013 com.android.internal.R.styleable.Extra_value); 2014 if (v != null) { 2015 if (v.type == TypedValue.TYPE_STRING) { 2016 CharSequence cs = v.coerceToString(); 2017 outBundle.putCharSequence(name, cs); 2018 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) { 2019 outBundle.putBoolean(name, v.data != 0); 2020 } else if (v.type >= TypedValue.TYPE_FIRST_INT 2021 && v.type <= TypedValue.TYPE_LAST_INT) { 2022 outBundle.putInt(name, v.data); 2023 } else if (v.type == TypedValue.TYPE_FLOAT) { 2024 outBundle.putFloat(name, v.getFloat()); 2025 } else { 2026 sa.recycle(); 2027 throw new XmlPullParserException("<" + tagName 2028 + "> only supports string, integer, float, color, and boolean at " 2029 + attrs.getPositionDescription()); 2030 } 2031 } else { 2032 sa.recycle(); 2033 throw new XmlPullParserException("<" + tagName 2034 + "> requires an android:value or android:resource attribute at " 2035 + attrs.getPositionDescription()); 2036 } 2037 2038 sa.recycle(); 2039 } 2040 2041 /** 2042 * Retrieve underlying AssetManager storage for these resources. 2043 */ 2044 public final AssetManager getAssets() { 2045 return mResourcesImpl.getAssets(); 2046 } 2047 2048 /** 2049 * Call this to remove all cached loaded layout resources from the 2050 * Resources object. Only intended for use with performance testing 2051 * tools. 2052 */ 2053 public final void flushLayoutCache() { 2054 mResourcesImpl.flushLayoutCache(); 2055 } 2056 2057 /** 2058 * Start preloading of resource data using this Resources object. Only 2059 * for use by the zygote process for loading common system resources. 2060 * {@hide} 2061 */ 2062 public final void startPreloading() { 2063 mResourcesImpl.startPreloading(); 2064 } 2065 2066 /** 2067 * Called by zygote when it is done preloading resources, to change back 2068 * to normal Resources operation. 2069 */ 2070 public final void finishPreloading() { 2071 mResourcesImpl.finishPreloading(); 2072 } 2073 2074 /** 2075 * @hide 2076 */ 2077 public LongSparseArray<ConstantState> getPreloadedDrawables() { 2078 return mResourcesImpl.getPreloadedDrawables(); 2079 } 2080 2081 /** 2082 * Loads an XML parser for the specified file. 2083 * 2084 * @param id the resource identifier for the file 2085 * @param type the type of resource (used for logging) 2086 * @return a parser for the specified XML file 2087 * @throws NotFoundException if the file could not be loaded 2088 */ 2089 @NonNull 2090 XmlResourceParser loadXmlResourceParser(@AnyRes int id, @NonNull String type) 2091 throws NotFoundException { 2092 final TypedValue value = obtainTempTypedValue(); 2093 try { 2094 final ResourcesImpl impl = mResourcesImpl; 2095 impl.getValue(id, value, true); 2096 if (value.type == TypedValue.TYPE_STRING) { 2097 return impl.loadXmlResourceParser(value.string.toString(), id, 2098 value.assetCookie, type); 2099 } 2100 throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) 2101 + " type #0x" + Integer.toHexString(value.type) + " is not valid"); 2102 } finally { 2103 releaseTempTypedValue(value); 2104 } 2105 } 2106 2107 /** 2108 * Loads an XML parser for the specified file. 2109 * 2110 * @param file the path for the XML file to parse 2111 * @param id the resource identifier for the file 2112 * @param assetCookie the asset cookie for the file 2113 * @param type the type of resource (used for logging) 2114 * @return a parser for the specified XML file 2115 * @throws NotFoundException if the file could not be loaded 2116 */ 2117 @NonNull 2118 XmlResourceParser loadXmlResourceParser(String file, int id, int assetCookie, 2119 String type) throws NotFoundException { 2120 return mResourcesImpl.loadXmlResourceParser(file, id, assetCookie, type); 2121 } 2122 2123 /** 2124 * Called by ConfigurationBoundResourceCacheTest. 2125 * @hide 2126 */ 2127 @VisibleForTesting 2128 public int calcConfigChanges(Configuration config) { 2129 return mResourcesImpl.calcConfigChanges(config); 2130 } 2131 2132 /** 2133 * Obtains styled attributes from the theme, if available, or unstyled 2134 * resources if the theme is null. 2135 * 2136 * @hide 2137 */ 2138 public static TypedArray obtainAttributes( 2139 Resources res, Theme theme, AttributeSet set, int[] attrs) { 2140 if (theme == null) { 2141 return res.obtainAttributes(set, attrs); 2142 } 2143 return theme.obtainStyledAttributes(set, attrs, 0, 0); 2144 } 2145} 2146