Example usage for android.widget RemoteViews RemoteViews

List of usage examples for android.widget RemoteViews RemoteViews

Introduction

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

Prototype

public RemoteViews(RemoteViews landscape, RemoteViews portrait) 

Source Link

Document

Create a new RemoteViews object that will inflate as the specified landspace or portrait RemoteViews, depending on the current configuration.

Usage

From source file:in.risysnetworks.shplayer.mediacontroller.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(SongDetail mSongDetail) {
    try {//from   w ww. j  ava 2  s  .c  o m
        String songName = mSongDetail.getTitle();
        String authorName = mSongDetail.getArtist();
        SongDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();

        RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),
                R.layout.player_small_notification);
        RemoteViews expandedView = null;
        if (supportBigNotifications) {
            expandedView = new RemoteViews(getApplicationContext().getPackageName(),
                    R.layout.player_big_notification);
        }

        Intent intent = new Intent(ApplicationSHPlayer.applicationContext, SHPlayerMainActivity.class);
        intent.setAction("openplayer");
        intent.setFlags(32768);
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationSHPlayer.applicationContext, 0,
                intent, 0);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.player).setContentIntent(contentIntent).setContentTitle(songName)
                .build();

        if (supportBigNotifications) {
            notification.bigContentView = expandedView;
        } else {
            notification.contentView = simpleContentView;
        }

        if (supportBigNotifications) {
            setListeners(expandedView);
        } else {
            setListeners(simpleContentView);
        }

        Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(ApplicationSHPlayer.applicationContext)
                : null;

        if (albumArt != null) {

            if (supportBigNotifications) {
                notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            } else {
                notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            }
        } else {

            if (supportBigNotifications) {
                notification.bigContentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            } else {
                notification.contentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            }
        }

        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        } else {
            notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
            notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
            notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
        }

        if (MediaController.getInstance().isAudioPaused()) {

            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            } else {
                notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
                notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            }
        } else {

            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            } else {
                notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
            }
        }

        if (supportBigNotifications) {
            notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
            notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
        } else {
            notification.contentView.setTextViewText(R.id.player_song_name, songName);
            notification.contentView.setTextViewText(R.id.player_author_name, authorName);
        }
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        startForeground(5, notification);

        if (remoteControlClient != null) {
            RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
            if (audioInfo != null && audioInfo.getCover(ApplicationSHPlayer.applicationContext) != null) {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover(ApplicationSHPlayer.applicationContext));
            }
            metadataEditor.apply();
            audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    } catch (Exception e) {
        e.printStackTrace();

        System.out.println(" error --> " + e.toString());
    }
}

From source file:com.mantraideas.androidaudio.manager.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(SongDetail mSongDetail) {
    try {/*from ww w . j  a  va  2 s.  c  o  m*/
        String songName = mSongDetail.getTitle();
        String authorName = mSongDetail.getArtist();
        SongDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();

        RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),
                R.layout.player_small_notification);
        RemoteViews expandedView = null;
        if (supportBigNotifications) {
            expandedView = new RemoteViews(getApplicationContext().getPackageName(),
                    R.layout.player_big_notification);
        }

        Intent intent = new Intent(MyApplication.applicationContext, MainActivity.class);
        intent.setAction("openplayer");
        intent.setFlags(32768);
        PendingIntent contentIntent = PendingIntent.getActivity(MyApplication.applicationContext, 0, intent, 0);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.player).setContentIntent(contentIntent).setContentTitle(songName)
                .build();

        notification.contentView = simpleContentView;
        if (supportBigNotifications) {
            notification.bigContentView = expandedView;
        }

        setListeners(simpleContentView);
        if (supportBigNotifications) {
            setListeners(expandedView);
        }

        Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(MyApplication.applicationContext) : null;

        if (albumArt != null) {
            notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            }
        } else {
            notification.contentView.setImageViewResource(R.id.player_album_art,
                    R.drawable.bg_default_album_art);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            }
        }
        notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
        notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        }

        if (MediaController.getInstance().isAudioPaused()) {
            notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
            notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            }
        } else {
            notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
            notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            }
        }

        notification.contentView.setTextViewText(R.id.player_song_name, songName);
        notification.contentView.setTextViewText(R.id.player_author_name, authorName);
        if (supportBigNotifications) {
            notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
            notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
        }
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        startForeground(5, notification);

        if (remoteControlClient != null) {
            RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
            if (audioInfo != null && audioInfo.getCover(MyApplication.applicationContext) != null) {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover(MyApplication.applicationContext));
            }
            metadataEditor.apply();
            audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.cowboycoders.cyclisimo.widgets.TrackWidgetProvider.java

