androidx.room.Room Java Examples

The following examples show how to use androidx.room.Room. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns the mesh database
 */
@RestrictTo(RestrictTo.Scope.LIBRARY)
static MeshNetworkDb getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (MeshNetworkDb.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        MeshNetworkDb.class, "mesh_network_database.db")
                        .addCallback(sRoomDatabaseCallback)
                        .addMigrations(MIGRATION_1_2)
                        .addMigrations(MIGRATION_2_3)
                        .addMigrations(MIGRATION_3_4)
                        .addMigrations(MIGRATION_4_5)
                        .addMigrations(MIGRATION_5_6)
                        .addMigrations(MIGRATION_6_7)
                        .addMigrations(MIGRATION_7_8)
                        .addMigrations(MIGRATION_8_9)
                        .build();
            }

        }
    }
    return INSTANCE;
}
 
Example #2
Source File: OpenScale.java    From openScale with GNU General Public License v3.0 6 votes vote down vote up
public void reopenDatabase(boolean truncate) throws SQLiteDatabaseCorruptException {
    if (appDB != null) {
        appDB.close();
    }

    appDB = Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME)
            .allowMainThreadQueries()
            .setJournalMode(truncate == true ? RoomDatabase.JournalMode.TRUNCATE : RoomDatabase.JournalMode.AUTOMATIC) // in truncate mode no sql cache files (-shm, -wal) are generated
            .addCallback(new RoomDatabase.Callback() {
                @Override
                public void onOpen(SupportSQLiteDatabase db) {
                    super.onOpen(db);
                    db.setForeignKeyConstraintsEnabled(true);
                }
            })
            .addMigrations(AppDatabase.MIGRATION_1_2, AppDatabase.MIGRATION_2_3, AppDatabase.MIGRATION_3_4, AppDatabase.MIGRATION_4_5)
            .build();
    measurementDAO = appDB.measurementDAO();
    userDAO = appDB.userDAO();
}
 
Example #3
Source File: StuffDatabase.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
static StuffDatabase create(Context ctxt, boolean memoryOnly, boolean truncate) {
  RoomDatabase.Builder<StuffDatabase> b;

  if (memoryOnly) {
    b=Room.inMemoryDatabaseBuilder(ctxt.getApplicationContext(),
      StuffDatabase.class);
  }
  else {
    b=Room.databaseBuilder(ctxt.getApplicationContext(), StuffDatabase.class,
      DB_NAME);
  }

  if (truncate) {
    b.setJournalMode(JournalMode.TRUNCATE);
  }

  b.openHelperFactory(SafeHelperFactory.fromUser(new SpannableStringBuilder("sekrit")));

  return(b.build());
}
 
Example #4
Source File: DokitViewManager.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
public DokitDatabase getDb() {
    if (mDB != null) {
        return mDB;
    }

    mDB = Room.databaseBuilder(DoraemonKit.APPLICATION,
            DokitDatabase.class,
            "dokit-database")
            //下面注释表示允许主线程进行数据库操作,但是不推荐这样做。
            //他可能造成主线程lock以及anr
            //所以我们的操作都是在新线程完成的
            .allowMainThreadQueries()
            .build();

    return mDB;
}
 
