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