/**
 * Gets the remote views.// ww  w . j  a v a  2 s . c  o m
 * 
 * @param context the context
 * @param trackId the track id
 * @param heightSize the layout height size
 */
private static RemoteViews getRemoteViews(Context context, long trackId, int heightSize) {
    int layout;
    switch (heightSize) {
    case 4:
        layout = R.layout.track_widget_4x4;
        break;
    case 3:
        layout = R.layout.track_widget_4x3;
        break;
    case 2:
        layout = R.layout.track_widget_4x2;
        break;
    case 1:
        layout = R.layout.track_widget_4x1;
        break;
    default:
        layout = R.layout.track_widget_4x2;
        break;
    }
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), layout);

    // Get the preferences
    long recordingTrackId = PreferencesUtils.getLong(context, R.string.recording_track_id_key);
    boolean isRecording = recordingTrackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
    boolean isPaused = PreferencesUtils.getBoolean(context, R.string.recording_track_paused_key,
            PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT);
    boolean metricUnits = PreferencesUtils.getBoolean(context, R.string.metric_units_key,
            PreferencesUtils.METRIC_UNITS_DEFAULT);
    boolean reportSpeed = PreferencesUtils.getBoolean(context, R.string.report_speed_key,
            PreferencesUtils.REPORT_SPEED_DEFAULT);
    int item1 = PreferencesUtils.getInt(context, R.string.track_widget_item1,
            PreferencesUtils.TRACK_WIDGET_ITEM1_DEFAULT);
    int item2 = PreferencesUtils.getInt(context, R.string.track_widget_item2,
            PreferencesUtils.TRACK_WIDGET_ITEM2_DEFAULT);

    // Get track and trip statistics
    MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
    if (trackId == -1L) {
        trackId = recordingTrackId;
    }
    Track track = trackId != -1L ? myTracksProviderUtils.getTrack(trackId)
            : myTracksProviderUtils.getLastTrack();
    TripStatistics tripStatistics = track == null ? null : track.getTripStatistics();

    updateStatisticsContainer(context, remoteViews, track);
    setItem(context, remoteViews, ITEM1_IDS, item1, tripStatistics, isRecording, isPaused, metricUnits,
            reportSpeed);
    setItem(context, remoteViews, ITEM2_IDS, item2, tripStatistics, isRecording, isPaused, metricUnits,
            reportSpeed);

    updateRecordButton(context, remoteViews, isRecording, isPaused);
    updateStopButton(context, remoteViews, isRecording);
    if (heightSize > 1) {
        int item3 = PreferencesUtils.getInt(context, R.string.track_widget_item3,
                PreferencesUtils.TRACK_WIDGET_ITEM3_DEFAULT);
        int item4 = PreferencesUtils.getInt(context, R.string.track_widget_item4,
                PreferencesUtils.TRACK_WIDGET_ITEM4_DEFAULT);
        setItem(context, remoteViews, ITEM3_IDS, item3, tripStatistics, isRecording, isPaused, metricUnits,
                reportSpeed);
        setItem(context, remoteViews, ITEM4_IDS, item4, tripStatistics, isRecording, isPaused, metricUnits,
                reportSpeed);
        updateRecordStatus(context, remoteViews, isRecording, isPaused);
    }
    return remoteViews;
}

