Skip to content

Commit

Permalink
Merge pull request #6 from IRA-Team/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hluhovskyi committed Oct 28, 2015
2 parents 781e6de + 58c6055 commit 7036a31
Show file tree
Hide file tree
Showing 27 changed files with 270 additions and 56 deletions.
8 changes: 3 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:support-v4:23.1'
compile 'com.android.support:design:23.0.1'

compile 'com.vk:androidsdk:+'
compile 'com.nanohttpd:nanohttpd:2.1.0'
compile 'asia.ivity.android:drag-sort-listview:1.0'
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.yqritc:recyclerview-flexibledivider:1.2.5'
compile 'com.android.support:support-v4:23.0.1'
compile 'com.melnykov:floatingactionbutton:1.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import com.irateam.vkplayer.R;
import com.irateam.vkplayer.activities.settings.SettingsActivity;
import com.irateam.vkplayer.adapter.AudioAdapter;
import com.irateam.vkplayer.adapters.AudioAdapter;
import com.irateam.vkplayer.controllers.PlayerController;
import com.irateam.vkplayer.models.Audio;
import com.irateam.vkplayer.receivers.DownloadFinishedReceiver;
Expand Down Expand Up @@ -224,7 +224,6 @@ public void onComplete(List<Audio> list) {

@Override
public void onError(String errorMessage) {
audioAdapter.setList(Collections.emptyList());
refreshLayout.setRefreshing(false);
Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_LONG)
.setAction(getString(R.string.title_snackbar_action), v -> audioService.repeatLastRequest())
Expand All @@ -246,7 +245,7 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
cacheUpdateReceiver = new DownloadFinishedReceiver() {
@Override
public void onDownloadFinished(Audio audio) {
audioAdapter.getList().add(audio);
audioAdapter.getList().add(0, audio);
audioAdapter.notifyDataSetChanged();
}
};
Expand Down Expand Up @@ -457,12 +456,17 @@ public void onError(String errorMessage) {
case R.id.action_add_to_playlist:
List<Audio> checked = audioAdapter.getCheckedItems();
List<Audio> playlist = playerService.getPlaylist();
playlist.addAll(checked);
List<Audio> listToAdd = new ArrayList<>();
for (Audio audio : checked) {
listToAdd.add(audio.clone());
}
playlist.addAll(0, listToAdd);
audioAdapter.notifyDataSetChanged();
Snackbar.make(coordinatorLayout, R.string.snackbar_add_to_playlist, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_cancel, (v -> {
for (int i = 0; i < checked.size(); i++)
playlist.remove(playlist.size() - 1);
playlist.remove(0);
audioAdapter.notifyDataSetChanged();
}))
.show();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand All @@ -15,9 +16,11 @@
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.irateam.vkplayer.R;
import com.irateam.vkplayer.models.Settings;
import com.irateam.vkplayer.services.AudioService;
import com.irateam.vkplayer.services.DownloadService;

import java.util.List;
Expand Down Expand Up @@ -126,4 +129,42 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class CachePreferenceFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_cache);
setHasOptionsMenu(true);

bindPreferenceSummaryToValue(findPreference("cache_clear"));

findPreference("cache_clear").setOnPreferenceClickListener(preference -> {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getString(R.string.clear_cache_dialog_title))
.setMessage(getString(R.string.clear_cache_dialog_text))
.setPositiveButton(getString(R.string.yes), (dialog, id) -> {
new AudioService(getActivity()).removeAllCachedAudio();
Toast.makeText(getActivity(), getString(R.string.cache_clear_complete), Toast.LENGTH_SHORT).show();
})
.setNegativeButton(getString(R.string.no), (dialog, id) -> {

});
builder.create().show();
return false;
});
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.irateam.vkplayer.adapter;
package com.irateam.vkplayer.adapters;

import android.content.Context;
import android.view.LayoutInflater;
Expand All @@ -10,6 +10,7 @@

