Add a new action for notification click: skip to next song

This commit is contained in:
Christopher Eby 2010-07-25 17:00:51 -05:00
parent 332d585961
commit 2a1361b626
6 changed files with 73 additions and 14 deletions

View File

@ -27,6 +27,11 @@
<item>Show When Playing</item> <item>Show When Playing</item>
<item>Always Show</item> <item>Always Show</item>
</string-array> </string-array>
<string-array name="notification_action_entries">
<item>Open Main Activity</item>
<item>Open Mini Player</item>
<item>Skip To Next Song</item>
</string-array>
<string-array name="entry_values"> <string-array name="entry_values">
<item>0</item> <item>0</item>
<item>1</item> <item>1</item>

View File

@ -100,10 +100,10 @@
<string name="headset_pause_summary">Pause when the headphones are unplugged.</string> <string name="headset_pause_summary">Pause when the headphones are unplugged.</string>
<string name="pref_notifications">Notifications</string> <string name="pref_notifications">Notifications</string>
<string name="mini_player_title">Use Mini Player</string>
<string name="mini_player_summary">Launch a pop-up music control dialog when the notification is pressed</string>
<string name="notification_mode_title">Notification Mode</string> <string name="notification_mode_title">Notification Mode</string>
<string name="notification_mode_summary">When to show the notification</string> <string name="notification_mode_summary">When to show the notification</string>
<string name="notification_action_title">Notification Action</string>
<string name="notification_action_summary">What to do when the notification is pressed</string>
<string name="pref_song_selector">Song Selector</string> <string name="pref_song_selector">Song Selector</string>
<string name="selector_on_startup_title">Open on Startup</string> <string name="selector_on_startup_title">Open on Startup</string>

View File

@ -48,11 +48,13 @@
android:entries="@array/notification_mode_entries" android:entries="@array/notification_mode_entries"
android:entryValues="@array/entry_values" android:entryValues="@array/entry_values"
android:defaultValue="1" /> android:defaultValue="1" />
<CheckBoxPreference <ListPreference
android:key="remote_player" android:key="notification_action"
android:title="@string/mini_player_title" android:title="@string/notification_action_title"
android:summary="@string/mini_player_summary" android:summary="@string/notification_action_summary"
android:defaultValue="true" /> android:entries="@array/notification_action_entries"
android:entryValues="@array/entry_values"
android:defaultValue="0" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_song_selector"> <PreferenceCategory android:title="@string/pref_song_selector">
<CheckBoxPreference <CheckBoxPreference

View File

