Example usage for android.widget RemoteViews RemoteViews

List of usage examples for android.widget RemoteViews RemoteViews

Introduction

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

Prototype

public RemoteViews(RemoteViews landscape, RemoteViews portrait) 

Source Link

Document

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

Usage

From source file:org.fdroid.enigtext.service.KeyCachingService.java

private void foregroundServiceModern() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.key_caching_notification);

    Intent intent = new Intent(this, KeyCachingService.class);
    intent.setAction(PASSPHRASE_EXPIRED_EVENT);
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.lock_cache_icon, pendingIntent);

    builder.setSmallIcon(R.drawable.icon_cached);
    builder.setContent(remoteViews);//w  w w  .  j  a va  2  s .c  o  m

    stopForeground(true);
    startForeground(SERVICE_RUNNING_ID, builder.build());
}

From source file:net.sourceforge.kalimbaradio.androidapp.util.NotificationUtil.java

private static RemoteViews createBigContentView(Context context, MusicDirectory.Entry song, Bitmap albumArt,
        boolean playing) {

    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_expanded);
    contentView.setTextViewText(R.id.notification_title, song.getTitle());
    contentView.setTextViewText(R.id.notification_artist, song.getArtist());
    contentView.setTextViewText(R.id.notification_album, song.getAlbum());
    contentView.setImageViewBitmap(R.id.notification_image, albumArt);
    contentView.setImageViewResource(R.id.notification_playpause,
            playing ? R.drawable.media_pause : R.drawable.media_start);

    Intent intent = new Intent("1");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    contentView.setOnClickPendingIntent(R.id.notification_playpause,
            PendingIntent.getService(context, 0, intent, 0));

    intent = new Intent("2");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
    contentView.setOnClickPendingIntent(R.id.notification_next,
            PendingIntent.getService(context, 0, intent, 0));

    intent = new Intent("3");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
    contentView.setOnClickPendingIntent(R.id.notification_prev,
            PendingIntent.getService(context, 0, intent, 0));

    intent = new Intent("4");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Constants.INTENT_EXTRA_NAME_HIDE_NOTIFICATION, true);
    contentView.setOnClickPendingIntent(R.id.notification_close,
            PendingIntent.getService(context, 0, intent, 0));

    return contentView;
}

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;//from   w  w  w  .  j a va2s. com
        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:ru.kaefik.isaifutdinov.an_weather_widget.AnWeatherWidget.java

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG_SERVICE, "   onReceive " + intent.getAction());
    super.onReceive(context, intent);
    if (FORCE_WIDGET_UPDATE.equals(intent.getAction())) {
        String nameCity = intent.getStringExtra(PARAM_CITY);
        String tempCity = intent.getStringExtra(PARAM_TEMP);
        String windCity = intent.getStringExtra(PARAM_WIND);
        String timeRefreshCity = intent.getStringExtra(PARAM_TIMEREFRESH);
        String weatherImageCity = intent.getStringExtra(PARAM_WEATHERIMAGE);
        String descriptionWeather = intent.getStringExtra(PARAM_DESCWEATHER);
        int WidgetId = intent.getIntExtra(PARAM_WIDGETID, 0);

        //TODO:    ? ,      ?.  ? ?  GetWeatherCityService

        Log.i(TAG_SERVICE, "onReceive " + nameCity + " -> " + tempCity);

        //  /*from ww w  .  j av a 2  s .c o  m*/
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ComponentName thisWidget = new ComponentName(context, AnWeatherWidget.class);
        int[] appWidgetId = appWidgetManager.getAppWidgetIds(thisWidget);

        for (int i = 0; i < appWidgetId.length; i++) {
            Log.i(TAG_SERVICE, "onReceive ->  appWidgetId = " + String.valueOf(appWidgetId[i]));
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget);
            if (WidgetId == appWidgetId[i]) {
                views.setTextViewText(R.id.cityNameText, nameCity);
                views.setTextViewText(R.id.tempCityText, tempCity);
                views.setTextViewText(R.id.windText, windCity);
                views.setTextViewText(R.id.timeRefreshText, timeRefreshCity);
                views.setTextViewText(R.id.descriptionWeatherText, descriptionWeather);
                views.setImageViewUri(R.id.weatherImageView,
                        Uri.parse("android.resource://ru.kaefik.isaifutdinov.an_weather_widget/mipmap/"
                                + "weather" + weatherImageCity));

                //    ? CLICK_WIDGET_BUTTON      onReceive
                // Intent ? Broadcast
                Intent active = new Intent(context, AnWeatherWidget.class);
                active.setAction(CLICK_WIDGET_BUTTON);
                //?  ?
                PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
                //?  ?
                views.setOnClickPendingIntent(R.id.refreshButton, actionPendingIntent);
                //? 
                // END -    ? CLICK_WIDGET_BUTTON      onReceive
                appWidgetManager.updateAppWidget(appWidgetId[i], views);
                break;
            }
            appWidgetManager.updateAppWidget(appWidgetId[i], views);
        }
    }
    if (CLICK_WIDGET_BUTTON.equals(intent.getAction())) {
        Log.i(TAG_SERVICE, "   ");

        //  
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ComponentName thisWidget = new ComponentName(context, AnWeatherWidget.class);
        int[] appWidgetId = appWidgetManager.getAppWidgetIds(thisWidget);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget);
        //  ?  
        for (int i = 0; i < appWidgetId.length; i++) {
            Log.i(TAG_SERVICE, "id     -> "
                    + String.valueOf(appWidgetId[i]));
            String nameCity = ConfigActivity.loadStringParametersFromFile(context,
                    String.valueOf(appWidgetId[i]));
            startGetWeatherCityService(context, appWidgetId[i], new CityModel(nameCity));
        }
    }
}

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  ww  .j  a v  a 2 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();
    if (Build.VERSION.SDK_INT >= 16) {
        notification.bigContentView = expanded;
    }
    mNotificationManager.notify(mNotificationModel.getStopwatchNotificationId(), 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 ava2  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.messic.android.player.MessicPlayerNotification.java

/**
 * Method to construct the notification cover
 * //  w  w  w . ja  v  a2 s .  c  om
 * @param playSong
 */
private void constructNotificationCover(final MDMSong playSong) {
    createNotification();
    if (currentNotificationCover == null) {
        Bitmap cover = AlbumCoverCache.getCover(playSong.getAlbum().getSid(),
                new AlbumCoverCache.CoverListener() {

                    public void setCover(Bitmap bitmap) {
                        // we need to recreate the remote view due to memory buffer problems with remote views and the image
                        // we send
                        RemoteViews contentView = new RemoteViews(service.getPackageName(),
                                R.layout.bignotification_player_layout);
                        notification.bigContentView = contentView;
                        notification.bigContentView.setTextViewText(R.id.base_tvcurrent_author,
                                playSong.getAlbum().getAuthor().getName());
                        notification.bigContentView.setTextViewText(R.id.base_tvcurrent_song,
                                playSong.getName());

                        Bitmap cover = ImageUtil.resizeToNotificationImageSize(service.getApplicationContext(),
                                bitmap);
                        notification.bigContentView.setImageViewBitmap(R.id.base_ivcurrent_cover, cover);
                        currentNotificationCover = cover;
                        mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification);
                    }

                    public void failed(Exception e) {
                        // TODO Auto-generated method stub

                    }
                });
        if (cover != null) {
            // we need to recreate the remote view due to memory buffer problems with remote views and the image we
            // send
            RemoteViews contentView = new RemoteViews(this.service.getPackageName(),
                    R.layout.bignotification_player_layout);
            notification.bigContentView = contentView;
            this.notification.bigContentView.setTextViewText(R.id.base_tvcurrent_author,
                    playSong.getAlbum().getAuthor().getName());
            this.notification.bigContentView.setTextViewText(R.id.base_tvcurrent_song, playSong.getName());

            cover = ImageUtil.resizeToNotificationImageSize(this.service.getApplicationContext(), cover);
            notification.bigContentView.setImageViewBitmap(R.id.base_ivcurrent_cover, cover);
            this.currentNotificationCover = cover;
        } else {
            notification.bigContentView.setImageViewResource(R.id.base_ivcurrent_cover, R.drawable.ic_launcher);
        }
    }
}

