2019-03-03 09:29:03 +00:00
/ *
* 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 .
* /
2018-12-12 20:59:07 +00:00
package code.name.monkey.retromusic.util
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.AsyncTask
2020-02-01 14:22:57 +00:00
import android.provider.MediaStore
2018-12-12 20:59:07 +00:00
import android.widget.Toast
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.model.Artist
2019-09-16 18:02:40 +00:00
import com.bumptech.glide.Glide
2018-12-12 20:59:07 +00:00
import com.bumptech.glide.load.engine.DiskCacheStrategy
2019-09-16 18:02:40 +00:00
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
2018-12-12 20:59:07 +00:00
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
2020-02-01 14:22:57 +00:00
import java.util.Locale
2018-12-12 20:59:07 +00:00
2019-09-16 18:02:40 +00:00
2018-12-12 20:59:07 +00:00
class CustomArtistImageUtil private constructor ( context : Context ) {
private val mPreferences : SharedPreferences
init {
mPreferences = context . applicationContext . getSharedPreferences ( CUSTOM _ARTIST _IMAGE _PREFS , Context . MODE _PRIVATE )
}
fun setCustomArtistImage ( artist : Artist , uri : Uri ) {
2019-09-16 18:02:40 +00:00
Glide . with ( App . getContext ( ) )
2018-12-12 20:59:07 +00:00
. load ( uri )
2019-09-16 18:02:40 +00:00
. asBitmap ( )
. diskCacheStrategy ( DiskCacheStrategy . NONE )
. skipMemoryCache ( true )
. into ( object : SimpleTarget < Bitmap > ( ) {
override fun onLoadFailed ( e : Exception ? , errorDrawable : Drawable ? ) {
super . onLoadFailed ( e , errorDrawable )
e !! . printStackTrace ( )
Toast . makeText ( App . getContext ( ) , e . toString ( ) , Toast . LENGTH _LONG ) . show ( )
}
2018-12-12 20:59:07 +00:00
2019-09-16 18:02:40 +00:00
override fun onResourceReady ( resource : Bitmap , glideAnimation : GlideAnimation < in Bitmap > ) {
2018-12-12 20:59:07 +00:00
object : AsyncTask < Void , Void , Void > ( ) {
@SuppressLint ( " ApplySharedPref " )
override fun doInBackground ( vararg params : Void ) : Void ? {
2019-09-09 13:04:53 +00:00
val dir = File ( App . getContext ( ) . filesDir , FOLDER _NAME )
2018-12-12 20:59:07 +00:00
if ( ! dir . exists ( ) ) {
if ( ! dir . mkdirs ( ) ) { // create the folder
return null
}
}
val file = File ( dir , getFileName ( artist ) )
var succesful = false
try {
val os = BufferedOutputStream ( FileOutputStream ( file ) )
succesful = ImageUtil . resizeBitmap ( resource , 2048 ) . compress ( Bitmap . CompressFormat . JPEG , 100 , os )
os . close ( )
} catch ( e : IOException ) {
2019-09-09 13:04:53 +00:00
Toast . makeText ( App . getContext ( ) , e . toString ( ) , Toast . LENGTH _LONG ) . show ( )
2018-12-12 20:59:07 +00:00
}
if ( succesful ) {
mPreferences . edit ( ) . putBoolean ( getFileName ( artist ) , true ) . commit ( )
2019-09-16 18:02:40 +00:00
ArtistSignatureUtil . getInstance ( App . getContext ( ) ) . updateArtistSignature ( artist . name )
2020-02-01 14:22:57 +00:00
App . getContext ( ) . contentResolver . notifyChange ( MediaStore . Audio . Artists . EXTERNAL _CONTENT _URI , null ) // trigger media store changed to force artist image reload
2018-12-12 20:59:07 +00:00
}
return null
}
} . execute ( )
}
} )
}
fun resetCustomArtistImage ( artist : Artist ) {
object : AsyncTask < Void , Void , Void > ( ) {
@SuppressLint ( " ApplySharedPref " )
override fun doInBackground ( vararg params : Void ) : Void ? {
mPreferences . edit ( ) . putBoolean ( getFileName ( artist ) , false ) . commit ( )
2019-09-16 18:02:40 +00:00
ArtistSignatureUtil . getInstance ( App . getContext ( ) ) . updateArtistSignature ( artist . name )
2020-02-01 14:22:57 +00:00
App . getContext ( ) . contentResolver . notifyChange ( MediaStore . Audio . Artists . EXTERNAL _CONTENT _URI , null ) // trigger media store changed to force artist image reload
2018-12-12 20:59:07 +00:00
val file = getFile ( artist )
if ( ! file . exists ( ) ) {
return null
} else {
file . delete ( )
}
return null
}
} . execute ( )
}
// shared prefs saves us many IO operations
fun hasCustomArtistImage ( artist : Artist ) : Boolean {
return mPreferences . getBoolean ( getFileName ( artist ) , false )
}
companion object {
private const val CUSTOM _ARTIST _IMAGE _PREFS = " custom_artist_image "
private const val FOLDER _NAME = " /custom_artist_images/ "
private var sInstance : CustomArtistImageUtil ? = null
fun getInstance ( context : Context ) : CustomArtistImageUtil {
if ( sInstance == null ) {
sInstance = CustomArtistImageUtil ( context . applicationContext )
}
return sInstance !!
}
fun getFileName ( artist : Artist ) : String {
var artistName = artist . name
// replace everything that is not a letter or a number with _
artistName = artistName . replace ( " [^a-zA-Z0-9] " . toRegex ( ) , " _ " )
return String . format ( Locale . US , " #%d#%s.jpeg " , artist . id , artistName )
}
@JvmStatic
fun getFile ( artist : Artist ) : File {
2019-09-09 13:04:53 +00:00
val dir = File ( App . getContext ( ) . filesDir , FOLDER _NAME )
2018-12-12 20:59:07 +00:00
return File ( dir , getFileName ( artist ) )
}
}
}