Code refactor

This commit is contained in:
Hemanth S 2020-06-09 23:34:22 +05:30
parent 3c7328f9c6
commit 10874744f3
28 changed files with 771 additions and 1205 deletions

View file

@ -1,124 +0,0 @@
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This 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 3 of the License, or (at your option) any later version.
*
* This software 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.
*/
package code.name.monkey.retromusic.helper
import android.media.audiofx.BassBoost
import android.media.audiofx.Equalizer
import android.media.audiofx.Virtualizer
import android.util.Log
import code.name.monkey.retromusic.interfaces.EqualizerInterface
/**
* @author Hemanth S (h4h13).
*/
class EqualizerHelper private constructor() : EqualizerInterface {
override val equalizer: Equalizer
override val bassBoost: BassBoost
override val virtualizer: Virtualizer
override val bandLevelHigh: Int
override val bandLevelLow: Int
override var isRunning = false
override val numberOfBands: Int
get() = equalizer.numberOfBands.toInt()
override var isBassBoostEnabled: Boolean
get() = bassBoost.enabled
set(isEnabled) {
bassBoost.enabled = isEnabled
}
override var bassBoostStrength: Int
get() = bassBoost.roundedStrength.toInt()
set(strength) = bassBoost.setStrength(strength.toShort())
override var isVirtualizerEnabled: Boolean
get() = virtualizer.enabled
set(isEnabled) {
virtualizer.enabled = isEnabled
}
override var virtualizerStrength: Int
get() = virtualizer.roundedStrength.toInt()
set(strength) = virtualizer.setStrength(strength.toShort())
init {
//Prevent form the reflection api.
if (ourInstance != null) {
throw RuntimeException("Use getInstance() method to get the single instance of this class.")
}
val i = MusicPlayerRemote.audioSessionId
equalizer = Equalizer(100, i)
equalizer.enabled = true
bassBoost = BassBoost(100, i)
virtualizer = Virtualizer(100, i)
bandLevelHigh = equalizer.bandLevelRange[1].toInt()
bandLevelLow = equalizer.bandLevelRange[0].toInt()
Log.i(TAG, "onCreate: $bandLevelHigh $bandLevelLow")
isRunning = true
}
//Make singleton from serialize and deserialize operation.
protected fun readResolve(): EqualizerHelper? {
return instance
}
override fun getCenterFreq(band: Int): Int {
return equalizer.getCenterFreq(band.toShort())
}
override fun getBandLevel(band: Int): Int {
return equalizer.getBandLevel(band.toShort()).toInt()
}
override fun setBandLevel(band: Int, level: Int) {
equalizer.setBandLevel(band.toShort(), level.toShort())
}
companion object {
private const val TAG = "EqualizerHelper"
@Volatile
private var ourInstance: EqualizerHelper? = null
//Double check locking pattern
//Check for the first time
//Check for the second time.
//if there is no instance available... create new one
val instance: EqualizerHelper?
get() {
if (ourInstance == null) {
synchronized(EqualizerHelper::class.java) {
if (ourInstance == null) {
ourInstance = EqualizerHelper()
}
}
}
return ourInstance
}
}
}

View file

@ -1,58 +0,0 @@
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This 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 3 of the License, or (at your option) any later version.
*
* This software 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.
*/
package code.name.monkey.retromusic.helper;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import code.name.monkey.retromusic.model.Playlist;
import code.name.monkey.retromusic.model.Song;
public class M3UWriter implements M3UConstants {
@Nullable
public static File write(@NonNull Context context,
@NonNull File dir,
@NonNull Playlist playlist) throws IOException {
if (!dir.exists()) //noinspection ResultOfMethodCallIgnored
dir.mkdirs();
File file = new File(dir, playlist.name.concat("." + EXTENSION));
ArrayList<Song> songs = playlist.getSongs(context);
if (songs.size() > 0) {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(HEADER);
for (Song song : songs) {
bw.newLine();
bw.write(ENTRY + song.getDuration() + DURATION_SEPARATOR + song.getArtistName() + " - " + song.getTitle());
bw.newLine();
bw.write(song.getData());
}
bw.close();
}
return file;
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This 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 3 of the License, or (at your option) any later version.
*
* This software 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.
*/
package code.name.monkey.retromusic.helper
import android.content.Context
import code.name.monkey.retromusic.model.Playlist
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.io.IOException
object M3UWriter : M3UConstants {
@JvmStatic
@Throws(IOException::class)
fun write(
context: Context,
dir: File,
playlist: Playlist
): File? {
if (!dir.exists()) dir.mkdirs()
val file = File(dir, playlist.name + "." + M3UConstants.EXTENSION)
val songs = playlist.getSongs(context)
if (songs.size > 0) {
val bw = BufferedWriter(FileWriter(file))
bw.write(M3UConstants.HEADER)
for (song in songs) {
bw.newLine()
bw.write(M3UConstants.ENTRY + song.duration + M3UConstants.DURATION_SEPARATOR + song.artistName + " - " + song.title)
bw.newLine()
bw.write(song.data)
}
bw.close()
}
return file
}
}