Combine stateChanged and songChanged events
In addition, move some of this event handling code into the PlaybackServiceActivity superclass
This commit is contained in:
1 parent
fdc1a87301
commit
ab543e3871
5 files changed
+104
-142
No files matched your search
@@ -20,10 +20,8 @@ package org.kreed.vanilla;
|
||||
|
||||
import org.kreed.vanilla.IPlaybackService;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
@@ -66,11 +64,6 @@ public class CoverView extends View {
|
||||
|
||||
mScroller = new Scroller(context);
|
||||
SNAP_VELOCITY = ViewConfiguration.get(context).getScaledMinimumFlingVelocity();
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(PlaybackService.EVENT_LOADED);
|
||||
filter.addAction(PlaybackService.EVENT_SONG_CHANGED);
|
||||
context.registerReceiver(mReceiver, filter);
|
||||
}
|
||||
|
||||
public void setPlaybackService(IPlaybackService service)
|
||||
@@ -460,22 +453,18 @@ public class CoverView extends View {
|
||||
}
|
||||
};
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
String action = intent.getAction();
|
||||
|
||||
if (PlaybackService.EVENT_LOADED.equals(action)) {
|
||||
public void onReceive(Intent intent)
|
||||
{
|
||||
String action = intent.getAction();
|
||||
if (PlaybackService.EVENT_LOADED.equals(action)) {
|
||||
refreshSongs();
|
||||
} else if (PlaybackService.EVENT_CHANGED.equals(action)) {
|
||||
Song currentSong = mSongs[STORE_SIZE / 2];
|
||||
Song playingSong = intent.getParcelableExtra("song");
|
||||
if (currentSong == null)
|
||||
mHandler.sendMessage(mHandler.obtainMessage(SET_COVER, playingSong));
|
||||
else if (!currentSong.equals(playingSong))
|
||||
refreshSongs();
|
||||
} else if (PlaybackService.EVENT_SONG_CHANGED.equals(action)) {
|
||||
Song currentSong = mSongs[STORE_SIZE / 2];
|
||||
Song playingSong = intent.getParcelableExtra("song");
|
||||
if (currentSong == null)
|
||||
mHandler.sendMessage(mHandler.obtainMessage(SET_COVER, playingSong));
|
||||
else if (!currentSong.equals(playingSong))
|
||||
refreshSongs();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -21,10 +21,8 @@ package org.kreed.vanilla;
|
||||
import org.kreed.vanilla.IPlaybackService;
|
||||
import org.kreed.vanilla.R;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
@@ -47,7 +45,6 @@ public class NowPlayingActivity extends PlaybackServiceActivity implements View.
|
||||
private IPlaybackService mService;
|
||||
|
||||
private ViewGroup mLayout;
|
||||
private CoverView mCoverView;
|
||||
private RelativeLayout mMessageOverlay;
|
||||
private View mControlsTop;
|
||||
private View mControlsBottom;
|
||||
@@ -127,7 +124,15 @@ public class NowPlayingActivity extends PlaybackServiceActivity implements View.
|
||||
}
|
||||
}
|
||||
|
||||
private void setState(int state)
|
||||
@Override
|
||||
public void unbindService(ServiceConnection connection)
|
||||
{
|
||||
super.unbindService(connection);
|
||||
mHandler.sendEmptyMessage(UNSET_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setState(int state)
|
||||
{
|
||||
mState = state;
|
||||
|
||||
@@ -178,34 +183,6 @@ public class NowPlayingActivity extends PlaybackServiceActivity implements View.
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart()
|
||||
{
|
||||
super.onStart();
|
||||
|
||||
bindPlaybackService(false);
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(PlaybackService.EVENT_SONG_CHANGED);
|
||||
filter.addAction(PlaybackService.EVENT_STATE_CHANGED);
|
||||
registerReceiver(mReceiver, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop()
|
||||
{
|
||||
super.onStop();
|
||||
|
||||
if (mService != null) {
|
||||
unbindService(this);
|
||||
try {
|
||||
unregisterReceiver(mReceiver);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// we haven't registered the receiver yet
|
||||
}
|
||||
mHandler.sendEmptyMessage(UNSET_SERVICE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setService(IPlaybackService service)
|
||||
@@ -232,26 +209,20 @@ public class NowPlayingActivity extends PlaybackServiceActivity implements View.
|
||||
setState(state);
|
||||
}
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
String action = intent.getAction();
|
||||
if (PlaybackService.EVENT_SONG_CHANGED.equals(action)) {
|
||||
if (mService == null)
|
||||
return;
|
||||
@Override
|
||||
protected void onServiceChange(Intent intent)
|
||||
{
|
||||
super.onServiceChange(intent);
|
||||
|
||||
try {
|
||||
mDuration = mService.getDuration();
|
||||
mHandler.sendEmptyMessage(UPDATE_PROGRESS);
|
||||
} catch (RemoteException e) {
|
||||
setService(null);
|
||||
}
|
||||
} else if (PlaybackService.EVENT_STATE_CHANGED.equals(action)) {
|
||||
setState(intent.getIntExtra("newState", 0));
|
||||
if (mService != null) {
|
||||
try {
|
||||
mDuration = mService.getDuration();
|
||||
mHandler.sendEmptyMessage(UPDATE_PROGRESS);
|
||||
} catch (RemoteException e) {
|
||||
setService(null);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu)
|
||||
@@ -271,11 +242,10 @@ public class NowPlayingActivity extends PlaybackServiceActivity implements View.
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(final MenuItem item)
|
||||
public boolean onOptionsItemSelected(MenuItem item)
|
||||
{
|
||||
switch (item.getItemId()) {
|
||||
case MENU_KILL:
|
||||
setService(null);
|
||||
unbindService(this);
|
||||
stopPlaybackService(this);
|
||||
break;
|
||||
|
||||
@@ -59,8 +59,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
public static final String NEXT_SONG = "org.kreed.vanilla.action.NEXT_SONG";
|
||||
|
||||
public static final String EVENT_LOADED = "org.kreed.vanilla.event.LOADED";
|
||||
public static final String EVENT_STATE_CHANGED = "org.kreed.vanilla.event.STATE_CHANGED";
|
||||
public static final String EVENT_SONG_CHANGED = "org.kreed.vanilla.event.SONG_CHANGED";
|
||||
public static final String EVENT_CHANGED = "org.kreed.vanilla.event.CHANGED";
|
||||
|
||||
public static final int ACTION_PLAY = 0;
|
||||
public static final int ACTION_ENQUEUE = 1;
|
||||
@@ -340,7 +339,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
if (song.path == null) {
|
||||
stateLoaded = false;
|
||||
} else {
|
||||
broadcastSongChange(song);
|
||||
broadcastChange(mState, mState, song);
|
||||
updateWidgets(song);
|
||||
}
|
||||
|
||||
@@ -363,7 +362,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
retrieveSongs();
|
||||
mSongTimeline = new ArrayList<Song>();
|
||||
Song song = getSong(0);
|
||||
broadcastSongChange(song);
|
||||
broadcastChange(mState, mState, song);
|
||||
updateWidgets(song);
|
||||
}
|
||||
|
||||
@@ -453,23 +452,29 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
}
|
||||
}
|
||||
|
||||
public void broadcastChange(int oldState, int newState, Song song)
|
||||
{
|
||||
if (newState != oldState || song != mLastSongBroadcast) {
|
||||
Intent intent = new Intent(EVENT_CHANGED);
|
||||
intent.putExtra("oldState", oldState);
|
||||
intent.putExtra("newState", newState);
|
||||
intent.putExtra("song", song);
|
||||
sendBroadcast(intent);
|
||||
|
||||
mLastSongBroadcast = song;
|
||||
}
|
||||
}
|
||||
|
||||
public void updateState(int state)
|
||||
{
|
||||
Song song = getSong(0);
|
||||
int oldState = mState;
|
||||
mState = state;
|
||||
|
||||
if (mState != oldState) {
|
||||
Intent intent = new Intent(EVENT_STATE_CHANGED);
|
||||
intent.putExtra("oldState", oldState);
|
||||
intent.putExtra("newState", state);
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
|
||||
Song song = getSong(0);
|
||||
if (song == null)
|
||||
return;
|
||||
|
||||
broadcastSongChange(song);
|
||||
broadcastChange(oldState, state, song);
|
||||
|
||||
boolean cancelNotification = updateNotification();
|
||||
updateWidgets(song);
|
||||
@@ -559,18 +564,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
pause();
|
||||
}
|
||||
|
||||
private void broadcastSongChange(Song song)
|
||||
{
|
||||
if (song == mLastSongBroadcast)
|
||||
return;
|
||||
|
||||
mLastSongBroadcast = song;
|
||||
|
||||
Intent intent = new Intent(EVENT_SONG_CHANGED);
|
||||
intent.putExtra("song", song);
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
|
||||
private void setCurrentSong(int delta)
|
||||
{
|
||||
if (mMediaPlayer == null)
|
||||
|
||||
@@ -19,16 +19,46 @@
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.IBinder;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
public class PlaybackServiceActivity extends Activity implements ServiceConnection {
|
||||
public abstract class PlaybackServiceActivity extends Activity implements ServiceConnection {
|
||||
protected CoverView mCoverView;
|
||||
|
||||
@Override
|
||||
public void onStart()
|
||||
{
|
||||
super.onStart();
|
||||
|
||||
bindPlaybackService(false);
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(PlaybackService.EVENT_CHANGED);
|
||||
filter.addAction(PlaybackService.EVENT_LOADED);
|
||||
registerReceiver(mReceiver, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop()
|
||||
{
|
||||
super.onStop();
|
||||
|
||||
unbindService(this);
|
||||
try {
|
||||
unregisterReceiver(mReceiver);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// we haven't registered the receiver yet
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean handleKeyLongPress(Activity activity, int keyCode)
|
||||
{
|
||||
switch (keyCode) {
|
||||
@@ -72,9 +102,8 @@ public class PlaybackServiceActivity extends Activity implements ServiceConnecti
|
||||
activity.stopService(new Intent(activity, PlaybackService.class));
|
||||
}
|
||||
|
||||
protected void setService(IPlaybackService service)
|
||||
{
|
||||
}
|
||||
protected abstract void setState(int state);
|
||||
protected abstract void setService(IPlaybackService service);
|
||||
|
||||
public void onServiceConnected(ComponentName name, IBinder service)
|
||||
{
|
||||
@@ -85,4 +114,21 @@ public class PlaybackServiceActivity extends Activity implements ServiceConnecti
|
||||
{
|
||||
setService(null);
|
||||
}
|
||||
|
||||
protected void onServiceChange(Intent intent)
|
||||
{
|
||||
int newState = intent.getIntExtra("newState", 0);
|
||||
if (intent.getIntExtra("oldState", 0) != newState)
|
||||
setState(newState);
|
||||
}
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
mCoverView.onReceive(intent);
|
||||
if (PlaybackService.EVENT_CHANGED.equals(intent.getAction()))
|
||||
onServiceChange(intent);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -21,10 +21,7 @@ package org.kreed.vanilla;
|
||||
import org.kreed.vanilla.IPlaybackService;
|
||||
import org.kreed.vanilla.R;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Bundle;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
@@ -33,8 +30,6 @@ import android.view.Window;
|
||||
import android.widget.ImageView;
|
||||
|
||||
public class RemoteActivity extends PlaybackServiceActivity implements View.OnClickListener {
|
||||
private CoverView mCoverView;
|
||||
|
||||
private View mOpenButton;
|
||||
private View mKillButton;
|
||||
private View mPreviousButton;
|
||||
@@ -63,28 +58,6 @@ public class RemoteActivity extends PlaybackServiceActivity implements View.OnCl
|
||||
mNextButton.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume()
|
||||
{
|
||||
super.onResume();
|
||||
|
||||
bindPlaybackService(true);
|
||||
registerReceiver(mReceiver, new IntentFilter(PlaybackService.EVENT_STATE_CHANGED));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause()
|
||||
{
|
||||
super.onPause();
|
||||
|
||||
unbindService(this);
|
||||
try {
|
||||
unregisterReceiver(mReceiver);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// we haven't registered the receiver yet
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setService(IPlaybackService service)
|
||||
{
|
||||
@@ -99,7 +72,8 @@ public class RemoteActivity extends PlaybackServiceActivity implements View.OnCl
|
||||
}
|
||||
}
|
||||
|
||||
private void setState(int state)
|
||||
@Override
|
||||
protected void setState(int state)
|
||||
{
|
||||
if (state == PlaybackService.STATE_NO_MEDIA)
|
||||
finish();
|
||||
@@ -129,14 +103,4 @@ public class RemoteActivity extends PlaybackServiceActivity implements View.OnCl
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
String action = intent.getAction();
|
||||
if (PlaybackService.EVENT_STATE_CHANGED.equals(action))
|
||||
setState(intent.getIntExtra("newState", 0));
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user