tempus/app/src/main/java/com/cappielloantonio/play/model/Song.java

376 lines
9.5 KiB
Java
Raw Normal View History

2020-11-20 15:38:08 +01:00
package com.cappielloantonio.play.model;
import android.annotation.SuppressLint;
2020-11-20 15:38:08 +01:00
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import com.cappielloantonio.play.subsonic.models.Child;
2020-11-22 19:11:38 +01:00
import java.time.Instant;
2020-11-20 15:38:08 +01:00
import java.util.UUID;
public class Song implements Parcelable {
private static final String TAG = "SongClass";
2020-11-24 10:52:00 +01:00
public static final String RECENTLY_PLAYED = "RECENTLY_PLAYED";
public static final String MOST_PLAYED = "MOST_PLAYED";
public static final String RECENTLY_ADDED = "RECENTLY_ADDED";
public static final String BY_GENRE = "BY_GENRE";
2020-11-25 08:21:30 +01:00
public static final String BY_GENRES = "BY_GENRES";
2020-11-24 10:52:00 +01:00
public static final String BY_ARTIST = "BY_ARTIST";
2020-11-28 14:50:15 +01:00
public static final String BY_YEAR = "BY_YEAR";
2020-11-30 20:54:05 +01:00
public static final String IS_FAVORITE = "IS_FAVORITE";
2021-04-26 19:37:05 +02:00
public static final String DOWNLOADED = "DOWNLOADED";
2021-04-18 20:23:09 +02:00
public static final String RADIO = "RADIO";
2020-11-20 15:38:08 +01:00
private String id;
private String title;
private int trackNumber;
private int discNumber;
private int year;
private long duration;
private String albumId;
private String albumName;
private String artistId;
private String artistName;
private String primary;
private String blurHash;
2020-11-20 15:38:08 +01:00
private boolean favorite;
private String path;
private long size;
private String container;
private int bitRate;
2020-11-22 19:11:38 +01:00
private long added;
private int playCount;
private long lastPlay;
2021-04-26 19:17:42 +02:00
private boolean offline;
public Song() {
this.id = UUID.randomUUID().toString();
}
public Song(Child child) {
this.id = child.getId();
this.title = child.getTitle();
this.trackNumber = child.getTrack();
this.discNumber = child.getDiscNumber();
this.year = child.getYear() != null ? child.getYear() : 0;
this.duration = child.getDuration();
this.albumId = child.getAlbumId();
this.albumName = child.getAlbum();
this.artistId = child.getArtistId();
this.artistName = child.getArtist();
this.primary = child.getCoverArtId();
this.favorite = child.getStarred() != null;
this.path = child.getPath();
this.size = child.getSize();
this.container = child.getContentType();
this.bitRate = child.getBitRate();
this.added = child.getCreated().getTime();
2020-11-22 19:11:38 +01:00
this.playCount = 0;
this.lastPlay = 0;
2021-04-26 19:17:42 +02:00
this.offline = false;
2020-11-20 15:38:08 +01:00
}
2021-07-28 15:28:32 +02:00
public Song(Queue queue) {
this.id = queue.getSongID();
this.title = queue.getTitle();
this.albumId = queue.getAlbumId();
this.albumName = queue.getAlbumName();
this.artistId = queue.getArtistId();
this.artistName = queue.getArtistName();
this.primary = queue.getPrimary();
2021-07-28 18:53:42 +02:00
this.duration = queue.getDuration();
2021-07-28 15:28:32 +02:00
}
2021-07-29 17:12:55 +02:00
public Song(Download download) {
this.id = download.getSongID();
this.title = download.getTitle();
this.albumId = download.getAlbumId();
this.albumName = download.getAlbumName();
this.artistId = download.getArtistId();
this.artistName = download.getArtistName();
this.primary = download.getPrimary();
this.duration = download.getDuration();
}
2020-11-20 15:38:08 +01:00
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public int getTrackNumber() {
return trackNumber;
}
public int getDiscNumber() {
return discNumber;
}
public int getYear() {
return year;
}
public long getDuration() {
return duration;
}
public String getAlbumId() {
return albumId;
}
public String getAlbumName() {
return albumName;
}
public String getArtistId() {
return artistId;
}
public String getArtistName() {
return artistName;
}
public String getPrimary() {
return primary;
}
public String getBlurHash() {
return blurHash;
}
2020-11-20 15:38:08 +01:00
public boolean isFavorite() {
return favorite;
}
public String getPath() {
return path;
}
public long getSize() {
return size;
}
public String getContainer() {
return container;
}
public int getBitRate() {
return bitRate;
}
2020-11-22 19:11:38 +01:00
public long getAdded() {
return added;
}
public int getPlayCount() {
return playCount;
}
public long getLastPlay() {
return lastPlay;
}
2021-04-26 19:17:42 +02:00
public boolean isOffline() {
return offline;
}
2021-07-28 15:28:32 +02:00
public void setId(String id) {
2020-11-20 15:38:08 +01:00
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setTrackNumber(int trackNumber) {
this.trackNumber = trackNumber;
}
public void setDiscNumber(int discNumber) {
this.discNumber = discNumber;
}
public void setYear(int year) {
this.year = year;
}
public void setDuration(long duration) {
this.duration = duration;
}
public void setAlbumId(String albumId) {
this.albumId = albumId;
}
public void setAlbumName(String albumName) {
this.albumName = albumName;
}
public void setArtistId(String artistId) {
this.artistId = artistId;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public void setPrimary(String primary) {
this.primary = primary;
}
public void setBlurHash(String blurHash) {
this.blurHash = blurHash;
}
2020-11-20 15:38:08 +01:00
public void setFavorite(boolean favorite) {
this.favorite = favorite;
}
public void setPath(String path) {
this.path = path;
}
public void setSize(long size) {
this.size = size;
}
public void setContainer(String container) {
this.container = container;
}
public void setBitRate(int bitRate) {
this.bitRate = bitRate;
}
2020-11-22 19:11:38 +01:00
public void setAdded(long added) {
this.added = added;
}
public void setLastPlay(long lastPlay) {
this.lastPlay = lastPlay;
}
2020-11-27 09:06:50 +01:00
public void setPlayCount(int playCount) {
this.playCount = playCount;
}
2021-07-27 14:53:03 +02:00
public void setOffline(boolean offline) {
this.offline = offline;
}
2021-04-26 19:17:42 +02:00
/*
Log.i(TAG, "increasePlayCount: " + isIncreased);
* Incremento il numero di ascolti solo se ho ascoltato la canzone da più tempo di:
* tempo dell'ultimo ascolto - (durata_canzone / 2)
* Ritorno un booleano
* Se vero, allora SongRepository scriverà nd DB l'incremento dell'ascolto
* Se falso, SongRepository non scriverà nulla nel db
*/
public boolean nowPlaying() {
long startPlayTime = Instant.now().toEpochMilli();
2021-07-27 14:53:03 +02:00
if (startPlayTime - (getDuration() / 2) > getLastPlay()) {
this.playCount++;
this.lastPlay = startPlayTime;
return true;
}
return false;
2020-11-22 19:11:38 +01:00
}
2020-11-20 15:38:08 +01:00
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Song song = (Song) o;
return id.equals(song.id);
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public String toString() {
return id;
}
@Override
public int describeContents() {
return 0;
}
@SuppressLint("NewApi")
2020-11-20 15:38:08 +01:00
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.title);
dest.writeInt(this.trackNumber);
dest.writeInt(this.discNumber);
dest.writeInt(this.year);
dest.writeLong(this.duration);
dest.writeString(this.albumId);
dest.writeString(this.albumName);
dest.writeString(this.artistId);
dest.writeString(this.artistName);
dest.writeString(this.primary);
dest.writeString(Boolean.toString(favorite));
dest.writeString(this.blurHash);
2020-11-20 15:38:08 +01:00
dest.writeString(this.path);
dest.writeLong(this.size);
dest.writeString(this.container);
dest.writeInt(this.bitRate);
2020-11-22 19:11:38 +01:00
dest.writeLong(this.added);
dest.writeInt(this.playCount);
dest.writeLong(this.lastPlay);
2021-04-26 19:17:42 +02:00
dest.writeBoolean(this.offline);
2020-11-20 15:38:08 +01:00
}
@SuppressLint("NewApi")
2020-11-20 15:38:08 +01:00
protected Song(Parcel in) {
this.id = in.readString();
this.title = in.readString();
this.trackNumber = in.readInt();
this.discNumber = in.readInt();
this.year = in.readInt();
this.duration = in.readLong();
this.albumId = in.readString();
this.albumName = in.readString();
this.artistId = in.readString();
this.artistName = in.readString();
this.primary = in.readString();
this.favorite = Boolean.parseBoolean(in.readString());
this.blurHash = in.readString();
2020-11-20 15:38:08 +01:00
this.path = in.readString();
this.size = in.readLong();
this.container = in.readString();
this.bitRate = in.readInt();
2020-11-22 19:11:38 +01:00
this.added = in.readLong();
this.playCount = in.readInt();
this.lastPlay = in.readLong();
2021-04-26 19:17:42 +02:00
this.offline = in.readBoolean();
2020-11-20 15:38:08 +01:00
}
public static final Creator<Song> CREATOR = new Creator<Song>() {
public Song createFromParcel(Parcel source) {
return new Song(source);
}
public Song[] newArray(int size) {
return new Song[size];
}
};
}