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

Борзенко Антон, 2537 #7

Open
wants to merge 3 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
Binary file added app-debug.apk
Binary file not shown.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AddPlayListActivity"
android:label="@string/title_activity_main_activity2" >
</activity>
</application>

</manifest>
52 changes: 52 additions & 0 deletions app/src/main/java/ru/ifmo/md/exam1/AddPlayListActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.ifmo.md.exam1;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
// {"name":"Mapei – Don't Wait",
// "url":"https://cs1-46v4.vk-cdn.net/p23/53f922c0d2cec9.mp3",
// "duration":"3:35",
// "popularity":76,
// "genres":["Blues","Alternative","Soul"],
// "year":2009}

public class AddPlayListActivity extends ActionBarActivity {
ArrayList<Track> items;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);

Intent intent = getIntent();
items = intent.getParcelableArrayListExtra("list");
pos = intent.getIntExtra("pos", 0);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
147 changes: 140 additions & 7 deletions app/src/main/java/ru/ifmo/md/exam1/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,172 @@
package ru.ifmo.md.exam1;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.DataSetObserver;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.JsonReader;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

private static final String MY_TAG = "EXAM2_MAIN";
ListView lv;
Adapter tracklistAdapter, playlistAdapter;
private final ArrayList<Track> trackList = new ArrayList<Track>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

loadMusic();
Log.d(MY_TAG, "there is " + trackList.size() + " loaded tracks");
if (!trackList.isEmpty()) {
Log.d(MY_TAG, "First track is '" + trackList.get(0).toString() + "'");
}
lv = (ListView)findViewById(R.id.listView);
tracklistAdapter = new TrackListAdapter(this, android.R.layout.simple_list_item_1, trackList);
lv.setAdapter((TrackListAdapter)tracklistAdapter);
// lv.deferNotifyDataSetChanged();
lv.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(showFullInfo(trackList.get(position)));
builder.setItems(new String[] {"Delete track", "Add to playlist", "Download"}, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
deleteTrack(position);
}
switch (which) {
case 0:
deleteTrack(position);
break;
case 1:
addToList(position);
break;
case 2:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(trackList.get(position).url));
startActivity(browserIntent);
}
}
});
builder.create().show();
}

private String showFullInfo(Track track) {
return track.name + " (" + track.duration + ") - " + track.year;
}
}
);
}

private void addToList(int position) {

}

private void deleteTrack(int position) {
String url = trackList.get(position).url;
trackList.remove(position);
((TrackListAdapter)tracklistAdapter).notifyDataSetChanged();
}

// {"name":"Mapei – Don't Wait",
// "url":"https://cs1-46v4.vk-cdn.net/p23/53f922c0d2cec9.mp3",
// "duration":"3:35",
// "popularity":76,
// "genres":["Blues","Alternative","Soul"],
// "year":2009}
private String[] getStringArray(JsonReader reader) throws IOException {
ArrayList<String> result = new ArrayList<String>();

reader.beginArray();
while (reader.hasNext()) {
result.add(reader.nextString());
}
reader.endArray();

return result.toArray(new String[result.size()]);
}
private Track readTrack(JsonReader reader) throws IOException {
Track result = new Track();

reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("name")) {
result.name = reader.nextString();
} else if (name.equals("url")) {
result.url = reader.nextString();
} else if (name.equals("duration")) {
result.duration = reader.nextString();
} else if (name.equals("popularity")) {
result.popularity = reader.nextInt();
} else if (name.equals("year")) {
result.year = reader.nextInt();
} else if (name.equals("genres")) {
result.genres = getStringArray(reader);
}
}
reader.endObject();

return result;
}
private void loadMusic() {
try {
JsonReader reader = new JsonReader(
new InputStreamReader(
getResources().openRawResource(R.raw.music)
)
);
reader.beginArray();
while (reader.hasNext()) {
trackList.add(readTrack(reader));
}
reader.endArray();
reader.close();
} catch (Exception e) {
Log.e(MY_TAG, e.getMessage());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void addPlaylist(MenuItem item) {
}

public void showPlaylists(MenuItem item) {
}
}
47 changes: 47 additions & 0 deletions app/src/main/java/ru/ifmo/md/exam1/Playlist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ru.ifmo.md.exam1;

import android.os.Parcel;
import android.os.Parcelable;

// {"name":"Mapei – Don't Wait",
// "url":"https://cs1-46v4.vk-cdn.net/p23/53f922c0d2cec9.mp3",
// "duration":"3:35",
// "popularity":76,
// "genres":["Blues","Alternative","Soul"],
// "year":2009}

public class Playlist implements Parcelable {
public String name = "New list";
public String[] list = new String[0];

public Playlist() {}

public Playlist(String name, String[] list) {
this.name = name;
this.list = list;
}

private Playlist(Parcel in) {
name = in.readString();
int length = in.readInt();
in.readStringArray(list = new String[length]);
}

public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int i) {
out.writeString(name);
out.writeInt(list.length);
out.writeStringArray(list);
}

public static final Creator CREATOR = new Creator() {
public Playlist createFromParcel(Parcel in) {
return new Playlist(in);
}
public Playlist[] newArray(int length) {
return new Playlist[length];
}
};
}
66 changes: 66 additions & 0 deletions app/src/main/java/ru/ifmo/md/exam1/Track.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ru.ifmo.md.exam1;

import android.os.Parcel;
import android.os.Parcelable;

// {"name":"Mapei – Don't Wait",
// "url":"https://cs1-46v4.vk-cdn.net/p23/53f922c0d2cec9.mp3",
// "duration":"3:35",
// "popularity":76,
// "genres":["Blues","Alternative","Soul"],
// "year":2009}

public class Track implements Parcelable {
public String name = null, url = null, duration = null;
private int genLength = 0;
public String[] genres = new String[0];
public int popularity = 0, year = 0;

public Track() {}

public Track(String name, String url, String duration, String[] genres, int popularity, int year) {
this.name = name;
this.url = url;
this.duration = duration;
this.genres = genres;
this.popularity = popularity;
this.year = year;
this.genLength = genres.length;
}

private Track(Parcel in) {
this.name = in.readString();
this.url = in.readString();
this.duration = in.readString();
this.genLength = in.readInt();
in.readStringArray(this.genres = new String[genLength]);
this.popularity = in.readInt();
this.year = in.readInt();
}

public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int i) {
out.writeString(name);
out.writeString(url);
out.writeString(duration);
out.writeInt(genres.length);
out.writeStringArray(genres);
out.writeInt(popularity);
out.writeInt(year);
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Track createFromParcel(Parcel in) {
return new Track(in);
}
public Track[] newArray(int length) {
return new Track[length];
}
};

public String toString() {
return name + " (" + duration + ")";
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/ru/ifmo/md/exam1/TrackListAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.ifmo.md.exam1;

import android.app.Activity;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

public class TrackListAdapter extends ArrayAdapter<Track> {
public TrackListAdapter(Activity activity, int id, ArrayList<Track> tracks) {
super(activity, id, tracks);
}
}
12 changes: 10 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><![CDATA[

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

]]>

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Loading