Skip to content

Commit

Permalink
notigfications
Browse files Browse the repository at this point in the history
  • Loading branch information
cqr committed Mar 1, 2012
1 parent 56ff5d9 commit 8d2183c
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 23 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions .classpath
@@ -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>
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Expand Up @@ -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>
Expand Down
7 changes: 7 additions & 0 deletions src/org/prx/android/playerhater/PlayerHater.java
Expand Up @@ -40,7 +40,14 @@ public interface PlayerHater {
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();
Expand Down
30 changes: 30 additions & 0 deletions src/org/prx/android/playerhater/PlayerHaterBinder.java
Expand Up @@ -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);
}
}
81 changes: 62 additions & 19 deletions src/org/prx/android/playerhater/PlayerHaterService.java
Expand Up @@ -53,9 +53,15 @@ public class PlayerHaterService extends Service implements OnErrorListener,
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() {
Expand Down Expand Up @@ -119,6 +125,7 @@ public IBinder onBind(Intent arg0) {

public boolean pause() throws IllegalStateException {
mediaPlayer.pause();
stopForeground(true);
sendIsPaused();
return true;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -382,7 +396,7 @@ private static PlayerListenerManager createPlayerListenerManager(
*/
protected void onHandlerMessage(Message m) { /* noop */
}

/*
* These are the events we send back to PlayerHaterListener;
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

}
28 changes: 28 additions & 0 deletions src/org/prx/android/playerhater/PlayerNotificationLayout.java
@@ -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;
}

}
1 change: 0 additions & 1 deletion src/org/prx/android/playerhater/TransientPlayer.java
Expand Up @@ -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 {
Expand Down

0 comments on commit 8d2183c

Please sign in to comment.