This repository was archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathAlbum.java
More file actions
261 lines (217 loc) · 7.05 KB
/
Album.java
File metadata and controls
261 lines (217 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.simplecity.amp_library.model;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import com.simplecity.amp_library.utils.ArtworkUtils;
import com.simplecity.amp_library.utils.ComparisonUtils;
import com.simplecity.amp_library.utils.DataManager;
import com.simplecity.amp_library.utils.StringUtils;
import io.reactivex.Single;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class Album implements
Serializable,
ArtworkProvider,
Comparable<Album>,
Sortable {
public long id;
public String name;
public List<Artist> artists = new ArrayList<>();
public String albumArtistName;
public int year;
public int numSongs;
public int numDiscs;
public long lastPlayed;
public long dateAdded;
public List<String> paths = new ArrayList<>();
public int songPlayCount;
private String artworkKey;
private String sortKey;
public Album(long id, String name, List<Artist> artists, String albumArtistName, int numSongs, int numDiscs, int year, long lastPlayed, long dateAdded, List<String> paths, int songPlayCount) {
this.id = id;
this.name = name;
this.artists = artists;
this.albumArtistName = albumArtistName;
this.numSongs = numSongs;
this.numDiscs = numDiscs;
this.year = year;
this.lastPlayed = lastPlayed;
this.dateAdded = dateAdded;
this.paths = paths;
this.songPlayCount = songPlayCount;
//Populate the artwork key & sort key properties if null.
setSortKey();
setArtworkKey();
}
public Single<List<Song>> getSongsSingle() {
return DataManager.getInstance().getSongsObservable(song -> song.albumId == id).firstOrError();
}
public static class Builder {
private long id;
private String name;
private List<Artist> artists = new ArrayList<>();
private String albumArtistName;
private int numSongs;
private int numDiscs;
private int year;
private long lastPlayed;
private long dateAdded;
private List<String> paths = new ArrayList<>();
private int songPlayCount;
public Builder id(long id) {
this.id = id;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder addArtist(Artist artist) {
if (!this.artists.contains(artist)) {
this.artists.add(artist);
}
return this;
}
public Builder albumArtist(String albumArtistName) {
this.albumArtistName = albumArtistName;
return this;
}
public Builder numSongs(int numSongs) {
this.numSongs = numSongs;
return this;
}
public Builder numDiscs(int numDiscs) {
this.numDiscs = numDiscs;
return this;
}
public Builder year(int year) {
this.year = year;
return this;
}
public Builder lastPlayed(long lastPlayed) {
if (lastPlayed > this.lastPlayed) {
this.lastPlayed = lastPlayed;
}
return this;
}
public Builder dateAdded(long dateAdded) {
if (dateAdded > this.dateAdded) {
this.dateAdded = dateAdded;
}
return this;
}
public Builder path(String path) {
if (!this.paths.contains(path)) {
this.paths.add(path);
}
return this;
}
public Builder songPlayCount(int playCount) {
songPlayCount = playCount;
return this;
}
public Album build() {
return new Album(id, name, artists, albumArtistName, numSongs, numDiscs, year, lastPlayed, dateAdded, paths, songPlayCount);
}
}
public AlbumArtist getAlbumArtist() {
return new AlbumArtist.Builder()
.name(albumArtistName)
.album(this)
.build();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Album album = (Album) o;
if (id != album.id) return false;
return name != null ? name.equals(album.name) : album.name == null;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Album{" +
"id=" + id +
", name='" + name + '\'' +
", artists=" + artists +
", albumArtistName='" + albumArtistName + '\'' +
", year=" + year +
", numSongs=" + numSongs +
", lastPlayed=" + lastPlayed +
", dateAdded=" + dateAdded +
", paths=" + paths +
'}';
}
@Override
public String getSortKey() {
if (sortKey == null) {
setSortKey();
}
return sortKey;
}
@Override
public void setSortKey() {
sortKey = StringUtils.keyFor(name);
}
@Override
@NonNull
public String getArtworkKey() {
if (artworkKey == null) setArtworkKey();
return artworkKey;
}
private void setArtworkKey() {
artworkKey = String.format("%s_%s", albumArtistName, name);
}
@Nullable
@Override
public String getRemoteArtworkUrl() {
try {
return "https://artwork.shuttlemusicplayer.app/api/v1/artwork"
+ "?artist=" + URLEncoder.encode(albumArtistName, StandardCharsets.UTF_8.name())
+ "&album=" + URLEncoder.encode(name, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
return null;
}
}
@Override
public InputStream getMediaStoreArtwork() {
return ArtworkUtils.getMediaStoreArtwork(this);
}
@Nullable
@Override
public InputStream getFolderArtwork() {
return ArtworkUtils.getFolderArtwork(getArtworkPath());
}
@Override
public InputStream getTagArtwork() {
return ArtworkUtils.getTagArtwork(getArtworkPath());
}
@Override
public List<File> getFolderArtworkFiles() {
return ArtworkUtils.getAllFolderArtwork(getArtworkPath());
}
@Nullable
@WorkerThread
private String getArtworkPath() {
if (paths != null && !paths.isEmpty()) {
return paths.get(0);
}
return null;
}
@Override
public int compareTo(@NonNull Album album) {
return ComparisonUtils.compare(getSortKey(), album.getSortKey());
}
}