Search in sources :

Example 1 with IntList

use of de.danoeh.antennapod.core.util.IntList in project AntennaPod by AntennaPod.

the class PlaybackService method setupNotification.

/**
     * Prepares notification and starts the service in the foreground.
     */
private void setupNotification(final PlaybackServiceMediaPlayer.PSMPInfo info) {
    final PendingIntent pIntent = PendingIntent.getActivity(this, 0, PlaybackService.getPlayerActivityIntent(this), PendingIntent.FLAG_UPDATE_CURRENT);
    if (notificationSetupThread != null) {
        notificationSetupThread.interrupt();
    }
    Runnable notificationSetupTask = new Runnable() {

        Bitmap icon = null;

        @Override
        public void run() {
            Log.d(TAG, "Starting background work");
            if (android.os.Build.VERSION.SDK_INT >= 11) {
                if (info.playable != null) {
                    int iconSize = getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
                    try {
                        icon = Glide.with(PlaybackService.this).load(info.playable.getImageLocation()).asBitmap().diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY).centerCrop().into(iconSize, iconSize).get();
                    } catch (Throwable tr) {
                        Log.e(TAG, "Error loading the media icon for the notification", tr);
                    }
                }
            }
            if (icon == null) {
                icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), ClientConfig.playbackServiceCallbacks.getNotificationIconResource(getApplicationContext()));
            }
            if (mediaPlayer == null) {
                return;
            }
            PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
            final int smallIcon = ClientConfig.playbackServiceCallbacks.getNotificationIconResource(getApplicationContext());
            if (!Thread.currentThread().isInterrupted() && started && info.playable != null) {
                String contentText = info.playable.getEpisodeTitle();
                String contentTitle = info.playable.getFeedTitle();
                Notification notification;
                // Builder is v7, even if some not overwritten methods return its parent's v4 interface
                NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(PlaybackService.this).setContentTitle(contentTitle).setContentText(contentText).setOngoing(false).setContentIntent(pIntent).setLargeIcon(icon).setSmallIcon(smallIcon).setWhen(// we don't need the time
                0).setPriority(// set notification priority
                UserPreferences.getNotifyPriority());
                IntList compactActionList = new IntList();
                // we start and 0 and then increment by 1 for each call to addAction
                int numActions = 0;
                if (isCasting) {
                    Intent stopCastingIntent = new Intent(PlaybackService.this, PlaybackService.class);
                    stopCastingIntent.putExtra(EXTRA_CAST_DISCONNECT, true);
                    PendingIntent stopCastingPendingIntent = PendingIntent.getService(PlaybackService.this, numActions, stopCastingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    notificationBuilder.addAction(R.drawable.ic_media_cast_disconnect, getString(R.string.cast_disconnect_label), stopCastingPendingIntent);
                    numActions++;
                }
                // always let them rewind
                PendingIntent rewindButtonPendingIntent = getPendingIntentForMediaAction(KeyEvent.KEYCODE_MEDIA_REWIND, numActions);
                notificationBuilder.addAction(android.R.drawable.ic_media_rew, getString(R.string.rewind_label), rewindButtonPendingIntent);
                if (UserPreferences.showRewindOnCompactNotification()) {
                    compactActionList.add(numActions);
                }
                numActions++;
                if (playerStatus == PlayerStatus.PLAYING) {
                    PendingIntent pauseButtonPendingIntent = getPendingIntentForMediaAction(KeyEvent.KEYCODE_MEDIA_PAUSE, numActions);
                    //pause action
                    notificationBuilder.addAction(//pause action
                    android.R.drawable.ic_media_pause, getString(R.string.pause_label), pauseButtonPendingIntent);
                    compactActionList.add(numActions++);
                } else {
                    PendingIntent playButtonPendingIntent = getPendingIntentForMediaAction(KeyEvent.KEYCODE_MEDIA_PLAY, numActions);
                    //play action
                    notificationBuilder.addAction(//play action
                    android.R.drawable.ic_media_play, getString(R.string.play_label), playButtonPendingIntent);
                    compactActionList.add(numActions++);
                }
                // ff follows play, then we have skip (if it's present)
                PendingIntent ffButtonPendingIntent = getPendingIntentForMediaAction(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, numActions);
                notificationBuilder.addAction(android.R.drawable.ic_media_ff, getString(R.string.fast_forward_label), ffButtonPendingIntent);
                if (UserPreferences.showFastForwardOnCompactNotification()) {
                    compactActionList.add(numActions);
                }
                numActions++;
                if (UserPreferences.isFollowQueue()) {
                    PendingIntent skipButtonPendingIntent = getPendingIntentForMediaAction(KeyEvent.KEYCODE_MEDIA_NEXT, numActions);
                    notificationBuilder.addAction(android.R.drawable.ic_media_next, getString(R.string.skip_episode_label), skipButtonPendingIntent);
                    if (UserPreferences.showSkipOnCompactNotification()) {
                        compactActionList.add(numActions);
                    }
                    numActions++;
                }
                PendingIntent stopButtonPendingIntent = getPendingIntentForMediaAction(KeyEvent.KEYCODE_MEDIA_STOP, numActions);
                notificationBuilder.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle().setMediaSession(mediaSession.getSessionToken()).setShowActionsInCompactView(compactActionList.toArray()).setShowCancelButton(true).setCancelButtonIntent(stopButtonPendingIntent)).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setColor(NotificationCompat.COLOR_DEFAULT);
                notification = notificationBuilder.build();
                if (playerStatus == PlayerStatus.PLAYING || playerStatus == PlayerStatus.PREPARING || playerStatus == PlayerStatus.SEEKING || isCasting) {
                    startForeground(NOTIFICATION_ID, notification);
                } else {
                    stopForeground(false);
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    mNotificationManager.notify(NOTIFICATION_ID, notification);
                }
                Log.d(TAG, "Notification set up");
            }
        }
    };
    notificationSetupThread = new Thread(notificationSetupTask);
    notificationSetupThread.start();
}
Also used : NotificationManager(android.app.NotificationManager) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) Notification(android.app.Notification) IntList(de.danoeh.antennapod.core.util.IntList) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v7.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Aggregations

Notification (android.app.Notification)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 NotificationCompat (android.support.v7.app.NotificationCompat)1 IntList (de.danoeh.antennapod.core.util.IntList)1