package com.ved.musicmapapp.adapters; import java.lang.reflect.Array; import java.util.ArrayList; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.util.Log; import com.ved.musicmapapp.MainActivity; import com.ved.musicmapapp.Objects.Artist; import com.ved.musicmapapp.Objects.Friend; import com.ved.musicmapapp.Objects.Genre; import com.ved.musicmapapp.Objects.Playlist; import com.ved.musicmapapp.Objects.PlaylistArSong; import com.ved.musicmapapp.Objects.PlaylistArtist; import com.ved.musicmapapp.Objects.Song; import com.ved.musicmapapp.Objects.User; public class DBAdapter { public static final String AUTHORITY = "musicMap";// specific for our app, // will be specified in // manifest public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY); public static final String prefix = "n_"; public static final String COLUMN_ID_AUTO = prefix + "id_auto"; public static final String COLUMN_ID = prefix + "id"; public static final String COLUMN_LAST_ADDED = prefix + "last_added"; public static final String COLUMN_NO_LAST_ADD = prefix + "no_last_add"; public static final String COLUMN_PERCENTAGE = prefix + "_percentage"; public static final String COLUMN_USER_ID = prefix + "user_id"; public static final String COLUMN_ARTIST_ID = prefix + "ARTIST_id"; public static final String COLUMN_SONG_ID = prefix + "song_id"; public static final String COLUMN_LAST_UPDATE = prefix + "last_update"; // TABLE USERS public static final String TABLE_USERS = prefix + "users"; public static final String COLUMN_USERNAME = prefix + "username"; public static final String COLUMN_FBID = prefix + "fbid"; public static final String COLUMN_FIRST_NAME = prefix + "first_name"; public static final String COLUMN_LAST_NAME = prefix + "last_name"; public static final String COLUMN_FULL_NAME = prefix + "full_name"; public static final String COLUMN_EMAIL = prefix + "email"; public static final String COLUMN_BIRTHDAY = prefix + "birthday"; public static final String COLUMN_CITY = prefix + "city"; public static final String COLUMN_COUNTRY = prefix + "country"; public static final String COLUMN_ADDRESS = prefix + "address"; public static final String COLUMN_LAT = prefix + "lat"; public static final String COLUMN_LON = prefix + "lon"; public static final String COLUMN_ABOUT_ME = prefix + "about_me"; public static final String COLUMN_GENDER = prefix + "gender"; public static final String COLUMN_RELIGION = prefix + "religion"; public static final String COLUMN_DEVICE_ID = prefix + "device_id"; public static final String COLUMN_JOIN_DATE = prefix + "join_date"; public static final String COLUMN_GCM_REGID = prefix + "gcm_regid"; public static final String COLUMN_DEVICE_TOKEN = prefix + "device_token"; public static final String COLUMN_IS_FOLLOW_ME = prefix + "is_follow_me"; public static final String COLUMN_IS_FOLLOWED = prefix + "is_followed"; public static final String COLUMN_NO_SONGS = prefix + "no_songs"; public static final String COLUMN_ARTIST_COUNT = prefix + "artist_count"; // TABLE ARTIST public static final String TABLE_ARTISTS = prefix + "artists"; public static final String COLUMN_NAME = prefix + "name"; public static final String COLUMN_NAME_CLEANED = prefix + "name_cleaned"; public static final String COLUMN_AVATAR_URL = prefix + "avatar_url"; public static final String COLUMN_AVATAR = prefix + "avatar"; public static final String COLUMN_GLOBAL_PLAYLIST = prefix + "global_playlist"; public static final String COLUMN_IS_LIKED = prefix + "is_liked"; public static final String COLUMN_HAS_ARTIST = prefix + "has_artist"; //TABLE PLAYLIST public static final String TABLE_PLAYLIST = prefix + "playlist"; public static final String COLUMN_DATE_ADDED = prefix + "date_added"; public static final String COLUMN_PLAYLIST_ID = prefix + "playlist_id"; public static final String COLUMN_PLAYLIST_DB_ID = prefix + "playlist_db_id"; public static final String COLUMN_IS_SYNC = prefix + "is_synch"; public static final String COLUMN_ARTIST_AVATAR = prefix + "artist_avatar"; public static final String COLUMN_TRACK_COUNT = prefix + "track_count"; public static final String TABLE_PLAYLIST_ARTISTS = prefix + "playlist_artists"; // public static final String TABLE_PLAYLIST_SONGS = prefix + "playlist_songs"; public static final String TABLE_PLAYLIST_AR_SONGS = prefix + "playlist_ar_songs"; // TABLE ARTIST public static final String TABLE_FRIENDS = prefix + "friends"; public static final String COLUMN_FBID_F = prefix + "fbid_f"; // TABLE SONG public static final String TABLE_SONGS = prefix + "songs"; public static final String COLUMN_TITLE = prefix + "title"; public static final String COLUMN_TITLE_CLEANED = prefix + "title_cleaned"; public static final String COLUMN_ALBUM = prefix + "album"; public static final String COLUMN_BUY_LINK_US = prefix + "buy_link_us"; public static final String COLUMN_BUY_LINK_GB = prefix + "buy_link_gb"; public static final String COLUMN_AMAZON_LINK = prefix + "amazon_link"; public static final String COLUMN_DELETED = prefix + "deleted"; public static final String COLUMN_LAST_LISTEN = prefix + "last_listen"; public static final String COLUMN_7DIGITAL_TRACK_ID_US = prefix + "7digital_track_id_us"; public static final String COLUMN_7DIGITAL_TRACK_ID_GB = prefix + "7digital_track_id_gb"; public static final String COLUMN_IS_HAS_SONG = prefix + "is_has_song"; // TABLE USERS_ARTISTS public static final String TABLE_USERS_ARTISTS = prefix + "users_artists"; // TABLE FB_BOOKS public static final String TABLE_FB_BOOKS = prefix + "fb_books"; public static final String COLUMN_BOOK_NAME = prefix + "book_name"; // TABLE FB_GAMES public static final String TABLE_FB_GAMES = prefix + "fb_games"; public static final String COLUMN_GAME_NAME = prefix + "game_name"; // TABLE FB_LANGUAGE public static final String TABLE_FB_LANGUAGES = prefix + "fb_languages"; public static final String COLUMN_LANGUAGE_NAME = prefix + "language_name"; // TABLE FB_MOVIES public static final String TABLE_FB_MOVIES = prefix + "fb_movies"; public static final String COLUMN_MOVIE_NAME = prefix + "movie_name"; // TABLE FB_MUSICS public static final String TABLE_FB_MUSICS = prefix + "fb_musics"; public static final String COLUMN_MUSIC_NAME = prefix + "music_name"; // TABLE FB_OTHERS public static final String TABLE_FB_OTHERS = prefix + "fb_others"; public static final String COLUMN_OTHER_NAME = prefix + "other_name"; // TABLE FB_SCHOOLS public static final String TABLE_FB_SCHOOLS = prefix + "fb_schools"; public static final String COLUMN_STYPE = prefix + "stype"; public static final String COLUMN_SNAME = prefix + "sname"; public static final String COLUMN_SYEAR = prefix + "syear"; // TABLE FB_WORKS public static final String TABLE_FB_WORKS = prefix + "fb_works"; public static final String COLUMN_EMPLOYER = prefix + "employer"; public static final String COLUMN_LOCATION = prefix + "location"; public static final String COLUMN_POSITION = prefix + "position"; public static final String COLUMN_DESCRIPTION = prefix + "description"; public static final String COLUMN_START_DATE = prefix + "start_date"; public static final String COLUMN_END_DATE = prefix + "end_date"; // TABLE FOLLOW_USERS public static final String TABLE_FOLLOW_USERS = prefix + "follow_users"; public static final String COLUMN_FOLLOWER_ID = prefix + "follower_id"; public static final String COLUMN_FOLLOWED_ID = prefix + "followed_id"; public static final String COLUMN_FOLLOWED = prefix + "followed"; public static final String COLUMN_START_FOLLOW = prefix + "start_follow"; public static final String COLUMN_END_FOLLOW = prefix + "end_follow"; // TABLE LIKE_SONGS public static final String TABLE_LIKE_SONGS = prefix + "like_songs"; public static final String COLUMN_LIKED = prefix + "liked"; public static final String COLUMN_START_LIKE = prefix + "start_like"; public static final String COLUMN_END_LIKE = prefix + "end_like"; // TABLE LIKE_USER_ARTISTS public static final String TABLE_LIKE_USER_ARTISTS = prefix + "like_user_artist"; public static final String COLUMN_USER_ARTIST_ID = prefix + "user_artist_id"; //TABLE USER GENERE public static final String TABLE_GENRE = prefix + "genre"; public static final String COLUMN_GENRE_ARTIST_ID = prefix + "artist_name"; public static final String COLUMN_GENRE_NAME = prefix + "genre_name"; private Context mContext; // Database creation SQL statement public static final String DATABASE_CREATE_USER = "CREATE TABLE IF NOT EXISTS " + TABLE_USERS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USERNAME + " text not null, " + COLUMN_FBID + " text not null, " + COLUMN_AVATAR + " blob , " + COLUMN_FIRST_NAME + " text not null, " + COLUMN_LAST_NAME + " text not null, " + COLUMN_FULL_NAME + " text not null, " + COLUMN_EMAIL + " text not null, " + COLUMN_BIRTHDAY + " text not null, " + COLUMN_CITY + " text not null, " + COLUMN_COUNTRY + " text not null, " + COLUMN_ADDRESS + " text not null, " + COLUMN_LAT + " double not null, " + COLUMN_LON + " double not null, " + COLUMN_ABOUT_ME + " text not null, " + COLUMN_GENDER + " text not null, " + COLUMN_RELIGION + " text not null, " + COLUMN_DEVICE_ID + " text not null, " + COLUMN_JOIN_DATE + " bigint not null, " + COLUMN_IS_FOLLOW_ME + " integer, " + COLUMN_IS_FOLLOWED + " integer, " + COLUMN_NO_SONGS + " integer, " + COLUMN_ARTIST_COUNT + " integer, " + COLUMN_PERCENTAGE + " integer, " + COLUMN_GCM_REGID + " text not null, " + COLUMN_LAST_ADDED + " text, " + COLUMN_NO_LAST_ADD + " integer, " + COLUMN_DEVICE_TOKEN + " text);"; public static final String DATABASE_CREATE_ARTIST = "CREATE TABLE IF NOT EXISTS " + TABLE_ARTISTS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_NAME + " text not null, " + COLUMN_LAST_UPDATE + " integer not null, " + COLUMN_GLOBAL_PLAYLIST + " integer not null, " + COLUMN_HAS_ARTIST + " integer not null, " + COLUMN_IS_LIKED + " integer not null, " + COLUMN_START_LIKE + " integer not null, " + COLUMN_END_LIKE + " integer not null, " + COLUMN_USER_ID + " text not null, " + COLUMN_AVATAR + " text, " + COLUMN_USER_ARTIST_ID + " integer not null);"; public static final String DATABASE_CREATE_SONG = "CREATE TABLE IF NOT EXISTS " + TABLE_SONGS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_TITLE + " text not null, " + COLUMN_ALBUM + " text not null, " + COLUMN_BUY_LINK_US + " text, " + COLUMN_BUY_LINK_GB + " text, " + COLUMN_7DIGITAL_TRACK_ID_US + " text, " +COLUMN_7DIGITAL_TRACK_ID_GB + " text, " + COLUMN_IS_HAS_SONG + " integer not null, " + COLUMN_IS_LIKED + " integer not null, " + COLUMN_START_LIKE + " bigint not null, " + COLUMN_USER_ID + " integer, " + COLUMN_END_LIKE + " bigint not null, " + COLUMN_ARTIST_ID + " integer not null);"; public static final String DATABASE_CREATE_PLAYLIST = "CREATE TABLE IF NOT EXISTS " + TABLE_PLAYLIST + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_NAME + " text not null, " + COLUMN_ARTIST_AVATAR + " text not null, " + COLUMN_TRACK_COUNT + " integer not null, " + COLUMN_DATE_ADDED + " integer not null, " + COLUMN_LAST_UPDATE + " integer not null);"; public static final String DATABASE_CREATE_GENRE = "CREATE TABLE IF NOT EXISTS " + TABLE_GENRE + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_GENRE_ARTIST_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_GENRE_NAME + " text not null);"; // public static final String DATABASE_CREATE_PLAYLIST_SONG = "CREATE TABLE IF NOT EXISTS " // + TABLE_PLAYLIST_SONGS // + " (" // + COLUMN_ID_AUTO // + " integer primary key autoincrement, " // + COLUMN_ID // + " integer not null, " // + COLUMN_USER_ID // + " integer, " // + COLUMN_IS_SYNC // + " integer not null, " // + COLUMN_PLAYLIST_ID // + " integer not null, " // + COLUMN_PLAYLIST_DB_ID // + " integer not null);"; public static final String DATABASE_CREATE_PLAYLIST_ARTIST = "CREATE TABLE IF NOT EXISTS " + TABLE_PLAYLIST_ARTISTS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_NAME + " text not null, " + COLUMN_LAST_UPDATE + " integer not null, " + COLUMN_GLOBAL_PLAYLIST + " integer not null, " + COLUMN_HAS_ARTIST + " integer not null, " + COLUMN_IS_LIKED + " integer not null, " + COLUMN_START_LIKE + " integer not null, " + COLUMN_END_LIKE + " integer not null, " + COLUMN_USER_ID + " text not null, " + COLUMN_AVATAR + " text ," + COLUMN_PLAYLIST_ID + " integer not null);"; public static final String DATABASE_CREATE_PL_ART_SONG = "CREATE TABLE IF NOT EXISTS " + TABLE_PLAYLIST_AR_SONGS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_TITLE + " text not null, " + COLUMN_ALBUM + " text not null, " + COLUMN_BUY_LINK_US + " text, " + COLUMN_BUY_LINK_GB + " text, " + COLUMN_7DIGITAL_TRACK_ID_US + " text, " +COLUMN_7DIGITAL_TRACK_ID_GB + " text, " + COLUMN_IS_HAS_SONG + " integer not null, " + COLUMN_IS_LIKED + " integer not null, " + COLUMN_START_LIKE + " bigint not null, " + COLUMN_USER_ID + " integer, " + COLUMN_END_LIKE + " bigint not null, " + COLUMN_ARTIST_ID + " integer not null, " + COLUMN_PLAYLIST_ID + " integer not null, " + COLUMN_IS_SYNC + " integer not null);"; public static final String DATABASE_CREATE_FRIEND = "CREATE TABLE IF NOT EXISTS " + TABLE_FRIENDS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_FBID + " text not null, " + COLUMN_NAME + " text not null, " + COLUMN_FBID_F + " text not null, " + COLUMN_AVATAR + " blob not null);"; public static final String DATABASE_CREATE_USER_ARTIST = "CREATE TABLE IF NOT EXISTS " + TABLE_USERS_ARTISTS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_ARTIST_ID + " integer not null);"; public static final String DATABASE_CREATE_FB_BOOK = "CREATE TABLE IF NOT EXISTS " + TABLE_FB_BOOKS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_BOOK_NAME + " text not null);"; public static final String DATABASE_CREATE_FB_GAME = "CREATE TABLE IF NOT EXISTS " + TABLE_FB_GAMES + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_GAME_NAME + " text not null);"; public static final String DATABASE_CREATE_FB_LANGUAGE = "CREATE TABLE IF NOT EXISTS " + TABLE_FB_LANGUAGES + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_LANGUAGE_NAME + " text not null);"; public static final String DATABASE_CREATE_FB_MOVIE = "CREATE TABLE IF NOT EXISTS " + TABLE_FB_MOVIES + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_MOVIE_NAME + " text not null);"; public static final String DATABASE_CREATE_FB_MUSIC = "CREATE TABLE IF NOT EXISTS " + TABLE_FB_MUSICS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_MUSIC_NAME + " text not null);"; public static final String DATABASE_CREATE_FB_OTHER = "CREATE TABLE IF NOT EXISTS " + TABLE_FB_OTHERS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_OTHER_NAME + " text not null);"; public static final String DATABASE_CREATE_FB_SCHOOL = "CREATE TABLE IF NOT EXISTS " + TABLE_FB_SCHOOLS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_STYPE + " text not null, " + COLUMN_SNAME + " text not null, " + COLUMN_SYEAR + " integer not null);"; public static final String DATABASE_CREATE_FB_WORK = "CREATE TABLE IF NOT EXISTS " + TABLE_FB_WORKS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_EMPLOYER + " text not null, " + COLUMN_LOCATION + " text not null, " + COLUMN_POSITION + " text not null, " + COLUMN_DESCRIPTION + " text not null, " + COLUMN_START_DATE + " text not null, " + COLUMN_END_DATE + " text not null);"; public static final String DATABASE_CREATE_FOLLOW_USER = "CREATE TABLE IF NOT EXISTS " + TABLE_FOLLOW_USERS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_FOLLOWER_ID + " integer not null, " + COLUMN_FOLLOWED_ID + " integer not null, " + COLUMN_FOLLOWED + " integer not null, " + COLUMN_START_FOLLOW + " bigint not null, " + COLUMN_END_FOLLOW + " bigint not null);"; public static final String DATABASE_CREATE_LIKE_SONG = "CREATE TABLE IF NOT EXISTS " + TABLE_LIKE_SONGS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_SONG_ID + " integer not null, " + COLUMN_LIKED + " integer not null, " + COLUMN_START_LIKE + " bigint not null, " + COLUMN_END_LIKE + " bigint not null);"; public static final String DATABASE_CREATE_LIKE_USER_ARTIST = "CREATE TABLE IF NOT EXISTS " + TABLE_LIKE_USER_ARTISTS + " (" + COLUMN_ID_AUTO + " integer primary key autoincrement, " + COLUMN_ID + " integer not null, " + COLUMN_USER_ID + " integer not null, " + COLUMN_USER_ARTIST_ID + " integer not null, " + COLUMN_LIKED + " integer not null, " + COLUMN_START_LIKE + " bigint not null, " + COLUMN_END_LIKE + " bigint not null);"; public DBAdapter(Context ctx) { this.mContext = ctx; } public boolean recreateTable(String tableName) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, tableName); int value = mContext.getContentResolver() .delete(contentUri, null, null); return value > 0; } /** * * @param name * @return */ public boolean updatePlaylist(Playlist playlist, User user) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST); ContentValues values = new ContentValues(); values.put(COLUMN_USER_ID, user.getID()); values.put(COLUMN_ID, playlist.getID()); values.put(COLUMN_NAME, playlist.getPlaylistName()); values.put(COLUMN_ARTIST_AVATAR, playlist.getArtistAvatar()); values.put(COLUMN_TRACK_COUNT, playlist.getNumberOfTracks()); values.put(COLUMN_DATE_ADDED, playlist.getDateAdded()); values.put(COLUMN_LAST_UPDATE, playlist.getLastUpdated()); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "' AND "+COLUMN_ID+"='"+playlist.getID()+"'", null, null); int updatedRows = -1; if (cursor.moveToFirst()) { int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID)); updatedRows = mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + id, null); } if(updatedRows > 0) return true; else return false; } public boolean addUserPlaylistIfNotExists(Playlist playlist, User user) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST); ContentValues values = new ContentValues(); values.put(COLUMN_USER_ID, user.getID()); values.put(COLUMN_ID, playlist.getID()); values.put(COLUMN_NAME, playlist.getPlaylistName()); values.put(COLUMN_ARTIST_AVATAR, playlist.getArtistAvatar()); values.put(COLUMN_TRACK_COUNT, playlist.getNumberOfTracks()); values.put(COLUMN_DATE_ADDED, playlist.getDateAdded()); values.put(COLUMN_LAST_UPDATE, playlist.getLastUpdated()); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "' AND "+COLUMN_NAME+"='" +playlist.getPlaylistName()+"'", null, null); long newID = -1; if (!cursor.moveToFirst()) { // if doesn't exists Uri result = mContext.getContentResolver().insert(contentUri, values); newID= ContentUris.parseId(result); } if(newID > 0) return true; else return false; } public boolean addUserPlaylist(Playlist playlist, User user) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST); ContentValues values = new ContentValues(); values.put(COLUMN_USER_ID, user.getID()); values.put(COLUMN_ID, playlist.getID()); values.put(COLUMN_NAME, playlist.getPlaylistName()); values.put(COLUMN_ARTIST_AVATAR, playlist.getArtistAvatar()); values.put(COLUMN_TRACK_COUNT, playlist.getNumberOfTracks()); values.put(COLUMN_DATE_ADDED, playlist.getDateAdded()); values.put(COLUMN_LAST_UPDATE, playlist.getLastUpdated()); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "' AND "+COLUMN_ID+"='"+playlist.getID()+"'", null, null); long newID = -1; if (cursor.moveToFirst()) { int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID)); mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + id, null); } else { Uri result = mContext.getContentResolver().insert(contentUri, values); newID= ContentUris.parseId(result); } if(newID > 0) return true; else return false; } public void addUser(User user) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_USERS); ContentValues values = new ContentValues(); values.put(COLUMN_ID, user.getID()); values.put(COLUMN_USERNAME, user.getUserName()); values.put(COLUMN_FBID, user.getFbid()); values.put(COLUMN_AVATAR, user.getAvatar()); values.put(COLUMN_FIRST_NAME, user.getFirstName()); values.put(COLUMN_LAST_NAME, user.getLastName()); values.put(COLUMN_FULL_NAME, user.getFullName()); values.put(COLUMN_EMAIL, user.getEmail()); values.put(COLUMN_BIRTHDAY, user.getBirthday()); values.put(COLUMN_CITY, user.getCity()); values.put(COLUMN_COUNTRY, user.getCountry()); values.put(COLUMN_ADDRESS, user.getAddress()); values.put(COLUMN_LAT, user.getLat()); values.put(COLUMN_LON, user.getLon()); values.put(COLUMN_IS_FOLLOW_ME, user.isFollowMe() ? 1 : 0); values.put(COLUMN_IS_FOLLOWED, user.isFollowed()); values.put(COLUMN_ABOUT_ME, user.getAboutMe()); values.put(COLUMN_GENDER, user.getGender()); values.put(COLUMN_RELIGION, user.getReligion()); values.put(COLUMN_DEVICE_ID, user.getDeviceId()); values.put(COLUMN_JOIN_DATE, user.getJoinDate()); values.put(COLUMN_GCM_REGID, user.getGcmRegId()); values.put(COLUMN_NO_SONGS, user.getNoSong()); values.put(COLUMN_ARTIST_COUNT, user.getArtistcount()); values.put(COLUMN_PERCENTAGE, user.getPercentage()); values.put(COLUMN_LAST_ADDED, user.getLastUpdate()); values.put(COLUMN_NO_LAST_ADD, user.getNoLastAdd()); // values.put(COLUMN_DEVICE_TOKEN, user.getDe); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_FBID + "='" + user.getFbid() + "'", null, null); Log.d("Men", "save--DB" + user.getUserName()); if (cursor.moveToFirst()) { Log.d("Men", "save--DB1" + user.getUserName()); int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID)); mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + id, null); } else { Log.d("Men", "save--DB2" + user.getUserName()); mContext.getContentResolver().insert(contentUri, values); Log.d("Men", "save--DB3" + user.getUserName()); } if (cursor != null) cursor.close(); if(user.getPlaylists() != null) { for(Playlist pl : user.getPlaylists()) { addUserPlaylist(pl, user); } } //artist if(user.getArtists() != null){ removeAllArtistAndSongByUser(user); ArrayList<Song> songs = new ArrayList<Song>(); for (Artist artist : user.getArtists()) { songs.addAll(artist.getSongs()); } ArrayList<Genre> genres = new ArrayList<Genre>(); for (Artist artist : user.getArtists()) { if(artist.getGenre() != null) genres.addAll(artist.getGenre()); } AddArtistsArrs(user.getArtists(), user); AddSongArrs(songs); addGenre(genres,user.getID()); } } public User getUser(String fbid) { User user = null; Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_USERS); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_FBID + "='" + fbid + "'", null, null); if (cursor.moveToFirst()) { user = new User(); user.setID(cursor.getInt(cursor.getColumnIndex(COLUMN_ID))); user.setUserName(cursor.getString(cursor .getColumnIndex(COLUMN_USERNAME))); user.setFbid(cursor.getString(cursor.getColumnIndex(COLUMN_FBID))); user.setAvatar(cursor.getBlob(cursor.getColumnIndex(COLUMN_AVATAR))); user.setFirstName(cursor.getString(cursor .getColumnIndex(COLUMN_FIRST_NAME))); user.setLastName(cursor.getString(cursor .getColumnIndex(COLUMN_LAST_NAME))); user.setFullName(cursor.getString(cursor .getColumnIndex(COLUMN_FULL_NAME))); user.setEmail(cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL))); user.setBirthday(cursor.getString(cursor .getColumnIndex(COLUMN_BIRTHDAY))); user.setCity(cursor.getString(cursor.getColumnIndex(COLUMN_CITY))); user.setCountry(cursor.getString(cursor .getColumnIndex(COLUMN_COUNTRY))); user.setAddress(cursor.getString(cursor .getColumnIndex(COLUMN_ADDRESS))); user.setLat(cursor.getDouble(cursor.getColumnIndex(COLUMN_LAT))); user.setLon(cursor.getDouble(cursor.getColumnIndex(COLUMN_LON))); user.setAboutMe(cursor.getString(cursor .getColumnIndex(COLUMN_ABOUT_ME))); user.setGender(cursor.getString(cursor .getColumnIndex(COLUMN_GENDER))); user.setReligion(cursor.getString(cursor .getColumnIndex(COLUMN_RELIGION))); user.setDeviceId(cursor.getString(cursor .getColumnIndex(COLUMN_DEVICE_ID))); user.setJoinDate(cursor.getLong(cursor .getColumnIndex(COLUMN_JOIN_DATE))); user.setGcmRegId(cursor.getString(cursor .getColumnIndex(COLUMN_GCM_REGID))); user.setNoSong(cursor.getInt(cursor.getColumnIndex(COLUMN_NO_SONGS))); user.setArtistcount(cursor.getInt(cursor .getColumnIndex(COLUMN_ARTIST_COUNT))); user.setLastUpdate(cursor.getString(cursor .getColumnIndex(COLUMN_LAST_ADDED))); user.setNoLastAdd(cursor.getInt(cursor .getColumnIndex(COLUMN_NO_LAST_ADD))); user.setPercentage(cursor.getInt(cursor .getColumnIndex(COLUMN_PERCENTAGE))); user.setArtists(getArtist(user)); user.setGenre(getAllGenres(user)); user.setPlaylists(getUserPlaylists(user)); } cursor.close(); return user; } public ArrayList<User> getAllUsers() { ArrayList<User> myArrs = new ArrayList<User>(); User user; Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_USERS); Cursor cursor = mContext.getContentResolver().query(contentUri, null, null, null, null); if (cursor.moveToFirst()) { do { user = new User(); user.setID(cursor.getInt(cursor.getColumnIndex(COLUMN_ID))); user.setUserName(cursor.getString(cursor .getColumnIndex(COLUMN_USERNAME))); user.setFbid(cursor.getString(cursor .getColumnIndex(COLUMN_FBID))); user.setAvatar(cursor.getBlob(cursor .getColumnIndex(COLUMN_AVATAR))); user.setFirstName(cursor.getString(cursor .getColumnIndex(COLUMN_FIRST_NAME))); user.setLastName(cursor.getString(cursor .getColumnIndex(COLUMN_LAST_NAME))); user.setFollowMe(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_FOLLOW_ME)) == 1 ? true : false); user.setFollowed(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_FOLLOWED)) == 1 ? true : false); user.setFullName(cursor.getString(cursor .getColumnIndex(COLUMN_FULL_NAME))); user.setEmail(cursor.getString(cursor .getColumnIndex(COLUMN_EMAIL))); user.setBirthday(cursor.getString(cursor .getColumnIndex(COLUMN_BIRTHDAY))); user.setCity(cursor.getString(cursor .getColumnIndex(COLUMN_CITY))); user.setCountry(cursor.getString(cursor .getColumnIndex(COLUMN_COUNTRY))); user.setAddress(cursor.getString(cursor .getColumnIndex(COLUMN_ADDRESS))); user.setLat(cursor.getDouble(cursor.getColumnIndex(COLUMN_LAT))); user.setLon(cursor.getDouble(cursor.getColumnIndex(COLUMN_LON))); user.setAboutMe(cursor.getString(cursor .getColumnIndex(COLUMN_ABOUT_ME))); user.setGender(cursor.getString(cursor .getColumnIndex(COLUMN_GENDER))); user.setReligion(cursor.getString(cursor .getColumnIndex(COLUMN_RELIGION))); user.setDeviceId(cursor.getString(cursor .getColumnIndex(COLUMN_DEVICE_ID))); user.setJoinDate(cursor.getLong(cursor .getColumnIndex(COLUMN_JOIN_DATE))); user.setGcmRegId(cursor.getString(cursor .getColumnIndex(COLUMN_GCM_REGID))); user.setNoSong(cursor.getInt(cursor .getColumnIndex(COLUMN_NO_SONGS))); user.setArtistcount(cursor.getInt(cursor .getColumnIndex(COLUMN_ARTIST_COUNT))); user.setLastUpdate(cursor.getString(cursor .getColumnIndex(COLUMN_LAST_ADDED))); user.setNoLastAdd(cursor.getInt(cursor .getColumnIndex(COLUMN_NO_LAST_ADD))); user.setPercentage(cursor.getInt(cursor .getColumnIndex(COLUMN_PERCENTAGE))); user.setArtists(getArtist(user)); user.setPlaylists(getUserPlaylists(user)); user.setGenre(getAllGenres(user)); myArrs.add(user); } while (cursor.moveToNext()); } cursor.close(); return myArrs; } public ArrayList<String> getAllGenres(User user){ ArrayList<String> allGenres = new ArrayList<String>(); for(int i = 0;i<user.getArtists().size();i++){ Artist arts = user.getArtists().get(i); if(arts.getGenre() != null) { for(int j = 0;j<arts.getGenre().size();j++){ ArrayList<String> g = arts.getGenreNames(); for(String gg : g){ if(!allGenres.toString().toLowerCase().contains(gg.toLowerCase())) allGenres.add(Character.toUpperCase(gg.trim().charAt(0)) + gg.trim().substring(1)); } } } } return allGenres; } public long addGenre(ArrayList<Genre> genres,int userID) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_GENRE); if (genres.size() == 0) return 0; long insertCount = 0; try { ContentValues[] valueList = new ContentValues[genres.size()]; int i = 0; for (Genre genre : genres) { if(genre.getGenreName().trim().equals("") || genre.getGenreName().equals("<unknown>")) continue; ContentValues values = new ContentValues(); values.put(COLUMN_GENRE_ARTIST_ID, genre.getArtistId()); values.put(COLUMN_GENRE_NAME, genre.getGenreName()); values.put(COLUMN_USER_ID, userID); valueList[i++] = values; } // returns ID insertCount = mContext.getContentResolver().bulkInsert(contentUri, valueList); } catch (Exception e) { e.printStackTrace(); } Log.d("Men123", "ADDED : " + genres.size()); return insertCount; } public void addArtist(Artist artist, User user) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_ARTISTS); ContentValues values = new ContentValues(); values.put(COLUMN_ID, artist.getID()); values.put(COLUMN_NAME, artist.getName()); values.put(COLUMN_AVATAR, artist.getAvatar()); values.put(COLUMN_LAST_UPDATE, artist.getLatUpdate()); values.put(COLUMN_GLOBAL_PLAYLIST, artist.getGlobalPlaylist());// int values.put(COLUMN_HAS_ARTIST, artist.isHasArtist());// bool values.put(COLUMN_IS_LIKED, artist.isLiked());// bool values.put(COLUMN_START_LIKE, artist.getStartLike());// long values.put(COLUMN_END_LIKE, artist.getEndLike());// long values.put(COLUMN_USER_ARTIST_ID, artist.getUserArtistId()); values.put(COLUMN_USER_ID, user.getID());// text Cursor cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "' AND " + COLUMN_ID + " ='" + artist.getID() + "' AND "+ COLUMN_USER_ARTIST_ID +"='"+artist.getUserArtistId()+"'", null, null); if (cursor.moveToFirst()) { int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID)); mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + id, null); } else { mContext.getContentResolver().insert(contentUri, values); } if (cursor != null) cursor.close(); } public void addPlaylistArtist(Artist artist, User user, int playlistId) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_ARTISTS); ContentValues values = new ContentValues(); values.put(COLUMN_ID, artist.getID()); values.put(COLUMN_NAME, artist.getName()); values.put(COLUMN_AVATAR, artist.getAvatar()); values.put(COLUMN_LAST_UPDATE, artist.getLatUpdate()); values.put(COLUMN_GLOBAL_PLAYLIST, artist.getGlobalPlaylist());// int values.put(COLUMN_HAS_ARTIST, artist.isHasArtist());// bool values.put(COLUMN_IS_LIKED, artist.isLiked());// bool values.put(COLUMN_START_LIKE, artist.getStartLike());// long values.put(COLUMN_END_LIKE, artist.getEndLike());// long values.put(COLUMN_USER_ID, user.getID());// text values.put(COLUMN_PLAYLIST_ID, playlistId);// text Cursor cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "' AND " + COLUMN_ID + " ='" + artist.getID() + "' AND "+ COLUMN_PLAYLIST_ID + " ='" + playlistId + "'", null, null); if (cursor.moveToFirst()) { int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID)); mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + id, null); } else { mContext.getContentResolver().insert(contentUri, values); } if (cursor != null) cursor.close(); } public void addPlaylistArtist(PlaylistArtist artist, User user) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_ARTISTS); ContentValues values = new ContentValues(); values.put(COLUMN_ID, artist.getID()); values.put(COLUMN_NAME, artist.getName()); values.put(COLUMN_AVATAR, artist.getAvatar()); values.put(COLUMN_LAST_UPDATE, artist.getLatUpdate()); values.put(COLUMN_GLOBAL_PLAYLIST, artist.getGlobalPlaylist());// int values.put(COLUMN_HAS_ARTIST, artist.isHasArtist());// bool values.put(COLUMN_IS_LIKED, artist.isLiked());// bool values.put(COLUMN_START_LIKE, artist.getStartLike());// long values.put(COLUMN_END_LIKE, artist.getEndLike());// long values.put(COLUMN_USER_ID, user.getID());// text values.put(COLUMN_PLAYLIST_ID, artist.getPlaylistId()); Cursor cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "' AND " + COLUMN_ID + " ='" + artist.getID() + "' AND " + COLUMN_PLAYLIST_ID +" ='"+artist.getPlaylistId()+"'", null, null); if (cursor.moveToFirst()) { int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID)); mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + id, null); } else { mContext.getContentResolver().insert(contentUri, values); } if (cursor != null) cursor.close(); } public void removeAllArtistAndSongByUser(User user) { // Remove artist Uri contentDeleteUri = Uri.withAppendedPath(CONTENT_URI, TABLE_ARTISTS); mContext.getContentResolver().delete(contentDeleteUri, COLUMN_USER_ID + "=?", new String[] { user.getID() + ""}); removeAllSongsByUserIDs(user.getID()); removeAllGenresByUserIDs(user.getID()); } public void removeAllSongsByUserIDs(int userID) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_SONGS); mContext.getContentResolver().delete(contentUri, COLUMN_USER_ID + "=?", new String[] { userID + "" }); } public void removeAllGenresByUserIDs(int userID) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_GENRE); mContext.getContentResolver().delete(contentUri, COLUMN_USER_ID + "=?", new String[] { userID + "" }); } public void removeAllArtistAndSongOfPlaylist(Playlist pl) { Uri contentDeleteUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_ARTISTS); mContext.getContentResolver().delete(contentDeleteUri, COLUMN_USER_ID + "=? AND "+COLUMN_PLAYLIST_ID+"=?", new String[] { pl.getUserId() + "" , pl.getID()+"" }); removeAllSongsOfPlaylist(pl); } public void removeAllSongsOfPlaylist(Playlist pl) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_AR_SONGS); mContext.getContentResolver().delete(contentUri, COLUMN_USER_ID + "=? AND "+COLUMN_PLAYLIST_ID+"=?", new String[] { pl.getUserId() + "" , pl.getID()+"" }); } public ArrayList<PlaylistArSong> getPlaylistSongs(User user, int playlistId) { ArrayList<PlaylistArSong> plSongs = new ArrayList<PlaylistArSong>(); Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_AR_SONGS); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "' AND "+COLUMN_PLAYLIST_ID+"='"+playlistId+"'", null, null); if (cursor.moveToFirst()) { do { PlaylistArSong song = new PlaylistArSong(); song.setID(cursor.getInt(cursor.getColumnIndex(COLUMN_ID))); song.setTitle(cursor.getString(cursor .getColumnIndex(COLUMN_TITLE))); song.setAlbum(cursor.getString(cursor .getColumnIndex(COLUMN_ALBUM))); song.setBuyLink(cursor.getString(cursor.getColumnIndex(COLUMN_BUY_LINK_US)),"us"); song.setBuyLink(cursor.getString(cursor.getColumnIndex(COLUMN_BUY_LINK_GB)),"gb"); song.set7DigigtalTrackId(cursor.getString(cursor.getColumnIndex(COLUMN_7DIGITAL_TRACK_ID_US)),"us"); song.set7DigigtalTrackId(cursor.getString(cursor.getColumnIndex(COLUMN_7DIGITAL_TRACK_ID_US)),"gb"); song.setHasSong(cursor.getInt(cursor.getColumnIndex(COLUMN_IS_HAS_SONG)) == 1); song.setLiked(cursor.getInt(cursor.getColumnIndex(COLUMN_IS_LIKED)) == 1); song.setStartLike(cursor.getLong(cursor.getColumnIndex(COLUMN_START_LIKE))); song.setEndLike(cursor.getLong(cursor.getColumnIndex(COLUMN_END_LIKE))); song.setPlaylistId(cursor.getInt(cursor.getColumnIndex(COLUMN_PLAYLIST_ID))); song.setAristitId(cursor.getInt(cursor .getColumnIndex(COLUMN_ARTIST_ID))); song.setUserId(cursor.getInt(cursor .getColumnIndex(COLUMN_USER_ID))); song.setIsSynch(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_SYNC))); plSongs.add(song); } while (cursor.moveToNext()); } cursor.close(); return plSongs; } public ArrayList<Playlist> getUserPlaylists(User user) { ArrayList<Playlist> playlists = null; Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "'", null, null); if (cursor.moveToFirst()) { playlists = new ArrayList<Playlist>(); do { Playlist pl = new Playlist(); pl.setDB_id(cursor.getInt(cursor.getColumnIndex(COLUMN_ID_AUTO))); pl.setID(cursor.getInt(cursor.getColumnIndex(COLUMN_ID))); pl.setUserId(cursor.getString(cursor.getColumnIndex(COLUMN_USER_ID))); pl.setPlaylistName(cursor.getString(cursor.getColumnIndex(COLUMN_NAME))); pl.setArtistAvatar(cursor.getString(cursor.getColumnIndex(COLUMN_ARTIST_AVATAR))); pl.setNumberOfTracks(cursor.getInt(cursor.getColumnIndex(COLUMN_TRACK_COUNT))); pl.setDateAdded(cursor.getLong(cursor.getColumnIndex(COLUMN_DATE_ADDED))); pl.setLastUpdated(cursor.getLong(cursor.getColumnIndex(COLUMN_LAST_UPDATE))); pl.setArtists(getPlaylistArtists(user, pl.getID())); playlists.add(pl); } while (cursor.moveToNext()); } cursor.close(); return playlists; } public ArrayList<PlaylistArtist> getPlaylistArtists(User user, int playlistId){ ArrayList<PlaylistArtist> artists = null; Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_ARTISTS); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "' AND "+COLUMN_PLAYLIST_ID + "='" + playlistId + "'", null, null); int totalSongs = 0; if (cursor.moveToFirst()) { artists = new ArrayList<PlaylistArtist>(); do { PlaylistArtist artist = new PlaylistArtist(); artist.setID(cursor.getInt(cursor.getColumnIndex(COLUMN_ID))); artist.setName(cursor.getString(cursor .getColumnIndex(COLUMN_NAME))); artist.setAvatar(cursor.getString(cursor .getColumnIndex(COLUMN_AVATAR))); artist.setLatUpdate(cursor.getLong(cursor .getColumnIndex(COLUMN_LAST_UPDATE))); artist.setGlobalPlaylist(cursor.getInt(cursor .getColumnIndex(COLUMN_GLOBAL_PLAYLIST))); artist.setHasArtist(cursor.getInt(cursor .getColumnIndex(COLUMN_HAS_ARTIST)) == 1); artist.setLiked(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_LIKED)) == 1); artist.setStartLike(cursor.getLong(cursor .getColumnIndex(COLUMN_START_LIKE))); artist.setEndLike(cursor.getLong(cursor .getColumnIndex(COLUMN_END_LIKE))); artist.setPlaylistId(cursor.getInt(cursor .getColumnIndex(COLUMN_PLAYLIST_ID))); ArrayList<PlaylistArSong> songs = getPlaylistArtSong(artist, user.getID()); if(songs != null) { totalSongs+=songs.size(); } artist.setSongs(songs); artists.add(artist); } while (cursor.moveToNext()); } cursor.close(); return artists; } public int updatePlaylistSong(PlaylistArSong song) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_AR_SONGS); ContentValues values = new ContentValues(); values.put(COLUMN_ID, song.getID()); values.put(COLUMN_TITLE, song.getTitle()); values.put(COLUMN_ALBUM, song.getAlbum()); values.put(COLUMN_BUY_LINK_US, song.getBuyLink("us")); values.put(COLUMN_7DIGITAL_TRACK_ID_US, song.get7DigigtalTrackId("us")); values.put(COLUMN_BUY_LINK_GB, song.getBuyLink("gb")); values.put(COLUMN_7DIGITAL_TRACK_ID_GB, song.get7DigigtalTrackId("gb")); values.put(COLUMN_IS_HAS_SONG, song.isHasSong()); values.put(COLUMN_IS_LIKED, song.isLiked()); values.put(COLUMN_START_LIKE, song.getStartLike()); values.put(COLUMN_END_LIKE, song.getEndLike()); values.put(COLUMN_ARTIST_ID, song.getAristitId()); values.put(COLUMN_USER_ID, song.getUserId()); values.put(COLUMN_PLAYLIST_ID, song.getPlaylistId()); values.put(COLUMN_IS_SYNC, song.getIsSynch()); int result = mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + song.getID() +" AND "+COLUMN_USER_ID + "= "+song.getUserId() +" AND "+COLUMN_PLAYLIST_ID +"="+song.getPlaylistId(), null); return result; } public int updatePlaylistById (int id, Playlist playlist) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST); ContentValues values = new ContentValues(); values.put(COLUMN_USER_ID, playlist.getUserId()); values.put(COLUMN_ID, playlist.getID()); values.put(COLUMN_NAME, playlist.getPlaylistName()); values.put(COLUMN_ARTIST_AVATAR, playlist.getArtistAvatar()); values.put(COLUMN_TRACK_COUNT, playlist.getNumberOfTracks()); values.put(COLUMN_DATE_ADDED, playlist.getDateAdded()); values.put(COLUMN_LAST_UPDATE, playlist.getLastUpdated()); int result = mContext.getContentResolver().update(contentUri, values, COLUMN_ID_AUTO + "=" + id, null); return result; } public ArrayList<Artist> getArtist(User user) { ArrayList<Artist> artists = new ArrayList<Artist>(); Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_ARTISTS); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_USER_ID + "='" + user.getID() + "'", null, null); int totalSongs = 0; if (cursor.moveToFirst()) { do { Artist artist = new Artist(); artist.setID(cursor.getInt(cursor.getColumnIndex(COLUMN_ID))); artist.setName(cursor.getString(cursor .getColumnIndex(COLUMN_NAME))); artist.setAvatar(cursor.getString(cursor .getColumnIndex(COLUMN_AVATAR))); artist.setLatUpdate(cursor.getLong(cursor .getColumnIndex(COLUMN_LAST_UPDATE))); artist.setGlobalPlaylist(cursor.getInt(cursor .getColumnIndex(COLUMN_GLOBAL_PLAYLIST))); artist.setHasArtist(cursor.getInt(cursor .getColumnIndex(COLUMN_HAS_ARTIST)) == 1); artist.setLiked(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_LIKED)) == 1); artist.setStartLike(cursor.getLong(cursor .getColumnIndex(COLUMN_START_LIKE))); artist.setEndLike(cursor.getLong(cursor .getColumnIndex(COLUMN_END_LIKE))); artist.setUserArtistId(cursor.getInt(cursor .getColumnIndex(COLUMN_USER_ARTIST_ID))); ArrayList<Song> songs = getSong(artist, user.getID()); //Log.i("check","Song size: " + songs.size()); if(songs.size() > 0){ ArrayList<Genre> genres = getGenre(artist.getID(),artist); artist.setGenres(genres); } totalSongs+=songs.size(); artist.setSongs(songs); artists.add(artist); } while (cursor.moveToNext()); } cursor.close(); Log.i("TotalTrackOfArtists", totalSongs+""); Log.i("check", "TotalTrackOfArtists://"+totalSongs+" -- user ://"+user.getFullName()); return artists; } public String addSongToPlaylist(PlaylistArSong song) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_AR_SONGS); ContentValues values = new ContentValues(); values.put(COLUMN_ID, song.getID()); values.put(COLUMN_TITLE, song.getTitle()); values.put(COLUMN_ALBUM, song.getAlbum()); values.put(COLUMN_BUY_LINK_US, song.getBuyLink("us")); values.put(COLUMN_7DIGITAL_TRACK_ID_US, song.get7DigigtalTrackId("us")); values.put(COLUMN_BUY_LINK_GB, song.getBuyLink("gb")); values.put(COLUMN_7DIGITAL_TRACK_ID_GB, song.get7DigigtalTrackId("gb")); values.put(COLUMN_IS_HAS_SONG, song.isHasSong()); values.put(COLUMN_IS_LIKED, song.isLiked()); values.put(COLUMN_START_LIKE, song.getStartLike()); values.put(COLUMN_END_LIKE, song.getEndLike()); values.put(COLUMN_ARTIST_ID, song.getAristitId()); values.put(COLUMN_USER_ID, song.getUserId()); values.put(COLUMN_PLAYLIST_ID, song.getPlaylistId()); values.put(COLUMN_IS_SYNC, song.getIsSynch()); Cursor cursor = null; if(song.getPlaylistId() != -1) { //Playlist is synched with server cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_ID + "='" + song.getID() + "' AND " + COLUMN_USER_ID+ "='" + song.getUserId() + "' AND " + COLUMN_PLAYLIST_ID+ "='"+song.getPlaylistId() +"' AND " + COLUMN_ARTIST_ID+ "='"+song.getAristitId()+"'" , null, null); } else { //Playlist is not synched with server do insertion on the basis of db_id cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_ID + "='" + song.getID() + "' AND " + COLUMN_USER_ID+ "='" + song.getUserId() +"'" , null, null); } String output = ""; if (cursor.moveToFirst()) { output = "Song Already Added to same playlist."; } else { Uri result = mContext.getContentResolver().insert(contentUri, values); long newID= ContentUris.parseId(result); if(newID > 0) output = ""; else output = "Something went wrong."; } if (cursor != null) { cursor.close(); } return output; } public long AddSongArrs(ArrayList<Song> songs) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_SONGS); if (songs.size() == 0) return 0; long insertCount = 0; try { ContentValues[] valueList = new ContentValues[songs.size()]; int i = 0; for (Song song : songs) { ContentValues values = new ContentValues(); values.put(COLUMN_ID, song.getID()); values.put(COLUMN_TITLE, song.getTitle()); values.put(COLUMN_ALBUM, song.getAlbum()); values.put(COLUMN_BUY_LINK_US, song.getBuyLink("us")); values.put(COLUMN_7DIGITAL_TRACK_ID_US, song.get7DigigtalTrackId("us")); values.put(COLUMN_BUY_LINK_GB, song.getBuyLink("gb")); values.put(COLUMN_7DIGITAL_TRACK_ID_GB, song.get7DigigtalTrackId("gb")); values.put(COLUMN_IS_HAS_SONG, song.isHasSong()); values.put(COLUMN_IS_LIKED, song.isLiked()); values.put(COLUMN_START_LIKE, song.getStartLike()); values.put(COLUMN_END_LIKE, song.getEndLike()); values.put(COLUMN_ARTIST_ID, song.getAristitId()); values.put(COLUMN_USER_ID, song.getUserId()); valueList[i++] = values; } // returns ID insertCount = mContext.getContentResolver().bulkInsert(contentUri, valueList); } catch (Exception e) { e.printStackTrace(); } Log.d("Men123", "ADDED : " + songs.size()); return insertCount; } public int UpdateSongArrs(Song song) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_SONGS); ContentValues values = new ContentValues(); values.put(COLUMN_ID, song.getID()); values.put(COLUMN_TITLE, song.getTitle()); values.put(COLUMN_ALBUM, song.getAlbum()); values.put(COLUMN_BUY_LINK_US, song.getBuyLink("us")); values.put(COLUMN_7DIGITAL_TRACK_ID_US, song.get7DigigtalTrackId("us")); values.put(COLUMN_BUY_LINK_GB, song.getBuyLink("gb")); values.put(COLUMN_7DIGITAL_TRACK_ID_GB, song.get7DigigtalTrackId("gb")); values.put(COLUMN_IS_HAS_SONG, song.isHasSong()); values.put(COLUMN_IS_LIKED, song.isLiked()); values.put(COLUMN_START_LIKE, song.getStartLike()); values.put(COLUMN_END_LIKE, song.getEndLike()); values.put(COLUMN_ARTIST_ID, song.getAristitId()); values.put(COLUMN_USER_ID, song.getUserId()); int result = mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + song.getID() +" AND "+COLUMN_USER_ID + "= "+song.getUserId() , null); return result; } public long AddArtistsArrs(ArrayList<Artist> artists, User user) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_ARTISTS); if (artists.size() == 0) return 0; long insertCount = 0; try { ContentValues[] valueList = new ContentValues[artists.size()]; int i = 0; for (Artist artist : artists) { ContentValues values = new ContentValues(); values.put(COLUMN_ID, artist.getID()); values.put(COLUMN_NAME, artist.getName()); values.put(COLUMN_AVATAR, artist.getAvatar()); values.put(COLUMN_LAST_UPDATE, artist.getLatUpdate()); values.put(COLUMN_GLOBAL_PLAYLIST, artist.getGlobalPlaylist());// int values.put(COLUMN_HAS_ARTIST, artist.isHasArtist());// bool values.put(COLUMN_IS_LIKED, artist.isLiked());// bool values.put(COLUMN_START_LIKE, artist.getStartLike());// long values.put(COLUMN_END_LIKE, artist.getEndLike());// long values.put(COLUMN_USER_ID, user.getID());// text values.put(COLUMN_USER_ARTIST_ID, artist.getUserArtistId()); valueList[i++] = values; } // returns ID insertCount = mContext.getContentResolver().bulkInsert(contentUri, valueList); } catch (Exception e) { e.printStackTrace(); } Log.d("Men123", "ADDED : " + insertCount); return insertCount; } public long AddPLArtSongArrs(ArrayList<PlaylistArSong> songs) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_AR_SONGS); if (songs.size() == 0) return 0; long insertCount = 0; try { ContentValues[] valueList = new ContentValues[songs.size()]; int i = 0; for (PlaylistArSong song : songs) { ContentValues values = new ContentValues(); values.put(COLUMN_ID, song.getID()); values.put(COLUMN_TITLE, song.getTitle()); values.put(COLUMN_ALBUM, song.getAlbum()); values.put(COLUMN_BUY_LINK_US, song.getBuyLink("us")); values.put(COLUMN_7DIGITAL_TRACK_ID_US, song.get7DigigtalTrackId("us")); values.put(COLUMN_BUY_LINK_GB, song.getBuyLink("gb")); values.put(COLUMN_7DIGITAL_TRACK_ID_GB, song.get7DigigtalTrackId("gb")); values.put(COLUMN_IS_HAS_SONG, song.isHasSong()); values.put(COLUMN_IS_LIKED, song.isLiked()); values.put(COLUMN_START_LIKE, song.getStartLike()); values.put(COLUMN_END_LIKE, song.getEndLike()); values.put(COLUMN_ARTIST_ID, song.getAristitId()); values.put(COLUMN_USER_ID, song.getUserId()); values.put(COLUMN_PLAYLIST_ID, song.getPlaylistId()); values.put(COLUMN_IS_SYNC, song.getIsSynch()); valueList[i++] = values; } insertCount = mContext.getContentResolver().bulkInsert(contentUri, valueList); } catch (Exception e) { e.printStackTrace(); } Log.d("Men123", "ADDED : " + songs.size()); return insertCount; } public ArrayList<PlaylistArSong> getPlaylistArtSong(PlaylistArtist artist, int userId) { ArrayList<PlaylistArSong> songs = null; Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_AR_SONGS); Cursor cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_ARTIST_ID + "='" + artist.getID() + "' AND " + COLUMN_USER_ID + "='" + userId + "' AND " +COLUMN_PLAYLIST_ID+"='"+ artist.getPlaylistId()+"'", null, null); if (cursor.moveToFirst()) { songs = new ArrayList<PlaylistArSong>(); do { PlaylistArSong song = new PlaylistArSong(); song.setID(cursor.getInt(cursor.getColumnIndex(COLUMN_ID))); song.setTitle(cursor.getString(cursor .getColumnIndex(COLUMN_TITLE))); song.setAlbum(cursor.getString(cursor .getColumnIndex(COLUMN_ALBUM))); song.setBuyLink(cursor.getString(cursor .getColumnIndex(COLUMN_BUY_LINK_US)),"us"); song.set7DigigtalTrackId(cursor.getString(cursor .getColumnIndex(COLUMN_7DIGITAL_TRACK_ID_US)),"us"); song.setBuyLink(cursor.getString(cursor.getColumnIndex(COLUMN_BUY_LINK_GB)),"gb"); song.set7DigigtalTrackId(cursor.getString(cursor.getColumnIndex(COLUMN_7DIGITAL_TRACK_ID_GB)),"gb"); song.setHasSong(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_HAS_SONG)) == 1); song.setLiked(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_LIKED)) == 1); song.setStartLike(cursor.getLong(cursor .getColumnIndex(COLUMN_START_LIKE))); song.setEndLike(cursor.getLong(cursor .getColumnIndex(COLUMN_END_LIKE))); song.setAristitId(cursor.getInt(cursor .getColumnIndex(COLUMN_ARTIST_ID))); song.setIsSynch(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_SYNC))); song.setPlaylistId(cursor.getInt(cursor .getColumnIndex(COLUMN_PLAYLIST_ID))); song.setUserId(cursor.getInt(cursor .getColumnIndex(COLUMN_USER_ID))); songs.add(song); } while (cursor.moveToNext()); } cursor.close(); return songs; } public ArrayList<Genre> getGenre(int artistID,Artist artist) { ArrayList<Genre> genres = new ArrayList<Genre>(); ArrayList<String> genreNames = new ArrayList<String>(); Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_GENRE); Cursor cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_GENRE_ARTIST_ID + "='" + artistID + "'", null, null); if (cursor.moveToFirst()) { do { Genre genre = new Genre(); genre.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID_AUTO))); genre.setArtistId(cursor.getInt(cursor.getColumnIndex(COLUMN_GENRE_ARTIST_ID))); String genName = cursor.getString(cursor.getColumnIndex(COLUMN_GENRE_NAME)); if(!genreNames.toString().toLowerCase().contains(genName.toLowerCase())) genreNames.add(Character.toUpperCase(genName.trim().charAt(0)) + genName.trim().substring(1)); genre.setGenreName(genName); genreNames.add(genName); genres.add(genre); } while (cursor.moveToNext()); } cursor.close(); artist.setAllGenreNames(genreNames); return genres; } public ArrayList<Song> getSong(Artist artist, int userID) { ArrayList<Song> songs = new ArrayList<Song>(); Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_SONGS); Cursor cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_ARTIST_ID + "='" + artist.getID() + "' AND " + COLUMN_USER_ID + "='" + userID + "'", null, null); if (cursor.moveToFirst()) { do { Song song = new Song(); song.setID(cursor.getInt(cursor.getColumnIndex(COLUMN_ID))); song.setTitle(cursor.getString(cursor .getColumnIndex(COLUMN_TITLE))); song.setAlbum(cursor.getString(cursor .getColumnIndex(COLUMN_ALBUM))); song.setBuyLink(cursor.getString(cursor .getColumnIndex(COLUMN_BUY_LINK_US)),"us"); song.set7DigigtalTrackId(cursor.getString(cursor .getColumnIndex(COLUMN_7DIGITAL_TRACK_ID_US)),"us"); song.setBuyLink(cursor.getString(cursor .getColumnIndex(COLUMN_BUY_LINK_GB)),"gb"); song.set7DigigtalTrackId(cursor.getString(cursor .getColumnIndex(COLUMN_7DIGITAL_TRACK_ID_GB)),"gb"); song.setHasSong(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_HAS_SONG)) == 1); song.setLiked(cursor.getInt(cursor .getColumnIndex(COLUMN_IS_LIKED)) == 1); song.setStartLike(cursor.getLong(cursor .getColumnIndex(COLUMN_START_LIKE))); song.setEndLike(cursor.getLong(cursor .getColumnIndex(COLUMN_END_LIKE))); songs.add(song); } while (cursor.moveToNext()); } cursor.close(); return songs; } public void addFriend(Friend f) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_FRIENDS); ContentValues values = new ContentValues(); values.put(COLUMN_FBID, f.getFbid()); values.put(COLUMN_NAME, f.getName()); values.put(COLUMN_AVATAR, f.getAvatar()); Cursor cursor = mContext.getContentResolver().query( contentUri, null, COLUMN_FBID + "='" + f.getFbid() + "' AND " + COLUMN_FBID_F + "='" + f.getFbidF() + "'", null, null); if (cursor.moveToFirst()) { int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID)); mContext.getContentResolver().update(contentUri, values, COLUMN_ID + "=" + id, null); } else { mContext.getContentResolver().insert(contentUri, values); } if (cursor != null) cursor.close(); } public boolean updateFriend(Friend f) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_FRIENDS); ContentValues values = new ContentValues(); values.put(COLUMN_FBID, f.getFbid()); values.put(COLUMN_NAME, f.getName()); values.put(COLUMN_AVATAR, f.getAvatar()); return mContext.getContentResolver().update(contentUri, values, COLUMN_ID_AUTO + "=" + f.getIdAuto(), null) > 0; } public void deleteUserPlaylist(User user, int playlistId) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST); int deletedRows = mContext.getContentResolver().delete(contentUri, COLUMN_USER_ID + "= ? AND "+COLUMN_ID +"= ?", new String[] { user.getID() + "", playlistId +""}); if(deletedRows > 0) { contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_ARTISTS); deletedRows = mContext.getContentResolver().delete(contentUri, COLUMN_USER_ID + "= ? AND "+COLUMN_PLAYLIST_ID +"= ?", new String[] { user.getID() + "", playlistId +""}); if(deletedRows > 0) { contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST_AR_SONGS); deletedRows = mContext.getContentResolver().delete(contentUri, COLUMN_USER_ID + "= ? AND "+COLUMN_PLAYLIST_ID +"= ?", new String[] { user.getID() + "", playlistId +""}); } } } public int deleteUserPlaylists(User user) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_PLAYLIST); return mContext.getContentResolver().delete(contentUri, COLUMN_USER_ID + "=?", new String[] { user.getID() + "" }); } public int deleteFriend(long id) { Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_FRIENDS); return mContext.getContentResolver().delete(contentUri, COLUMN_ID_AUTO + "=?", new String[] { id + "" }); } public ArrayList<Friend> getFriends(String fbid) { ArrayList<Friend> friends = new ArrayList<Friend>(); Uri contentUri = Uri.withAppendedPath(CONTENT_URI, TABLE_FRIENDS); Cursor cursor = mContext.getContentResolver().query(contentUri, null, COLUMN_FBID + "='" + fbid + "'", null, null); if (cursor.moveToFirst()) { do { Friend f = new Friend(cursor.getInt(cursor .getColumnIndex(COLUMN_ID_AUTO)), cursor.getString(cursor.getColumnIndex(COLUMN_FBID)), cursor.getString(cursor.getColumnIndex(COLUMN_NAME)), cursor.getString(cursor.getColumnIndex(COLUMN_FBID_F)), cursor.getBlob(cursor.getColumnIndex(COLUMN_AVATAR))); friends.add(f); } while (cursor.moveToNext()); } if (cursor != null) cursor.close(); return friends; } /* * // create a song public long addFriend(LocalSong s) { Uri contentUri = * Uri.withAppendedPath(CONTENT_URI, TABLE_SONGS); * * long idAdded=0; ContentValues values = new ContentValues(); * values.put(COLUMN_TITLE, s.getTitle()); values.put(COLUMN_TITLE_EDITED, * s.getTitle()); values.put(COLUMN_ARTIST, s.getArtist()); * values.put(COLUMN_ARTIST_EDITED, s.getArtist()); values.put(COLUMN_TYPE, * s.getType()); values.put(COLUMN_LAST_LISTEN, s.getLastListen()); Uri * resultUri=mContext.getContentResolver().insert(contentUri, values); * idAdded=Long.parseLong(resultUri.getLastPathSegment()); return idAdded; } * * public boolean updateSong(LocalSong s) { Uri contentUri = * Uri.withAppendedPath(CONTENT_URI, TABLE_SONGS); * * ContentValues values = new ContentValues(); values.put(COLUMN_TITLE, * s.getTitle()); values.put(COLUMN_TITLE_EDITED, s.getTitle()); * values.put(COLUMN_ARTIST, s.getArtist()); * values.put(COLUMN_ARTIST_EDITED, s.getArtist()); * values.put(COLUMN_LAST_LISTEN, s.getLastListen()); return * mContext.getContentResolver().update(contentUri, * values,COLUMN_ID+"="+s.getID(),null)>0; } * * public int deleteSong(long id){ Uri contentUri = * Uri.withAppendedPath(CONTENT_URI, TABLE_SONGS); return * mContext.getContentResolver().delete(contentUri, COLUMN_ID+"=?", new * String[]{id+""}); } */ }