Example usage for android.widget RemoteViews setImageViewResource

List of usage examples for android.widget RemoteViews setImageViewResource

Introduction

In this page you can find the example usage for android.widget RemoteViews setImageViewResource.

Prototype

public void setImageViewResource(int viewId, int srcId) 

Source Link

Document

Equivalent to calling ImageView#setImageResource(int)

Usage

From source file:org.videolan.vlc.audio.AudioService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
    try {//from  ww w . j  ava2  s. c o m
        Media media = getCurrentMedia();
        if (media == null)
            return;
        Bitmap cover = AudioUtil.getCover(this, media, 64);
        String title = media.getTitle();
        String artist = media.getArtist();
        String album = media.getAlbum();
        Notification notification;

        // add notification to status bar
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_vlc).setTicker(title + " - " + artist).setAutoCancel(false)
                .setOngoing(true);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(MainActivity.ACTION_SHOW_PLAYER);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.putExtra(START_FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (LibVlcUtil.isJellyBeanOrLater()) {
            Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
            Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
            Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
            Intent iStop = new Intent(ACTION_REMOTE_STOP);
            PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piStop = PendingIntent.getBroadcast(this, 0, iStop,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
            if (cover != null)
                view.setImageViewBitmap(R.id.cover, cover);
            view.setTextViewText(R.id.songName, title);
            view.setTextViewText(R.id.artist, artist);
            view.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view.setOnClickPendingIntent(R.id.forward, piForward);
            view.setOnClickPendingIntent(R.id.stop, piStop);
            view.setOnClickPendingIntent(R.id.content, pendingIntent);

            RemoteViews view_expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded);
            if (cover != null)
                view_expanded.setImageViewBitmap(R.id.cover, cover);
            view_expanded.setTextViewText(R.id.songName, title);
            view_expanded.setTextViewText(R.id.artist, artist);
            view_expanded.setTextViewText(R.id.album, album);
            view_expanded.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view_expanded.setOnClickPendingIntent(R.id.backward, piBackward);
            view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view_expanded.setOnClickPendingIntent(R.id.forward, piForward);
            view_expanded.setOnClickPendingIntent(R.id.stop, piStop);
            view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent);

            notification = builder.build();
            notification.contentView = view;
            notification.bigContentView = view_expanded;
        } else {
            builder.setLargeIcon(cover).setContentTitle(title)
                    .setContentText(LibVlcUtil.isJellyBeanOrLater() ? artist : media.getSubtitle())
                    .setContentInfo(album).setContentIntent(pendingIntent);
            notification = builder.build();
        }

        startService(new Intent(this, AudioService.class));
        startForeground(3, notification);
    } catch (NoSuchMethodError e) {
        // Compat library is wrong on 3.2
        // http://code.google.com/p/android/issues/detail?id=36359
        // http://code.google.com/p/android/issues/detail?id=36502
    }
}

From source file:com.perm.DoomPlay.PlayingService.java

