FileDocCategorySizeDatePackage
AssetManager.javaAPI DocAndroid 1.5 API24514Wed May 06 22:41:54 BST 2009android.content.res

AssetManager

public final class AssetManager extends Object
Provides access to an application's raw asset files; see {@link Resources} for the way most applications will want to retrieve their resource data. This class presents a lower-level API that allows you to open and read raw files that have been bundled with the application as a simple stream of bytes.

Fields Summary
public static final int
ACCESS_UNKNOWN
Mode for {@link #open(String, int)}: no specific information about how data will be accessed.
public static final int
ACCESS_RANDOM
Mode for {@link #open(String, int)}: Read chunks, and seek forward and backward.
public static final int
ACCESS_STREAMING
Mode for {@link #open(String, int)}: Read sequentially, with an occasional forward seek.
public static final int
ACCESS_BUFFER
Mode for {@link #open(String, int)}: Attempt to load contents into memory, for fast small reads.
private static final String
TAG
private static final boolean
localLOGV
private static final Object
mSync
private static final android.util.TypedValue
mValue
private static final long[]
mOffsets
private static AssetManager
mSystem
private int
mObject
private StringBlock[]
mStringBlocks
private int
mNumRefs
private boolean
mOpen
private String
mAssetDir
private String
mAppName
static final int
STYLE_NUM_ENTRIES
static final int
STYLE_TYPE
static final int
STYLE_DATA
static final int
STYLE_ASSET_COOKIE
static final int
STYLE_RESOURCE_ID
static final int
STYLE_CHANGING_CONFIGURATIONS
Constructors Summary
public AssetManager()
Create a new AssetManager containing only the basic system assets. Applications will not generally use this method, instead retrieving the appropriate asset manager with {@link Resources#getAssets}. Not for use by applications. {@hide}


                                            
      
        synchronized (mSync) {
            init();
            if (localLOGV) Log.v(TAG, "New asset manager: " + this);
            ensureSystemAssets();
        }
    
private AssetManager(boolean isSystem)

        init();
        if (localLOGV) Log.v(TAG, "New asset manager: " + this);
    
Methods Summary
public final native intaddAssetPath(java.lang.String path)
Add an additional set of assets to the asset manager. This can be either a directory or ZIP file. Not for use by applications. Returns the cookie of the added asset, or 0 on failure. {@hide}

static final native booleanapplyStyle(int theme, int defStyleAttr, int defStyleRes, int xmlParser, int[] inAttrs, int[] outValues, int[] outIndices)

static final native voidapplyThemeStyle(int theme, int styleRes, boolean force)

public voidclose()
Close this asset manager.

        synchronized(mSync) {
            //System.out.println("Release: num=" + mNumRefs
            //                   + ", released=" + mReleased);
            if (mOpen) {
                mOpen = false;
                decRefsLocked();
            }
        }
    
static final native voidcopyTheme(int dest, int source)

final intcreateTheme()

        synchronized (mSync) {
            if (!mOpen) {
                throw new RuntimeException("Assetmanager has been closed");
            }
            mNumRefs++;
            return newTheme();
        }
    
private final voiddecRefsLocked()

    /*package*/      
                 
                 
    /*package*/    
                   
    /*package*/     
    /*package*/       
        
         

          
         

          
         
    
          
         
    
        
         
    /*package*/          
    /*package*/        
    /*package*/        
                                                                 
                                                                 
    /*package*/            

           

         
         
    /*package*/     

        
        

        
        mNumRefs--;
        //System.out.println("Dec streams: mNumRefs=" + mNumRefs
        //                   + " mReleased=" + mReleased);
        if (mNumRefs == 0) {
            destroy();
        }
    
private final native voiddeleteTheme(int theme)

private final native voiddestroy()

private final native voiddestroyAsset(int asset)

static final native voiddumpTheme(int theme, int priority, java.lang.String tag, java.lang.String prefix)

final voidensureStringBlocks()

        if (mStringBlocks == null) {
            synchronized (mSync) {
                if (mStringBlocks == null) {
                    makeStringBlocks(true);
                }
            }
        }
    
private static voidensureSystemAssets()

        synchronized (mSync) {
            if (mSystem == null) {
                AssetManager system = new AssetManager(true);
                system.makeStringBlocks(false);
                mSystem = system;
            }
        }
    
protected voidfinalize()

        destroy();
    
final native int[]getArrayIntResource(int arrayRes)

final native intgetArraySize(int resource)

private final native int[]getArrayStringInfo(int arrayRes)

private final native java.lang.String[]getArrayStringResource(int arrayRes)

private final native longgetAssetLength(int asset)

private final native longgetAssetRemainingLength(int asset)

public final native java.lang.StringgetCookieName(int cookie)
{@hide}

public static final native intgetGlobalAssetCount()
{@hide}

public static final native intgetGlobalAssetManagerCount()
{@hide}

public final native java.lang.String[]getLocales()
Get the locales that this asset manager contains data for.

private final native intgetNativeStringBlock(int block)

final java.lang.CharSequencegetPooledString(int block, int id)

        //System.out.println("Get pooled: block=" + block
        //                   + ", id=#" + Integer.toHexString(id)
        //                   + ", blocks=" + mStringBlocks);
        return mStringBlocks[block-1].get(id);
    
final java.lang.CharSequencegetResourceBagText(int ident, int bagEntryId)
Retrieve the string value associated with a particular resource identifier for the current configuration / skin.

        synchronized (mSync) {
            TypedValue tmpValue = mValue;
            int block = loadResourceBagValue(ident, bagEntryId, tmpValue, true);
            if (block >= 0) {
                if (tmpValue.type == TypedValue.TYPE_STRING) {
                    return mStringBlocks[block].get(tmpValue.data);
                }
                return tmpValue.coerceToString();
            }
        }
        return null;
    
final native java.lang.StringgetResourceEntryName(int resid)

final native intgetResourceIdentifier(java.lang.String type, java.lang.String name, java.lang.String defPackage)
Retrieve the resource identifier for the given resource name.

final native java.lang.StringgetResourceName(int resid)

final native java.lang.StringgetResourcePackageName(int resid)

final java.lang.String[]getResourceStringArray(int id)
Retrieve the string array associated with a particular resource identifier.

param
id Resource id of the string array

        String[] retArray = getArrayStringResource(id);
        return retArray;
    
final java.lang.CharSequencegetResourceText(int ident)
Retrieve the string value associated with a particular resource identifier for the current configuration / skin.

        synchronized (mSync) {
            TypedValue tmpValue = mValue;
            int block = loadResourceValue(ident, tmpValue, true);
            if (block >= 0) {
                if (tmpValue.type == TypedValue.TYPE_STRING) {
                    return mStringBlocks[block].get(tmpValue.data);
                }
                return tmpValue.coerceToString();
            }
        }
        return null;
    
final java.lang.CharSequence[]getResourceTextArray(int id)
Retrieve the text array associated with a particular resource identifier.

param
id Resource id of the string array

        int[] rawInfoArray = getArrayStringInfo(id);
        int rawInfoArrayLen = rawInfoArray.length;
        final int infoArrayLen = rawInfoArrayLen / 2;
        int block;
        int index;
        CharSequence[] retArray = new CharSequence[infoArrayLen];
        for (int i = 0, j = 0; i < rawInfoArrayLen; i = i + 2, j++) {
            block = rawInfoArray[i];
            index = rawInfoArray[i + 1];
            retArray[j] = index >= 0 ? mStringBlocks[block].get(index) : null;
        }
        return retArray;
    
final native java.lang.StringgetResourceTypeName(int resid)

final booleangetResourceValue(int ident, android.util.TypedValue outValue, boolean resolveRefs)

        int block = loadResourceValue(ident, outValue, resolveRefs);
        if (block >= 0) {
            if (outValue.type != TypedValue.TYPE_STRING) {
                return true;
            }
            outValue.string = mStringBlocks[block].get(outValue.data);
            return true;
        }
        return false;
    
private final native intgetStringBlockCount()

public static android.content.res.AssetManagergetSystem()
Return a global shared asset manager that provides access to only system assets (no application assets). {@hide}

        ensureSystemAssets();
        return mSystem;
    
final booleangetThemeValue(int theme, int ident, android.util.TypedValue outValue, boolean resolveRefs)

        int block = loadThemeAttributeValue(theme, ident, outValue, resolveRefs);
        if (block >= 0) {
            if (outValue.type != TypedValue.TYPE_STRING) {
                return true;
            }
            StringBlock[] blocks = mStringBlocks;
            if (blocks == null) {
                ensureStringBlocks();
            }
            outValue.string = blocks[block].get(outValue.data);
            return true;
        }
        return false;
    
private final native voidinit()

public final native booleanisUpToDate()
Determine whether the state in this asset manager is up-to-date with the files on the filesystem. If false is returned, you need to instantiate a new AssetManager class to see the new data. {@hide}

public final native java.lang.String[]list(java.lang.String path)
Return a String array of all the assets at the given path.

param
path A relative path within the assets, i.e., "docs/home.html".
return
String[] Array of strings, one for each asset. These file names are relative to 'path'. You can open the file by concatenating 'path' and a name in the returned string (via File) and passing that to open().
see
#open

private final native intloadResourceBagValue(int ident, int bagEntryId, android.util.TypedValue outValue, boolean resolve)
Returns true if the resource was found, filling in mRetStringBlock and mRetData.

private final native intloadResourceValue(int ident, android.util.TypedValue outValue, boolean resolve)
Returns true if the resource was found, filling in mRetStringBlock and mRetData.

static final native intloadThemeAttributeValue(int theme, int ident, android.util.TypedValue outValue, boolean resolve)

private final voidmakeStringBlocks(boolean copyFromSystem)

        final int sysNum = copyFromSystem ? mSystem.mStringBlocks.length : 0;
        final int num = getStringBlockCount();
        mStringBlocks = new StringBlock[num];
        if (localLOGV) Log.v(TAG, "Making string blocks for " + this
                + ": " + num);
        for (int i=0; i<num; i++) {
            if (i < sysNum) {
                mStringBlocks[i] = mSystem.mStringBlocks[i];
            } else {
                mStringBlocks[i] = new StringBlock(getNativeStringBlock(i), true);
            }
        }
    
private final native intnewTheme()

public final java.io.InputStreamopen(java.lang.String fileName)
Open an asset using ACCESS_STREAMING mode. This provides access to files that have been bundled with an application as assets -- that is, files placed in to the "assets" directory.

param
fileName The name of the asset to open. This name can be hierarchical.
see
#open(String, int)
see
#list

        return open(fileName, ACCESS_STREAMING);
    
public final java.io.InputStreamopen(java.lang.String fileName, int accessMode)
Open an asset using an explicit access mode, returning an InputStream to read its contents. This provides access to files that have been bundled with an application as assets -- that is, files placed in to the "assets" directory.

param
fileName The name of the asset to open. This name can be hierarchical.
param
accessMode Desired access mode for retrieving the data.
see
#ACCESS_UNKNOWN
see
#ACCESS_STREAMING
see
#ACCESS_RANDOM
see
#ACCESS_BUFFER
see
#open(String)
see
#list

        synchronized (mSync) {
            if (!mOpen) {
                throw new RuntimeException("Assetmanager has been closed");
            }
            int asset = openAsset(fileName, accessMode);
            if (asset != 0) {
                mNumRefs++;
                return new AssetInputStream(asset);
            }
        }
        throw new FileNotFoundException("Asset file: " + fileName);
    
private final native intopenAsset(java.lang.String fileName, int accessMode)

private final native android.os.ParcelFileDescriptoropenAssetFd(java.lang.String fileName, long[] outOffsets)

public final AssetFileDescriptoropenFd(java.lang.String fileName)

        synchronized (mSync) {
            if (!mOpen) {
                throw new RuntimeException("Assetmanager has been closed");
            }
            ParcelFileDescriptor pfd = openAssetFd(fileName, mOffsets);
            if (pfd != null) {
                return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]);
            }
        }
        throw new FileNotFoundException("Asset file: " + fileName);
    
public final java.io.InputStreamopenNonAsset(java.lang.String fileName)
{@hide} Open a non-asset file as an asset using ACCESS_STREAMING mode. This provides direct access to all of the files included in an application package (not only its assets). Applications should not normally use this.

see
#open(String)

        return openNonAsset(0, fileName, ACCESS_STREAMING);
    
public final java.io.InputStreamopenNonAsset(java.lang.String fileName, int accessMode)
{@hide} Open a non-asset file as an asset using a specific access mode. This provides direct access to all of the files included in an application package (not only its assets). Applications should not normally use this.

see
#open(String, int)

        return openNonAsset(0, fileName, accessMode);
    
public final java.io.InputStreamopenNonAsset(int cookie, java.lang.String fileName)
{@hide} Open a non-asset in a specified package. Not for use by applications.

param
cookie Identifier of the package to be opened.
param
fileName Name of the asset to retrieve.

        return openNonAsset(cookie, fileName, ACCESS_STREAMING);
    
public final java.io.InputStreamopenNonAsset(int cookie, java.lang.String fileName, int accessMode)
{@hide} Open a non-asset in a specified package. Not for use by applications.

param
cookie Identifier of the package to be opened.
param
fileName Name of the asset to retrieve.
param
accessMode Desired access mode for retrieving the data.

        synchronized (mSync) {
            if (!mOpen) {
                throw new RuntimeException("Assetmanager has been closed");
            }
            int asset = openNonAssetNative(cookie, fileName, accessMode);
            if (asset != 0) {
                mNumRefs++;
                return new AssetInputStream(asset);
            }
        }
        throw new FileNotFoundException("Asset absolute file: " + fileName);
    
public final AssetFileDescriptoropenNonAssetFd(java.lang.String fileName)

        return openNonAssetFd(0, fileName);
    
public final AssetFileDescriptoropenNonAssetFd(int cookie, java.lang.String fileName)

        synchronized (mSync) {
            if (!mOpen) {
                throw new RuntimeException("Assetmanager has been closed");
            }
            ParcelFileDescriptor pfd = openNonAssetFdNative(cookie,
                    fileName, mOffsets);
            if (pfd != null) {
                return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]);
            }
        }
        throw new FileNotFoundException("Asset absolute file: " + fileName);
    
private native android.os.ParcelFileDescriptoropenNonAssetFdNative(int cookie, java.lang.String fileName, long[] outOffsets)

private final native intopenNonAssetNative(int cookie, java.lang.String fileName, int accessMode)

private final native intopenXmlAssetNative(int cookie, java.lang.String fileName)

final XmlBlockopenXmlBlockAsset(java.lang.String fileName)
{@hide} Retrieve a non-asset as a compiled XML file. Not for use by applications.

param
fileName The name of the file to retrieve.

        return openXmlBlockAsset(0, fileName);
    
final XmlBlockopenXmlBlockAsset(int cookie, java.lang.String fileName)
{@hide} Retrieve a non-asset as a compiled XML file. Not for use by applications.

param
cookie Identifier of the package to be opened.
param
fileName Name of the asset to retrieve.

        synchronized (mSync) {
            if (!mOpen) {
                throw new RuntimeException("Assetmanager has been closed");
            }
            int xmlBlock = openXmlAssetNative(cookie, fileName);
            if (xmlBlock != 0) {
                mNumRefs++;
                return new XmlBlock(this, xmlBlock);
            }
        }
        throw new FileNotFoundException("Asset XML file: " + fileName);
    
public final XmlResourceParseropenXmlResourceParser(java.lang.String fileName)
Retrieve a parser for a compiled XML file.

param
fileName The name of the file to retrieve.

        return openXmlResourceParser(0, fileName);
    
public final XmlResourceParseropenXmlResourceParser(int cookie, java.lang.String fileName)
Retrieve a parser for a compiled XML file.

param
cookie Identifier of the package to be opened.
param
fileName The name of the file to retrieve.

        XmlBlock block = openXmlBlockAsset(cookie, fileName);
        XmlResourceParser rp = block.newParser();
        block.close();
        return rp;
    
private final native intreadAsset(int asset, byte[] b, int off, int len)

private final native intreadAssetChar(int asset)

final voidreleaseTheme(int theme)

        synchronized (mSync) {
            deleteTheme(theme);
            decRefsLocked();
        }
    
final native intretrieveArray(int resource, int[] outValues)

final native booleanretrieveAttributes(int xmlParser, int[] inAttrs, int[] outValues, int[] outIndices)

private final native longseekAsset(int asset, long offset, int whence)

public final native voidsetConfiguration(int mcc, int mnc, java.lang.String locale, int orientation, int touchscreen, int density, int keyboard, int keyboardHidden, int navigation, int screenWidth, int screenHeight, int majorVersion)
Change the configuation used when retrieving resources. Not for use by applications. {@hide}

public final native voidsetLocale(java.lang.String locale)
Change the locale being used by this asset manager. Not for use by applications. {@hide}

voidxmlBlockGone()

        synchronized (mSync) {
            decRefsLocked();
        }