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);
}
/** 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. */
@SuppressLint("NewApi")
private void setupNotification() {
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
final PendingIntent pIntent = PendingIntent.getActivity(this, 0,
PlaybackService.getPlayerActivityIntent(this),
PendingIntent.FLAG_UPDATE_CURRENT);
if (notificationSetupTask != null) {
notificationSetupTask.cancel(true);
}
notificationSetupTask = new AsyncTask<Void, Void, Void>() {
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 (media != null && media != null) {
int iconSize = getResources().getDimensionPixelSize(
android.R.dimen.notification_large_icon_width);
icon = BitmapDecoder.decodeBitmapFromWorkerTaskResource(
iconSize, media);
icon = BitmapDecoder
.decodeBitmapFromWorkerTaskResource(iconSize,
media);
}
}
@ -998,18 +1011,29 @@ public class PlaybackService extends Service {
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 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,
Intent pauseButtonIntent = new Intent(
PlaybackService.this, PlaybackService.class);
pauseButtonIntent.putExtra(
MediaButtonReceiver.EXTRA_KEYCODE,
KeyEvent.KEYCODE_MEDIA_PAUSE);
PendingIntent pauseButtonPendingIntent = PendingIntent.getService(
this, 0, pauseButtonIntent,
PendingIntent pauseButtonPendingIntent = PendingIntent
.getService(PlaybackService.this, 0,
pauseButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder = new Notification.Builder(
this)
PlaybackService.this)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setOngoing(true)
@ -1022,7 +1046,8 @@ public class PlaybackService extends Service {
notification = notificationBuilder.build();
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
this).setContentTitle(contentTitle)
PlaybackService.this)
.setContentTitle(contentTitle)
.setContentText(contentText).setOngoing(true)
.setContentIntent(pIntent).setLargeIcon(icon)
.setSmallIcon(R.drawable.ic_stat_antenna);
@ -1032,6 +1057,17 @@ public class PlaybackService extends Service {
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();
}
}
/**
* Seek a specific position from the current position