private RemoteViews getNotifViews(int layoutId) {
    RemoteViews views = new RemoteViews(getPackageName(), layoutId);

    Audio audio = audios.get(indexCurrentTrack);

    views.setTextViewText(R.id.notifTitle, audio.getTitle());
    views.setTextViewText(R.id.notifArtist, audio.getArtist());

    Bitmap cover = AlbumArtGetter.getBitmapFromStore(audio.getAid(), this);

    if (cover == null) {
        Bitmap tempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fallback_cover);
        views.setImageViewBitmap(R.id.notifAlbum, tempBitmap);
        tempBitmap.recycle();//from ww w.j av a  2s. c om
    } else {
        //TODO: java.lang.IllegalArgumentException: RemoteViews for widget update exceeds
        // maximum bitmap memory usage (used: 3240000, max: 2304000)
        // The total memory cannot exceed that required to fill the device's screen once
        try {
            views.setImageViewBitmap(R.id.notifAlbum, cover);
        } catch (IllegalArgumentException e) {
            Bitmap tempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fallback_cover);
            views.setImageViewBitmap(R.id.notifAlbum, tempBitmap);
            tempBitmap.recycle();
        } finally {
            cover.recycle();
        }
    }

    views.setImageViewResource(R.id.notifPlay, isPlaying ? R.drawable.widget_pause : R.drawable.widget_play);

    ComponentName componentName = new ComponentName(this, PlayingService.class);

    Intent intentPlay = new Intent(actionPlay);
    intentPlay.setComponent(componentName);
    views.setOnClickPendingIntent(R.id.notifPlay, PendingIntent.getService(this, 0, intentPlay, 0));

    Intent intentNext = new Intent(actionNext);
    intentNext.setComponent(componentName);
    views.setOnClickPendingIntent(R.id.notifNext, PendingIntent.getService(this, 0, intentNext, 0));

    Intent intentPrevious = new Intent(actionPrevious);
    intentPrevious.setComponent(componentName);
    views.setOnClickPendingIntent(R.id.notifPrevious, PendingIntent.getService(this, 0, intentPrevious, 0));

    Intent intentClose = new Intent(actionClose);
    intentClose.setComponent(componentName);
    views.setOnClickPendingIntent(R.id.notifClose, PendingIntent.getService(this, 0, intentClose, 0));

    return views;
}

From source file:com.dzt.musicplay.player.AudioService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
    Constant.print_i("AudioService----------->showNotification");
    try {//ww  w. j a  v a  2s  .c  om
        Media media = getCurrentMedia();
        if (media == null)
            return;
        Bitmap cover = AudioUtil.getCover(this, media, 64);
        String title = media.getTitle();
        String artist = media.getArtist();
        String album = media.getAlbum();
        Notification notification;

        // add notification to status bar
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon_music).setTicker(title + " - " + artist).setAutoCancel(false)
                .setOngoing(true);

        //Activity
        Intent notificationIntent = new Intent(this, MusicPlayActivity.class);
        notificationIntent.setAction(MusicPlayActivity.ACTION_SHOW_PLAYER);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.putExtra(START_FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (LibVlcUtil.isJellyBeanOrLater()) {
            Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
            Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
            Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
            Intent iStop = new Intent(ACTION_REMOTE_STOP);
            PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piStop = PendingIntent.getBroadcast(this, 0, iStop,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
            if (cover != null)
                view.setImageViewBitmap(R.id.cover, cover);
            view.setTextViewText(R.id.songName, title);
            view.setTextViewText(R.id.artist, artist);
            view.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view.setOnClickPendingIntent(R.id.forward, piForward);
            view.setOnClickPendingIntent(R.id.stop, piStop);
            view.setOnClickPendingIntent(R.id.content, pendingIntent);

            RemoteViews view_expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded);
            if (cover != null)
                view_expanded.setImageViewBitmap(R.id.cover, cover);
            view_expanded.setTextViewText(R.id.songName, title);
            view_expanded.setTextViewText(R.id.artist, artist);
            view_expanded.setTextViewText(R.id.album, album);
            view_expanded.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view_expanded.setOnClickPendingIntent(R.id.backward, piBackward);
            view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view_expanded.setOnClickPendingIntent(R.id.forward, piForward);
            view_expanded.setOnClickPendingIntent(R.id.stop, piStop);
            view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent);

            notification = builder.build();
            notification.contentView = view;
            notification.bigContentView = view_expanded;
        } else {
            builder.setLargeIcon(cover).setContentTitle(title)
                    .setContentText(LibVlcUtil.isJellyBeanOrLater() ? artist : media.getSubtitle())
                    .setContentInfo(album).setContentIntent(pendingIntent);
            notification = builder.build();
        }

        startService(new Intent(this, AudioService.class));
        startForeground(3, notification);
    } catch (NoSuchMethodError e) {
        // Compat library is wrong on 3.2
        // http://code.google.com/p/android/issues/detail?id=36359
        // http://code.google.com/p/android/issues/detail?id=36502
    }
    Constant.print_i("AudioService----------->showNotification----end");
}

