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,20 +976,33 @@ 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);
if (notificationSetupTask != null) {
notificationSetupTask.cancel(true);
}
notificationSetupTask = new AsyncTask<Void, Void, Void>() {
Bitmap icon = null; Bitmap icon = null;
@Override
protected Void doInBackground(Void... params) {
if (AppConfig.DEBUG)
Log.d(TAG, "Starting background work");
if (android.os.Build.VERSION.SDK_INT >= 11) { if (android.os.Build.VERSION.SDK_INT >= 11) {
if (media != null && media != null) { if (media != null && media != null) {
int iconSize = getResources().getDimensionPixelSize( int iconSize = getResources().getDimensionPixelSize(
android.R.dimen.notification_large_icon_width); android.R.dimen.notification_large_icon_width);
icon = BitmapDecoder.decodeBitmapFromWorkerTaskResource( icon = BitmapDecoder
iconSize, media); .decodeBitmapFromWorkerTaskResource(iconSize,
media);
} }
} }
@ -998,18 +1011,29 @@ public class PlaybackService extends Service {
R.drawable.ic_stat_antenna); R.drawable.ic_stat_antenna);
} }
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (!isCancelled() && status == PlayerStatus.PLAYING
&& media != null) {
String contentText = media.getFeedTitle(); String contentText = media.getFeedTitle();
String contentTitle = media.getEpisodeTitle(); String contentTitle = media.getEpisodeTitle();
Notification notification = null; Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= 16) { if (android.os.Build.VERSION.SDK_INT >= 16) {
Intent pauseButtonIntent = new Intent(this, PlaybackService.class); Intent pauseButtonIntent = new Intent(
pauseButtonIntent.putExtra(MediaButtonReceiver.EXTRA_KEYCODE, PlaybackService.this, PlaybackService.class);
pauseButtonIntent.putExtra(
MediaButtonReceiver.EXTRA_KEYCODE,
KeyEvent.KEYCODE_MEDIA_PAUSE); KeyEvent.KEYCODE_MEDIA_PAUSE);
PendingIntent pauseButtonPendingIntent = PendingIntent.getService( PendingIntent pauseButtonPendingIntent = PendingIntent
this, 0, pauseButtonIntent, .getService(PlaybackService.this, 0,
pauseButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder = new Notification.Builder( Notification.Builder notificationBuilder = new Notification.Builder(
this) PlaybackService.this)
.setContentTitle(contentTitle) .setContentTitle(contentTitle)
.setContentText(contentText) .setContentText(contentText)
.setOngoing(true) .setOngoing(true)
@ -1022,7 +1046,8 @@ public class PlaybackService extends Service {
notification = notificationBuilder.build(); notification = notificationBuilder.build();
} else { } else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
this).setContentTitle(contentTitle) PlaybackService.this)
.setContentTitle(contentTitle)
.setContentText(contentText).setOngoing(true) .setContentText(contentText).setOngoing(true)
.setContentIntent(pIntent).setLargeIcon(icon) .setContentIntent(pIntent).setLargeIcon(icon)
.setSmallIcon(R.drawable.ic_stat_antenna); .setSmallIcon(R.drawable.ic_stat_antenna);
@ -1032,6 +1057,17 @@ public class PlaybackService extends Service {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Notification set up"); 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();
}
}
/** /**
* Seek a specific position from the current position * Seek a specific position from the current position