diff --git a/application/mediadb/src/main/java/org/videolan/vlc/database/EqualizerDao.kt b/application/mediadb/src/main/java/org/videolan/vlc/database/EqualizerDao.kt new file mode 100644 index 000000000..249358197 --- /dev/null +++ b/application/mediadb/src/main/java/org/videolan/vlc/database/EqualizerDao.kt @@ -0,0 +1,54 @@ +/* + * ************************************************************************ + * EqualizerDao.kt + * ************************************************************************* + * Copyright © 2024 VLC authors and VideoLAN + * Author: Nicolas POMEPUY + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * ************************************************************************** + * + * + */ + +package org.videolan.vlc.database + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import androidx.room.Transaction +import kotlinx.coroutines.flow.Flow +import org.videolan.vlc.mediadb.models.EqualizerBand +import org.videolan.vlc.mediadb.models.EqualizerEntry +import org.videolan.vlc.mediadb.models.EqualizerWithBands + +@Dao +interface EqualizerDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(equalizerEntry: EqualizerEntry): Long + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertBands(equalizerBand: EqualizerBand) + + @Delete + fun delete(equalizerEntry: EqualizerEntry) + + @Transaction + @Query("SELECT * FROM equalizer_entry") + fun getAllEqualizerEntries(): Flow> + + +} diff --git a/application/mediadb/src/main/java/org/videolan/vlc/database/MediaDatabase.kt b/application/mediadb/src/main/java/org/videolan/vlc/database/MediaDatabase.kt index 18a90f78c..3fef848e1 100644 --- a/application/mediadb/src/main/java/org/videolan/vlc/database/MediaDatabase.kt +++ b/application/mediadb/src/main/java/org/videolan/vlc/database/MediaDatabase.kt @@ -29,11 +29,17 @@ import androidx.sqlite.db.SupportSQLiteDatabase import org.videolan.resources.AndroidDevices import org.videolan.tools.SingletonHolder import org.videolan.vlc.mediadb.Converters -import org.videolan.vlc.mediadb.models.* +import org.videolan.vlc.mediadb.models.BrowserFav +import org.videolan.vlc.mediadb.models.CustomDirectory +import org.videolan.vlc.mediadb.models.EqualizerBand +import org.videolan.vlc.mediadb.models.EqualizerEntry +import org.videolan.vlc.mediadb.models.ExternalSub +import org.videolan.vlc.mediadb.models.Slave +import org.videolan.vlc.mediadb.models.Widget private const val DB_NAME = "vlc_database" -@Database(entities = [ExternalSub::class, Slave::class, BrowserFav::class, CustomDirectory::class, Widget::class], version = 36, exportSchema = false) +@Database(entities = [ExternalSub::class, Slave::class, BrowserFav::class, CustomDirectory::class, Widget::class, EqualizerBand::class, EqualizerEntry::class], version = 37, exportSchema = false) @TypeConverters(Converters::class) abstract class MediaDatabase: RoomDatabase() { abstract fun externalSubDao(): ExternalSubDao @@ -41,6 +47,7 @@ abstract class MediaDatabase: RoomDatabase() { abstract fun browserFavDao(): BrowserFavDao abstract fun widgetDao(): WidgetDao abstract fun customDirectoryDao(): CustomDirectoryDao + abstract fun equalizerDao(): EqualizerDao companion object : SingletonHolder({ buildDatabase(it.applicationContext) }) } @@ -55,7 +62,7 @@ private fun buildDatabase(context: Context) = Room.databaseBuilder(context.appli migration_21_22, migration_22_23, migration_23_24, migration_24_25, migration_25_26, migration_26_27, migration_27_28, migration_28_29, migration_29_30, migration_30_31, migration_31_32, migration_32_33, - migration_33_34, migration_34_35, migration_35_36) + migration_33_34, migration_34_35, migration_35_36, migration_36_37) .addCallback(object : RoomDatabase.Callback() { override fun onCreate(db: SupportSQLiteDatabase) { if (!AndroidDevices.isTv) populateDB(context) } }) diff --git a/application/mediadb/src/main/java/org/videolan/vlc/database/Migrations.kt b/application/mediadb/src/main/java/org/videolan/vlc/database/Migrations.kt index ea19c43c8..0398bffcc 100644 --- a/application/mediadb/src/main/java/org/videolan/vlc/database/Migrations.kt +++ b/application/mediadb/src/main/java/org/videolan/vlc/database/Migrations.kt @@ -261,6 +261,15 @@ val migration_35_36 = object:Migration(35, 36) { } } +val migration_36_37 = object:Migration(36, 37) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("CREATE TABLE `equalizer_entry` (`name` TEXT NOT NULL, `preamp` REAL NOT NULL, `preset_index` INTEGER NOT NULL, `is_disabled` INTEGER NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)") + database.execSQL("CREATE UNIQUE INDEX `index_equalizer_entry_name` ON `equalizer_entry` (`name`)") + database.execSQL("CREATE TABLE `equalizer_band` (`index` INTEGER NOT NULL, `value` REAL NOT NULL, `equalizer_entry` INTEGER NOT NULL, PRIMARY KEY(`index`, `equalizer_entry`), FOREIGN KEY(`equalizer_entry`) REFERENCES `equalizer_entry`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )") + + } +} + @OptIn(DelicateCoroutinesApi::class) fun populateDB(context: Context) = GlobalScope.launch(Dispatchers.IO) { val uris = listOf(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MOVIES_DIRECTORY_URI, diff --git a/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerBand.kt b/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerBand.kt new file mode 100644 index 000000000..27d572ef1 --- /dev/null +++ b/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerBand.kt @@ -0,0 +1,47 @@ +/* + * ************************************************************************ + * EqualizerBand.kt + * ************************************************************************* + * Copyright © 2024 VLC authors and VideoLAN + * Author: Nicolas POMEPUY + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * ************************************************************************** + * + * + */ + +package org.videolan.vlc.mediadb.models + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.ForeignKey + +@Entity( + tableName = "equalizer_band", + primaryKeys = ["index", "equalizer_entry"], + foreignKeys = [ForeignKey(entity = EqualizerEntry::class, + parentColumns = ["id"], + childColumns = ["equalizer_entry"], + onDelete = ForeignKey.CASCADE)] +) +data class EqualizerBand( + @ColumnInfo(name = "index") + val index: Int, + @ColumnInfo(name = "value") + val bandValue: Float, +) { + @ColumnInfo(name = "equalizer_entry") + var equalizerEntry: Long = 0 +} diff --git a/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerEntry.kt b/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerEntry.kt new file mode 100644 index 000000000..0a4c31cfd --- /dev/null +++ b/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerEntry.kt @@ -0,0 +1,46 @@ +/* + * ************************************************************************ + * EqualizerEntry.kt + * ************************************************************************* + * Copyright © 2024 VLC authors and VideoLAN + * Author: Nicolas POMEPUY + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * ************************************************************************** + * + * + */ + +package org.videolan.vlc.mediadb.models + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.Index +import androidx.room.PrimaryKey + +@Entity(tableName = "equalizer_entry", indices = [Index(value = ["name"], unique = true)]) +data class EqualizerEntry( + + @ColumnInfo(name = "name") + val name: String, + @ColumnInfo(name = "preamp") + val preamp: Float, + @ColumnInfo(name = "preset_index") + val presetIndex: Int = -1, + @ColumnInfo(name = "is_disabled") + val isDisabled: Boolean = false, +) { + @PrimaryKey(autoGenerate = true) + var id: Long = 0 +} diff --git a/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerWithBands.kt b/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerWithBands.kt new file mode 100644 index 000000000..11421fd85 --- /dev/null +++ b/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/EqualizerWithBands.kt @@ -0,0 +1,37 @@ +/* + * ************************************************************************ + * EqualizerWithBands.kt + * ************************************************************************* + * Copyright © 2024 VLC authors and VideoLAN + * Author: Nicolas POMEPUY + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * ************************************************************************** + * + * + */ + +package org.videolan.vlc.mediadb.models + +import androidx.room.Embedded +import androidx.room.Relation + +data class EqualizerWithBands( + @Embedded val equalizerEntry: EqualizerEntry, + @Relation( + parentColumn = "id", + entityColumn = "equalizer_entry" + ) + val bands: List +) \ No newline at end of file diff --git a/application/vlc-android/src/org/videolan/vlc/repository/EqualizerRepository.kt b/application/vlc-android/src/org/videolan/vlc/repository/EqualizerRepository.kt new file mode 100644 index 000000000..ebf3a941b --- /dev/null +++ b/application/vlc-android/src/org/videolan/vlc/repository/EqualizerRepository.kt @@ -0,0 +1,56 @@ +/* + * ************************************************************************ + * EqualizerRepository.kt + * ************************************************************************* + * Copyright © 2024 VLC authors and VideoLAN + * Author: Nicolas POMEPUY + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * ************************************************************************** + * + * + */ + +package org.videolan.vlc.repository + +import android.content.Context +import org.videolan.tools.CoroutineContextProvider +import org.videolan.tools.SingletonHolder +import org.videolan.vlc.database.EqualizerDao +import org.videolan.vlc.database.MediaDatabase +import org.videolan.vlc.mediadb.models.EqualizerEntry +import org.videolan.vlc.mediadb.models.EqualizerWithBands + +class EqualizerRepository(private val equalizerDao: EqualizerDao, private val coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) { + + + val equalizerEntries by lazy { + equalizerDao.getAllEqualizerEntries() + } + + fun addOrUpdateEqualizer(equalizer:EqualizerWithBands) { + val id = equalizerDao.insert(equalizer.equalizerEntry) + equalizer.bands.forEach { + it.equalizerEntry = id + equalizerDao.insertBands(it) + } + } + + fun delete(equalizerEntry: EqualizerEntry) { + equalizerDao.delete(equalizerEntry) + } + + + companion object : SingletonHolder({ EqualizerRepository(MediaDatabase.getInstance(it).equalizerDao()) }) +} \ No newline at end of file