From source file:org.videolan.vlc.PlaybackService.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void showNotification() {
    try {/*from   w w w.  j a va2  s .c o  m*/
        MediaWrapper media = getCurrentMedia();
        if (media == null)
            return;
        Bitmap cover = AudioUtil.getCover(this, media, 64);
        String title = media.getTitle();
        String artist = Util.getMediaArtist(this, media);
        String album = Util.getMediaAlbum(this, media);
        Notification notification;

        if (media.isArtistUnknown() && media.isAlbumUnknown() && media.getNowPlaying() != null) {
            artist = media.getNowPlaying();
            album = "";
        }

        //Watch notification dismissed
        PendingIntent piStop = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_REMOTE_STOP),
                PendingIntent.FLAG_UPDATE_CURRENT);

        // add notification to status bar
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_vlc).setTicker(title + " - " + artist)
                .setAutoCancel(!MediaPlayer().isPlaying()).setOngoing(MediaPlayer().isPlaying())
                .setDeleteIntent(piStop);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(AudioPlayerContainerActivity.ACTION_SHOW_PLAYER);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.putExtra(START_FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (AndroidUtil.isJellyBeanOrLater()) {
            Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
            Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
            Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
            PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews view = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.notification);
            view.setImageViewBitmap(R.id.cover,
                    cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover);
            view.setTextViewText(R.id.songName, title);
            view.setTextViewText(R.id.artist, artist);
            view.setImageViewResource(R.id.play_pause,
                    MediaPlayer().isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view.setOnClickPendingIntent(R.id.forward, piForward);
            view.setOnClickPendingIntent(R.id.stop, piStop);
            view.setOnClickPendingIntent(R.id.content, pendingIntent);

            RemoteViews view_expanded = new RemoteViews(BuildConfig.APPLICATION_ID,
                    R.layout.notification_expanded);
            view_expanded.setImageViewBitmap(R.id.cover,
                    cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover);
            view_expanded.setTextViewText(R.id.songName, title);
            view_expanded.setTextViewText(R.id.artist, artist);
            view_expanded.setTextViewText(R.id.album, album);
            view_expanded.setImageViewResource(R.id.play_pause,
                    MediaPlayer().isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view_expanded.setOnClickPendingIntent(R.id.backward, piBackward);
            view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view_expanded.setOnClickPendingIntent(R.id.forward, piForward);
            view_expanded.setOnClickPendingIntent(R.id.stop, piStop);
            view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent);

            if (AndroidUtil.isLolliPopOrLater()) {
                //Hide stop button on pause, we swipe notification to stop
                view.setViewVisibility(R.id.stop, MediaPlayer().isPlaying() ? View.VISIBLE : View.INVISIBLE);
                view_expanded.setViewVisibility(R.id.stop,
                        MediaPlayer().isPlaying() ? View.VISIBLE : View.INVISIBLE);
                //Make notification appear on lockscreen
                builder.setVisibility(Notification.VISIBILITY_PUBLIC);
            }

            notification = builder.build();
            notification.contentView = view;
            notification.bigContentView = view_expanded;
        } else {
            builder.setLargeIcon(
                    cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover)
                    .setContentTitle(title)
                    .setContentText(
                            AndroidUtil.isJellyBeanOrLater() ? artist : Util.getMediaSubtitle(this, media))
                    .setContentInfo(album).setContentIntent(pendingIntent);
            notification = builder.build();
        }

        startService(new Intent(this, PlaybackService.class));
        if (!AndroidUtil.isLolliPopOrLater() || MediaPlayer().isPlaying())
            startForeground(3, notification);
        else {
            stopForeground(false);
            NotificationManagerCompat.from(this).notify(3, notification);
        }
    } catch (NoSuchMethodError e) {
        // Compat library is wrong on 3.2
        // http://code.google.com/p/android/issues/detail?id=36359
        // http://code.google.com/p/android/issues/detail?id=36502
    }
}

From source file:com.andreadec.musicplayer.MusicService.java

