Spin the notification off into its own class

This commit is contained in:
Christopher Eby 2010-03-13 01:26:57 -06:00
parent 7e05f88c4a
commit fdc1a87301
2 changed files with 49 additions and 21 deletions

View File

@ -29,11 +29,9 @@ import java.util.List;
import java.util.Random;
import org.kreed.vanilla.IPlaybackService;
import org.kreed.vanilla.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -52,7 +50,6 @@ import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.RemoteViews;
public class PlaybackService extends Service implements Runnable, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, SharedPreferences.OnSharedPreferenceChangeListener {
private static final int NOTIFICATION_ID = 2;
@ -280,7 +277,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
private boolean mHeadsetPause;
private boolean mHeadsetOnly;
private boolean mUseRemotePlayer;
private boolean mNotifyWhilePaused;
private boolean mScrobble;
@ -403,7 +399,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
mSettings.registerOnSharedPreferenceChangeListener(this);
mHeadsetPause = mSettings.getBoolean("headset_pause", true);
mHeadsetOnly = mSettings.getBoolean("headset_only", false);
mUseRemotePlayer = mSettings.getBoolean("remote_player", true);
mNotifyWhilePaused = mSettings.getBoolean("notify_while_paused", true);
mScrobble = mSettings.getBoolean("scrobble", false);
@ -447,7 +442,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
if (mHeadsetOnly && isSpeakerOn() && mMediaPlayer.isPlaying())
pause();
} else if ("remote_player".equals(key)) {
mUseRemotePlayer = mSettings.getBoolean(key, true);
updateNotification();
} else if ("notify_while_paused".equals(key)){
mNotifyWhilePaused = mSettings.getBoolean(key, true);
@ -513,21 +507,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
return true;
}
int statusIcon = mState == STATE_PLAYING ? R.drawable.status_icon : R.drawable.status_icon_paused;
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification);
views.setImageViewResource(R.id.icon, statusIcon);
views.setTextViewText(R.id.title, song.title);
views.setTextViewText(R.id.artist, song.artist);
Notification notification = new Notification();
notification.contentView = views;
notification.icon = statusIcon;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(this, mUseRemotePlayer ? RemoteActivity.class : NowPlayingActivity.class);
notification.contentIntent = PendingIntent.getActivity(ContextApplication.getContext(), 0, intent, 0);
mNotification = notification;
mNotification = new SongNotification(song, mState == STATE_PLAYING);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
return false;

View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
*
* This file is part of Vanilla Music Player.
*
* Vanilla Music Player is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Vanilla Music Player is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kreed.vanilla;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
public class SongNotification extends Notification {
public SongNotification(Song song, boolean playing)
{
Context context = ContextApplication.getContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean remoteView = prefs.getBoolean("remote_player", true);
int statusIcon = playing ? R.drawable.status_icon : R.drawable.status_icon_paused;
RemoteViews views = new RemoteViews(ContextApplication.getContext().getPackageName(), R.layout.notification);
views.setImageViewResource(R.id.icon, statusIcon);
views.setTextViewText(R.id.title, song.title);
views.setTextViewText(R.id.artist, song.artist);
contentView = views;
icon = statusIcon;
flags |= Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(context, remoteView ? RemoteActivity.class : NowPlayingActivity.class);
contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
}
}