import com.irateam.vkplayer.R;
import com.irateam.vkplayer.models.Audio;
import com.irateam.vkplayer.player.Player;
import com.irateam.vkplayer.services.PlayerService;
import com.irateam.vkplayer.ui.AudioListElement;
import com.irateam.vkplayer.utils.AlbumCoverUtils;
Expand All @@ -27,13 +28,24 @@ public class AudioAdapter extends BaseAdapter implements Filterable {
private List<Integer> checkedList = new ArrayList<>();

private boolean sortMode = false;
private Player.PlayerEventListener playerEventListener;

public AudioAdapter(Context context) {
this.context = context;
}

public void setPlayerService(PlayerService playerService) {
this.playerService = playerService;
playerEventListener = (position, audio, event) -> notifyDataSetChanged();
playerService.addPlayerEventListener(playerEventListener);
}

public int getPlayingAudioId() {
if (playerService != null && playerService.getPlayingAudio() != null) {
return playerService.getPlayingAudio().id;
} else {
return -1;
}
}

public List<Audio> getList() {
Expand Down Expand Up @@ -69,8 +81,8 @@ public void updateAudiosById(List<Audio> audios) {
}

public void removeChecked() {
for (int i : checkedList) {
list.remove(i);
for (Audio audio : getCheckedItems()) {
list.remove(audio);
}
notifyDataSetChanged();
}
Expand Down Expand Up @@ -103,6 +115,13 @@ public View getView(final int position, View view, ViewGroup parent) {
element.setTitle(audio.title);
element.setArtist(audio.artist);
element.setCoverDrawable(AlbumCoverUtils.createFromAudio(audio));
if (audio.id == getPlayingAudioId() ) {
if (playerService.isReady()) {
element.setPlaying(playerService.isPlaying());
} else {
element.setPreparing(true);
}
}
element.setCoverOnClickListener((v) -> {
if (!sortMode) {
notifyCoverChecked(position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void setPlayerService(final PlayerService playerService) {
public void onEvent(int position, Audio audio, Player.PlayerEvent event) {
super.onEvent(position, audio, event);
switch (event) {
case PLAY:
case START:
setAudio(position, audio);
break;
case PAUSE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.content.res.Resources;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -131,7 +130,7 @@ public void setFabOnClickListener(View.OnClickListener listener) {
@Override
public void onEvent(int position, Audio audio, Player.PlayerEvent event) {
switch (event) {
case PLAY:
case START:
setAudio(position, audio);
setPlayPause(true);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public List<Audio> getAll() {
SQLiteDatabase db = getReadableDatabase();
List<Audio> list = new ArrayList<>();

Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, ID);
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, "_id DESC");
if (cursor.moveToFirst()) {
do {
list.add(fromCursor(cursor));
Expand All @@ -86,6 +86,12 @@ public List<Audio> getAll() {
return list;
}

public void removeAll() {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, null, null);
db.close();
}

public static ContentValues toContentValues(Audio audio) {
ContentValues cv = new ContentValues();
cv.put(ID, audio.id);
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/com/irateam/vkplayer/models/Audio.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.io.IOException;
import java.net.URL;

public class Audio extends VKApiAudio {
public class Audio extends VKApiAudio implements Cloneable {
public String cachePath;

public boolean isCached() {
Expand Down Expand Up @@ -67,5 +67,19 @@ public Audio[] newArray(int size) {
}
};


public Audio clone() {
Audio audio = new Audio();
audio.id = id;
audio.owner_id = owner_id;
audio.artist = artist;
audio.title = title;
audio.duration = duration;
audio.url = url;
audio.lyrics_id = lyrics_id;
audio.album_id = album_id;
audio.genre = genre;
audio.access_key = access_key;
audio.cachePath = cachePath;
return audio;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static Notification create(Context context, int index, Audio audio, Playe
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Intent intent = new Intent(context, PlayerService.class);
intent.setAction(PlayerService.STOP);

Intent contentIntent = new Intent(context, AudioActivity.class);
contentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle()
.setShowCancelButton(true)
.setCancelButtonIntent(PendingIntent.getService(context, 0, intent, 0));
Expand All @@ -51,12 +54,12 @@ public static Notification create(Context context, int index, Audio audio, Playe
.setStyle(style)
.setPriority(Notification.PRIORITY_HIGH)
.setWhen(0)
.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, AudioActivity.class), 0))
.setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
.addAction(R.drawable.ic_notification_prev_white_24dp,
context.getString(R.string.notification_previous),
createAction(context, PlayerService.PREVIOUS));

if (event == Player.PlayerEvent.RESUME || event == Player.PlayerEvent.PLAY) {
if (event == Player.PlayerEvent.RESUME || event == Player.PlayerEvent.START) {
builder.setSmallIcon(R.drawable.ic_notification_play_white_18dp)
.addAction(R.drawable.ic_notification_pause_white_24dp,
context.getString(R.string.notification_pause),
Expand Down
29 changes: 19 additions & 10 deletions app/src/main/java/com/irateam/vkplayer/player/Player.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.irateam.vkplayer.player;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
Expand All @@ -26,7 +25,7 @@ public class Player extends MediaPlayer implements MediaPlayer.OnCompletionListe

private boolean stateReady = false;

public Player(Context context) {
public Player() {
super();
setAudioStreamType(AudioManager.STREAM_MUSIC);
setOnPreparedListener(this);
Expand Down Expand Up @@ -70,7 +69,7 @@ public void play(int index) {
setOnBufferingUpdateListener(null);
setDataSource(playingAudio.getPlayingUrl());
prepareAsync();
notifyPlayerEvent(index, playingAudio, PlayerEvent.PLAY);
notifyPlayerEvent(index, playingAudio, PlayerEvent.START);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -85,11 +84,9 @@ public void resume() {
}

public void stop() {
if (isReady() && playingAudio != null) {
super.stop();
notifyPlayerEvent(getPlayingAudioIndex(), playingAudio, PlayerEvent.STOP);
playingAudio = null;
}
super.reset();
notifyPlayerEvent(getPlayingAudioIndex(), playingAudio, PlayerEvent.STOP);
playingAudio = null;
}

public void pause() {
Expand All @@ -106,10 +103,16 @@ public int getPauseTime() {

public void next() {
int nextIndex;
if (list == null || list.size() == 0) {
stop();
return;
}
if (randomState) {
int size = list.size();
do
nextIndex = random.nextInt(list.size());
while (getPlayingAudioIndex() == nextIndex);
nextIndex = random.nextInt(size);
while (size > 1 &&
getPlayingAudioIndex() == nextIndex);
randomStack.push(playingAudio);
} else {
nextIndex = getPlayingAudioIndex() + 1;
Expand All @@ -123,6 +126,10 @@ public void next() {

public void previous() {
int previousIndex;
if (list == null || list.size() == 0) {
stop();
return;
}
if (randomState && !randomStack.empty()) {
previousIndex = list.indexOf(randomStack.pop());
} else {
Expand Down Expand Up @@ -210,6 +217,7 @@ public void onPrepared(MediaPlayer mp) {
start();
startProgress();
setOnBufferingUpdateListener(this);
notifyPlayerEvent(getPlayingAudioIndex(), playingAudio, PlayerEvent.START);
}

@Override
Expand Down Expand Up @@ -258,6 +266,7 @@ public enum RepeatState {
}

public enum PlayerEvent {
START,
PLAY,
PAUSE,
RESUME,
Expand Down
Loading

0 comments on commit 7036a31

Please sign in to comment.