From source file:co.codecrunch.musicplayerlite.manager.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(SongDetail mSongDetail) {
    try {//  w  w  w  .jav a 2  s. com
        String songName = mSongDetail.getTitle();
        String authorName = mSongDetail.getArtist();
        String albumName = mSongDetail.getDisplay_name();
        SongDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();

        RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),
                R.layout.player_small_notification);
        RemoteViews expandedView = null;
        if (supportBigNotifications) {
            expandedView = new RemoteViews(getApplicationContext().getPackageName(),
                    R.layout.player_big_notification);
        }

        Intent intent = new Intent(ApplicationMusicPlayer.applicationContext, MusicPlayerBaseActivity.class);
        intent.setAction("openplayer");
        intent.setFlags(32768);
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationMusicPlayer.applicationContext, 0,
                intent, 0);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.player).setContentIntent(contentIntent).setContentTitle(songName)
                .build();

        notification.contentView = simpleContentView;
        if (supportBigNotifications) {
            notification.bigContentView = expandedView;
        }

        setListeners(simpleContentView);
        if (supportBigNotifications) {
            setListeners(expandedView);
        }

        Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(ApplicationMusicPlayer.applicationContext)
                : null;

        if (albumArt != null) {
            notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            }
        } else {
            notification.contentView.setImageViewResource(R.id.player_album_art,
                    R.drawable.bg_default_album_art);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            }
        }
        notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
        notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        }

        if (MediaController.getInstance().isAudioPaused()) {
            notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
            notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            }
        } else {
            notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
            notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            }
        }

        notification.contentView.setTextViewText(R.id.player_song_name, songName);
        notification.contentView.setTextViewText(R.id.player_author_name, authorName);
        if (supportBigNotifications) {
            notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
            notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
            //                notification.bigContentView.setTextViewText(R.id.player_albumname, albumName);
        }
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        startForeground(5, notification);

        if (remoteControlClient != null) {
            RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
            if (audioInfo != null && audioInfo.getCover(ApplicationMusicPlayer.applicationContext) != null) {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover(ApplicationMusicPlayer.applicationContext));
            }
            metadataEditor.apply();
            audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.opensilk.fuzzyclock.FuzzyWidgetService.java

/**
 * Updates single appwidget, will schedule next update based on current logic
 * @param id/*from  w w w.  ja  va 2 s  .  c  o m*/
 */
@DebugLog
private void updateWidget(int id) {
    FuzzyPrefs settings = sWidgetSettings.get((Integer) id);
    if (settings == null) {
        return; // Once setup is done we will be called again.
    }
    if (LOGV)
        Log.v(TAG, "Updating widget id=" + id + " " + settings.toString());
    mFuzzyClock.loadPreferences(settings);
    mFuzzyClock.setDateFormat();
    mFuzzyClock.updateTime();
    Bitmap bitmap = mFuzzyClock.createBitmap();
    if (bitmap == null) {
        return;
    }
    // build onClick intent
    Intent intent = new Intent(mContext, FuzzyWidgetSettings.class);
    intent.setAction(String.format(Locale.US, "dummy_%d", id));
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
    PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // send bitmap to remote view
    RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.fuzzy_widget);
    views.setImageViewBitmap(R.id.fuzzy_clock_image, bitmap);
    views.setContentDescription(R.id.fuzzy_clock_image, mFuzzyClock.getContentDescription());
    views.setOnClickPendingIntent(R.id.fuzzy_clock_image, pi);
    mWidgetManager.updateAppWidget(id, views);
    scheduleUpdate(mFuzzyClock.getLogic().getCalendar().getTimeInMillis(),
            mFuzzyClock.getLogic().getNextIntervalMilli(), id);
}

From source file:android.support.v7.app.NotificationCompatImplBase.java

