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

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, 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

Public constructors

RenamingDelegatingContext

Added in API level 1
public 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
public 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
public 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
public 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
public 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
public 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
public 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
public File getDatabasePath (String name)

Returns the absolute path on the filesystem where a database created with openOrCreateDatabase(String, int, 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
public String getDatabasePrefix ()

Returns
String

getFileStreamPath

Added in API level 1
public 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
public 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
public 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
public 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. Value is either 0 or a combination of Context.MODE_PRIVATE, Context.MODE_WORLD_READABLE, Context.MODE_WORLD_WRITEABLE, and Context.MODE_APPEND

Returns
FileOutputStream The resulting FileOutputStream.

Throws
FileNotFoundException

openOrCreateDatabase

Added in API level 1
public 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. Value is either 0 or a combination of Context.MODE_PRIVATE, Context.MODE_WORLD_READABLE, Context.MODE_WORLD_WRITEABLE, Context.MODE_ENABLE_WRITE_AHEAD_LOGGING, and Context.MODE_NO_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
Deprecated in API level 24
public 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. Value is either 0 or a combination of Context.MODE_PRIVATE, Context.MODE_WORLD_READABLE, Context.MODE_WORLD_WRITEABLE, Context.MODE_ENABLE_WRITE_AHEAD_LOGGING, and Context.MODE_NO_LOCALIZED_COLLATORS

factory SQLiteDatabase.CursorFactory: An optional factory class that is called to instantiate a cursor when query is called.

errorHandler DatabaseErrorHandler: This value may be null.

Returns
SQLiteDatabase The contents of a newly created database with the given name.

providerWithRenamedContext

Added in API level 1
public static 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
public static T providerWithRenamedContext (Class<T> contentProvider, 
                Context c, 
                String filePrefix)

Parameters
contentProvider Class

c Context

filePrefix String

Returns
T

Throws
IllegalAccessException
InstantiationException