PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/dialogs/SongDetailDialog.kt

179 lines
7.9 KiB
Kotlin
Raw Normal View History

2019-03-03 09:20:15 +00:00
/*
2020-10-06 08:46:04 +00:00
* Copyright (c) 2020 Hemanth Savarla.
2019-03-03 09:20:15 +00:00
*
* Licensed under the GNU General Public License v3
*
2020-10-06 08:46:04 +00:00
* 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.
2019-03-03 09:20:15 +00:00
*
* 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.
2020-10-06 08:46:04 +00:00
*
2019-03-03 09:20:15 +00:00
*/
2018-12-06 08:52:57 +00:00
package code.name.monkey.retromusic.dialogs
2020-05-23 21:47:23 +00:00
import android.annotation.SuppressLint
2019-05-15 20:55:57 +00:00
import android.app.Dialog
2018-12-06 08:52:57 +00:00
import android.content.Context
import android.os.Bundle
import android.text.Spanned
import android.util.Log
2020-05-23 21:47:23 +00:00
import android.view.LayoutInflater
2018-12-06 08:52:57 +00:00
import android.widget.TextView
2019-05-15 20:55:57 +00:00
import androidx.annotation.NonNull
2020-03-01 12:05:43 +00:00
import androidx.core.text.HtmlCompat
2019-05-15 20:55:57 +00:00
import androidx.fragment.app.DialogFragment
2020-05-23 22:26:45 +00:00
import code.name.monkey.retromusic.EXTRA_SONG
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.R
2020-07-19 11:21:15 +00:00
import code.name.monkey.retromusic.extensions.colorButtons
2020-07-19 14:39:04 +00:00
import code.name.monkey.retromusic.extensions.materialDialog
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.MusicUtil
2020-10-06 08:46:04 +00:00
import java.io.File
import java.io.IOException
2018-12-06 08:52:57 +00:00
import org.jaudiotagger.audio.AudioFileIO
import org.jaudiotagger.audio.exceptions.CannotReadException
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException
import org.jaudiotagger.tag.TagException
2019-05-15 20:55:57 +00:00
class SongDetailDialog : DialogFragment() {
2018-12-06 08:52:57 +00:00
2020-05-23 21:47:23 +00:00
@SuppressLint("InflateParams")
2019-05-15 20:55:57 +00:00
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
2019-09-17 19:36:13 +00:00
val context: Context = requireContext()
2020-05-23 21:47:23 +00:00
val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_file_details, null)
2019-05-15 20:55:57 +00:00
2020-05-23 22:26:45 +00:00
val song = requireArguments().getParcelable<Song>(EXTRA_SONG)
2019-05-15 20:55:57 +00:00
val fileName: TextView = dialogView.findViewById(R.id.fileName)
val filePath: TextView = dialogView.findViewById(R.id.filePath)
val fileSize: TextView = dialogView.findViewById(R.id.fileSize)
val fileFormat: TextView = dialogView.findViewById(R.id.fileFormat)
val trackLength: TextView = dialogView.findViewById(R.id.trackLength)
val bitRate: TextView = dialogView.findViewById(R.id.bitrate)
val samplingRate: TextView = dialogView.findViewById(R.id.samplingRate)
2020-05-23 21:47:23 +00:00
fileName.text = makeTextWithTitle(context, R.string.label_file_name, "-")
filePath.text = makeTextWithTitle(context, R.string.label_file_path, "-")
fileSize.text = makeTextWithTitle(context, R.string.label_file_size, "-")
fileFormat.text = makeTextWithTitle(context, R.string.label_file_format, "-")
trackLength.text = makeTextWithTitle(context, R.string.label_track_length, "-")
bitRate.text = makeTextWithTitle(context, R.string.label_bit_rate, "-")
samplingRate.text = makeTextWithTitle(context, R.string.label_sampling_rate, "-")
2018-12-06 08:52:57 +00:00
if (song != null) {
val songFile = File(song.data)
if (songFile.exists()) {
2020-05-23 21:47:23 +00:00
fileName.text = makeTextWithTitle(context, R.string.label_file_name, songFile.name)
filePath.text =
2020-05-23 21:47:23 +00:00
makeTextWithTitle(context, R.string.label_file_path, songFile.absolutePath)
2020-02-01 17:53:26 +00:00
fileSize.text =
makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_file_size,
getFileSizeString(songFile.length())
)
2018-12-06 08:52:57 +00:00
try {
val audioFile = AudioFileIO.read(songFile)
val audioHeader = audioFile.audioHeader
fileFormat.text =
2020-05-23 21:47:23 +00:00
makeTextWithTitle(context, R.string.label_file_format, audioHeader.format)
2020-02-01 17:53:26 +00:00
trackLength.text = makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_track_length,
2020-02-01 17:53:26 +00:00
MusicUtil.getReadableDurationString((audioHeader.trackLength * 1000).toLong())
)
bitRate.text = makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_bit_rate,
audioHeader.bitRate + " kb/s"
)
2020-02-01 17:53:26 +00:00
samplingRate.text =
makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_sampling_rate,
audioHeader.sampleRate + " Hz"
)
2019-05-15 20:55:57 +00:00
} catch (@NonNull e: CannotReadException) {
2018-12-06 08:52:57 +00:00
Log.e(TAG, "error while reading the song file", e)
// fallback
2020-02-01 17:53:26 +00:00
trackLength.text = makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_track_length,
2020-02-01 17:53:26 +00:00
MusicUtil.getReadableDurationString(song.duration)
)
2019-05-15 20:55:57 +00:00
} catch (@NonNull e: IOException) {
2018-12-06 08:52:57 +00:00
Log.e(TAG, "error while reading the song file", e)
2020-02-01 17:53:26 +00:00
trackLength.text = makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_track_length,
2020-02-01 17:53:26 +00:00
MusicUtil.getReadableDurationString(song.duration)
)
2019-05-15 20:55:57 +00:00
} catch (@NonNull e: TagException) {
2018-12-06 08:52:57 +00:00
Log.e(TAG, "error while reading the song file", e)
2020-02-01 17:53:26 +00:00
trackLength.text = makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_track_length,
2020-02-01 17:53:26 +00:00
MusicUtil.getReadableDurationString(song.duration)
)
2019-05-15 20:55:57 +00:00
} catch (@NonNull e: ReadOnlyFileException) {
2018-12-06 08:52:57 +00:00
Log.e(TAG, "error while reading the song file", e)
2020-02-01 17:53:26 +00:00
trackLength.text = makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_track_length,
2020-02-01 17:53:26 +00:00
MusicUtil.getReadableDurationString(song.duration)
)
2019-05-15 20:55:57 +00:00
} catch (@NonNull e: InvalidAudioFrameException) {
2018-12-06 08:52:57 +00:00
Log.e(TAG, "error while reading the song file", e)
2020-02-01 17:53:26 +00:00
trackLength.text = makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_track_length,
2020-02-01 17:53:26 +00:00
MusicUtil.getReadableDurationString(song.duration)
)
2018-12-06 08:52:57 +00:00
}
} else {
// fallback
2020-05-23 21:47:23 +00:00
fileName.text = makeTextWithTitle(context, R.string.label_file_name, song.title)
2020-02-01 17:53:26 +00:00
trackLength.text = makeTextWithTitle(
context,
2020-05-23 21:47:23 +00:00
R.string.label_track_length,
2020-02-01 17:53:26 +00:00
MusicUtil.getReadableDurationString(song.duration)
)
2018-12-06 08:52:57 +00:00
}
}
2020-07-19 14:39:04 +00:00
return materialDialog(R.string.action_details)
2020-09-09 12:37:25 +00:00
.setPositiveButton(android.R.string.ok, null)
2020-05-23 21:47:23 +00:00
.setView(dialogView)
.create()
2020-07-19 11:21:15 +00:00
.colorButtons()
2018-12-06 08:52:57 +00:00
}
companion object {
2018-12-10 15:28:13 +00:00
val TAG: String = SongDetailDialog::class.java.simpleName
2018-12-06 08:52:57 +00:00
fun create(song: Song): SongDetailDialog {
val dialog = SongDetailDialog()
val args = Bundle()
2020-05-23 22:26:45 +00:00
args.putParcelable(EXTRA_SONG, song)
2018-12-06 08:52:57 +00:00
dialog.arguments = args
return dialog
}
private fun makeTextWithTitle(context: Context, titleResId: Int, text: String?): Spanned {
2020-03-01 12:05:43 +00:00
return HtmlCompat.fromHtml(
"<b>" + context.resources.getString(titleResId) + ": " + "</b>" + text,
HtmlCompat.FROM_HTML_MODE_LEGACY
)
2018-12-06 08:52:57 +00:00
}
private fun getFileSizeString(sizeInBytes: Long): String {
val fileSizeInKB = sizeInBytes / 1024
val fileSizeInMB = fileSizeInKB / 1024
2019-03-04 03:55:09 +00:00
return "$fileSizeInMB MB"
2018-12-06 08:52:57 +00:00
}
}
}