Skip to content

Commit

Permalink
Merge pull request #114 from brianwernick/issue_110
Browse files Browse the repository at this point in the history
Fixed Issue #110
  • Loading branch information
brianwernick committed Feb 20, 2016
2 parents 4f45881 + 57f39a2 commit bcb2fce
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
Expand All @@ -40,40 +42,37 @@ public class EMLockScreen {
public static final String SESSION_TAG = "EMLockScreen.Session";
public static final String RECEIVER_EXTRA_CLASS = "com.devbrackets.android.exomedia.RECEIVER_EXTRA_CLASS";

@NonNull
private Context context;
private Class<? extends Service> mediaServiceClass;

private boolean showLockScreen = true;

@Nullable
private Bitmap appIconBitmap;
@Nullable
private MediaSessionCompat mediaSession;

private boolean showLockScreen = true;

/**
* Creates a new EMLockScreen object
*
* @param context The context to use for holding a MediaSession and sending action intents
* @param mediaServiceClass The class for the service that owns the backing MediaService and to notify of playback actions
*/
public EMLockScreen(Context context, Class<? extends Service> mediaServiceClass) {
public EMLockScreen(@NonNull Context context, @NonNull Class<? extends Service> mediaServiceClass) {
this.context = context;
this.mediaServiceClass = mediaServiceClass;

ComponentName componentName = new ComponentName(context, MediaControlsReceiver.class.getName());

mediaSession = new MediaSessionCompat(context, SESSION_TAG, componentName, getMediaButtonReceiverPendingIntent(componentName));
mediaSession = new MediaSessionCompat(context, SESSION_TAG, componentName, getMediaButtonReceiverPendingIntent(componentName, mediaServiceClass));
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(new SessionCallback());
mediaSession.setCallback(new SessionCallback(mediaServiceClass));
}

public void release() {
if (mediaSession != null) {
mediaSession.release();
}

context = null;
appIconBitmap = null;
mediaServiceClass = null;
mediaServiceClass = null;
}

/**
Expand All @@ -92,7 +91,7 @@ public void setLockScreenEnabled(boolean enabled) {
showLockScreen = enabled;

//Remove the lock screen when disabling
if (!enabled) {
if (!enabled && mediaSession != null) {
mediaSession.setActive(false);
}
}
Expand Down Expand Up @@ -135,8 +134,9 @@ public void updateLockScreenInformation(String title, String album, String artis
metaDataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, mediaArtwork);
}

mediaSession.setMetadata(metaDataBuilder.build());

if (mediaSession != null) {
mediaSession.setMetadata(metaDataBuilder.build());
}

//Updates the available playback controls
PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder();
Expand All @@ -151,11 +151,11 @@ public void updateLockScreenInformation(String title, String album, String artis
}
}

private PendingIntent getMediaButtonReceiverPendingIntent(ComponentName componentName) {
private PendingIntent getMediaButtonReceiverPendingIntent(ComponentName componentName, @NonNull Class<? extends Service> serviceClass) {
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(componentName);

mediaButtonIntent.putExtra(RECEIVER_EXTRA_CLASS, mediaServiceClass.getName());
mediaButtonIntent.putExtra(RECEIVER_EXTRA_CLASS, serviceClass.getName());
return PendingIntent.getBroadcast(context, 0, mediaButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
}

Expand Down Expand Up @@ -192,7 +192,7 @@ private long getPlaybackOptions(EMNotification.NotificationMediaState mediaState
* @param serviceClass The service class to notify of intents
* @return The resulting PendingIntent
*/
private PendingIntent createPendingIntent(String action, Class<? extends Service> serviceClass) {
private PendingIntent createPendingIntent(@NonNull String action, @NonNull Class<? extends Service> serviceClass) {
Intent intent = new Intent(context, serviceClass);
intent.setAction(action);

Expand All @@ -201,17 +201,17 @@ private PendingIntent createPendingIntent(String action, Class<? extends Service

/**
* A simple callback class to listen to the notifications received from the lock screen
* and forward them to the {@link #mediaServiceClass}
* and forward them to the specified Class
*/
private class SessionCallback extends MediaSessionCompat.Callback {
private PendingIntent playPausePendingIntent, nextPendingIntent, previousPendingIntent;

public SessionCallback() {
public SessionCallback(@NonNull Class<? extends Service> serviceClass) {
super();

playPausePendingIntent = createPendingIntent(EMRemoteActions.ACTION_PLAY_PAUSE, mediaServiceClass);
nextPendingIntent = createPendingIntent(EMRemoteActions.ACTION_NEXT, mediaServiceClass);
previousPendingIntent = createPendingIntent(EMRemoteActions.ACTION_PREVIOUS, mediaServiceClass);
playPausePendingIntent = createPendingIntent(EMRemoteActions.ACTION_PLAY_PAUSE, serviceClass);
nextPendingIntent = createPendingIntent(EMRemoteActions.ACTION_NEXT, serviceClass);
previousPendingIntent = createPendingIntent(EMRemoteActions.ACTION_PREVIOUS, serviceClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.view.View;
Expand All @@ -36,16 +37,17 @@
* media playback applications.
*/
public class EMNotification {
@NonNull
private Context context;
private NotificationManager notificationManager;
@NonNull
private NotificationInfo notificationInfo = new NotificationInfo();

@Nullable
private NotificationManager notificationManager;
@Nullable
private Class<? extends Service> mediaServiceClass;

private RemoteViews customNotification;
private RemoteViews bigContent;

public EMNotification(Context context) {
public EMNotification(@NonNull Context context) {
this.context = context;
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
Expand All @@ -58,17 +60,14 @@ public void release() {
dismiss();

mediaServiceClass = null;
customNotification = null;
bigContent = null;

notificationInfo.clean();
}

/**
* Dismisses the current active notification
*/
public void dismiss() {
if (notificationManager != null && notificationInfo != null) {
if (notificationManager != null) {
notificationManager.cancel(notificationInfo.getNotificationId());
}
}
Expand All @@ -78,7 +77,7 @@ public void dismiss() {
* ready for playback (e.g. paused). The notification information
* will need to be updated by calling {@link #setNotificationBaseInformation(int, int)}
* and {@link #updateNotificationInformation(String, String, String, Bitmap, Bitmap)} and can be retrieved
* with {@link #getNotification(android.app.PendingIntent)}
* with {@link #getNotification(android.app.PendingIntent, Class)}
*
* @param enabled True if notifications should be shown
*/
Expand All @@ -90,7 +89,7 @@ public void setNotificationsEnabled(boolean enabled) {
notificationInfo.setShowNotifications(enabled);

//Remove the notification when disabling
if (!enabled) {
if (!enabled && notificationManager != null) {
notificationManager.cancel(notificationInfo.getNotificationId());
}
}
Expand Down Expand Up @@ -171,8 +170,8 @@ public void updateNotificationInformation(@Nullable String title, @Nullable Stri
notificationInfo.setSecondaryImage(secondaryNotificationImage);
notificationInfo.setMediaState(notificationMediaState);

if (notificationInfo.getShowNotifications()) {
notificationManager.notify(notificationInfo.getNotificationId(), getNotification(notificationInfo.getPendingIntent()));
if (notificationInfo.getShowNotifications() && notificationManager != null && mediaServiceClass != null) {
notificationManager.notify(notificationInfo.getNotificationId(), getNotification(notificationInfo.getPendingIntent(), mediaServiceClass));
}
}

Expand All @@ -185,16 +184,16 @@ public void updateNotificationInformation(@Nullable String title, @Nullable Stri
* @return The constructed notification
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public Notification getNotification(@Nullable PendingIntent pendingIntent) {
public Notification getNotification(@Nullable PendingIntent pendingIntent, @NonNull Class<? extends Service> serviceClass) {
setClickPendingIntent(pendingIntent);
RemoteViews customNotificationViews = getCustomNotification();
RemoteViews customNotificationViews = getCustomNotification(serviceClass);

boolean allowSwipe = notificationInfo.getMediaState() == null || !notificationInfo.getMediaState().isPlaying();

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContent(customNotificationViews);
builder.setContentIntent(pendingIntent);
builder.setDeleteIntent(createPendingIntent(EMRemoteActions.ACTION_STOP, mediaServiceClass));
builder.setDeleteIntent(createPendingIntent(EMRemoteActions.ACTION_STOP, serviceClass));
builder.setSmallIcon(notificationInfo.getAppIcon());
builder.setAutoCancel(allowSwipe);
builder.setOngoing(!allowSwipe);
Expand All @@ -212,7 +211,7 @@ public Notification getNotification(@Nullable PendingIntent pendingIntent) {
//Build the notification and set the expanded content view if there is a service to inform of clicks
Notification notification = builder.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && mediaServiceClass != null) {
notification.bigContentView = getBigNotification();
notification.bigContentView = getBigNotification(serviceClass);
notification.bigContentView.setOnClickPendingIntent(R.id.exomedia_big_notification_touch_area, pendingIntent);
}

Expand All @@ -224,12 +223,12 @@ public Notification getNotification(@Nullable PendingIntent pendingIntent) {
*
* @return The resulting RemoteViews
*/
private RemoteViews getCustomNotification() {
customNotification = new RemoteViews(context.getPackageName(), R.layout.exomedia_notification_content);
private RemoteViews getCustomNotification(@NonNull Class<? extends Service> serviceClass) {
RemoteViews customNotification = new RemoteViews(context.getPackageName(), R.layout.exomedia_notification_content);

customNotification.setOnClickPendingIntent(R.id.exomedia_notification_playpause, createPendingIntent(EMRemoteActions.ACTION_PLAY_PAUSE, mediaServiceClass));
customNotification.setOnClickPendingIntent(R.id.exomedia_notification_next, createPendingIntent(EMRemoteActions.ACTION_NEXT, mediaServiceClass));
customNotification.setOnClickPendingIntent(R.id.exomedia_notification_prev, createPendingIntent(EMRemoteActions.ACTION_PREVIOUS, mediaServiceClass));
customNotification.setOnClickPendingIntent(R.id.exomedia_notification_playpause, createPendingIntent(EMRemoteActions.ACTION_PLAY_PAUSE, serviceClass));
customNotification.setOnClickPendingIntent(R.id.exomedia_notification_next, createPendingIntent(EMRemoteActions.ACTION_NEXT, serviceClass));
customNotification.setOnClickPendingIntent(R.id.exomedia_notification_prev, createPendingIntent(EMRemoteActions.ACTION_PREVIOUS, serviceClass));

customNotification.setTextViewText(R.id.exomedia_notification_title, notificationInfo.getTitle());
customNotification.setTextViewText(R.id.exomedia_notification_album, notificationInfo.getAlbum());
Expand All @@ -250,13 +249,13 @@ private RemoteViews getCustomNotification() {
*
* @return The resulting RemoteViews
*/
private RemoteViews getBigNotification() {
bigContent = new RemoteViews(context.getPackageName(), R.layout.exomedia_big_notification_content);
private RemoteViews getBigNotification(@NonNull Class<? extends Service> serviceClass) {
RemoteViews bigContent = new RemoteViews(context.getPackageName(), R.layout.exomedia_big_notification_content);

bigContent.setOnClickPendingIntent(R.id.exomedia_big_notification_close, createPendingIntent(EMRemoteActions.ACTION_STOP, mediaServiceClass));
bigContent.setOnClickPendingIntent(R.id.exomedia_big_notification_playpause, createPendingIntent(EMRemoteActions.ACTION_PLAY_PAUSE, mediaServiceClass));
bigContent.setOnClickPendingIntent(R.id.exomedia_big_notification_next, createPendingIntent(EMRemoteActions.ACTION_NEXT, mediaServiceClass));
bigContent.setOnClickPendingIntent(R.id.exomedia_big_notification_prev, createPendingIntent(EMRemoteActions.ACTION_PREVIOUS, mediaServiceClass));
bigContent.setOnClickPendingIntent(R.id.exomedia_big_notification_close, createPendingIntent(EMRemoteActions.ACTION_STOP, serviceClass));
bigContent.setOnClickPendingIntent(R.id.exomedia_big_notification_playpause, createPendingIntent(EMRemoteActions.ACTION_PLAY_PAUSE, serviceClass));
bigContent.setOnClickPendingIntent(R.id.exomedia_big_notification_next, createPendingIntent(EMRemoteActions.ACTION_NEXT, serviceClass));
bigContent.setOnClickPendingIntent(R.id.exomedia_big_notification_prev, createPendingIntent(EMRemoteActions.ACTION_PREVIOUS, serviceClass));

bigContent.setTextViewText(R.id.exomedia_big_notification_title, notificationInfo.getTitle());
bigContent.setTextViewText(R.id.exomedia_big_notification_album, notificationInfo.getAlbum());
Expand Down Expand Up @@ -313,7 +312,7 @@ private void updateBigNotificationMediaState(RemoteViews bigContent) {
* @param serviceClass The service class to notify of intents
* @return The resulting PendingIntent
*/
private PendingIntent createPendingIntent(String action, Class<? extends Service> serviceClass) {
private PendingIntent createPendingIntent(String action, @NonNull Class<? extends Service> serviceClass) {
Intent intent = new Intent(context, serviceClass);
intent.setAction(action);

Expand Down
Loading

0 comments on commit bcb2fce

Please sign in to comment.