Example #5
Source File: WordRoomDatabase.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 6 votes vote down vote up
public static WordRoomDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (WordRoomDatabase.class) {
            if (INSTANCE == null) {
                // database creation goes here

                /*
                 * use databaseBuilder() to build Room database named "word_database".
                 * It uses destructive database migration (if database schema is changed, destroy and recreate database)
                 * Destructive migration is not preferred in real world apps
                 * */
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(), WordRoomDatabase.class, "word_database")
                        .fallbackToDestructiveMigration()
                        .addCallback(sRoomDatabaseCallback)
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
Example #6
Source File: WordRoomDatabase.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 6 votes vote down vote up
public static WordRoomDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (WordRoomDatabase.class) {
            if (INSTANCE == null) {
                // database creation goes here

                /*
                 * use databaseBuilder() to build Room database named "word_database".
                 * It uses destructive database migration (if database schema is changed, destroy and recreate database)
                 * Destructive migration is not preferred in real world apps
                 * */
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(), WordRoomDatabase.class, "word_database")
                        .fallbackToDestructiveMigration()
                        .addCallback(sRoomDatabaseCallback)
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
Example #7
Source File: WordRoomDatabase.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 6 votes vote down vote up
public static WordRoomDatabase getDatabase(final Context context){
    if (INSTANCE == null){
        synchronized (WordRoomDatabase.class){
            if (INSTANCE == null){
                // database creation goes here

                /*
                * use databaseBuilder() to build Room database named "word_database".
                * It uses destructive database migration (if database schema is changed, destroy and recreate database)
                * Destructive migration is not preferred in real world apps
                * */
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(), WordRoomDatabase.class, "word_database")
                        .fallbackToDestructiveMigration()
                        .addCallback(sRoomDatabaseCallback)
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
Example #8
Source File: LoginsDatabase.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
public static LoginsDatabase getDatabase(Context context) {
    if (INSTANCE == null) {
        synchronized (LoginsDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(), LoginsDatabase.class,
                        DATABASE_NAME).allowMainThreadQueries().build();
            }
        }
    }
    return INSTANCE;

}
 
Example #9
Source File: SearchHistoryDaoTest.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Before
public void before() throws Exception {
  // using an in-memory database because the information stored here disappears when the
  // process is killed
  database = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
    SearchHistoryDatabase.class)
    // allowing main thread queries, just for testing
    .allowMainThreadQueries()
    .build();

  searchHistoryDao = database.searchHistoryDao();
}
 
Example #10
Source File: MigrateCryptTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@Test
public void encryptAndMigrate() throws IOException {
  assertEquals(SQLCipherUtils.State.DOES_NOT_EXIST, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME));

  TestV1Database v1 = Room.databaseBuilder(ctxt, TestV1Database.class, DB_NAME).build();
  String keyOne = UUID.randomUUID().toString();
  TestEntity entity = new TestEntity(keyOne);

  v1.testStore().insert(entity);

  List<TestEntity> entitiesOne = v1.testStore().loadAll();

  assertEquals(1, entitiesOne.size());
  assertEquals(keyOne, entitiesOne.get(0).id);

  v1.close();

  assertEquals(SQLCipherUtils.State.UNENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME));
  SQLCipherUtils.encrypt(ctxt, ctxt.getDatabasePath(DB_NAME), PASSPHRASE.toCharArray());
  assertEquals(SQLCipherUtils.State.ENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME));

  SafeHelperFactory factory=
      SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE));
  TestV2Database v2 = Room.databaseBuilder(ctxt, TestV2Database.class, DB_NAME)
      .addMigrations(TestV2Database.MIGRATION_1_2)
      .openHelperFactory(factory)
      .build();

  List<com.commonsware.cwac.saferoom.test.room.migratecrypt.v2.TestEntity> entitiesTwo =
      v2.testStore().loadAll();

  assertEquals(1, entitiesTwo.size());
  assertEquals(keyOne, entitiesTwo.get(0).id);
}
 
Example #11
Source File: GanderPersistence.java    From Gander with Apache License 2.0 5 votes vote down vote up
public static GanderStorage getInstance(Context context) {
    if (GANDER_DATABASE_INSTANCE == null) {
        GANDER_DATABASE_INSTANCE = Room.databaseBuilder(context, GanderPersistence.class, "GanderDatabase")
                .fallbackToDestructiveMigration()
                .build();
    }
    return GANDER_DATABASE_INSTANCE;
}
 
Example #12
Source File: DeadlineDatabase.java    From Deadline with GNU General Public License v3.0 5 votes vote down vote up
public static DeadlineDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (DeadlineDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        DeadlineDatabase.class, "event_database.db")
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
Example #13
Source File: SearchHistoryDatabase.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static SearchHistoryDatabase buildDatabase(final Context appContext) {
  return Room.databaseBuilder(appContext, SearchHistoryDatabase.class, DATABASE_NAME).addCallback(
    new Callback() {
      @Override
      public void onCreate(@NonNull SupportSQLiteDatabase db) {
        super.onCreate(db);
        SearchHistoryDatabase database = SearchHistoryDatabase.getInstance(appContext);
        database.setDatabaseCreated();
      }
    }).build();
}
 
Example #14
Source File: WordRoomDatabase.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
static WordRoomDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (WordRoomDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(), WordRoomDatabase.class, "word_database")
                        .fallbackToDestructiveMigration()
                        .addCallback(sRoomDatabaseCallback)
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
Example #15
Source File: RemindersDatabase.java    From BaldPhone with Apache License 2.0 5 votes vote down vote up
public static RemindersDatabase getInstance(Context context) {
    synchronized (LOCK) {
        if (remindersDatabase == null)
            remindersDatabase = Room.databaseBuilder(context.getApplicationContext(),
                    RemindersDatabase.class, "reminders")
                    .allowMainThreadQueries()
                    .build();
        return remindersDatabase;
    }
}
 
Example #16
Source File: AppsDatabase.java    From BaldPhone with Apache License 2.0 5 votes vote down vote up
public static AppsDatabase getInstance(Context context) {
    synchronized (LOCK) {
        if (appsDatabase == null)
            appsDatabase = Room.databaseBuilder(context.getApplicationContext(),
                    AppsDatabase.class, "applications").allowMainThreadQueries().build();
        return appsDatabase;
    }
}
 
Example #17
Source File: RoomFileDatabase.java    From ArchPackages with GNU General Public License v3.0 5 votes vote down vote up
public static RoomFileDatabase getRoomFileDatabase(final Context context) {
    if (roomFileDatabase == null) {
        synchronized (RoomFileDatabase.class) {
            if (roomFileDatabase == null) {
                roomFileDatabase = Room.databaseBuilder(context.getApplicationContext(),
                        RoomFileDatabase.class,
                        "room_file_database")
                        .build();
            }
        }
    }
    return roomFileDatabase;
}
 
