FileDocCategorySizeDatePackage
RenamingDelegatingContext.javaAPI DocAndroid 1.5 API6711Wed May 06 22:42:02 BST 2009android.test

RenamingDelegatingContext

public class RenamingDelegatingContext extends android.content.ContextWrapper
This is a class which delegates to the given context, but performs database and file operations with a renamed database/file name (prefixes default names with a given prefix).

Fields Summary
private android.content.Context
mFileContext
private String
mFilePrefix
private Set
mDatabaseNames
private Set
mFileNames
Constructors Summary
public RenamingDelegatingContext(android.content.Context context, String filePrefix)

param
context : the context that will be delagated.
param
filePrefix : a prefix with which database and file names will be prefixed.

        super(context);
        mFileContext = context;
        mFilePrefix = filePrefix;
    
public RenamingDelegatingContext(android.content.Context context, android.content.Context fileContext, String filePrefix)

param
context : the context that will be delagated.
param
fileContext : the context that file and db methods will be delgated to
param
filePrefix : a prefix with which database and file names will be prefixed.

        super(context);
        mFileContext = fileContext;
        mFilePrefix = filePrefix;
    
Methods Summary
public java.lang.String[]databaseList()

        return mDatabaseNames.toArray(new String[]{});
    
public booleandeleteDatabase(java.lang.String name)

        if (mDatabaseNames.contains(name)) {
            mDatabaseNames.remove(name);
            return mFileContext.deleteDatabase(renamedFileName(name));
        } else {
            return false;
        }
    
public booleandeleteFile(java.lang.String name)

        if (mFileNames.contains(name)) {
            mFileNames.remove(name);
            return mFileContext.deleteFile(renamedFileName(name));
        } else {
            return false;
        }
    
public java.lang.String[]fileList()

        return mFileNames.toArray(new String[]{});
    
public java.lang.StringgetDatabasePrefix()

        return mFilePrefix;
    
public java.io.FilegetFileStreamPath(java.lang.String name)

        return mFileContext.getFileStreamPath(renamedFileName(name));
    
public voidmakeExistingFilesAndDbsAccessible()
Makes accessible all files and databases whose names match the filePrefix that was passed to the constructor. Normally only files and databases that were created through this context are accessible.

        String[] databaseList = mFileContext.databaseList();
        for (String diskName : databaseList) {
            if (shouldDiskNameBeVisible(diskName)) {
                mDatabaseNames.add(publicNameFromDiskName(diskName));
            }
        }
        String[] fileList = mFileContext.fileList();
        for (String diskName : fileList) {
            if (shouldDiskNameBeVisible(diskName)) {
                mFileNames.add(publicNameFromDiskName(diskName));
            }
        }
    
public java.io.FileInputStreamopenFileInput(java.lang.String name)

        final String internalName = renamedFileName(name);
        if (mFileNames.contains(name)) {
            return mFileContext.openFileInput(internalName);
        } else {
            throw new FileNotFoundException(internalName);
        }
    
public java.io.FileOutputStreamopenFileOutput(java.lang.String name, int mode)

        mFileNames.add(name);
        return mFileContext.openFileOutput(renamedFileName(name), mode);
    
public android.database.sqlite.SQLiteDatabaseopenOrCreateDatabase(java.lang.String name, int mode, SQLiteDatabase.CursorFactory factory)

        final String internalName = renamedFileName(name);
        if (!mDatabaseNames.contains(name)) {
            mDatabaseNames.add(name);
            mFileContext.deleteDatabase(internalName);
        }
        return mFileContext.openOrCreateDatabase(internalName, mode, factory);
    
public static TproviderWithRenamedContext(java.lang.Class contentProvider, android.content.Context c, java.lang.String filePrefix)


          
                 
               
        return providerWithRenamedContext(contentProvider, c, filePrefix, false);
    
public static TproviderWithRenamedContext(java.lang.Class contentProvider, android.content.Context c, java.lang.String filePrefix, boolean allowAccessToExistingFilesAndDbs)

        Class<T> mProviderClass = contentProvider;
        T mProvider = mProviderClass.newInstance();
        RenamingDelegatingContext mContext = new RenamingDelegatingContext(c, filePrefix);
        if (allowAccessToExistingFilesAndDbs) {
            mContext.makeExistingFilesAndDbsAccessible();
        }
        mProvider.attachInfo(mContext, null);
        return mProvider;
    
java.lang.StringpublicNameFromDiskName(java.lang.String diskName)
Returns the public name (everything following the prefix) of the given diskName.

param
diskName name of the database/file.

        if (!shouldDiskNameBeVisible(diskName)) {
            throw new IllegalArgumentException("disk file should not be visible: " + diskName);
        }
        return diskName.substring(mFilePrefix.length(), diskName.length());
    
private java.lang.StringrenamedFileName(java.lang.String name)

        return mFilePrefix + name;
    
booleanshouldDiskNameBeVisible(java.lang.String diskName)
Returns if the given diskName starts with the given prefix or not.

param
diskName name of the database/file.

        return diskName.startsWith(mFilePrefix);