Browse files

custom notification with buttons -- only partially done

  • Loading branch information...
1 parent a470a2b commit 02d80d3e90c2950cba837a335ad6f2753bef781c @rebeccanesson rebeccanesson committed Nov 2, 2012
View
2 .project
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
- <name>PlayerHater</name>
+ <name>tal2_PlayerHater</name>
<comment></comment>
<projects>
</projects>
View
BIN libs/NotificationCompat2-1.1.2.jar
Binary file not shown.
View
2 project.properties
@@ -8,5 +8,5 @@
# project structure.
# Project target.
-target=android-8
+target=android-14
android.library=true
View
BIN res/drawable-hdpi/av_pause.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-hdpi/av_play.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
38 res/layout/notification.xml
@@ -0,0 +1,38 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent" >
+
+ <ImageView
+ android:id="@+id/notImage"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentTop="true"
+ android:layout_marginRight="10dp" />
+
+ <ImageView
+ android:id="@+id/notPlayPause"
+ android:layout_width="wrap_content"
+ android:layout_height="fill_parent"
+ android:src="@drawable/av_pause"
+ android:layout_alignParentRight="true"
+ android:padding="3dp" />
+
+ <TextView
+ android:id="@+id/notContentTitle"
+ style="@android:style/TextAppearance.Medium"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_toRightOf="@id/notImage"
+ android:layout_toLeftOf="@id/notPlayPause"
+ android:textStyle="bold" />
+
+ <TextView
+ android:id="@+id/notContentText"
+ style="@android:style/TextAppearance.Small"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_toRightOf="@id/notImage"
+ android:layout_below="@id/notContentTitle"
+ android:layout_toLeftOf="@id/notPlayPause" />
+
+</RelativeLayout>
View
17 src/org/prx/android/playerhater/BroadcastReceiver.java
@@ -2,6 +2,7 @@
import java.io.IOException;
+import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
@@ -28,15 +29,16 @@ public void onReceive(Context context, Intent intent) {
BroadcastReceiver.doReceive(context, intent);
}
+ @SuppressLint("NewApi")
public static void doReceive(Context context, Intent intent) {
Log.d(TAG, "Received");
- if (mService.isPlaying() &&
+ if (mService.isPlaying() && intent.getAction() != null &&
(intent.getAction().equals(Intent.ACTION_HEADSET_PLUG) ||
intent.getAction().equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY))
&& intent.getIntExtra("state", 0) == 0) {
mService.pause();
}
- if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
+ if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
Log.d(TAG, "Key event is " + event);
if (event.getAction() == KeyEvent.ACTION_DOWN) { return; }
@@ -57,6 +59,17 @@ public static void doReceive(Context context, Intent intent) {
}
}
}
+ if (intent.getAction() != null && intent.getAction().equals(NotificationHandler.PLAY_PAUSE_ACTION)) {
+ try {
+ if (mService.isPlaying()) {
+ mService.pause();
+ } else {
+ mService.play();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
View
39 src/org/prx/android/playerhater/NotificationHandler.java
@@ -1,9 +1,12 @@
package org.prx.android.playerhater;
+import com.jakewharton.notificationcompat2.NotificationCompat2;
+
import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
+import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.RemoteViews;
@@ -12,6 +15,10 @@
protected static final int NOTIFICATION_NU = 9747245;
+ public static final int PLAY_PAUSE_CLICK_ID = 845832;
+ public static final String PLAY_PAUSE_ACTION = "org.prx.playerhater.PlayPause";
+
+
private boolean notificationIsVisible = false;
private String mNotificationTitle = "PlayerHater";
@@ -63,21 +70,37 @@ public void setView(RemoteViews remoteViews) {
updateCurrentNotification();
}
-
public void setNotificationIcon(int notificationIcon) {
mNotificationIcon = notificationIcon;
updateCurrentNotification();
}
private Notification getNotification() {
- if (mNotification == null) {
- mNotification = new Notification(mNotificationIcon, mNotificationTitle, 0);
- }
- if (mNotificationView != null) {
- mNotification.contentView = mNotificationView;
- } else {
- mNotification.setLatestEventInfo(mService.getApplicationContext(), mNotificationTitle, mNotificationText, mContentIntent);
+ //Notification and intent of the notification
+ Notification mNotification = new NotificationCompat2.Builder(mService.getApplicationContext())
+ .setContentTitle(mNotificationTitle)
+ .setContentText(mNotificationText)
+ .setContentIntent(mContentIntent)
+ .setSmallIcon(mNotificationIcon)
+ .setLargeIcon(BitmapFactory.decodeResource(mService.getResources(),mNotificationIcon))
+ .build();
+
+ Intent intent = new Intent(mService.getApplicationContext(), BroadcastReceiver.class);
+ intent.setAction(NotificationHandler.PLAY_PAUSE_ACTION);
+ PendingIntent playPausePendingIntent = PendingIntent.getBroadcast(mService.getApplicationContext(),
+ NotificationHandler.PLAY_PAUSE_CLICK_ID, intent, 0);
+
+ //Remoteview and intent for my button
+ RemoteViews notificationView = new RemoteViews(mService.getBaseContext().getPackageName(), R.layout.notification);
+
+ if (notificationView != null) {
+ notificationView.setOnClickPendingIntent(R.id.notPlayPause, playPausePendingIntent);
+ notificationView.setTextViewText(R.id.notContentTitle, mNotificationTitle);
+ notificationView.setTextViewText(R.id.notContentText, mNotificationText);
+ notificationView.setImageViewResource(R.id.notImage, mNotificationIcon);
+ mNotification.contentView = notificationView;
}
+
return mNotification;
}
View
5 src/org/prx/android/playerhater/PlaybackService.java
@@ -3,6 +3,8 @@
import java.io.FileDescriptor;
import java.io.IOException;
+import android.annotation.SuppressLint;
+import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
@@ -28,6 +30,7 @@
import android.util.Log;
import android.widget.RemoteViews;
+@SuppressLint("NewApi")
public class PlaybackService extends Service implements OnErrorListener,
OnPreparedListener, OnSeekCompleteListener, OnCompletionListener {
@@ -82,6 +85,7 @@ public void handleMessage(Message m) {
}
};
+ @SuppressLint("NewApi")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
@@ -193,6 +197,7 @@ public void setNotificationIntentActivity(Activity activity) {
public void setNotificationView(int view) {
mNotificationHandler.setView(new RemoteViews(getPackageName(), view));
}
+
public int getState() {
return mediaPlayer.getState();
View
1 src/org/prx/android/playerhater/PlayerHaterBinder.java
@@ -301,4 +301,5 @@ public void setLockScreenImage(Resources res, int id) {
public void setLockScreenTitle(String title) {
mService.setLockScreenTitle(title);
}
+
}

0 comments on commit 02d80d3

Please sign in to comment.