2017-08-07 06:02:30 -07:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.room.Room;
|
2017-08-07 06:02:30 -07:00
|
|
|
import android.content.Context;
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2017-08-07 06:02:30 -07:00
|
|
|
|
|
|
|
import org.schabi.newpipe.database.AppDatabase;
|
|
|
|
|
|
|
|
import static org.schabi.newpipe.database.AppDatabase.DATABASE_NAME;
|
2018-01-17 13:24:59 -08:00
|
|
|
import static org.schabi.newpipe.database.Migrations.MIGRATION_11_12;
|
2017-08-07 06:02:30 -07:00
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
public final class NewPipeDatabase {
|
2017-08-07 06:02:30 -07:00
|
|
|
|
2018-03-08 10:39:24 -03:00
|
|
|
private static volatile AppDatabase databaseInstance;
|
2017-08-07 06:02:30 -07:00
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
private NewPipeDatabase() {
|
|
|
|
//no instance
|
|
|
|
}
|
|
|
|
|
2018-03-08 10:39:24 -03:00
|
|
|
private static AppDatabase getDatabase(Context context) {
|
|
|
|
return Room
|
|
|
|
.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
|
2018-01-17 13:24:59 -08:00
|
|
|
.addMigrations(MIGRATION_11_12)
|
2018-01-17 13:53:32 -08:00
|
|
|
.fallbackToDestructiveMigration()
|
2018-01-17 13:24:59 -08:00
|
|
|
.build();
|
2017-09-03 03:04:18 -03:00
|
|
|
}
|
2017-08-07 06:02:30 -07:00
|
|
|
|
|
|
|
@NonNull
|
2018-03-08 10:39:24 -03:00
|
|
|
public static AppDatabase getInstance(@NonNull Context context) {
|
|
|
|
AppDatabase result = databaseInstance;
|
|
|
|
if (result == null) {
|
|
|
|
synchronized (NewPipeDatabase.class) {
|
|
|
|
result = databaseInstance;
|
|
|
|
if (result == null) {
|
|
|
|
databaseInstance = (result = getDatabase(context));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-01-15 12:30:52 -08:00
|
|
|
}
|
2017-08-07 06:02:30 -07:00
|
|
|
}
|