private void updateNotificationMessage() {
    /* Update remote control client */
    if (Build.VERSION.SDK_INT >= 14) {
        if (currentPlayingItem == null) {
            remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
        } else {//  w  w w. j a v  a2s. c o  m
            if (isPlaying()) {
                remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
            } else {
                remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
            }
            RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, currentPlayingItem.getTitle());
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, currentPlayingItem.getArtist());
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST,
                    currentPlayingItem.getArtist());
            metadataEditor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, getDuration());
            if (currentPlayingItem.hasImage()) {
                metadataEditor.putBitmap(METADATA_KEY_ARTWORK, currentPlayingItem.getImage());
            } else {
                metadataEditor.putBitmap(METADATA_KEY_ARTWORK, icon.copy(icon.getConfig(), false));
            }
            metadataEditor.apply();
        }
    }

    /* Update notification */
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(R.drawable.audio_white);
    //notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    notificationBuilder.setOngoing(true);

    int playPauseIcon = isPlaying() ? R.drawable.pause : R.drawable.play;
    //int playPauseString = isPlaying() ? R.string.pause : R.string.play;
    notificationBuilder.setContentIntent(pendingIntent);

    String notificationMessage = "";
    if (currentPlayingItem == null) {
        notificationMessage = getResources().getString(R.string.noSong);
    } else {
        if (currentPlayingItem.getArtist() != null && !currentPlayingItem.getArtist().equals(""))
            notificationMessage = currentPlayingItem.getArtist() + " - ";
        notificationMessage += currentPlayingItem.getTitle();
    }

    /*notificationBuilder.addAction(R.drawable.previous, getResources().getString(R.string.previous), previousPendingIntent);
    notificationBuilder.addAction(playPauseIcon, getResources().getString(playPauseString), playpausePendingIntent);
    notificationBuilder.addAction(R.drawable.next, getResources().getString(R.string.next), nextPendingIntent);*/
    notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
    notificationBuilder.setContentText(notificationMessage);

    notification = notificationBuilder.build();

    RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.layout_notification);

    if (currentPlayingItem == null) {
        notificationLayout.setTextViewText(R.id.textViewArtist, getString(R.string.app_name));
        notificationLayout.setTextViewText(R.id.textViewTitle, getString(R.string.noSong));
        notificationLayout.setImageViewBitmap(R.id.imageViewNotification,
                BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    } else {
        notificationLayout.setTextViewText(R.id.textViewArtist, currentPlayingItem.getArtist());
        notificationLayout.setTextViewText(R.id.textViewTitle, currentPlayingItem.getTitle());
        Bitmap image = currentPlayingItem.getImage();
        if (image != null) {
            notificationLayout.setImageViewBitmap(R.id.imageViewNotification, image);
        } else {
            notificationLayout.setImageViewBitmap(R.id.imageViewNotification,
                    BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
        }
    }
    notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationQuit, quitPendingIntent);
    notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationPrevious, previousPendingIntent);
    notificationLayout.setImageViewResource(R.id.buttonNotificationPlayPause, playPauseIcon);
    notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationPlayPause, playpausePendingIntent);
    notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationNext, nextPendingIntent);
    notification.bigContentView = notificationLayout;

    notificationManager.notify(Constants.NOTIFICATION_MAIN, notification);
}

From source file:android.app.Notification.java

/**
 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
 * layout./*from   w ww . j  a  va2  s .  c  o  m*/
 *
 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
 * in the view.</p>
 * @param context       The context for your application / activity.
 * @param contentTitle The title that goes in the expanded entry.
 * @param contentText  The text that goes in the expanded entry.
 * @param contentIntent The intent to launch when the user clicks the expanded notification.
 * If this is an activity, it must include the
 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
 * that you take care of task management as described in the
 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
 * Stack</a> document.
 *
 * @deprecated Use {@link Builder} instead.
 */
