PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/activities/tageditor/AlbumTagEditorActivity.kt

210 lines
7.6 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.activities.tageditor
2018-12-06 08:52:57 +00:00
import android.app.Activity
import android.content.res.ColorStateList
2019-12-01 15:45:12 +00:00
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.drawable.Drawable
2018-12-06 08:52:57 +00:00
import android.net.Uri
import android.os.Bundle
2019-12-01 15:45:12 +00:00
import android.text.Editable
import android.text.TextWatcher
2020-01-05 20:25:55 +00:00
import android.transition.Slide
2018-12-06 08:52:57 +00:00
import android.widget.Toast
2019-12-01 15:45:12 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.MaterialUtil
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.R
2019-07-30 21:46:50 +00:00
import code.name.monkey.retromusic.extensions.appHandleColor
2019-12-01 15:45:12 +00:00
import code.name.monkey.retromusic.glide.palette.BitmapPaletteTranscoder
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper
import code.name.monkey.retromusic.model.Song
2019-12-01 15:45:12 +00:00
import code.name.monkey.retromusic.util.ImageUtil
2018-12-12 20:59:07 +00:00
import code.name.monkey.retromusic.util.RetroColorUtil.generatePalette
import code.name.monkey.retromusic.util.RetroColorUtil.getColor
import com.bumptech.glide.Glide
2018-12-06 08:52:57 +00:00
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
2020-10-06 08:46:04 +00:00
import java.util.*
import kotlinx.android.synthetic.main.activity_album_tag_editor.*
2018-12-06 08:52:57 +00:00
import org.jaudiotagger.tag.FieldKey
class AlbumTagEditorActivity : AbsTagEditorActivity(), TextWatcher {
2019-12-30 11:01:50 +00:00
override val contentViewLayout: Int
get() = R.layout.activity_album_tag_editor
2020-01-05 20:25:55 +00:00
private fun windowEnterTransition() {
val slide = Slide()
slide.excludeTarget(R.id.appBarLayout, true)
slide.excludeTarget(R.id.status_bar, true)
slide.excludeTarget(android.R.id.statusBarBackground, true)
slide.excludeTarget(android.R.id.navigationBarBackground, true)
window.enterTransition = slide
}
2020-08-21 18:01:52 +00:00
override fun loadImageFromFile(selectedFile: Uri?) {
2019-12-30 11:01:50 +00:00
2020-08-21 18:01:52 +00:00
Glide.with(this@AlbumTagEditorActivity).load(selectedFile).asBitmap()
2019-12-30 11:01:50 +00:00
.transcode(BitmapPaletteTranscoder(this), BitmapPaletteWrapper::class.java)
.diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
.into(object : SimpleTarget<BitmapPaletteWrapper>() {
override fun onResourceReady(
resource: BitmapPaletteWrapper?,
glideAnimation: GlideAnimation<in BitmapPaletteWrapper>?
) {
getColor(resource?.palette, Color.TRANSPARENT)
albumArtBitmap = resource?.bitmap?.let { ImageUtil.resizeBitmap(it, 2048) }
setImageBitmap(
albumArtBitmap,
getColor(
resource?.palette,
ATHUtil.resolveColor(
this@AlbumTagEditorActivity,
R.attr.defaultFooterColor
)
2019-12-30 11:01:50 +00:00
)
)
deleteAlbumArt = false
dataChanged()
setResult(Activity.RESULT_OK)
}
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
super.onLoadFailed(e, errorDrawable)
Toast.makeText(this@AlbumTagEditorActivity, e.toString(), Toast.LENGTH_LONG)
.show()
}
})
}
private var albumArtBitmap: Bitmap? = null
private var deleteAlbumArt: Boolean = false
private fun setupToolbar() {
2020-02-22 12:10:12 +00:00
toolbar.setBackgroundColor(ATHUtil.resolveColor(this, R.attr.colorSurface))
setSupportActionBar(toolbar)
2019-12-30 11:01:50 +00:00
}
override fun onCreate(savedInstanceState: Bundle?) {
setDrawUnderStatusBar()
super.onCreate(savedInstanceState)
2020-01-05 20:25:55 +00:00
window.sharedElementsUseOverlay = true
2020-05-24 18:32:32 +00:00
imageContainer?.transitionName = getString(R.string.transition_album_art)
2020-01-05 20:25:55 +00:00
windowEnterTransition()
2019-12-30 11:01:50 +00:00
setUpViews()
setupToolbar()
}
private fun setUpViews() {
fillViewsWithFileTags()
MaterialUtil.setTint(yearContainer, false)
MaterialUtil.setTint(genreContainer, false)
MaterialUtil.setTint(albumTitleContainer, false)
MaterialUtil.setTint(albumArtistContainer, false)
albumText.appHandleColor().addTextChangedListener(this)
albumArtistText.appHandleColor().addTextChangedListener(this)
genreTitle.appHandleColor().addTextChangedListener(this)
yearTitle.appHandleColor().addTextChangedListener(this)
}
private fun fillViewsWithFileTags() {
albumText.setText(albumTitle)
albumArtistText.setText(albumArtistName)
genreTitle.setText(genreName)
yearTitle.setText(songYear)
}
override fun loadCurrentImage() {
val bitmap = albumArt
setImageBitmap(
bitmap,
getColor(
generatePalette(bitmap),
ATHUtil.resolveColor(this, R.attr.defaultFooterColor)
)
)
deleteAlbumArt = false
}
private fun toastLoadingFailed() {
Toast.makeText(
this@AlbumTagEditorActivity,
R.string.could_not_download_album_cover,
Toast.LENGTH_SHORT
).show()
}
override fun searchImageOnWeb() {
searchWebFor(albumText.text.toString(), albumArtistText.text.toString())
}
override fun deleteImage() {
setImageBitmap(
2020-02-25 13:15:23 +00:00
BitmapFactory.decodeResource(resources, R.drawable.default_audio_art),
2019-12-30 11:01:50 +00:00
ATHUtil.resolveColor(this, R.attr.defaultFooterColor)
)
deleteAlbumArt = true
dataChanged()
}
override fun save() {
val fieldKeyValueMap = EnumMap<FieldKey, String>(FieldKey::class.java)
fieldKeyValueMap[FieldKey.ALBUM] = albumText.text.toString()
2020-10-06 08:46:04 +00:00
// android seems not to recognize album_artist field so we additionally write the normal artist field
2019-12-30 11:01:50 +00:00
fieldKeyValueMap[FieldKey.ARTIST] = albumArtistText.text.toString()
fieldKeyValueMap[FieldKey.ALBUM_ARTIST] = albumArtistText.text.toString()
fieldKeyValueMap[FieldKey.GENRE] = genreTitle.text.toString()
fieldKeyValueMap[FieldKey.YEAR] = yearTitle.text.toString()
2020-01-29 17:25:43 +00:00
writeValuesToFiles(
fieldKeyValueMap,
if (deleteAlbumArt) ArtworkInfo(id, null)
2019-12-30 11:01:50 +00:00
else if (albumArtBitmap == null) null else ArtworkInfo(id, albumArtBitmap!!)
)
}
2020-08-31 12:30:07 +00:00
override fun getSongPaths(): List<String> {
return repository.albumById(id).songs
.map(Song::data)
2019-12-30 11:01:50 +00:00
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable) {
dataChanged()
}
override fun setColors(color: Int) {
super.setColors(color)
saveFab.backgroundTintList = ColorStateList.valueOf(color)
}
companion object {
val TAG: String = AlbumTagEditorActivity::class.java.simpleName
}
2018-12-06 08:52:57 +00:00
}