store the songs list in the service, not activity
This commit is contained in:
parent
ac0ccad4f8
commit
c4fb58d76b
2 changed files with 67 additions and 60 deletions
|
@ -1,15 +1,11 @@
|
|||
package musicplayer.simplemobiletools.com;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
|
@ -17,17 +13,11 @@ import android.widget.ImageView;
|
|||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private static final int MIN_DURATION_MS = 20000;
|
||||
private ArrayList<Song> songs;
|
||||
private MusicService musicService;
|
||||
private boolean isMusicBound;
|
||||
|
||||
|
@ -41,18 +31,6 @@ public class MainActivity extends AppCompatActivity {
|
|||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.bind(this);
|
||||
songs = new ArrayList<>();
|
||||
|
||||
getSortedSongs();
|
||||
|
||||
final SongAdapter adapter = new SongAdapter(this, songs);
|
||||
songsList.setAdapter(adapter);
|
||||
songsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
songPicked(position);
|
||||
}
|
||||
});
|
||||
|
||||
final Intent intent = new Intent(this, MusicService.class);
|
||||
bindService(intent, musicConnection, Context.BIND_AUTO_CREATE);
|
||||
|
@ -72,48 +50,17 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void getSortedSongs() {
|
||||
getSongs();
|
||||
Collections.sort(songs, new Comparator<Song>() {
|
||||
public int compare(Song a, Song b) {
|
||||
return a.getTitle().compareTo(b.getTitle());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void getSongs() {
|
||||
final ContentResolver musicResolver = getContentResolver();
|
||||
final Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
||||
final Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
|
||||
|
||||
if (musicCursor != null && musicCursor.moveToFirst()) {
|
||||
final int idIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
|
||||
final int titleIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
|
||||
final int artistIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
|
||||
final int durationIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
|
||||
do {
|
||||
if (musicCursor.getInt(durationIndex) > MIN_DURATION_MS) {
|
||||
final long id = musicCursor.getLong(idIndex);
|
||||
final String title = musicCursor.getString(titleIndex);
|
||||
final String artist = musicCursor.getString(artistIndex);
|
||||
songs.add(new Song(id, title, artist));
|
||||
}
|
||||
} while (musicCursor.moveToNext());
|
||||
musicCursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceConnection musicConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
||||
final MusicService.MyBinder binder = (MusicService.MyBinder) iBinder;
|
||||
musicService = binder.getService();
|
||||
musicService.setSongs(songs);
|
||||
isMusicBound = true;
|
||||
|
||||
updateSongInfo(musicService.getCurrSong());
|
||||
if (musicService.isPlaying())
|
||||
setPauseIcon();
|
||||
fillSongsListView();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -122,6 +69,17 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
};
|
||||
|
||||
private void fillSongsListView() {
|
||||
final SongAdapter adapter = new SongAdapter(this, musicService.getSongs());
|
||||
songsList.setAdapter(adapter);
|
||||
songsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
songPicked(position);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if (isMusicBound) {
|
||||
|
@ -172,7 +130,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
public void resumePauseSong() {
|
||||
if (songs.isEmpty() || musicService == null)
|
||||
if (musicService == null)
|
||||
return;
|
||||
|
||||
if (musicService.isPlaying()) {
|
||||
|
@ -200,7 +158,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
private void playPreviousSong() {
|
||||
if (songs.isEmpty() || musicService == null)
|
||||
if (musicService == null)
|
||||
return;
|
||||
|
||||
musicService.playPreviousSong();
|
||||
|
@ -209,7 +167,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
private void playNextSong() {
|
||||
if (songs.isEmpty() || musicService == null)
|
||||
if (musicService == null)
|
||||
return;
|
||||
|
||||
musicService.playNextSong();
|
||||
|
@ -226,7 +184,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
private boolean isPlaylistEmpty() {
|
||||
if (songs == null || songs.isEmpty()) {
|
||||
if (musicService == null || musicService.getSongs().isEmpty()) {
|
||||
Utils.showToast(this, R.string.playlist_empty);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package musicplayer.simplemobiletools.com;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentUris;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.database.Cursor;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.net.Uri;
|
||||
|
@ -22,11 +24,14 @@ import com.squareup.otto.Subscribe;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Random;
|
||||
|
||||
public class MusicService extends Service
|
||||
implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
|
||||
private static final String TAG = MusicService.class.getSimpleName();
|
||||
private static final int MIN_DURATION_MS = 20000;
|
||||
private final IBinder musicBind = new MyBinder();
|
||||
private HeadsetPlugReceiver headsetPlugReceiver;
|
||||
private IncomingCallReceiver incomingCallReceiver;
|
||||
|
@ -48,6 +53,7 @@ public class MusicService extends Service
|
|||
bus.register(this);
|
||||
}
|
||||
|
||||
getSortedSongs();
|
||||
headsetPlugReceiver = new HeadsetPlugReceiver();
|
||||
incomingCallReceiver = new IncomingCallReceiver();
|
||||
wasPlayingAtCall = false;
|
||||
|
@ -63,6 +69,37 @@ public class MusicService extends Service
|
|||
player.setOnErrorListener(this);
|
||||
}
|
||||
|
||||
private void fillPlaylist() {
|
||||
final ContentResolver musicResolver = getContentResolver();
|
||||
final Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
||||
final Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
|
||||
|
||||
if (musicCursor != null && musicCursor.moveToFirst()) {
|
||||
final int idIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
|
||||
final int titleIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
|
||||
final int artistIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
|
||||
final int durationIndex = musicCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
|
||||
do {
|
||||
if (musicCursor.getInt(durationIndex) > MIN_DURATION_MS) {
|
||||
final long id = musicCursor.getLong(idIndex);
|
||||
final String title = musicCursor.getString(titleIndex);
|
||||
final String artist = musicCursor.getString(artistIndex);
|
||||
songs.add(new Song(id, title, artist));
|
||||
}
|
||||
} while (musicCursor.moveToNext());
|
||||
musicCursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void getSortedSongs() {
|
||||
fillPlaylist();
|
||||
Collections.sort(songs, new Comparator<Song>() {
|
||||
public int compare(Song a, Song b) {
|
||||
return a.getTitle().compareTo(b.getTitle());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private int getNewSongId() {
|
||||
final int cnt = songs.size();
|
||||
if (cnt == 0) {
|
||||
|
@ -85,6 +122,9 @@ public class MusicService extends Service
|
|||
}
|
||||
|
||||
public void playPreviousSong() {
|
||||
if (songs.isEmpty())
|
||||
return;
|
||||
|
||||
if (player == null)
|
||||
initMediaPlayer();
|
||||
|
||||
|
@ -99,6 +139,9 @@ public class MusicService extends Service
|
|||
}
|
||||
|
||||
public void pauseSong() {
|
||||
if (songs.isEmpty())
|
||||
return;
|
||||
|
||||
if (player == null)
|
||||
initMediaPlayer();
|
||||
|
||||
|
@ -107,6 +150,9 @@ public class MusicService extends Service
|
|||
}
|
||||
|
||||
public void resumeSong() {
|
||||
if (songs.isEmpty())
|
||||
return;
|
||||
|
||||
if (player == null)
|
||||
initMediaPlayer();
|
||||
|
||||
|
@ -137,6 +183,9 @@ public class MusicService extends Service
|
|||
}
|
||||
|
||||
public void setSong(int songId, boolean addNewSong) {
|
||||
if (songs.isEmpty())
|
||||
return;
|
||||
|
||||
if (player == null)
|
||||
initMediaPlayer();
|
||||
|
||||
|
@ -175,8 +224,8 @@ public class MusicService extends Service
|
|||
return false;
|
||||
}
|
||||
|
||||
public void setSongs(ArrayList<Song> songs) {
|
||||
this.songs = songs;
|
||||
public ArrayList<Song> getSongs() {
|
||||
return songs;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue