Most visited

Recently visited

Added in API level 1
Deprecated since API level 24

RenamingDelegatingContext

public class RenamingDelegatingContext
extends ContextWrapper

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.test.RenamingDelegatingContext


This class was deprecated in API level 24.
New tests should be written using the Android Testing Support Library.

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).

Summary

Inherited constants

From class android.content.Context

Public constructors

RenamingDelegatingContext(Context context, String filePrefix)
RenamingDelegatingContext(Context context, Context fileContext, String filePrefix)

Public methods

String[] databaseList()

Returns an array of strings naming the private databases associated with this Context's application package.

boolean deleteDatabase(String name)

Delete an existing private SQLiteDatabase associated with this Context's application package.

boolean deleteFile(String name)

Delete the given private file associated with this Context's application package.

String[] fileList()

Returns an array of strings naming the private files associated with this Context's application package.

File getCacheDir()

In order to support calls to getCacheDir(), we create a temp cache dir (inside the real one) and return it instead.

File getDatabasePath(String name)

Returns the absolute path on the filesystem where a database created with openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory) is stored.

String getDatabasePrefix()
File getFileStreamPath(String name)

Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.

void makeExistingFilesAndDbsAccessible()

Makes accessible all files and databases whose names match the filePrefix that was passed to the constructor.

FileInputStream openFileInput(String name)

Open a private file associated with this Context's application package for reading.

FileOutputStream openFileOutput(String name, int mode)

Open a private file associated with this Context's application package for writing.

SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory)

Open a new private SQLiteDatabase associated with this Context's application package.

SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler)

Open a new private SQLiteDatabase associated with this Context's application package.

static <T extends ContentProvider> T providerWithRenamedContext(Class<T> contentProvider, Context c, String filePrefix, boolean allowAccessToExistingFilesAndDbs)
static <T extends ContentProvider> T providerWithRenamedContext(Class<T> contentProvider, Context c, String filePrefix)

Inherited methods

From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object

Public constructors

RenamingDelegatingContext

Added in API level 1
RenamingDelegatingContext (Context context, 
                String filePrefix)

Parameters
context Context: : the context that will be delegated.
filePrefix String: : a prefix with which database and file names will be prefixed.

RenamingDelegatingContext

Added in API level 1
RenamingDelegatingContext (Context context, 
                Context fileContext, 
                String filePrefix)

Parameters
context Context: : the context that will be delegated.
fileContext Context: : the context that file and db methods will be delegated to
filePrefix String: : a prefix with which database and file names will be prefixed.

Public methods

databaseList

Added in API level 1
String[] databaseList ()

Returns an array of strings naming the private databases associated with this Context's application package.

Returns
String[] Array of strings naming the private databases.

deleteDatabase

Added in API level 1
boolean deleteDatabase (String name)

Delete an existing private SQLiteDatabase associated with this Context's application package.

Parameters
name String: The name (unique in the application package) of the database.
Returns
boolean true if the database was successfully deleted; else false.

deleteFile

Added in API level 1
boolean deleteFile (String name)

Delete the given private file associated with this Context's application package.

Parameters
name String: The name of the file to delete; can not contain path separators.
Returns
boolean true if the file was successfully deleted; else false.

fileList

Added in API level 1
String[] fileList ()

Returns an array of strings naming the private files associated with this Context's application package.

Returns
String[] Array of strings naming the private files.

getCacheDir

Added in API level 1
File getCacheDir ()

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.

Returns
File The path of the directory holding application cache files.

getDatabasePath

Added in API level 1
File getDatabasePath (String name)

Returns the absolute path on the filesystem where a database created with openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory) is stored.

The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.

Parameters
name String: The name of the database for which you would like to get its path.
Returns
File An absolute path to the given database.

getDatabasePrefix

Added in API level 1
String getDatabasePrefix ()

Returns
String

getFileStreamPath

Added in API level 1
File getFileStreamPath (String name)

Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.

The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.

Parameters
name String: The name of the file for which you would like to get its path.
Returns
File An absolute path to the given file.

makeExistingFilesAndDbsAccessible

Added in API level 1
void makeExistingFilesAndDbsAccessible ()

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.

openFileInput

Added in API level 1
FileInputStream openFileInput (String name)

Open a private file associated with this Context's application package for reading.

Parameters
name String: The name of the file to open; can not contain path separators.
Returns
FileInputStream The resulting FileInputStream.
Throws
FileNotFoundException

openFileOutput

Added in API level 1
FileOutputStream openFileOutput (String name, 
                int mode)

Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

No additional permissions are required for the calling app to read or write the returned file.

Parameters
name String: The name of the file to open; can not contain path separators.
mode int: Operating mode. Use 0 or MODE_PRIVATE for the default operation. Use MODE_APPEND to append to an existing file.
Returns
FileOutputStream The resulting FileOutputStream.
Throws
FileNotFoundException

openOrCreateDatabase

Added in API level 1
SQLiteDatabase openOrCreateDatabase (String name, 
                int mode, 
                SQLiteDatabase.CursorFactory factory)

Open a new private SQLiteDatabase associated with this Context's application package. Create the database file if it doesn't exist.

Parameters
name String: The name (unique in the application package) of the database.
mode int: Operating mode. Use 0 or MODE_PRIVATE for the default operation. Use MODE_ENABLE_WRITE_AHEAD_LOGGING to enable write-ahead logging by default. Use MODE_NO_LOCALIZED_COLLATORS to disable localized collators.
factory SQLiteDatabase.CursorFactory: An optional factory class that is called to instantiate a cursor when query is called.
Returns
SQLiteDatabase The contents of a newly created database with the given name.

openOrCreateDatabase

Added in API level 11
SQLiteDatabase openOrCreateDatabase (String name, 
                int mode, 
                SQLiteDatabase.CursorFactory factory, 
                DatabaseErrorHandler errorHandler)

Open a new private SQLiteDatabase associated with this Context's application package. Creates the database file if it doesn't exist.

Accepts input param: a concrete instance of DatabaseErrorHandler to be used to handle corruption when sqlite reports database corruption.

Parameters
name String: The name (unique in the application package) of the database.
mode int: Operating mode. Use 0 or MODE_PRIVATE for the default operation. Use MODE_ENABLE_WRITE_AHEAD_LOGGING to enable write-ahead logging by default. Use MODE_NO_LOCALIZED_COLLATORS to disable localized collators.
factory SQLiteDatabase.CursorFactory: An optional factory class that is called to instantiate a cursor when query is called.
errorHandler DatabaseErrorHandler: the DatabaseErrorHandler to be used when sqlite reports database corruption. if null, DefaultDatabaseErrorHandler is assumed.
Returns
SQLiteDatabase The contents of a newly created database with the given name.

providerWithRenamedContext

Added in API level 1
T providerWithRenamedContext (Class<T> contentProvider, 
                Context c, 
                String filePrefix, 
                boolean allowAccessToExistingFilesAndDbs)

Parameters
contentProvider Class
c Context
filePrefix String
allowAccessToExistingFilesAndDbs boolean
Returns
T
Throws
IllegalAccessException
InstantiationException

providerWithRenamedContext

Added in API level 1
T providerWithRenamedContext (Class<T> contentProvider, 
                Context c, 
                String filePrefix)

Parameters
contentProvider Class
c Context
filePrefix String
Returns
T
Throws
IllegalAccessException
InstantiationException

Hooray!