Resources.java revision 21fc8ba39c4799a346caf95218d2bd60ee42ca1a
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.res;
18
19import com.android.internal.util.XmlUtils;
20
21import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23
24import android.content.pm.ActivityInfo;
25import android.graphics.Movie;
26import android.graphics.drawable.Drawable;
27import android.graphics.drawable.ColorDrawable;
28import android.graphics.drawable.Drawable.ConstantState;
29import android.os.Build;
30import android.os.Bundle;
31import android.os.IBinder;
32import android.os.Trace;
33import android.util.AttributeSet;
34import android.util.DisplayMetrics;
35import android.util.Log;
36import android.util.Slog;
37import android.util.TypedValue;
38import android.util.LongSparseArray;
39
40import java.io.IOException;
41import java.io.InputStream;
42import java.lang.ref.WeakReference;
43import java.util.Locale;
44
45import libcore.icu.NativePluralRules;
46
47/**
48 * Class for accessing an application's resources.  This sits on top of the
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&mdash;by providing sets of alternative
65 * resources&mdash;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>
71 */
72public class Resources {
73    static final String TAG = "Resources";
74
75    private static final boolean DEBUG_LOAD = false;
76    private static final boolean DEBUG_CONFIG = false;
77    private static final boolean DEBUG_ATTRIBUTES_CACHE = false;
78    private static final boolean TRACE_FOR_PRELOAD = false;
79    private static final boolean TRACE_FOR_MISS_PRELOAD = false;
80
81    private static final int LAYOUT_DIR_CONFIG = ActivityInfo.activityInfoConfigToNative(
82            ActivityInfo.CONFIG_LAYOUT_DIRECTION);
83
84    private static final int ID_OTHER = 0x01000004;
85
86    private static final Object sSync = new Object();
87
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.
91    private static final LongSparseArray<Drawable.ConstantState>[] sPreloadedDrawables;
92    private static final LongSparseArray<Drawable.ConstantState> sPreloadedColorDrawables
93            = new LongSparseArray<Drawable.ConstantState>();
94    private static final LongSparseArray<ColorStateList> sPreloadedColorStateLists
95            = new LongSparseArray<ColorStateList>();
96
97    // Used by BridgeResources in layoutlib
98    static Resources mSystem = null;
99
100    private static boolean sPreloaded;
101    private static int sPreloadedDensity;
102
103    // These are protected by mAccessLock.
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);
112
113    private TypedValue mTmpValue = new TypedValue();
114    private boolean mPreloading;
115
116    private TypedArray mCachedStyledAttributes = null;
117    private RuntimeException mLastRetrievedAttrs = null;
118
119    private int mLastCachedXmlBlockIndex = -1;
120    private final int[] mCachedXmlBlockIds = { 0, 0, 0, 0 };
121    private final XmlBlock[] mCachedXmlBlocks = new XmlBlock[4];
122
123    private final AssetManager mAssets;
124    private final Configuration mConfiguration = new Configuration();
125    private final DisplayMetrics mMetrics = new DisplayMetrics();
126    private NativePluralRules mPluralRule;
127
128    private CompatibilityInfo mCompatibilityInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
129    private WeakReference<IBinder> mToken;
130
131    static {
132        sPreloadedDrawables = new LongSparseArray[2];
133        sPreloadedDrawables[0] = new LongSparseArray<Drawable.ConstantState>();
134        sPreloadedDrawables[1] = new LongSparseArray<Drawable.ConstantState>();
135    }
136
137    /** @hide */
138    public static int selectDefaultTheme(int curTheme, int targetSdkVersion) {
139        return selectSystemTheme(curTheme, targetSdkVersion,
140                com.android.internal.R.style.Theme,
141                com.android.internal.R.style.Theme_Holo,
142                com.android.internal.R.style.Theme_DeviceDefault);
143    }
144
145    /** @hide */
146    public static int selectSystemTheme(int curTheme, int targetSdkVersion,
147            int orig, int holo, int deviceDefault) {
148        if (curTheme != 0) {
149            return curTheme;
150        }
151        if (targetSdkVersion < Build.VERSION_CODES.HONEYCOMB) {
152            return orig;
153        }
154        if (targetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
155            return holo;
156        }
157        return deviceDefault;
158    }
159
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     */
183    public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) {
184        this(assets, metrics, config, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
185    }
186
187    /**
188     * Creates a new Resources object with CompatibilityInfo.
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).
195     * @param compatInfo this resource's compatibility info. Must not be null.
196     * @param token The Activity token for determining stack affiliation. Usually null.
197     * @hide
198     */
199    public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config,
200            CompatibilityInfo compatInfo, IBinder token) {
201        mAssets = assets;
202        mMetrics.setToDefaults();
203        if (compatInfo != null) {
204            mCompatibilityInfo = compatInfo;
205        }
206        mToken = new WeakReference<IBinder>(token);
207        updateConfiguration(config, metrics);
208        assets.ensureStringBlocks();
209    }
210
211    /**
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() {
218        synchronized (sSync) {
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    /**
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.
261     *
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.
265     * @param quantity The number used to get the correct string for the current language's
266     *           plural rules.
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 {
274        NativePluralRules rule = getPluralRule();
275        CharSequence res = mAssets.getResourceBagText(id,
276                attrForQuantityCode(rule.quantityForInt(quantity)));
277        if (res != null) {
278            return res;
279        }
280        res = mAssets.getResourceBagText(id, ID_OTHER);
281        if (res != null) {
282            return res;
283        }
284        throw new NotFoundException("Plural resource ID #0x" + Integer.toHexString(id)
285                + " quantity=" + quantity
286                + " item=" + stringForQuantityCode(rule.quantityForInt(quantity)));
287    }
288
289    private NativePluralRules getPluralRule() {
290        synchronized (sSync) {
291            if (mPluralRule == null) {
292                mPluralRule = NativePluralRules.forLocale(mConfiguration.locale);
293            }
294            return mPluralRule;
295        }
296    }
297
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
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    /**
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.
375     *
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.
379     *
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    /**
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.
406     *
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 {
546        synchronized (mAccessLock) {
547            TypedValue value = mTmpValue;
548            if (value == null) {
549                mTmpValue = value = new TypedValue();
550            }
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 {
581        synchronized (mAccessLock) {
582            TypedValue value = mTmpValue;
583            if (value == null) {
584                mTmpValue = value = new TypedValue();
585            }
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 {
618        synchronized (mAccessLock) {
619            TypedValue value = mTmpValue;
620            if (value == null) {
621                mTmpValue = value = new TypedValue();
622            }
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) {
652        synchronized (mAccessLock) {
653            TypedValue value = mTmpValue;
654            if (value == null) {
655                mTmpValue = value = new TypedValue();
656            }
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.
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     *
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.
689     * @return Drawable An object that can be used to draw this resource.
690     * @throws NotFoundException Throws NotFoundException if the given ID does
691     *         not exist.
692     */
693    public Drawable getDrawable(int id) throws NotFoundException {
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 {
710        TypedValue value;
711        synchronized (mAccessLock) {
712            value = mTmpValue;
713            if (value == null) {
714                value = new TypedValue();
715            } else {
716                mTmpValue = null;
717            }
718            getValue(id, value, true);
719        }
720        final Drawable res = loadDrawable(value, id, theme);
721        synchronized (mAccessLock) {
722            if (mTmpValue == null) {
723                mTmpValue = value;
724            }
725        }
726        return res;
727    }
728
729    /**
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.
738     *
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}.
744     * @return Drawable An object that can be used to draw this resource.
745     * @throws NotFoundException Throws NotFoundException if the given ID does
746     *             not exist.
747     * @see #getDrawableForDensity(int, int, Theme)
748     */
749    public Drawable getDrawableForDensity(int id, int density) throws NotFoundException {
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) {
768        TypedValue value;
769        synchronized (mAccessLock) {
770            value = mTmpValue;
771            if (value == null) {
772                value = new TypedValue();
773            } else {
774                mTmpValue = null;
775            }
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) {
787                    value.density = mMetrics.densityDpi;
788                } else {
789                    value.density = (value.density * mMetrics.densityDpi) / density;
790                }
791            }
792        }
793
794        final Drawable res = loadDrawable(value, id, theme);
795        synchronized (mAccessLock) {
796            if (mTmpValue == null) {
797                mTmpValue = value;
798            }
799        }
800        return res;
801    }
802
803    /**
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 {
838        TypedValue value;
839        synchronized (mAccessLock) {
840            value = mTmpValue;
841            if (value == null) {
842                value = new TypedValue();
843            }
844            getValue(id, value, true);
845            if (value.type >= TypedValue.TYPE_FIRST_INT
846                && value.type <= TypedValue.TYPE_LAST_INT) {
847                mTmpValue = value;
848                return value.data;
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");
853            }
854            mTmpValue = null;
855        }
856        ColorStateList csl = loadColorStateList(value, id);
857        synchronized (mAccessLock) {
858            if (mTmpValue == null) {
859                mTmpValue = value;
860            }
861        }
862        return csl.getDefaultColor();
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 {
880        TypedValue value;
881        synchronized (mAccessLock) {
882            value = mTmpValue;
883            if (value == null) {
884                value = new TypedValue();
885            } else {
886                mTmpValue = null;
887            }
888            getValue(id, value, true);
889        }
890        ColorStateList res = loadColorStateList(value, id);
891        synchronized (mAccessLock) {
892            if (mTmpValue == null) {
893                mTmpValue = value;
894            }
895        }
896        return res;
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 {
913        synchronized (mAccessLock) {
914            TypedValue value = mTmpValue;
915            if (value == null) {
916                mTmpValue = value = new TypedValue();
917            }
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 {
941        synchronized (mAccessLock) {
942            TypedValue value = mTmpValue;
943            if (value == null) {
944                mTmpValue = value = new TypedValue();
945            }
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 {
1045        TypedValue value;
1046        synchronized (mAccessLock) {
1047            value = mTmpValue;
1048            if (value == null) {
1049                value = new TypedValue();
1050            } else {
1051                mTmpValue = null;
1052            }
1053        }
1054        InputStream res = openRawResource(id, value);
1055        synchronized (mAccessLock) {
1056            if (mTmpValue == null) {
1057                mTmpValue = value;
1058            }
1059        }
1060        return res;
1061    }
1062
1063    /**
1064     * Open a data stream for reading a raw resource.  This can only be used
1065     * with resources whose value is the name of an asset file -- that is, it can be
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.
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 {
1112        TypedValue value;
1113        synchronized (mAccessLock) {
1114            value = mTmpValue;
1115            if (value == null) {
1116                value = new TypedValue();
1117            } else {
1118                mTmpValue = null;
1119            }
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            }
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 {
1158        boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs);
1159        if (found) {
1160            return;
1161        }
1162        throw new NotFoundException("Resource ID #0x"
1163                                    + Integer.toHexString(id));
1164    }
1165
1166    /**
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)
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    /**
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        /**
1269         * Return a TypedArray holding the values defined by
1270         * <var>Theme</var> which are listed in <var>attrs</var>.
1271         *
1272         * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done
1273         * with the array.
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) {
1288            final int len = attrs.length;
1289            final TypedArray array = getCachedStyledAttributes(len);
1290            array.mTheme = this;
1291            array.mRsrcs = attrs;
1292            AssetManager.applyStyle(mTheme, 0, 0, 0, attrs,
1293                    array.mData, array.mIndices);
1294            return array;
1295        }
1296
1297        /**
1298         * Return a TypedArray holding the values defined by the style
1299         * resource <var>resid</var> which are listed in <var>attrs</var>.
1300         *
1301         * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done
1302         * with the array.
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         */
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;
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        /**
1355         * Return a TypedArray holding the attribute values in
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         *
1361         * <p>Be sure to call {@link TypedArray#recycle() TypedArray.recycle()} when you are done
1362         * with the array.
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>&lt;Button
1379         * textColor="#ff000000"&gt;</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
1387         *                     defaults values for the TypedArray.  Can be
1388         *                     0 to not look for defaults.
1389         * @param defStyleRes A resource identifier of a style resource that
1390         *                    supplies default values for the TypedArray,
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) {
1404            final int len = attrs.length;
1405            final TypedArray array = getCachedStyledAttributes(len);
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).
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);
1414
1415            array.mTheme = this;
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        /**
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        /**
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        }
1505
1506        @Override
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
1517        @SuppressWarnings("hiding")
1518        private final AssetManager mAssets;
1519        private final long mTheme;
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    }
1561
1562    /**
1563     * Store the newly updated configuration.
1564     */
1565    public void updateConfiguration(Configuration config,
1566            DisplayMetrics metrics) {
1567        updateConfiguration(config, metrics, null);
1568    }
1569
1570    /**
1571     * @hide
1572     */
1573    public void updateConfiguration(Configuration config,
1574            DisplayMetrics metrics, CompatibilityInfo compat) {
1575        synchronized (mAccessLock) {
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            }
1582            if (compat != null) {
1583                mCompatibilityInfo = compat;
1584            }
1585            if (metrics != null) {
1586                mMetrics.setTo(metrics);
1587            }
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.
1597            mCompatibilityInfo.applyToDisplayMetrics(mMetrics);
1598
1599            int configChanges = 0xfffffff;
1600            if (config != null) {
1601                mTmpConfig.setTo(config);
1602                int density = config.densityDpi;
1603                if (density == Configuration.DENSITY_DPI_UNDEFINED) {
1604                    density = mMetrics.noncompatDensityDpi;
1605                }
1606
1607                mCompatibilityInfo.applyToConfiguration(density, mTmpConfig);
1608
1609                if (mTmpConfig.locale == null) {
1610                    mTmpConfig.locale = Locale.getDefault();
1611                    mTmpConfig.setLayoutDirection(mTmpConfig.locale);
1612                }
1613                configChanges = mConfiguration.updateFrom(mTmpConfig);
1614                configChanges = ActivityInfo.activityInfoConfigToNative(configChanges);
1615            }
1616            if (mConfiguration.locale == null) {
1617                mConfiguration.locale = Locale.getDefault();
1618                mConfiguration.setLayoutDirection(mConfiguration.locale);
1619            }
1620            if (mConfiguration.densityDpi != Configuration.DENSITY_DPI_UNDEFINED) {
1621                mMetrics.densityDpi = mConfiguration.densityDpi;
1622                mMetrics.density = mConfiguration.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
1623            }
1624            mMetrics.scaledDensity = mMetrics.density * mConfiguration.fontScale;
1625
1626            String locale = null;
1627            if (mConfiguration.locale != null) {
1628                locale = adjustLanguageTag(localeToLanguageTag(mConfiguration.locale));
1629            }
1630            int width, height;
1631            if (mMetrics.widthPixels >= mMetrics.heightPixels) {
1632                width = mMetrics.widthPixels;
1633                height = mMetrics.heightPixels;
1634            } else {
1635                //noinspection SuspiciousNameCombination
1636                width = mMetrics.heightPixels;
1637                //noinspection SuspiciousNameCombination
1638                height = mMetrics.widthPixels;
1639            }
1640            int keyboardHidden = mConfiguration.keyboardHidden;
1641            if (keyboardHidden == Configuration.KEYBOARDHIDDEN_NO
1642                    && mConfiguration.hardKeyboardHidden
1643                            == Configuration.HARDKEYBOARDHIDDEN_YES) {
1644                keyboardHidden = Configuration.KEYBOARDHIDDEN_SOFT;
1645            }
1646            mAssets.setConfiguration(mConfiguration.mcc, mConfiguration.mnc,
1647                    locale, mConfiguration.orientation,
1648                    mConfiguration.touchscreen,
1649                    mConfiguration.densityDpi, mConfiguration.keyboard,
1650                    keyboardHidden, mConfiguration.navigation, width, height,
1651                    mConfiguration.smallestScreenWidthDp,
1652                    mConfiguration.screenWidthDp, mConfiguration.screenHeightDp,
1653                    mConfiguration.screenLayout, mConfiguration.uiMode,
1654                    Build.VERSION.RESOURCES_SDK_INT);
1655
1656            if (DEBUG_CONFIG) {
1657                Slog.i(TAG, "**** Updating config of " + this + ": final config is " + mConfiguration
1658                        + " final compat is " + mCompatibilityInfo);
1659            }
1660
1661            clearDrawableCacheLocked(mDrawableCache, configChanges);
1662            clearDrawableCacheLocked(mColorDrawableCache, configChanges);
1663
1664            mColorStateListCache.clear();
1665
1666            flushLayoutCache();
1667        }
1668        synchronized (sSync) {
1669            if (mPluralRule != null) {
1670                mPluralRule = NativePluralRules.forLocale(config.locale);
1671            }
1672        }
1673    }
1674
1675    private void clearDrawableCacheLocked(
1676            LongSparseArray<WeakReference<ConstantState>> cache,
1677            int configChanges) {
1678        int N = cache.size();
1679        if (DEBUG_CONFIG) {
1680            Log.d(TAG, "Cleaning up drawables config changes: 0x"
1681                    + Integer.toHexString(configChanges));
1682        }
1683        for (int i=0; i<N; i++) {
1684            WeakReference<Drawable.ConstantState> ref = cache.valueAt(i);
1685            if (ref != null) {
1686                Drawable.ConstantState cs = ref.get();
1687                if (cs != null) {
1688                    if (Configuration.needNewResources(
1689                            configChanges, cs.getChangingConfigurations())) {
1690                        if (DEBUG_CONFIG) {
1691                            Log.d(TAG, "FLUSHING #0x"
1692                                    + Long.toHexString(mDrawableCache.keyAt(i))
1693                                    + " / " + cs + " with changes: 0x"
1694                                    + Integer.toHexString(cs.getChangingConfigurations()));
1695                        }
1696                        cache.setValueAt(i, null);
1697                    } else if (DEBUG_CONFIG) {
1698                        Log.d(TAG, "(Keeping #0x"
1699                                + Long.toHexString(cache.keyAt(i))
1700                                + " / " + cs + " with changes: 0x"
1701                                + Integer.toHexString(cs.getChangingConfigurations())
1702                                + ")");
1703                    }
1704                }
1705            }
1706        }
1707    }
1708
1709    // Locale.toLanguageTag() is not available in Java6. LayoutLib overrides
1710    // this method to enable users to use Java6.
1711    private String localeToLanguageTag(Locale locale) {
1712        return locale.toLanguageTag();
1713    }
1714
1715    /**
1716     * {@code Locale.toLanguageTag} will transform the obsolete (and deprecated)
1717     * language codes "in", "ji" and "iw" to "id", "yi" and "he" respectively.
1718     *
1719     * All released versions of android prior to "L" used the deprecated language
1720     * tags, so we will need to support them for backwards compatibility.
1721     *
1722     * Note that this conversion needs to take place *after* the call to
1723     * {@code toLanguageTag} because that will convert all the deprecated codes to
1724     * the new ones, even if they're set manually.
1725     */
1726    private static String adjustLanguageTag(String languageTag) {
1727        final int separator = languageTag.indexOf('-');
1728        final String language;
1729        final String remainder;
1730
1731        if (separator == -1) {
1732            language = languageTag;
1733            remainder = "";
1734        } else {
1735            language = languageTag.substring(0, separator);
1736            remainder = languageTag.substring(separator);
1737        }
1738
1739        if ("id".equals(language)) {
1740            return "in" + remainder;
1741        } else if ("yi".equals(language)) {
1742            return "ji" + remainder;
1743        } else if ("he".equals(language)) {
1744            return "iw" + remainder;
1745        } else {
1746            return languageTag;
1747        }
1748    }
1749
1750    /**
1751     * Update the system resources configuration if they have previously
1752     * been initialized.
1753     *
1754     * @hide
1755     */
1756    public static void updateSystemConfiguration(Configuration config, DisplayMetrics metrics,
1757            CompatibilityInfo compat) {
1758        if (mSystem != null) {
1759            mSystem.updateConfiguration(config, metrics, compat);
1760            //Log.i(TAG, "Updated system resources " + mSystem
1761            //        + ": " + mSystem.getConfiguration());
1762        }
1763    }
1764
1765    /**
1766     * Return the current display metrics that are in effect for this resource
1767     * object.  The returned object should be treated as read-only.
1768     *
1769     * @return The resource's current display metrics.
1770     */
1771    public DisplayMetrics getDisplayMetrics() {
1772        if (DEBUG_CONFIG) Slog.v(TAG, "Returning DisplayMetrics: " + mMetrics.widthPixels
1773                + "x" + mMetrics.heightPixels + " " + mMetrics.density);
1774        return mMetrics;
1775    }
1776
1777    /**
1778     * Return the current configuration that is in effect for this resource
1779     * object.  The returned object should be treated as read-only.
1780     *
1781     * @return The resource's current configuration.
1782     */
1783    public Configuration getConfiguration() {
1784        return mConfiguration;
1785    }
1786
1787    /**
1788     * Return the compatibility mode information for the application.
1789     * The returned object should be treated as read-only.
1790     *
1791     * @return compatibility info.
1792     * @hide
1793     */
1794    public CompatibilityInfo getCompatibilityInfo() {
1795        return mCompatibilityInfo;
1796    }
1797
1798    /**
1799     * This is just for testing.
1800     * @hide
1801     */
1802    public void setCompatibilityInfo(CompatibilityInfo ci) {
1803        if (ci != null) {
1804            mCompatibilityInfo = ci;
1805            updateConfiguration(mConfiguration, mMetrics);
1806        }
1807    }
1808
1809    /**
1810     * Return a resource identifier for the given resource name.  A fully
1811     * qualified resource name is of the form "package:type/entry".  The first
1812     * two components (package and type) are optional if defType and
1813     * defPackage, respectively, are specified here.
1814     *
1815     * <p>Note: use of this function is discouraged.  It is much more
1816     * efficient to retrieve resources by identifier than by name.
1817     *
1818     * @param name The name of the desired resource.
1819     * @param defType Optional default resource type to find, if "type/" is
1820     *                not included in the name.  Can be null to require an
1821     *                explicit type.
1822     * @param defPackage Optional default package to find, if "package:" is
1823     *                   not included in the name.  Can be null to require an
1824     *                   explicit package.
1825     *
1826     * @return int The associated resource identifier.  Returns 0 if no such
1827     *         resource was found.  (0 is not a valid resource ID.)
1828     */
1829    public int getIdentifier(String name, String defType, String defPackage) {
1830        if (name == null) {
1831            throw new NullPointerException("name is null");
1832        }
1833        try {
1834            return Integer.parseInt(name);
1835        } catch (Exception e) {
1836            // Ignore
1837        }
1838        return mAssets.getResourceIdentifier(name, defType, defPackage);
1839    }
1840
1841    /**
1842     * Return true if given resource identifier includes a package.
1843     *
1844     * @hide
1845     */
1846    public static boolean resourceHasPackage(int resid) {
1847        return (resid >>> 24) != 0;
1848    }
1849
1850    /**
1851     * Return the full name for a given resource identifier.  This name is
1852     * a single string of the form "package:type/entry".
1853     *
1854     * @param resid The resource identifier whose name is to be retrieved.
1855     *
1856     * @return A string holding the name of the resource.
1857     *
1858     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
1859     *
1860     * @see #getResourcePackageName
1861     * @see #getResourceTypeName
1862     * @see #getResourceEntryName
1863     */
1864    public String getResourceName(int resid) throws NotFoundException {
1865        String str = mAssets.getResourceName(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 package name for a given resource identifier.
1873     *
1874     * @param resid The resource identifier whose package name is to be
1875     * retrieved.
1876     *
1877     * @return A string holding the package 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 getResourcePackageName(int resid) throws NotFoundException {
1884        String str = mAssets.getResourcePackageName(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     * Return the type name for a given resource identifier.
1892     *
1893     * @param resid The resource identifier whose type name is to be
1894     * retrieved.
1895     *
1896     * @return A string holding the type name of the resource.
1897     *
1898     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
1899     *
1900     * @see #getResourceName
1901     */
1902    public String getResourceTypeName(int resid) throws NotFoundException {
1903        String str = mAssets.getResourceTypeName(resid);
1904        if (str != null) return str;
1905        throw new NotFoundException("Unable to find resource ID #0x"
1906                + Integer.toHexString(resid));
1907    }
1908
1909    /**
1910     * Return the entry name for a given resource identifier.
1911     *
1912     * @param resid The resource identifier whose entry name is to be
1913     * retrieved.
1914     *
1915     * @return A string holding the entry name of the resource.
1916     *
1917     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
1918     *
1919     * @see #getResourceName
1920     */
1921    public String getResourceEntryName(int resid) throws NotFoundException {
1922        String str = mAssets.getResourceEntryName(resid);
1923        if (str != null) return str;
1924        throw new NotFoundException("Unable to find resource ID #0x"
1925                + Integer.toHexString(resid));
1926    }
1927
1928    /**
1929     * Parse a series of {@link android.R.styleable#Extra &lt;extra&gt;} tags from
1930     * an XML file.  You call this when you are at the parent tag of the
1931     * extra tags, and it will return once all of the child tags have been parsed.
1932     * This will call {@link #parseBundleExtra} for each extra tag encountered.
1933     *
1934     * @param parser The parser from which to retrieve the extras.
1935     * @param outBundle A Bundle in which to place all parsed extras.
1936     * @throws XmlPullParserException
1937     * @throws IOException
1938     */
1939    public void parseBundleExtras(XmlResourceParser parser, Bundle outBundle)
1940            throws XmlPullParserException, IOException {
1941        int outerDepth = parser.getDepth();
1942        int type;
1943        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1944               && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1945            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1946                continue;
1947            }
1948
1949            String nodeName = parser.getName();
1950            if (nodeName.equals("extra")) {
1951                parseBundleExtra("extra", parser, outBundle);
1952                XmlUtils.skipCurrentTag(parser);
1953
1954            } else {
1955                XmlUtils.skipCurrentTag(parser);
1956            }
1957        }
1958    }
1959
1960    /**
1961     * Parse a name/value pair out of an XML tag holding that data.  The
1962     * AttributeSet must be holding the data defined by
1963     * {@link android.R.styleable#Extra}.  The following value types are supported:
1964     * <ul>
1965     * <li> {@link TypedValue#TYPE_STRING}:
1966     * {@link Bundle#putCharSequence Bundle.putCharSequence()}
1967     * <li> {@link TypedValue#TYPE_INT_BOOLEAN}:
1968     * {@link Bundle#putCharSequence Bundle.putBoolean()}
1969     * <li> {@link TypedValue#TYPE_FIRST_INT}-{@link TypedValue#TYPE_LAST_INT}:
1970     * {@link Bundle#putCharSequence Bundle.putBoolean()}
1971     * <li> {@link TypedValue#TYPE_FLOAT}:
1972     * {@link Bundle#putCharSequence Bundle.putFloat()}
1973     * </ul>
1974     *
1975     * @param tagName The name of the tag these attributes come from; this is
1976     * only used for reporting error messages.
1977     * @param attrs The attributes from which to retrieve the name/value pair.
1978     * @param outBundle The Bundle in which to place the parsed value.
1979     * @throws XmlPullParserException If the attributes are not valid.
1980     */
1981    public void parseBundleExtra(String tagName, AttributeSet attrs,
1982            Bundle outBundle) throws XmlPullParserException {
1983        TypedArray sa = obtainAttributes(attrs,
1984                com.android.internal.R.styleable.Extra);
1985
1986        String name = sa.getString(
1987                com.android.internal.R.styleable.Extra_name);
1988        if (name == null) {
1989            sa.recycle();
1990            throw new XmlPullParserException("<" + tagName
1991                    + "> requires an android:name attribute at "
1992                    + attrs.getPositionDescription());
1993        }
1994
1995        TypedValue v = sa.peekValue(
1996                com.android.internal.R.styleable.Extra_value);
1997        if (v != null) {
1998            if (v.type == TypedValue.TYPE_STRING) {
1999                CharSequence cs = v.coerceToString();
2000                outBundle.putCharSequence(name, cs);
2001            } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
2002                outBundle.putBoolean(name, v.data != 0);
2003            } else if (v.type >= TypedValue.TYPE_FIRST_INT
2004                    && v.type <= TypedValue.TYPE_LAST_INT) {
2005                outBundle.putInt(name, v.data);
2006            } else if (v.type == TypedValue.TYPE_FLOAT) {
2007                outBundle.putFloat(name, v.getFloat());
2008            } else {
2009                sa.recycle();
2010                throw new XmlPullParserException("<" + tagName
2011                        + "> only supports string, integer, float, color, and boolean at "
2012                        + attrs.getPositionDescription());
2013            }
2014        } else {
2015            sa.recycle();
2016            throw new XmlPullParserException("<" + tagName
2017                    + "> requires an android:value or android:resource attribute at "
2018                    + attrs.getPositionDescription());
2019        }
2020
2021        sa.recycle();
2022    }
2023
2024    /**
2025     * Retrieve underlying AssetManager storage for these resources.
2026     */
2027    public final AssetManager getAssets() {
2028        return mAssets;
2029    }
2030
2031    /**
2032     * Call this to remove all cached loaded layout resources from the
2033     * Resources object.  Only intended for use with performance testing
2034     * tools.
2035     */
2036    public final void flushLayoutCache() {
2037        synchronized (mCachedXmlBlockIds) {
2038            // First see if this block is in our cache.
2039            final int num = mCachedXmlBlockIds.length;
2040            for (int i=0; i<num; i++) {
2041                mCachedXmlBlockIds[i] = -0;
2042                XmlBlock oldBlock = mCachedXmlBlocks[i];
2043                if (oldBlock != null) {
2044                    oldBlock.close();
2045                }
2046                mCachedXmlBlocks[i] = null;
2047            }
2048        }
2049    }
2050
2051    /**
2052     * Start preloading of resource data using this Resources object.  Only
2053     * for use by the zygote process for loading common system resources.
2054     * {@hide}
2055     */
2056    public final void startPreloading() {
2057        synchronized (sSync) {
2058            if (sPreloaded) {
2059                throw new IllegalStateException("Resources already preloaded");
2060            }
2061            sPreloaded = true;
2062            mPreloading = true;
2063            sPreloadedDensity = DisplayMetrics.DENSITY_DEVICE;
2064            mConfiguration.densityDpi = sPreloadedDensity;
2065            updateConfiguration(null, null);
2066        }
2067    }
2068
2069    /**
2070     * Called by zygote when it is done preloading resources, to change back
2071     * to normal Resources operation.
2072     */
2073    public final void finishPreloading() {
2074        if (mPreloading) {
2075            mPreloading = false;
2076            flushLayoutCache();
2077        }
2078    }
2079
2080    /**
2081     * @hide
2082     */
2083    public LongSparseArray<Drawable.ConstantState> getPreloadedDrawables() {
2084        return sPreloadedDrawables[0];
2085    }
2086
2087    private boolean verifyPreloadConfig(int changingConfigurations, int allowVarying,
2088            int resourceId, String name) {
2089        // We allow preloading of resources even if they vary by font scale (which
2090        // doesn't impact resource selection) or density (which we handle specially by
2091        // simply turning off all preloading), as well as any other configs specified
2092        // by the caller.
2093        if (((changingConfigurations&~(ActivityInfo.CONFIG_FONT_SCALE |
2094                ActivityInfo.CONFIG_DENSITY)) & ~allowVarying) != 0) {
2095            String resName;
2096            try {
2097                resName = getResourceName(resourceId);
2098            } catch (NotFoundException e) {
2099                resName = "?";
2100            }
2101            Log.w(TAG, "Preloaded " + name + " resource #0x"
2102                    + Integer.toHexString(resourceId)
2103                    + " (" + resName + ") that varies with configuration!!");
2104            return false;
2105        }
2106        if (TRACE_FOR_PRELOAD) {
2107            String resName;
2108            try {
2109                resName = getResourceName(resourceId);
2110            } catch (NotFoundException e) {
2111                resName = "?";
2112            }
2113            Log.w(TAG, "Preloading " + name + " resource #0x"
2114                    + Integer.toHexString(resourceId)
2115                    + " (" + resName + ")");
2116        }
2117        return true;
2118    }
2119
2120    /*package*/ Drawable loadDrawable(TypedValue value, int id, Theme theme) throws NotFoundException {
2121        if (TRACE_FOR_PRELOAD) {
2122            // Log only framework resources
2123            if ((id >>> 24) == 0x1) {
2124                final String name = getResourceName(id);
2125                if (name != null) {
2126                    Log.d("PreloadDrawable", name);
2127                }
2128            }
2129        }
2130
2131        boolean isColorDrawable = false;
2132        if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
2133                value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
2134            isColorDrawable = true;
2135        }
2136        final long key = isColorDrawable ? value.data :
2137                (((long) value.assetCookie) << 32) | value.data;
2138
2139        Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key);
2140
2141        if (dr != null) {
2142            return dr;
2143        }
2144        Drawable.ConstantState cs;
2145        if (isColorDrawable) {
2146            cs = sPreloadedColorDrawables.get(key);
2147        } else {
2148            cs = sPreloadedDrawables[mConfiguration.getLayoutDirection()].get(key);
2149        }
2150        if (cs != null) {
2151            dr = cs.newDrawable(this);
2152        } else {
2153            if (isColorDrawable) {
2154                dr = new ColorDrawable(value.data);
2155            }
2156
2157            if (dr == null) {
2158                if (value.string == null) {
2159                    throw new NotFoundException(
2160                            "Resource is not a Drawable (color or path): " + value);
2161                }
2162
2163                String file = value.string.toString();
2164
2165                if (TRACE_FOR_MISS_PRELOAD) {
2166                    // Log only framework resources
2167                    if ((id >>> 24) == 0x1) {
2168                        final String name = getResourceName(id);
2169                        if (name != null) android.util.Log.d(TAG, "Loading framework drawable #"
2170                                + Integer.toHexString(id) + ": " + name
2171                                + " at " + file);
2172                    }
2173                }
2174
2175                if (DEBUG_LOAD) Log.v(TAG, "Loading drawable for cookie "
2176                        + value.assetCookie + ": " + file);
2177
2178                if (file.endsWith(".xml")) {
2179                    Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
2180                    try {
2181                        XmlResourceParser rp = loadXmlResourceParser(
2182                                file, id, value.assetCookie, "drawable");
2183                        dr = Drawable.createFromXml(this, rp);
2184                        rp.close();
2185                    } catch (Exception e) {
2186                        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2187                        NotFoundException rnf = new NotFoundException(
2188                            "File " + file + " from drawable resource ID #0x"
2189                            + Integer.toHexString(id));
2190                        rnf.initCause(e);
2191                        throw rnf;
2192                    }
2193                    Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2194
2195                } else {
2196                    Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
2197                    try {
2198                        InputStream is = mAssets.openNonAsset(
2199                                value.assetCookie, file, AssetManager.ACCESS_STREAMING);
2200        //                System.out.println("Opened file " + file + ": " + is);
2201                        dr = Drawable.createFromResourceStream(this, value, is,
2202                                file, null);
2203                        is.close();
2204        //                System.out.println("Created stream: " + dr);
2205                    } catch (Exception e) {
2206                        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2207                        NotFoundException rnf = new NotFoundException(
2208                            "File " + file + " from drawable resource ID #0x"
2209                            + Integer.toHexString(id));
2210                        rnf.initCause(e);
2211                        throw rnf;
2212                    }
2213                    Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2214                }
2215            }
2216        }
2217
2218        if (dr != null) {
2219            dr.setChangingConfigurations(value.changingConfigurations);
2220            cs = dr.getConstantState();
2221            if (cs != null) {
2222                if (mPreloading) {
2223                    final int changingConfigs = cs.getChangingConfigurations();
2224                    if (isColorDrawable) {
2225                        if (verifyPreloadConfig(changingConfigs, 0, value.resourceId,
2226                                "drawable")) {
2227                            sPreloadedColorDrawables.put(key, cs);
2228                        }
2229                    } else {
2230                        if (verifyPreloadConfig(changingConfigs,
2231                                LAYOUT_DIR_CONFIG, value.resourceId, "drawable")) {
2232                            if ((changingConfigs&LAYOUT_DIR_CONFIG) == 0) {
2233                                // If this resource does not vary based on layout direction,
2234                                // we can put it in all of the preload maps.
2235                                sPreloadedDrawables[0].put(key, cs);
2236                                sPreloadedDrawables[1].put(key, cs);
2237                            } else {
2238                                // Otherwise, only in the layout dir we loaded it for.
2239                                final LongSparseArray<Drawable.ConstantState> preloads
2240                                        = sPreloadedDrawables[mConfiguration.getLayoutDirection()];
2241                                preloads.put(key, cs);
2242                            }
2243                        }
2244                    }
2245                } else {
2246                    synchronized (mAccessLock) {
2247                        //Log.i(TAG, "Saving cached drawable @ #" +
2248                        //        Integer.toHexString(key.intValue())
2249                        //        + " in " + this + ": " + cs);
2250                        if (isColorDrawable) {
2251                            mColorDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
2252                        } else {
2253                            mDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
2254                        }
2255                    }
2256                }
2257            }
2258        }
2259
2260        return dr;
2261    }
2262
2263    private Drawable getCachedDrawable(
2264            LongSparseArray<WeakReference<ConstantState>> drawableCache,
2265            long key) {
2266        synchronized (mAccessLock) {
2267            WeakReference<Drawable.ConstantState> wr = drawableCache.get(key);
2268            if (wr != null) {   // we have the key
2269                Drawable.ConstantState entry = wr.get();
2270                if (entry != null) {
2271                    //Log.i(TAG, "Returning cached drawable @ #" +
2272                    //        Integer.toHexString(((Integer)key).intValue())
2273                    //        + " in " + this + ": " + entry);
2274                    return entry.newDrawable(this);
2275                }
2276                else {  // our entry has been purged
2277                    drawableCache.delete(key);
2278                }
2279            }
2280        }
2281        return null;
2282    }
2283
2284    /*package*/ ColorStateList loadColorStateList(TypedValue value, int id)
2285            throws NotFoundException {
2286        if (TRACE_FOR_PRELOAD) {
2287            // Log only framework resources
2288            if ((id >>> 24) == 0x1) {
2289                final String name = getResourceName(id);
2290                if (name != null) android.util.Log.d("PreloadColorStateList", name);
2291            }
2292        }
2293
2294        final long key = (((long) value.assetCookie) << 32) | value.data;
2295
2296        ColorStateList csl;
2297
2298        if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
2299                value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
2300
2301            csl = sPreloadedColorStateLists.get(key);
2302            if (csl != null) {
2303                return csl;
2304            }
2305
2306            csl = ColorStateList.valueOf(value.data);
2307            if (mPreloading) {
2308                if (verifyPreloadConfig(value.changingConfigurations, 0, value.resourceId,
2309                        "color")) {
2310                    sPreloadedColorStateLists.put(key, csl);
2311                }
2312            }
2313
2314            return csl;
2315        }
2316
2317        csl = getCachedColorStateList(key);
2318        if (csl != null) {
2319            return csl;
2320        }
2321
2322        csl = sPreloadedColorStateLists.get(key);
2323        if (csl != null) {
2324            return csl;
2325        }
2326
2327        if (value.string == null) {
2328            throw new NotFoundException(
2329                    "Resource is not a ColorStateList (color or path): " + value);
2330        }
2331
2332        final String file = value.string.toString();
2333
2334        if (file.endsWith(".xml")) {
2335            Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
2336            try {
2337                final XmlResourceParser rp = loadXmlResourceParser(
2338                        file, id, value.assetCookie, "colorstatelist");
2339                csl = ColorStateList.createFromXml(this, rp);
2340                rp.close();
2341            } catch (Exception e) {
2342                Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2343                NotFoundException rnf = new NotFoundException(
2344                    "File " + file + " from color state list resource ID #0x"
2345                    + Integer.toHexString(id));
2346                rnf.initCause(e);
2347                throw rnf;
2348            }
2349            Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2350        } else {
2351            throw new NotFoundException(
2352                    "File " + file + " from drawable resource ID #0x"
2353                    + Integer.toHexString(id) + ": .xml extension required");
2354        }
2355
2356        if (csl != null) {
2357            if (mPreloading) {
2358                if (verifyPreloadConfig(value.changingConfigurations, 0, value.resourceId,
2359                        "color")) {
2360                    sPreloadedColorStateLists.put(key, csl);
2361                }
2362            } else {
2363                synchronized (mAccessLock) {
2364                    //Log.i(TAG, "Saving cached color state list @ #" +
2365                    //        Integer.toHexString(key.intValue())
2366                    //        + " in " + this + ": " + csl);
2367                    mColorStateListCache.put(key, new WeakReference<ColorStateList>(csl));
2368                }
2369            }
2370        }
2371
2372        return csl;
2373    }
2374
2375    private ColorStateList getCachedColorStateList(long key) {
2376        synchronized (mAccessLock) {
2377            WeakReference<ColorStateList> wr = mColorStateListCache.get(key);
2378            if (wr != null) {   // we have the key
2379                ColorStateList entry = wr.get();
2380                if (entry != null) {
2381                    //Log.i(TAG, "Returning cached color state list @ #" +
2382                    //        Integer.toHexString(((Integer)key).intValue())
2383                    //        + " in " + this + ": " + entry);
2384                    return entry;
2385                } else {  // our entry has been purged
2386                    mColorStateListCache.delete(key);
2387                }
2388            }
2389        }
2390        return null;
2391    }
2392
2393    /*package*/ XmlResourceParser loadXmlResourceParser(int id, String type)
2394            throws NotFoundException {
2395        synchronized (mAccessLock) {
2396            TypedValue value = mTmpValue;
2397            if (value == null) {
2398                mTmpValue = value = new TypedValue();
2399            }
2400            getValue(id, value, true);
2401            if (value.type == TypedValue.TYPE_STRING) {
2402                return loadXmlResourceParser(value.string.toString(), id,
2403                        value.assetCookie, type);
2404            }
2405            throw new NotFoundException(
2406                    "Resource ID #0x" + Integer.toHexString(id) + " type #0x"
2407                    + Integer.toHexString(value.type) + " is not valid");
2408        }
2409    }
2410
2411    /*package*/ XmlResourceParser loadXmlResourceParser(String file, int id,
2412            int assetCookie, String type) throws NotFoundException {
2413        if (id != 0) {
2414            try {
2415                // These may be compiled...
2416                synchronized (mCachedXmlBlockIds) {
2417                    // First see if this block is in our cache.
2418                    final int num = mCachedXmlBlockIds.length;
2419                    for (int i=0; i<num; i++) {
2420                        if (mCachedXmlBlockIds[i] == id) {
2421                            //System.out.println("**** REUSING XML BLOCK!  id="
2422                            //                   + id + ", index=" + i);
2423                            return mCachedXmlBlocks[i].newParser();
2424                        }
2425                    }
2426
2427                    // Not in the cache, create a new block and put it at
2428                    // the next slot in the cache.
2429                    XmlBlock block = mAssets.openXmlBlockAsset(
2430                            assetCookie, file);
2431                    if (block != null) {
2432                        int pos = mLastCachedXmlBlockIndex+1;
2433                        if (pos >= num) pos = 0;
2434                        mLastCachedXmlBlockIndex = pos;
2435                        XmlBlock oldBlock = mCachedXmlBlocks[pos];
2436                        if (oldBlock != null) {
2437                            oldBlock.close();
2438                        }
2439                        mCachedXmlBlockIds[pos] = id;
2440                        mCachedXmlBlocks[pos] = block;
2441                        //System.out.println("**** CACHING NEW XML BLOCK!  id="
2442                        //                   + id + ", index=" + pos);
2443                        return block.newParser();
2444                    }
2445                }
2446            } catch (Exception e) {
2447                NotFoundException rnf = new NotFoundException(
2448                        "File " + file + " from xml type " + type + " resource ID #0x"
2449                        + Integer.toHexString(id));
2450                rnf.initCause(e);
2451                throw rnf;
2452            }
2453        }
2454
2455        throw new NotFoundException(
2456                "File " + file + " from xml type " + type + " resource ID #0x"
2457                + Integer.toHexString(id));
2458    }
2459
2460    /*package*/ void recycleCachedStyledAttributes(TypedArray attrs) {
2461        synchronized (mAccessLock) {
2462            final TypedArray cached = mCachedStyledAttributes;
2463            if (cached == null || cached.mData.length < attrs.mData.length) {
2464                mCachedStyledAttributes = attrs;
2465            }
2466        }
2467    }
2468
2469    private TypedArray getCachedStyledAttributes(int len) {
2470        synchronized (mAccessLock) {
2471            TypedArray attrs = mCachedStyledAttributes;
2472            if (attrs != null) {
2473                mCachedStyledAttributes = null;
2474                if (DEBUG_ATTRIBUTES_CACHE) {
2475                    mLastRetrievedAttrs = new RuntimeException("here");
2476                    mLastRetrievedAttrs.fillInStackTrace();
2477                }
2478
2479                attrs.mLength = len;
2480                int fullLen = len * AssetManager.STYLE_NUM_ENTRIES;
2481                if (attrs.mData.length >= fullLen) {
2482                    return attrs;
2483                }
2484                attrs.mData = new int[fullLen];
2485                attrs.mIndices = new int[1+len];
2486                return attrs;
2487            }
2488            if (DEBUG_ATTRIBUTES_CACHE) {
2489                RuntimeException here = new RuntimeException("here");
2490                here.fillInStackTrace();
2491                if (mLastRetrievedAttrs != null) {
2492                    Log.i(TAG, "Allocated new TypedArray of " + len + " in " + this, here);
2493                    Log.i(TAG, "Last retrieved attributes here", mLastRetrievedAttrs);
2494                }
2495                mLastRetrievedAttrs = here;
2496            }
2497            return new TypedArray(this,
2498                    new int[len*AssetManager.STYLE_NUM_ENTRIES],
2499                    new int[1+len], len);
2500        }
2501    }
2502
2503    private Resources() {
2504        mAssets = AssetManager.getSystem();
2505        // NOTE: Intentionally leaving this uninitialized (all values set
2506        // to zero), so that anyone who tries to do something that requires
2507        // metrics will get a very wrong value.
2508        mConfiguration.setToDefaults();
2509        mMetrics.setToDefaults();
2510        updateConfiguration(null, null);
2511        mAssets.ensureStringBlocks();
2512    }
2513}
2514