Skip to content
This repository
Browse code

custom notification with buttons -- only partially done

  • Loading branch information...
commit 02d80d3e90c2950cba837a335ad6f2753bef781c 1 parent a470a2b
Rebecca Nesson authored
2  .project
... ...
@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <projectDescription>
3  
-	<name>PlayerHater</name>
  3
+	<name>tal2_PlayerHater</name>
4 4
 	<comment></comment>
5 5
 	<projects>
6 6
 	</projects>
BIN  libs/NotificationCompat2-1.1.2.jar
Binary file not shown
2  project.properties
@@ -8,5 +8,5 @@
8 8
 # project structure.
9 9
 
10 10
 # Project target.
11  
-target=android-8
  11
+target=android-14
12 12
 android.library=true
BIN  res/drawable-hdpi/av_pause.png
BIN  res/drawable-hdpi/av_play.png
38  res/layout/notification.xml
... ...
@@ -0,0 +1,38 @@
  1
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2
+    android:layout_width="fill_parent"
  3
+    android:layout_height="fill_parent" >
  4
+
  5
+    <ImageView
  6
+        android:id="@+id/notImage"
  7
+        android:layout_width="wrap_content"
  8
+        android:layout_height="wrap_content"
  9
+        android:layout_alignParentTop="true"
  10
+        android:layout_marginRight="10dp" />
  11
+    
  12
+    <ImageView
  13
+        android:id="@+id/notPlayPause"
  14
+        android:layout_width="wrap_content"
  15
+        android:layout_height="fill_parent"
  16
+        android:src="@drawable/av_pause"
  17
+        android:layout_alignParentRight="true"
  18
+        android:padding="3dp" />
  19
+
  20
+    <TextView
  21
+        android:id="@+id/notContentTitle"
  22
+        style="@android:style/TextAppearance.Medium"
  23
+        android:layout_width="wrap_content"
  24
+        android:layout_height="wrap_content"
  25
+        android:layout_toRightOf="@id/notImage"
  26
+        android:layout_toLeftOf="@id/notPlayPause"
  27
+        android:textStyle="bold" />
  28
+
  29
+    <TextView
  30
+        android:id="@+id/notContentText"
  31
+        style="@android:style/TextAppearance.Small"
  32
+        android:layout_width="wrap_content"
  33
+        android:layout_height="wrap_content"
  34
+        android:layout_toRightOf="@id/notImage"
  35
+        android:layout_below="@id/notContentTitle"
  36
+        android:layout_toLeftOf="@id/notPlayPause" />
  37
+
  38
+</RelativeLayout>
17  src/org/prx/android/playerhater/BroadcastReceiver.java
@@ -2,6 +2,7 @@
2 2
 
3 3
 import java.io.IOException;
4 4
 
  5
+import android.annotation.SuppressLint;
5 6
 import android.content.Context;
6 7
 import android.content.Intent;
7 8
 import android.media.AudioManager;
@@ -28,15 +29,16 @@ public void onReceive(Context context, Intent intent) {
28 29
 		BroadcastReceiver.doReceive(context, intent); 
29 30
 	}
30 31
 	
  32
+	@SuppressLint("NewApi")
31 33
 	public static void doReceive(Context context, Intent intent) {
32 34
 		Log.d(TAG, "Received"); 
33  
-		if (mService.isPlaying() && 
  35
+		if (mService.isPlaying() && intent.getAction() != null && 
34 36
 				(intent.getAction().equals(Intent.ACTION_HEADSET_PLUG) ||
35 37
 			     intent.getAction().equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY))
36 38
 				&& intent.getIntExtra("state", 0) == 0) {
37 39
 			mService.pause();
38 40
 		}
39  
-		if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) { 
  41
+		if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) { 
40 42
             KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
41 43
             Log.d(TAG, "Key event is " + event); 
42 44
             if (event.getAction() == KeyEvent.ACTION_DOWN) { return; }
@@ -57,6 +59,17 @@ public static void doReceive(Context context, Intent intent) {
57 59
             	}
58 60
             }
59 61
 		}
  62
+		if (intent.getAction() != null && intent.getAction().equals(NotificationHandler.PLAY_PAUSE_ACTION)) { 
  63
+			try { 
  64
+				if (mService.isPlaying()) { 
  65
+					mService.pause(); 
  66
+				} else { 
  67
+					mService.play();
  68
+				}
  69
+			} catch (IOException e) { 
  70
+				e.printStackTrace(); 
  71
+			}
  72
+		}
60 73
 	}
61 74
 
62 75
 }
39  src/org/prx/android/playerhater/NotificationHandler.java
... ...
@@ -1,9 +1,12 @@
1 1
 package org.prx.android.playerhater;
2 2
 
  3
+import com.jakewharton.notificationcompat2.NotificationCompat2;
  4
+
3 5
 import android.app.Activity;
4 6
 import android.app.Notification;
5 7
 import android.app.PendingIntent;
6 8
 import android.content.Intent;
  9
+import android.graphics.BitmapFactory;
7 10
 import android.os.Bundle;
