Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vadim Semenov, 2539 #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<provider
android:name=".database.Provider"
android:authorities=".database.Provider"
android:exported="false" />

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/assets/music.txt

Large diffs are not rendered by default.

126 changes: 123 additions & 3 deletions app/src/main/java/ru/ifmo/md/exam1/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
package ru.ifmo.md.exam1;

import android.support.v7.app.ActionBarActivity;
import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import ru.ifmo.md.exam1.database.Provider;
import ru.ifmo.md.exam1.database.genre.GenreContract;
import ru.ifmo.md.exam1.database.playlist.PlaylistContract;
import ru.ifmo.md.exam1.database.song.SongContract;

public class MainActivity extends ActionBarActivity {

public class MainActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private ListView listView;
private SimpleCursorAdapter playlistAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
listView = (ListView) findViewById(R.id.list_view);

String[] fromColumns = {PlaylistContract.Playlist.NAME};
int[] toViews = {R.id.playlist_display_name};

playlistAdapter = new SimpleCursorAdapter(this, R.id.list_item, null, fromColumns, toViews, 0);
listView.setAdapter(playlistAdapter);
getLoaderManager().initLoader(0, null, this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand All @@ -36,4 +65,95 @@ public boolean onOptionsItemSelected(MenuItem item) {

return super.onOptionsItemSelected(item);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, Provider.PLAYLIST_CONTENT_URI,
PlaylistContract.Playlist.ALL_COLUMNS, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (data == null) {
data = parse();
}
playlistAdapter.swapCursor(data);
}

private Cursor parse() {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(getAssets().open("music.txt")));
} catch (IOException e) {
e.printStackTrace();
}
String jsonLine = "";
try {
String currentLine = in.readLine();
while (currentLine != null) {
jsonLine += currentLine;
currentLine = in.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}

JSONArray allSongs;
StringBuilder songs = new StringBuilder("[");
try {
allSongs = new JSONArray(jsonLine);
for (int i = 0; i < allSongs.length(); ++i) {
JSONObject song = allSongs.getJSONObject(i);
songs.append(song.getString("name"));
addSongToDatabase(song);
}
} catch (JSONException e) {
e.printStackTrace();
}

ContentValues contentValues = new ContentValues();
contentValues.put(PlaylistContract.Playlist.ID, "0");
contentValues.put(PlaylistContract.Playlist.NAME, "All songs");
contentValues.put(PlaylistContract.Playlist.SONGS, songs.toString());
getContentResolver().insert(Provider.PLAYLIST_CONTENT_URI, contentValues);

return getContentResolver().query(Provider.PLAYLIST_CONTENT_URI, PlaylistContract.Playlist.ALL_COLUMNS, null, null, null);
}

private int songID = 1;
private int genreID = 1;
private void addSongToDatabase(JSONObject song) throws JSONException {
String songName = song.getString("name");
String songUrl = song.getString("url");
String songDurationString = song.getString("duration");
long songPopularity = song.getLong("popularity");
JSONArray genres = song.getJSONArray("genres");
long songYear = song.getLong("year");

ContentValues contentValues = new ContentValues();
contentValues.put(SongContract.Song.ID, songID++);
contentValues.put(SongContract.Song.NAME, songName);
contentValues.put(SongContract.Song.URL, songUrl);
contentValues.put(SongContract.Song.DURATION, songDurationString);
contentValues.put(SongContract.Song.DURATION, songPopularity);
contentValues.put(SongContract.Song.GENRES, genres.toString());
contentValues.put(SongContract.Song.YEAR, songYear);
contentValues.put(SongContract.Song.VALID_STATE, 1);
getContentResolver().insert(Provider.SONG_CONTENT_URI, contentValues);

for (int j = 0; j < genres.length(); ++j) {
String genre = genres.getString(j);

contentValues.clear();
contentValues.put(GenreContract.Genre.ID, genreID++);
contentValues.put(GenreContract.Genre.NAME, genre);
contentValues.put(GenreContract.Genre.VALID_STATE, 1);
getContentResolver().update(Provider.GENRE_CONTENT_URI, contentValues, null, null);
}
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
playlistAdapter.swapCursor(null);
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/ru/ifmo/md/exam1/PlaylistAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.ifmo.md.exam1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
* Created by vadim on 23/01/15.
*/
public class PlaylistAdapter extends ArrayAdapter<String> {
public PlaylistAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View rowView = inflater.inflate(R.layout.playlist_item_layout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.playlist_display_name);
textView.setText(getItem(position));
return rowView;
}
}
169 changes: 169 additions & 0 deletions app/src/main/java/ru/ifmo/md/exam1/database/Provider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package ru.ifmo.md.exam1.database;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

import ru.ifmo.md.exam1.database.genre.GenreDatabaseHelper;
import ru.ifmo.md.exam1.database.playlist.PlaylistDatabaseHelper;
import ru.ifmo.md.exam1.database.song.SongDatabaseHelper;

/**
* Created by vadim on 23/01/15.
*/
public class Provider extends ContentProvider {
public static final String TAG = Provider.class.getSimpleName();

public static final String AUTHORITY = Provider.class.getCanonicalName();

public static final Uri PLAYLIST_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PlaylistDatabaseHelper.PLAYLIST_TABLE);
public static final int URI_PLAYLIST_ID = 1;

public static final Uri GENRE_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + GenreDatabaseHelper.GENRE_TABLE);
public static final int URI_GENRE_ID = 2;

public static final Uri SONG_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + SongDatabaseHelper.SONG_TABLE);
public static final int URI_SONG_ID = 3;

