2020-05-22 10:51:08 +00:00
|
|
|
/*
|
2020-10-06 08:46:04 +00:00
|
|
|
* Copyright (c) 2020 Hemanth Savarla.
|
2020-05-22 10:51:08 +00:00
|
|
|
*
|
2020-10-06 08:46:04 +00:00
|
|
|
* Licensed under the GNU General Public License v3
|
2020-05-22 10:51:08 +00:00
|
|
|
*
|
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.
|
|
|
|
*
|
|
|
|
* 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-05-22 10:51:08 +00:00
|
|
|
*
|
|
|
|
*/
|
2019-04-20 05:29:45 +00:00
|
|
|
package code.name.monkey.retromusic.adapter
|
2018-11-30 01:06:16 +00:00
|
|
|
|
|
|
|
import android.graphics.PorterDuff
|
2019-12-06 14:25:21 +00:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2018-11-30 01:06:16 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import code.name.monkey.appthemehelper.util.ATHUtil
|
2019-10-09 13:46:25 +00:00
|
|
|
import code.name.monkey.retromusic.R
|
2019-12-06 14:25:21 +00:00
|
|
|
import code.name.monkey.retromusic.adapter.base.AbsMultiSelectAdapter
|
|
|
|
import code.name.monkey.retromusic.adapter.base.MediaEntryViewHolder
|
2021-09-08 18:30:20 +00:00
|
|
|
import code.name.monkey.retromusic.glide.GlideApp
|
|
|
|
import code.name.monkey.retromusic.glide.RetroGlideExtension
|
2018-11-30 01:06:16 +00:00
|
|
|
import code.name.monkey.retromusic.glide.audiocover.AudioFileCover
|
2020-09-23 20:55:12 +00:00
|
|
|
import code.name.monkey.retromusic.interfaces.ICabHolder
|
|
|
|
import code.name.monkey.retromusic.interfaces.ICallbacks
|
2020-01-17 17:19:06 +00:00
|
|
|
import code.name.monkey.retromusic.util.MusicUtil
|
2018-11-30 01:06:16 +00:00
|
|
|
import code.name.monkey.retromusic.util.RetroUtil
|
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
|
|
|
import com.bumptech.glide.signature.MediaStoreSignature
|
2021-09-08 18:30:20 +00:00
|
|
|
import me.zhanghai.android.fastscroll.PopupTextProvider
|
2018-11-30 01:06:16 +00:00
|
|
|
import java.io.File
|
|
|
|
import java.text.DecimalFormat
|
2019-12-06 14:25:21 +00:00
|
|
|
import kotlin.math.log10
|
|
|
|
import kotlin.math.pow
|
2018-12-12 20:59:07 +00:00
|
|
|
|
2019-09-03 16:02:10 +00:00
|
|
|
class SongFileAdapter(
|
2021-09-11 11:22:47 +00:00
|
|
|
override val activity: AppCompatActivity,
|
2020-01-17 17:19:06 +00:00
|
|
|
private var dataSet: List<File>,
|
|
|
|
private val itemLayoutRes: Int,
|
2020-12-05 13:32:49 +00:00
|
|
|
private val iCallbacks: ICallbacks?,
|
|
|
|
iCabHolder: ICabHolder?
|
2019-11-15 17:44:42 +00:00
|
|
|
) : AbsMultiSelectAdapter<SongFileAdapter.ViewHolder, File>(
|
2020-12-05 13:32:49 +00:00
|
|
|
activity, iCabHolder, R.menu.menu_media_selection
|
2020-01-17 17:19:06 +00:00
|
|
|
), PopupTextProvider {
|
2019-11-15 17:44:42 +00:00
|
|
|
|
2019-12-06 14:25:21 +00:00
|
|
|
init {
|
|
|
|
this.setHasStableIds(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int {
|
2020-01-17 17:19:06 +00:00
|
|
|
return if (dataSet[position].isDirectory) FOLDER else FILE
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemId(position: Int): Long {
|
2020-01-17 17:19:06 +00:00
|
|
|
return dataSet[position].hashCode().toLong()
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun swapDataSet(songFiles: List<File>) {
|
|
|
|
this.dataSet = songFiles
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
|
|
|
return ViewHolder(LayoutInflater.from(activity).inflate(itemLayoutRes, parent, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, index: Int) {
|
2020-01-17 17:19:06 +00:00
|
|
|
val file = dataSet[index]
|
2019-12-06 14:25:21 +00:00
|
|
|
holder.itemView.isActivated = isChecked(file)
|
|
|
|
holder.title?.text = getFileTitle(file)
|
|
|
|
if (holder.text != null) {
|
|
|
|
if (holder.itemViewType == FILE) {
|
|
|
|
holder.text?.text = getFileText(file)
|
|
|
|
} else {
|
|
|
|
holder.text?.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (holder.image != null) {
|
|
|
|
loadFileImage(file, holder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getFileTitle(file: File): String {
|
|
|
|
return file.name
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getFileText(file: File): String? {
|
|
|
|
return if (file.isDirectory) null else readableFileSize(file.length())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadFileImage(file: File, holder: ViewHolder) {
|
|
|
|
val iconColor = ATHUtil.resolveColor(activity, R.attr.colorControlNormal)
|
|
|
|
if (file.isDirectory) {
|
|
|
|
holder.image?.let {
|
|
|
|
it.setColorFilter(iconColor, PorterDuff.Mode.SRC_IN)
|
2020-07-19 21:00:30 +00:00
|
|
|
it.setImageResource(R.drawable.ic_folder)
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
2020-02-25 13:15:23 +00:00
|
|
|
holder.imageTextContainer?.setCardBackgroundColor(
|
|
|
|
ATHUtil.resolveColor(
|
|
|
|
activity,
|
|
|
|
R.attr.colorSurface
|
|
|
|
)
|
|
|
|
)
|
2019-12-06 14:25:21 +00:00
|
|
|
} else {
|
|
|
|
val error = RetroUtil.getTintedVectorDrawable(
|
2020-07-19 21:00:30 +00:00
|
|
|
activity, R.drawable.ic_file_music, iconColor
|
2019-12-06 14:25:21 +00:00
|
|
|
)
|
2021-09-08 18:30:20 +00:00
|
|
|
GlideApp.with(activity)
|
2020-04-15 08:49:28 +00:00
|
|
|
.load(AudioFileCover(file.path))
|
|
|
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
|
|
|
.error(error)
|
|
|
|
.placeholder(error)
|
2021-09-08 18:30:20 +00:00
|
|
|
.transition(RetroGlideExtension.getDefaultTransition())
|
2020-04-15 08:49:28 +00:00
|
|
|
.signature(MediaStoreSignature("", file.lastModified(), 0))
|
2021-09-08 18:30:20 +00:00
|
|
|
.into(holder.image!!)
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemCount(): Int {
|
2020-01-17 17:19:06 +00:00
|
|
|
return dataSet.size
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
override fun getIdentifier(position: Int): File {
|
2020-01-17 17:19:06 +00:00
|
|
|
return dataSet[position]
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun getName(`object`: File): String {
|
|
|
|
return getFileTitle(`object`)
|
|
|
|
}
|
|
|
|
|
2020-08-13 17:08:37 +00:00
|
|
|
override fun onMultipleItemAction(menuItem: MenuItem, selection: List<File>) {
|
2020-12-05 13:32:49 +00:00
|
|
|
if (iCallbacks == null) return
|
|
|
|
iCallbacks.onMultipleItemAction(menuItem, selection as ArrayList<File>)
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 17:19:06 +00:00
|
|
|
override fun getPopupText(position: Int): String {
|
|
|
|
return getSectionName(position)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getSectionName(position: Int): String {
|
|
|
|
return MusicUtil.getSectionName(dataSet[position].name)
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inner class ViewHolder(itemView: View) : MediaEntryViewHolder(itemView) {
|
|
|
|
|
|
|
|
init {
|
2020-12-05 13:32:49 +00:00
|
|
|
if (menu != null && iCallbacks != null) {
|
2019-12-06 14:25:21 +00:00
|
|
|
menu?.setOnClickListener { v ->
|
2020-04-16 18:13:12 +00:00
|
|
|
val position = layoutPosition
|
2019-12-06 14:25:21 +00:00
|
|
|
if (isPositionInRange(position)) {
|
2020-12-05 13:32:49 +00:00
|
|
|
iCallbacks.onFileMenuClicked(dataSet[position], v)
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (imageTextContainer != null) {
|
|
|
|
imageTextContainer?.cardElevation = 0f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onClick(v: View?) {
|
2020-04-16 18:13:12 +00:00
|
|
|
val position = layoutPosition
|
2019-12-06 14:25:21 +00:00
|
|
|
if (isPositionInRange(position)) {
|
|
|
|
if (isInQuickSelectMode) {
|
|
|
|
toggleChecked(position)
|
|
|
|
} else {
|
2020-12-05 13:32:49 +00:00
|
|
|
iCallbacks?.onFileSelected(dataSet[position])
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLongClick(v: View?): Boolean {
|
2020-04-16 18:13:12 +00:00
|
|
|
val position = layoutPosition
|
2019-12-06 14:25:21 +00:00
|
|
|
return isPositionInRange(position) && toggleChecked(position)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun isPositionInRange(position: Int): Boolean {
|
2020-01-17 17:19:06 +00:00
|
|
|
return position >= 0 && position < dataSet.size
|
2019-12-06 14:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
private const val FILE = 0
|
|
|
|
private const val FOLDER = 1
|
|
|
|
|
|
|
|
fun readableFileSize(size: Long): String {
|
|
|
|
if (size <= 0) return "$size B"
|
|
|
|
val units = arrayOf("B", "KB", "MB", "GB", "TB")
|
|
|
|
val digitGroups = (log10(size.toDouble()) / log10(1024.0)).toInt()
|
|
|
|
return DecimalFormat("#,##0.##").format(size / 1024.0.pow(digitGroups.toDouble())) + " " + units[digitGroups]
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
}
|