Example usage for android.widget RemoteViews setViewVisibility

List of usage examples for android.widget RemoteViews setViewVisibility

Introduction

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

Prototype

public void setViewVisibility(int viewId, int visibility) 

Source Link

Document

Equivalent to calling View#setVisibility(int)

Usage

From source file:org.physical_web.physicalweb.UriBeaconDiscoveryService.java

private void updateSummaryNotificationRemoteViewsFirstBeacon(String url, RemoteViews remoteViews) {
    MetadataResolver.UrlMetadata urlMetadata_firstBeacon = mUrlToUrlMetadata.get(url);
    if (urlMetadata_firstBeacon != null) {
        String title = mUrlToUrlMetadata.get(url).title;
        String description = mUrlToUrlMetadata.get(url).description;
        Bitmap icon = mUrlToUrlMetadata.get(url).icon;
        remoteViews.setImageViewBitmap(R.id.icon_firstBeacon, icon);
        remoteViews.setTextViewText(R.id.title_firstBeacon, title);
        remoteViews.setTextViewText(R.id.url_firstBeacon, url);
        remoteViews.setTextViewText(R.id.description_firstBeacon, description);
        // Recolor notifications to have light text for non-Lollipop devices
        if (!(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
            remoteViews.setTextColor(R.id.title_firstBeacon, NON_LOLLIPOP_NOTIFICATION_TITLE_COLOR);
            remoteViews.setTextColor(R.id.url_firstBeacon, NON_LOLLIPOP_NOTIFICATION_URL_COLOR);
            remoteViews.setTextColor(R.id.description_firstBeacon, NON_LOLLIPOP_NOTIFICATION_SNIPPET_COLOR);
        }/*from   www .j  av  a 2 s .  co m*/

        // Create an intent that will open the browser to the beacon's url
        // if the user taps the notification
        PendingIntent pendingIntent = createNavigateToUrlPendingIntent(url);
        remoteViews.setOnClickPendingIntent(R.id.first_beacon_main_layout, pendingIntent);
        remoteViews.setViewVisibility(R.id.firstBeaconLayout, View.VISIBLE);
    } else {
        remoteViews.setViewVisibility(R.id.firstBeaconLayout, View.GONE);
    }
}

From source file:org.physical_web.physicalweb.UriBeaconDiscoveryService.java

private void updateSummaryNotificationRemoteViewsSecondBeacon(String url, RemoteViews remoteViews) {
    MetadataResolver.UrlMetadata urlMetadata_secondBeacon = mUrlToUrlMetadata.get(url);
    if (urlMetadata_secondBeacon != null) {
        String title = mUrlToUrlMetadata.get(url).title;
        String description = mUrlToUrlMetadata.get(url).description;
        Bitmap icon = mUrlToUrlMetadata.get(url).icon;
        remoteViews.setImageViewBitmap(R.id.icon_secondBeacon, icon);
        remoteViews.setTextViewText(R.id.title_secondBeacon, title);
        remoteViews.setTextViewText(R.id.url_secondBeacon, url);
        remoteViews.setTextViewText(R.id.description_secondBeacon, description);
        // Recolor notifications to have light text for non-Lollipop devices
        if (!(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
            remoteViews.setTextColor(R.id.title_secondBeacon, NON_LOLLIPOP_NOTIFICATION_TITLE_COLOR);
            remoteViews.setTextColor(R.id.url_secondBeacon, NON_LOLLIPOP_NOTIFICATION_URL_COLOR);
            remoteViews.setTextColor(R.id.description_secondBeacon, NON_LOLLIPOP_NOTIFICATION_SNIPPET_COLOR);
        }/*  w w w.  j a va 2s.c  o m*/

        // Create an intent that will open the browser to the beacon's url
        // if the user taps the notification
        PendingIntent pendingIntent = createNavigateToUrlPendingIntent(url);
        remoteViews.setOnClickPendingIntent(R.id.second_beacon_main_layout, pendingIntent);
        remoteViews.setViewVisibility(R.id.secondBeaconLayout, View.VISIBLE);
    } else {
        remoteViews.setViewVisibility(R.id.secondBeaconLayout, View.GONE);
    }
}

From source file:github.daneren2005.dsub.util.Notifications.java

private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean expanded,
        boolean playing, boolean remote) {

    // 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  va  2 s.c o  m*/
        ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(context);
        Bitmap bitmap = null;
        if (imageLoader != null) {
            bitmap = imageLoader.getCachedImage(context, song, false);
        }
        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.w(TAG, "Failed to get notification cover art", x);
        rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
    }

    // 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);

    boolean persistent = Util.getPreferences(context)
            .getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false);
    if (persistent) {
        if (expanded) {
            rv.setImageViewResource(R.id.control_pause,
                    playing ? R.drawable.notification_pause : R.drawable.notification_start);
        } else {
            rv.setImageViewResource(R.id.control_previous,
                    playing ? R.drawable.notification_pause : R.drawable.notification_start);
            rv.setImageViewResource(R.id.control_pause, R.drawable.notification_forward);
            rv.setImageViewResource(R.id.control_next, R.drawable.notification_close);
        }
    }

    // Create actions for media buttons
    PendingIntent pendingIntent;
    int previous = 0, pause = 0, next = 0, close = 0;
    if (persistent && !expanded) {
        pause = R.id.control_previous;
        next = R.id.control_pause;
        close = R.id.control_next;
    } else {
        previous = R.id.control_previous;
        pause = R.id.control_pause;
        next = R.id.control_next;
    }

    if ((remote || persistent) && close == 0 && expanded) {
        close = R.id.notification_close;
        rv.setViewVisibility(close, View.VISIBLE);
    }

    if (previous > 0) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_PREVIOUS");
        prevIntent.setComponent(new ComponentName(context, DownloadService.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(previous, pendingIntent);
    }
    if (pause > 0) {
        if (playing) {
            Intent pauseIntent = new Intent("KEYCODE_MEDIA_PLAY_PAUSE");
            pauseIntent.setComponent(new ComponentName(context, DownloadService.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(pause, pendingIntent);
        } else {
            Intent prevIntent = new Intent("KEYCODE_MEDIA_START");
            prevIntent.setComponent(new ComponentName(context, DownloadService.class));
            prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                    new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
            pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
            rv.setOnClickPendingIntent(pause, pendingIntent);
        }
    }
    if (next > 0) {
        Intent nextIntent = new Intent("KEYCODE_MEDIA_NEXT");
        nextIntent.setComponent(new ComponentName(context, DownloadService.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(next, pendingIntent);
    }
    if (close > 0) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_STOP");
        prevIntent.setComponent(new ComponentName(context, DownloadService.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(close, pendingIntent);
    }
}

From source file:com.android.deskclock.data.StopwatchNotificationBuilderPreN.java

@Override
public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes//from   ww w.jav a 2 s .c o  m
    final int eventLabel = R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.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(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
        final PendingIntent pendingPause = Utils.pendingServiceIntent(context, pause);
        expanded.setOnClickPendingIntent(leftButtonId, pendingPause);

        // Right button: Add Lap
        if (DataModel.getDataModel().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(context, StopwatchService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
            final PendingIntent pendingLap = Utils.pendingServiceIntent(context, lap);
            expanded.setOnClickPendingIntent(rightButtonId, pendingLap);
            expanded.setViewVisibility(rightButtonId, VISIBLE);
        } else {
            expanded.setViewVisibility(rightButtonId, INVISIBLE);
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().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(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
        final PendingIntent pendingStart = Utils.pendingServiceIntent(context, start);
        expanded.setOnClickPendingIntent(leftButtonId, pendingStart);

        // Right button: Reset (dismisses notification and resets stopwatch)
        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(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
        final PendingIntent pendingReset = Utils.pendingServiceIntent(context, reset);
        expanded.setOnClickPendingIntent(rightButtonId, pendingReset);

        // 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(context, StopwatchService.class)
            .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    final Notification notification = new NotificationCompat.Builder(context).setLocalOnly(true)
            .setOngoing(running).setContent(collapsed).setAutoCancel(stopwatch.isPaused())
            .setPriority(NotificationCompat.PRIORITY_MAX).setSmallIcon(R.drawable.stat_notify_stopwatch)
            .setDeleteIntent(Utils.pendingServiceIntent(context, reset))
            .setColor(ContextCompat.getColor(context, R.color.default_background)).build();
    notification.bigContentView = expanded;
    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.
 *///from   w ww.  j  a  v a2s  .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:com.onyx.deskclock.deskclock.data.StopwatchModel.java

/**
 * Updates the notification to reflect the latest state of the stopwatch and recorded laps.
 *///  w  w  w  .ja  va 2s  .c om
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:io.teak.sdk.NotificationBuilder.java

public static Notification createNativeNotification(final Context context, Bundle bundle,
        TeakNotification teakNotificaton) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    // Rich text message
    Spanned richMessageText = Html.fromHtml(teakNotificaton.message);

    // Configure notification behavior
    builder.setPriority(NotificationCompat.PRIORITY_MAX);
    builder.setDefaults(NotificationCompat.DEFAULT_ALL);
    builder.setOnlyAlertOnce(true);//from  www .j av a2 s. c o m
    builder.setAutoCancel(true);
    builder.setTicker(richMessageText);

    // Set small view image
    try {
        PackageManager pm = context.getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), 0);
        builder.setSmallIcon(ai.icon);
    } catch (Exception e) {
        Log.e(LOG_TAG, "Unable to load icon resource for Notification.");
        return null;
    }

    Random rng = new Random();

    // Create intent to fire if/when notification is cleared
    Intent pushClearedIntent = new Intent(
            context.getPackageName() + TeakNotification.TEAK_NOTIFICATION_CLEARED_INTENT_ACTION_SUFFIX);
    pushClearedIntent.putExtras(bundle);
    PendingIntent pushClearedPendingIntent = PendingIntent.getBroadcast(context, rng.nextInt(),
            pushClearedIntent, PendingIntent.FLAG_ONE_SHOT);
    builder.setDeleteIntent(pushClearedPendingIntent);

    // Create intent to fire if/when notification is opened, attach bundle info
    Intent pushOpenedIntent = new Intent(
            context.getPackageName() + TeakNotification.TEAK_NOTIFICATION_OPENED_INTENT_ACTION_SUFFIX);
    pushOpenedIntent.putExtras(bundle);
    PendingIntent pushOpenedPendingIntent = PendingIntent.getBroadcast(context, rng.nextInt(), pushOpenedIntent,
            PendingIntent.FLAG_ONE_SHOT);
    builder.setContentIntent(pushOpenedPendingIntent);

    // Because we can't be certain that the R class will line up with what is at SDK build time
    // like in the case of Unity et. al.
    class IdHelper {
        public int id(String identifier) {
            int ret = context.getResources().getIdentifier(identifier, "id", context.getPackageName());
            if (ret == 0) {
                throw new Resources.NotFoundException("Could not find R.id." + identifier);
            }
            return ret;
        }

        public int layout(String identifier) {
            int ret = context.getResources().getIdentifier(identifier, "layout", context.getPackageName());
            if (ret == 0) {
                throw new Resources.NotFoundException("Could not find R.layout." + identifier);
            }
            return ret;
        }
    }
    IdHelper R = new IdHelper(); // Declaring local as 'R' ensures we don't accidentally use the other R

    // Configure notification small view
    RemoteViews smallView = new RemoteViews(context.getPackageName(),
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? R.layout("teak_notif_no_title_v21")
                    : R.layout("teak_notif_no_title"));

    // Set small view image
    try {
        PackageManager pm = context.getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), 0);
        smallView.setImageViewResource(R.id("left_image"), ai.icon);
        builder.setSmallIcon(ai.icon);
    } catch (Exception e) {
        Log.e(LOG_TAG, "Unable to load icon resource for Notification.");
        return null;
    }

    // Set small view text
    smallView.setTextViewText(R.id("text"), richMessageText);

    // Check for Jellybean (API 16, 4.1)+ for expanded view
    RemoteViews bigView = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && teakNotificaton.longText != null
            && !teakNotificaton.longText.isEmpty()) {
        bigView = new RemoteViews(context.getPackageName(),
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? R.layout("teak_big_notif_image_text_v21")
                        : R.layout("teak_big_notif_image_text"));

        // Set big view text
        bigView.setTextViewText(R.id("text"), Html.fromHtml(teakNotificaton.longText));

        URI imageAssetA = null;
        try {
            imageAssetA = new URI(teakNotificaton.imageAssetA);
        } catch (Exception ignored) {
        }

        Bitmap topImageBitmap = null;
        if (imageAssetA != null) {
            try {
                URL aURL = new URL(imageAssetA.toString());
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                topImageBitmap = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
            } catch (Exception ignored) {
            }
        }

        if (topImageBitmap == null) {
            try {
                InputStream istr = context.getAssets().open("teak_notif_large_image_default.png");
                topImageBitmap = BitmapFactory.decodeStream(istr);
            } catch (Exception ignored) {
            }
        }

        if (topImageBitmap != null) {
            // Set large bitmap
            bigView.setImageViewBitmap(R.id("top_image"), topImageBitmap);
        } else {
            Log.e(LOG_TAG, "Unable to load image asset for Notification.");
            // Hide pulldown
            smallView.setViewVisibility(R.id("pulldown_layout"), View.INVISIBLE);
        }
    } else {
        // Hide pulldown
        smallView.setViewVisibility(R.id("pulldown_layout"), View.INVISIBLE);
    }

    // Voodoo from http://stackoverflow.com/questions/28169474/notification-background-in-android-lollipop-is-white-can-we-change-it
    int topId = Resources.getSystem().getIdentifier("status_bar_latest_event_content", "id", "android");
    int topBigLayout = Resources.getSystem().getIdentifier("notification_template_material_big_media_narrow",
            "layout", "android");
    int topSmallLayout = Resources.getSystem().getIdentifier("notification_template_material_media", "layout",
            "android");

    RemoteViews topBigView = null;
    if (bigView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // This is invisible inner view - to have media_actions in hierarchy
        RemoteViews innerTopView = new RemoteViews("android", topBigLayout);
        bigView.addView(android.R.id.empty, innerTopView);

        // This should be on top - we need status_bar_latest_event_content as top layout
        topBigView = new RemoteViews("android", topBigLayout);
        topBigView.removeAllViews(topId);
        topBigView.addView(topId, bigView);
    } else if (bigView != null) {
        topBigView = bigView;
    }

    // This should be on top - we need status_bar_latest_event_content as top layout
    RemoteViews topSmallView;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        topSmallView = new RemoteViews("android", topSmallLayout);
        topSmallView.removeAllViews(topId);
        topSmallView.addView(topId, smallView);
    } else {
        topSmallView = smallView;
    }

    builder.setContent(topSmallView);

    Notification n = builder.build();
    if (topBigView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // Use reflection to avoid compile-time issues, we check minimum API version above
        try {
            Field bigContentViewField = n.getClass().getField("bigContentView");
            bigContentViewField.set(n, topBigView);
        } catch (Exception ignored) {
        }
    }

    return n;
}

From source file:com.android.deskclock.data.StopwatchNotificationBuilderN.java

@Override
public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes/* w ww .ja v  a 2s  .com*/
    final int eventLabel = R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.getResources();
    final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime();

    final RemoteViews content = new RemoteViews(pname, R.layout.chronometer_notif_content);
    content.setChronometer(R.id.chronometer, base, null, running);

    final List<Notification.Action> actions = new ArrayList<>(2);

    if (running) {
        // Left button: Pause
        final Intent pause = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_pause_24dp);
        final CharSequence title1 = res.getText(R.string.sw_pause_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());

        // Right button: Add Lap
        if (DataModel.getDataModel().canAddMoreLaps()) {
            final Intent lap = new Intent(context, StopwatchService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

            final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_sw_lap_24dp);
            final CharSequence title2 = res.getText(R.string.sw_lap_button);
            final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap);
            actions.add(new Notification.Action.Builder(icon2, title2, intent2).build());
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().getLaps().size();
        if (lapCount > 0) {
            final int lapNumber = lapCount + 1;
            final String lap = res.getString(R.string.sw_notification_lap_number, lapNumber);
            content.setTextViewText(R.id.state, lap);
            content.setViewVisibility(R.id.state, VISIBLE);
        } else {
            content.setViewVisibility(R.id.state, GONE);
        }
    } else {
        // Left button: Start
        final Intent start = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_start_24dp);
        final CharSequence title1 = res.getText(R.string.sw_start_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, start);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());

        // Right button: Reset (dismisses notification and resets stopwatch)
        final Intent reset = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_reset_24dp);
        final CharSequence title2 = res.getText(R.string.sw_reset_button);
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset);
        actions.add(new Notification.Action.Builder(icon2, title2, intent2).build());

        // Indicate the stopwatch is paused.
        content.setTextViewText(R.id.state, res.getString(R.string.swn_paused));
        content.setViewVisibility(R.id.state, VISIBLE);
    }

    // Swipe away will reset the stopwatch without bringing forward the app.
    final Intent reset = new Intent(context, StopwatchService.class)
            .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    return new Notification.Builder(context).setLocalOnly(true).setOngoing(running)
            .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused())
            .setPriority(Notification.PRIORITY_MAX).setSmallIcon(R.drawable.stat_notify_stopwatch)
            .setGroup(nm.getStopwatchNotificationGroupKey())
            .setStyle(new Notification.DecoratedCustomViewStyle())
            .setDeleteIntent(Utils.pendingServiceIntent(context, reset))
            .setActions(actions.toArray(new Notification.Action[actions.size()]))
            .setColor(ContextCompat.getColor(context, R.color.default_background)).build();
}