From source file:com.abhijitvalluri.android.fitnotifications.HomeFragment.java

private void initializeDemoButton() {
    mDemoTV.setOnClickListener(new View.OnClickListener() {
        @Override//w ww .  j a v a 2 s . c  o  m
        public void onClick(View v) {
            Bundle newExtra = new Bundle();

            NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
            String notificationText = "Sample notification subject";
            String notificationBigText = "Sample notification body. This is where the details of the notification will be shown.";

            StringBuilder sb = new StringBuilder();
            sb.append("[").append("example").append("] ");
            sb.append(notificationText);
            if (notificationBigText.length() > 0) {
                sb.append(" -- ").append(notificationBigText);
            }

            RemoteViews contentView = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification);
            contentView.setTextViewText(R.id.customNotificationText,
                    getString(R.string.placeholder_notification_text));
            builder.setSmallIcon(R.drawable.ic_sms_white_24dp).setContentText(sb.toString()).setExtras(newExtra)
                    .setContentTitle("Sample Notification Title").setContent(contentView);

            // Creates an explicit intent for the SettingsActivity in the app
            Intent settingsIntent = new Intent(mContext, SettingsActivity.class);

            // The stack builder object will contain an artificial back stack for the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // the application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(SettingsActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(settingsIntent);
            PendingIntent settingsPendingIntent = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(settingsPendingIntent).setAutoCancel(true);

            ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE))
                    .notify(Constants.NOTIFICATION_ID, builder.build());

            Toast.makeText(mContext, getString(R.string.test_notification_sent), Toast.LENGTH_LONG).show();

            if (mDismissPlaceholderNotif) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE))
                                .cancel(Constants.NOTIFICATION_ID);
                    }
                }, mPlaceholderNotifDismissDelayMillis);
            }
        }
    });
}

