diff options
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java | 87 |
1 files changed, 35 insertions, 52 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java b/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java index a2b6d4ed..16d12100 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java @@ -18,13 +18,12 @@ */ package github.daneren2005.dsub.domain; -import android.annotation.TargetApi; import android.content.Context; -import android.content.SharedPreferences; import android.media.MediaMetadataRetriever; -import android.os.Build; import android.util.Log; +import androidx.annotation.NonNull; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -41,6 +40,7 @@ import java.io.Serializable; import java.util.Collections; import java.util.Comparator; import java.util.Locale; +import java.util.Objects; import github.daneren2005.dsub.service.DownloadService; import github.daneren2005.dsub.util.Constants; @@ -59,7 +59,7 @@ public class MusicDirectory implements Serializable { private List<Entry> children; public MusicDirectory() { - children = new ArrayList<Entry>(); + children = new ArrayList<>(); } public MusicDirectory(List<Entry> children) { this.children = children; @@ -111,16 +111,16 @@ public class MusicDirectory implements Serializable { return children; } - List<Entry> result = new ArrayList<Entry>(children.size()); + List<Entry> result = new ArrayList<>(children.size()); for (Entry child : children) { - if (child != null && child.isDirectory() && includeDirs || !child.isDirectory() && includeFiles) { + if (child != null && child.isDirectory() && includeDirs || !Objects.requireNonNull(child).isDirectory() && includeFiles) { result.add(child); } } return result; } public synchronized List<Entry> getSongs() { - List<Entry> result = new ArrayList<Entry>(); + List<Entry> result = new ArrayList<>(); for (Entry child : children) { if (child != null && !child.isDirectory() && !child.isVideo()) { result.add(child); @@ -149,11 +149,9 @@ public class MusicDirectory implements Serializable { public synchronized boolean updateMetadata(MusicDirectory refreshedDirectory) { boolean metadataUpdated = false; - Iterator<Entry> it = children.iterator(); - while(it.hasNext()) { - Entry entry = it.next(); + for (Entry entry : children) { int index = refreshedDirectory.children.indexOf(entry); - if(index != -1) { + if (index != -1) { final Entry refreshed = refreshedDirectory.children.get(index); entry.setTitle(refreshed.getTitle()); @@ -168,7 +166,7 @@ public class MusicDirectory implements Serializable { entry.setStarred(refreshed.isStarred()); entry.setRating(refreshed.getRating()); entry.setType(refreshed.getType()); - if(!Util.equals(entry.getCoverArt(), refreshed.getCoverArt())) { + if (!Util.equals(entry.getCoverArt(), refreshed.getCoverArt())) { metadataUpdated = true; entry.setCoverArt(refreshed.getCoverArt()); } @@ -188,7 +186,7 @@ public class MusicDirectory implements Serializable { found.setStarred(refreshed.isStarred()); found.setRating(refreshed.getRating()); found.setType(refreshed.getType()); - if(!Util.equals(found.getCoverArt(), refreshed.getCoverArt())) { + if (!Util.equals(found.getCoverArt(), refreshed.getCoverArt())) { found.setCoverArt(refreshed.getCoverArt()); metadataUpdate = DownloadService.METADATA_UPDATED_COVER_ART; } @@ -205,7 +203,7 @@ public class MusicDirectory implements Serializable { while(it.hasNext()) { Entry entry = it.next(); // No longer exists in here - if(refreshedDirectory.children.indexOf(entry) == -1) { + if(!refreshedDirectory.children.contains(entry)) { it.remove(); changed = true; } @@ -279,7 +277,6 @@ public class MusicDirectory implements Serializable { this.linkedArtist = artist; } - @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1) public void loadMetadata(File file) { try { MediaMetadataRetriever metadata = new MediaMetadataRetriever(); @@ -325,7 +322,7 @@ public class MusicDirectory implements Serializable { if (index != -1) { filename = filename.substring(index + 1); if (getTrack() != null) { - filename = filename.replace(String.format("%02d ", getTrack()), ""); + filename = filename.replace(String.format(Locale.getDefault(), "%02d ", getTrack()), ""); } index = filename.lastIndexOf('.'); @@ -601,17 +598,17 @@ public class MusicDirectory implements Serializable { this.closeness = closeness; } - public boolean isOnlineId(Context context) { - try { - String cacheLocation = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); - return cacheLocation == null || id == null || id.indexOf(cacheLocation) == -1; - } catch(Exception e) { - Log.w(TAG, "Failed to check online id validity"); - - // Err on the side of default functionality - return true; - } - } +// public boolean isOnlineId(Context context) { +// try { +// String cacheLocation = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); +// return cacheLocation == null || id == null || !id.contains(cacheLocation); +// } catch(Exception e) { +// Log.w(TAG, "Failed to check online id validity"); +// +// // Err on the side of default functionality +// return true; +// } +// } @Override public boolean equals(Object o) { @@ -631,49 +628,35 @@ public class MusicDirectory implements Serializable { return id.hashCode(); } - @Override + @NonNull + @Override public String toString() { return title; } public byte[] toByteArray() throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutput out = null; - try { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + ObjectOutput out; out = new ObjectOutputStream(bos); out.writeObject(this); out.flush(); return bos.toByteArray(); - } finally { - try { - bos.close(); - } catch (IOException ex) { - // ignore close exception - } } + // ignore close exception } public static Entry fromByteArray(byte[] byteArray) throws IOException, ClassNotFoundException { ByteArrayInputStream bis = new ByteArrayInputStream(byteArray); - ObjectInput in = null; - try { - in = new ObjectInputStream(bis); + try (ObjectInput in = new ObjectInputStream(bis)) { return (Entry) in.readObject(); - } finally { - try { - if (in != null) { - in.close(); - } - } catch (IOException ex) { - // ignore close exception - } } + // ignore close exception } } public static class EntryComparator implements Comparator<Entry> { - private boolean byYear; - private Collator collator; + private final boolean byYear; + private final Collator collator; public EntryComparator(boolean byYear) { this.byYear = byYear; @@ -715,7 +698,7 @@ public class MusicDirectory implements Serializable { Integer lhsTrack = lhs.getTrack(); Integer rhsTrack = rhs.getTrack(); - if(lhsTrack == rhsTrack) { + if(Objects.equals(lhsTrack, rhsTrack)) { return collator.compare(lhs.getTitle(), rhs.getTitle()); } else if(lhsTrack != null && rhsTrack != null) { return lhsTrack.compareTo(rhsTrack); @@ -730,7 +713,7 @@ public class MusicDirectory implements Serializable { } public static void sort(List<Entry> entries, boolean byYear) { try { - Collections.sort(entries, new EntryComparator(byYear)); + entries.sort(new EntryComparator(byYear)); } catch (Exception e) { Log.w(TAG, "Failed to sort MusicDirectory"); } |