List of usage examples for android.widget RemoteViews setImageViewBitmap
public void setImageViewBitmap(int viewId, Bitmap bitmap)
From source file:github.madmarty.madsonic.util.Util.java
private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean playing) { // Use the same text for the ticker and the expanded notification String title = song.getTitle(); String arist = song.getArtist(); String album = song.getAlbum(); // Set the album art. try {/*from w ww .j a v a 2s. c o m*/ int size = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight(); Bitmap bitmap = FileUtil.getAlbumArtBitmap(context, song, size); if (bitmap == null) { // set default album art rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album); } else { rv.setImageViewBitmap(R.id.notification_image, bitmap); } } catch (Exception x) { LOG.warn("Failed to get notification cover art", x); rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album); } rv.setImageViewResource(R.id.control_starred, song.isStarred() ? android.R.drawable.btn_star_big_on : android.R.drawable.btn_star_big_off); // set the text for the notifications rv.setTextViewText(R.id.notification_title, title); rv.setTextViewText(R.id.notification_artist, arist); rv.setTextViewText(R.id.notification_album, album); Pair<Integer, Integer> colors = getNotificationTextColors(context); if (colors.getFirst() != null) { rv.setTextColor(R.id.notification_title, colors.getFirst()); } if (colors.getSecond() != null) { rv.setTextColor(R.id.notification_artist, colors.getSecond()); } if (!playing) { rv.setImageViewResource(R.id.control_pause, R.drawable.notification_play); rv.setImageViewResource(R.id.control_previous, R.drawable.notification_stop); } // Create actions for media buttons PendingIntent pendingIntent; if (playing) { Intent prevIntent = new Intent("KEYCODE_MEDIA_PREVIOUS"); prevIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); prevIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS)); pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0); rv.setOnClickPendingIntent(R.id.control_previous, pendingIntent); } else { Intent prevIntent = new Intent("KEYCODE_MEDIA_STOP"); prevIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); prevIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP)); pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0); rv.setOnClickPendingIntent(R.id.control_previous, pendingIntent); } Intent starredIntent = new Intent("KEYCODE_MEDIA_STARRED"); starredIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); starredIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_STAR)); pendingIntent = PendingIntent.getService(context, 0, starredIntent, 0); rv.setOnClickPendingIntent(R.id.control_starred, pendingIntent); Intent pauseIntent = new Intent("KEYCODE_MEDIA_PLAY_PAUSE"); pauseIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); pauseIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); pendingIntent = PendingIntent.getService(context, 0, pauseIntent, 0); rv.setOnClickPendingIntent(R.id.control_pause, pendingIntent); Intent nextIntent = new Intent("KEYCODE_MEDIA_NEXT"); nextIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); nextIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT)); pendingIntent = PendingIntent.getService(context, 0, nextIntent, 0); rv.setOnClickPendingIntent(R.id.control_next, pendingIntent); }
From source file:com.darshancomputing.BatteryIndicatorPro.BatteryInfoService.java
private void updateWidgets(BatteryInfo info) { //Intent mainWindowIntent = new Intent(this, BatteryInfoActivity.class); //PendingIntent mainWindowPendingIntent = PendingIntent.getActivity(this, RC_MAIN, mainWindowIntent, 0); //PendingIntent currentInfoPendingIntent = PendingIntent.getActivity(this, RC_MAIN, currentInfoIntent, 0); if (info == null) { cwbg.setLevel(0);/*from ww w . ja va 2 s .c om*/ } else { bl.setLevel(info.percent); cwbg.setLevel(info.percent); } for (Integer widgetId : widgetIds) { RemoteViews rv; android.appwidget.AppWidgetProviderInfo awpInfo = widgetManager.getAppWidgetInfo(widgetId); if (awpInfo == null) continue; // Based on Developer Console crash reports, this can be null sometimes int initLayout = awpInfo.initialLayout; if (initLayout == R.layout.circle_app_widget) { rv = new RemoteViews(getPackageName(), R.layout.circle_app_widget); rv.setImageViewBitmap(R.id.circle_widget_image_view, cwbg.getBitmap()); } else { rv = new RemoteViews(getPackageName(), R.layout.full_app_widget); if (info == null) { rv.setImageViewBitmap(R.id.battery_level_view, cwbg.getBitmap()); rv.setTextViewText(R.id.fully_charged, ""); rv.setTextViewText(R.id.time_remaining, ""); rv.setTextViewText(R.id.until_what, ""); } else { rv.setImageViewBitmap(R.id.battery_level_view, bl.getBitmap()); if (info.prediction.what == BatteryInfo.Prediction.NONE) { rv.setTextViewText(R.id.fully_charged, str.timeRemaining(info)); rv.setTextViewText(R.id.time_remaining, ""); rv.setTextViewText(R.id.until_what, ""); } else { rv.setTextViewText(R.id.fully_charged, ""); rv.setTextViewText(R.id.time_remaining, str.timeRemaining(info)); rv.setTextViewText(R.id.until_what, str.untilWhat(info)); } } } if (info == null) rv.setTextViewText(R.id.level, "XX" + str.percent_symbol); else rv.setTextViewText(R.id.level, "" + info.percent + str.percent_symbol); rv.setOnClickPendingIntent(R.id.widget_layout, currentInfoPendingIntent); widgetManager.updateAppWidget(widgetId, rv); } }
From source file:com.yamin.kk.service.AudioService.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void showNotification() { try {//w ww . j a va 2 s.co m Bitmap cover = AudioUtil.getCover(this, getCurrentMedia(), 64); String title = getCurrentMedia().getTitle(); String artist = getCurrentMedia().getArtist(); String album = getCurrentMedia().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 (Util.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 : R.drawable.ic_play); 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 : R.drawable.ic_play); 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(Util.isJellyBeanOrLater() ? artist : getCurrentMedia().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:uk.org.ngo.squeezer.util.ImageWorker.java
/** * Loads the requested image in to an {@link ImageView} in the given {@link RemoteViews} * and updates the notification when done. * * @param context system context//from w w w. j a va 2 s . c om * @param data The URL of the image to download * @param remoteViews The {@link RemoteViews} that contains the {@link ImageView} * @param viewId The identifier for the {@link ImageView} * @param width Resize the image to this width (and save it in the memory cache as such) * @param height Resize the image to this height (and save it in the memory cache as such) * @param nm The notification manager * @param notificationId Identifier of the notification to update * @param notification The notification to post */ public void loadImage(Context context, final Object data, final RemoteViews remoteViews, @IdRes int viewId, int width, int height, NotificationManagerCompat nm, int notificationId, Notification notification) { Bitmap bitmap = null; String memCacheKey = hashKeyForMemory(String.valueOf(data), width, height); if (mImageCache != null) { bitmap = mImageCache.getBitmapFromMemCache(memCacheKey); } if (bitmap != null) { // Bitmap found in memory cache if (BuildConfig.DEBUG) { addDebugSwatch(new Canvas(bitmap), mCacheDebugColorMemory); } remoteViews.setImageViewBitmap(viewId, bitmap); nm.notify(notificationId, notification); } else { final RemoteViewBitmapWorkerTask task = new RemoteViewBitmapWorkerTask(remoteViews, viewId, nm, notificationId, notification); final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task); remoteViews.setImageViewBitmap(viewId, asyncDrawable.getBitmap()); // NOTE: This uses a custom version of AsyncTask that has been pulled from the // framework and slightly modified. Refer to the docs at the top of the class // for more info on what was changed. task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR, new BitmapWorkerTaskParams(width, height, data, memCacheKey)); } }
From source file:leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService.java
private void updateWidgets(PlaybackStatus playbackStatus) { final RemoteViews view = new RemoteViews(getPackageName(), R.layout.podcast_widget_player); if (playbackStatus == PlaybackStatus.PLAYING) { view.setImageViewResource(R.id.widget_play, R.drawable.media_player_pause_24x24); } else if (playbackStatus == PlaybackStatus.PAUSED) { view.setImageViewResource(R.id.widget_play, R.drawable.media_player_play_24x24); }// ww w.ja v a 2 s. c o m view.setTextViewText(R.id.widget_title, activeAudio.getTitle()); view.setTextViewText(R.id.widget_length, activeAudio.getDuration()); Picasso.with(getBaseContext()).setLoggingEnabled(true); Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { view.setImageViewBitmap(R.id.widget_thumbail, bitmap); } @Override public void onBitmapFailed(Drawable errorDrawable) { view.setImageViewResource(R.id.widget_thumbail, R.drawable.picture); } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { view.setImageViewResource(R.id.widget_thumbail, R.drawable.picture); } }; Picasso.with(getBaseContext()).load(activeAudio.getPoster()).into(target); // Push update for this widget to the home screen ComponentName thisWidget = new ComponentName(this, PodcastWidgetProvider.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(thisWidget, view); }
From source file:org.videolan.vlc.audio.AudioService.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void showNotification() { try {//from w ww .ja 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.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:org.videolan.vlc.PlaybackService.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void showNotification() { try {// ww w. j a v a 2 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.kasungunathilaka.sarigama.service.PlayerService.java
private void setupNotification(Song song, String notificationType) { Bitmap remote_picture = null;//from w ww .j a v a 2 s. c om 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:com.irccloud.android.data.collection.NotificationsList.java
@SuppressLint("NewApi") private android.app.Notification buildNotification(String ticker, int cid, int bid, long[] eids, String title, String text, int count, Intent replyIntent, String network, ArrayList<Notification> messages, NotificationCompat.Action otherAction, Bitmap largeIcon, Bitmap wearBackground) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel c = new NotificationChannel(String.valueOf(bid), title, NotificationManager.IMPORTANCE_HIGH); c.setGroup(String.valueOf(cid)); ((NotificationManager) IRCCloudApplication.getInstance().getApplicationContext() .getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(c); }//from ww w . j av a 2 s. c o m SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext()); int defaults = 0; NotificationCompat.Builder builder = new NotificationCompat.Builder( IRCCloudApplication.getInstance().getApplicationContext(), String.valueOf(bid)) .setContentTitle( title + ((network != null && !network.equals(title)) ? (" (" + network + ")") : "")) .setContentText(Html.fromHtml(text)).setAutoCancel(true).setTicker(ticker) .setWhen(eids[0] / 1000).setSmallIcon(R.drawable.ic_stat_notify).setLargeIcon(largeIcon) .setColor(IRCCloudApplication.getInstance().getApplicationContext().getResources() .getColor(R.color.ic_background)) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) .setCategory(NotificationCompat.CATEGORY_MESSAGE) .setPriority(hasTouchWiz() ? NotificationCompat.PRIORITY_DEFAULT : NotificationCompat.PRIORITY_HIGH) .setOnlyAlertOnce(false); if (ticker != null && (System.currentTimeMillis() - prefs.getLong("lastNotificationTime", 0)) > 2000) { String ringtone = prefs.getString("notify_ringtone", "android.resource://" + IRCCloudApplication.getInstance().getApplicationContext().getPackageName() + "/" + R.raw.digit); if (ringtone.length() > 0) builder.setSound(Uri.parse(ringtone)); } int led_color = Integer.parseInt(prefs.getString("notify_led_color", "1")); if (led_color == 1) { defaults = android.app.Notification.DEFAULT_LIGHTS; } else if (led_color == 2) { builder.setLights(0xFF0000FF, 500, 500); } if (prefs.getBoolean("notify_vibrate", true) && ticker != null && (System.currentTimeMillis() - prefs.getLong("lastNotificationTime", 0)) > 2000) defaults |= android.app.Notification.DEFAULT_VIBRATE; else builder.setVibrate(new long[] { 0L }); builder.setDefaults(defaults); SharedPreferences.Editor editor = prefs.edit(); editor.putLong("lastNotificationTime", System.currentTimeMillis()); editor.commit(); Intent i = new Intent(); i.setComponent(new ComponentName(IRCCloudApplication.getInstance().getApplicationContext().getPackageName(), "com.irccloud.android.MainActivity")); i.putExtra("bid", bid); i.setData(Uri.parse("bid://" + bid)); Intent dismiss = new Intent(IRCCloudApplication.getInstance().getApplicationContext().getResources() .getString(R.string.DISMISS_NOTIFICATION)); dismiss.setData(Uri.parse("irccloud-dismiss://" + bid)); dismiss.putExtra("bid", bid); dismiss.putExtra("eids", eids); PendingIntent dismissPendingIntent = PendingIntent.getBroadcast( IRCCloudApplication.getInstance().getApplicationContext(), 0, dismiss, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent( PendingIntent.getActivity(IRCCloudApplication.getInstance().getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT)); builder.setDeleteIntent(dismissPendingIntent); WearableExtender wearableExtender = new WearableExtender(); wearableExtender.setBackground(wearBackground); if (messages != null && messages.size() > 0) { StringBuilder weartext = new StringBuilder(); String servernick = getServerNick(messages.get(0).cid); NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle(servernick); style.setConversationTitle(title + ((network != null) ? (" (" + network + ")") : "")); for (Notification n : messages) { if (n != null && n.message != null && n.message.length() > 0) { if (weartext.length() > 0) weartext.append("<br/>"); if (n.message_type.equals("buffer_me_msg")) { style.addMessage(Html.fromHtml(n.message).toString(), n.eid / 1000, " " + ((n.nick == null) ? servernick : n.nick)); weartext.append("<b> ").append((n.nick == null) ? servernick : n.nick).append("</b> ") .append(n.message); } else { style.addMessage(Html.fromHtml(n.message).toString(), n.eid / 1000, n.nick); weartext.append("<b><").append((n.nick == null) ? servernick : n.nick) .append("></b> ").append(n.message); } } } ArrayList<String> history = new ArrayList<>(messages.size()); for (int j = messages.size() - 1; j >= 0; j--) { Notification n = messages.get(j); if (n != null) { if (n.nick == null) history.add(Html.fromHtml(n.message).toString()); else break; } } builder.setRemoteInputHistory(history.toArray(new String[history.size()])); builder.setStyle(style); if (messages.size() > 1) { wearableExtender.addPage( new NotificationCompat.Builder(IRCCloudApplication.getInstance().getApplicationContext()) .setContentText(Html.fromHtml(weartext.toString())) .extend(new WearableExtender().setStartScrollBottom(true)).build()); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { weartext.setLength(0); int j = 0; for (Notification n : messages) { if (messages.size() - ++j < 3) { if (n != null && n.message != null && n.message.length() > 0) { if (weartext.length() > 0) weartext.append("<br/>"); if (n.message_type.equals("buffer_me_msg")) { weartext.append("<b> ").append((n.nick == null) ? servernick : n.nick) .append("</b> ").append(n.message); } else { weartext.append("<b><").append((n.nick == null) ? servernick : n.nick) .append("></b> ").append(n.message); } } } } RemoteViews bigContentView = new RemoteViews( IRCCloudApplication.getInstance().getApplicationContext().getPackageName(), R.layout.notification_expanded); bigContentView.setTextViewText(R.id.title, title + (!title.equals(network) ? (" (" + network + ")") : "")); bigContentView.setTextViewText(R.id.text, Html.fromHtml(weartext.toString())); bigContentView.setImageViewBitmap(R.id.image, largeIcon); bigContentView.setLong(R.id.time, "setTime", eids[0] / 1000); if (count > 3) { bigContentView.setViewVisibility(R.id.more, View.VISIBLE); bigContentView.setTextViewText(R.id.more, "+" + (count - 3) + " more"); } else { bigContentView.setViewVisibility(R.id.more, View.GONE); } if (replyIntent != null && prefs.getBoolean("notify_quickreply", true)) { bigContentView.setViewVisibility(R.id.actions, View.VISIBLE); bigContentView.setViewVisibility(R.id.action_divider, View.VISIBLE); i = new Intent(IRCCloudApplication.getInstance().getApplicationContext(), QuickReplyActivity.class); i.setData(Uri.parse("irccloud-bid://" + bid)); i.putExtras(replyIntent); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent quickReplyIntent = PendingIntent.getActivity( IRCCloudApplication.getInstance().getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT); bigContentView.setOnClickPendingIntent(R.id.action_reply, quickReplyIntent); } builder.setCustomBigContentView(bigContentView); } } if (replyIntent != null) { PendingIntent replyPendingIntent = PendingIntent.getService( IRCCloudApplication.getInstance().getApplicationContext(), bid + 1, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { builder.addAction(new NotificationCompat.Action.Builder(0, "Reply", replyPendingIntent) .setAllowGeneratedReplies(true) .addRemoteInput( new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build()) .build()); } NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder( R.drawable.ic_wearable_reply, "Reply", replyPendingIntent).setAllowGeneratedReplies(true) .addRemoteInput( new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build()); NotificationCompat.Action.WearableExtender actionExtender = new NotificationCompat.Action.WearableExtender() .setHintLaunchesActivity(true).setHintDisplayActionInline(true); wearableExtender.addAction(actionBuilder.extend(actionExtender).build()); NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder( title + ((network != null) ? (" (" + network + ")") : "")) .setReadPendingIntent(dismissPendingIntent).setReplyAction(replyPendingIntent, new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build()); if (messages != null) { for (Notification n : messages) { if (n != null && n.nick != null && n.message != null && n.message.length() > 0) { if (n.buffer_type.equals("conversation")) { if (n.message_type.equals("buffer_me_msg")) unreadConvBuilder .addMessage(" " + n.nick + " " + Html.fromHtml(n.message).toString()); else unreadConvBuilder.addMessage(Html.fromHtml(n.message).toString()); } else { if (n.message_type.equals("buffer_me_msg")) unreadConvBuilder .addMessage(" " + n.nick + " " + Html.fromHtml(n.message).toString()); else unreadConvBuilder .addMessage(n.nick + " said: " + Html.fromHtml(n.message).toString()); } } } } else { unreadConvBuilder.addMessage(text); } unreadConvBuilder.setLatestTimestamp(eids[count - 1] / 1000); builder.extend(new NotificationCompat.CarExtender().setUnreadConversation(unreadConvBuilder.build())); } if (replyIntent != null && prefs.getBoolean("notify_quickreply", true) && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { i = new Intent(IRCCloudApplication.getInstance().getApplicationContext(), QuickReplyActivity.class); i.setData(Uri.parse("irccloud-bid://" + bid)); i.putExtras(replyIntent); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent quickReplyIntent = PendingIntent.getActivity( IRCCloudApplication.getInstance().getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(R.drawable.ic_action_reply, "Quick Reply", quickReplyIntent); } if (otherAction != null) { int drawable = 0; if (otherAction.getIcon() == R.drawable.ic_wearable_add) drawable = R.drawable.ic_action_add; else if (otherAction.getIcon() == R.drawable.ic_wearable_reply) drawable = R.drawable.ic_action_reply; builder.addAction( new NotificationCompat.Action(drawable, otherAction.getTitle(), otherAction.getActionIntent())); wearableExtender.addAction(otherAction); } builder.extend(wearableExtender); return builder.build(); }
From source file:com.gaze.webpaser.StackWidgetService.java
public RemoteViews getViewAt(int position) { Log.i(LOG_TAG, "getViewAt " + position); // position will always range from 0 to getCount() - 1. // We construct a remote views item based on our widget item xml file, // and set the // text based on the position. RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.item_widget); rv.setTextViewText(R.id.textView_title, mlist.get(position).getTitle()); String time = Util.getFormatTime(mlist.get(position).getTime()); rv.setTextViewText(R.id.textView3_time, time); // Next, we set a fill-intent which will be used to fill-in the pending // intent template // which is set on the collection view in StackWidgetProvider. Bundle extras = new Bundle(); extras.putInt(StackWidgetProvider.EXTRA_ITEM, position); Intent fillInIntent = new Intent(); fillInIntent.putExtras(extras);/*from w ww. j a v a 2s .c o m*/ rv.setOnClickFillInIntent(R.id.widget1, fillInIntent); // You can do heaving lifting in here, synchronously. For example, if // you need to // process an image, fetch something from the network, etc., it is ok to // do it here, // synchronously. A loading view will show up in lieu of the actual // contents in the // interim. // try { // System.out.println("Loading view " + position); // Thread.sleep(500); // } catch (InterruptedException e) { // e.printStackTrace(); // } Bitmap bitmap = getBitmap(GlobalData.baseUrl + mlist.get(position).getImagePath()); if (bitmap != null) rv.setImageViewBitmap(R.id.imageView1, bitmap); // Return the remote views object. return rv; }