Allow the notification to never be shown

This commit is contained in:
Christopher Eby 2010-04-02 10:51:23 -05:00
parent 2116ca6fa6
commit efef58276e
4 changed files with 43 additions and 28 deletions

View File

@ -5,4 +5,14 @@
<item>Enqueue</item> <item>Enqueue</item>
<item>Last Used Action</item> <item>Last Used Action</item>
</string-array> </string-array>
<string-array name="notification_mode_entries">
<item>Never Show</item>
<item>Show When Playing</item>
<item>Always Show</item>
</string-array>
<string-array name="entry_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources> </resources>

View File

@ -35,8 +35,8 @@
<string name="remote_player_summary_on">Clicking the notification will open a mini-player dialog</string> <string name="remote_player_summary_on">Clicking the notification will open a mini-player dialog</string>
<string name="remote_player_summary_off">Clicking the notification will open a the full player activity</string> <string name="remote_player_summary_off">Clicking the notification will open a the full player activity</string>
<string name="notify_while_paused_title">Persistent Notification</string> <string name="notification_mode_title">Notification Mode</string>
<string name="notify_while_paused_summary">Always display the notification</string> <string name="notification_mode_summary">When to show the notification</string>
<string name="phone_input_title">Use Phone Dialer Input</string> <string name="phone_input_title">Use Phone Dialer Input</string>
<string name="phone_input_summary">Enter filter text using buttons 1-9, where 1 matches 1, a, b or c, etc</string> <string name="phone_input_summary">Enter filter text using buttons 1-9, where 1 matches 1, a, b or c, etc</string>

View File

@ -15,17 +15,19 @@
android:summary="@string/headset_pause_summary" /> android:summary="@string/headset_pause_summary" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_notifications"> <PreferenceCategory android:title="@string/pref_notifications">
<ListPreference
android:key="notification_mode"
android:title="@string/notification_mode_title"
android:summary="@string/notification_mode_summary"
android:entries="@array/notification_mode_entries"
android:entryValues="@array/entry_values"
android:defaultValue="1" />
<CheckBoxPreference <CheckBoxPreference
android:key="remote_player" android:key="remote_player"
android:title="@string/remote_player_title" android:title="@string/remote_player_title"
android:defaultValue="true" android:defaultValue="true"
android:summaryOn="@string/remote_player_summary_on" android:summaryOn="@string/remote_player_summary_on"
android:summaryOff="@string/remote_player_summary_off" /> android:summaryOff="@string/remote_player_summary_off" />
<CheckBoxPreference
android:key="notify_while_paused"
android:title="@string/notify_while_paused_title"
android:defaultValue="true"
android:summary="@string/notify_while_paused_summary" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_song_selector"> <PreferenceCategory android:title="@string/pref_song_selector">
<CheckBoxPreference <CheckBoxPreference

View File