@ -131,7 +131,6 @@ public class MediaButtonHandler implements Handler.Callback {
Context context = ContextApplication.getContext(); Context context = ContextApplication.getContext();
Intent intent = new Intent(context, PlaybackService.class); Intent intent = new Intent(context, PlaybackService.class);
intent.setAction(action); intent.setAction(action);
intent.putExtra("autoplay", true);
context.startService(intent); context.startService(intent);
} }
@ -160,7 +159,7 @@ public class MediaButtonHandler implements Handler.Callback {
if (mHandler.hasMessages(MSG_SINGLE_PRESS_TIMEOUT)) { if (mHandler.hasMessages(MSG_SINGLE_PRESS_TIMEOUT)) {
// double click // double click
mHandler.removeMessages(MSG_SINGLE_PRESS_TIMEOUT); mHandler.removeMessages(MSG_SINGLE_PRESS_TIMEOUT);
act(PlaybackService.ACTION_NEXT_SONG); act(PlaybackService.ACTION_NEXT_SONG_AUTOPLAY);
} else { } else {
mHandler.sendEmptyMessageDelayed(MSG_SINGLE_PRESS_TIMEOUT, DOUBLE_CLICK_DELAY); mHandler.sendEmptyMessageDelayed(MSG_SINGLE_PRESS_TIMEOUT, DOUBLE_CLICK_DELAY);
} }
@ -168,11 +167,11 @@ public class MediaButtonHandler implements Handler.Callback {
break; break;
case KeyEvent.KEYCODE_MEDIA_NEXT: case KeyEvent.KEYCODE_MEDIA_NEXT:
if (action == KeyEvent.ACTION_DOWN) if (action == KeyEvent.ACTION_DOWN)
act(PlaybackService.ACTION_NEXT_SONG); act(PlaybackService.ACTION_NEXT_SONG_AUTOPLAY);
break; break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS: case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
if (action == KeyEvent.ACTION_DOWN) if (action == KeyEvent.ACTION_DOWN)
act(PlaybackService.ACTION_PREVIOUS_SONG); act(PlaybackService.ACTION_PREVIOUS_SONG_AUTOPLAY);
break; break;
default: default:
return false; return false;

View File

@ -79,10 +79,24 @@ public final class PlaybackService extends Service implements Handler.Callback,
* instead. * instead.
*/ */
public static final String ACTION_NEXT_SONG_DELAYED = "org.kreed.vanilla.action.NEXT_SONG_DELAYED"; public static final String ACTION_NEXT_SONG_DELAYED = "org.kreed.vanilla.action.NEXT_SONG_DELAYED";
/**
* Action for startService: advance to the next song.
*
* Like ACTION_NEXT_SONG, but starts playing automatically if paused
* when this is called.
*/
public static final String ACTION_NEXT_SONG_AUTOPLAY = "org.kreed.vanilla.action.NEXT_SONG_AUTOPLAY";
/** /**
* Action for startService: go back to the previous song. * Action for startService: go back to the previous song.
*/ */
public static final String ACTION_PREVIOUS_SONG = "org.kreed.vanilla.action.PREVIOUS_SONG"; public static final String ACTION_PREVIOUS_SONG = "org.kreed.vanilla.action.PREVIOUS_SONG";
/**
* Action for startService: go back to the previous song.
*
* Like ACTION_PREVIOUS_SONG, but starts playing automatically if paused
* when this is called.
*/
public static final String ACTION_PREVIOUS_SONG_AUTOPLAY = "org.kreed.vanilla.action.PREVIOUS_SONG_AUTOPLAY";
/** /**
* Intent action that may be invoked through startService. * Intent action that may be invoked through startService.
* *
@ -233,6 +247,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
// feedback. // feedback.
broadcastReplaceSong(0, getSong(+1)); broadcastReplaceSong(0, getSong(+1));
go(1, false); go(1, false);
} else if (ACTION_NEXT_SONG_AUTOPLAY.equals(action)) {
go(1, true);
} else if (ACTION_NEXT_SONG_DELAYED.equals(action)) { } else if (ACTION_NEXT_SONG_DELAYED.equals(action)) {
if (mHandler.hasMessages(CALL_GO, Integer.valueOf(1))) { if (mHandler.hasMessages(CALL_GO, Integer.valueOf(1))) {
mHandler.removeMessages(CALL_GO, Integer.valueOf(1)); mHandler.removeMessages(CALL_GO, Integer.valueOf(1));
@ -242,6 +258,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
} }
} else if (ACTION_PREVIOUS_SONG.equals(action)) { } else if (ACTION_PREVIOUS_SONG.equals(action)) {
go(-1, false); go(-1, false);
} else if (ACTION_PREVIOUS_SONG_AUTOPLAY.equals(action)) {
go(-1, true);
} else if (ACTION_PLAY_ITEMS.equals(action)) { } else if (ACTION_PLAY_ITEMS.equals(action)) {
mTimeline.chooseSongs(false, intent.getIntExtra("type", 3), intent.getLongExtra("id", -1)); mTimeline.chooseSongs(false, intent.getIntExtra("type", 3), intent.getLongExtra("id", -1));
mHandler.sendEmptyMessage(TRACK_CHANGED); mHandler.sendEmptyMessage(TRACK_CHANGED);

View File

@ -24,6 +24,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.RemoteViews; import android.widget.RemoteViews;
/** /**
@ -36,6 +37,19 @@ import android.widget.RemoteViews;
* lower line is the song artist. * lower line is the song artist.
*/ */
public class SongNotification extends Notification { public class SongNotification extends Notification {
/**
* Notification click action: open LaunchActivity.
*/
private static final int ACTION_MAIN_ACTIVITY = 0;
/**
* Notification click action: open MiniPlaybackActivity.
*/
private static final int ACTION_MINI_ACTIVITY = 1;
/**
* Notification click action: skip to next song.
*/
private static final int ACTION_NEXT_SONG = 2;
/** /**
* Create a SongNotification. Call through the NotificationManager to * Create a SongNotification. Call through the NotificationManager to
* display it. * display it.
@ -47,7 +61,7 @@ public class SongNotification extends Notification {
{ {
Context context = ContextApplication.getContext(); Context context = ContextApplication.getContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean remoteView = prefs.getBoolean("remote_player", true); int action = Integer.parseInt(prefs.getString("notification_action", "0"));
int statusIcon = playing ? R.drawable.status_icon : R.drawable.status_icon_paused; int statusIcon = playing ? R.drawable.status_icon : R.drawable.status_icon_paused;
RemoteViews views = new RemoteViews(ContextApplication.getContext().getPackageName(), R.layout.notification); RemoteViews views = new RemoteViews(ContextApplication.getContext().getPackageName(), R.layout.notification);
@ -58,7 +72,28 @@ public class SongNotification extends Notification {
contentView = views; contentView = views;
icon = statusIcon; icon = statusIcon;
flags |= Notification.FLAG_ONGOING_EVENT; flags |= Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(context, remoteView ? MiniPlaybackActivity.class : LaunchActivity.class);
contentIntent = PendingIntent.getActivity(context, 0, intent, 0); Intent intent;
switch (action) {
case ACTION_NEXT_SONG:
intent = new Intent(context, PlaybackService.class);
intent.setAction(PlaybackService.ACTION_NEXT_SONG_AUTOPLAY);
contentIntent = PendingIntent.getService(context, 0, intent, 0);
break;
case ACTION_MINI_ACTIVITY:
intent = new Intent(context, MiniPlaybackActivity.class);
contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
break;
default:
Log.w("VanillaMusic", "Unknown value for notification_action: " + action + ". Resetting to 0.");
SharedPreferences.Editor editor = prefs.edit();
editor.putString("notification_action", "0");
editor.commit();
// fall through
case ACTION_MAIN_ACTIVITY:
intent = new Intent(context, LaunchActivity.class);
contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
break;
}
} }
} }