private static RemoteViews applyStandardTemplate(Context context, CharSequence contentTitle,
        CharSequence contentText, CharSequence contentInfo, int number, Bitmap largeIcon, CharSequence subText,
        boolean useChronometer, long when, int resId, boolean fitIn1U) {
    RemoteViews contentView = new RemoteViews(context.getPackageName(), resId);
    boolean showLine3 = false;
    boolean showLine2 = false;

    // On versions before Jellybean, the large icon was shown by SystemUI, so we need to hide
    // it here.//www .  j  a v  a 2 s  .  com
    if (largeIcon != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        contentView.setViewVisibility(R.id.icon, View.VISIBLE);
        contentView.setImageViewBitmap(R.id.icon, largeIcon);
    } else {
        contentView.setViewVisibility(R.id.icon, View.GONE);
    }
    if (contentTitle != null) {
        contentView.setTextViewText(R.id.title, contentTitle);
    }
    if (contentText != null) {
        contentView.setTextViewText(R.id.text, contentText);
        showLine3 = true;
    }
    if (contentInfo != null) {
        contentView.setTextViewText(R.id.info, contentInfo);
        contentView.setViewVisibility(R.id.info, View.VISIBLE);
        showLine3 = true;
    } else if (number > 0) {
        final int tooBig = context.getResources().getInteger(R.integer.status_bar_notification_info_maxnum);
        if (number > tooBig) {
            contentView.setTextViewText(R.id.info,
                    context.getResources().getString(R.string.status_bar_notification_info_overflow));
        } else {
            NumberFormat f = NumberFormat.getIntegerInstance();
            contentView.setTextViewText(R.id.info, f.format(number));
        }
        contentView.setViewVisibility(R.id.info, View.VISIBLE);
        showLine3 = true;
    } else {
        contentView.setViewVisibility(R.id.info, View.GONE);
    }

    // Need to show three lines? Only allow on Jellybean+
    if (subText != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        contentView.setTextViewText(R.id.text, subText);
        if (contentText != null) {
            contentView.setTextViewText(R.id.text2, contentText);
            contentView.setViewVisibility(R.id.text2, View.VISIBLE);
            showLine2 = true;
        } else {
            contentView.setViewVisibility(R.id.text2, View.GONE);
        }
    }

    // RemoteViews.setViewPadding and RemoteViews.setTextViewTextSize is not available on ICS-
    if (showLine2 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (fitIn1U) {
            // need to shrink all the type to make sure everything fits
            final Resources res = context.getResources();
            final float subTextSize = res.getDimensionPixelSize(R.dimen.notification_subtext_size);
            contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
        }
        // vertical centering
        contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
    }

    if (when != 0) {
        if (useChronometer) {
            contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
            contentView.setLong(R.id.chronometer, "setBase",
                    when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
            contentView.setBoolean(R.id.chronometer, "setStarted", true);
        } else {
            contentView.setViewVisibility(R.id.time, View.VISIBLE);
            contentView.setLong(R.id.time, "setTime", when);
        }
    }
    contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
    return contentView;
}

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();/*  w w w  . ja  v a  2s .c o  m*/
    } 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:net.pmarks.chromadoze.NoiseService.java

private void addButtonToNotification(Notification n) {
    // Create a new RV with a Stop button.
    RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notification_with_stop_button);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0,
            newStopIntent(this, R.string.stop_reason_notification), PendingIntent.FLAG_CANCEL_CURRENT);
    rv.setOnClickPendingIntent(R.id.stop_button, pendingIntent);

    // Pre-render the original RV, and copy some of the colors.
    final View inflated = n.contentView.apply(this, new FrameLayout(this));
    final TextView titleText = findTextView(inflated, getString(R.string.app_name));
    final TextView defaultText = findTextView(inflated, getString(R.string.notification_text));
    rv.setInt(R.id.divider, "setBackgroundColor", defaultText.getTextColors().getDefaultColor());
    rv.setInt(R.id.stop_button_square, "setBackgroundColor", titleText.getTextColors().getDefaultColor());

    // Insert a copy of the original RV into the new one.
    rv.addView(R.id.notification_insert, n.contentView.clone());

    // Splice everything back into the original's root view.
    int id = Resources.getSystem().getIdentifier("status_bar_latest_event_content", "id", "android");
    n.contentView.removeAllViews(id);//w w w . j  a v a2 s  . com
    n.contentView.addView(id, rv);
}

From source file:com.freshplanet.nativeExtensions.C2DMBroadcastReceiver.java

/**
 * Get the parameters from the message and create a notification from it.
 * @param context// www  . ja v  a2 s.  c  om
 * @param intent
 */