From source file:com.androidinspain.deskclock.data.StopwatchNotificationBuilder.java

public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes/*ww w  . j  a v  a  2  s . c  o  m*/
    final int eventLabel = com.androidinspain.deskclock.R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, StopwatchService.class)
            .setAction(StopwatchService.ACTION_SHOW_STOPWATCH).putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getService(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.getResources();
    final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime();

    final RemoteViews content = new RemoteViews(pname,
            com.androidinspain.deskclock.R.layout.chronometer_notif_content);
    content.setChronometer(com.androidinspain.deskclock.R.id.chronometer, base, null, running);

    final List<Action> actions = new ArrayList<>(2);

    if (running) {
        // Left button: Pause
        final Intent pause = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_PAUSE_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon1 = com.androidinspain.deskclock.R.drawable.ic_pause_24dp;
        final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_pause_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause);
        actions.add(new Action.Builder(icon1, title1, intent1).build());

        // Right button: Add Lap
        if (DataModel.getDataModel().canAddMoreLaps()) {
            final Intent lap = new Intent(context, StopwatchService.class)
                    .setAction(StopwatchService.ACTION_LAP_STOPWATCH)
                    .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

            @DrawableRes
            final int icon2 = com.androidinspain.deskclock.R.drawable.ic_sw_lap_24dp;
            final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_lap_button);
            final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap);
            actions.add(new Action.Builder(icon2, title2, intent2).build());
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().getLaps().size();
        if (lapCount > 0) {
            final int lapNumber = lapCount + 1;
            final String lap = res.getString(com.androidinspain.deskclock.R.string.sw_notification_lap_number,
                    lapNumber);
            content.setTextViewText(com.androidinspain.deskclock.R.id.state, lap);
            content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE);
        } else {
            content.setViewVisibility(com.androidinspain.deskclock.R.id.state, GONE);
        }
    } else {
        // Left button: Start
        final Intent start = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_START_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon1 = com.androidinspain.deskclock.R.drawable.ic_start_24dp;
        final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_start_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, start);
        actions.add(new Action.Builder(icon1, title1, intent1).build());

        // Right button: Reset (dismisses notification and resets stopwatch)
        final Intent reset = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_RESET_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon2 = com.androidinspain.deskclock.R.drawable.ic_reset_24dp;
        final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_reset_button);
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset);
        actions.add(new Action.Builder(icon2, title2, intent2).build());

        // Indicate the stopwatch is paused.
        content.setTextViewText(com.androidinspain.deskclock.R.id.state,
                res.getString(com.androidinspain.deskclock.R.string.swn_paused));
        content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE);
    }

    final Builder notification = new NotificationCompat.Builder(context).setLocalOnly(true).setOngoing(running)
            .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused())
            .setPriority(Notification.PRIORITY_MAX)
            .setSmallIcon(com.androidinspain.deskclock.R.drawable.stat_notify_stopwatch)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setColor(ContextCompat.getColor(context, com.androidinspain.deskclock.R.color.default_background));

    if (Utils.isNOrLater()) {
        notification.setGroup(nm.getStopwatchNotificationGroupKey());
    }

    for (Action action : actions) {
        notification.addAction(action);
    }

    return notification.build();
}