@Deprecated
public void setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText,
        PendingIntent contentIntent) {
    // TODO: rewrite this to use Builder
    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_template_base);
    if (this.icon != 0) {
        contentView.setImageViewResource(R.id.icon, this.icon);
    }
    if (priority < PRIORITY_LOW) {
        contentView.setInt(R.id.icon, "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
        contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
                R.drawable.notification_bg_low);
    }
    if (contentTitle != null) {
        contentView.setTextViewText(R.id.title, contentTitle);
    }
    if (contentText != null) {
        contentView.setTextViewText(R.id.text, contentText);
    }
    if (this.when != 0) {
        contentView.setViewVisibility(R.id.time, View.VISIBLE);
        contentView.setLong(R.id.time, "setTime", when);
    }
    if (this.number != 0) {
        NumberFormat f = NumberFormat.getIntegerInstance();
        contentView.setTextViewText(R.id.info, f.format(this.number));
    }

    this.contentView = contentView;
    this.contentIntent = contentIntent;
}

From source file:com.kasungunathilaka.sarigama.service.PlayerService.java

private void setupNotification(Song song, String notificationType) {
    Bitmap remote_picture = null;//from  w  ww  .  j a v  a2  s .  c o m
    RemoteViews smallNotification;
    RemoteViews bigNotification;
    Notification notification;

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    if (song != null) {
        if (song.getSongImage().contentEquals("http://sarigama.lk/img/songs/default.jpg")) {
            remote_picture = BitmapFactory.decodeResource(PlayerService.this.getResources(),
                    R.drawable.default_song_art);
        } else {
            try {
                remote_picture = BitmapFactory
                        .decodeStream((InputStream) new URL(song.getSongImage()).getContent());
            } catch (Exception e) {
                remote_picture = BitmapFactory.decodeResource(PlayerService.this.getResources(),
                        R.drawable.default_song_art);
                e.printStackTrace();
            }
        }
    }

    switch (notificationType) {
    case HomeActivity.NOTIFICATION_TYPE_PLAY:
        try {
            smallNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_small);
            smallNotification.setImageViewBitmap(R.id.ivSmallNotificationSongImage, remote_picture);
            smallNotification.setTextViewText(R.id.tvSmallNotificationSong, song.getSongTitle());
            smallNotification.setTextViewText(R.id.tvSmallNotificationArtist, song.getSongArtist());
            smallNotification.setImageViewResource(R.id.ibSmallPlay, R.drawable.ic_pause_white);
            smallNotification.setOnClickPendingIntent(R.id.flSmallPlay, playIntent);
            smallNotification.setOnClickPendingIntent(R.id.ivSmallNext, nextIntent);

            bigNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_big);
            bigNotification.setImageViewBitmap(R.id.ivBigNotificationSongImage, remote_picture);
            bigNotification.setTextViewText(R.id.tvBigNotificationSong, song.getSongTitle());
            bigNotification.setTextViewText(R.id.tvBigNotificationArtist, song.getSongArtist());
            bigNotification.setImageViewResource(R.id.ibBigPlay, R.drawable.ic_pause_white);
            bigNotification.setOnClickPendingIntent(R.id.ibBigNext, nextIntent);
            bigNotification.setOnClickPendingIntent(R.id.ibBigPlay, playIntent);
            bigNotification.setOnClickPendingIntent(R.id.ibBigPrevious, previousIntent);

            notification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_play_white)
                    .setAutoCancel(false).setOngoing(true).setContent(smallNotification)
                    .setContentIntent(startIntent).build();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                notification.bigContentView = bigNotification;
            }
            notificationManager.notify(NOTIFICATION_ID, notification);
        } catch (Exception e) {
            e.printStackTrace();
        }
        break;
    case HomeActivity.NOTIFICATION_TYPE_PAUSE:
        try {
            smallNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_small);
            smallNotification.setImageViewResource(R.id.ibSmallPlay, R.drawable.ic_play_white);

            bigNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_big);
            bigNotification.setImageViewBitmap(R.id.ivBigNotificationSongImage, remote_picture);
            bigNotification.setTextViewText(R.id.tvBigNotificationSong, song.getSongTitle());
            bigNotification.setTextViewText(R.id.tvBigNotificationArtist, song.getSongArtist());
            bigNotification.setImageViewResource(R.id.ibBigPlay, R.drawable.ic_play_white);
            bigNotification.setOnClickPendingIntent(R.id.ibBigNext, nextIntent);
            bigNotification.setOnClickPendingIntent(R.id.ibBigPlay, playIntent);
            bigNotification.setOnClickPendingIntent(R.id.ibBigPrevious, previousIntent);

            notification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_pause_white)
                    .setOngoing(false).setAutoCancel(false).setContent(smallNotification)
                    .setContentIntent(startIntent).build();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                notification.bigContentView = bigNotification;
            }
            notificationManager.notify(NOTIFICATION_ID, notification);
        } catch (Exception e) {
            e.printStackTrace();
        }
        break;
    case HomeActivity.NOTIFICATION_TYPE_DISMISS:
        notificationManager.cancel(NOTIFICATION_ID);
        break;
    }
}

