Пример #1
0
  /**
   * Will use the already existing app database if {@link BaseDatabaseDefinition#backupEnabled()} is
   * true. If the existing is not there we will try to use the prepackaged database for that
   * purpose.
   *
   * @param databaseName The name of the database to restore
   * @param prepackagedName The name of the prepackaged db file
   */
  public void restoreDatabase(String databaseName, String prepackagedName) {
    final File dbPath = FlowManager.getContext().getDatabasePath(databaseName);

    // If the database already exists, return
    if (dbPath.exists()) {
      return;
    }

    // Make sure we have a path to the file
    dbPath.getParentFile().mkdirs();

    // Try to copy database file
    try {
      // check existing and use that as backup
      File existingDb =
          FlowManager.getContext().getDatabasePath(databaseDefinition.getDatabaseFileName());
      InputStream inputStream;
      // if it exists and the integrity is ok
      if (existingDb.exists()
          && (databaseDefinition.backupEnabled()
              && FlowManager.isDatabaseIntegrityOk(backupHelper))) {
        inputStream = new FileInputStream(existingDb);
      } else {
        inputStream = FlowManager.getContext().getAssets().open(prepackagedName);
      }
      writeDB(dbPath, inputStream);
    } catch (IOException e) {
      FlowLog.logError(e);
    }
  }
Пример #2
0
  public FlowSQLiteOpenHelper(
      BaseDatabaseDefinition databaseDefinition, DatabaseHelperListener listener) {
    super(
        FlowManager.getContext(),
        databaseDefinition.isInMemory() ? null : databaseDefinition.getDatabaseFileName(),
        null,
        databaseDefinition.getDatabaseVersion());
    databaseHelperListener = listener;
    this.databaseDefinition = databaseDefinition;

    movePrepackagedDB(
        this.databaseDefinition.getDatabaseFileName(),
        this.databaseDefinition.getDatabaseFileName());

    if (databaseDefinition.backupEnabled()) {
      // Temp database mirrors existing
      backupHelper =
          new SQLiteOpenHelper(
              FlowManager.getContext(),
              getTempDbFileName(),
              null,
              databaseDefinition.getDatabaseVersion()) {
            @Override
            public void onOpen(SQLiteDatabase db) {
              FlowSQLiteOpenHelper.this.onOpen(db);
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
              FlowSQLiteOpenHelper.this.onCreate(db);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              FlowSQLiteOpenHelper.this.onUpgrade(db, oldVersion, newVersion);
            }
          };
      restoreDatabase(getTempDbFileName(), this.databaseDefinition.getDatabaseFileName());
      backupHelper.getWritableDatabase();
    }
  }