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 | |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 19 | import com.android.internal.util.XmlUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | import org.xmlpull.v1.XmlPullParser; |
| 22 | import org.xmlpull.v1.XmlPullParserException; |
| 23 | |
Dianne Hackborn | ebff8f9 | 2011-05-12 18:07:47 -0700 | [diff] [blame] | 24 | import android.content.pm.ActivityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.graphics.Movie; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.graphics.drawable.ColorDrawable; |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable.ConstantState; |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 29 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.Bundle; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 31 | import android.os.IBinder; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 32 | import android.os.Trace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.util.AttributeSet; |
| 34 | import android.util.DisplayMetrics; |
| 35 | import android.util.Log; |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 36 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.util.TypedValue; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 38 | import android.util.LongSparseArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
| 42 | import java.lang.ref.WeakReference; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 43 | import java.util.Locale; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 45 | import libcore.icu.NativePluralRules; |
| 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | /** |
| 48 | * 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] | 49 | * asset manager of the application (accessible through {@link #getAssets}) and |
| 50 | * provides a high-level API for getting typed data from the assets. |
| 51 | * |
| 52 | * <p>The Android resource system keeps track of all non-code assets associated with an |
| 53 | * application. You can use this class to access your application's resources. You can generally |
| 54 | * acquire the {@link android.content.res.Resources} instance associated with your application |
| 55 | * with {@link android.content.Context#getResources getResources()}.</p> |
| 56 | * |
| 57 | * <p>The Android SDK tools compile your application's resources into the application binary |
| 58 | * at build time. To use a resource, you must install it correctly in the source tree (inside |
| 59 | * your project's {@code res/} directory) and build your application. As part of the build |
| 60 | * process, the SDK tools generate symbols for each resource, which you can use in your application |
| 61 | * code to access the resources.</p> |
| 62 | * |
| 63 | * <p>Using application resources makes it easy to update various characteristics of your |
| 64 | * application without modifying code, and—by providing sets of alternative |
| 65 | * resources—enables you to optimize your application for a variety of device configurations |
| 66 | * (such as for different languages and screen sizes). This is an important aspect of developing |
| 67 | * Android applications that are compatible on different types of devices.</p> |
| 68 | * |
| 69 | * <p>For more information about using resources, see the documentation about <a |
| 70 | * 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] | 71 | */ |
| 72 | public class Resources { |
| 73 | static final String TAG = "Resources"; |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 74 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | private static final boolean DEBUG_LOAD = false; |
| 76 | private static final boolean DEBUG_CONFIG = false; |
Dianne Hackborn | ab0f485 | 2011-09-12 16:59:06 -0700 | [diff] [blame] | 77 | private static final boolean DEBUG_ATTRIBUTES_CACHE = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | private static final boolean TRACE_FOR_PRELOAD = false; |
Dianne Hackborn | 9b44aae | 2011-09-02 19:17:16 -0700 | [diff] [blame] | 79 | private static final boolean TRACE_FOR_MISS_PRELOAD = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 81 | private static final int LAYOUT_DIR_CONFIG = ActivityInfo.activityInfoConfigToNative( |
| 82 | ActivityInfo.CONFIG_LAYOUT_DIRECTION); |
| 83 | |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 84 | private static final int ID_OTHER = 0x01000004; |
| 85 | |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 86 | private static final Object sSync = new Object(); |
Fabrice Di Meglio | b9a13b8 | 2013-04-15 14:05:30 -0700 | [diff] [blame] | 87 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | // Information about preloaded resources. Note that they are not |
| 89 | // protected by a lock, because while preloading in zygote we are all |
| 90 | // single-threaded, and after that these are immutable. |
Fabrice Di Meglio | b9a13b8 | 2013-04-15 14:05:30 -0700 | [diff] [blame] | 91 | private static final LongSparseArray<Drawable.ConstantState>[] sPreloadedDrawables; |
| 92 | private static final LongSparseArray<Drawable.ConstantState> sPreloadedColorDrawables |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 93 | = new LongSparseArray<Drawable.ConstantState>(); |
Romain Guy | 5d911c3 | 2012-04-12 16:25:17 -0700 | [diff] [blame] | 94 | private static final LongSparseArray<ColorStateList> sPreloadedColorStateLists |
| 95 | = new LongSparseArray<ColorStateList>(); |
Fabrice Di Meglio | b9a13b8 | 2013-04-15 14:05:30 -0700 | [diff] [blame] | 96 | |
Alan Viverette | edc4664 | 2014-02-01 01:43:16 -0800 | [diff] [blame] | 97 | // Used by BridgeResources in layoutlib |
| 98 | static Resources mSystem = null; |
| 99 | |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 100 | private static boolean sPreloaded; |
| 101 | private static int sPreloadedDensity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 103 | // These are protected by mAccessLock. |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 104 | private final Object mAccessLock = new Object(); |
| 105 | private final Configuration mTmpConfig = new Configuration(); |
| 106 | private final LongSparseArray<WeakReference<Drawable.ConstantState>> mDrawableCache |
| 107 | = new LongSparseArray<WeakReference<Drawable.ConstantState>>(0); |
| 108 | private final LongSparseArray<WeakReference<ColorStateList>> mColorStateListCache |
| 109 | = new LongSparseArray<WeakReference<ColorStateList>>(0); |
| 110 | private final LongSparseArray<WeakReference<Drawable.ConstantState>> mColorDrawableCache |
| 111 | = new LongSparseArray<WeakReference<Drawable.ConstantState>>(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 113 | private TypedValue mTmpValue = new TypedValue(); |
| 114 | private boolean mPreloading; |
| 115 | |
| 116 | private TypedArray mCachedStyledAttributes = null; |
| 117 | private RuntimeException mLastRetrievedAttrs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | |
| 119 | private int mLastCachedXmlBlockIndex = -1; |
| 120 | private final int[] mCachedXmlBlockIds = { 0, 0, 0, 0 }; |
| 121 | private final XmlBlock[] mCachedXmlBlocks = new XmlBlock[4]; |
| 122 | |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 123 | private final AssetManager mAssets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | private final Configuration mConfiguration = new Configuration(); |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 125 | private final DisplayMetrics mMetrics = new DisplayMetrics(); |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 126 | private NativePluralRules mPluralRule; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 127 | |
| 128 | private CompatibilityInfo mCompatibilityInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; |
| 129 | private WeakReference<IBinder> mToken; |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 130 | |
Fabrice Di Meglio | b9a13b8 | 2013-04-15 14:05:30 -0700 | [diff] [blame] | 131 | static { |
| 132 | sPreloadedDrawables = new LongSparseArray[2]; |
| 133 | sPreloadedDrawables[0] = new LongSparseArray<Drawable.ConstantState>(); |
| 134 | sPreloadedDrawables[1] = new LongSparseArray<Drawable.ConstantState>(); |
| 135 | } |
| 136 | |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 137 | /** @hide */ |
| 138 | public static int selectDefaultTheme(int curTheme, int targetSdkVersion) { |
| 139 | return selectSystemTheme(curTheme, targetSdkVersion, |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 140 | com.android.internal.R.style.Theme, |
| 141 | com.android.internal.R.style.Theme_Holo, |
| 142 | com.android.internal.R.style.Theme_DeviceDefault); |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** @hide */ |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 146 | public static int selectSystemTheme(int curTheme, int targetSdkVersion, |
| 147 | int orig, int holo, int deviceDefault) { |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 148 | if (curTheme != 0) { |
| 149 | return curTheme; |
| 150 | } |
| 151 | if (targetSdkVersion < Build.VERSION_CODES.HONEYCOMB) { |
| 152 | return orig; |
| 153 | } |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 154 | if (targetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 155 | return holo; |
| 156 | } |
| 157 | return deviceDefault; |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 158 | } |
| 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | /** |
| 161 | * This exception is thrown by the resource APIs when a requested resource |
| 162 | * can not be found. |
| 163 | */ |
| 164 | public static class NotFoundException extends RuntimeException { |
| 165 | public NotFoundException() { |
| 166 | } |
| 167 | |
| 168 | public NotFoundException(String name) { |
| 169 | super(name); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Create a new Resources object on top of an existing set of assets in an |
| 175 | * AssetManager. |
| 176 | * |
| 177 | * @param assets Previously created AssetManager. |
| 178 | * @param metrics Current display metrics to consider when |
| 179 | * selecting/computing resource values. |
| 180 | * @param config Desired device configuration to consider when |
| 181 | * selecting/computing resource values (optional). |
| 182 | */ |
Romain Guy | 5d911c3 | 2012-04-12 16:25:17 -0700 | [diff] [blame] | 183 | public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) { |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 184 | this(assets, metrics, config, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 188 | * Creates a new Resources object with CompatibilityInfo. |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 189 | * |
| 190 | * @param assets Previously created AssetManager. |
| 191 | * @param metrics Current display metrics to consider when |
| 192 | * selecting/computing resource values. |
| 193 | * @param config Desired device configuration to consider when |
| 194 | * selecting/computing resource values (optional). |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 195 | * @param compatInfo this resource's compatibility info. Must not be null. |
| 196 | * @param token The Activity token for determining stack affiliation. Usually null. |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 197 | * @hide |
| 198 | */ |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 199 | public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config, |
| 200 | CompatibilityInfo compatInfo, IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | mAssets = assets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | mMetrics.setToDefaults(); |
Adam Lesinski | 79a8ffe | 2013-09-19 20:33:15 -0700 | [diff] [blame] | 203 | if (compatInfo != null) { |
| 204 | mCompatibilityInfo = compatInfo; |
| 205 | } |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 206 | mToken = new WeakReference<IBinder>(token); |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 207 | updateConfiguration(config, metrics); |
| 208 | assets.ensureStringBlocks(); |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | * Return a global shared Resources object that provides access to only |
| 213 | * system resources (no application resources), and is not configured for |
| 214 | * the current screen (can not use dimension units, does not change based |
| 215 | * on orientation, etc). |
| 216 | */ |
| 217 | public static Resources getSystem() { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 218 | synchronized (sSync) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | Resources ret = mSystem; |
| 220 | if (ret == null) { |
| 221 | ret = new Resources(); |
| 222 | mSystem = ret; |
| 223 | } |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Return the string value associated with a particular resource ID. The |
| 231 | * returned object will be a String if this is a plain string; it will be |
| 232 | * some other type of CharSequence if it is styled. |
| 233 | * {@more} |
| 234 | * |
| 235 | * @param id The desired resource identifier, as generated by the aapt |
| 236 | * tool. This integer encodes the package, type, and resource |
| 237 | * entry. The value 0 is an invalid identifier. |
| 238 | * |
| 239 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 240 | * |
| 241 | * @return CharSequence The string data associated with the resource, plus |
| 242 | * possibly styled text information. |
| 243 | */ |
| 244 | public CharSequence getText(int id) throws NotFoundException { |
| 245 | CharSequence res = mAssets.getResourceText(id); |
| 246 | if (res != null) { |
| 247 | return res; |
| 248 | } |
| 249 | throw new NotFoundException("String resource ID #0x" |
| 250 | + Integer.toHexString(id)); |
| 251 | } |
| 252 | |
| 253 | /** |
Elliott Hughes | 95d5ab3 | 2013-03-08 11:26:57 -0800 | [diff] [blame] | 254 | * Returns the character sequence necessary for grammatically correct pluralization |
| 255 | * of the given resource ID for the given quantity. |
| 256 | * Note that the character sequence is selected based solely on grammatical necessity, |
| 257 | * and that such rules differ between languages. Do not assume you know which string |
| 258 | * will be returned for a given quantity. See |
| 259 | * <a href="{@docRoot}guide/topics/resources/string-resource.html#Plurals">String Resources</a> |
| 260 | * for more detail. |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 261 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | * @param id The desired resource identifier, as generated by the aapt |
| 263 | * tool. This integer encodes the package, type, and resource |
| 264 | * entry. The value 0 is an invalid identifier. |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 265 | * @param quantity The number used to get the correct string for the current language's |
| 266 | * plural rules. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | * |
| 268 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 269 | * |
| 270 | * @return CharSequence The string data associated with the resource, plus |
| 271 | * possibly styled text information. |
| 272 | */ |
| 273 | public CharSequence getQuantityText(int id, int quantity) throws NotFoundException { |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 274 | NativePluralRules rule = getPluralRule(); |
| 275 | CharSequence res = mAssets.getResourceBagText(id, |
| 276 | attrForQuantityCode(rule.quantityForInt(quantity))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | if (res != null) { |
| 278 | return res; |
| 279 | } |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 280 | res = mAssets.getResourceBagText(id, ID_OTHER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | if (res != null) { |
| 282 | return res; |
| 283 | } |
| 284 | throw new NotFoundException("Plural resource ID #0x" + Integer.toHexString(id) |
| 285 | + " quantity=" + quantity |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 286 | + " item=" + stringForQuantityCode(rule.quantityForInt(quantity))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 289 | private NativePluralRules getPluralRule() { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 290 | synchronized (sSync) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | if (mPluralRule == null) { |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 292 | mPluralRule = NativePluralRules.forLocale(mConfiguration.locale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | } |
| 294 | return mPluralRule; |
| 295 | } |
| 296 | } |
| 297 | |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 298 | private static int attrForQuantityCode(int quantityCode) { |
| 299 | switch (quantityCode) { |
| 300 | case NativePluralRules.ZERO: return 0x01000005; |
| 301 | case NativePluralRules.ONE: return 0x01000006; |
| 302 | case NativePluralRules.TWO: return 0x01000007; |
| 303 | case NativePluralRules.FEW: return 0x01000008; |
| 304 | case NativePluralRules.MANY: return 0x01000009; |
| 305 | default: return ID_OTHER; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | private static String stringForQuantityCode(int quantityCode) { |
| 310 | switch (quantityCode) { |
| 311 | case NativePluralRules.ZERO: return "zero"; |
| 312 | case NativePluralRules.ONE: return "one"; |
| 313 | case NativePluralRules.TWO: return "two"; |
| 314 | case NativePluralRules.FEW: return "few"; |
| 315 | case NativePluralRules.MANY: return "many"; |
| 316 | default: return "other"; |
| 317 | } |
| 318 | } |
| 319 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | /** |
| 321 | * Return the string value associated with a particular resource ID. It |
| 322 | * will be stripped of any styled text information. |
| 323 | * {@more} |
| 324 | * |
| 325 | * @param id The desired resource identifier, as generated by the aapt |
| 326 | * tool. This integer encodes the package, type, and resource |
| 327 | * entry. The value 0 is an invalid identifier. |
| 328 | * |
| 329 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 330 | * |
| 331 | * @return String The string data associated with the resource, |
| 332 | * stripped of styled text information. |
| 333 | */ |
| 334 | public String getString(int id) throws NotFoundException { |
| 335 | CharSequence res = getText(id); |
| 336 | if (res != null) { |
| 337 | return res.toString(); |
| 338 | } |
| 339 | throw new NotFoundException("String resource ID #0x" |
| 340 | + Integer.toHexString(id)); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /** |
| 345 | * Return the string value associated with a particular resource ID, |
| 346 | * substituting the format arguments as defined in {@link java.util.Formatter} |
| 347 | * and {@link java.lang.String#format}. It will be stripped of any styled text |
| 348 | * information. |
| 349 | * {@more} |
| 350 | * |
| 351 | * @param id The desired resource identifier, as generated by the aapt |
| 352 | * tool. This integer encodes the package, type, and resource |
| 353 | * entry. The value 0 is an invalid identifier. |
| 354 | * |
| 355 | * @param formatArgs The format arguments that will be used for substitution. |
| 356 | * |
| 357 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 358 | * |
| 359 | * @return String The string data associated with the resource, |
| 360 | * stripped of styled text information. |
| 361 | */ |
| 362 | public String getString(int id, Object... formatArgs) throws NotFoundException { |
| 363 | String raw = getString(id); |
| 364 | return String.format(mConfiguration.locale, raw, formatArgs); |
| 365 | } |
| 366 | |
| 367 | /** |
Elliott Hughes | 95d5ab3 | 2013-03-08 11:26:57 -0800 | [diff] [blame] | 368 | * Formats the string necessary for grammatically correct pluralization |
| 369 | * of the given resource ID for the given quantity, using the given arguments. |
| 370 | * Note that the string is selected based solely on grammatical necessity, |
| 371 | * and that such rules differ between languages. Do not assume you know which string |
| 372 | * will be returned for a given quantity. See |
| 373 | * <a href="{@docRoot}guide/topics/resources/string-resource.html#Plurals">String Resources</a> |
| 374 | * for more detail. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | * |
Elliott Hughes | 95d5ab3 | 2013-03-08 11:26:57 -0800 | [diff] [blame] | 376 | * <p>Substitution of format arguments works as if using |
| 377 | * {@link java.util.Formatter} and {@link java.lang.String#format}. |
| 378 | * The resulting string will be stripped of any styled text information. |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 379 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | * @param id The desired resource identifier, as generated by the aapt |
| 381 | * tool. This integer encodes the package, type, and resource |
| 382 | * entry. The value 0 is an invalid identifier. |
| 383 | * @param quantity The number used to get the correct string for the current language's |
| 384 | * plural rules. |
| 385 | * @param formatArgs The format arguments that will be used for substitution. |
| 386 | * |
| 387 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 388 | * |
| 389 | * @return String The string data associated with the resource, |
| 390 | * stripped of styled text information. |
| 391 | */ |
| 392 | public String getQuantityString(int id, int quantity, Object... formatArgs) |
| 393 | throws NotFoundException { |
| 394 | String raw = getQuantityText(id, quantity).toString(); |
| 395 | return String.format(mConfiguration.locale, raw, formatArgs); |
| 396 | } |
| 397 | |
| 398 | /** |
Elliott Hughes | 95d5ab3 | 2013-03-08 11:26:57 -0800 | [diff] [blame] | 399 | * Returns the string necessary for grammatically correct pluralization |
| 400 | * of the given resource ID for the given quantity. |
| 401 | * Note that the string is selected based solely on grammatical necessity, |
| 402 | * and that such rules differ between languages. Do not assume you know which string |
| 403 | * will be returned for a given quantity. See |
| 404 | * <a href="{@docRoot}guide/topics/resources/string-resource.html#Plurals">String Resources</a> |
| 405 | * for more detail. |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 406 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | * @param id The desired resource identifier, as generated by the aapt |
| 408 | * tool. This integer encodes the package, type, and resource |
| 409 | * entry. The value 0 is an invalid identifier. |
| 410 | * @param quantity The number used to get the correct string for the current language's |
| 411 | * plural rules. |
| 412 | * |
| 413 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 414 | * |
| 415 | * @return String The string data associated with the resource, |
| 416 | * stripped of styled text information. |
| 417 | */ |
| 418 | public String getQuantityString(int id, int quantity) throws NotFoundException { |
| 419 | return getQuantityText(id, quantity).toString(); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Return the string value associated with a particular resource ID. The |
| 424 | * returned object will be a String if this is a plain string; it will be |
| 425 | * some other type of CharSequence if it is styled. |
| 426 | * |
| 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. |
| 430 | * |
| 431 | * @param def The default CharSequence to return. |
| 432 | * |
| 433 | * @return CharSequence The string data associated with the resource, plus |
| 434 | * possibly styled text information, or def if id is 0 or not found. |
| 435 | */ |
| 436 | public CharSequence getText(int id, CharSequence def) { |
| 437 | CharSequence res = id != 0 ? mAssets.getResourceText(id) : null; |
| 438 | return res != null ? res : def; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Return the styled text array associated with a particular resource ID. |
| 443 | * |
| 444 | * @param id The desired resource identifier, as generated by the aapt |
| 445 | * tool. This integer encodes the package, type, and resource |
| 446 | * entry. The value 0 is an invalid identifier. |
| 447 | * |
| 448 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 449 | * |
| 450 | * @return The styled text array associated with the resource. |
| 451 | */ |
| 452 | public CharSequence[] getTextArray(int id) throws NotFoundException { |
| 453 | CharSequence[] res = mAssets.getResourceTextArray(id); |
| 454 | if (res != null) { |
| 455 | return res; |
| 456 | } |
| 457 | throw new NotFoundException("Text array resource ID #0x" |
| 458 | + Integer.toHexString(id)); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Return the string array associated with a particular resource ID. |
| 463 | * |
| 464 | * @param id The desired resource identifier, as generated by the aapt |
| 465 | * tool. This integer encodes the package, type, and resource |
| 466 | * entry. The value 0 is an invalid identifier. |
| 467 | * |
| 468 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 469 | * |
| 470 | * @return The string array associated with the resource. |
| 471 | */ |
| 472 | public String[] getStringArray(int id) throws NotFoundException { |
| 473 | String[] res = mAssets.getResourceStringArray(id); |
| 474 | if (res != null) { |
| 475 | return res; |
| 476 | } |
| 477 | throw new NotFoundException("String array resource ID #0x" |
| 478 | + Integer.toHexString(id)); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Return the int array associated with a particular resource ID. |
| 483 | * |
| 484 | * @param id The desired resource identifier, as generated by the aapt |
| 485 | * tool. This integer encodes the package, type, and resource |
| 486 | * entry. The value 0 is an invalid identifier. |
| 487 | * |
| 488 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 489 | * |
| 490 | * @return The int array associated with the resource. |
| 491 | */ |
| 492 | public int[] getIntArray(int id) throws NotFoundException { |
| 493 | int[] res = mAssets.getArrayIntResource(id); |
| 494 | if (res != null) { |
| 495 | return res; |
| 496 | } |
| 497 | throw new NotFoundException("Int array resource ID #0x" |
| 498 | + Integer.toHexString(id)); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Return an array of heterogeneous values. |
| 503 | * |
| 504 | * @param id The desired resource identifier, as generated by the aapt |
| 505 | * tool. This integer encodes the package, type, and resource |
| 506 | * entry. The value 0 is an invalid identifier. |
| 507 | * |
| 508 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 509 | * |
| 510 | * @return Returns a TypedArray holding an array of the array values. |
| 511 | * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} |
| 512 | * when done with it. |
| 513 | */ |
| 514 | public TypedArray obtainTypedArray(int id) throws NotFoundException { |
| 515 | int len = mAssets.getArraySize(id); |
| 516 | if (len < 0) { |
| 517 | throw new NotFoundException("Array resource ID #0x" |
| 518 | + Integer.toHexString(id)); |
| 519 | } |
| 520 | |
| 521 | TypedArray array = getCachedStyledAttributes(len); |
| 522 | array.mLength = mAssets.retrieveArray(id, array.mData); |
| 523 | array.mIndices[0] = 0; |
| 524 | |
| 525 | return array; |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Retrieve a dimensional for a particular resource ID. Unit |
| 530 | * conversions are based on the current {@link DisplayMetrics} associated |
| 531 | * with the resources. |
| 532 | * |
| 533 | * @param id The desired resource identifier, as generated by the aapt |
| 534 | * tool. This integer encodes the package, type, and resource |
| 535 | * entry. The value 0 is an invalid identifier. |
| 536 | * |
| 537 | * @return Resource dimension value multiplied by the appropriate |
| 538 | * metric. |
| 539 | * |
| 540 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 541 | * |
| 542 | * @see #getDimensionPixelOffset |
| 543 | * @see #getDimensionPixelSize |
| 544 | */ |
| 545 | public float getDimension(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 546 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | TypedValue value = mTmpValue; |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 548 | if (value == null) { |
| 549 | mTmpValue = value = new TypedValue(); |
| 550 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | getValue(id, value, true); |
| 552 | if (value.type == TypedValue.TYPE_DIMENSION) { |
| 553 | return TypedValue.complexToDimension(value.data, mMetrics); |
| 554 | } |
| 555 | throw new NotFoundException( |
| 556 | "Resource ID #0x" + Integer.toHexString(id) + " type #0x" |
| 557 | + Integer.toHexString(value.type) + " is not valid"); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Retrieve a dimensional for a particular resource ID for use |
| 563 | * as an offset in raw pixels. This is the same as |
| 564 | * {@link #getDimension}, except the returned value is converted to |
| 565 | * integer pixels for you. An offset conversion involves simply |
| 566 | * truncating the base value to an integer. |
| 567 | * |
| 568 | * @param id The desired resource identifier, as generated by the aapt |
| 569 | * tool. This integer encodes the package, type, and resource |
| 570 | * entry. The value 0 is an invalid identifier. |
| 571 | * |
| 572 | * @return Resource dimension value multiplied by the appropriate |
| 573 | * metric and truncated to integer pixels. |
| 574 | * |
| 575 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 576 | * |
| 577 | * @see #getDimension |
| 578 | * @see #getDimensionPixelSize |
| 579 | */ |
| 580 | public int getDimensionPixelOffset(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 581 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | TypedValue value = mTmpValue; |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 583 | if (value == null) { |
| 584 | mTmpValue = value = new TypedValue(); |
| 585 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | getValue(id, value, true); |
| 587 | if (value.type == TypedValue.TYPE_DIMENSION) { |
| 588 | return TypedValue.complexToDimensionPixelOffset( |
| 589 | value.data, mMetrics); |
| 590 | } |
| 591 | throw new NotFoundException( |
| 592 | "Resource ID #0x" + Integer.toHexString(id) + " type #0x" |
| 593 | + Integer.toHexString(value.type) + " is not valid"); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Retrieve a dimensional for a particular resource ID for use |
| 599 | * as a size in raw pixels. This is the same as |
| 600 | * {@link #getDimension}, except the returned value is converted to |
| 601 | * integer pixels for use as a size. A size conversion involves |
| 602 | * rounding the base value, and ensuring that a non-zero base value |
| 603 | * is at least one pixel in size. |
| 604 | * |
| 605 | * @param id The desired resource identifier, as generated by the aapt |
| 606 | * tool. This integer encodes the package, type, and resource |
| 607 | * entry. The value 0 is an invalid identifier. |
| 608 | * |
| 609 | * @return Resource dimension value multiplied by the appropriate |
| 610 | * metric and truncated to integer pixels. |
| 611 | * |
| 612 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 613 | * |
| 614 | * @see #getDimension |
| 615 | * @see #getDimensionPixelOffset |
| 616 | */ |
| 617 | public int getDimensionPixelSize(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 618 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | TypedValue value = mTmpValue; |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 620 | if (value == null) { |
| 621 | mTmpValue = value = new TypedValue(); |
| 622 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | getValue(id, value, true); |
| 624 | if (value.type == TypedValue.TYPE_DIMENSION) { |
| 625 | return TypedValue.complexToDimensionPixelSize( |
| 626 | value.data, mMetrics); |
| 627 | } |
| 628 | throw new NotFoundException( |
| 629 | "Resource ID #0x" + Integer.toHexString(id) + " type #0x" |
| 630 | + Integer.toHexString(value.type) + " is not valid"); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Retrieve a fractional unit for a particular resource ID. |
| 636 | * |
| 637 | * @param id The desired resource identifier, as generated by the aapt |
| 638 | * tool. This integer encodes the package, type, and resource |
| 639 | * entry. The value 0 is an invalid identifier. |
| 640 | * @param base The base value of this fraction. In other words, a |
| 641 | * standard fraction is multiplied by this value. |
| 642 | * @param pbase The parent base value of this fraction. In other |
| 643 | * words, a parent fraction (nn%p) is multiplied by this |
| 644 | * value. |
| 645 | * |
| 646 | * @return Attribute fractional value multiplied by the appropriate |
| 647 | * base value. |
| 648 | * |
| 649 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 650 | */ |
| 651 | public float getFraction(int id, int base, int pbase) { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 652 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | TypedValue value = mTmpValue; |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 654 | if (value == null) { |
| 655 | mTmpValue = value = new TypedValue(); |
| 656 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | getValue(id, value, true); |
| 658 | if (value.type == TypedValue.TYPE_FRACTION) { |
| 659 | return TypedValue.complexToFraction(value.data, base, pbase); |
| 660 | } |
| 661 | throw new NotFoundException( |
| 662 | "Resource ID #0x" + Integer.toHexString(id) + " type #0x" |
| 663 | + Integer.toHexString(value.type) + " is not valid"); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Return a drawable object associated with a particular resource ID. |
| 669 | * Various types of objects will be returned depending on the underlying |
| 670 | * resource -- for example, a solid color, PNG image, scalable image, etc. |
| 671 | * The Drawable API hides these implementation details. |
Dianne Hackborn | fb5c3db | 2012-05-18 15:24:24 -0700 | [diff] [blame] | 672 | * |
| 673 | * <p class="note"><strong>Note:</strong> Prior to |
| 674 | * {@link android.os.Build.VERSION_CODES#JELLY_BEAN}, this function |
| 675 | * would not correctly retrieve the final configuration density when |
| 676 | * the resource ID passed here is an alias to another Drawable resource. |
| 677 | * This means that if the density configuration of the alias resource |
| 678 | * is different than the actual resource, the density of the returned |
| 679 | * Drawable would be incorrect, resulting in bad scaling. To work |
| 680 | * around this, you can instead retrieve the Drawable through |
| 681 | * {@link TypedArray#getDrawable TypedArray.getDrawable}. Use |
| 682 | * {@link android.content.Context#obtainStyledAttributes(int[]) |
| 683 | * Context.obtainStyledAttributes} with |
| 684 | * an array containing the resource ID of interest to create the TypedArray.</p> |
| 685 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | * @param id The desired resource identifier, as generated by the aapt |
| 687 | * tool. This integer encodes the package, type, and resource |
| 688 | * entry. The value 0 is an invalid identifier. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | * @return Drawable An object that can be used to draw this resource. |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 690 | * @throws NotFoundException Throws NotFoundException if the given ID does |
| 691 | * not exist. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | */ |
| 693 | public Drawable getDrawable(int id) throws NotFoundException { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 694 | return getDrawable(id, null); |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Return a drawable object associated with a particular resource ID and |
| 699 | * styled for the specified theme. |
| 700 | * |
| 701 | * @param id The desired resource identifier, as generated by the aapt |
| 702 | * tool. This integer encodes the package, type, and resource |
| 703 | * entry. The value 0 is an invalid identifier. |
| 704 | * @param theme The theme used to style the drawable attributes. |
| 705 | * @return Drawable An object that can be used to draw this resource. |
| 706 | * @throws NotFoundException Throws NotFoundException if the given ID does |
| 707 | * not exist. |
| 708 | */ |
| 709 | public Drawable getDrawable(int id, Theme theme) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 710 | TypedValue value; |
| 711 | synchronized (mAccessLock) { |
| 712 | value = mTmpValue; |
| 713 | if (value == null) { |
| 714 | value = new TypedValue(); |
| 715 | } else { |
| 716 | mTmpValue = null; |
| 717 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | getValue(id, value, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | } |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 720 | final Drawable res = loadDrawable(value, id, theme); |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 721 | synchronized (mAccessLock) { |
| 722 | if (mTmpValue == null) { |
| 723 | mTmpValue = value; |
| 724 | } |
| 725 | } |
| 726 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /** |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 730 | * Return a drawable object associated with a particular resource ID for the |
| 731 | * given screen density in DPI. This will set the drawable's density to be |
| 732 | * the device's density multiplied by the ratio of actual drawable density |
| 733 | * to requested density. This allows the drawable to be scaled up to the |
| 734 | * correct size if needed. Various types of objects will be returned |
| 735 | * depending on the underlying resource -- for example, a solid color, PNG |
| 736 | * image, scalable image, etc. The Drawable API hides these implementation |
| 737 | * details. |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 738 | * |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 739 | * @param id The desired resource identifier, as generated by the aapt tool. |
| 740 | * This integer encodes the package, type, and resource entry. |
| 741 | * The value 0 is an invalid identifier. |
| 742 | * @param density the desired screen density indicated by the resource as |
| 743 | * found in {@link DisplayMetrics}. |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 744 | * @return Drawable An object that can be used to draw this resource. |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 745 | * @throws NotFoundException Throws NotFoundException if the given ID does |
| 746 | * not exist. |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 747 | * @see #getDrawableForDensity(int, int, Theme) |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 748 | */ |
| 749 | public Drawable getDrawableForDensity(int id, int density) throws NotFoundException { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 750 | return getDrawableForDensity(id, density, null); |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Return a drawable object associated with a particular resource ID for the |
| 755 | * given screen density in DPI and styled for the specified theme. |
| 756 | * |
| 757 | * @param id The desired resource identifier, as generated by the aapt tool. |
| 758 | * This integer encodes the package, type, and resource entry. |
| 759 | * The value 0 is an invalid identifier. |
| 760 | * @param density The desired screen density indicated by the resource as |
| 761 | * found in {@link DisplayMetrics}. |
| 762 | * @param theme The theme used to style the drawable attributes. |
| 763 | * @return Drawable An object that can be used to draw this resource. |
| 764 | * @throws NotFoundException Throws NotFoundException if the given ID does |
| 765 | * not exist. |
| 766 | */ |
| 767 | public Drawable getDrawableForDensity(int id, int density, Theme theme) { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 768 | TypedValue value; |
| 769 | synchronized (mAccessLock) { |
| 770 | value = mTmpValue; |
| 771 | if (value == null) { |
| 772 | value = new TypedValue(); |
| 773 | } else { |
| 774 | mTmpValue = null; |
| 775 | } |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 776 | getValueForDensity(id, density, value, true); |
| 777 | |
| 778 | /* |
| 779 | * Pretend the requested density is actually the display density. If |
| 780 | * the drawable returned is not the requested density, then force it |
| 781 | * to be scaled later by dividing its density by the ratio of |
| 782 | * requested density to actual device density. Drawables that have |
| 783 | * undefined density or no density don't need to be handled here. |
| 784 | */ |
| 785 | if (value.density > 0 && value.density != TypedValue.DENSITY_NONE) { |
| 786 | if (value.density == density) { |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 787 | value.density = mMetrics.densityDpi; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 788 | } else { |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 789 | value.density = (value.density * mMetrics.densityDpi) / density; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 790 | } |
| 791 | } |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 792 | } |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 793 | |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 794 | final Drawable res = loadDrawable(value, id, theme); |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 795 | synchronized (mAccessLock) { |
| 796 | if (mTmpValue == null) { |
| 797 | mTmpValue = value; |
| 798 | } |
| 799 | } |
| 800 | return res; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | * Return a movie object associated with the particular resource ID. |
| 805 | * @param id The desired resource identifier, as generated by the aapt |
| 806 | * tool. This integer encodes the package, type, and resource |
| 807 | * entry. The value 0 is an invalid identifier. |
| 808 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 809 | * |
| 810 | */ |
| 811 | public Movie getMovie(int id) throws NotFoundException { |
| 812 | InputStream is = openRawResource(id); |
| 813 | Movie movie = Movie.decodeStream(is); |
| 814 | try { |
| 815 | is.close(); |
| 816 | } |
| 817 | catch (java.io.IOException e) { |
| 818 | // don't care, since the return value is valid |
| 819 | } |
| 820 | return movie; |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Return a color integer associated with a particular resource ID. |
| 825 | * If the resource holds a complex |
| 826 | * {@link android.content.res.ColorStateList}, then the default color from |
| 827 | * the set is returned. |
| 828 | * |
| 829 | * @param id The desired resource identifier, as generated by the aapt |
| 830 | * tool. This integer encodes the package, type, and resource |
| 831 | * entry. The value 0 is an invalid identifier. |
| 832 | * |
| 833 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 834 | * |
| 835 | * @return Returns a single color value in the form 0xAARRGGBB. |
| 836 | */ |
| 837 | public int getColor(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 838 | TypedValue value; |
| 839 | synchronized (mAccessLock) { |
| 840 | value = mTmpValue; |
| 841 | if (value == null) { |
| 842 | value = new TypedValue(); |
| 843 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | getValue(id, value, true); |
| 845 | if (value.type >= TypedValue.TYPE_FIRST_INT |
| 846 | && value.type <= TypedValue.TYPE_LAST_INT) { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 847 | mTmpValue = value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | return value.data; |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 849 | } else if (value.type != TypedValue.TYPE_STRING) { |
| 850 | throw new NotFoundException( |
| 851 | "Resource ID #0x" + Integer.toHexString(id) + " type #0x" |
| 852 | + Integer.toHexString(value.type) + " is not valid"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | } |
Dianne Hackborn | e5b50a6 | 2013-02-11 16:18:42 -0800 | [diff] [blame] | 854 | mTmpValue = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | } |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 856 | ColorStateList csl = loadColorStateList(value, id); |
| 857 | synchronized (mAccessLock) { |
| 858 | if (mTmpValue == null) { |
| 859 | mTmpValue = value; |
| 860 | } |
| 861 | } |
| 862 | return csl.getDefaultColor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /** |
| 866 | * Return a color state list associated with a particular resource ID. The |
| 867 | * resource may contain either a single raw color value, or a complex |
| 868 | * {@link android.content.res.ColorStateList} holding multiple possible colors. |
| 869 | * |
| 870 | * @param id The desired resource identifier of a {@link ColorStateList}, |
| 871 | * as generated by the aapt tool. This integer encodes the package, type, and resource |
| 872 | * entry. The value 0 is an invalid identifier. |
| 873 | * |
| 874 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 875 | * |
| 876 | * @return Returns a ColorStateList object containing either a single |
| 877 | * solid color or multiple colors that can be selected based on a state. |
| 878 | */ |
| 879 | public ColorStateList getColorStateList(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 880 | TypedValue value; |
| 881 | synchronized (mAccessLock) { |
| 882 | value = mTmpValue; |
| 883 | if (value == null) { |
| 884 | value = new TypedValue(); |
| 885 | } else { |
| 886 | mTmpValue = null; |
| 887 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | getValue(id, value, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | } |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 890 | ColorStateList res = loadColorStateList(value, id); |
| 891 | synchronized (mAccessLock) { |
| 892 | if (mTmpValue == null) { |
| 893 | mTmpValue = value; |
| 894 | } |
| 895 | } |
| 896 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /** |
| 900 | * Return a boolean associated with a particular resource ID. This can be |
| 901 | * used with any integral resource value, and will return true if it is |
| 902 | * non-zero. |
| 903 | * |
| 904 | * @param id The desired resource identifier, as generated by the aapt |
| 905 | * tool. This integer encodes the package, type, and resource |
| 906 | * entry. The value 0 is an invalid identifier. |
| 907 | * |
| 908 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 909 | * |
| 910 | * @return Returns the boolean value contained in the resource. |
| 911 | */ |
| 912 | public boolean getBoolean(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 913 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | TypedValue value = mTmpValue; |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 915 | if (value == null) { |
| 916 | mTmpValue = value = new TypedValue(); |
| 917 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | getValue(id, value, true); |
| 919 | if (value.type >= TypedValue.TYPE_FIRST_INT |
| 920 | && value.type <= TypedValue.TYPE_LAST_INT) { |
| 921 | return value.data != 0; |
| 922 | } |
| 923 | throw new NotFoundException( |
| 924 | "Resource ID #0x" + Integer.toHexString(id) + " type #0x" |
| 925 | + Integer.toHexString(value.type) + " is not valid"); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Return an integer associated with a particular resource ID. |
| 931 | * |
| 932 | * @param id The desired resource identifier, as generated by the aapt |
| 933 | * tool. This integer encodes the package, type, and resource |
| 934 | * entry. The value 0 is an invalid identifier. |
| 935 | * |
| 936 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 937 | * |
| 938 | * @return Returns the integer value contained in the resource. |
| 939 | */ |
| 940 | public int getInteger(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 941 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | TypedValue value = mTmpValue; |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 943 | if (value == null) { |
| 944 | mTmpValue = value = new TypedValue(); |
| 945 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | getValue(id, value, true); |
| 947 | if (value.type >= TypedValue.TYPE_FIRST_INT |
| 948 | && value.type <= TypedValue.TYPE_LAST_INT) { |
| 949 | return value.data; |
| 950 | } |
| 951 | throw new NotFoundException( |
| 952 | "Resource ID #0x" + Integer.toHexString(id) + " type #0x" |
| 953 | + Integer.toHexString(value.type) + " is not valid"); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * Return an XmlResourceParser through which you can read a view layout |
| 959 | * description for the given resource ID. This parser has limited |
| 960 | * functionality -- in particular, you can't change its input, and only |
| 961 | * the high-level events are available. |
| 962 | * |
| 963 | * <p>This function is really a simple wrapper for calling |
| 964 | * {@link #getXml} with a layout resource. |
| 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 | * |
| 970 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 971 | * |
| 972 | * @return A new parser object through which you can read |
| 973 | * the XML data. |
| 974 | * |
| 975 | * @see #getXml |
| 976 | */ |
| 977 | public XmlResourceParser getLayout(int id) throws NotFoundException { |
| 978 | return loadXmlResourceParser(id, "layout"); |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * Return an XmlResourceParser through which you can read an animation |
| 983 | * description for the given resource ID. This parser has limited |
| 984 | * functionality -- in particular, you can't change its input, and only |
| 985 | * the high-level events are available. |
| 986 | * |
| 987 | * <p>This function is really a simple wrapper for calling |
| 988 | * {@link #getXml} with an animation resource. |
| 989 | * |
| 990 | * @param id The desired resource identifier, as generated by the aapt |
| 991 | * tool. This integer encodes the package, type, and resource |
| 992 | * entry. The value 0 is an invalid identifier. |
| 993 | * |
| 994 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 995 | * |
| 996 | * @return A new parser object through which you can read |
| 997 | * the XML data. |
| 998 | * |
| 999 | * @see #getXml |
| 1000 | */ |
| 1001 | public XmlResourceParser getAnimation(int id) throws NotFoundException { |
| 1002 | return loadXmlResourceParser(id, "anim"); |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * Return an XmlResourceParser through which you can read a generic XML |
| 1007 | * resource for the given resource ID. |
| 1008 | * |
| 1009 | * <p>The XmlPullParser implementation returned here has some limited |
| 1010 | * functionality. In particular, you can't change its input, and only |
| 1011 | * high-level parsing events are available (since the document was |
| 1012 | * pre-parsed for you at build time, which involved merging text and |
| 1013 | * stripping comments). |
| 1014 | * |
| 1015 | * @param id The desired resource identifier, as generated by the aapt |
| 1016 | * tool. This integer encodes the package, type, and resource |
| 1017 | * entry. The value 0 is an invalid identifier. |
| 1018 | * |
| 1019 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1020 | * |
| 1021 | * @return A new parser object through which you can read |
| 1022 | * the XML data. |
| 1023 | * |
| 1024 | * @see android.util.AttributeSet |
| 1025 | */ |
| 1026 | public XmlResourceParser getXml(int id) throws NotFoundException { |
| 1027 | return loadXmlResourceParser(id, "xml"); |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * Open a data stream for reading a raw resource. This can only be used |
| 1032 | * with resources whose value is the name of an asset files -- that is, it can be |
| 1033 | * used to open drawable, sound, and raw resources; it will fail on string |
| 1034 | * and color resources. |
| 1035 | * |
| 1036 | * @param id The resource identifier to open, as generated by the appt |
| 1037 | * tool. |
| 1038 | * |
| 1039 | * @return InputStream Access to the resource data. |
| 1040 | * |
| 1041 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1042 | * |
| 1043 | */ |
| 1044 | public InputStream openRawResource(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1045 | TypedValue value; |
| 1046 | synchronized (mAccessLock) { |
| 1047 | value = mTmpValue; |
| 1048 | if (value == null) { |
| 1049 | value = new TypedValue(); |
| 1050 | } else { |
| 1051 | mTmpValue = null; |
| 1052 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | } |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1054 | InputStream res = openRawResource(id, value); |
| 1055 | synchronized (mAccessLock) { |
| 1056 | if (mTmpValue == null) { |
| 1057 | mTmpValue = value; |
| 1058 | } |
| 1059 | } |
| 1060 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * 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] | 1065 | * 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] | 1066 | * used to open drawable, sound, and raw resources; it will fail on string |
| 1067 | * and color resources. |
| 1068 | * |
| 1069 | * @param id The resource identifier to open, as generated by the appt tool. |
| 1070 | * @param value The TypedValue object to hold the resource information. |
| 1071 | * |
| 1072 | * @return InputStream Access to the resource data. |
| 1073 | * |
| 1074 | * @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] | 1075 | */ |
| 1076 | public InputStream openRawResource(int id, TypedValue value) throws NotFoundException { |
| 1077 | getValue(id, value, true); |
| 1078 | |
| 1079 | try { |
| 1080 | return mAssets.openNonAsset(value.assetCookie, value.string.toString(), |
| 1081 | AssetManager.ACCESS_STREAMING); |
| 1082 | } catch (Exception e) { |
| 1083 | NotFoundException rnf = new NotFoundException("File " + value.string.toString() + |
| 1084 | " from drawable resource ID #0x" + Integer.toHexString(id)); |
| 1085 | rnf.initCause(e); |
| 1086 | throw rnf; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * Open a file descriptor for reading a raw resource. This can only be used |
| 1092 | * with resources whose value is the name of an asset files -- that is, it can be |
| 1093 | * used to open drawable, sound, and raw resources; it will fail on string |
| 1094 | * and color resources. |
| 1095 | * |
| 1096 | * <p>This function only works for resources that are stored in the package |
| 1097 | * as uncompressed data, which typically includes things like mp3 files |
| 1098 | * and png images. |
| 1099 | * |
| 1100 | * @param id The resource identifier to open, as generated by the appt |
| 1101 | * tool. |
| 1102 | * |
| 1103 | * @return AssetFileDescriptor A new file descriptor you can use to read |
| 1104 | * the resource. This includes the file descriptor itself, as well as the |
| 1105 | * offset and length of data where the resource appears in the file. A |
| 1106 | * null is returned if the file exists but is compressed. |
| 1107 | * |
| 1108 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1109 | * |
| 1110 | */ |
| 1111 | public AssetFileDescriptor openRawResourceFd(int id) throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1112 | TypedValue value; |
| 1113 | synchronized (mAccessLock) { |
| 1114 | value = mTmpValue; |
| 1115 | if (value == null) { |
| 1116 | value = new TypedValue(); |
| 1117 | } else { |
| 1118 | mTmpValue = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | } |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1120 | getValue(id, value, true); |
| 1121 | } |
| 1122 | try { |
| 1123 | return mAssets.openNonAssetFd( |
| 1124 | value.assetCookie, value.string.toString()); |
| 1125 | } catch (Exception e) { |
| 1126 | NotFoundException rnf = new NotFoundException( |
| 1127 | "File " + value.string.toString() |
| 1128 | + " from drawable resource ID #0x" |
| 1129 | + Integer.toHexString(id)); |
| 1130 | rnf.initCause(e); |
| 1131 | throw rnf; |
| 1132 | } finally { |
| 1133 | synchronized (mAccessLock) { |
| 1134 | if (mTmpValue == null) { |
| 1135 | mTmpValue = value; |
| 1136 | } |
| 1137 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * Return the raw data associated with a particular resource ID. |
| 1143 | * |
| 1144 | * @param id The desired resource identifier, as generated by the aapt |
| 1145 | * tool. This integer encodes the package, type, and resource |
| 1146 | * entry. The value 0 is an invalid identifier. |
| 1147 | * @param outValue Object in which to place the resource data. |
| 1148 | * @param resolveRefs If true, a resource that is a reference to another |
| 1149 | * resource will be followed so that you receive the |
| 1150 | * actual final resource data. If false, the TypedValue |
| 1151 | * will be filled in with the reference itself. |
| 1152 | * |
| 1153 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1154 | * |
| 1155 | */ |
| 1156 | public void getValue(int id, TypedValue outValue, boolean resolveRefs) |
| 1157 | throws NotFoundException { |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 1158 | boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | if (found) { |
| 1160 | return; |
| 1161 | } |
| 1162 | throw new NotFoundException("Resource ID #0x" |
| 1163 | + Integer.toHexString(id)); |
| 1164 | } |
| 1165 | |
| 1166 | /** |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 1167 | * Get the raw value associated with a resource with associated density. |
| 1168 | * |
| 1169 | * @param id resource identifier |
| 1170 | * @param density density in DPI |
| 1171 | * @param resolveRefs If true, a resource that is a reference to another |
| 1172 | * resource will be followed so that you receive the actual final |
| 1173 | * resource data. If false, the TypedValue will be filled in with |
| 1174 | * the reference itself. |
| 1175 | * @throws NotFoundException Throws NotFoundException if the given ID does |
| 1176 | * not exist. |
| 1177 | * @see #getValue(String, TypedValue, boolean) |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 1178 | */ |
| 1179 | public void getValueForDensity(int id, int density, TypedValue outValue, boolean resolveRefs) |
| 1180 | throws NotFoundException { |
| 1181 | boolean found = mAssets.getResourceValue(id, density, outValue, resolveRefs); |
| 1182 | if (found) { |
| 1183 | return; |
| 1184 | } |
| 1185 | throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id)); |
| 1186 | } |
| 1187 | |
| 1188 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | * Return the raw data associated with a particular resource ID. |
| 1190 | * See getIdentifier() for information on how names are mapped to resource |
| 1191 | * IDs, and getString(int) for information on how string resources are |
| 1192 | * retrieved. |
| 1193 | * |
| 1194 | * <p>Note: use of this function is discouraged. It is much more |
| 1195 | * efficient to retrieve resources by identifier than by name. |
| 1196 | * |
| 1197 | * @param name The name of the desired resource. This is passed to |
| 1198 | * getIdentifier() with a default type of "string". |
| 1199 | * @param outValue Object in which to place the resource data. |
| 1200 | * @param resolveRefs If true, a resource that is a reference to another |
| 1201 | * resource will be followed so that you receive the |
| 1202 | * actual final resource data. If false, the TypedValue |
| 1203 | * will be filled in with the reference itself. |
| 1204 | * |
| 1205 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1206 | * |
| 1207 | */ |
| 1208 | public void getValue(String name, TypedValue outValue, boolean resolveRefs) |
| 1209 | throws NotFoundException { |
| 1210 | int id = getIdentifier(name, "string", null); |
| 1211 | if (id != 0) { |
| 1212 | getValue(id, outValue, resolveRefs); |
| 1213 | return; |
| 1214 | } |
| 1215 | throw new NotFoundException("String resource name " + name); |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * This class holds the current attribute values for a particular theme. |
| 1220 | * In other words, a Theme is a set of values for resource attributes; |
| 1221 | * these are used in conjunction with {@link TypedArray} |
| 1222 | * to resolve the final value for an attribute. |
| 1223 | * |
| 1224 | * <p>The Theme's attributes come into play in two ways: (1) a styled |
| 1225 | * attribute can explicit reference a value in the theme through the |
| 1226 | * "?themeAttribute" syntax; (2) if no value has been defined for a |
| 1227 | * particular styled attribute, as a last resort we will try to find that |
| 1228 | * attribute's value in the Theme. |
| 1229 | * |
| 1230 | * <p>You will normally use the {@link #obtainStyledAttributes} APIs to |
| 1231 | * retrieve XML attributes with style and theme information applied. |
| 1232 | */ |
| 1233 | public final class Theme { |
| 1234 | /** |
| 1235 | * Place new attribute values into the theme. The style resource |
| 1236 | * specified by <var>resid</var> will be retrieved from this Theme's |
| 1237 | * resources, its values placed into the Theme object. |
| 1238 | * |
| 1239 | * <p>The semantics of this function depends on the <var>force</var> |
| 1240 | * argument: If false, only values that are not already defined in |
| 1241 | * the theme will be copied from the system resource; otherwise, if |
| 1242 | * any of the style's attributes are already defined in the theme, the |
| 1243 | * current values in the theme will be overwritten. |
| 1244 | * |
| 1245 | * @param resid The resource ID of a style resource from which to |
| 1246 | * obtain attribute values. |
| 1247 | * @param force If true, values in the style resource will always be |
| 1248 | * used in the theme; otherwise, they will only be used |
| 1249 | * if not already defined in the theme. |
| 1250 | */ |
| 1251 | public void applyStyle(int resid, boolean force) { |
| 1252 | AssetManager.applyThemeStyle(mTheme, resid, force); |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Set this theme to hold the same contents as the theme |
| 1257 | * <var>other</var>. If both of these themes are from the same |
| 1258 | * Resources object, they will be identical after this function |
| 1259 | * returns. If they are from different Resources, only the resources |
| 1260 | * they have in common will be set in this theme. |
| 1261 | * |
| 1262 | * @param other The existing Theme to copy from. |
| 1263 | */ |
| 1264 | public void setTo(Theme other) { |
| 1265 | AssetManager.copyTheme(mTheme, other.mTheme); |
| 1266 | } |
| 1267 | |
| 1268 | /** |
John Spurlock | 330dd53 | 2012-12-18 12:03:11 -0500 | [diff] [blame] | 1269 | * Return a TypedArray holding the values defined by |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | * <var>Theme</var> which are listed in <var>attrs</var>. |
| 1271 | * |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 1272 | * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done |
| 1273 | * with the array. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | * |
| 1275 | * @param attrs The desired attributes. |
| 1276 | * |
| 1277 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1278 | * |
| 1279 | * @return Returns a TypedArray holding an array of the attribute values. |
| 1280 | * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} |
| 1281 | * when done with it. |
| 1282 | * |
| 1283 | * @see Resources#obtainAttributes |
| 1284 | * @see #obtainStyledAttributes(int, int[]) |
| 1285 | * @see #obtainStyledAttributes(AttributeSet, int[], int, int) |
| 1286 | */ |
| 1287 | public TypedArray obtainStyledAttributes(int[] attrs) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1288 | final int len = attrs.length; |
| 1289 | final TypedArray array = getCachedStyledAttributes(len); |
| 1290 | array.mTheme = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | array.mRsrcs = attrs; |
| 1292 | AssetManager.applyStyle(mTheme, 0, 0, 0, attrs, |
| 1293 | array.mData, array.mIndices); |
| 1294 | return array; |
| 1295 | } |
| 1296 | |
| 1297 | /** |
John Spurlock | 330dd53 | 2012-12-18 12:03:11 -0500 | [diff] [blame] | 1298 | * 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] | 1299 | * resource <var>resid</var> which are listed in <var>attrs</var>. |
| 1300 | * |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 1301 | * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done |
| 1302 | * with the array. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | * |
| 1304 | * @param resid The desired style resource. |
| 1305 | * @param attrs The desired attributes in the style. |
| 1306 | * |
| 1307 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1308 | * |
| 1309 | * @return Returns a TypedArray holding an array of the attribute values. |
| 1310 | * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} |
| 1311 | * when done with it. |
| 1312 | * |
| 1313 | * @see Resources#obtainAttributes |
| 1314 | * @see #obtainStyledAttributes(int[]) |
| 1315 | * @see #obtainStyledAttributes(AttributeSet, int[], int, int) |
| 1316 | */ |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1317 | public TypedArray obtainStyledAttributes(int resid, int[] attrs) throws NotFoundException { |
| 1318 | final int len = attrs.length; |
| 1319 | final TypedArray array = getCachedStyledAttributes(len); |
| 1320 | array.mTheme = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | array.mRsrcs = attrs; |
| 1322 | |
| 1323 | AssetManager.applyStyle(mTheme, 0, resid, 0, attrs, |
| 1324 | array.mData, array.mIndices); |
| 1325 | if (false) { |
| 1326 | int[] data = array.mData; |
| 1327 | |
| 1328 | System.out.println("**********************************************************"); |
| 1329 | System.out.println("**********************************************************"); |
| 1330 | System.out.println("**********************************************************"); |
| 1331 | System.out.println("Attributes:"); |
| 1332 | String s = " Attrs:"; |
| 1333 | int i; |
| 1334 | for (i=0; i<attrs.length; i++) { |
| 1335 | s = s + " 0x" + Integer.toHexString(attrs[i]); |
| 1336 | } |
| 1337 | System.out.println(s); |
| 1338 | s = " Found:"; |
| 1339 | TypedValue value = new TypedValue(); |
| 1340 | for (i=0; i<attrs.length; i++) { |
| 1341 | int d = i*AssetManager.STYLE_NUM_ENTRIES; |
| 1342 | value.type = data[d+AssetManager.STYLE_TYPE]; |
| 1343 | value.data = data[d+AssetManager.STYLE_DATA]; |
| 1344 | value.assetCookie = data[d+AssetManager.STYLE_ASSET_COOKIE]; |
| 1345 | value.resourceId = data[d+AssetManager.STYLE_RESOURCE_ID]; |
| 1346 | s = s + " 0x" + Integer.toHexString(attrs[i]) |
| 1347 | + "=" + value; |
| 1348 | } |
| 1349 | System.out.println(s); |
| 1350 | } |
| 1351 | return array; |
| 1352 | } |
| 1353 | |
| 1354 | /** |
John Spurlock | 330dd53 | 2012-12-18 12:03:11 -0500 | [diff] [blame] | 1355 | * Return a TypedArray holding the attribute values in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | * <var>set</var> |
| 1357 | * that are listed in <var>attrs</var>. In addition, if the given |
| 1358 | * AttributeSet specifies a style class (through the "style" attribute), |
| 1359 | * that style will be applied on top of the base attributes it defines. |
| 1360 | * |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 1361 | * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done |
| 1362 | * with the array. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | * |
| 1364 | * <p>When determining the final value of a particular attribute, there |
| 1365 | * are four inputs that come into play:</p> |
| 1366 | * |
| 1367 | * <ol> |
| 1368 | * <li> Any attribute values in the given AttributeSet. |
| 1369 | * <li> The style resource specified in the AttributeSet (named |
| 1370 | * "style"). |
| 1371 | * <li> The default style specified by <var>defStyleAttr</var> and |
| 1372 | * <var>defStyleRes</var> |
| 1373 | * <li> The base values in this theme. |
| 1374 | * </ol> |
| 1375 | * |
| 1376 | * <p>Each of these inputs is considered in-order, with the first listed |
| 1377 | * taking precedence over the following ones. In other words, if in the |
| 1378 | * AttributeSet you have supplied <code><Button |
| 1379 | * textColor="#ff000000"></code>, then the button's text will |
| 1380 | * <em>always</em> be black, regardless of what is specified in any of |
| 1381 | * the styles. |
| 1382 | * |
| 1383 | * @param set The base set of attribute values. May be null. |
| 1384 | * @param attrs The desired attributes to be retrieved. |
| 1385 | * @param defStyleAttr An attribute in the current theme that contains a |
| 1386 | * reference to a style resource that supplies |
John Spurlock | 330dd53 | 2012-12-18 12:03:11 -0500 | [diff] [blame] | 1387 | * defaults values for the TypedArray. Can be |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1388 | * 0 to not look for defaults. |
| 1389 | * @param defStyleRes A resource identifier of a style resource that |
John Spurlock | 330dd53 | 2012-12-18 12:03:11 -0500 | [diff] [blame] | 1390 | * supplies default values for the TypedArray, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1391 | * used only if defStyleAttr is 0 or can not be found |
| 1392 | * in the theme. Can be 0 to not look for defaults. |
| 1393 | * |
| 1394 | * @return Returns a TypedArray holding an array of the attribute values. |
| 1395 | * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} |
| 1396 | * when done with it. |
| 1397 | * |
| 1398 | * @see Resources#obtainAttributes |
| 1399 | * @see #obtainStyledAttributes(int[]) |
| 1400 | * @see #obtainStyledAttributes(int, int[]) |
| 1401 | */ |
| 1402 | public TypedArray obtainStyledAttributes(AttributeSet set, |
| 1403 | int[] attrs, int defStyleAttr, int defStyleRes) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1404 | final int len = attrs.length; |
| 1405 | final TypedArray array = getCachedStyledAttributes(len); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | |
| 1407 | // XXX note that for now we only work with compiled XML files. |
| 1408 | // To support generic XML files we will need to manually parse |
| 1409 | // out the attributes from the XML file (applying type information |
| 1410 | // contained in the resources and such). |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1411 | final XmlBlock.Parser parser = (XmlBlock.Parser)set; |
| 1412 | AssetManager.applyStyle(mTheme, defStyleAttr, defStyleRes, |
| 1413 | parser != null ? parser.mParseState : 0, attrs, array.mData, array.mIndices); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1414 | |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1415 | array.mTheme = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | array.mRsrcs = attrs; |
| 1417 | array.mXml = parser; |
| 1418 | |
| 1419 | if (false) { |
| 1420 | int[] data = array.mData; |
| 1421 | |
| 1422 | System.out.println("Attributes:"); |
| 1423 | String s = " Attrs:"; |
| 1424 | int i; |
| 1425 | for (i=0; i<set.getAttributeCount(); i++) { |
| 1426 | s = s + " " + set.getAttributeName(i); |
| 1427 | int id = set.getAttributeNameResource(i); |
| 1428 | if (id != 0) { |
| 1429 | s = s + "(0x" + Integer.toHexString(id) + ")"; |
| 1430 | } |
| 1431 | s = s + "=" + set.getAttributeValue(i); |
| 1432 | } |
| 1433 | System.out.println(s); |
| 1434 | s = " Found:"; |
| 1435 | TypedValue value = new TypedValue(); |
| 1436 | for (i=0; i<attrs.length; i++) { |
| 1437 | int d = i*AssetManager.STYLE_NUM_ENTRIES; |
| 1438 | value.type = data[d+AssetManager.STYLE_TYPE]; |
| 1439 | value.data = data[d+AssetManager.STYLE_DATA]; |
| 1440 | value.assetCookie = data[d+AssetManager.STYLE_ASSET_COOKIE]; |
| 1441 | value.resourceId = data[d+AssetManager.STYLE_RESOURCE_ID]; |
| 1442 | s = s + " 0x" + Integer.toHexString(attrs[i]) |
| 1443 | + "=" + value; |
| 1444 | } |
| 1445 | System.out.println(s); |
| 1446 | } |
| 1447 | |
| 1448 | return array; |
| 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * Retrieve the value of an attribute in the Theme. The contents of |
| 1453 | * <var>outValue</var> are ultimately filled in by |
| 1454 | * {@link Resources#getValue}. |
| 1455 | * |
| 1456 | * @param resid The resource identifier of the desired theme |
| 1457 | * attribute. |
| 1458 | * @param outValue Filled in with the ultimate resource value supplied |
| 1459 | * by the attribute. |
| 1460 | * @param resolveRefs If true, resource references will be walked; if |
| 1461 | * false, <var>outValue</var> may be a |
| 1462 | * TYPE_REFERENCE. In either case, it will never |
| 1463 | * be a TYPE_ATTRIBUTE. |
| 1464 | * |
| 1465 | * @return boolean Returns true if the attribute was found and |
| 1466 | * <var>outValue</var> is valid, else false. |
| 1467 | */ |
| 1468 | public boolean resolveAttribute(int resid, TypedValue outValue, |
| 1469 | boolean resolveRefs) { |
| 1470 | boolean got = mAssets.getThemeValue(mTheme, resid, outValue, resolveRefs); |
| 1471 | if (false) { |
| 1472 | System.out.println( |
| 1473 | "resolveAttribute #" + Integer.toHexString(resid) |
| 1474 | + " got=" + got + ", type=0x" + Integer.toHexString(outValue.type) |
| 1475 | + ", data=0x" + Integer.toHexString(outValue.data)); |
| 1476 | } |
| 1477 | return got; |
| 1478 | } |
| 1479 | |
| 1480 | /** |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1481 | * Return a drawable object associated with a particular resource ID |
| 1482 | * and styled for the Theme. |
| 1483 | * |
| 1484 | * @param id The desired resource identifier, as generated by the aapt |
| 1485 | * tool. This integer encodes the package, type, and resource |
| 1486 | * entry. The value 0 is an invalid identifier. |
| 1487 | * @return Drawable An object that can be used to draw this resource. |
| 1488 | * @throws NotFoundException Throws NotFoundException if the given ID |
| 1489 | * does not exist. |
| 1490 | */ |
| 1491 | public Drawable getDrawable(int id) throws NotFoundException { |
| 1492 | return Resources.this.getDrawable(id, this); |
| 1493 | } |
| 1494 | |
| 1495 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | * Print contents of this theme out to the log. For debugging only. |
| 1497 | * |
| 1498 | * @param priority The log priority to use. |
| 1499 | * @param tag The log tag to use. |
| 1500 | * @param prefix Text to prefix each line printed. |
| 1501 | */ |
| 1502 | public void dump(int priority, String tag, String prefix) { |
| 1503 | AssetManager.dumpTheme(mTheme, priority, tag, prefix); |
| 1504 | } |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1505 | |
| 1506 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1507 | protected void finalize() throws Throwable { |
| 1508 | super.finalize(); |
| 1509 | mAssets.releaseTheme(mTheme); |
| 1510 | } |
| 1511 | |
| 1512 | /*package*/ Theme() { |
| 1513 | mAssets = Resources.this.mAssets; |
| 1514 | mTheme = mAssets.createTheme(); |
| 1515 | } |
| 1516 | |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1517 | @SuppressWarnings("hiding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1518 | private final AssetManager mAssets; |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1519 | private final long mTheme; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * Generate a new Theme object for this set of Resources. It initially |
| 1524 | * starts out empty. |
| 1525 | * |
| 1526 | * @return Theme The newly created Theme container. |
| 1527 | */ |
| 1528 | public final Theme newTheme() { |
| 1529 | return new Theme(); |
| 1530 | } |
| 1531 | |
| 1532 | /** |
| 1533 | * Retrieve a set of basic attribute values from an AttributeSet, not |
| 1534 | * performing styling of them using a theme and/or style resources. |
| 1535 | * |
| 1536 | * @param set The current attribute values to retrieve. |
| 1537 | * @param attrs The specific attributes to be retrieved. |
| 1538 | * @return Returns a TypedArray holding an array of the attribute values. |
| 1539 | * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} |
| 1540 | * when done with it. |
| 1541 | * |
| 1542 | * @see Theme#obtainStyledAttributes(AttributeSet, int[], int, int) |
| 1543 | */ |
| 1544 | public TypedArray obtainAttributes(AttributeSet set, int[] attrs) { |
| 1545 | int len = attrs.length; |
| 1546 | TypedArray array = getCachedStyledAttributes(len); |
| 1547 | |
| 1548 | // XXX note that for now we only work with compiled XML files. |
| 1549 | // To support generic XML files we will need to manually parse |
| 1550 | // out the attributes from the XML file (applying type information |
| 1551 | // contained in the resources and such). |
| 1552 | XmlBlock.Parser parser = (XmlBlock.Parser)set; |
| 1553 | mAssets.retrieveAttributes(parser.mParseState, attrs, |
| 1554 | array.mData, array.mIndices); |
| 1555 | |
| 1556 | array.mRsrcs = attrs; |
| 1557 | array.mXml = parser; |
| 1558 | |
| 1559 | return array; |
| 1560 | } |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1561 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1562 | /** |
| 1563 | * Store the newly updated configuration. |
| 1564 | */ |
| 1565 | public void updateConfiguration(Configuration config, |
| 1566 | DisplayMetrics metrics) { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1567 | updateConfiguration(config, metrics, null); |
| 1568 | } |
| 1569 | |
| 1570 | /** |
| 1571 | * @hide |
| 1572 | */ |
| 1573 | public void updateConfiguration(Configuration config, |
| 1574 | DisplayMetrics metrics, CompatibilityInfo compat) { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1575 | synchronized (mAccessLock) { |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 1576 | if (false) { |
| 1577 | Slog.i(TAG, "**** Updating config of " + this + ": old config is " |
| 1578 | + mConfiguration + " old compat is " + mCompatibilityInfo); |
| 1579 | Slog.i(TAG, "**** Updating config of " + this + ": new config is " |
| 1580 | + config + " new compat is " + compat); |
| 1581 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1582 | if (compat != null) { |
| 1583 | mCompatibilityInfo = compat; |
| 1584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | if (metrics != null) { |
| 1586 | mMetrics.setTo(metrics); |
| 1587 | } |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 1588 | // NOTE: We should re-arrange this code to create a Display |
| 1589 | // with the CompatibilityInfo that is used everywhere we deal |
| 1590 | // with the display in relation to this app, rather than |
| 1591 | // doing the conversion here. This impl should be okay because |
| 1592 | // we make sure to return a compatible display in the places |
| 1593 | // where there are public APIs to retrieve the display... but |
| 1594 | // it would be cleaner and more maintainble to just be |
| 1595 | // consistently dealing with a compatible display everywhere in |
| 1596 | // the framework. |
Adam Lesinski | 79a8ffe | 2013-09-19 20:33:15 -0700 | [diff] [blame] | 1597 | mCompatibilityInfo.applyToDisplayMetrics(mMetrics); |
| 1598 | |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 1599 | int configChanges = 0xfffffff; |
| 1600 | if (config != null) { |
| 1601 | mTmpConfig.setTo(config); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 1602 | int density = config.densityDpi; |
| 1603 | if (density == Configuration.DENSITY_DPI_UNDEFINED) { |
| 1604 | density = mMetrics.noncompatDensityDpi; |
| 1605 | } |
Adam Lesinski | 79a8ffe | 2013-09-19 20:33:15 -0700 | [diff] [blame] | 1606 | |
| 1607 | mCompatibilityInfo.applyToConfiguration(density, mTmpConfig); |
| 1608 | |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 1609 | if (mTmpConfig.locale == null) { |
| 1610 | mTmpConfig.locale = Locale.getDefault(); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1611 | mTmpConfig.setLayoutDirection(mTmpConfig.locale); |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 1612 | } |
| 1613 | configChanges = mConfiguration.updateFrom(mTmpConfig); |
| 1614 | configChanges = ActivityInfo.activityInfoConfigToNative(configChanges); |
| 1615 | } |
| 1616 | if (mConfiguration.locale == null) { |
| 1617 | mConfiguration.locale = Locale.getDefault(); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1618 | mConfiguration.setLayoutDirection(mConfiguration.locale); |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 1619 | } |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 1620 | if (mConfiguration.densityDpi != Configuration.DENSITY_DPI_UNDEFINED) { |
| 1621 | mMetrics.densityDpi = mConfiguration.densityDpi; |
| 1622 | mMetrics.density = mConfiguration.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE; |
| 1623 | } |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 1624 | mMetrics.scaledDensity = mMetrics.density * mConfiguration.fontScale; |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 1625 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | String locale = null; |
| 1627 | if (mConfiguration.locale != null) { |
| 1628 | locale = mConfiguration.locale.getLanguage(); |
| 1629 | if (mConfiguration.locale.getCountry() != null) { |
| 1630 | locale += "-" + mConfiguration.locale.getCountry(); |
| 1631 | } |
| 1632 | } |
| 1633 | int width, height; |
| 1634 | if (mMetrics.widthPixels >= mMetrics.heightPixels) { |
| 1635 | width = mMetrics.widthPixels; |
| 1636 | height = mMetrics.heightPixels; |
| 1637 | } else { |
| 1638 | //noinspection SuspiciousNameCombination |
| 1639 | width = mMetrics.heightPixels; |
| 1640 | //noinspection SuspiciousNameCombination |
| 1641 | height = mMetrics.widthPixels; |
| 1642 | } |
| 1643 | int keyboardHidden = mConfiguration.keyboardHidden; |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 1644 | if (keyboardHidden == Configuration.KEYBOARDHIDDEN_NO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | && mConfiguration.hardKeyboardHidden |
| 1646 | == Configuration.HARDKEYBOARDHIDDEN_YES) { |
| 1647 | keyboardHidden = Configuration.KEYBOARDHIDDEN_SOFT; |
| 1648 | } |
| 1649 | mAssets.setConfiguration(mConfiguration.mcc, mConfiguration.mnc, |
| 1650 | locale, mConfiguration.orientation, |
| 1651 | mConfiguration.touchscreen, |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 1652 | mConfiguration.densityDpi, mConfiguration.keyboard, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | keyboardHidden, mConfiguration.navigation, width, height, |
Dianne Hackborn | 69cb875 | 2011-05-19 18:13:32 -0700 | [diff] [blame] | 1654 | mConfiguration.smallestScreenWidthDp, |
Dianne Hackborn | ebff8f9 | 2011-05-12 18:07:47 -0700 | [diff] [blame] | 1655 | mConfiguration.screenWidthDp, mConfiguration.screenHeightDp, |
Dianne Hackborn | 3b81bc1 | 2011-01-15 11:50:52 -0800 | [diff] [blame] | 1656 | mConfiguration.screenLayout, mConfiguration.uiMode, |
| 1657 | Build.VERSION.RESOURCES_SDK_INT); |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 1658 | |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 1659 | if (DEBUG_CONFIG) { |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 1660 | Slog.i(TAG, "**** Updating config of " + this + ": final config is " + mConfiguration |
| 1661 | + " final compat is " + mCompatibilityInfo); |
| 1662 | } |
| 1663 | |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1664 | clearDrawableCacheLocked(mDrawableCache, configChanges); |
| 1665 | clearDrawableCacheLocked(mColorDrawableCache, configChanges); |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 1666 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | mColorStateListCache.clear(); |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 1668 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1669 | flushLayoutCache(); |
| 1670 | } |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1671 | synchronized (sSync) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | if (mPluralRule != null) { |
Elliott Hughes | 1ad636c | 2010-07-01 16:51:48 -0700 | [diff] [blame] | 1673 | mPluralRule = NativePluralRules.forLocale(config.locale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1678 | private void clearDrawableCacheLocked( |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 1679 | LongSparseArray<WeakReference<ConstantState>> cache, |
| 1680 | int configChanges) { |
| 1681 | int N = cache.size(); |
| 1682 | if (DEBUG_CONFIG) { |
| 1683 | Log.d(TAG, "Cleaning up drawables config changes: 0x" |
| 1684 | + Integer.toHexString(configChanges)); |
| 1685 | } |
| 1686 | for (int i=0; i<N; i++) { |
| 1687 | WeakReference<Drawable.ConstantState> ref = cache.valueAt(i); |
| 1688 | if (ref != null) { |
| 1689 | Drawable.ConstantState cs = ref.get(); |
| 1690 | if (cs != null) { |
| 1691 | if (Configuration.needNewResources( |
| 1692 | configChanges, cs.getChangingConfigurations())) { |
| 1693 | if (DEBUG_CONFIG) { |
| 1694 | Log.d(TAG, "FLUSHING #0x" |
| 1695 | + Long.toHexString(mDrawableCache.keyAt(i)) |
| 1696 | + " / " + cs + " with changes: 0x" |
| 1697 | + Integer.toHexString(cs.getChangingConfigurations())); |
| 1698 | } |
| 1699 | cache.setValueAt(i, null); |
| 1700 | } else if (DEBUG_CONFIG) { |
| 1701 | Log.d(TAG, "(Keeping #0x" |
| 1702 | + Long.toHexString(cache.keyAt(i)) |
| 1703 | + " / " + cs + " with changes: 0x" |
| 1704 | + Integer.toHexString(cs.getChangingConfigurations()) |
| 1705 | + ")"); |
| 1706 | } |
| 1707 | } |
| 1708 | } |
| 1709 | } |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 1710 | } |
| 1711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | /** |
| 1713 | * Update the system resources configuration if they have previously |
| 1714 | * been initialized. |
| 1715 | * |
| 1716 | * @hide |
| 1717 | */ |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1718 | public static void updateSystemConfiguration(Configuration config, DisplayMetrics metrics, |
| 1719 | CompatibilityInfo compat) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | if (mSystem != null) { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1721 | mSystem.updateConfiguration(config, metrics, compat); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | //Log.i(TAG, "Updated system resources " + mSystem |
| 1723 | // + ": " + mSystem.getConfiguration()); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | /** |
| 1728 | * Return the current display metrics that are in effect for this resource |
| 1729 | * object. The returned object should be treated as read-only. |
| 1730 | * |
| 1731 | * @return The resource's current display metrics. |
| 1732 | */ |
| 1733 | public DisplayMetrics getDisplayMetrics() { |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 1734 | if (DEBUG_CONFIG) Slog.v(TAG, "Returning DisplayMetrics: " + mMetrics.widthPixels |
| 1735 | + "x" + mMetrics.heightPixels + " " + mMetrics.density); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | return mMetrics; |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * Return the current configuration that is in effect for this resource |
| 1741 | * object. The returned object should be treated as read-only. |
| 1742 | * |
| 1743 | * @return The resource's current configuration. |
| 1744 | */ |
| 1745 | public Configuration getConfiguration() { |
| 1746 | return mConfiguration; |
| 1747 | } |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 1748 | |
| 1749 | /** |
| 1750 | * Return the compatibility mode information for the application. |
| 1751 | * The returned object should be treated as read-only. |
| 1752 | * |
Dianne Hackborn | 3904d03 | 2011-05-27 12:09:11 -0700 | [diff] [blame] | 1753 | * @return compatibility info. |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 1754 | * @hide |
| 1755 | */ |
| 1756 | public CompatibilityInfo getCompatibilityInfo() { |
Adam Lesinski | 79a8ffe | 2013-09-19 20:33:15 -0700 | [diff] [blame] | 1757 | return mCompatibilityInfo; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 1758 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1759 | |
| 1760 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 1761 | * This is just for testing. |
| 1762 | * @hide |
| 1763 | */ |
| 1764 | public void setCompatibilityInfo(CompatibilityInfo ci) { |
Adam Lesinski | 79a8ffe | 2013-09-19 20:33:15 -0700 | [diff] [blame] | 1765 | if (ci != null) { |
| 1766 | mCompatibilityInfo = ci; |
| 1767 | updateConfiguration(mConfiguration, mMetrics); |
| 1768 | } |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1772 | * Return a resource identifier for the given resource name. A fully |
| 1773 | * qualified resource name is of the form "package:type/entry". The first |
| 1774 | * two components (package and type) are optional if defType and |
| 1775 | * defPackage, respectively, are specified here. |
| 1776 | * |
| 1777 | * <p>Note: use of this function is discouraged. It is much more |
| 1778 | * efficient to retrieve resources by identifier than by name. |
| 1779 | * |
| 1780 | * @param name The name of the desired resource. |
| 1781 | * @param defType Optional default resource type to find, if "type/" is |
| 1782 | * not included in the name. Can be null to require an |
| 1783 | * explicit type. |
| 1784 | * @param defPackage Optional default package to find, if "package:" is |
| 1785 | * not included in the name. Can be null to require an |
| 1786 | * explicit package. |
| 1787 | * |
| 1788 | * @return int The associated resource identifier. Returns 0 if no such |
| 1789 | * resource was found. (0 is not a valid resource ID.) |
| 1790 | */ |
| 1791 | public int getIdentifier(String name, String defType, String defPackage) { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 1792 | if (name == null) { |
| 1793 | throw new NullPointerException("name is null"); |
| 1794 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | try { |
| 1796 | return Integer.parseInt(name); |
| 1797 | } catch (Exception e) { |
| 1798 | // Ignore |
| 1799 | } |
| 1800 | return mAssets.getResourceIdentifier(name, defType, defPackage); |
| 1801 | } |
| 1802 | |
| 1803 | /** |
Jeff Sharkey | 47b5033 | 2013-03-15 14:46:46 -0700 | [diff] [blame] | 1804 | * Return true if given resource identifier includes a package. |
| 1805 | * |
| 1806 | * @hide |
| 1807 | */ |
| 1808 | public static boolean resourceHasPackage(int resid) { |
| 1809 | return (resid >>> 24) != 0; |
| 1810 | } |
| 1811 | |
| 1812 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | * Return the full name for a given resource identifier. This name is |
| 1814 | * a single string of the form "package:type/entry". |
| 1815 | * |
| 1816 | * @param resid The resource identifier whose name is to be retrieved. |
| 1817 | * |
| 1818 | * @return A string holding the name of the resource. |
| 1819 | * |
| 1820 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1821 | * |
| 1822 | * @see #getResourcePackageName |
| 1823 | * @see #getResourceTypeName |
| 1824 | * @see #getResourceEntryName |
| 1825 | */ |
| 1826 | public String getResourceName(int resid) throws NotFoundException { |
| 1827 | String str = mAssets.getResourceName(resid); |
| 1828 | if (str != null) return str; |
| 1829 | throw new NotFoundException("Unable to find resource ID #0x" |
| 1830 | + Integer.toHexString(resid)); |
| 1831 | } |
| 1832 | |
| 1833 | /** |
| 1834 | * Return the package name for a given resource identifier. |
| 1835 | * |
| 1836 | * @param resid The resource identifier whose package name is to be |
| 1837 | * retrieved. |
| 1838 | * |
| 1839 | * @return A string holding the package name of the resource. |
| 1840 | * |
| 1841 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1842 | * |
| 1843 | * @see #getResourceName |
| 1844 | */ |
| 1845 | public String getResourcePackageName(int resid) throws NotFoundException { |
| 1846 | String str = mAssets.getResourcePackageName(resid); |
| 1847 | if (str != null) return str; |
| 1848 | throw new NotFoundException("Unable to find resource ID #0x" |
| 1849 | + Integer.toHexString(resid)); |
| 1850 | } |
| 1851 | |
| 1852 | /** |
| 1853 | * Return the type name for a given resource identifier. |
| 1854 | * |
| 1855 | * @param resid The resource identifier whose type name is to be |
| 1856 | * retrieved. |
| 1857 | * |
| 1858 | * @return A string holding the type name of the resource. |
| 1859 | * |
| 1860 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1861 | * |
| 1862 | * @see #getResourceName |
| 1863 | */ |
| 1864 | public String getResourceTypeName(int resid) throws NotFoundException { |
| 1865 | String str = mAssets.getResourceTypeName(resid); |
| 1866 | if (str != null) return str; |
| 1867 | throw new NotFoundException("Unable to find resource ID #0x" |
| 1868 | + Integer.toHexString(resid)); |
| 1869 | } |
| 1870 | |
| 1871 | /** |
| 1872 | * Return the entry name for a given resource identifier. |
| 1873 | * |
| 1874 | * @param resid The resource identifier whose entry name is to be |
| 1875 | * retrieved. |
| 1876 | * |
| 1877 | * @return A string holding the entry name of the resource. |
| 1878 | * |
| 1879 | * @throws NotFoundException Throws NotFoundException if the given ID does not exist. |
| 1880 | * |
| 1881 | * @see #getResourceName |
| 1882 | */ |
| 1883 | public String getResourceEntryName(int resid) throws NotFoundException { |
| 1884 | String str = mAssets.getResourceEntryName(resid); |
| 1885 | if (str != null) return str; |
| 1886 | throw new NotFoundException("Unable to find resource ID #0x" |
| 1887 | + Integer.toHexString(resid)); |
| 1888 | } |
| 1889 | |
| 1890 | /** |
| 1891 | * Parse a series of {@link android.R.styleable#Extra <extra>} tags from |
| 1892 | * 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] | 1893 | * 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] | 1894 | * This will call {@link #parseBundleExtra} for each extra tag encountered. |
| 1895 | * |
| 1896 | * @param parser The parser from which to retrieve the extras. |
| 1897 | * @param outBundle A Bundle in which to place all parsed extras. |
| 1898 | * @throws XmlPullParserException |
| 1899 | * @throws IOException |
| 1900 | */ |
| 1901 | public void parseBundleExtras(XmlResourceParser parser, Bundle outBundle) |
| 1902 | throws XmlPullParserException, IOException { |
| 1903 | int outerDepth = parser.getDepth(); |
| 1904 | int type; |
| 1905 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 1906 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1907 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1908 | continue; |
| 1909 | } |
| 1910 | |
| 1911 | String nodeName = parser.getName(); |
| 1912 | if (nodeName.equals("extra")) { |
| 1913 | parseBundleExtra("extra", parser, outBundle); |
| 1914 | XmlUtils.skipCurrentTag(parser); |
| 1915 | |
| 1916 | } else { |
| 1917 | XmlUtils.skipCurrentTag(parser); |
| 1918 | } |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | /** |
| 1923 | * Parse a name/value pair out of an XML tag holding that data. The |
| 1924 | * AttributeSet must be holding the data defined by |
| 1925 | * {@link android.R.styleable#Extra}. The following value types are supported: |
| 1926 | * <ul> |
| 1927 | * <li> {@link TypedValue#TYPE_STRING}: |
| 1928 | * {@link Bundle#putCharSequence Bundle.putCharSequence()} |
| 1929 | * <li> {@link TypedValue#TYPE_INT_BOOLEAN}: |
| 1930 | * {@link Bundle#putCharSequence Bundle.putBoolean()} |
| 1931 | * <li> {@link TypedValue#TYPE_FIRST_INT}-{@link TypedValue#TYPE_LAST_INT}: |
| 1932 | * {@link Bundle#putCharSequence Bundle.putBoolean()} |
| 1933 | * <li> {@link TypedValue#TYPE_FLOAT}: |
| 1934 | * {@link Bundle#putCharSequence Bundle.putFloat()} |
| 1935 | * </ul> |
| 1936 | * |
| 1937 | * @param tagName The name of the tag these attributes come from; this is |
| 1938 | * only used for reporting error messages. |
| 1939 | * @param attrs The attributes from which to retrieve the name/value pair. |
| 1940 | * @param outBundle The Bundle in which to place the parsed value. |
| 1941 | * @throws XmlPullParserException If the attributes are not valid. |
| 1942 | */ |
| 1943 | public void parseBundleExtra(String tagName, AttributeSet attrs, |
| 1944 | Bundle outBundle) throws XmlPullParserException { |
| 1945 | TypedArray sa = obtainAttributes(attrs, |
| 1946 | com.android.internal.R.styleable.Extra); |
| 1947 | |
| 1948 | String name = sa.getString( |
| 1949 | com.android.internal.R.styleable.Extra_name); |
| 1950 | if (name == null) { |
| 1951 | sa.recycle(); |
| 1952 | throw new XmlPullParserException("<" + tagName |
| 1953 | + "> requires an android:name attribute at " |
| 1954 | + attrs.getPositionDescription()); |
| 1955 | } |
| 1956 | |
| 1957 | TypedValue v = sa.peekValue( |
| 1958 | com.android.internal.R.styleable.Extra_value); |
| 1959 | if (v != null) { |
| 1960 | if (v.type == TypedValue.TYPE_STRING) { |
| 1961 | CharSequence cs = v.coerceToString(); |
| 1962 | outBundle.putCharSequence(name, cs); |
| 1963 | } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) { |
| 1964 | outBundle.putBoolean(name, v.data != 0); |
| 1965 | } else if (v.type >= TypedValue.TYPE_FIRST_INT |
| 1966 | && v.type <= TypedValue.TYPE_LAST_INT) { |
| 1967 | outBundle.putInt(name, v.data); |
| 1968 | } else if (v.type == TypedValue.TYPE_FLOAT) { |
| 1969 | outBundle.putFloat(name, v.getFloat()); |
| 1970 | } else { |
| 1971 | sa.recycle(); |
| 1972 | throw new XmlPullParserException("<" + tagName |
| 1973 | + "> only supports string, integer, float, color, and boolean at " |
| 1974 | + attrs.getPositionDescription()); |
| 1975 | } |
| 1976 | } else { |
| 1977 | sa.recycle(); |
| 1978 | throw new XmlPullParserException("<" + tagName |
| 1979 | + "> requires an android:value or android:resource attribute at " |
| 1980 | + attrs.getPositionDescription()); |
| 1981 | } |
| 1982 | |
| 1983 | sa.recycle(); |
| 1984 | } |
| 1985 | |
| 1986 | /** |
| 1987 | * Retrieve underlying AssetManager storage for these resources. |
| 1988 | */ |
| 1989 | public final AssetManager getAssets() { |
| 1990 | return mAssets; |
| 1991 | } |
| 1992 | |
| 1993 | /** |
| 1994 | * Call this to remove all cached loaded layout resources from the |
| 1995 | * Resources object. Only intended for use with performance testing |
| 1996 | * tools. |
| 1997 | */ |
| 1998 | public final void flushLayoutCache() { |
| 1999 | synchronized (mCachedXmlBlockIds) { |
| 2000 | // First see if this block is in our cache. |
| 2001 | final int num = mCachedXmlBlockIds.length; |
| 2002 | for (int i=0; i<num; i++) { |
| 2003 | mCachedXmlBlockIds[i] = -0; |
| 2004 | XmlBlock oldBlock = mCachedXmlBlocks[i]; |
| 2005 | if (oldBlock != null) { |
| 2006 | oldBlock.close(); |
| 2007 | } |
| 2008 | mCachedXmlBlocks[i] = null; |
| 2009 | } |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | /** |
| 2014 | * Start preloading of resource data using this Resources object. Only |
| 2015 | * for use by the zygote process for loading common system resources. |
| 2016 | * {@hide} |
| 2017 | */ |
| 2018 | public final void startPreloading() { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 2019 | synchronized (sSync) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2020 | if (sPreloaded) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2021 | throw new IllegalStateException("Resources already preloaded"); |
| 2022 | } |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2023 | sPreloaded = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2024 | mPreloading = true; |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2025 | sPreloadedDensity = DisplayMetrics.DENSITY_DEVICE; |
| 2026 | mConfiguration.densityDpi = sPreloadedDensity; |
| 2027 | updateConfiguration(null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | /** |
| 2032 | * Called by zygote when it is done preloading resources, to change back |
| 2033 | * to normal Resources operation. |
| 2034 | */ |
| 2035 | public final void finishPreloading() { |
| 2036 | if (mPreloading) { |
| 2037 | mPreloading = false; |
| 2038 | flushLayoutCache(); |
| 2039 | } |
| 2040 | } |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2041 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2042 | /** |
| 2043 | * @hide |
| 2044 | */ |
| 2045 | public LongSparseArray<Drawable.ConstantState> getPreloadedDrawables() { |
| 2046 | return sPreloadedDrawables[0]; |
| 2047 | } |
| 2048 | |
Dianne Hackborn | f1ae269 | 2013-04-19 14:09:37 -0700 | [diff] [blame] | 2049 | private boolean verifyPreloadConfig(int changingConfigurations, int allowVarying, |
| 2050 | int resourceId, String name) { |
| 2051 | // We allow preloading of resources even if they vary by font scale (which |
| 2052 | // doesn't impact resource selection) or density (which we handle specially by |
| 2053 | // simply turning off all preloading), as well as any other configs specified |
| 2054 | // by the caller. |
Fabrice Di Meglio | dc25d25 | 2013-04-09 18:04:29 -0700 | [diff] [blame] | 2055 | if (((changingConfigurations&~(ActivityInfo.CONFIG_FONT_SCALE | |
Dianne Hackborn | f1ae269 | 2013-04-19 14:09:37 -0700 | [diff] [blame] | 2056 | ActivityInfo.CONFIG_DENSITY)) & ~allowVarying) != 0) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2057 | String resName; |
| 2058 | try { |
Fabrice Di Meglio | dc25d25 | 2013-04-09 18:04:29 -0700 | [diff] [blame] | 2059 | resName = getResourceName(resourceId); |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2060 | } catch (NotFoundException e) { |
| 2061 | resName = "?"; |
| 2062 | } |
| 2063 | Log.w(TAG, "Preloaded " + name + " resource #0x" |
Fabrice Di Meglio | dc25d25 | 2013-04-09 18:04:29 -0700 | [diff] [blame] | 2064 | + Integer.toHexString(resourceId) |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2065 | + " (" + resName + ") that varies with configuration!!"); |
| 2066 | return false; |
| 2067 | } |
Fabrice Di Meglio | b9a13b8 | 2013-04-15 14:05:30 -0700 | [diff] [blame] | 2068 | if (TRACE_FOR_PRELOAD) { |
| 2069 | String resName; |
| 2070 | try { |
| 2071 | resName = getResourceName(resourceId); |
| 2072 | } catch (NotFoundException e) { |
| 2073 | resName = "?"; |
| 2074 | } |
| 2075 | Log.w(TAG, "Preloading " + name + " resource #0x" |
| 2076 | + Integer.toHexString(resourceId) |
| 2077 | + " (" + resName + ")"); |
| 2078 | } |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2079 | return true; |
| 2080 | } |
| 2081 | |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 2082 | /*package*/ Drawable loadDrawable(TypedValue value, int id, Theme theme) throws NotFoundException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2083 | if (TRACE_FOR_PRELOAD) { |
| 2084 | // Log only framework resources |
| 2085 | if ((id >>> 24) == 0x1) { |
| 2086 | final String name = getResourceName(id); |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 2087 | if (name != null) { |
| 2088 | Log.d("PreloadDrawable", name); |
| 2089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 2093 | boolean isColorDrawable = false; |
| 2094 | if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && |
| 2095 | value.type <= TypedValue.TYPE_LAST_COLOR_INT) { |
| 2096 | isColorDrawable = true; |
| 2097 | } |
Romain Guy | 5f49c30 | 2012-09-06 16:33:31 -0700 | [diff] [blame] | 2098 | final long key = isColorDrawable ? value.data : |
| 2099 | (((long) value.assetCookie) << 32) | value.data; |
| 2100 | |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 2101 | Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2102 | |
| 2103 | if (dr != null) { |
| 2104 | return dr; |
| 2105 | } |
Dianne Hackborn | f1ae269 | 2013-04-19 14:09:37 -0700 | [diff] [blame] | 2106 | Drawable.ConstantState cs; |
| 2107 | if (isColorDrawable) { |
| 2108 | cs = sPreloadedColorDrawables.get(key); |
| 2109 | } else { |
| 2110 | cs = sPreloadedDrawables[mConfiguration.getLayoutDirection()].get(key); |
| 2111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2112 | if (cs != null) { |
Dianne Hackborn | c297480 | 2009-09-13 18:22:19 -0700 | [diff] [blame] | 2113 | dr = cs.newDrawable(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2114 | } else { |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 2115 | if (isColorDrawable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2116 | dr = new ColorDrawable(value.data); |
| 2117 | } |
| 2118 | |
| 2119 | if (dr == null) { |
| 2120 | if (value.string == null) { |
| 2121 | throw new NotFoundException( |
| 2122 | "Resource is not a Drawable (color or path): " + value); |
| 2123 | } |
| 2124 | |
| 2125 | String file = value.string.toString(); |
| 2126 | |
Dianne Hackborn | 9b44aae | 2011-09-02 19:17:16 -0700 | [diff] [blame] | 2127 | if (TRACE_FOR_MISS_PRELOAD) { |
| 2128 | // Log only framework resources |
| 2129 | if ((id >>> 24) == 0x1) { |
| 2130 | final String name = getResourceName(id); |
| 2131 | if (name != null) android.util.Log.d(TAG, "Loading framework drawable #" |
| 2132 | + Integer.toHexString(id) + ": " + name |
| 2133 | + " at " + file); |
| 2134 | } |
| 2135 | } |
| 2136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2137 | if (DEBUG_LOAD) Log.v(TAG, "Loading drawable for cookie " |
| 2138 | + value.assetCookie + ": " + file); |
| 2139 | |
| 2140 | if (file.endsWith(".xml")) { |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2141 | Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2142 | try { |
| 2143 | XmlResourceParser rp = loadXmlResourceParser( |
| 2144 | file, id, value.assetCookie, "drawable"); |
| 2145 | dr = Drawable.createFromXml(this, rp); |
| 2146 | rp.close(); |
| 2147 | } catch (Exception e) { |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2148 | Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2149 | NotFoundException rnf = new NotFoundException( |
| 2150 | "File " + file + " from drawable resource ID #0x" |
| 2151 | + Integer.toHexString(id)); |
| 2152 | rnf.initCause(e); |
| 2153 | throw rnf; |
| 2154 | } |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2155 | Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2156 | |
| 2157 | } else { |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2158 | Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2159 | try { |
| 2160 | InputStream is = mAssets.openNonAsset( |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 2161 | value.assetCookie, file, AssetManager.ACCESS_STREAMING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2162 | // System.out.println("Opened file " + file + ": " + is); |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 2163 | dr = Drawable.createFromResourceStream(this, value, is, |
| 2164 | file, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2165 | is.close(); |
| 2166 | // System.out.println("Created stream: " + dr); |
| 2167 | } catch (Exception e) { |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2168 | Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2169 | NotFoundException rnf = new NotFoundException( |
| 2170 | "File " + file + " from drawable resource ID #0x" |
| 2171 | + Integer.toHexString(id)); |
| 2172 | rnf.initCause(e); |
| 2173 | throw rnf; |
| 2174 | } |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2175 | Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | if (dr != null) { |
| 2181 | dr.setChangingConfigurations(value.changingConfigurations); |
| 2182 | cs = dr.getConstantState(); |
| 2183 | if (cs != null) { |
| 2184 | if (mPreloading) { |
Dianne Hackborn | f1ae269 | 2013-04-19 14:09:37 -0700 | [diff] [blame] | 2185 | final int changingConfigs = cs.getChangingConfigurations(); |
| 2186 | if (isColorDrawable) { |
| 2187 | if (verifyPreloadConfig(changingConfigs, 0, value.resourceId, |
| 2188 | "drawable")) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2189 | sPreloadedColorDrawables.put(key, cs); |
Dianne Hackborn | f1ae269 | 2013-04-19 14:09:37 -0700 | [diff] [blame] | 2190 | } |
| 2191 | } else { |
| 2192 | if (verifyPreloadConfig(changingConfigs, |
| 2193 | LAYOUT_DIR_CONFIG, value.resourceId, "drawable")) { |
| 2194 | if ((changingConfigs&LAYOUT_DIR_CONFIG) == 0) { |
| 2195 | // If this resource does not vary based on layout direction, |
| 2196 | // we can put it in all of the preload maps. |
| 2197 | sPreloadedDrawables[0].put(key, cs); |
| 2198 | sPreloadedDrawables[1].put(key, cs); |
| 2199 | } else { |
| 2200 | // Otherwise, only in the layout dir we loaded it for. |
| 2201 | final LongSparseArray<Drawable.ConstantState> preloads |
| 2202 | = sPreloadedDrawables[mConfiguration.getLayoutDirection()]; |
| 2203 | preloads.put(key, cs); |
| 2204 | } |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2205 | } |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 2206 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2207 | } else { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 2208 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2209 | //Log.i(TAG, "Saving cached drawable @ #" + |
| 2210 | // Integer.toHexString(key.intValue()) |
| 2211 | // + " in " + this + ": " + cs); |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 2212 | if (isColorDrawable) { |
| 2213 | mColorDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs)); |
| 2214 | } else { |
| 2215 | mDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs)); |
| 2216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | return dr; |
| 2223 | } |
| 2224 | |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 2225 | private Drawable getCachedDrawable( |
| 2226 | LongSparseArray<WeakReference<ConstantState>> drawableCache, |
| 2227 | long key) { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 2228 | synchronized (mAccessLock) { |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 2229 | WeakReference<Drawable.ConstantState> wr = drawableCache.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2230 | if (wr != null) { // we have the key |
| 2231 | Drawable.ConstantState entry = wr.get(); |
| 2232 | if (entry != null) { |
| 2233 | //Log.i(TAG, "Returning cached drawable @ #" + |
| 2234 | // Integer.toHexString(((Integer)key).intValue()) |
| 2235 | // + " in " + this + ": " + entry); |
Masanori Ogino | 708654c | 2010-07-20 18:55:51 +0900 | [diff] [blame] | 2236 | return entry.newDrawable(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | } |
| 2238 | else { // our entry has been purged |
Masanori Ogino | c7d9d27 | 2010-07-10 12:10:41 +0900 | [diff] [blame] | 2239 | drawableCache.delete(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | return null; |
| 2244 | } |
| 2245 | |
| 2246 | /*package*/ ColorStateList loadColorStateList(TypedValue value, int id) |
| 2247 | throws NotFoundException { |
| 2248 | if (TRACE_FOR_PRELOAD) { |
| 2249 | // Log only framework resources |
| 2250 | if ((id >>> 24) == 0x1) { |
| 2251 | final String name = getResourceName(id); |
| 2252 | if (name != null) android.util.Log.d("PreloadColorStateList", name); |
| 2253 | } |
| 2254 | } |
| 2255 | |
Romain Guy | 5d911c3 | 2012-04-12 16:25:17 -0700 | [diff] [blame] | 2256 | final long key = (((long) value.assetCookie) << 32) | value.data; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2257 | |
| 2258 | ColorStateList csl; |
| 2259 | |
| 2260 | if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && |
| 2261 | value.type <= TypedValue.TYPE_LAST_COLOR_INT) { |
| 2262 | |
Romain Guy | 5d911c3 | 2012-04-12 16:25:17 -0700 | [diff] [blame] | 2263 | csl = sPreloadedColorStateLists.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2264 | if (csl != null) { |
| 2265 | return csl; |
| 2266 | } |
| 2267 | |
| 2268 | csl = ColorStateList.valueOf(value.data); |
| 2269 | if (mPreloading) { |
Dianne Hackborn | f1ae269 | 2013-04-19 14:09:37 -0700 | [diff] [blame] | 2270 | if (verifyPreloadConfig(value.changingConfigurations, 0, value.resourceId, |
| 2271 | "color")) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2272 | sPreloadedColorStateLists.put(key, csl); |
| 2273 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | return csl; |
| 2277 | } |
| 2278 | |
| 2279 | csl = getCachedColorStateList(key); |
| 2280 | if (csl != null) { |
| 2281 | return csl; |
| 2282 | } |
| 2283 | |
Romain Guy | 5d911c3 | 2012-04-12 16:25:17 -0700 | [diff] [blame] | 2284 | csl = sPreloadedColorStateLists.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2285 | if (csl != null) { |
| 2286 | return csl; |
| 2287 | } |
| 2288 | |
| 2289 | if (value.string == null) { |
| 2290 | throw new NotFoundException( |
| 2291 | "Resource is not a ColorStateList (color or path): " + value); |
| 2292 | } |
| 2293 | |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 2294 | final String file = value.string.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2295 | |
| 2296 | if (file.endsWith(".xml")) { |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2297 | Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2298 | try { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 2299 | final XmlResourceParser rp = loadXmlResourceParser( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2300 | file, id, value.assetCookie, "colorstatelist"); |
| 2301 | csl = ColorStateList.createFromXml(this, rp); |
| 2302 | rp.close(); |
| 2303 | } catch (Exception e) { |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2304 | Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2305 | NotFoundException rnf = new NotFoundException( |
| 2306 | "File " + file + " from color state list resource ID #0x" |
| 2307 | + Integer.toHexString(id)); |
| 2308 | rnf.initCause(e); |
| 2309 | throw rnf; |
| 2310 | } |
Dianne Hackborn | 0acc193 | 2013-04-12 17:49:17 -0700 | [diff] [blame] | 2311 | Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2312 | } else { |
| 2313 | throw new NotFoundException( |
| 2314 | "File " + file + " from drawable resource ID #0x" |
| 2315 | + Integer.toHexString(id) + ": .xml extension required"); |
| 2316 | } |
| 2317 | |
| 2318 | if (csl != null) { |
| 2319 | if (mPreloading) { |
Dianne Hackborn | f1ae269 | 2013-04-19 14:09:37 -0700 | [diff] [blame] | 2320 | if (verifyPreloadConfig(value.changingConfigurations, 0, value.resourceId, |
| 2321 | "color")) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 2322 | sPreloadedColorStateLists.put(key, csl); |
| 2323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2324 | } else { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 2325 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2326 | //Log.i(TAG, "Saving cached color state list @ #" + |
| 2327 | // Integer.toHexString(key.intValue()) |
| 2328 | // + " in " + this + ": " + csl); |
Romain Guy | 5d911c3 | 2012-04-12 16:25:17 -0700 | [diff] [blame] | 2329 | mColorStateListCache.put(key, new WeakReference<ColorStateList>(csl)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | return csl; |
| 2335 | } |
| 2336 | |
Romain Guy | 5d911c3 | 2012-04-12 16:25:17 -0700 | [diff] [blame] | 2337 | private ColorStateList getCachedColorStateList(long key) { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 2338 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2339 | WeakReference<ColorStateList> wr = mColorStateListCache.get(key); |
| 2340 | if (wr != null) { // we have the key |
| 2341 | ColorStateList entry = wr.get(); |
| 2342 | if (entry != null) { |
| 2343 | //Log.i(TAG, "Returning cached color state list @ #" + |
| 2344 | // Integer.toHexString(((Integer)key).intValue()) |
| 2345 | // + " in " + this + ": " + entry); |
| 2346 | return entry; |
Romain Guy | 5d911c3 | 2012-04-12 16:25:17 -0700 | [diff] [blame] | 2347 | } else { // our entry has been purged |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | mColorStateListCache.delete(key); |
| 2349 | } |
| 2350 | } |
| 2351 | } |
| 2352 | return null; |
| 2353 | } |
| 2354 | |
| 2355 | /*package*/ XmlResourceParser loadXmlResourceParser(int id, String type) |
| 2356 | throws NotFoundException { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 2357 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2358 | TypedValue value = mTmpValue; |
Dianne Hackborn | e5b50a6 | 2013-02-11 16:18:42 -0800 | [diff] [blame] | 2359 | if (value == null) { |
| 2360 | mTmpValue = value = new TypedValue(); |
| 2361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2362 | getValue(id, value, true); |
| 2363 | if (value.type == TypedValue.TYPE_STRING) { |
| 2364 | return loadXmlResourceParser(value.string.toString(), id, |
| 2365 | value.assetCookie, type); |
| 2366 | } |
| 2367 | throw new NotFoundException( |
| 2368 | "Resource ID #0x" + Integer.toHexString(id) + " type #0x" |
| 2369 | + Integer.toHexString(value.type) + " is not valid"); |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | /*package*/ XmlResourceParser loadXmlResourceParser(String file, int id, |
| 2374 | int assetCookie, String type) throws NotFoundException { |
| 2375 | if (id != 0) { |
| 2376 | try { |
| 2377 | // These may be compiled... |
| 2378 | synchronized (mCachedXmlBlockIds) { |
| 2379 | // First see if this block is in our cache. |
| 2380 | final int num = mCachedXmlBlockIds.length; |
| 2381 | for (int i=0; i<num; i++) { |
| 2382 | if (mCachedXmlBlockIds[i] == id) { |
| 2383 | //System.out.println("**** REUSING XML BLOCK! id=" |
| 2384 | // + id + ", index=" + i); |
| 2385 | return mCachedXmlBlocks[i].newParser(); |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | // Not in the cache, create a new block and put it at |
| 2390 | // the next slot in the cache. |
| 2391 | XmlBlock block = mAssets.openXmlBlockAsset( |
| 2392 | assetCookie, file); |
| 2393 | if (block != null) { |
| 2394 | int pos = mLastCachedXmlBlockIndex+1; |
| 2395 | if (pos >= num) pos = 0; |
| 2396 | mLastCachedXmlBlockIndex = pos; |
| 2397 | XmlBlock oldBlock = mCachedXmlBlocks[pos]; |
| 2398 | if (oldBlock != null) { |
| 2399 | oldBlock.close(); |
| 2400 | } |
| 2401 | mCachedXmlBlockIds[pos] = id; |
| 2402 | mCachedXmlBlocks[pos] = block; |
| 2403 | //System.out.println("**** CACHING NEW XML BLOCK! id=" |
| 2404 | // + id + ", index=" + pos); |
| 2405 | return block.newParser(); |
| 2406 | } |
| 2407 | } |
| 2408 | } catch (Exception e) { |
| 2409 | NotFoundException rnf = new NotFoundException( |
| 2410 | "File " + file + " from xml type " + type + " resource ID #0x" |
| 2411 | + Integer.toHexString(id)); |
| 2412 | rnf.initCause(e); |
| 2413 | throw rnf; |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | throw new NotFoundException( |
| 2418 | "File " + file + " from xml type " + type + " resource ID #0x" |
| 2419 | + Integer.toHexString(id)); |
| 2420 | } |
| 2421 | |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 2422 | /*package*/ void recycleCachedStyledAttributes(TypedArray attrs) { |
| 2423 | synchronized (mAccessLock) { |
| 2424 | final TypedArray cached = mCachedStyledAttributes; |
| 2425 | if (cached == null || cached.mData.length < attrs.mData.length) { |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 2426 | mCachedStyledAttributes = attrs; |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2431 | private TypedArray getCachedStyledAttributes(int len) { |
Dianne Hackborn | 50707cc | 2013-02-08 15:32:05 -0800 | [diff] [blame] | 2432 | synchronized (mAccessLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2433 | TypedArray attrs = mCachedStyledAttributes; |
| 2434 | if (attrs != null) { |
| 2435 | mCachedStyledAttributes = null; |
Dianne Hackborn | ab0f485 | 2011-09-12 16:59:06 -0700 | [diff] [blame] | 2436 | if (DEBUG_ATTRIBUTES_CACHE) { |
| 2437 | mLastRetrievedAttrs = new RuntimeException("here"); |
| 2438 | mLastRetrievedAttrs.fillInStackTrace(); |
| 2439 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2440 | |
| 2441 | attrs.mLength = len; |
| 2442 | int fullLen = len * AssetManager.STYLE_NUM_ENTRIES; |
| 2443 | if (attrs.mData.length >= fullLen) { |
| 2444 | return attrs; |
| 2445 | } |
| 2446 | attrs.mData = new int[fullLen]; |
| 2447 | attrs.mIndices = new int[1+len]; |
| 2448 | return attrs; |
| 2449 | } |
Dianne Hackborn | ab0f485 | 2011-09-12 16:59:06 -0700 | [diff] [blame] | 2450 | if (DEBUG_ATTRIBUTES_CACHE) { |
| 2451 | RuntimeException here = new RuntimeException("here"); |
| 2452 | here.fillInStackTrace(); |
| 2453 | if (mLastRetrievedAttrs != null) { |
| 2454 | Log.i(TAG, "Allocated new TypedArray of " + len + " in " + this, here); |
| 2455 | Log.i(TAG, "Last retrieved attributes here", mLastRetrievedAttrs); |
| 2456 | } |
| 2457 | mLastRetrievedAttrs = here; |
| 2458 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2459 | return new TypedArray(this, |
| 2460 | new int[len*AssetManager.STYLE_NUM_ENTRIES], |
| 2461 | new int[1+len], len); |
| 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | private Resources() { |
| 2466 | mAssets = AssetManager.getSystem(); |
| 2467 | // NOTE: Intentionally leaving this uninitialized (all values set |
| 2468 | // to zero), so that anyone who tries to do something that requires |
| 2469 | // metrics will get a very wrong value. |
| 2470 | mConfiguration.setToDefaults(); |
| 2471 | mMetrics.setToDefaults(); |
| 2472 | updateConfiguration(null, null); |
| 2473 | mAssets.ensureStringBlocks(); |
| 2474 | } |
| 2475 | } |