public void handleMessage(Context context, Intent intent) {
    try {
        registerResources(context);
        extractColors(context);

        FREContext ctxt = C2DMExtension.context;

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // icon is required for notification.
        // @see http://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar.html

        int icon = notificationIcon;
        long when = System.currentTimeMillis();

        // json string

        String parameters = intent.getStringExtra("parameters");
        String facebookId = null;
        JSONObject object = null;
        if (parameters != null) {
            try {
                object = (JSONObject) new JSONTokener(parameters).nextValue();
            } catch (Exception e) {
                Log.d(TAG, "cannot parse the object");
            }
        }
        if (object != null && object.has("facebookId")) {
            facebookId = object.getString("facebookId");
        }

        CharSequence tickerText = intent.getStringExtra("tickerText");
        CharSequence contentTitle = intent.getStringExtra("contentTitle");
        CharSequence contentText = intent.getStringExtra("contentText");

        Intent notificationIntent = new Intent(context, Class.forName(context.getPackageName() + ".AppEntry"));

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        Notification notification = new Notification(icon, tickerText, when);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        RemoteViews contentView = new RemoteViews(context.getPackageName(), customLayout);

        contentView.setTextViewText(customLayoutTitle, contentTitle);
        contentView.setTextViewText(customLayoutDescription, contentText);

        contentView.setTextColor(customLayoutTitle, notification_text_color);
        contentView.setFloat(customLayoutTitle, "setTextSize",
                notification_title_size_factor * notification_text_size);
        contentView.setTextColor(customLayoutDescription, notification_text_color);
        contentView.setFloat(customLayoutDescription, "setTextSize",
                notification_description_size_factor * notification_text_size);

        if (facebookId != null) {
            Log.d(TAG, "bitmap not null");
            CreateNotificationTask cNT = new CreateNotificationTask();
            cNT.setParams(customLayoutImageContainer, NotifId, nm, notification, contentView);
            String src = "http://graph.facebook.com/" + facebookId + "/picture?type=normal";
            URL url = new URL(src);
            cNT.execute(url);
        } else {
            Log.d(TAG, "bitmap null");
            contentView.setImageViewResource(customLayoutImageContainer, customLayoutImage);
            notification.contentView = contentView;
            nm.notify(NotifId, notification);
        }
        NotifId++;

        if (ctxt != null) {
            parameters = parameters == null ? "" : parameters;
            ctxt.dispatchStatusEventAsync("COMING_FROM_NOTIFICATION", parameters);
        }

    } catch (Exception e) {
        Log.e(TAG, "Error activating application:", e);
    }
}

From source file:com.putlocker.upload.DownloadService.java

/**
 * @param job The job to notify about//from  w  ww  .j  a  v a2  s .  c om
 * @param context The context to use
 * Show a notification while this service is running.
 */
public Notification createDownloadNotification(PutlockerUpDownloadJob job, Context context, int progress) {
    CharSequence text;
    if (job.getJobType() == PutlockerUpDownloadJob.DOWNLOAD_JOB) {
        text = context.getText(R.string.remote_service_started);
    } else {
        text = context.getText(R.string.service_upload_started);
    }

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, PutlockerTransferList.class), 0);
    // Set the icon, scrolling text and timestamp
    Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
    notification.contentView = new RemoteViews(context.getApplicationContext().getPackageName(),
            R.layout.download_progress);

    notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_launcher);

    if (job.getJobType() == PutlockerUpDownloadJob.DOWNLOAD_JOB) {

        if (FileFactory.typeFromFileName(job.getName()) == FileType.FILE_TYPE_VIDEO) {
            notification.contentIntent = PendingIntent.getActivity(this, 0,
                    getIntentForJob((PutlockerDownloadJob) job), Intent.FLAG_ACTIVITY_NEW_TASK);
        } else {
            notification.contentIntent = contentIntent;
        }

        notification.contentView.setTextViewText(R.id.status_text,
                context.getString(R.string.download_started, job.getName()));
    } else {
        notification.contentView.setTextViewText(R.id.status_text,
                context.getString(R.string.upload_started, job.getName()));
        notification.contentIntent = contentIntent;
    }

    notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
    return notification;
}