Fixed the notification coloring. Automatically refreshs notification when the color setting is changed. Colored notification for all Android versions now

This commit is contained in:
Karim Abou Zeid 2015-06-17 11:12:46 +02:00
commit d2fda4a839
33 changed files with 391 additions and 33 deletions

View file

@ -8,9 +8,11 @@ import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.support.v4.app.NotificationCompat;
import android.support.v7.graphics.Palette;
@ -34,6 +36,8 @@ public class PlayingNotificationHelper {
public static final String TAG = PlayingNotificationHelper.class.getSimpleName();
public static final int NOTIFICATION_ID = 1337;
public static final String ACTION_NOTIFICATION_COLOR_PREFERENCE_CHANGED = "com.kabouzeid.gramophone.NOTIFICATION_COLOR_PREFERENCE_CHANGED";
public static final String EXTRA_NOTIFICATION_COLORED = "com.kabouzeid.gramophone.EXTRA_NOTIFICATION_COLORED";
private final MusicService service;
@ -44,20 +48,53 @@ public class PlayingNotificationHelper {
private RemoteViews notificationLayoutExpanded;
private Song currentSong;
private boolean isPlaying;
private String currentAlbumArtUri;
private boolean isColored;
private boolean isReceiverRegistered;
private boolean isNotificationShown;
final IntentFilter intentFilter;
public PlayingNotificationHelper(final MusicService service) {
this.service = service;
notificationManager = (NotificationManager) service
.getSystemService(Context.NOTIFICATION_SERVICE);
intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_NOTIFICATION_COLOR_PREFERENCE_CHANGED);
}
private BroadcastReceiver notificationColorPreferenceChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_NOTIFICATION_COLOR_PREFERENCE_CHANGED)) {
boolean isColored = intent.getBooleanExtra(EXTRA_NOTIFICATION_COLORED, false);
if (isNotificationShown && PlayingNotificationHelper.this.isColored != isColored) {
buildNotification(currentSong, isPlaying, isColored);
}
}
}
};
public void buildNotification(final Song song, final boolean isPlaying) {
buildNotification(song, isPlaying, PreferenceUtils.getInstance(service).coloredNotification());
}
private void buildNotification(final Song song, final boolean isPlaying, final boolean isColored) {
this.isColored = isColored;
currentSong = song;
this.isPlaying = isPlaying;
if (!isReceiverRegistered)
service.registerReceiver(notificationColorPreferenceChangedReceiver, intentFilter);
isReceiverRegistered = true;
isNotificationShown = true;
notificationLayout = new RemoteViews(service.getPackageName(),
R.layout.notification_controller);
isColored ? R.layout.notification_controller_colored : R.layout.notification_controller);
notificationLayoutExpanded = new RemoteViews(service.getPackageName(),
R.layout.notification_controller_big);
isColored ? R.layout.notification_controller_big_colored : R.layout.notification_controller_big);
notification = new NotificationCompat.Builder(service)
.setSmallIcon(R.drawable.ic_notification)
@ -73,8 +110,8 @@ public class PlayingNotificationHelper {
setUpCollapsedLayout();
setUpExpandedLayout();
loadAlbumArt();
setUpPlaybackActions(isPlaying);
setUpExpandedPlaybackActions(isPlaying);
setUpPlaybackActions();
setUpExpandedPlaybackActions();
service.startForeground(NOTIFICATION_ID, notification);
}
@ -87,7 +124,7 @@ public class PlayingNotificationHelper {
return taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
private void setUpExpandedPlaybackActions(boolean isPlaying) {
private void setUpExpandedPlaybackActions() {
notificationLayoutExpanded.setOnClickPendingIntent(R.id.action_play_pause,
retrievePlaybackActions(1));
@ -104,7 +141,7 @@ public class PlayingNotificationHelper {
isPlaying ? R.drawable.ic_pause_white_36dp : R.drawable.ic_play_arrow_white_36dp);
}
private void setUpPlaybackActions(boolean isPlaying) {
private void setUpPlaybackActions() {
notificationLayout.setOnClickPendingIntent(R.id.action_play_pause,
retrievePlaybackActions(1));
@ -183,12 +220,14 @@ public class PlayingNotificationHelper {
}
private void setAlbumArt(Bitmap albumArt) {
int defaultColor = service.getResources().getColor(R.color.default_notification_color);
int defaultColor = isColored ?
service.getResources().getColor(R.color.default_colored_notification_color) :
service.getResources().getColor(R.color.default_notification_color);
int newColor = defaultColor;
if (albumArt != null) {
notificationLayout.setImageViewBitmap(R.id.icon, albumArt);
notificationLayoutExpanded.setImageViewBitmap(R.id.icon, albumArt);
if (PreferenceUtils.getInstance(service).coloredNotification())
if (isColored)
newColor = Palette.from(albumArt).generate().getVibrantColor(defaultColor);
} else {
notificationLayout.setImageViewResource(R.id.icon, R.drawable.default_album_art);
@ -201,11 +240,17 @@ public class PlayingNotificationHelper {
}
public void killNotification() {
if (isReceiverRegistered)
service.unregisterReceiver(notificationColorPreferenceChangedReceiver);
isReceiverRegistered = false;
service.stopForeground(true);
notification = null;
isNotificationShown = false;
}
public void updatePlayState(final boolean isPlaying) {
this.isPlaying = isPlaying;
if (notification == null || notificationManager == null) {
return;
}

View file

@ -13,6 +13,7 @@ public class UIPreferenceChangedEvent {
public static final int COLORED_NAVIGATION_BAR_CURRENT_PLAYING_CHANGED = 6;
public static final int COLORED_NAVIGATION_BAR_CHANGED = 10;
public static final int COLORED_NAVIGATION_BAR_OTHER_SCREENS_CHANGED = 7;
public static final int COLORED_NOTIFICATION_CHANGED = 11;
private final int action;
private final Object value;

View file

@ -17,6 +17,7 @@ import com.afollestad.materialdialogs.util.DialogUtils;
import com.kabouzeid.gramophone.App;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.dialogs.ColorChooserDialog;
import com.kabouzeid.gramophone.helper.PlayingNotificationHelper;
import com.kabouzeid.gramophone.model.UIPreferenceChangedEvent;
import com.kabouzeid.gramophone.prefs.ColorChooserPreference;
import com.kabouzeid.gramophone.ui.activities.base.AbsBaseActivity;
@ -137,11 +138,13 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
}
Preference coloredNotification = findPreference("colored_notification");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
coloredNotification.setEnabled(false);
coloredNotification.setWidgetLayoutResource(0);
coloredNotification.setSummary(R.string.pref_only_lollipop);
}
coloredNotification.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
getActivity().sendBroadcast(new Intent(PlayingNotificationHelper.ACTION_NOTIFICATION_COLOR_PREFERENCE_CHANGED).putExtra(PlayingNotificationHelper.EXTRA_NOTIFICATION_COLORED, (boolean) newValue));
return true;
}
});
equalizer = findPreference("equalizer");
resolveEqualizer();