From d85b90f56e75ea33a3fcd93d03aa37501c357807 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Sun, 10 Feb 2013 16:19:25 +0100 Subject: [PATCH] Added expandable notifications to PlaybackService --- res/values/strings.xml | 1 + src/de/danoeh/antennapod/feed/FeedMedia.java | 7 +++ .../antennapod/service/PlaybackService.java | 58 +++++++++++++++---- .../danoeh/antennapod/util/BitmapDecoder.java | 47 +++++++++------ 4 files changed, 83 insertions(+), 30 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 8cba50a57..1d7fffc6c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -53,6 +53,7 @@ Download Play + Pause Stream Remove Mark as read diff --git a/src/de/danoeh/antennapod/feed/FeedMedia.java b/src/de/danoeh/antennapod/feed/FeedMedia.java index b0a6fe5d4..a96649a62 100644 --- a/src/de/danoeh/antennapod/feed/FeedMedia.java +++ b/src/de/danoeh/antennapod/feed/FeedMedia.java @@ -146,5 +146,12 @@ public class FeedMedia extends FeedFile { public boolean isInProgress() { return (this.position > 0); } + + public FeedImage getImage() { + if (item != null && item.getFeed() != null) { + return item.getFeed().getImage(); + } + return null; + } } diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java index 1f5e382e3..c43668260 100644 --- a/src/de/danoeh/antennapod/service/PlaybackService.java +++ b/src/de/danoeh/antennapod/service/PlaybackService.java @@ -11,6 +11,7 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import android.annotation.SuppressLint; +import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; @@ -47,6 +48,7 @@ import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.feed.MediaType; import de.danoeh.antennapod.receiver.MediaButtonReceiver; import de.danoeh.antennapod.receiver.PlayerWidget; +import de.danoeh.antennapod.util.BitmapDecoder; import de.danoeh.antennapod.util.ChapterUtils; /** Controls the MediaPlayer that plays a FeedMedia-file */ @@ -125,7 +127,6 @@ public class PlaybackService extends Service { public static boolean isRunning = false; private static final int NOTIFICATION_ID = 1; - private NotificationCompat.Builder notificationBuilder; private AudioManager audioManager; private ComponentName mediaButtonReceiver; @@ -907,22 +908,57 @@ public class PlaybackService extends Service { } /** Prepares notification and starts the service in the foreground. */ + @SuppressLint("NewApi") private void setupNotification() { PendingIntent pIntent = PendingIntent.getActivity(this, 0, PlaybackService.getPlayerActivityIntent(this), PendingIntent.FLAG_UPDATE_CURRENT); - Bitmap icon = BitmapFactory.decodeResource(getResources(), - R.drawable.ic_stat_antenna); - notificationBuilder = new NotificationCompat.Builder(this) - .setContentTitle( - getString(R.string.playbackservice_notification_title)) - .setContentText( - getString(R.string.playbackservice_notification_content)) - .setOngoing(true).setContentIntent(pIntent).setLargeIcon(icon) - .setSmallIcon(R.drawable.ic_stat_antenna); + Bitmap icon = null; + if (android.os.Build.VERSION.SDK_INT >= 11) { + if (media != null && media.getImage() != null + && media.getImage().getFile_url() != null) { + int iconSize = getResources().getDimensionPixelSize( + android.R.dimen.notification_large_icon_width); + icon = BitmapDecoder.decodeBitmap(iconSize, media.getImage() + .getFile_url()); + } + } + if (icon == null) { + icon = BitmapFactory.decodeResource(getResources(), + R.drawable.ic_stat_antenna); + } - startForeground(NOTIFICATION_ID, notificationBuilder.getNotification()); + String contentText = media.getItem().getFeed().getTitle(); + String contentTitle = media.getItem().getTitle(); + Notification notification = null; + if (android.os.Build.VERSION.SDK_INT >= 16) { + Intent pauseButtonIntent = new Intent(this, PlaybackService.class); + pauseButtonIntent.putExtra(MediaButtonReceiver.EXTRA_KEYCODE, + KeyEvent.KEYCODE_MEDIA_PAUSE); + PendingIntent pauseButtonPendingIntent = PendingIntent + .getService(this, 0, pauseButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT); + Notification.Builder notificationBuilder = new Notification.Builder( + this) + .setContentTitle(contentTitle) + .setContentText(contentText) + .setOngoing(true) + .setContentIntent(pIntent) + .setLargeIcon(icon) + .setSmallIcon(R.drawable.ic_stat_antenna) + .addAction(android.R.drawable.ic_media_pause, + getString(R.string.pause_label), + pauseButtonPendingIntent); + notification = notificationBuilder.build(); + } else { + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( + this).setContentTitle(contentTitle) + .setContentText(contentText).setOngoing(true) + .setContentIntent(pIntent).setLargeIcon(icon) + .setSmallIcon(R.drawable.ic_stat_antenna); + notification = notificationBuilder.getNotification(); + } + startForeground(NOTIFICATION_ID, notification); if (AppConfig.DEBUG) Log.d(TAG, "Notification set up"); } diff --git a/src/de/danoeh/antennapod/util/BitmapDecoder.java b/src/de/danoeh/antennapod/util/BitmapDecoder.java index 270cff0c9..8dd4953c6 100644 --- a/src/de/danoeh/antennapod/util/BitmapDecoder.java +++ b/src/de/danoeh/antennapod/util/BitmapDecoder.java @@ -1,5 +1,10 @@ package de.danoeh.antennapod.util; +import java.io.File; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; + import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; @@ -17,26 +22,30 @@ public class BitmapDecoder { } public static Bitmap decodeBitmap(int preferredLength, String fileUrl) { - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(fileUrl, options); - int srcWidth = options.outWidth; - int srcHeight = options.outHeight; - int length = Math.max(srcWidth, srcHeight); - int sampleSize = calculateSampleSize(preferredLength, length); - if (AppConfig.DEBUG) - Log.d(TAG, "Using samplesize " + sampleSize); - options.inJustDecodeBounds = false; - options.inSampleSize = sampleSize; - options.inPreferredConfig = Bitmap.Config.ARGB_8888; + if (fileUrl != null && new File(fileUrl).exists()) { + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(fileUrl, options); + int srcWidth = options.outWidth; + int srcHeight = options.outHeight; + int length = Math.max(srcWidth, srcHeight); + int sampleSize = calculateSampleSize(preferredLength, length); + if (AppConfig.DEBUG) + Log.d(TAG, "Using samplesize " + sampleSize); + options.inJustDecodeBounds = false; + options.inSampleSize = sampleSize; + options.inPreferredConfig = Bitmap.Config.ARGB_8888; - Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl, options); - if (decodedBitmap == null) { - Log.i(TAG, - "Bitmap could not be decoded in custom sample size. Trying default sample size (path was " - + fileUrl + ")"); - decodedBitmap = BitmapFactory.decodeFile(fileUrl); + Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl, options); + if (decodedBitmap == null) { + Log.i(TAG, + "Bitmap could not be decoded in custom sample size. Trying default sample size (path was " + + fileUrl + ")"); + decodedBitmap = BitmapFactory.decodeFile(fileUrl); + } + return decodedBitmap; + } else { + return null; } - return decodedBitmap; } }