Amend last commit. Added new drawables and removed obsolete drawables and layouts.
This commit is contained in:
parent
7fcb4a314e
commit
4e8c3694d4
37 changed files with 218 additions and 631 deletions
|
|
@ -0,0 +1,70 @@
|
|||
package com.kabouzeid.gramophone.helper;
|
||||
|
||||
import android.media.RemoteControlClient;
|
||||
import android.os.Build;
|
||||
import android.support.v4.media.session.MediaSessionCompat;
|
||||
import android.support.v4.media.session.PlaybackStateCompat;
|
||||
|
||||
public class MediaSessionHelper {
|
||||
|
||||
public static void applyState(MediaSessionCompat session, PlaybackStateCompat playbackState) {
|
||||
session.setPlaybackState(playbackState);
|
||||
ensureTransportControls(session, playbackState);
|
||||
}
|
||||
|
||||
private static void ensureTransportControls(MediaSessionCompat session, PlaybackStateCompat playbackState) {
|
||||
long actions = playbackState.getActions();
|
||||
Object rccObj = session.getRemoteControlClient();
|
||||
|
||||
if (actions != 0
|
||||
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
|
||||
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
|
||||
&& rccObj != null) {
|
||||
|
||||
int transportControls = 0;
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS;
|
||||
}
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_REWIND) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_REWIND;
|
||||
}
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_PLAY) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PLAY;
|
||||
}
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_PLAY_PAUSE) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE;
|
||||
}
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_PAUSE) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PAUSE;
|
||||
}
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_STOP) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_STOP;
|
||||
}
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_FAST_FORWARD) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD;
|
||||
}
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_NEXT;
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
if ((actions & PlaybackStateCompat.ACTION_SEEK_TO) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE;
|
||||
}
|
||||
|
||||
if ((actions & PlaybackStateCompat.ACTION_SET_RATING) != 0) {
|
||||
transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_RATING;
|
||||
}
|
||||
}
|
||||
((RemoteControlClient) rccObj).setTransportControlFlags(transportControls);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,16 +5,15 @@ package com.kabouzeid.gramophone.helper;
|
|||
*/
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.TaskStackBuilder;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.support.v4.media.session.MediaSessionCompat;
|
||||
import android.support.v7.app.NotificationCompat;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
|
@ -22,192 +21,101 @@ import com.kabouzeid.gramophone.service.MusicService;
|
|||
import com.kabouzeid.gramophone.ui.activities.MusicControllerActivity;
|
||||
import com.kabouzeid.gramophone.util.MusicUtil;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import com.nostra13.universalimageloader.core.assist.FailReason;
|
||||
import com.nostra13.universalimageloader.core.assist.ImageSize;
|
||||
import com.nostra13.universalimageloader.core.assist.ViewScaleType;
|
||||
import com.nostra13.universalimageloader.core.imageaware.NonViewAware;
|
||||
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
|
||||
|
||||
public class PlayingNotificationHelper {
|
||||
|
||||
public static final String TAG = PlayingNotificationHelper.class.getSimpleName();
|
||||
public static final int NOTIFICATION_ID = 1337;
|
||||
|
||||
private final MusicService service;
|
||||
public static Notification buildNotification(final Context context, MediaSessionCompat.Token sessionToken, final Song song, final boolean isPlaying) {
|
||||
|
||||
private final NotificationManager notificationManager;
|
||||
private Notification notification = null;
|
||||
NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle()
|
||||
.setMediaSession(sessionToken)
|
||||
.setShowActionsInCompactView(0, 1, 2);
|
||||
|
||||
private RemoteViews notificationLayout;
|
||||
private RemoteViews notificationLayoutExpanded;
|
||||
style.setShowCancelButton(true);
|
||||
style.setCancelButtonIntent(retrievePlaybackAction(context, 3));
|
||||
|
||||
private Song currentSong;
|
||||
private String currentAlbumArtUri;
|
||||
Bitmap albumArt = ImageLoader.getInstance().loadImageSync(MusicUtil.getAlbumArtUri(song.albumId).toString());
|
||||
if (albumArt == null) {
|
||||
albumArt = BitmapFactory.decodeResource(context.getResources(), R.drawable.default_album_art);
|
||||
}
|
||||
|
||||
public PlayingNotificationHelper(final MusicService service) {
|
||||
this.service = service;
|
||||
notificationManager = (NotificationManager) service
|
||||
.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
}
|
||||
|
||||
public void buildNotification(final Song song, final boolean isPlaying) {
|
||||
currentSong = song;
|
||||
notificationLayout = new RemoteViews(service.getPackageName(),
|
||||
R.layout.notification_playing);
|
||||
notificationLayoutExpanded = new RemoteViews(service.getPackageName(),
|
||||
R.layout.notification_playing_expanded);
|
||||
|
||||
notification = new NotificationCompat.Builder(service)
|
||||
return new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentIntent(getOpenMusicControllerPendingIntent())
|
||||
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||
.setPriority(NotificationCompat.PRIORITY_MAX)
|
||||
.setLargeIcon(albumArt)
|
||||
.setContentIntent(getOpenMusicControllerPendingIntent(context))
|
||||
.setContentTitle(song.title)
|
||||
.setContentText(song.artistName)
|
||||
.setSubText(song.albumName)
|
||||
.setWhen(0)
|
||||
.setShowWhen(false)
|
||||
.setStyle(style)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.setContent(notificationLayout)
|
||||
.setStyle(new NotificationCompat.MediaStyle())
|
||||
|
||||
.addAction(R.drawable.ic_skip_previous_white_36dp,
|
||||
"",
|
||||
retrievePlaybackAction(context, 2))
|
||||
|
||||
.addAction(isPlaying ? R.drawable.ic_pause_white_36dp : R.drawable.ic_play_arrow_white_36dp,
|
||||
"",
|
||||
retrievePlaybackAction(context, 0))
|
||||
|
||||
.addAction(R.drawable.ic_skip_next_white_36dp,
|
||||
"",
|
||||
retrievePlaybackAction(context, 1))
|
||||
|
||||
.setOnlyAlertOnce(true)
|
||||
.build();
|
||||
notification.bigContentView = notificationLayoutExpanded;
|
||||
|
||||
setUpCollapsedLayout();
|
||||
setUpExpandedLayout();
|
||||
loadAlbumArt();
|
||||
setUpPlaybackActions(isPlaying);
|
||||
setUpExpandedPlaybackActions(isPlaying);
|
||||
|
||||
service.startForeground(NOTIFICATION_ID, notification);
|
||||
}
|
||||
|
||||
private PendingIntent getOpenMusicControllerPendingIntent() {
|
||||
Intent result = new Intent(service, MusicControllerActivity.class);
|
||||
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(service);
|
||||
private static PendingIntent getOpenMusicControllerPendingIntent(final Context context) {
|
||||
Intent result = new Intent(context, MusicControllerActivity.class);
|
||||
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
|
||||
taskStackBuilder.addParentStack(MusicControllerActivity.class);
|
||||
taskStackBuilder.addNextIntent(result);
|
||||
return taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
|
||||
private void setUpExpandedPlaybackActions(boolean isPlaying) {
|
||||
notificationLayoutExpanded.setOnClickPendingIntent(R.id.button_toggle_play_pause,
|
||||
retrievePlaybackActions(1));
|
||||
|
||||
notificationLayoutExpanded.setOnClickPendingIntent(R.id.button_next,
|
||||
retrievePlaybackActions(2));
|
||||
|
||||
notificationLayoutExpanded.setOnClickPendingIntent(R.id.button_prev,
|
||||
retrievePlaybackActions(3));
|
||||
|
||||
notificationLayoutExpanded.setOnClickPendingIntent(R.id.button_quit,
|
||||
retrievePlaybackActions(4));
|
||||
|
||||
notificationLayoutExpanded.setImageViewResource(R.id.button_toggle_play_pause,
|
||||
isPlaying ? R.drawable.ic_pause_white_48dp : R.drawable.ic_play_arrow_white_48dp);
|
||||
}
|
||||
|
||||
private void setUpPlaybackActions(boolean isPlaying) {
|
||||
notificationLayout.setOnClickPendingIntent(R.id.button_toggle_play_pause,
|
||||
retrievePlaybackActions(1));
|
||||
|
||||
notificationLayout.setOnClickPendingIntent(R.id.button_next,
|
||||
retrievePlaybackActions(2));
|
||||
|
||||
notificationLayout.setOnClickPendingIntent(R.id.button_quit,
|
||||
retrievePlaybackActions(4));
|
||||
|
||||
notificationLayout.setImageViewResource(R.id.button_toggle_play_pause,
|
||||
isPlaying ? R.drawable.ic_pause_white_48dp : R.drawable.ic_play_arrow_white_48dp);
|
||||
}
|
||||
|
||||
private PendingIntent retrievePlaybackActions(final int which) {
|
||||
Intent action;
|
||||
PendingIntent pendingIntent;
|
||||
final ComponentName serviceName = new ComponentName(service, MusicService.class);
|
||||
private static PendingIntent retrievePlaybackAction(final Context context, final int which) {
|
||||
String actionString = null;
|
||||
switch (which) {
|
||||
case 1:
|
||||
action = new Intent(MusicService.ACTION_TOGGLE_PLAYBACK);
|
||||
action.setComponent(serviceName);
|
||||
pendingIntent = PendingIntent.getService(service, 1, action, 0);
|
||||
return pendingIntent;
|
||||
case 2:
|
||||
action = new Intent(MusicService.ACTION_SKIP);
|
||||
action.setComponent(serviceName);
|
||||
pendingIntent = PendingIntent.getService(service, 2, action, 0);
|
||||
return pendingIntent;
|
||||
case 3:
|
||||
action = new Intent(MusicService.ACTION_REWIND);
|
||||
action.setComponent(serviceName);
|
||||
pendingIntent = PendingIntent.getService(service, 3, action, 0);
|
||||
return pendingIntent;
|
||||
case 4:
|
||||
action = new Intent(MusicService.ACTION_QUIT);
|
||||
action.setComponent(serviceName);
|
||||
pendingIntent = PendingIntent.getService(service, 4, action, 0);
|
||||
return pendingIntent;
|
||||
default:
|
||||
case 0:
|
||||
actionString = MusicService.ACTION_TOGGLE_PLAYBACK;
|
||||
break;
|
||||
case 1:
|
||||
actionString = MusicService.ACTION_SKIP;
|
||||
break;
|
||||
case 2:
|
||||
actionString = MusicService.ACTION_REWIND;
|
||||
break;
|
||||
case 3:
|
||||
actionString = MusicService.ACTION_QUIT;
|
||||
break;
|
||||
}
|
||||
if (actionString != null) {
|
||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||
Intent actionIntent = new Intent(actionString);
|
||||
actionIntent.setComponent(serviceName);
|
||||
return PendingIntent.getService(context, 0, actionIntent, 0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void setUpCollapsedLayout() {
|
||||
if (currentSong != null) {
|
||||
notificationLayout.setTextViewText(R.id.song_title, currentSong.title);
|
||||
notificationLayout.setTextViewText(R.id.song_artist, currentSong.artistName);
|
||||
}
|
||||
}
|
||||
|
||||
private void setUpExpandedLayout() {
|
||||
if (currentSong != null) {
|
||||
notificationLayoutExpanded.setTextViewText(R.id.song_title, currentSong.title);
|
||||
notificationLayoutExpanded.setTextViewText(R.id.song_artist, currentSong.artistName);
|
||||
notificationLayoutExpanded.setTextViewText(R.id.album_title, currentSong.albumName);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadAlbumArt() {
|
||||
currentAlbumArtUri = MusicUtil.getAlbumArtUri(currentSong.albumId).toString();
|
||||
ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() {
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
if (currentAlbumArtUri.equals(imageUri))
|
||||
// copy() prevents the original bitmap in the memory cache from being recycled by the remote views
|
||||
setAlbumArt(loadedImage.copy(loadedImage.getConfig(), true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
if (currentAlbumArtUri.equals(imageUri))
|
||||
setAlbumArt(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setAlbumArt(Bitmap albumArt) {
|
||||
if (albumArt != null) {
|
||||
notificationLayout.setImageViewBitmap(R.id.album_art, albumArt);
|
||||
notificationLayoutExpanded.setImageViewBitmap(R.id.album_art, albumArt);
|
||||
} else {
|
||||
notificationLayout.setImageViewResource(R.id.album_art, R.drawable.default_album_art);
|
||||
notificationLayoutExpanded.setImageViewResource(R.id.album_art, R.drawable.default_album_art);
|
||||
}
|
||||
notificationManager.notify(NOTIFICATION_ID, notification);
|
||||
}
|
||||
|
||||
public void killNotification() {
|
||||
service.stopForeground(true);
|
||||
notification = null;
|
||||
}
|
||||
|
||||
public void updatePlayState(final boolean isPlaying) {
|
||||
if (notification == null || notificationManager == null) {
|
||||
return;
|
||||
}
|
||||
if (notificationLayout != null) {
|
||||
notificationLayout.setImageViewResource(R.id.button_toggle_play_pause,
|
||||
isPlaying ? R.drawable.ic_pause_white_48dp : R.drawable.ic_play_arrow_white_48dp);
|
||||
}
|
||||
if (notificationLayoutExpanded != null) {
|
||||
notificationLayoutExpanded.setImageViewResource(R.id.button_toggle_play_pause,
|
||||
isPlaying ? R.drawable.ic_pause_white_48dp : R.drawable.ic_play_arrow_white_48dp);
|
||||
}
|
||||
notificationManager.notify(NOTIFICATION_ID, notification);
|
||||
}
|
||||
// private void loadAlbumArt() {
|
||||
// currentAlbumArtUri = MusicUtil.getAlbumArtUri(currentSong.albumId).toString();
|
||||
// ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() {
|
||||
// @Override
|
||||
// public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
// if (currentAlbumArtUri.equals(imageUri))
|
||||
// // copy() prevents the original bitmap in the memory cache from being recycled by the remote views
|
||||
// setAlbumArt(loadedImage.copy(loadedImage.getConfig(), true));
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
// if (currentAlbumArtUri.equals(imageUri))
|
||||
// setAlbumArt(null);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue