Allow the widget to start the playback service
This commit is contained in:
1 parent
bae99ac33f
commit
09070cd065
3 files changed
+43
-22
No files matched your search
@@ -3,7 +3,8 @@
|
||||
<string name="app_name">Vanilla Music</string>
|
||||
|
||||
<string name="no_songs">No songs found on your device.</string>
|
||||
<string name="connect_to_service">Connect to Service</string>
|
||||
|
||||
<string name="starting">Starting up...</string>
|
||||
|
||||
<string name="enqueue">Enqueue</string>
|
||||
<string name="play">Play</string>
|
||||
|
||||
@@ -64,8 +64,12 @@ public class OneCellWidget extends AppWidgetProvider {
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.one_cell_widget);
|
||||
|
||||
views.setImageViewResource(R.id.play_pause, playing ? R.drawable.hidden_pause : R.drawable.hidden_play);
|
||||
views.setOnClickPendingIntent(R.id.play_pause, PendingIntent.getBroadcast(context, 0, new Intent(PlaybackService.TOGGLE_PLAYBACK), 0));
|
||||
views.setOnClickPendingIntent(R.id.next, PendingIntent.getBroadcast(context, 0, new Intent(PlaybackService.NEXT_SONG), 0));
|
||||
Intent playPause = new Intent(context, PlaybackService.class);
|
||||
playPause.setAction(PlaybackService.TOGGLE_PLAYBACK);
|
||||
views.setOnClickPendingIntent(R.id.play_pause, PendingIntent.getService(context, 0, playPause, 0));
|
||||
Intent next = new Intent(context, PlaybackService.class);
|
||||
next.setAction(PlaybackService.NEXT_SONG);
|
||||
views.setOnClickPendingIntent(R.id.next, PendingIntent.getService(context, 0, next, 0));
|
||||
|
||||
if (song == null) {
|
||||
views.setImageViewResource(R.id.cover_view, R.drawable.icon);
|
||||
|
||||
@@ -47,6 +47,7 @@ import android.preference.PreferenceManager;
|
||||
import android.telephony.PhoneStateListener;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class PlaybackService extends Service implements Runnable, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private static final int NOTIFICATION_ID = 2;
|
||||
@@ -64,6 +65,9 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
public static final int STATE_NO_MEDIA = 1;
|
||||
public static final int STATE_PLAYING = 2;
|
||||
|
||||
private boolean mStarted;
|
||||
private int mPendingGo = -1;
|
||||
|
||||
public IPlaybackService.Stub mBinder = new IPlaybackService.Stub() {
|
||||
public boolean isLoaded()
|
||||
{
|
||||
@@ -120,7 +124,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent)
|
||||
{
|
||||
@@ -133,11 +136,39 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
new Thread(this).start();
|
||||
}
|
||||
|
||||
private void go(int delta)
|
||||
{
|
||||
if (!mStarted) {
|
||||
Toast.makeText(this, R.string.starting, Toast.LENGTH_SHORT).show();
|
||||
mPendingGo = delta;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mHandler.hasMessages(GO)) {
|
||||
mHandler.removeMessages(GO);
|
||||
Intent launcher = new Intent(this, NowPlayingActivity.class);
|
||||
launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
startActivity(launcher);
|
||||
} else {
|
||||
mHandler.sendMessageDelayed(mHandler.obtainMessage(GO, delta, 0), 250);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(Intent intent, int flags)
|
||||
{
|
||||
String action = intent.getAction();
|
||||
|
||||
if (TOGGLE_PLAYBACK.equals(action)) {
|
||||
go(0);
|
||||
} else if (NEXT_SONG.equals(action)) {
|
||||
go(1);
|
||||
}
|
||||
|
||||
if (mHandler != null)
|
||||
mHandler.sendMessage(mHandler.obtainMessage(DO_ITEM, intent));
|
||||
|
||||
mStarted = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -201,18 +232,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
}
|
||||
}
|
||||
|
||||
private void go(int delta)
|
||||
{
|
||||
if (mHandler.hasMessages(GO)) {
|
||||
mHandler.removeMessages(GO);
|
||||
Intent launcher = new Intent(PlaybackService.this, NowPlayingActivity.class);
|
||||
launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
startActivity(launcher);
|
||||
} else {
|
||||
mHandler.sendMessageDelayed(mHandler.obtainMessage(GO, delta, 0), 250);
|
||||
}
|
||||
}
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context content, Intent intent)
|
||||
@@ -226,10 +245,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
} else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)
|
||||
|| Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)) {
|
||||
mHandler.sendEmptyMessage(RETRIEVE_SONGS);
|
||||
} else if (TOGGLE_PLAYBACK.equals(action)) {
|
||||
go(0);
|
||||
} else if (NEXT_SONG.equals(action)) {
|
||||
go(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -378,14 +393,15 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
|
||||
sendBroadcast(new Intent(EVENT_LOADED));
|
||||
|
||||
if (mPendingGo != -1)
|
||||
mHandler.sendMessage(mHandler.obtainMessage(GO, mPendingGo, 0));
|
||||
|
||||
updateNotification(getSong(0));
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(Intent.ACTION_HEADSET_PLUG);
|
||||
filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
|
||||
filter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
||||
filter.addAction(TOGGLE_PLAYBACK);
|
||||
filter.addAction(NEXT_SONG);
|
||||
registerReceiver(mReceiver, filter);
|
||||
|
||||
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
||||
|
||||
Reference in new issue
Block a user