FileDocCategorySizeDatePackage
RenamingDelegatingContext.javaAPI DocAndroid 5.1 API9134Thu Mar 12 22:22:42 GMT 2015android.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 File
mCacheDir
private final Object
mSync
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.io.FilegetCacheDir()
In order to support calls to getCacheDir(), we create a temp cache dir (inside the real one) and return it instead. This code is basically getCacheDir(), except it uses the real cache dir as the parent directory and creates a test cache dir inside that.

        synchronized (mSync) {
            if (mCacheDir == null) {
                mCacheDir = new File(mFileContext.getCacheDir(), renamedFileName("cache"));
            }
            if (!mCacheDir.exists()) {
                if(!mCacheDir.mkdirs()) {
                    Log.w("RenamingDelegatingContext", "Unable to create cache directory");
                    return null;
                }
                FileUtils.setPermissions(
                        mCacheDir.getPath(),
                        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
                        -1, -1);
            }
        }
        return mCacheDir;
    
public java.io.FilegetDatabasePath(java.lang.String name)

        return mFileContext.getDatabasePath(renamedFileName(name));
    
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 android.database.sqlite.SQLiteDatabaseopenOrCreateDatabase(java.lang.String name, int mode, SQLiteDatabase.CursorFactory factory, android.database.DatabaseErrorHandler errorHandler)

        final String internalName = renamedFileName(name);
        if (!mDatabaseNames.contains(name)) {
            mDatabaseNames.add(name);
            mFileContext.deleteDatabase(internalName);
        }
        return mFileContext.openOrCreateDatabase(internalName, mode, factory, errorHandler);
    
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.attachInfoForTesting(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);