From source file:com.wizardsofm.deskclock.data.StopwatchNotificationBuilderN.java

@Override
public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes//w w w  .j a  va 2 s. c o m
    final int eventLabel = com.wizardsofm.deskclock.R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.getResources();
    final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime();

    final RemoteViews content = new RemoteViews(pname,
            com.wizardsofm.deskclock.R.layout.chronometer_notif_content);
    content.setChronometer(com.wizardsofm.deskclock.R.id.chronometer, base, null, running);

    final List<Notification.Action> actions = new ArrayList<>(2);

    if (running) {
        // Left button: Pause
        final Intent pause = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_pause_24dp);
        final CharSequence title1 = res.getText(com.wizardsofm.deskclock.R.string.sw_pause_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());

        // Right button: Add Lap
        if (DataModel.getDataModel().canAddMoreLaps()) {
            final Intent lap = new Intent(context, StopwatchService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

            final Icon icon2 = Icon.createWithResource(context,
                    com.wizardsofm.deskclock.R.drawable.ic_sw_lap_24dp);
            final CharSequence title2 = res.getText(com.wizardsofm.deskclock.R.string.sw_lap_button);
            final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap);
            actions.add(new Notification.Action.Builder(icon2, title2, intent2).build());
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().getLaps().size();
        if (lapCount > 0) {
            final int lapNumber = lapCount + 1;
            final String lap = res.getString(com.wizardsofm.deskclock.R.string.sw_notification_lap_number,
                    lapNumber);
            content.setTextViewText(com.wizardsofm.deskclock.R.id.state, lap);
            content.setViewVisibility(com.wizardsofm.deskclock.R.id.state, VISIBLE);
        } else {
            content.setViewVisibility(com.wizardsofm.deskclock.R.id.state, GONE);
        }
    } else {
        // Left button: Start
        final Intent start = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_start_24dp);
        final CharSequence title1 = res.getText(com.wizardsofm.deskclock.R.string.sw_start_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, start);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());

        // Right button: Reset (dismisses notification and resets stopwatch)
        final Intent reset = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon2 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_reset_24dp);
        final CharSequence title2 = res.getText(com.wizardsofm.deskclock.R.string.sw_reset_button);
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset);
        actions.add(new Notification.Action.Builder(icon2, title2, intent2).build());

        // Indicate the stopwatch is paused.
        content.setTextViewText(com.wizardsofm.deskclock.R.id.state,
                res.getString(com.wizardsofm.deskclock.R.string.swn_paused));
        content.setViewVisibility(com.wizardsofm.deskclock.R.id.state, VISIBLE);
    }

    // Swipe away will reset the stopwatch without bringing forward the app.
    final Intent reset = new Intent(context, StopwatchService.class)
            .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    return new Notification.Builder(context).setLocalOnly(true).setOngoing(running)
            .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused())
            .setPriority(Notification.PRIORITY_MAX)
            .setSmallIcon(com.wizardsofm.deskclock.R.drawable.stat_notify_stopwatch)
            .setGroup(nm.getStopwatchNotificationGroupKey())
            .setStyle(new Notification.DecoratedCustomViewStyle())
            .setDeleteIntent(Utils.pendingServiceIntent(context, reset))
            .setActions(actions.toArray(new Notification.Action[actions.size()]))
            .setColor(ContextCompat.getColor(context, com.wizardsofm.deskclock.R.color.default_background))
            .build();
}