Search in sources :

Example 1 with LocalMusicComparator

use of com.sdsmdg.harjot.MusicDNA.utilities.comparators.LocalMusicComparator in project MusicDNA by harjot-oberai.

the class HomeActivity method getLocalSongs.

private void getLocalSongs() {
    localTrackList.clear();
    finalLocalSearchResultList.clear();
    albums.clear();
    finalAlbums.clear();
    artists.clear();
    finalArtists.clear();
    ContentResolver musicResolver = this.getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
    if (musicCursor != null && musicCursor.moveToFirst()) {
        //get columns
        int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
        int albumColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
        int pathColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
        int durationColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
        //add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            String thisAlbum = musicCursor.getString(albumColumn);
            String path = musicCursor.getString(pathColumn);
            long duration = musicCursor.getLong(durationColumn);
            if (duration > 10000) {
                LocalTrack lt = new LocalTrack(thisId, thisTitle, thisArtist, thisAlbum, path, duration);
                localTrackList.add(lt);
                finalLocalSearchResultList.add(lt);
                int pos;
                if (thisAlbum != null) {
                    pos = checkAlbum(thisAlbum);
                    if (pos != -1) {
                        albums.get(pos).getAlbumSongs().add(lt);
                    } else {
                        List<LocalTrack> llt = new ArrayList<>();
                        llt.add(lt);
                        Album ab = new Album(thisAlbum, llt);
                        albums.add(ab);
                    }
                    if (pos != -1) {
                        finalAlbums.get(pos).getAlbumSongs().add(lt);
                    } else {
                        List<LocalTrack> llt = new ArrayList<>();
                        llt.add(lt);
                        Album ab = new Album(thisAlbum, llt);
                        finalAlbums.add(ab);
                    }
                }
                if (thisArtist != null) {
                    pos = checkArtist(thisArtist);
                    if (pos != -1) {
                        artists.get(pos).getArtistSongs().add(lt);
                    } else {
                        List<LocalTrack> llt = new ArrayList<>();
                        llt.add(lt);
                        Artist ab = new Artist(thisArtist, llt);
                        artists.add(ab);
                    }
                    if (pos != -1) {
                        finalArtists.get(pos).getArtistSongs().add(lt);
                    } else {
                        List<LocalTrack> llt = new ArrayList<>();
                        llt.add(lt);
                        Artist ab = new Artist(thisArtist, llt);
                        finalArtists.add(ab);
                    }
                }
                File f = new File(path);
                String dirName = f.getParentFile().getName();
                if (getFolder(dirName) == null) {
                    MusicFolder mf = new MusicFolder(dirName);
                    mf.getLocalTracks().add(lt);
                    allMusicFolders.getMusicFolders().add(mf);
                } else {
                    getFolder(dirName).getLocalTracks().add(lt);
                }
            }
        } while (musicCursor.moveToNext());
    }
    if (musicCursor != null)
        musicCursor.close();
    System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
    try {
        if (localTrackList.size() > 0) {
            Collections.sort(localTrackList, new LocalMusicComparator());
            Collections.sort(finalLocalSearchResultList, new LocalMusicComparator());
        }
        if (albums.size() > 0) {
            Collections.sort(albums, new AlbumComparator());
            Collections.sort(finalAlbums, new AlbumComparator());
        }
        if (artists.size() > 0) {
            Collections.sort(artists, new ArtistComparator());
            Collections.sort(finalArtists, new ArtistComparator());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    List<UnifiedTrack> tmp = new ArrayList<>();
    boolean queueCurrentIndexCollision = false;
    int indexCorrection = 0;
    for (int i = 0; i < queue.getQueue().size(); i++) {
        UnifiedTrack ut = queue.getQueue().get(i);
        if (ut.getType()) {
            if (!checkTrack(ut.getLocalTrack())) {
                if (i == queueCurrentIndex) {
                    queueCurrentIndexCollision = true;
                } else if (i < queueCurrentIndex) {
                    indexCorrection++;
                }
                tmp.add(ut);
            }
        }
    }
    for (int i = 0; i < tmp.size(); i++) {
        queue.getQueue().remove(tmp.get(i));
    }
    if (queueCurrentIndexCollision) {
        if (queue.getQueue().size() > 0) {
            queueCurrentIndex = 0;
        } else {
            queue = new Queue();
        }
    } else {
        queueCurrentIndex -= indexCorrection;
    }
    tmp.clear();
    for (int i = 0; i < recentlyPlayed.getRecentlyPlayed().size(); i++) {
        UnifiedTrack ut = recentlyPlayed.getRecentlyPlayed().get(i);
        if (ut.getType()) {
            if (!checkTrack(ut.getLocalTrack())) {
                tmp.add(ut);
            }
        }
    }
    for (int i = 0; i < tmp.size(); i++) {
        recentlyPlayed.getRecentlyPlayed().remove(tmp.get(i));
    }
    List<UnifiedTrack> temp = new ArrayList<>();
    List<Playlist> tmpPL = new ArrayList<>();
    for (int i = 0; i < allPlaylists.getPlaylists().size(); i++) {
        Playlist pl = allPlaylists.getPlaylists().get(i);
        for (int j = 0; j < pl.getSongList().size(); j++) {
            UnifiedTrack ut = pl.getSongList().get(j);
            if (ut.getType()) {
                if (!checkTrack(ut.getLocalTrack())) {
                    temp.add(ut);
                }
            }
        }
        for (int j = 0; j < temp.size(); j++) {
            pl.getSongList().remove(temp.get(j));
        }
        temp.clear();
        if (pl.getSongList().size() == 0) {
            tmpPL.add(pl);
        }
    }
    for (int i = 0; i < tmpPL.size(); i++) {
        allPlaylists.getPlaylists().remove(tmpPL.get(i));
    }
    tmpPL.clear();
}
Also used : Artist(com.sdsmdg.harjot.MusicDNA.models.Artist) ArrayList(java.util.ArrayList) Album(com.sdsmdg.harjot.MusicDNA.models.Album) LocalTrack(com.sdsmdg.harjot.MusicDNA.models.LocalTrack) Cursor(android.database.Cursor) LocalMusicComparator(com.sdsmdg.harjot.MusicDNA.utilities.comparators.LocalMusicComparator) Uri(android.net.Uri) MusicFolder(com.sdsmdg.harjot.MusicDNA.models.MusicFolder) TextPaint(android.text.TextPaint) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver) Playlist(com.sdsmdg.harjot.MusicDNA.models.Playlist) UnifiedTrack(com.sdsmdg.harjot.MusicDNA.models.UnifiedTrack) AlbumComparator(com.sdsmdg.harjot.MusicDNA.utilities.comparators.AlbumComparator) File(java.io.File) Queue(com.sdsmdg.harjot.MusicDNA.models.Queue) ArtistComparator(com.sdsmdg.harjot.MusicDNA.utilities.comparators.ArtistComparator)

