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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
/*
This file is part of Subsonic.
Subsonic 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.
Subsonic 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.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2009 (C) Sindre Mehus
*/
package github.daneren2005.dsub.domain;
import android.media.MediaMetadataRetriever;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
/**
* @author Sindre Mehus
*/
public class MusicDirectory implements Serializable {
private static final String TAG = MusicDirectory.class.getSimpleName();
private String name;
private String id;
private String parent;
private List<Entry> children;
public MusicDirectory() {
children = new ArrayList<Entry>();
}
public MusicDirectory(List<Entry> children) {
this.children = children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public void addChild(Entry child) {
children.add(child);
}
public void addChildren(List<Entry> children) {
this.children.addAll(children);
}
public void replaceChildren(List<Entry> children) {
this.children = children;
}
public List<Entry> getChildren() {
return getChildren(true, true);
}
public List<Entry> getChildren(boolean includeDirs, boolean includeFiles) {
if (includeDirs && includeFiles) {
return children;
}
List<Entry> result = new ArrayList<Entry>(children.size());
for (Entry child : children) {
if (child.isDirectory() && includeDirs || !child.isDirectory() && includeFiles) {
result.add(child);
}
}
return result;
}
public int getChildrenSize() {
return children.size();
}
public void sortChildren(boolean byYear) {
EntryComparator.sort(children, byYear);
}
public static class Entry implements Serializable {
public static final int TYPE_SONG = 0;
public static final int TYPE_PODCAST = 1;
public static final int TYPE_AUDIO_BOOK = 2;
private String id;
private String parent;
private String grandParent;
private String albumId;
private String artistId;
private boolean directory;
private String title;
private String album;
private String artist;
private Integer track;
private Integer year;
private String genre;
private String contentType;
private String suffix;
private String transcodedContentType;
private String transcodedSuffix;
private String coverArt;
private Long size;
private Integer duration;
private Integer bitRate;
private String path;
private boolean video;
private Integer discNumber;
private boolean starred;
private Integer rating;
private Bookmark bookmark;
private int type = 0;
private int closeness;
public void loadMetadata(File file) {
try {
MediaMetadataRetriever metadata = new MediaMetadataRetriever();
metadata.setDataSource(file.getAbsolutePath());
String discNumber = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER);
if(discNumber == null) {
discNumber = "1/1";
}
int slashIndex = discNumber.indexOf("/");
if(slashIndex > 0) {
discNumber = discNumber.substring(0, slashIndex);
}
try {
setDiscNumber(Integer.parseInt(discNumber));
} catch(Exception e) {
Log.w(TAG, "Non numbers in disc field!");
}
String bitrate = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);
setBitRate(Integer.parseInt((bitrate != null) ? bitrate : "0") / 1000);
String length = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
setDuration(Integer.parseInt(length) / 1000);
String artist = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
if(artist != null) {
setArtist(artist);
}
String album = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
if(album != null) {
setAlbum(album);
}
metadata.release();
} catch(Exception e) {
Log.i(TAG, "Device doesn't properly support MediaMetadataRetreiver", e);
}
}
public void rebaseTitleOffPath() {
try {
String filename = getPath();
int index = filename.lastIndexOf('/');
if (index != -1) {
filename = filename.substring(index + 1);
if (getTrack() != null) {
filename = filename.replace(String.format("%02d ", getTrack()), "");
}
index = filename.lastIndexOf('.');
if(index != -1) {
filename = filename.substring(0, index);
}
setTitle(filename);
}
} catch(Exception e) {
Log.w(TAG, "Failed to update title based off of path", e);
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getGrandParent() {
return grandParent;
}
public void setGrandParent(String grandParent) {
this.grandParent = grandParent;
}
public String getAlbumId() {
return albumId;
}
public void setAlbumId(String albumId) {
this.albumId = albumId;
}
public String getArtistId() {
return artistId;
}
public void setArtistId(String artistId) {
this.artistId = artistId;
}
public boolean isDirectory() {
return directory;
}
public void setDirectory(boolean directory) {
this.directory = directory;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAlbum() {
return album;
}
public boolean isAlbum() {
return getParent() != null || getArtist() != null;
}
public String getAlbumDisplay() {
if(album != null && title.startsWith("Disc ")) {
return album;
} else {
return title;
}
}
public void setAlbum(String album) {
this.album = album;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public Integer getTrack() {
return track;
}
public void setTrack(Integer track) {
this.track = track;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String getTranscodedContentType() {
return transcodedContentType;
}
public void setTranscodedContentType(String transcodedContentType) {
this.transcodedContentType = transcodedContentType;
}
public String getTranscodedSuffix() {
return transcodedSuffix;
}
public void setTranscodedSuffix(String transcodedSuffix) {
this.transcodedSuffix = transcodedSuffix;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public Integer getBitRate() {
return bitRate;
}
public void setBitRate(Integer bitRate) {
this.bitRate = bitRate;
}
public String getCoverArt() {
return coverArt;
}
public void setCoverArt(String coverArt) {
this.coverArt = coverArt;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isVideo() {
return video;
}
public void setVideo(boolean video) {
this.video = video;
}
public Integer getDiscNumber() {
return discNumber;
}
public void setDiscNumber(Integer discNumber) {
this.discNumber = discNumber;
}
public boolean isStarred() {
return starred;
}
public void setStarred(boolean starred) {
this.starred = starred;
}
public int getRating() {
return rating == null ? 0 : rating;
}
public void setRating(Integer rating) {
this.rating = rating;
}
public Bookmark getBookmark() {
return bookmark;
}
public void setBookmark(Bookmark bookmark) {
this.bookmark = bookmark;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public boolean isSong() {
return type == TYPE_SONG;
}
public boolean isPodcast() {
return this instanceof PodcastEpisode || type == TYPE_PODCAST;
}
public boolean isAudiBook() {
return type == TYPE_AUDIO_BOOK;
}
public int getCloseness() {
return closeness;
}
public void setCloseness(int closeness) {
this.closeness = closeness;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Entry entry = (Entry) o;
return id.equals(entry.id);
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public String toString() {
return title;
}
}
public static class EntryComparator implements Comparator<Entry> {
private boolean byYear;
public EntryComparator(boolean byYear) {
this.byYear = byYear;
}
public int compare(Entry lhs, Entry rhs) {
if(lhs.isDirectory() && !rhs.isDirectory()) {
return -1;
} else if(!lhs.isDirectory() && rhs.isDirectory()) {
return 1;
} else if(lhs.isDirectory() && rhs.isDirectory()) {
if(byYear) {
Integer lhsYear = lhs.getYear();
Integer rhsYear = rhs.getYear();
if(lhsYear != null && rhsYear != null) {
return lhsYear.compareTo(rhsYear);
} else if(lhsYear != null) {
return -1;
} else if(rhsYear != null) {
return 1;
}
}
return lhs.getTitle().compareToIgnoreCase(rhs.getTitle());
}
Integer lhsDisc = lhs.getDiscNumber();
Integer rhsDisc = rhs.getDiscNumber();
if(lhsDisc != null && rhsDisc != null) {
if(lhsDisc < rhsDisc) {
return -1;
} else if(lhsDisc > rhsDisc) {
return 1;
}
}
Integer lhsTrack = lhs.getTrack();
Integer rhsTrack = rhs.getTrack();
if(lhsTrack != null && rhsTrack != null) {
return lhsTrack.compareTo(rhsTrack);
} else if(lhsTrack != null) {
return -1;
} else if(rhsTrack != null) {
return 1;
}
return lhs.getTitle().compareToIgnoreCase(rhs.getTitle());
}
public static void sort(List<Entry> entries) {
sort(entries, true);
}
public static void sort(List<Entry> entries, boolean byYear) {
try {
Collections.sort(entries, new EntryComparator(byYear));
} catch (Exception e) {
Log.w(TAG, "Failed to sort MusicDirectory");
}
}
}
}
|