Converted some util, glide classes to Kotlin
This commit is contained in:
parent
5ee5611fbe
commit
c09e39b23f
7 changed files with 253 additions and 1311 deletions
|
@ -11,29 +11,18 @@
|
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
package code.name.monkey.retromusic.glide.audiocover
|
||||
|
||||
package code.name.monkey.retromusic.glide.audiocover;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
/** @author Karim Abou Zeid (kabouzeid) */
|
||||
public class AudioFileCover {
|
||||
public final String filePath;
|
||||
|
||||
public AudioFileCover(String filePath) {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return filePath.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object object) {
|
||||
if (object instanceof AudioFileCover){
|
||||
return ((AudioFileCover) object).filePath.equals(filePath);
|
||||
/** @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
class AudioFileCover(val filePath: String) {
|
||||
override fun hashCode(): Int {
|
||||
return filePath.hashCode()
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return if (other is AudioFileCover) {
|
||||
other.filePath == filePath
|
||||
} else false
|
||||
}
|
||||
}
|
|
@ -11,76 +11,57 @@
|
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
package code.name.monkey.retromusic.glide.audiocover
|
||||
|
||||
package code.name.monkey.retromusic.glide.audiocover;
|
||||
import android.media.MediaMetadataRetriever
|
||||
import com.bumptech.glide.Priority
|
||||
import com.bumptech.glide.load.DataSource
|
||||
import com.bumptech.glide.load.data.DataFetcher
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
|
||||
import android.media.MediaMetadataRetriever;
|
||||
|
||||
import com.bumptech.glide.Priority;
|
||||
import com.bumptech.glide.load.DataSource;
|
||||
import com.bumptech.glide.load.data.DataFetcher;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class AudioFileCoverFetcher implements DataFetcher<InputStream> {
|
||||
private final AudioFileCover model;
|
||||
|
||||
private InputStream stream;
|
||||
|
||||
public AudioFileCoverFetcher(AudioFileCover model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadData(@NotNull Priority priority, @NotNull DataCallback<? super InputStream> callback) {
|
||||
final MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
||||
try {
|
||||
retriever.setDataSource(model.filePath);
|
||||
byte[] picture = retriever.getEmbeddedPicture();
|
||||
if (picture != null) {
|
||||
stream = new ByteArrayInputStream(picture);
|
||||
} else {
|
||||
stream = AudioFileCoverUtils.fallback(model.filePath);
|
||||
}
|
||||
callback.onDataReady(stream);
|
||||
} catch (FileNotFoundException e) {
|
||||
callback.onLoadFailed(e);
|
||||
} finally {
|
||||
retriever.release();
|
||||
class AudioFileCoverFetcher(private val model: AudioFileCover) : DataFetcher<InputStream> {
|
||||
private var stream: InputStream? = null
|
||||
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
|
||||
val retriever = MediaMetadataRetriever()
|
||||
try {
|
||||
retriever.setDataSource(model.filePath)
|
||||
val picture = retriever.embeddedPicture
|
||||
stream = if (picture != null) {
|
||||
ByteArrayInputStream(picture)
|
||||
} else {
|
||||
AudioFileCoverUtils.fallback(model.filePath)
|
||||
}
|
||||
callback.onDataReady(stream)
|
||||
} catch (e: FileNotFoundException) {
|
||||
callback.onLoadFailed(e)
|
||||
} finally {
|
||||
retriever.release()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanup() {
|
||||
// already cleaned up in loadData and ByteArrayInputStream will be GC'd
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException ignore) {
|
||||
// can't do much about it
|
||||
}
|
||||
override fun cleanup() {
|
||||
// already cleaned up in loadData and ByteArrayInputStream will be GC'd
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream?.close()
|
||||
} catch (ignore: IOException) {
|
||||
// can't do much about it
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
// cannot cancel
|
||||
}
|
||||
override fun cancel() {
|
||||
// cannot cancel
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Class<InputStream> getDataClass() {
|
||||
return InputStream.class;
|
||||
}
|
||||
override fun getDataClass(): Class<InputStream> {
|
||||
return InputStream::class.java
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public DataSource getDataSource() {
|
||||
return DataSource.LOCAL;
|
||||
}
|
||||
}
|
||||
override fun getDataSource(): DataSource {
|
||||
return DataSource.LOCAL
|
||||
}
|
||||
}
|
|
@ -11,43 +11,35 @@
|
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
package code.name.monkey.retromusic.glide.audiocover
|
||||
|
||||
package code.name.monkey.retromusic.glide.audiocover;
|
||||
import com.bumptech.glide.load.Options
|
||||
import com.bumptech.glide.load.model.ModelLoader
|
||||
import com.bumptech.glide.load.model.ModelLoader.LoadData
|
||||
import com.bumptech.glide.load.model.ModelLoaderFactory
|
||||
import com.bumptech.glide.load.model.MultiModelLoaderFactory
|
||||
import com.bumptech.glide.signature.ObjectKey
|
||||
import java.io.InputStream
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.load.Options;
|
||||
import com.bumptech.glide.load.model.ModelLoader;
|
||||
import com.bumptech.glide.load.model.ModelLoaderFactory;
|
||||
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
|
||||
import com.bumptech.glide.signature.ObjectKey;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class AudioFileCoverLoader implements ModelLoader<AudioFileCover, InputStream> {
|
||||
|
||||
@Override
|
||||
public LoadData<InputStream> buildLoadData(@NonNull @NotNull AudioFileCover audioFileCover, int width, int height, @NonNull @NotNull Options options) {
|
||||
return new LoadData<>(new ObjectKey(audioFileCover.filePath), new AudioFileCoverFetcher(audioFileCover));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handles(@NonNull @NotNull AudioFileCover audioFileCover) {
|
||||
return audioFileCover.filePath != null;
|
||||
}
|
||||
|
||||
public static class Factory implements ModelLoaderFactory<AudioFileCover, InputStream> {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ModelLoader<AudioFileCover, InputStream> build(@NonNull @NotNull MultiModelLoaderFactory multiFactory) {
|
||||
return new AudioFileCoverLoader();
|
||||
class AudioFileCoverLoader : ModelLoader<AudioFileCover, InputStream> {
|
||||
override fun buildLoadData(
|
||||
audioFileCover: AudioFileCover,
|
||||
width: Int,
|
||||
height: Int,
|
||||
options: Options
|
||||
): LoadData<InputStream> {
|
||||
return LoadData(ObjectKey(audioFileCover.filePath), AudioFileCoverFetcher(audioFileCover))
|
||||
}
|
||||
|
||||
@Override
|
||||
public void teardown() {
|
||||
override fun handles(audioFileCover: AudioFileCover): Boolean {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Factory : ModelLoaderFactory<AudioFileCover, InputStream> {
|
||||
override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<AudioFileCover, InputStream> {
|
||||
return AudioFileCoverLoader()
|
||||
}
|
||||
|
||||
override fun teardown() {}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue