Called by Room when it is initialized. @param configuration The database configuration.
(@NonNull DatabaseConfiguration configuration)
| 194 | * @param configuration The database configuration. |
| 195 | */ |
| 196 | @CallSuper |
| 197 | public void init(@NonNull DatabaseConfiguration configuration) { |
| 198 | mOpenHelper = createOpenHelper(configuration); |
| 199 | Set<Class<? extends AutoMigrationSpec>> requiredAutoMigrationSpecs = |
| 200 | getRequiredAutoMigrationSpecs(); |
| 201 | BitSet usedSpecs = new BitSet(); |
| 202 | for (Class<? extends AutoMigrationSpec> spec : requiredAutoMigrationSpecs) { |
| 203 | int foundIndex = -1; |
| 204 | for (int providedIndex = configuration.autoMigrationSpecs.size() - 1; |
| 205 | providedIndex >= 0; providedIndex-- |
| 206 | ) { |
| 207 | Object provided = configuration.autoMigrationSpecs.get(providedIndex); |
| 208 | if (spec.isAssignableFrom(provided.getClass())) { |
| 209 | foundIndex = providedIndex; |
| 210 | usedSpecs.set(foundIndex); |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | if (foundIndex < 0) { |
| 215 | throw new IllegalArgumentException( |
| 216 | "A required auto migration spec (" + spec.getCanonicalName() |
| 217 | + ") is missing in the database configuration."); |
| 218 | } |
| 219 | mAutoMigrationSpecs.put(spec, configuration.autoMigrationSpecs.get(foundIndex)); |
| 220 | } |
| 221 | |
| 222 | for (int providedIndex = configuration.autoMigrationSpecs.size() - 1; |
| 223 | providedIndex >= 0; providedIndex--) { |
| 224 | if (!usedSpecs.get(providedIndex)) { |
| 225 | throw new IllegalArgumentException("Unexpected auto migration specs found. " |
| 226 | + "Annotate AutoMigrationSpec implementation with " |
| 227 | + "@ProvidedAutoMigrationSpec annotation or remove this spec from the " |
| 228 | + "builder."); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | List<Migration> autoMigrations = getAutoMigrations(mAutoMigrationSpecs); |
| 233 | for (Migration autoMigration : autoMigrations) { |
| 234 | boolean migrationExists = configuration.migrationContainer.getMigrations() |
| 235 | .containsKey(autoMigration.startVersion); |
| 236 | if (!migrationExists) { |
| 237 | configuration.migrationContainer.addMigrations(autoMigration); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Configure SqliteCopyOpenHelper if it is available: |
| 242 | SQLiteCopyOpenHelper copyOpenHelper = unwrapOpenHelper(SQLiteCopyOpenHelper.class, |
| 243 | mOpenHelper); |
| 244 | if (copyOpenHelper != null) { |
| 245 | copyOpenHelper.setDatabaseConfiguration(configuration); |
| 246 | } |
| 247 | |
| 248 | AutoClosingRoomOpenHelper autoClosingRoomOpenHelper = |
| 249 | unwrapOpenHelper(AutoClosingRoomOpenHelper.class, mOpenHelper); |
| 250 | |
| 251 | if (autoClosingRoomOpenHelper != null) { |
| 252 | mAutoCloser = autoClosingRoomOpenHelper.getAutoCloser(); |
| 253 | mInvalidationTracker.setAutoCloser(mAutoCloser); |
no test coverage detected