8 11
 import android.os.Parcelable;
9 12
 import android.widget.RemoteViews;
@@ -12,6 +15,10 @@
12 15
 	
13 16
 	protected static final int NOTIFICATION_NU = 9747245;
14 17
 	
  18
+	public static final int PLAY_PAUSE_CLICK_ID = 845832; 
  19
+	public static final String PLAY_PAUSE_ACTION = "org.prx.playerhater.PlayPause"; 
  20
+	
  21
+	
15 22
 	private boolean notificationIsVisible = false;
16 23
 	
17 24
 	private String mNotificationTitle = "PlayerHater";
@@ -63,21 +70,37 @@ public void setView(RemoteViews remoteViews) {
63 70
 		updateCurrentNotification();
64 71
 	}
65 72
 
66  
-
67 73
 	public void setNotificationIcon(int notificationIcon) {
68 74
 		mNotificationIcon = notificationIcon;
69 75
 		updateCurrentNotification();
70 76
 	}
71 77
 	
72 78
 	private Notification getNotification() {
73  
-		if (mNotification == null) {
74  
-			mNotification = new Notification(mNotificationIcon, mNotificationTitle, 0);
75  
-		}
76  
-		if (mNotificationView != null) {
77  
-			mNotification.contentView = mNotificationView;
78  
-		} else {
79  
-			mNotification.setLatestEventInfo(mService.getApplicationContext(), mNotificationTitle, mNotificationText, mContentIntent);
  79
+		//Notification and intent of the notification 
  80
+		Notification mNotification = new NotificationCompat2.Builder(mService.getApplicationContext())
  81
+        			.setContentTitle(mNotificationTitle)
  82
+        			.setContentText(mNotificationText)
  83
+        			.setContentIntent(mContentIntent)
  84
+        			.setSmallIcon(mNotificationIcon)
  85
+        			.setLargeIcon(BitmapFactory.decodeResource(mService.getResources(),mNotificationIcon))
  86
+        			.build();
  87
+
  88
+		Intent intent = new Intent(mService.getApplicationContext(), BroadcastReceiver.class);
  89
+		intent.setAction(NotificationHandler.PLAY_PAUSE_ACTION); 
  90
+		PendingIntent playPausePendingIntent = PendingIntent.getBroadcast(mService.getApplicationContext(), 
  91
+				NotificationHandler.PLAY_PAUSE_CLICK_ID, intent, 0);
  92
+		
  93
+		//Remoteview and intent for my button
  94
+		RemoteViews notificationView = new RemoteViews(mService.getBaseContext().getPackageName(), R.layout.notification);
  95
+		
  96
+		if (notificationView != null) { 
  97
+			notificationView.setOnClickPendingIntent(R.id.notPlayPause, playPausePendingIntent);
  98
+			notificationView.setTextViewText(R.id.notContentTitle, mNotificationTitle); 
  99
+			notificationView.setTextViewText(R.id.notContentText, mNotificationText); 
  100
+			notificationView.setImageViewResource(R.id.notImage, mNotificationIcon); 
  101
+			mNotification.contentView = notificationView;
80 102
 		}
  103
+
81 104
 		return mNotification;
82 105
 	}
83 106
 
5  src/org/prx/android/playerhater/PlaybackService.java
@@ -3,6 +3,8 @@
3 3
 import java.io.FileDescriptor;
4 4
 import java.io.IOException;
5 5
 
  6
+import android.annotation.SuppressLint;
  7
+import android.annotation.TargetApi;
6 8
 import android.app.Activity;
7 9
 import android.app.PendingIntent;
8 10
 import android.app.Service;
@@ -28,6 +30,7 @@
28 30
 import android.util.Log;
29 31
 import android.widget.RemoteViews;
30 32
 
  33
+@SuppressLint("NewApi")
31 34
 public class PlaybackService extends Service implements OnErrorListener,
32 35
 		OnPreparedListener, OnSeekCompleteListener, OnCompletionListener {
33 36
 
@@ -82,6 +85,7 @@ public void handleMessage(Message m) {
82 85
 		}
83 86
 	};
84 87
 
  88
+	@SuppressLint("NewApi")
85 89
 	@Override
86 90
 	public void onStart(Intent intent, int startId) {
87 91
 		super.onStart(intent, startId);
@@ -193,6 +197,7 @@ public void setNotificationIntentActivity(Activity activity) {
193 197
 	public void setNotificationView(int view) {
194 198
 		mNotificationHandler.setView(new RemoteViews(getPackageName(), view));
195 199
 	}
  200
+	
196 201
 
197 202
 	public int getState() {
198 203
 		return mediaPlayer.getState();
1  src/org/prx/android/playerhater/PlayerHaterBinder.java
@@ -301,4 +301,5 @@ public void setLockScreenImage(Resources res, int id) {
301 301
 	public void setLockScreenTitle(String title) {
302 302
 		mService.setLockScreenTitle(title); 
303 303
 	}
  304
+
304 305
 }

0 notes on commit 02d80d3

Please sign in to comment.
Something went wrong with that request. Please try again.