From source file:br.com.viniciuscr.notification2android.mediaPlayer.MediaPlaybackService.java

private void updateNotification() {
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.statusbar);
    views.setImageViewResource(R.id.icon, R.drawable.stat_notify_musicplayer);
    if (getAudioId() < 0) {
        // streaming
        views.setTextViewText(R.id.trackname, getPath());
        views.setTextViewText(R.id.artistalbum, null);
    } else {//from   w ww.j  a  v  a 2 s .  c  o  m
        String artist = getArtistName();
        views.setTextViewText(R.id.trackname, getTrackName());
        if (artist == null || artist.equals(MediaStore.UNKNOWN_STRING)) {
            artist = getString(R.string.unknown_artist_name);
        }
        String album = getAlbumName();
        if (album == null || album.equals(MediaStore.UNKNOWN_STRING)) {
            album = getString(R.string.unknown_album_name);
        }

        views.setTextViewText(R.id.artistalbum, getString(R.string.notification_artist_album, artist, album));
    }
    Notification status = new Notification();
    status.contentView = views;
    status.flags |= Notification.FLAG_ONGOING_EVENT;
    status.icon = R.drawable.stat_notify_musicplayer;
    status.contentIntent = PendingIntent.getActivity(this, 0,
            new Intent("com.android.music.PLAYBACK_VIEWER").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
    startForeground(PLAYBACKSERVICE_STATUS, status);
}

From source file:org.moire.ultrasonic.service.DownloadServiceImpl.java

@SuppressWarnings("IconColors")
private Notification buildForegroundNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_stat_ultrasonic);

    builder.setAutoCancel(false);/*from   w ww  .j  a v a 2 s  .  c  o  m*/
    builder.setOngoing(true);
    builder.setWhen(System.currentTimeMillis());

    RemoteViews contentView = new RemoteViews(this.getPackageName(), R.layout.notification);
    Util.linkButtons(this, contentView, false);
    RemoteViews bigView = new RemoteViews(this.getPackageName(), R.layout.notification_large);
    Util.linkButtons(this, bigView, false);

    builder.setContent(contentView);

    Intent notificationIntent = new Intent(this, DownloadActivity.class);
    builder.setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, 0));

    if (playerState == PlayerState.PAUSED || playerState == PlayerState.IDLE) {
        contentView.setImageViewResource(R.id.control_play, R.drawable.media_start_normal_dark);
        bigView.setImageViewResource(R.id.control_play, R.drawable.media_start_normal_dark);
    } else if (playerState == PlayerState.STARTED) {
        contentView.setImageViewResource(R.id.control_play, R.drawable.media_pause_normal_dark);
        bigView.setImageViewResource(R.id.control_play, R.drawable.media_pause_normal_dark);
    }

    final Entry song = currentPlaying.getSong();
    final String title = song.getTitle();
    final String text = song.getArtist();
    final String album = song.getAlbum();
    final int imageSize = Util.getNotificationImageSize(this);

    try {
        final Bitmap nowPlayingImage = FileUtil.getAlbumArtBitmap(this, currentPlaying.getSong(), imageSize,
                true);
        if (nowPlayingImage == null) {
            contentView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
            bigView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
        } else {
            contentView.setImageViewBitmap(R.id.notification_image, nowPlayingImage);
            bigView.setImageViewBitmap(R.id.notification_image, nowPlayingImage);
        }
    } catch (Exception x) {
        Log.w(TAG, "Failed to get notification cover art", x);
        contentView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
        bigView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
    }

    contentView.setTextViewText(R.id.trackname, title);
    bigView.setTextViewText(R.id.trackname, title);
    contentView.setTextViewText(R.id.artist, text);
    bigView.setTextViewText(R.id.artist, text);
    contentView.setTextViewText(R.id.album, album);
    bigView.setTextViewText(R.id.album, album);

    Notification notification = builder.build();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notification.bigContentView = bigView;
    }

    return notification;
}