Example #18
Source File: NoteRoomDatabase.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
static NoteRoomDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (NoteRoomDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(), NoteRoomDatabase.class, "note_database")
                        .fallbackToDestructiveMigration()
                        .addCallback(sRoomDatabaseCallback)
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
Example #19
Source File: HistoryDatabase.java    From SecScanQR with GNU General Public License v3.0 5 votes vote down vote up
public static synchronized HistoryDatabase getInstance(Context context){
    if (instance == null){
        historyDatabaseHelper = new DatabaseHelper(context);
        instance = Room.databaseBuilder(context.getApplicationContext(), HistoryDatabase.class, "history_database").fallbackToDestructiveMigration().addCallback(roomCallback).build();
    }
    return instance;
}
 
Example #20
Source File: AppDatabase.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
static AppDatabase getDatabase(Context context) {
	if (instance == null) {
		synchronized (AppDatabase.class) {
			if (instance == null) {
				instance = Room.databaseBuilder(context.getApplicationContext(),
						AppDatabase.class, "apps-database.db").build();
			}
		}
	}
	return instance;
}
 
Example #21
Source File: WordRoomDatabase.java    From android-room-with-a-view with Apache License 2.0 5 votes vote down vote up
static WordRoomDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (WordRoomDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        WordRoomDatabase.class, "word_database")
                        .addCallback(sRoomDatabaseCallback)
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
Example #22
Source File: WordDaoTest.java    From android-room-with-a-view with Apache License 2.0 5 votes vote down vote up
@Before
public void createDb() {
    Context context = ApplicationProvider.getApplicationContext();
    // Using an in-memory database because the information stored here disappears when the
    // process is killed.
    mDb = Room.inMemoryDatabaseBuilder(context, WordRoomDatabase.class)
            // Allowing main thread queries, just for testing.
            .allowMainThreadQueries()
            .build();
    mWordDao = mDb.wordDao();
}
 
Example #23
Source File: ZephyrDatabase.java    From zephyr with MIT License 5 votes vote down vote up
public static ZephyrDatabase buildDatabase(final Context appContext) {
    return Room.databaseBuilder(appContext, ZephyrDatabase.class, Constants.DB_NAME)
            .addMigrations(DatabaseMigrationFactory.getMigrations())
            .fallbackToDestructiveMigration()
            .fallbackToDestructiveMigrationOnDowngrade()
            .build();
}
 
Example #24
Source File: ActivityRoomDatabase.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
static ActivityRoomDatabase getInstance(Context context) {
    if (INSTANCE == null) {
        synchronized (ActivityRoomDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        ActivityRoomDatabase.class,"activity_database.db")
                        .setJournalMode(JournalMode.TRUNCATE)
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
Example #25
Source File: UserDataBase.java    From CloudReader with Apache License 2.0 5 votes vote down vote up
public static UserDataBase getDatabase() {
    if (sInstance == null) {
        sInstance = Room.databaseBuilder(RootApplication.getContext(),
                UserDataBase.class, "User.db")
                .addMigrations(MIGRATION_2_3)
                .build();
    }
    return sInstance;
}
 
Example #26
Source File: MaterialisticDatabase.java    From materialistic with Apache License 2.0 5 votes vote down vote up
public static synchronized MaterialisticDatabase getInstance(Context context) {
    if (sInstance == null) {
        sInstance = setupBuilder(Room.databaseBuilder(context.getApplicationContext(),
                MaterialisticDatabase.class,
                DbConstants.DB_NAME))
                .build();
    }
    return sInstance;
}
 
Example #27
Source File: InMemoryDatabase.java    From materialistic with Apache License 2.0 5 votes vote down vote up
public static synchronized MaterialisticDatabase getInstance(Context context) {
    if (sInstance == null) {
        sInstance = setupBuilder(Room.inMemoryDatabaseBuilder(context.getApplicationContext(),
                MaterialisticDatabase.class))
                .allowMainThreadQueries()
                .build();
    }
    return sInstance;
}
 
Example #28
Source File: PlayerModule.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
public PlaybackStateDatabase providePlaybackStateDatabase(Context context) {
    return Room.databaseBuilder(context, PlaybackStateDatabase.class, "playerState")
            .allowMainThreadQueries()
            .build();
}
 
Example #29
Source File: DatabaseTest.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void initDatabase() {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    appDB = Room.inMemoryDatabaseBuilder(context, AppDatabase.class).build();
    userDao = appDB.userDAO();
    measurementDAO = appDB.measurementDAO();
}
 
Example #30
Source File: RestApplication.java    From android-rest-client-template with MIT License 5 votes vote down vote up
@Override
  public void onCreate() {
      super.onCreate();
      // when upgrading versions, kill the original tables by using
// fallbackToDestructiveMigration()
      myDatabase = Room.databaseBuilder(this, MyDatabase.class,
              MyDatabase.NAME).fallbackToDestructiveMigration().build();

      // use chrome://inspect to inspect your SQL database
      Stetho.initializeWithDefaults(this);
  }