Permalink
Browse files

notigfications

  • Loading branch information...
1 parent 56ff5d9 commit 8d2183cb61f15a1840142e05ad503f33d0a75872 @chrisrhoden chrisrhoden committed Mar 1, 2012
View
BIN .DS_Store
Binary file not shown.
View
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
View
@@ -3,7 +3,7 @@
package="org.prx.android.playerhater"
android:versionCode="1"
android:versionName="0.0.1" >
- <uses-sdk android:minSdkVersion="8" />
+ <uses-sdk android:minSdkVersion="8"/>
<application>
<service android:name=".PlayerHaterService"></service>
</application>
@@ -40,7 +40,14 @@
void transientPlay(FileDescriptor file, boolean isDuckable);
void setNotificationIntentActivity(Activity activityTriggeredOnNotificationTouch);
+ void setNotificationIcon(int notificationIcon);
void setNotificationView(int notificationView);
+ void setNotificationTitle(String notificationTitle);
+ void setNotificationText(String notificationText);
+
+ void setAutoNotify(boolean autoNotify);
+ void startForeground();
+ void stopForeground();
int getCurrentPosition();
int getDuration();
@@ -250,4 +250,34 @@ public void transientPlay(FileDescriptor file, boolean isDuckable) {
public int getDuration() {
return mService.getDuration();
}
+
+ @Override
+ public void setNotificationIcon(int notificationIcon) {
+ mService.setNotificationIcon(notificationIcon);
+ }
+
+ @Override
+ public void setAutoNotify(boolean autoNotify) {
+ mService.setAutoNotify(autoNotify);
+ }
+
+ @Override
+ public void startForeground() {
+ mService.doStartForeground();
+ }
+
+ @Override
+ public void stopForeground() {
+ mService.doStopForeground();
+ }
+
+ @Override
+ public void setNotificationTitle(String notificationTitle) {
+ mService.setNotificationTitle(notificationTitle);
+ }
+
+ @Override
+ public void setNotificationText(String notificationText) {
+ mService.setNotificationText(notificationText);
+ }
}
@@ -53,9 +53,15 @@
private AudioManager mAudioManager;
private PlayerHaterListener mPlayerHaterListener;
private OnAudioFocusChangeListener mAudioFocusChangeListener;
+
+
+ private String mNotificationTitle = "PlayerHater";
+ private String mNotificationText = "Version 0.0.1";
+
+ private boolean mAutoNotify = true;
private Bundle mBundle;
-
+
private boolean playAfterSeek;
private Handler mHandler = new Handler() {
@@ -119,6 +125,7 @@ public IBinder onBind(Intent arg0) {
public boolean pause() throws IllegalStateException {
mediaPlayer.pause();
+ stopForeground(true);
sendIsPaused();
return true;
}
@@ -180,6 +187,8 @@ public boolean play() throws IllegalStateException, IOException {
case MediaPlayerWrapper.PAUSED:
mediaPlayer.start();
sendIsPlaying();
+ if (mAutoNotify)
+ startForeground(12314, buildNotification("Playing..."));
break;
default:
throw new IllegalStateException();
@@ -237,36 +246,37 @@ private void performPrepare() {
public boolean stop() {
mediaPlayer.stop();
+ stopForeground(true);
sendIsStopped();
return true;
}
-
+
/*
* THE BUNDLE
*/
-
+
public Bundle getBundle() {
return mBundle;
}
public void commitBundle(Bundle icicle) {
mBundle = icicle;
}
-
+
/*
* We proxy these events.
*/
public void setOnSeekCompleteListener(OnSeekCompleteListener listener) {
mOnSeekCompleteListener = listener;
}
-
+
@Override
public void onSeekComplete(MediaPlayer mp) {
if (playAfterSeek) {
try {
play();
} catch (Exception e) {
- //oof.
+ // oof.
}
}
if (mOnSeekCompleteListener != null)
@@ -276,12 +286,14 @@ public void onSeekComplete(MediaPlayer mp) {
public void setOnPreparedListener(OnPreparedListener listener) {
mOnPreparedListener = listener;
}
-
+
@Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "MediaPlayer is prepared, beginning playback of "
+ getNowPlaying());
mediaPlayer.start();
+ if (mAutoNotify)
+ startForeground(12314, buildNotification("Playing..."));
sendIsPlaying();
mAudioManager.requestAudioFocus(mAudioFocusChangeListener,
@@ -296,7 +308,7 @@ public void onPrepared(MediaPlayer mp) {
public void setOnErrorListener(OnErrorListener listener) {
mOnErrorListener = listener;
}
-
+
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e(TAG, "Got MediaPlayer error: " + what + " / " + extra);
@@ -330,17 +342,19 @@ protected Notification buildNotification(String text) {
}
protected Notification buildNotification(String text, int pendingFlag) {
- Notification notification = new Notification(mNotificationIcon, text,
- System.currentTimeMillis());
-
- if (mNotificationIntentClass != null && mNotificationView != null) {
+ Notification notification = new Notification(mNotificationIcon,
+ "PlayerHater", 0);
+ notification.setLatestEventInfo(getApplicationContext(), mNotificationTitle,
+ mNotificationText, null);
+ if (mNotificationView != null) {
notification.contentView = mNotificationView;
- notification.contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, mNotificationIntentClass), pendingFlag);
- notification.flags |= Notification.FLAG_ONGOING_EVENT;
- notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
}
-
+ if (mNotificationIntentClass != null) {
+ notification.contentIntent = PendingIntent.getActivity(
+ getApplicationContext(), 777, new Intent(
+ getApplicationContext(), mNotificationIntentClass),
+ PendingIntent.FLAG_UPDATE_CURRENT);
+ }
return notification;
}
@@ -382,7 +396,7 @@ private static PlayerListenerManager createPlayerListenerManager(
*/
protected void onHandlerMessage(Message m) { /* noop */
}
-
+
/*
* These are the events we send back to PlayerHaterListener;
*/
@@ -418,7 +432,7 @@ private void sendIsStopped() {
public void setListener(PlayerHaterListener listener) {
mPlayerHaterListener = listener;
}
-
+
public void duck() {
Log.d(TAG, "Ducking...");
mediaPlayer.setVolume(0.1f, 0.1f);
@@ -429,4 +443,33 @@ public void unduck() {
mediaPlayer.setVolume(1.0f, 1.0f);
}
+ public void setNotificationIcon(int notificationIcon) {
+ mNotificationIcon = notificationIcon;
+ }
+
+ public void setAutoNotify(boolean autoNotify) {
+ mAutoNotify = autoNotify;
+ }
+
+ public void doStartForeground() {
+ if (!mAutoNotify) {
+ startForeground(2394827, buildNotification());
+ } else {
+ Log.e(TAG,
+ "startForeground() was called, but set to do this automatically. Ignoring request.");
+ }
+ }
+
+ public void doStopForeground() {
+ stopForeground(true);
+ }
+
+ public void setNotificationTitle(String notificationTitle) {
+ mNotificationTitle = notificationTitle;
+ }
+
+ public void setNotificationText(String notificationText) {
+ mNotificationText = notificationText;
+ }
+
}
@@ -0,0 +1,28 @@
+package org.prx.android.playerhater;
+
+import android.content.Context;
+import android.graphics.Color;
+import android.widget.ImageView;
+import android.widget.RelativeLayout;
+
+public class PlayerNotificationLayout extends RelativeLayout {
+
+ public ImageView icon;
+
+ public PlayerNotificationLayout(Context context) {
+ super(context);
+ setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
+ addView(icon = makeIcon(context));
+ }
+
+ private static ImageView makeIcon(Context context) {
+ ImageView icon = new ImageView(context);
+ LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
+ params.alignWithParent = true;
+ params.rightMargin = 10;
+ icon.setBackgroundColor(Color.BLUE);
+ icon.setLayoutParams(params);
+ return icon;
+ }
+
+}
@@ -4,7 +4,6 @@
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
-import android.util.Log;
import android.media.MediaPlayer.OnCompletionListener;
public class TransientPlayer {

0 comments on commit 8d2183c

Please sign in to comment.