Search in sources :

Example 1 with Queue

use of com.sdsmdg.harjot.MusicDNA.models.Queue in project MusicDNA by harjot-oberai.

the class HomeActivity method getLocalSongs.

private void getLocalSongs() {
    localTrackList.clear();
    recentlyAddedTrackList.clear();
    finalLocalSearchResultList.clear();
    finalRecentlyAddedTrackSearchResultList.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, MediaStore.MediaColumns.DATE_ADDED + " DESC");
    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);
                if (recentlyAddedTrackList.size() <= 50) {
                    recentlyAddedTrackList.add(lt);
                    finalRecentlyAddedTrackSearchResultList.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 Queue

use of com.sdsmdg.harjot.MusicDNA.models.Queue in project MusicDNA by harjot-oberai.

the class HomeActivity method getSavedData.

private void getSavedData() {
    try {
        Gson gson = new Gson();
        Log.d("TIME", "start");
        String json2 = mPrefs.getString("allPlaylists", "");
        allPlaylists = gson.fromJson(json2, AllPlaylists.class);
        Log.d("TIME", "allPlaylists");
        String json3 = mPrefs.getString("queue", "");
        queue = gson.fromJson(json3, Queue.class);
        Log.d("TIME", "queue");
        String json4 = mPrefs.getString("recentlyPlayed", "");
        recentlyPlayed = gson.fromJson(json4, RecentlyPlayed.class);
        Log.d("TIME", "recents");
        String json5 = mPrefs.getString("favouriteTracks", "");
        favouriteTracks = gson.fromJson(json5, Favourite.class);
        Log.d("TIME", "fav");
        String json6 = mPrefs.getString("queueCurrentIndex", "");
        queueCurrentIndex = gson.fromJson(json6, Integer.class);
        Log.d("TIME", "queueCurrentindex");
        String json8 = mPrefs.getString("settings", "");
        settings = gson.fromJson(json8, Settings.class);
        Log.d("TIME", "settings");
        String json9 = mPrefs.getString("equalizer", "");
        equalizerModel = gson.fromJson(json9, EqualizerModel.class);
        Log.d("TIME", "equalizer");
        String json = mPrefs.getString("savedDNAs", "");
        savedDNAs = gson.fromJson(json, AllSavedDNA.class);
        Log.d("TIME", "savedDNAs");
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        String json7 = mPrefs.getString("versionCode", "");
        prevVersionCode = gson.fromJson(json7, Integer.class);
        Log.d("TIME", "VersionCode : " + prevVersionCode + " : " + versionCode);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : AllSavedDNA(com.sdsmdg.harjot.MusicDNA.models.AllSavedDNA) RecentlyPlayed(com.sdsmdg.harjot.MusicDNA.models.RecentlyPlayed) EqualizerModel(com.sdsmdg.harjot.MusicDNA.models.EqualizerModel) Gson(com.google.gson.Gson) AllPlaylists(com.sdsmdg.harjot.MusicDNA.models.AllPlaylists) Queue(com.sdsmdg.harjot.MusicDNA.models.Queue) Favourite(com.sdsmdg.harjot.MusicDNA.models.Favourite) Settings(com.sdsmdg.harjot.MusicDNA.models.Settings) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with Queue

use of com.sdsmdg.harjot.MusicDNA.models.Queue in project MusicDNA by harjot-oberai.

the class HomeActivity method onShuffleEnabled.

@Override
public void onShuffleEnabled() {
    originalQueue = new Queue();
    for (UnifiedTrack ut : queue.getQueue()) {
        originalQueue.addToQueue(ut);
    }
    originalQueueIndex = queueCurrentIndex;
    UnifiedTrack ut = queue.getQueue().get(queueCurrentIndex);
    Collections.shuffle(queue.getQueue());
    for (int i = 0; i < queue.getQueue().size(); i++) {
        if (ut.equals(queue.getQueue().get(i))) {
            queue.getQueue().remove(i);
            break;
        }
    }
    queue.getQueue().add(0, ut);
    queueCurrentIndex = 0;
    new SaveQueue().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : UnifiedTrack(com.sdsmdg.harjot.MusicDNA.models.UnifiedTrack) Queue(com.sdsmdg.harjot.MusicDNA.models.Queue) TextPaint(android.text.TextPaint)

Aggregations

Queue (com.sdsmdg.harjot.MusicDNA.models.Queue)3 TextPaint (android.text.TextPaint)2 UnifiedTrack (com.sdsmdg.harjot.MusicDNA.models.UnifiedTrack)2 FileNotFoundException (java.io.FileNotFoundException)2 ContentResolver (android.content.ContentResolver)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 Gson (com.google.gson.Gson)1 Album (com.sdsmdg.harjot.MusicDNA.models.Album)1 AllPlaylists (com.sdsmdg.harjot.MusicDNA.models.AllPlaylists)1 AllSavedDNA (com.sdsmdg.harjot.MusicDNA.models.AllSavedDNA)1 Artist (com.sdsmdg.harjot.MusicDNA.models.Artist)1 EqualizerModel (com.sdsmdg.harjot.MusicDNA.models.EqualizerModel)1 Favourite (com.sdsmdg.harjot.MusicDNA.models.Favourite)1 LocalTrack (com.sdsmdg.harjot.MusicDNA.models.LocalTrack)1 MusicFolder (com.sdsmdg.harjot.MusicDNA.models.MusicFolder)1 Playlist (com.sdsmdg.harjot.MusicDNA.models.Playlist)1 RecentlyPlayed (com.sdsmdg.harjot.MusicDNA.models.RecentlyPlayed)1 Settings (com.sdsmdg.harjot.MusicDNA.models.Settings)1 AlbumComparator (com.sdsmdg.harjot.MusicDNA.utilities.comparators.AlbumComparator)1