From 41b81ce14c328e5a8346afaa11f5b2a6d1e52ed1 Mon Sep 17 00:00:00 2001 From: Lincoln Date: Mon, 14 Aug 2017 23:04:59 -0400 Subject: [PATCH] Swipeable classic notification (#244) * Made notification swipeable when not playing not swipeable when playing. Removed "X" close button * Fixed music stop on tap and on app close * Refactor notification classes --- .../notification/PlayingNotification.java | 54 ++++++++++++++++--- .../notification/PlayingNotificationImpl.java | 36 +++---------- .../PlayingNotificationImpl24.java | 54 +------------------ app/src/main/res/layout/notification_big.xml | 14 ----- 4 files changed, 54 insertions(+), 104 deletions(-) diff --git a/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotification.java b/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotification.java index 858c8486..6228c68f 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotification.java +++ b/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotification.java @@ -1,17 +1,55 @@ package com.kabouzeid.gramophone.service.notification; +import android.app.Notification; +import android.app.NotificationManager; + import com.kabouzeid.gramophone.service.MusicService; -/** - * @author Karim Abou Zeid (kabouzeid) - */ +import static android.content.Context.NOTIFICATION_SERVICE; -public interface PlayingNotification { - int NOTIFICATION_ID = 1; +public abstract class PlayingNotification { - void init(MusicService service); + private static final int NOTIFICATION_ID = 1; + private static final int NOTIFY_MODE_FOREGROUND = 1; + private static final int NOTIFY_MODE_BACKGROUND = 0; - void update(); + private int notifyMode = NOTIFY_MODE_BACKGROUND; - void stop(); + private NotificationManager notificationManager; + protected MusicService service; + boolean stopped; + + public synchronized void init(MusicService service) { + this.service = service; + notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE); + } + + abstract public void update(); + + public synchronized void stop() { + stopped = true; + service.stopForeground(true); + notificationManager.cancel(NOTIFICATION_ID); + } + + void updateNotifyModeAndPostNotification(Notification notification) { + int newNotifyMode; + if (service.isPlaying()) { + newNotifyMode = NOTIFY_MODE_FOREGROUND; + } else { + newNotifyMode = NOTIFY_MODE_BACKGROUND; + } + + if (notifyMode != newNotifyMode && newNotifyMode == NOTIFY_MODE_BACKGROUND) { + service.stopForeground(false); + } + + if (newNotifyMode == NOTIFY_MODE_FOREGROUND) { + service.startForeground(NOTIFICATION_ID, notification); + } else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) { + notificationManager.notify(NOTIFICATION_ID, notification); + } + + notifyMode = newNotifyMode; + } } diff --git a/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotificationImpl.java b/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotificationImpl.java index 3e156c96..843affc6 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotificationImpl.java +++ b/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotificationImpl.java @@ -1,9 +1,5 @@ package com.kabouzeid.gramophone.service.notification; -/** - * @author Karim Abou Zeid (kabouzeid) - */ - import android.app.Notification; import android.app.PendingIntent; import android.content.ComponentName; @@ -36,19 +32,10 @@ import com.kabouzeid.gramophone.util.PhonographColorUtil; import com.kabouzeid.gramophone.util.PreferenceUtil; import com.kabouzeid.gramophone.util.Util; -public class PlayingNotificationImpl implements PlayingNotification { - - private MusicService service; +public class PlayingNotificationImpl extends PlayingNotification { private Target target; - private boolean stopped; - - @Override - public synchronized void init(MusicService service) { - this.service = service; - } - @Override public synchronized void update() { stopped = false; @@ -81,16 +68,19 @@ public class PlayingNotificationImpl implements PlayingNotification { Intent action = new Intent(service, MainActivity.class); action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); - PendingIntent openAppPendingIntent = PendingIntent.getActivity(service, 0, action, 0); + final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, 0); + final PendingIntent deleteIntent = buildPendingIntent(service, MusicService.ACTION_QUIT, null); final Notification notification = new NotificationCompat.Builder(service) .setSmallIcon(R.drawable.ic_notification) - .setContentIntent(openAppPendingIntent) + .setContentIntent(clickIntent) + .setDeleteIntent(deleteIntent) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setPriority(NotificationCompat.PRIORITY_MAX) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setContent(notificationLayout) .setCustomBigContentView(notificationLayoutBig) + .setOngoing(isPlaying) .build(); final int bigNotificationImageSize = service.getResources().getDimensionPixelSize(R.dimen.notification_big_image_size); @@ -132,7 +122,7 @@ public class PlayingNotificationImpl implements PlayingNotification { if (stopped) return; // notification has been stopped before loading was finished - service.startForeground(NOTIFICATION_ID, notification); + updateNotifyModeAndPostNotification(notification); } private void setBackgroundColor(int color) { @@ -147,7 +137,6 @@ public class PlayingNotificationImpl implements PlayingNotification { Bitmap prev = createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, primary), 1.5f); Bitmap next = createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, primary), 1.5f); Bitmap playPause = createBitmap(Util.getTintedVectorDrawable(service, isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp, primary), 1.5f); - Bitmap close = createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_close_white_24dp, secondary), 1f); notificationLayout.setTextColor(R.id.title, primary); notificationLayout.setTextColor(R.id.text, secondary); @@ -161,19 +150,12 @@ public class PlayingNotificationImpl implements PlayingNotification { notificationLayoutBig.setImageViewBitmap(R.id.action_prev, prev); notificationLayoutBig.setImageViewBitmap(R.id.action_next, next); notificationLayoutBig.setImageViewBitmap(R.id.action_play_pause, playPause); - notificationLayoutBig.setImageViewBitmap(R.id.action_quit, close); } }); } }); } - @Override - public synchronized void stop() { - stopped = true; - service.stopForeground(true); - } - private void linkButtons(final RemoteViews notificationLayout, final RemoteViews notificationLayoutBig) { PendingIntent pendingIntent; @@ -193,10 +175,6 @@ public class PlayingNotificationImpl implements PlayingNotification { pendingIntent = buildPendingIntent(service, MusicService.ACTION_SKIP, serviceName); notificationLayout.setOnClickPendingIntent(R.id.action_next, pendingIntent); notificationLayoutBig.setOnClickPendingIntent(R.id.action_next, pendingIntent); - - // Quit - pendingIntent = buildPendingIntent(service, MusicService.ACTION_QUIT, serviceName); - notificationLayoutBig.setOnClickPendingIntent(R.id.action_quit, pendingIntent); } private PendingIntent buildPendingIntent(Context context, final String action, final ComponentName serviceName) { diff --git a/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotificationImpl24.java b/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotificationImpl24.java index 283b5c5f..ebf24b56 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotificationImpl24.java +++ b/app/src/main/java/com/kabouzeid/gramophone/service/notification/PlayingNotificationImpl24.java @@ -1,7 +1,5 @@ package com.kabouzeid.gramophone.service.notification; -import android.app.Notification; -import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Intent; @@ -25,32 +23,11 @@ import com.kabouzeid.gramophone.service.MusicService; import com.kabouzeid.gramophone.ui.activities.MainActivity; import com.kabouzeid.gramophone.util.PreferenceUtil; -import static android.content.Context.NOTIFICATION_SERVICE; import static com.kabouzeid.gramophone.service.MusicService.ACTION_REWIND; import static com.kabouzeid.gramophone.service.MusicService.ACTION_SKIP; import static com.kabouzeid.gramophone.service.MusicService.ACTION_TOGGLE_PAUSE; -/** - * @author Karim Abou Zeid (kabouzeid) - */ - -public class PlayingNotificationImpl24 implements PlayingNotification { - private static final int NOTIFY_MODE_FOREGROUND = 1; - private static final int NOTIFY_MODE_BACKGROUND = 0; - - private MusicService service; - - private NotificationManager notificationManager; - - private int notifyMode = NOTIFY_MODE_BACKGROUND; - - private boolean stopped; - - @Override - public synchronized void init(MusicService service) { - this.service = service; - notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE); - } +public class PlayingNotificationImpl24 extends PlayingNotification { @Override public synchronized void update() { @@ -140,35 +117,6 @@ public class PlayingNotificationImpl24 implements PlayingNotification { final ComponentName serviceName = new ComponentName(service, MusicService.class); Intent intent = new Intent(action); intent.setComponent(serviceName); - return PendingIntent.getService(service, 0, intent, 0); } - - private void updateNotifyModeAndPostNotification(Notification notification) { - int newNotifyMode; - if (service.isPlaying()) { - newNotifyMode = NOTIFY_MODE_FOREGROUND; - } else { - newNotifyMode = NOTIFY_MODE_BACKGROUND; - } - - if (notifyMode != newNotifyMode && newNotifyMode == NOTIFY_MODE_BACKGROUND) { - service.stopForeground(false); - } - - if (newNotifyMode == NOTIFY_MODE_FOREGROUND) { - service.startForeground(NOTIFICATION_ID, notification); - } else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) { - notificationManager.notify(NOTIFICATION_ID, notification); - } - - notifyMode = newNotifyMode; - } - - @Override - public synchronized void stop() { - stopped = true; - service.stopForeground(true); - notificationManager.cancel(NOTIFICATION_ID); - } } diff --git a/app/src/main/res/layout/notification_big.xml b/app/src/main/res/layout/notification_big.xml index 9bc97941..535a85bc 100644 --- a/app/src/main/res/layout/notification_big.xml +++ b/app/src/main/res/layout/notification_big.xml @@ -36,18 +36,6 @@ android:scaleType="centerCrop" tools:ignore="ContentDescription" /> - -