The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | package android.test; |
| 2 | |
| 3 | import com.google.android.collect.Sets; |
| 4 | |
| 5 | import android.content.Context; |
| 6 | import android.content.ContextWrapper; |
| 7 | import android.content.ContentProvider; |
| 8 | import android.database.sqlite.SQLiteDatabase; |
| 9 | |
| 10 | import java.io.File; |
| 11 | import java.io.FileInputStream; |
| 12 | import java.io.FileNotFoundException; |
| 13 | import java.io.FileOutputStream; |
| 14 | import java.util.Set; |
| 15 | |
| 16 | /** |
| 17 | * This is a class which delegates to the given context, but performs database |
| 18 | * and file operations with a renamed database/file name (prefixes default |
| 19 | * names with a given prefix). |
| 20 | */ |
| 21 | public class RenamingDelegatingContext extends ContextWrapper { |
| 22 | |
| 23 | private Context mFileContext; |
| 24 | private String mFilePrefix = null; |
| 25 | |
| 26 | private Set<String> mDatabaseNames = Sets.newHashSet(); |
| 27 | private Set<String> mFileNames = Sets.newHashSet(); |
| 28 | |
| 29 | public static <T extends ContentProvider> T providerWithRenamedContext( |
| 30 | Class<T> contentProvider, Context c, String filePrefix) |
| 31 | throws IllegalAccessException, InstantiationException { |
| 32 | return providerWithRenamedContext(contentProvider, c, filePrefix, false); |
| 33 | } |
| 34 | |
| 35 | public static <T extends ContentProvider> T providerWithRenamedContext( |
| 36 | Class<T> contentProvider, Context c, String filePrefix, |
| 37 | boolean allowAccessToExistingFilesAndDbs) |
| 38 | throws IllegalAccessException, InstantiationException { |
| 39 | Class<T> mProviderClass = contentProvider; |
| 40 | T mProvider = mProviderClass.newInstance(); |
| 41 | RenamingDelegatingContext mContext = new RenamingDelegatingContext(c, filePrefix); |
| 42 | if (allowAccessToExistingFilesAndDbs) { |
| 43 | mContext.makeExistingFilesAndDbsAccessible(); |
| 44 | } |
| 45 | mProvider.attachInfo(mContext, null); |
| 46 | return mProvider; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Makes accessible all files and databases whose names match the filePrefix that was passed to |
| 51 | * the constructor. Normally only files and databases that were created through this context are |
| 52 | * accessible. |
| 53 | */ |
| 54 | public void makeExistingFilesAndDbsAccessible() { |
| 55 | String[] databaseList = mFileContext.databaseList(); |
| 56 | for (String diskName : databaseList) { |
| 57 | if (shouldDiskNameBeVisible(diskName)) { |
| 58 | mDatabaseNames.add(publicNameFromDiskName(diskName)); |
| 59 | } |
| 60 | } |
| 61 | String[] fileList = mFileContext.fileList(); |
| 62 | for (String diskName : fileList) { |
| 63 | if (shouldDiskNameBeVisible(diskName)) { |
| 64 | mFileNames.add(publicNameFromDiskName(diskName)); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns if the given diskName starts with the given prefix or not. |
| 71 | * @param diskName name of the database/file. |
| 72 | */ |
| 73 | boolean shouldDiskNameBeVisible(String diskName) { |
| 74 | return diskName.startsWith(mFilePrefix); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the public name (everything following the prefix) of the given diskName. |
| 79 | * @param diskName name of the database/file. |
| 80 | */ |
| 81 | String publicNameFromDiskName(String diskName) { |
| 82 | if (!shouldDiskNameBeVisible(diskName)) { |
| 83 | throw new IllegalArgumentException("disk file should not be visible: " + diskName); |
| 84 | } |
| 85 | return diskName.substring(mFilePrefix.length(), diskName.length()); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param context : the context that will be delagated. |
| 90 | * @param filePrefix : a prefix with which database and file names will be |
| 91 | * prefixed. |
| 92 | */ |
| 93 | public RenamingDelegatingContext(Context context, String filePrefix) { |
| 94 | super(context); |
| 95 | mFileContext = context; |
| 96 | mFilePrefix = filePrefix; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param context : the context that will be delagated. |
| 101 | * @param fileContext : the context that file and db methods will be delgated to |
| 102 | * @param filePrefix : a prefix with which database and file names will be |
| 103 | * prefixed. |
| 104 | */ |
| 105 | public RenamingDelegatingContext(Context context, Context fileContext, String filePrefix) { |
| 106 | super(context); |
| 107 | mFileContext = fileContext; |
| 108 | mFilePrefix = filePrefix; |
| 109 | } |
| 110 | |
| 111 | public String getDatabasePrefix() { |
| 112 | return mFilePrefix; |
| 113 | } |
| 114 | |
| 115 | private String renamedFileName(String name) { |
| 116 | return mFilePrefix + name; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public SQLiteDatabase openOrCreateDatabase(String name, |
| 121 | int mode, SQLiteDatabase.CursorFactory factory) { |
| 122 | final String internalName = renamedFileName(name); |
| 123 | if (!mDatabaseNames.contains(name)) { |
| 124 | mDatabaseNames.add(name); |
| 125 | mFileContext.deleteDatabase(internalName); |
| 126 | } |
| 127 | return mFileContext.openOrCreateDatabase(internalName, mode, factory); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean deleteDatabase(String name) { |
| 132 | if (mDatabaseNames.contains(name)) { |
| 133 | mDatabaseNames.remove(name); |
| 134 | return mFileContext.deleteDatabase(renamedFileName(name)); |
| 135 | } else { |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public String[] databaseList() { |
| 142 | return mDatabaseNames.toArray(new String[]{}); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public FileInputStream openFileInput(String name) |
| 147 | throws FileNotFoundException { |
| 148 | final String internalName = renamedFileName(name); |
| 149 | if (mFileNames.contains(name)) { |
| 150 | return mFileContext.openFileInput(internalName); |
| 151 | } else { |
| 152 | throw new FileNotFoundException(internalName); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public FileOutputStream openFileOutput(String name, int mode) |
| 158 | throws FileNotFoundException { |
| 159 | mFileNames.add(name); |
| 160 | return mFileContext.openFileOutput(renamedFileName(name), mode); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public File getFileStreamPath(String name) { |
| 165 | return mFileContext.getFileStreamPath(renamedFileName(name)); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean deleteFile(String name) { |
| 170 | if (mFileNames.contains(name)) { |
| 171 | mFileNames.remove(name); |
| 172 | return mFileContext.deleteFile(renamedFileName(name)); |
| 173 | } else { |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public String[] fileList() { |
| 180 | return mFileNames.toArray(new String[]{}); |
| 181 | } |
| 182 | |
| 183 | // /** |
| 184 | // * Given an array of files returns only those whose names indicate that they belong to this |
| 185 | // * context. |
| 186 | // * @param allFiles the original list of files |
| 187 | // * @return the pruned list of files |
| 188 | // */ |
| 189 | // private String[] prunedFileList(String[] allFiles) { |
| 190 | // List<String> files = Lists.newArrayList(); |
| 191 | // for (String file : allFiles) { |
| 192 | // if (file.startsWith(mFilePrefix)) { |
| 193 | // files.add(file); |
| 194 | // } |
| 195 | // } |
| 196 | // return files.toArray(new String[]{}); |
| 197 | // } |
| 198 | } |