Example 2 with LocalMusicComparator

use of com.sdsmdg.harjot.MusicDNA.utilities.comparators.LocalMusicComparator in project MusicDNA by harjot-oberai.

the class HomeActivity method refreshAlbumAndArtists.

public void refreshAlbumAndArtists() {
    albums.clear();
    finalAlbums.clear();
    artists.clear();
    finalArtists.clear();
    for (int i = 0; i < localTrackList.size(); i++) {
        LocalTrack lt = localTrackList.get(i);
        String thisAlbum = lt.getAlbum();
        int pos = checkAlbum(thisAlbum);
        if (pos != -1) {
            albums.get(pos).getAlbumSongs().add(lt);
        } else {
            List<LocalTrack> llt = new ArrayList<>();
            llt.add(lt);
            Album ab = new Album(thisAlbum, llt);
            albums.add(ab);
        }
        if (pos != -1) {
            finalAlbums.get(pos).getAlbumSongs().add(lt);
        } else {
            List<LocalTrack> llt = new ArrayList<>();
            llt.add(lt);
            Album ab = new Album(thisAlbum, llt);
            finalAlbums.add(ab);
        }
        String thisArtist = lt.getArtist();
        pos = checkArtist(thisArtist);
        if (pos != -1) {
            artists.get(pos).getArtistSongs().add(lt);
        } else {
            List<LocalTrack> llt = new ArrayList<>();
            llt.add(lt);
            Artist ab = new Artist(thisArtist, llt);
            artists.add(ab);
        }
        if (pos != -1) {
            finalArtists.get(pos).getArtistSongs().add(lt);
        } else {
            List<LocalTrack> llt = new ArrayList<>();
            llt.add(lt);
            Artist ab = new Artist(thisArtist, llt);
            finalArtists.add(ab);
        }
    }
    System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
    try {
        if (localTrackList.size() > 0) {
            Collections.sort(localTrackList, new LocalMusicComparator());
            Collections.sort(finalLocalSearchResultList, new LocalMusicComparator());
        }
        if (albums.size() > 0) {
            Collections.sort(albums, new AlbumComparator());
            Collections.sort(finalAlbums, new AlbumComparator());
        }
        if (artists.size() > 0) {
            Collections.sort(artists, new ArtistComparator());
            Collections.sort(finalArtists, new ArtistComparator());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Artist(com.sdsmdg.harjot.MusicDNA.models.Artist) ArrayList(java.util.ArrayList) Album(com.sdsmdg.harjot.MusicDNA.models.Album) LocalTrack(com.sdsmdg.harjot.MusicDNA.models.LocalTrack) LocalMusicComparator(com.sdsmdg.harjot.MusicDNA.utilities.comparators.LocalMusicComparator) AlbumComparator(com.sdsmdg.harjot.MusicDNA.utilities.comparators.AlbumComparator) TextPaint(android.text.TextPaint) ArtistComparator(com.sdsmdg.harjot.MusicDNA.utilities.comparators.ArtistComparator) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

TextPaint (android.text.TextPaint)2 Album (com.sdsmdg.harjot.MusicDNA.models.Album)2 Artist (com.sdsmdg.harjot.MusicDNA.models.Artist)2 LocalTrack (com.sdsmdg.harjot.MusicDNA.models.LocalTrack)2 AlbumComparator (com.sdsmdg.harjot.MusicDNA.utilities.comparators.AlbumComparator)2 ArtistComparator (com.sdsmdg.harjot.MusicDNA.utilities.comparators.ArtistComparator)2 LocalMusicComparator (com.sdsmdg.harjot.MusicDNA.utilities.comparators.LocalMusicComparator)2 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 ContentResolver (android.content.ContentResolver)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 MusicFolder (com.sdsmdg.harjot.MusicDNA.models.MusicFolder)1 Playlist (com.sdsmdg.harjot.MusicDNA.models.Playlist)1 Queue (com.sdsmdg.harjot.MusicDNA.models.Queue)1 UnifiedTrack (com.sdsmdg.harjot.MusicDNA.models.UnifiedTrack)1 File (java.io.File)1