List of usage examples for android.widget RemoteViews setOnClickPendingIntent
public void setOnClickPendingIntent(int viewId, PendingIntent pendingIntent)
From source file:com.android.mail.utils.NotificationActionUtils.java
public static Notification createUndoNotification(final Context context, final NotificationAction notificationAction, final int notificationId) { LogUtils.i(LOG_TAG, "createUndoNotification for %s", notificationAction.getNotificationActionType()); final NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.ic_notification_mail_24dp); builder.setWhen(notificationAction.getWhen()); builder.setCategory(NotificationCompat.CATEGORY_EMAIL); final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification); undoView.setTextViewText(R.id.description_text, context.getString(notificationAction.getActionTextResId())); final String packageName = context.getPackageName(); final Intent clickIntent = new Intent(NotificationActionIntentService.ACTION_UNDO); clickIntent.setPackage(packageName); clickIntent.setData(notificationAction.mConversation.uri); putNotificationActionExtra(clickIntent, notificationAction); final PendingIntent clickPendingIntent = PendingIntent.getService(context, notificationId, clickIntent, PendingIntent.FLAG_CANCEL_CURRENT); undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent); builder.setContent(undoView);/*from w w w . j ava 2s. c o m*/ // When the notification is cleared, we perform the destructive action final Intent deleteIntent = new Intent(NotificationActionIntentService.ACTION_DESTRUCT); deleteIntent.setPackage(packageName); deleteIntent.setData(notificationAction.mConversation.uri); putNotificationActionExtra(deleteIntent, notificationAction); final PendingIntent deletePendingIntent = PendingIntent.getService(context, notificationId, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setDeleteIntent(deletePendingIntent); final Notification notification = builder.build(); return notification; }
From source file:com.tct.mail.utils.NotificationActionUtils.java
public static Notification createUndoNotification(final Context context, final NotificationAction notificationAction, final int notificationId) { LogUtils.i(LOG_TAG, "createUndoNotification for %s", notificationAction.getNotificationActionType()); final NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.ic_notification_mail_24dp); builder.setWhen(notificationAction.getWhen()); final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification); undoView.setTextViewText(R.id.description_text, context.getString(notificationAction.getActionTextResId())); final String packageName = context.getPackageName(); final Intent clickIntent = new Intent(NotificationActionIntentService.ACTION_UNDO); clickIntent.setPackage(packageName); clickIntent.setData(notificationAction.mConversation.uri); putNotificationActionExtra(clickIntent, notificationAction); final PendingIntent clickPendingIntent = PendingIntent.getService(context, notificationId, clickIntent, PendingIntent.FLAG_CANCEL_CURRENT); undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent); builder.setContent(undoView);//from www. j av a 2s . c om // When the notification is cleared, we perform the destructive action final Intent deleteIntent = new Intent(NotificationActionIntentService.ACTION_DESTRUCT); deleteIntent.setPackage(packageName); deleteIntent.setData(notificationAction.mConversation.uri); putNotificationActionExtra(deleteIntent, notificationAction); final PendingIntent deletePendingIntent = PendingIntent.getService(context, notificationId, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setDeleteIntent(deletePendingIntent); final Notification notification = builder.build(); return notification; }
From source file:com.android.deskclock.data.StopwatchModel.java
/** * Updates the notification to reflect the latest state of the stopwatch and recorded laps. */// w w w . ja v a2 s .c o m void updateNotification() { final Stopwatch stopwatch = getStopwatch(); // Notification should be hidden if the stopwatch has no time or the app is open. if (stopwatch.isReset() || mNotificationModel.isApplicationInForeground()) { mNotificationManager.cancel(mNotificationModel.getStopwatchNotificationId()); return; } @StringRes final int eventLabel = R.string.label_notification; // Intent to load the app when the notification is tapped. final Intent showApp = new Intent(mContext, HandleDeskClockApiCalls.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingShowApp = PendingIntent.getActivity(mContext, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); // Compute some values required below. final boolean running = stopwatch.isRunning(); final String pname = mContext.getPackageName(); final Resources res = mContext.getResources(); final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime(); final RemoteViews collapsed = new RemoteViews(pname, R.layout.stopwatch_notif_collapsed); collapsed.setChronometer(R.id.swn_collapsed_chronometer, base, null, running); collapsed.setOnClickPendingIntent(R.id.swn_collapsed_hitspace, pendingShowApp); collapsed.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch); final RemoteViews expanded = new RemoteViews(pname, R.layout.stopwatch_notif_expanded); expanded.setChronometer(R.id.swn_expanded_chronometer, base, null, running); expanded.setOnClickPendingIntent(R.id.swn_expanded_hitspace, pendingShowApp); expanded.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch); @IdRes final int leftButtonId = R.id.swn_left_button; @IdRes final int rightButtonId = R.id.swn_right_button; if (running) { // Left button: Pause expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_pause_button)); setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_pause_24dp); final Intent pause = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(leftButtonId, pendingServiceIntent(pause)); // Right button: Add Lap if (canAddMoreLaps()) { expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_lap_button)); setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_sw_lap_24dp); final Intent lap = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(rightButtonId, pendingServiceIntent(lap)); expanded.setViewVisibility(rightButtonId, VISIBLE); } else { expanded.setViewVisibility(rightButtonId, INVISIBLE); } // Show the current lap number if any laps have been recorded. final int lapCount = getLaps().size(); if (lapCount > 0) { final int lapNumber = lapCount + 1; final String lap = res.getString(R.string.sw_notification_lap_number, lapNumber); collapsed.setTextViewText(R.id.swn_collapsed_laps, lap); collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE); expanded.setTextViewText(R.id.swn_expanded_laps, lap); expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE); } else { collapsed.setViewVisibility(R.id.swn_collapsed_laps, GONE); expanded.setViewVisibility(R.id.swn_expanded_laps, GONE); } } else { // Left button: Start expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_start_button)); setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_start_24dp); final Intent start = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(leftButtonId, pendingServiceIntent(start)); // Right button: Reset (HandleDeskClockApiCalls will also bring forward the app) expanded.setViewVisibility(rightButtonId, VISIBLE); expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_reset_button)); setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_reset_24dp); final Intent reset = new Intent(mContext, HandleDeskClockApiCalls.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(rightButtonId, pendingActivityIntent(reset)); // Indicate the stopwatch is paused. collapsed.setTextViewText(R.id.swn_collapsed_laps, res.getString(R.string.swn_paused)); collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE); expanded.setTextViewText(R.id.swn_expanded_laps, res.getString(R.string.swn_paused)); expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE); } // Swipe away will reset the stopwatch without bringing forward the app. final Intent reset = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Notification notification = new NotificationCompat.Builder(mContext).setLocalOnly(true) .setOngoing(running).setContent(collapsed).setAutoCancel(stopwatch.isPaused()) .setPriority(Notification.PRIORITY_MAX).setDeleteIntent(pendingServiceIntent(reset)) .setSmallIcon(R.drawable.ic_tab_stopwatch_activated).build(); notification.bigContentView = expanded; mNotificationManager.notify(mNotificationModel.getStopwatchNotificationId(), notification); }
From source file:org.artags.android.app.widget.AbstractWidgetProvider.java
private void updateTag(Tag tag) { setCurrentTag(tag);/*from w ww. java 2s. co m*/ RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.widget); remoteViews.setImageViewBitmap(R.id.widget_thumbnail, tag.getBitmap()); remoteViews.setTextViewText(R.id.widget_text, tag.getText()); Intent active = new Intent(mContext, getClass()); active.setAction(Constants.ACTION_SHOW_TAG); PendingIntent actionPendingIntent = PendingIntent.getBroadcast(mContext, 0, active, 0); remoteViews.setOnClickPendingIntent(R.id.widget_thumbnail, actionPendingIntent); if ((tag != null) && (remoteViews != null)) { mAppWidgetManager.updateAppWidget(mAppWidgetIds, remoteViews); Log.d(Constants.LOG_TAG, "Widget updated"); } }
From source file:org.xbmc.kore.service.NotificationObserver.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void buildNotification(PlayerType.GetActivePlayersReturnType getActivePlayerResult, PlayerType.PropertyValue getPropertiesResult, ListType.ItemsAll getItemResult) { final String title, underTitle, poster; int smallIcon, playPauseIcon, rewindIcon, ffIcon; boolean isVideo = ((getItemResult.type.equals(ListType.ItemsAll.TYPE_MOVIE)) || (getItemResult.type.equals(ListType.ItemsAll.TYPE_EPISODE))); switch (getItemResult.type) { case ListType.ItemsAll.TYPE_MOVIE: title = getItemResult.title;/* w w w. j ava 2s .c o m*/ underTitle = getItemResult.tagline; poster = getItemResult.thumbnail; smallIcon = R.drawable.ic_movie_white_24dp; break; case ListType.ItemsAll.TYPE_EPISODE: title = getItemResult.title; String seasonEpisode = String.format(mContext.getString(R.string.season_episode_abbrev), getItemResult.season, getItemResult.episode); underTitle = String.format("%s | %s", getItemResult.showtitle, seasonEpisode); poster = getItemResult.art.poster; smallIcon = R.drawable.ic_tv_white_24dp; break; case ListType.ItemsAll.TYPE_SONG: title = getItemResult.title; underTitle = getItemResult.displayartist + " | " + getItemResult.album; poster = getItemResult.thumbnail; smallIcon = R.drawable.ic_headset_white_24dp; break; case ListType.ItemsAll.TYPE_MUSIC_VIDEO: title = getItemResult.title; underTitle = Utils.listStringConcat(getItemResult.artist, ", ") + " | " + getItemResult.album; poster = getItemResult.thumbnail; smallIcon = R.drawable.ic_headset_white_24dp; break; default: // We don't know what this is, forget it return; } switch (getPropertiesResult.speed) { case 1: playPauseIcon = R.drawable.ic_pause_white_24dp; break; default: playPauseIcon = R.drawable.ic_play_arrow_white_24dp; break; } // Create the actions, depending on the type of media PendingIntent rewindPendingItent, ffPendingItent, playPausePendingIntent; playPausePendingIntent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_PLAY_PAUSE); if (getItemResult.type.equals(ListType.ItemsAll.TYPE_SONG)) { rewindPendingItent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_PREVIOUS); rewindIcon = R.drawable.ic_skip_previous_white_24dp; ffPendingItent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_NEXT); ffIcon = R.drawable.ic_skip_next_white_24dp; } else { rewindPendingItent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_REWIND); rewindIcon = R.drawable.ic_fast_rewind_white_24dp; ffPendingItent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_FAST_FORWARD); ffIcon = R.drawable.ic_fast_forward_white_24dp; } // Setup the collpased and expanded notifications final RemoteViews collapsedRV = new RemoteViews(mContext.getPackageName(), R.layout.notification_colapsed); collapsedRV.setImageViewResource(R.id.rewind, rewindIcon); collapsedRV.setOnClickPendingIntent(R.id.rewind, rewindPendingItent); collapsedRV.setImageViewResource(R.id.play, playPauseIcon); collapsedRV.setOnClickPendingIntent(R.id.play, playPausePendingIntent); collapsedRV.setImageViewResource(R.id.fast_forward, ffIcon); collapsedRV.setOnClickPendingIntent(R.id.fast_forward, ffPendingItent); collapsedRV.setTextViewText(R.id.title, title); collapsedRV.setTextViewText(R.id.text2, underTitle); final RemoteViews expandedRV = new RemoteViews(mContext.getPackageName(), R.layout.notification_expanded); expandedRV.setImageViewResource(R.id.rewind, rewindIcon); expandedRV.setOnClickPendingIntent(R.id.rewind, rewindPendingItent); expandedRV.setImageViewResource(R.id.play, playPauseIcon); expandedRV.setOnClickPendingIntent(R.id.play, playPausePendingIntent); expandedRV.setImageViewResource(R.id.fast_forward, ffIcon); expandedRV.setOnClickPendingIntent(R.id.fast_forward, ffPendingItent); expandedRV.setTextViewText(R.id.title, title); expandedRV.setTextViewText(R.id.text2, underTitle); final int expandedIconResId; if (isVideo) { expandedIconResId = R.id.icon_slim; expandedRV.setViewVisibility(R.id.icon_slim, View.VISIBLE); expandedRV.setViewVisibility(R.id.icon_square, View.GONE); } else { expandedIconResId = R.id.icon_square; expandedRV.setViewVisibility(R.id.icon_slim, View.GONE); expandedRV.setViewVisibility(R.id.icon_square, View.VISIBLE); } // Build the notification NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); final Notification notification = builder.setSmallIcon(smallIcon).setShowWhen(false).setOngoing(true) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setCategory(NotificationCompat.CATEGORY_TRANSPORT).setContentIntent(mRemoteStartPendingIntent) .setContent(collapsedRV).build(); // This is a convoluted way of loading the image and showing the // notification, but it's what works with Picasso and is efficient. // Here's what's going on: // // 1. The image is loaded asynchronously into a Target, and only after // it is loaded is the notification shown. Using targets is a lot more // efficient than letting Picasso load it directly into the // notification imageview, which causes a lot of flickering // // 2. The target needs to be static, because Picasso only keeps a weak // reference to it, so we need to keed a strong reference and reset it // to null when we're done. We also need to check if it is not null in // case a previous request hasn't finished yet. // // 3. We can only show the notification after the bitmap is loaded into // the target, so it is done in the callback // // 4. We specifically resize the image to the same dimensions used in // the remote, so that Picasso reuses it in the remote and here from the cache Resources resources = mContext.getResources(); final int posterWidth = resources.getDimensionPixelOffset(R.dimen.now_playing_poster_width); final int posterHeight = isVideo ? resources.getDimensionPixelOffset(R.dimen.now_playing_poster_height) : posterWidth; if (picassoTarget == null) { picassoTarget = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { showNotification(bitmap); } @Override public void onBitmapFailed(Drawable errorDrawable) { CharacterDrawable avatarDrawable = UIUtils.getCharacterAvatar(mContext, title); showNotification(Utils.drawableToBitmap(avatarDrawable, posterWidth, posterHeight)); } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } private void showNotification(Bitmap bitmap) { collapsedRV.setImageViewBitmap(R.id.icon, bitmap); if (Utils.isJellybeanOrLater()) { notification.bigContentView = expandedRV; expandedRV.setImageViewBitmap(expandedIconResId, bitmap); } NotificationManager notificationManager = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notification); picassoTarget = null; } }; // Load the image HostManager hostManager = HostManager.getInstance(mContext); hostManager.getPicasso().load(hostManager.getHostInfo().getImageUrl(poster)) .resize(posterWidth, posterHeight).into(picassoTarget); } }
From source file:com.onyx.deskclock.deskclock.data.StopwatchModel.java
/** * Updates the notification to reflect the latest state of the stopwatch and recorded laps. *//*w w w. j a v a 2 s .com*/ void updateNotification() { final Stopwatch stopwatch = getStopwatch(); // Notification should be hidden if the stopwatch has no time or the app is open. if (stopwatch.isReset() || mNotificationModel.isApplicationInForeground()) { mNotificationManager.cancel(mNotificationModel.getStopwatchNotificationId()); return; } @StringRes final int eventLabel = R.string.label_notification; // Intent to load the app when the notification is tapped. final Intent showApp = new Intent(mContext, HandleDeskClockApiCalls.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingShowApp = PendingIntent.getActivity(mContext, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); // Compute some values required below. final boolean running = stopwatch.isRunning(); final String pname = mContext.getPackageName(); final Resources res = mContext.getResources(); final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime(); final RemoteViews collapsed = new RemoteViews(pname, R.layout.stopwatch_notif_collapsed); collapsed.setChronometer(R.id.swn_collapsed_chronometer, base, null, running); collapsed.setOnClickPendingIntent(R.id.swn_collapsed_hitspace, pendingShowApp); collapsed.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch); final RemoteViews expanded = new RemoteViews(pname, R.layout.stopwatch_notif_expanded); expanded.setChronometer(R.id.swn_expanded_chronometer, base, null, running); expanded.setOnClickPendingIntent(R.id.swn_expanded_hitspace, pendingShowApp); expanded.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch); @IdRes final int leftButtonId = R.id.swn_left_button; @IdRes final int rightButtonId = R.id.swn_right_button; if (running) { // Left button: Pause expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_pause_button)); // setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_pause_24dp); final Intent pause = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(leftButtonId, pendingServiceIntent(pause)); // Right button: Add Lap if (canAddMoreLaps()) { expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_lap_button)); // setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_sw_lap_24dp); final Intent lap = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(rightButtonId, pendingServiceIntent(lap)); expanded.setViewVisibility(rightButtonId, VISIBLE); } else { expanded.setViewVisibility(rightButtonId, INVISIBLE); } // Show the current lap number if any laps have been recorded. final int lapCount = getLaps().size(); if (lapCount > 0) { final int lapNumber = lapCount + 1; final String lap = res.getString(R.string.sw_notification_lap_number, lapNumber); collapsed.setTextViewText(R.id.swn_collapsed_laps, lap); collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE); expanded.setTextViewText(R.id.swn_expanded_laps, lap); expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE); } else { collapsed.setViewVisibility(R.id.swn_collapsed_laps, GONE); expanded.setViewVisibility(R.id.swn_expanded_laps, GONE); } } else { // Left button: Start expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_start_button)); // setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_start_24dp); final Intent start = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(leftButtonId, pendingServiceIntent(start)); // Right button: Reset (HandleDeskClockApiCalls will also bring forward the app) expanded.setViewVisibility(rightButtonId, VISIBLE); expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_reset_button)); // setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_reset_24dp); final Intent reset = new Intent(mContext, HandleDeskClockApiCalls.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(rightButtonId, pendingActivityIntent(reset)); // Indicate the stopwatch is paused. collapsed.setTextViewText(R.id.swn_collapsed_laps, res.getString(R.string.swn_paused)); collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE); expanded.setTextViewText(R.id.swn_expanded_laps, res.getString(R.string.swn_paused)); expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE); } // Swipe away will reset the stopwatch without bringing forward the app. final Intent reset = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Notification notification = new NotificationCompat.Builder(mContext).setLocalOnly(true) .setOngoing(running).setContent(collapsed).setAutoCancel(stopwatch.isPaused()) .setPriority(Notification.PRIORITY_MAX).setDeleteIntent(pendingServiceIntent(reset)) .setSmallIcon(R.drawable.ic_tab_stopwatch_activated).build(); if (Build.VERSION.SDK_INT >= 16) { notification.bigContentView = expanded; } mNotificationManager.notify(mNotificationModel.getStopwatchNotificationId(), notification); }
From source file:de.ub0r.android.portaltimer.UpdateReceiver.java
private boolean updateNotification(final Context context) { Log.d(TAG, "updateNotification()"); lastUpdate = System.currentTimeMillis(); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); ArrayList<Timer> timers = new ArrayList<Timer>(); mNow = System.currentTimeMillis(); mNextTarget = 0;// www .j a va 2 s . com boolean alert = false; Log.d(TAG, "mNow: " + mNow); for (int j = 0; j < Timer.TIMER_IDS.length; j++) { Timer t = new Timer(context, j); timers.add(t); long tt = t.getTarget(); Log.d(TAG, "target(" + j + "): " + tt); if (tt > 0) { if (mNextTarget == 0 || tt < mNextTarget) { mNextTarget = tt; } if (tt < mNow) { alert = true; t.reset(context); } } } Log.d(TAG, "mNextTarget: " + mNextTarget); NotificationCompat.Builder b = new NotificationCompat.Builder(context); b.setPriority(1000); Intent i = new Intent(context, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); b.setContentIntent(PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT)); b.setContentTitle(context.getString(R.string.app_name)); b.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)); b.setSmallIcon(R.drawable.ic_stat_timer); b.setAutoCancel(false); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { // GB- b.setContentText(context.getString(R.string.notification_text, timers.get(0).getFormatted(), timers.get(1).getFormatted(), timers.get(2).getFormatted())); } else { // HC+ RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.notification); for (int j = 0; j < Timer.TIMER_IDS.length; j++) { v.setTextViewText(Timer.TIMER_IDS[j], timers.get(j).getFormatted().toString()); Intent ij = new Intent(Timer.TIMER_KEYS[j], null, context, UpdateReceiver.class); v.setOnClickPendingIntent(Timer.TIMER_IDS[j], PendingIntent.getBroadcast(context, 0, ij, PendingIntent.FLAG_UPDATE_CURRENT)); } v.setOnClickPendingIntent(R.id.settings, PendingIntent.getActivity(context, 0, new Intent(context, SettingsActivity.class), PendingIntent.FLAG_UPDATE_CURRENT)); b.setContent(v); } if (mNextTarget <= 0 && !alert) { // we don't need any notification b.setOngoing(false); nm.notify(0, b.build()); return false; } else if (alert) { // show notification without running Timer b.setOngoing(mNextTarget > 0); SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context); if (p.getBoolean("vibrate", true)) { b.setVibrate(VIBRATE); } String n = p.getString("notification", null); if (n == null) { // default b.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); } else if (n.length() > 1) { try { b.setSound(Uri.parse(n)); } catch (Exception e) { Log.e(TAG, "invalid notification uri", e); b.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); } } // else: silent nm.notify(0, b.build()); return true; } else { // show notification with running Timer b.setOngoing(true); nm.notify(0, b.build()); return true; } }
From source file:me.sandrin.xkcdwidget.XKCDAppWidgetProvider.java
private void updateRemoteViews(Context context, RemoteViews views) { if (title != null) { views.setTextViewText(R.id.title, title); }// w ww .j a v a 2 s . com if (image != null) { views.setImageViewBitmap(R.id.image, image); } else { views.setImageViewBitmap(R.id.image, null); } Intent goToSiteIntent = new Intent(Intent.ACTION_VIEW); goToSiteIntent.setData(Uri.parse("http://xkcd.com")); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, goToSiteIntent, 0); views.setOnClickPendingIntent(R.id.image, pendingIntent); if (altText != null) { views.setTextViewText(R.id.alt_text, altText); } Intent intent = new Intent(context, getClass()); intent.setAction(UPDATE); views.setOnClickPendingIntent(R.id.sync, PendingIntent.getBroadcast(context, 0, intent, 0)); }
From source file:com.audiokernel.euphonyrmt.service.NotificationHandler.java
/** * This method constructs the notification base, otherwise known as the collapsed notification. * The expanded notification method builds upon this method. * * @param resultView The RemoteView to begin with, be it new or from the current notification. *//*from w w w . j av a2s .co m*/ private void buildBaseNotification(final RemoteViews resultView) { final PendingIntent closeAction = buildPendingIntent(MPDroidService.ACTION_STOP); final PendingIntent nextAction = buildPendingIntent(MPDControl.ACTION_NEXT); resultView.setViewVisibility(R.id.notificationClose, View.VISIBLE); resultView.setOnClickPendingIntent(R.id.notificationClose, closeAction); updateStatePaused(resultView); resultView.setOnClickPendingIntent(R.id.notificationNext, nextAction); resultView.setImageViewResource(R.id.notificationIcon, AlbumCoverDownloadListener.getNoCoverResource()); }
From source file:com.syncedsynapse.kore2.service.NotificationService.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void buildNotification(PlayerType.GetActivePlayersReturnType getActivePlayerResult, PlayerType.PropertyValue getPropertiesResult, ListType.ItemsAll getItemResult) { final String title, underTitle, poster; int smallIcon, playPauseIcon, rewindIcon, ffIcon; boolean isVideo = ((getItemResult.type.equals(ListType.ItemsAll.TYPE_MOVIE)) || (getItemResult.type.equals(ListType.ItemsAll.TYPE_EPISODE))); switch (getItemResult.type) { case ListType.ItemsAll.TYPE_MOVIE: title = getItemResult.title;/* w w w . j ava2 s .c om*/ underTitle = getItemResult.tagline; poster = getItemResult.thumbnail; smallIcon = R.drawable.ic_movie_white_24dp; break; case ListType.ItemsAll.TYPE_EPISODE: title = getItemResult.title; String seasonEpisode = String.format(getString(R.string.season_episode_abbrev), getItemResult.season, getItemResult.episode); underTitle = String.format("%s | %s", getItemResult.showtitle, seasonEpisode); poster = getItemResult.art.poster; smallIcon = R.drawable.ic_tv_white_24dp; break; case ListType.ItemsAll.TYPE_SONG: title = getItemResult.title; underTitle = getItemResult.displayartist + " | " + getItemResult.album; poster = getItemResult.thumbnail; smallIcon = R.drawable.ic_headset_white_24dp; break; case ListType.ItemsAll.TYPE_MUSIC_VIDEO: title = getItemResult.title; underTitle = Utils.listStringConcat(getItemResult.artist, ", ") + " | " + getItemResult.album; poster = getItemResult.thumbnail; smallIcon = R.drawable.ic_headset_white_24dp; break; default: // We don't know what this is, forget it return; } switch (getPropertiesResult.speed) { case 1: playPauseIcon = R.drawable.ic_pause_white_24dp; break; default: playPauseIcon = R.drawable.ic_play_arrow_white_24dp; break; } // Create the actions, depending on the type of media PendingIntent rewindPendingItent, ffPendingItent, playPausePendingIntent; playPausePendingIntent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_PLAY_PAUSE); if (getItemResult.type.equals(ListType.ItemsAll.TYPE_SONG)) { rewindPendingItent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_PREVIOUS); rewindIcon = R.drawable.ic_skip_previous_white_24dp; ffPendingItent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_NEXT); ffIcon = R.drawable.ic_skip_next_white_24dp; } else { rewindPendingItent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_REWIND); rewindIcon = R.drawable.ic_fast_rewind_white_24dp; ffPendingItent = buildActionPendingIntent(getActivePlayerResult.playerid, IntentActionsService.ACTION_FAST_FORWARD); ffIcon = R.drawable.ic_fast_forward_white_24dp; } // Setup the collpased and expanded notifications final RemoteViews collapsedRV = new RemoteViews(this.getPackageName(), R.layout.notification_colapsed); collapsedRV.setImageViewResource(R.id.rewind, rewindIcon); collapsedRV.setOnClickPendingIntent(R.id.rewind, rewindPendingItent); collapsedRV.setImageViewResource(R.id.play, playPauseIcon); collapsedRV.setOnClickPendingIntent(R.id.play, playPausePendingIntent); collapsedRV.setImageViewResource(R.id.fast_forward, ffIcon); collapsedRV.setOnClickPendingIntent(R.id.fast_forward, ffPendingItent); collapsedRV.setTextViewText(R.id.title, title); collapsedRV.setTextViewText(R.id.text2, underTitle); final RemoteViews expandedRV = new RemoteViews(this.getPackageName(), R.layout.notification_expanded); expandedRV.setImageViewResource(R.id.rewind, rewindIcon); expandedRV.setOnClickPendingIntent(R.id.rewind, rewindPendingItent); expandedRV.setImageViewResource(R.id.play, playPauseIcon); expandedRV.setOnClickPendingIntent(R.id.play, playPausePendingIntent); expandedRV.setImageViewResource(R.id.fast_forward, ffIcon); expandedRV.setOnClickPendingIntent(R.id.fast_forward, ffPendingItent); expandedRV.setTextViewText(R.id.title, title); expandedRV.setTextViewText(R.id.text2, underTitle); final int expandedIconResId; if (isVideo) { expandedIconResId = R.id.icon_slim; expandedRV.setViewVisibility(R.id.icon_slim, View.VISIBLE); expandedRV.setViewVisibility(R.id.icon_square, View.GONE); } else { expandedIconResId = R.id.icon_square; expandedRV.setViewVisibility(R.id.icon_slim, View.GONE); expandedRV.setViewVisibility(R.id.icon_square, View.VISIBLE); } // Build the notification NotificationCompat.Builder builder = new NotificationCompat.Builder(this); final Notification notification = builder.setSmallIcon(smallIcon).setShowWhen(false).setOngoing(true) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setCategory(NotificationCompat.CATEGORY_TRANSPORT).setContentIntent(mRemoteStartPendingIntent) .setContent(collapsedRV).build(); // This is a convoluted way of loading the image and showing the // notification, but it's what works with Picasso and is efficient. // Here's what's going on: // // 1. The image is loaded asynchronously into a Target, and only after // it is loaded is the notification shown. Using targets is a lot more // efficient than letting Picasso load it directly into the // notification imageview, which causes a lot of flickering // // 2. The target needs to be static, because Picasso only keeps a weak // reference to it, so we need to keed a strong reference and reset it // to null when we're done. We also need to check if it is not null in // case a previous request hasn't finished yet. // // 3. We can only show the notification after the bitmap is loaded into // the target, so it is done in the callback // // 4. We specifically resize the image to the same dimensions used in // the remote, so that Picasso reuses it in the remote and here from the cache Resources resources = this.getResources(); final int posterWidth = resources.getDimensionPixelOffset(R.dimen.now_playing_poster_width); final int posterHeight = isVideo ? resources.getDimensionPixelOffset(R.dimen.now_playing_poster_height) : posterWidth; if (picassoTarget == null) { picassoTarget = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { showNotification(bitmap); } @Override public void onBitmapFailed(Drawable errorDrawable) { CharacterDrawable avatarDrawable = UIUtils.getCharacterAvatar(NotificationService.this, title); showNotification(Utils.drawableToBitmap(avatarDrawable, posterWidth, posterHeight)); } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } private void showNotification(Bitmap bitmap) { collapsedRV.setImageViewBitmap(R.id.icon, bitmap); if (Utils.isJellybeanOrLater()) { notification.bigContentView = expandedRV; expandedRV.setImageViewBitmap(expandedIconResId, bitmap); } NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notification); picassoTarget = null; } }; // Load the image HostManager hostManager = HostManager.getInstance(this); hostManager.getPicasso().load(hostManager.getHostInfo().getImageUrl(poster)) .resize(posterWidth, posterHeight).into(picassoTarget); } }