PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsMusicServiceFragment.kt

134 lines
4.0 KiB
Kotlin
Raw Normal View History

2020-10-06 08:46:04 +00:00
/*
* Copyright (c) 2020 Hemanth Savarla.
*
* 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.
*
*/
2019-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.fragments.base
2018-11-30 01:06:16 +00:00
import android.content.Context
2020-01-06 03:12:32 +00:00
import android.net.Uri
2018-11-30 01:06:16 +00:00
import android.os.Bundle
import android.view.View
2020-01-06 03:12:32 +00:00
import android.webkit.MimeTypeMap
import androidx.annotation.LayoutRes
2018-11-30 01:06:16 +00:00
import androidx.fragment.app.Fragment
2020-08-11 21:31:09 +00:00
import androidx.navigation.navOptions
import code.name.monkey.retromusic.R
2019-04-20 05:29:45 +00:00
import code.name.monkey.retromusic.activities.base.AbsMusicServiceActivity
import code.name.monkey.retromusic.interfaces.IMusicServiceEventListener
2020-01-06 03:12:32 +00:00
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.RetroUtil
import java.io.File
import java.net.URLEncoder
import java.util.*
2020-10-06 08:46:04 +00:00
import org.jaudiotagger.audio.AudioFileIO
2018-11-30 01:06:16 +00:00
/**
* Created by hemanths on 18/08/17.
*/
open class AbsMusicServiceFragment(@LayoutRes layout: Int) : Fragment(layout),
IMusicServiceEventListener {
2020-08-13 17:48:49 +00:00
2020-08-11 21:31:09 +00:00
val navOptions by lazy {
navOptions {
2020-08-13 17:48:49 +00:00
launchSingleTop = false
2020-08-11 21:31:09 +00:00
anim {
enter = R.anim.retro_fragment_open_enter
exit = R.anim.retro_fragment_open_exit
popEnter = R.anim.retro_fragment_close_enter
popExit = R.anim.retro_fragment_close_exit
}
}
}
2020-08-13 17:48:49 +00:00
var serviceActivity: AbsMusicServiceActivity? = null
2018-11-30 01:06:16 +00:00
private set
2019-03-25 12:43:43 +00:00
override fun onAttach(context: Context) {
2018-11-30 01:06:16 +00:00
super.onAttach(context)
try {
serviceActivity = context as AbsMusicServiceActivity?
2018-11-30 01:06:16 +00:00
} catch (e: ClassCastException) {
2019-03-25 12:43:43 +00:00
throw RuntimeException(context.javaClass.simpleName + " must be an instance of " + AbsMusicServiceActivity::class.java.simpleName)
2018-11-30 01:06:16 +00:00
}
}
override fun onDetach() {
super.onDetach()
serviceActivity = null
2018-11-30 01:06:16 +00:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
serviceActivity?.addMusicServiceEventListener(this)
2018-11-30 01:06:16 +00:00
}
override fun onDestroyView() {
super.onDestroyView()
serviceActivity?.removeMusicServiceEventListener(this)
2018-11-30 01:06:16 +00:00
}
override fun onPlayingMetaChanged() {
}
override fun onServiceConnected() {
}
override fun onServiceDisconnected() {
}
override fun onQueueChanged() {
}
override fun onPlayStateChanged() {
}
override fun onRepeatModeChanged() {
}
override fun onShuffleModeChanged() {
}
override fun onMediaStoreChanged() {
2020-01-06 03:12:32 +00:00
}
fun getSongInfo(song: Song): String {
val file = File(song.data)
if (file.exists()) {
2020-02-01 18:14:04 +00:00
return try {
val audioHeader = AudioFileIO.read(File(song.data)).audioHeader
val string: StringBuilder = StringBuilder()
val uriFile = Uri.fromFile(file)
string.append(getMimeType(uriFile.toString())).append("")
string.append(audioHeader.bitRate).append(" kb/s").append("")
string.append(RetroUtil.frequencyCount(audioHeader.sampleRate.toInt()))
.append(" kHz")
2020-02-01 18:14:04 +00:00
string.toString()
} catch (er: Exception) {
" - "
}
2020-01-06 03:12:32 +00:00
}
return "-"
}
2018-11-30 01:06:16 +00:00
2020-01-06 03:12:32 +00:00
private fun getMimeType(url: String): String? {
var type: String? = MimeTypeMap.getFileExtensionFromUrl(
URLEncoder.encode(url, "utf-8")
).toUpperCase(Locale.getDefault())
if (type == null) {
type = url.substring(url.lastIndexOf(".") + 1)
}
return type
2018-11-30 01:06:16 +00:00
}
}