From source file:de.hackerspacebremen.push.PushIntentService.java

private void updateAppWidget(Context context, SpaceData data) {
    SharedPreferences dataPersistence = getSharedPreferences(Constants.SPACE_DATA_PERSISTENCE,
            Context.MODE_PRIVATE);
    Editor editor = dataPersistence.edit();
    editor.putBoolean(Constants.SPACE_OPEN_DATA_KEY, data.isSpaceOpen());
    editor.commit();//  ww w  . j  a  va 2 s . co  m

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget);
    remoteViews.setViewVisibility(R.id.indicatorImage, View.VISIBLE);
    remoteViews.setViewVisibility(R.id.errorText, View.GONE);

    if (data.isSpaceOpen()) {
        remoteViews.setImageViewResource(R.id.indicatorImage, R.drawable.banner);
    } else {
        remoteViews.setImageViewResource(R.id.indicatorImage, R.drawable.banner_blur);
    }

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName componentName = new ComponentName(context, HackerspaceWidgetProvider.class);
    appWidgetManager.updateAppWidget(componentName, remoteViews);
}

From source file:com.abhijitvalluri.android.fitnotifications.setup.AppIntroActivity.java

private void addDemoSlide() {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    final boolean dismissPlaceholderNotif = preferences
            .getBoolean(getString(R.string.dismiss_placeholder_notif_key), false);
    final int placeholderNotifDismissDelayMillis = preferences
            .getInt(getString(R.string.placeholder_dismiss_delay_key), Constants.DEFAULT_DELAY_SECONDS) * 1000;
    final Handler handler = new Handler();

    // Demo/*from w  w w .j  av a 2 s  . c  o m*/
    addSlide(new SimpleSlide.Builder().layout(R.layout.fragment_intro).title(R.string.intro_done_title)
            .description(R.string.intro_done_desc).image(R.drawable.intro_done).background(R.color.colorAccent)
            .backgroundDark(R.color.colorAccentDark).buttonCtaLabel(R.string.test_notification)
            .buttonCtaClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle newExtra = new Bundle();

                    NotificationCompat.Builder builder = new NotificationCompat.Builder(AppIntroActivity.this);
                    String notificationText = "Sample notification subject";
                    String notificationBigText = "Sample notification body. This is where the details of the notification will be shown.";

                    StringBuilder sb = new StringBuilder();
                    sb.append("[").append("example").append("] ");
                    sb.append(notificationText);
                    if (notificationBigText.length() > 0) {
                        sb.append(" -- ").append(notificationBigText);
                    }

                    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
                    contentView.setTextViewText(R.id.customNotificationText,
                            getString(R.string.placeholder_notification_text));
                    builder.setSmallIcon(R.drawable.ic_sms_white_24dp).setContentText(sb.toString())
                            .setExtras(newExtra).setContentTitle("Sample Notification Title")
                            .setContent(contentView);

                    // Creates an explicit intent for the SettingsActivity in the app
                    Intent settingsIntent = new Intent(AppIntroActivity.this, SettingsActivity.class);

                    // The stack builder object will contain an artificial back stack for the
                    // started Activity.
                    // This ensures that navigating backward from the Activity leads out of
                    // the application to the Home screen.
                    TaskStackBuilder stackBuilder = TaskStackBuilder.create(AppIntroActivity.this);
                    // Adds the back stack for the Intent (but not the Intent itself)
                    stackBuilder.addParentStack(SettingsActivity.class);
                    // Adds the Intent that starts the Activity to the top of the stack
                    stackBuilder.addNextIntent(settingsIntent);
                    PendingIntent settingsPendingIntent = stackBuilder.getPendingIntent(0,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    builder.setContentIntent(settingsPendingIntent).setAutoCancel(true);

                    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID,
                            builder.build());

                    Toast.makeText(AppIntroActivity.this, getString(R.string.test_notification_sent),
                            Toast.LENGTH_LONG).show();

                    if (dismissPlaceholderNotif) {
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                                        .cancel(NOTIFICATION_ID);
                            }
                        }, placeholderNotifDismissDelayMillis);
                    }
                }
            }).build());
}