From source file:mp.teardrop.PlaybackService.java

/**
 * Create a song notification. Call through the NotificationManager to
 * display it./*from w w w  .j  a  v a  2 s .  co  m*/
 *
 * @param song The Song to display information about.
 * @param state The state. Determines whether to show paused or playing icon.
 */
public Notification createNotification(Song song, int state) {
    boolean playing = (state & FLAG_PLAYING) != 0;

    RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification);
    RemoteViews expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded);

    Bitmap cover = song.getCover(this);
    if (cover == null) {
        views.setImageViewResource(R.id.cover, R.drawable.fallback_cover);
        expanded.setImageViewResource(R.id.cover, R.drawable.fallback_cover);
    } else {
        views.setImageViewBitmap(R.id.cover, cover);
        expanded.setImageViewBitmap(R.id.cover, cover);
    }

    int playButton = getPlayButtonResource(playing);

    views.setImageViewResource(R.id.play_pause, playButton);
    expanded.setImageViewResource(R.id.play_pause, playButton);

    ComponentName service = new ComponentName(this, PlaybackService.class);

    Intent previous = new Intent(PlaybackService.ACTION_PREVIOUS_SONG);
    previous.setComponent(service);
    expanded.setOnClickPendingIntent(R.id.previous, PendingIntent.getService(this, 0, previous, 0));

    Intent playPause = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK_NOTIFICATION);
    playPause.setComponent(service);
    views.setOnClickPendingIntent(R.id.play_pause, PendingIntent.getService(this, 0, playPause, 0));
    expanded.setOnClickPendingIntent(R.id.play_pause, PendingIntent.getService(this, 0, playPause, 0));

    Intent next = new Intent(PlaybackService.ACTION_NEXT_SONG);
    next.setComponent(service);
    views.setOnClickPendingIntent(R.id.next, PendingIntent.getService(this, 0, next, 0));
    expanded.setOnClickPendingIntent(R.id.next, PendingIntent.getService(this, 0, next, 0));

    Intent close = new Intent(PlaybackService.ACTION_CLOSE_NOTIFICATION);
    close.setComponent(service);
    views.setOnClickPendingIntent(R.id.close, PendingIntent.getService(this, 0, close, 0));
    expanded.setOnClickPendingIntent(R.id.close, PendingIntent.getService(this, 0, close, 0));

    views.setTextViewText(R.id.title, song.title);
    views.setTextViewText(R.id.artist, song.artist);
    expanded.setTextViewText(R.id.title, song.title);
    expanded.setTextViewText(R.id.album, song.album);
    expanded.setTextViewText(R.id.artist, song.artist);

    Notification notification = new Notification();
    notification.contentView = views;
    notification.icon = R.drawable.status_icon;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.contentIntent = mNotificationAction;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // expanded view is available since 4.1
        notification.bigContentView = expanded;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notification.visibility = Notification.VISIBILITY_PUBLIC;
    }

    //        if(mNotificationNag) {
    //            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    //                notification.priority = Notification.PRIORITY_MAX;
    //                notification.vibrate = new long[0]; // needed to get headsup
    //            } else {
    //                notification.tickerText = song.title + " - " + song.artist;
    //            }
    //        }

    return notification;
}