@ -64,6 +64,10 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
public static final int FLAG_NO_MEDIA = 0x2; public static final int FLAG_NO_MEDIA = 0x2;
public static final int FLAG_PLAYING = 0x1; public static final int FLAG_PLAYING = 0x1;
public static final int NEVER = 0;
public static final int WHEN_PLAYING = 1;
public static final int ALWAYS = 2;
private int mPendingGo = -1; private int mPendingGo = -1;
public IPlaybackService.Stub mBinder = new IPlaybackService.Stub() { public IPlaybackService.Stub mBinder = new IPlaybackService.Stub() {
@ -122,6 +126,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
} }
} }
}; };
@Override @Override
public IBinder onBind(Intent intent) public IBinder onBind(Intent intent)
{ {
@ -224,6 +229,9 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
Log.w("VanillaMusic", e); Log.w("VanillaMusic", e);
} }
} }
if (cancelNotification && mNotificationManager != null)
mNotificationManager.cancel(NOTIFICATION_ID);
} }
private BroadcastReceiver mReceiver = new BroadcastReceiver() { private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@ -272,8 +280,8 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
private boolean mHeadsetPause; private boolean mHeadsetPause;
private boolean mHeadsetOnly; private boolean mHeadsetOnly;
private boolean mNotifyWhilePaused;
private boolean mScrobble; private boolean mScrobble;
private int mNotificationMode;
private Handler mHandler; private Handler mHandler;
private MediaPlayer mMediaPlayer; private MediaPlayer mMediaPlayer;
@ -362,7 +370,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
mSettings.registerOnSharedPreferenceChangeListener(this); mSettings.registerOnSharedPreferenceChangeListener(this);
mHeadsetPause = mSettings.getBoolean("headset_pause", true); mHeadsetPause = mSettings.getBoolean("headset_pause", true);
mHeadsetOnly = mSettings.getBoolean("headset_only", false); mHeadsetOnly = mSettings.getBoolean("headset_only", false);
mNotifyWhilePaused = mSettings.getBoolean("notify_while_paused", true); mNotificationMode = Integer.parseInt(mSettings.getString("notification_mode", "1"));
mScrobble = mSettings.getBoolean("scrobble", false); mScrobble = mSettings.getBoolean("scrobble", false);
PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE); PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
@ -397,12 +405,11 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
if (mHeadsetOnly && isSpeakerOn()) if (mHeadsetOnly && isSpeakerOn())
unsetFlag(FLAG_PLAYING); unsetFlag(FLAG_PLAYING);
} else if ("remote_player".equals(key)) { } else if ("remote_player".equals(key)) {
// the preference is loaded in SongNotification class
updateNotification(getSong(0)); updateNotification(getSong(0));
} else if ("notify_while_paused".equals(key)){ } else if ("notification_mode".equals(key)){
mNotifyWhilePaused = mSettings.getBoolean(key, true); mNotificationMode = Integer.parseInt(mSettings.getString("notification_mode", "1"));
updateNotification(getSong(0)); updateNotification(getSong(0));
if (!mNotifyWhilePaused && (mState & FLAG_PLAYING) == 0)
stopForegroundCompat(true);
} else if ("scrobble".equals(key)) { } else if ("scrobble".equals(key)) {
mScrobble = mSettings.getBoolean("scrobble", false); mScrobble = mSettings.getBoolean("scrobble", false);
} }
@ -454,13 +461,11 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
Song lastBroadcast = mLastSongBroadcast; Song lastBroadcast = mLastSongBroadcast;
broadcastChange(oldState, state, song); broadcastChange(oldState, state, song);
boolean cancelNotification;
if (state != oldState || song != lastBroadcast) if (state != oldState || song != lastBroadcast)
cancelNotification = updateNotification(song); updateNotification(song);
else
cancelNotification = false;
if ((state & FLAG_PLAYING) != 0 && (oldState & FLAG_PLAYING) == 0) { if ((state & FLAG_PLAYING) != 0 && (oldState & FLAG_PLAYING) == 0) {
if (mNotificationMode != NEVER)
startForegroundCompat(NOTIFICATION_ID, mNotification); startForegroundCompat(NOTIFICATION_ID, mNotification);
if (mMediaPlayerInitialized) { if (mMediaPlayerInitialized) {
synchronized (mMediaPlayer) { synchronized (mMediaPlayer) {
@ -468,7 +473,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
} }
} }
} else if ((state & FLAG_PLAYING) == 0 && (oldState & FLAG_PLAYING) != 0) { } else if ((state & FLAG_PLAYING) == 0 && (oldState & FLAG_PLAYING) != 0) {
stopForegroundCompat(cancelNotification); stopForegroundCompat(false);
if (mMediaPlayerInitialized) { if (mMediaPlayerInitialized) {
synchronized (mMediaPlayer) { synchronized (mMediaPlayer) {
mMediaPlayer.pause(); mMediaPlayer.pause();
@ -481,17 +486,15 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
return true; return true;
} }
private boolean updateNotification(Song song) private void updateNotification(Song song)
{ {
if (song == null || !mNotifyWhilePaused && (mState & FLAG_PLAYING) == 0) { boolean shouldNotify = mNotificationMode == ALWAYS || mNotificationMode == WHEN_PLAYING && (mState & FLAG_PLAYING) != 0;
if (mNotificationManager != null) if (song != null && shouldNotify) {
mNotificationManager.cancel(NOTIFICATION_ID);
return true;
}
mNotification = new SongNotification(song, (mState & FLAG_PLAYING) != 0); mNotification = new SongNotification(song, (mState & FLAG_PLAYING) != 0);
mNotificationManager.notify(NOTIFICATION_ID, mNotification); mNotificationManager.notify(NOTIFICATION_ID, mNotification);
return false; } else {
stopForegroundCompat(true);
}
} }
private boolean isSpeakerOn() private boolean isSpeakerOn()