Android Open Source - MusicPlayer-OLD Playlists






From Project

Back to project page MusicPlayer-OLD.

License

The source code is released under:

Apache License

If you think the Android project MusicPlayer-OLD listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2013 Andrea De Cesare//from   w  ww  . ja  v  a  2 s.  c  o  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andreadec.musicplayer;

import java.util.*;

import android.content.ContentValues;
import android.database.*;
import android.database.sqlite.*;

import com.andreadec.musicplayer.database.PlaylistsDatabase;

public class Playlists {
  private static ArrayList<Playlist> playlists;
  
  public static ArrayList<Playlist> getPlaylists() {
    if(playlists==null) loadPlaylists();
    return playlists;
  }
  
  private static void loadPlaylists() {
    playlists = new ArrayList<Playlist>();
    PlaylistsDatabase playlistsDatabase = new PlaylistsDatabase();
    SQLiteDatabase db = playlistsDatabase.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT id, name FROM Playlists ORDER BY position", null);
    while(cursor.moveToNext()) {
      long id = cursor.getLong(0);
      String name = cursor.getString(1);
      Playlist playlist = new Playlist(id, name);
      playlists.add(playlist);
    }
    cursor.close();
    db.close();
  }
  
  public static Playlist addPlaylist(String name) {
    if(playlists==null) loadPlaylists();
    long id = -1;
    PlaylistsDatabase playlistsDatabase = new PlaylistsDatabase();
    SQLiteDatabase db = playlistsDatabase.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("name", name);
    try {
      id = db.insertOrThrow("Playlists", null, values);
    } catch(Exception e) {
    } finally {
      db.close();
    }
    if(id==-1) return null; // Something went wrong
    
    Playlist playlist = new Playlist(id, name);
    playlists.add(playlist);
    return playlist;
  }
  
  public static void deletePlaylist(Playlist playlist) {
    if(playlists==null) loadPlaylists();
    PlaylistsDatabase playlistsDatabase = new PlaylistsDatabase();
    SQLiteDatabase db = playlistsDatabase.getWritableDatabase();
    db.delete("SongsInPlaylist", "idPlaylist="+playlist.getId(), null);
    db.delete("Playlists", "id="+playlist.getId(), null);
    db.close();
    playlists.remove(playlist);
  }
  
  public static void sortPlaylists(int from, int to) {
    if(playlists==null) loadPlaylists();
    if(to>from) {
      Collections.rotate(playlists.subList(from, to+1), -1);
    } else {
      Collections.rotate(playlists.subList(to, from+1), +1);
    }
    PlaylistsDatabase playlistsDatabase = new PlaylistsDatabase();
    SQLiteDatabase db = playlistsDatabase.getWritableDatabase();
    for(int i=0; i<playlists.size(); i++) {
      Playlist playlist = playlists.get(i);
      ContentValues values = new ContentValues();
      values.put("position", i);
      db.update("Playlists", values, "id="+playlist.getId(), null);
    }
    db.close();
  }
  
  public static PlaylistSong getSavedSongFromPlaylist(long idSong) {
    PlaylistSong song = null;
    PlaylistsDatabase playlistsDatabase = new PlaylistsDatabase();
    SQLiteDatabase db = playlistsDatabase.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT idPlaylist, uri, artist, title, hasImage FROM SongsInPlaylist WHERE idSong="+idSong, null);
    if(cursor.moveToNext()) {
      Playlist playlist = null;
      long idPlaylist = cursor.getLong(0);
      for(Playlist p : Playlists.getPlaylists()) {
        if(p.getId()==idPlaylist) playlist=p;
        break;
      }
      String uri = cursor.getString(1);
      String artist = cursor.getString(2);
      String title = cursor.getString(3);
      boolean hasImage = cursor.getInt(4)==1;
      song = new PlaylistSong(uri, artist, title, idSong, hasImage, playlist);
    }
    cursor.close();
    db.close();
    return song;
  }
}




Java Source Code List

com.andreadec.musicplayer.AboutActivity.java
com.andreadec.musicplayer.Action.java
com.andreadec.musicplayer.BrowserDirectory.java
com.andreadec.musicplayer.BrowserFragment.java
com.andreadec.musicplayer.BrowserSong.java
com.andreadec.musicplayer.Constants.java
com.andreadec.musicplayer.DirectoryChooserDialog.java
com.andreadec.musicplayer.ImageLoaderTask.java
com.andreadec.musicplayer.IndexFolderService.java
com.andreadec.musicplayer.Information.java
com.andreadec.musicplayer.MainActivity.java
com.andreadec.musicplayer.MusicPlayerApplication.java
com.andreadec.musicplayer.MusicPlayerFragment.java
com.andreadec.musicplayer.MusicService.java
com.andreadec.musicplayer.PlayableItem.java
com.andreadec.musicplayer.PlaylistFragment.java
com.andreadec.musicplayer.PlaylistSong.java
com.andreadec.musicplayer.Playlist.java
com.andreadec.musicplayer.Playlists.java
com.andreadec.musicplayer.PodcastEpisodeDownloaderService.java
com.andreadec.musicplayer.PodcastEpisode.java
com.andreadec.musicplayer.PodcastParser.java
com.andreadec.musicplayer.Podcast.java
com.andreadec.musicplayer.PodcastsFragment.java
com.andreadec.musicplayer.PreferencesActivity.java
com.andreadec.musicplayer.RadioFragment.java
com.andreadec.musicplayer.Radio.java
com.andreadec.musicplayer.SearchActivity.java
com.andreadec.musicplayer.ShakeListener.java
com.andreadec.musicplayer.Utils.java
com.andreadec.musicplayer.adapters.BrowserArrayAdapter.java
com.andreadec.musicplayer.adapters.MusicListArrayAdapter.java
com.andreadec.musicplayer.adapters.NavigationDrawerArrayAdapter.java
com.andreadec.musicplayer.adapters.PlaylistArrayAdapter.java
com.andreadec.musicplayer.adapters.PodcastsArrayAdapter.java
com.andreadec.musicplayer.adapters.RadioArrayAdapter.java
com.andreadec.musicplayer.adapters.SearchResultsArrayAdapter.java
com.andreadec.musicplayer.comparators.BrowserSongsComparator.java
com.andreadec.musicplayer.comparators.PlaylistsComparator.java
com.andreadec.musicplayer.database.PlaylistsDatabase.java
com.andreadec.musicplayer.database.PodcastsDatabase.java
com.andreadec.musicplayer.database.RadiosDatabase.java
com.andreadec.musicplayer.database.SongsDatabase.java
com.andreadec.musicplayer.filters.AudioFileFilter.java
com.andreadec.musicplayer.filters.DirectoryFilter.java