private static final String PLAYLIST_CONTENT_TYPE = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + PlaylistDatabaseHelper.PLAYLIST_TABLE;
private static final String GENRE_CONTENT_TYPE = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + GenreDatabaseHelper.GENRE_TABLE;
private static final String SONG_CONTENT_TYPE = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + SongDatabaseHelper.SONG_TABLE;

private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static {
URI_MATCHER.addURI(AUTHORITY, PlaylistDatabaseHelper.PLAYLIST_TABLE, URI_PLAYLIST_ID);
URI_MATCHER.addURI(AUTHORITY, GenreDatabaseHelper.GENRE_TABLE, URI_GENRE_ID);
URI_MATCHER.addURI(AUTHORITY, SongDatabaseHelper.SONG_TABLE, URI_SONG_ID);
}

private PlaylistDatabaseHelper playlistDBHelper;
private GenreDatabaseHelper genreDBHelper;
private SongDatabaseHelper songDBHelper;

@Override
public boolean onCreate() {
playlistDBHelper = new PlaylistDatabaseHelper(getContext());
genreDBHelper = new GenreDatabaseHelper(getContext());
songDBHelper = new SongDatabaseHelper(getContext());
return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
int match = URI_MATCHER.match(uri);
Cursor cursor;
switch (match) {
case URI_PLAYLIST_ID:
cursor = playlistDBHelper.getReadableDatabase().query(PlaylistDatabaseHelper.PLAYLIST_TABLE,
projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), PLAYLIST_CONTENT_URI);
break;
case URI_GENRE_ID:
cursor = genreDBHelper.getReadableDatabase().query(GenreDatabaseHelper.GENRE_TABLE,
projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), GENRE_CONTENT_URI);
break;
case URI_SONG_ID:
cursor = songDBHelper.getReadableDatabase().query(SongDatabaseHelper.SONG_TABLE,
projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), SONG_CONTENT_URI);
default:
throw new IllegalArgumentException("No such URI found: " + match);
}
return cursor;
}

@Override
public String getType(Uri uri) {
int match = URI_MATCHER.match(uri);
switch (match) {
case URI_PLAYLIST_ID:
return PLAYLIST_CONTENT_TYPE;
case URI_GENRE_ID:
return GENRE_CONTENT_TYPE;
case URI_SONG_ID:
return SONG_CONTENT_TYPE;
default:
throw new IllegalArgumentException("No such URI found: " + match);
}
}

@Override
public Uri insert(Uri uri, ContentValues values) {
Uri resultURI;
int match = URI_MATCHER.match(uri);
long rowID;
switch (match) {
case URI_PLAYLIST_ID:
rowID = playlistDBHelper.getWritableDatabase().insert(
PlaylistDatabaseHelper.PLAYLIST_TABLE, null, values);
resultURI = ContentUris.withAppendedId(PLAYLIST_CONTENT_URI, rowID);
break;
case URI_GENRE_ID:
rowID = genreDBHelper.getWritableDatabase().insert(
GenreDatabaseHelper.GENRE_TABLE, null, values);
resultURI = ContentUris.withAppendedId(GENRE_CONTENT_URI, rowID);
break;
case URI_SONG_ID:
rowID = songDBHelper.getWritableDatabase().insert(
SongDatabaseHelper.SONG_TABLE, null, values);
resultURI = ContentUris.withAppendedId(SONG_CONTENT_URI, rowID);
break;
default:
throw new IllegalArgumentException("No such URI found: " + match);
}
getContext().getContentResolver().notifyChange(resultURI, null);
return resultURI;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int result;
int match = URI_MATCHER.match(uri);
switch (match) {
case URI_PLAYLIST_ID:
result = playlistDBHelper.getWritableDatabase().delete(
PlaylistDatabaseHelper.PLAYLIST_TABLE, selection, selectionArgs);
break;
case URI_GENRE_ID:
result = genreDBHelper.getWritableDatabase().delete(
GenreDatabaseHelper.GENRE_TABLE, selection, selectionArgs);
break;
case URI_SONG_ID:
result = songDBHelper.getWritableDatabase().delete(
SongDatabaseHelper.SONG_TABLE, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("No such URI found: " + match);
}
getContext().getContentResolver().notifyChange(uri, null);
return result;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int result;
int match = URI_MATCHER.match(uri);
switch (match) {
case URI_PLAYLIST_ID:
result = playlistDBHelper.getWritableDatabase().update(
PlaylistDatabaseHelper.PLAYLIST_TABLE, values, selection, selectionArgs);
break;
case URI_GENRE_ID:
result = genreDBHelper.getWritableDatabase().update(
GenreDatabaseHelper.GENRE_TABLE, values, selection, selectionArgs);
break;
case URI_SONG_ID:
result = songDBHelper.getWritableDatabase().update(
SongDatabaseHelper.SONG_TABLE, values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("No such URI found: " + match);
}
getContext().getContentResolver().notifyChange(uri, null);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.ifmo.md.exam1.database.genre;

import android.net.Uri;
import android.provider.BaseColumns;

import ru.ifmo.md.exam1.database.song.SongContract;

/**
* Created by vadim on 23/01/15.
*/
public class GenreContract {
public interface SongColumns {
public static final String ID = "genre_id";
public static final String NAME = "genre_name";
public static final String VALID_STATE = "genre_valid_state";
}

public static final String AUTHORITY = SongContract.class.getCanonicalName();

public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY);

public static final String PATH = "genres";

public static final class Genre implements SongColumns, BaseColumns {
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH).build();

public static final String[] ALL_COLUMNS = {
Genre._ID,
Genre.ID,
Genre.NAME,
Genre.VALID_STATE
};

public static Uri buildPhotoUri(String songId) {
return CONTENT_URI.buildUpon().appendPath(songId).build();
}
}
}
Loading