Load playback service notification bitmap in another thread

This commit is contained in:
daniel oeh 2013-03-26 20:29:18 +01:00
parent 09edb416aa
commit 79ed9e464a
1 changed files with 81 additions and 45 deletions

View File

@ -976,61 +976,97 @@ public class PlaybackService extends Service {
sendBroadcast(intent); sendBroadcast(intent);
} }
/** Used by setupNotification to load notification data in another thread. */
private AsyncTask<Void, Void, Void> notificationSetupTask;
/** Prepares notification and starts the service in the foreground. */ /** Prepares notification and starts the service in the foreground. */
@SuppressLint("NewApi") @SuppressLint("NewApi")
private void setupNotification() { private void setupNotification() {
PendingIntent pIntent = PendingIntent.getActivity(this, 0, final PendingIntent pIntent = PendingIntent.getActivity(this, 0,
PlaybackService.getPlayerActivityIntent(this), PlaybackService.getPlayerActivityIntent(this),
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap icon = null; if (notificationSetupTask != null) {
if (android.os.Build.VERSION.SDK_INT >= 11) { notificationSetupTask.cancel(true);
if (media != null && media != null) { }
int iconSize = getResources().getDimensionPixelSize( notificationSetupTask = new AsyncTask<Void, Void, Void>() {
android.R.dimen.notification_large_icon_width); Bitmap icon = null;
icon = BitmapDecoder.decodeBitmapFromWorkerTaskResource(
iconSize, media); @Override
protected Void doInBackground(Void... params) {
if (AppConfig.DEBUG)
Log.d(TAG, "Starting background work");
if (android.os.Build.VERSION.SDK_INT >= 11) {
if (media != null && media != null) {
int iconSize = getResources().getDimensionPixelSize(
android.R.dimen.notification_large_icon_width);
icon = BitmapDecoder
.decodeBitmapFromWorkerTaskResource(iconSize,
media);
}
}
if (icon == null) {
icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_stat_antenna);
}
return null;
} }
} @Override
if (icon == null) { protected void onPostExecute(Void result) {
icon = BitmapFactory.decodeResource(getResources(), super.onPostExecute(result);
R.drawable.ic_stat_antenna); if (!isCancelled() && status == PlayerStatus.PLAYING
&& media != null) {
String contentText = media.getFeedTitle();
String contentTitle = media.getEpisodeTitle();
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= 16) {
Intent pauseButtonIntent = new Intent(
PlaybackService.this, PlaybackService.class);
pauseButtonIntent.putExtra(
MediaButtonReceiver.EXTRA_KEYCODE,
KeyEvent.KEYCODE_MEDIA_PAUSE);
PendingIntent pauseButtonPendingIntent = PendingIntent
.getService(PlaybackService.this, 0,
pauseButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder = new Notification.Builder(
PlaybackService.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(
PlaybackService.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");
}
}
};
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
notificationSetupTask
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
notificationSetupTask.execute();
} }
String contentText = media.getFeedTitle();
String contentTitle = media.getEpisodeTitle();
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");
} }
/** /**