Search in sources :

Example 56 with RemoteViews

use of android.widget.RemoteViews in project Android-Developers-Samples by johnjohndoe.

the class MainActivity method createNotification.

/**
     * This sample demonstrates notifications with custom content views.
     *
     * <p>On API level 16 and above a big content view is also defined that is used for the
     * 'expanded' notification. The notification is created by the NotificationCompat.Builder.
     * The expanded content view is set directly on the {@link android.app.Notification} once it has been build.
     * (See {@link android.app.Notification#bigContentView}.) </p>
     *
     * <p>The content views are inflated as {@link android.widget.RemoteViews} directly from their XML layout
     * definitions using {@link android.widget.RemoteViews#RemoteViews(String, int)}.</p>
     */
private void createNotification() {
    // BEGIN_INCLUDE(notificationCompat)
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    // END_INCLUDE(notificationCompat)
    // BEGIN_INCLUDE(intent)
    //Create Intent to launch this Activity again if the notification is clicked.
    Intent i = new Intent(this, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(intent);
    // END_INCLUDE(intent)
    // BEGIN_INCLUDE(ticker)
    // Sets the ticker text
    builder.setTicker(getResources().getString(R.string.custom_notification));
    // Sets the small icon for the ticker
    builder.setSmallIcon(R.drawable.ic_stat_custom);
    // END_INCLUDE(ticker)
    // BEGIN_INCLUDE(buildNotification)
    // Cancel the notification when clicked
    builder.setAutoCancel(true);
    // Build the notification
    Notification notification = builder.build();
    // END_INCLUDE(buildNotification)
    // BEGIN_INCLUDE(customLayout)
    // Inflate the notification layout as RemoteViews
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
    // Set text on a TextView in the RemoteViews programmatically.
    final String time = DateFormat.getTimeInstance().format(new Date()).toString();
    final String text = getResources().getString(R.string.collapsed, time);
    contentView.setTextViewText(R.id.textView, text);
    /* Workaround: Need to set the content view here directly on the notification.
         * NotificationCompatBuilder contains a bug that prevents this from working on platform
         * versions HoneyComb.
         * See https://code.google.com/p/android/issues/detail?id=30495
         */
    notification.contentView = contentView;
    // big content view set here is displayed.)
    if (Build.VERSION.SDK_INT >= 16) {
        // Inflate and set the layout for the expanded notification view
        RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
        notification.bigContentView = expandedView;
    }
    // END_INCLUDE(customLayout)
    // START_INCLUDE(notify)
    // Use the NotificationManager to show the notification
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(0, notification);
// END_INCLUDE(notify)
}
Also used : RemoteViews(android.widget.RemoteViews) NotificationManager(android.app.NotificationManager) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) Date(java.util.Date)

Example 57 with RemoteViews

use of android.widget.RemoteViews in project cw-andtutorials by commonsguy.

the class AppWidget method onUpdate.

@Override
public void onUpdate(Context ctxt, AppWidgetManager mgr, int[] appWidgetIds) {
    ComponentName me = new ComponentName(ctxt, AppWidget.class);
    RemoteViews updateViews = new RemoteViews("apt.tutorial", R.layout.widget);
    RestaurantHelper helper = new RestaurantHelper(ctxt);
    try {
        Cursor c = helper.getReadableDatabase().rawQuery("SELECT COUNT(*) FROM restaurants", null);
        c.moveToFirst();
        int count = c.getInt(0);
        c.close();
        if (count > 0) {
            int offset = (int) (count * Math.random());
            String[] args = { String.valueOf(offset) };
            c = helper.getReadableDatabase().rawQuery("SELECT name FROM restaurants LIMIT 1 OFFSET ?", args);
            c.moveToFirst();
            updateViews.setTextViewText(R.id.name, c.getString(0));
        } else {
            updateViews.setTextViewText(R.id.name, ctxt.getString(R.string.empty));
        }
    } finally {
        helper.close();
    }
    mgr.updateAppWidget(me, updateViews);
}
Also used : RemoteViews(android.widget.RemoteViews) ComponentName(android.content.ComponentName) Cursor(android.database.Cursor)

Example 58 with RemoteViews

use of android.widget.RemoteViews in project mobile-android by photo.

the class AbstractUploaderService method showUploadNotification.

private Notification showUploadNotification(File file) {
    int icon = getNotificationIcon();
    CharSequence tickerText = getString(R.string.notification_uploading_photo, file.getName());
    long when = System.currentTimeMillis();
    CharSequence contentMessageTitle = getString(R.string.notification_uploading_photo, file.getName());
    PendingIntent contentIntent = getStandardPendingIntent();
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_upload);
    contentView.setImageViewResource(R.id.image, icon);
    contentView.setTextViewText(R.id.title, contentMessageTitle);
    contentView.setProgressBar(R.id.progress, 100, 0, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    if (contentIntent != null) {
        builder.setContentIntent(contentIntent);
    }
    Notification notification = builder.setTicker(tickerText).setWhen(when).setSmallIcon(icon).setContent(contentView).build();
    CommonUtils.debug(TAG, "Is notification content view null: " + (notification.contentView == null));
    // need to explicitly set contentView again because of bug in compat
    // library. Solution found here
    // http://stackoverflow.com/a/12574534/527759
    notification.contentView = contentView;
    mNotificationManager.notify(NOTIFICATION_UPLOAD_PROGRESS, notification);
    return notification;
}
Also used : RemoteViews(android.widget.RemoteViews) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification)

Example 59 with RemoteViews

use of android.widget.RemoteViews in project Etar-Calendar by Etar-Group.

the class AlertReceiver method makeDigestNotification.

/**
     * Creates an expanding digest notification for expired events.
     */
public static NotificationWrapper makeDigestNotification(Context context, ArrayList<AlertService.NotificationInfo> notificationInfos, String digestTitle, boolean expandable) {
    if (notificationInfos == null || notificationInfos.size() < 1) {
        return null;
    }
    Resources res = context.getResources();
    int numEvents = notificationInfos.size();
    long[] eventIds = new long[notificationInfos.size()];
    long[] startMillis = new long[notificationInfos.size()];
    for (int i = 0; i < notificationInfos.size(); i++) {
        eventIds[i] = notificationInfos.get(i).eventId;
        startMillis[i] = notificationInfos.get(i).startMillis;
    }
    // Create an intent triggered by clicking on the status icon that shows the alerts list.
    PendingIntent pendingClickIntent = createAlertActivityIntent(context);
    // Create an intent triggered by dismissing the digest notification that clears all
    // expired events.
    Intent deleteIntent = new Intent();
    deleteIntent.setClass(context, DismissAlarmsService.class);
    deleteIntent.setAction(DELETE_ALL_ACTION);
    deleteIntent.putExtra(AlertUtils.EVENT_IDS_KEY, eventIds);
    deleteIntent.putExtra(AlertUtils.EVENT_STARTS_KEY, startMillis);
    PendingIntent pendingDeleteIntent = PendingIntent.getService(context, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    if (digestTitle == null || digestTitle.length() == 0) {
        digestTitle = res.getString(R.string.no_title_label);
    }
    Notification.Builder notificationBuilder = new Notification.Builder(context);
    notificationBuilder.setContentText(digestTitle);
    notificationBuilder.setSmallIcon(R.drawable.stat_notify_calendar_multiple);
    notificationBuilder.setContentIntent(pendingClickIntent);
    notificationBuilder.setDeleteIntent(pendingDeleteIntent);
    String nEventsStr = res.getQuantityString(R.plurals.Nevents, numEvents, numEvents);
    notificationBuilder.setContentTitle(nEventsStr);
    Notification n;
    if (Utils.isJellybeanOrLater()) {
        // New-style notification...
        // Set to min priority to encourage the notification manager to collapse it.
        notificationBuilder.setPriority(Notification.PRIORITY_MIN);
        if (expandable) {
            // Multiple reminders.  Combine into an expanded digest notification.
            Notification.InboxStyle expandedBuilder = new Notification.InboxStyle(notificationBuilder);
            int i = 0;
            for (AlertService.NotificationInfo info : notificationInfos) {
                if (i < NOTIFICATION_DIGEST_MAX_LENGTH) {
                    String name = info.eventName;
                    if (TextUtils.isEmpty(name)) {
                        name = context.getResources().getString(R.string.no_title_label);
                    }
                    String timeLocation = AlertUtils.formatTimeLocation(context, info.startMillis, info.allDay, info.location);
                    TextAppearanceSpan primaryTextSpan = new TextAppearanceSpan(context, R.style.NotificationPrimaryText);
                    TextAppearanceSpan secondaryTextSpan = new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
                    // Event title in bold.
                    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
                    stringBuilder.append(name);
                    stringBuilder.setSpan(primaryTextSpan, 0, stringBuilder.length(), 0);
                    stringBuilder.append("  ");
                    // Followed by time and location.
                    int secondaryIndex = stringBuilder.length();
                    stringBuilder.append(timeLocation);
                    stringBuilder.setSpan(secondaryTextSpan, secondaryIndex, stringBuilder.length(), 0);
                    expandedBuilder.addLine(stringBuilder);
                    i++;
                } else {
                    break;
                }
            }
            // If there are too many to display, add "+X missed events" for the last line.
            int remaining = numEvents - i;
            if (remaining > 0) {
                String nMoreEventsStr = res.getQuantityString(R.plurals.N_remaining_events, remaining, remaining);
                // TODO: Add highlighting and icon to this last entry once framework allows it.
                expandedBuilder.setSummaryText(nMoreEventsStr);
            }
            // Remove the title in the expanded form (redundant with the listed items).
            expandedBuilder.setBigContentTitle("");
            n = expandedBuilder.build();
        } else {
            n = notificationBuilder.build();
        }
    } else {
        // Old-style notification (pre-JB).  We only need a standard notification (no
        // buttons) but use a custom view so it is consistent with the others.
        n = notificationBuilder.getNotification();
        // Use custom view with buttons to provide JB-like functionality (snooze/email).
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
        contentView.setImageViewResource(R.id.image, R.drawable.stat_notify_calendar_multiple);
        contentView.setTextViewText(R.id.title, nEventsStr);
        contentView.setTextViewText(R.id.text, digestTitle);
        contentView.setViewVisibility(R.id.time, View.VISIBLE);
        contentView.setViewVisibility(R.id.map_button, View.GONE);
        contentView.setViewVisibility(R.id.call_button, View.GONE);
        contentView.setViewVisibility(R.id.email_button, View.GONE);
        contentView.setViewVisibility(R.id.snooze_button, View.GONE);
        contentView.setViewVisibility(R.id.end_padding, View.VISIBLE);
        n.contentView = contentView;
        // Use timestamp to force expired digest notification to the bottom (there is no
        // priority setting before JB release).  This is hidden by the custom view.
        n.when = 1;
    }
    NotificationWrapper nw = new NotificationWrapper(n);
    if (AlertService.DEBUG) {
        for (AlertService.NotificationInfo info : notificationInfos) {
            nw.add(new NotificationWrapper(null, 0, info.eventId, info.startMillis, info.endMillis, false));
        }
    }
    return nw;
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) RemoteViews(android.widget.RemoteViews) Resources(android.content.res.Resources) PendingIntent(android.app.PendingIntent) SpannableStringBuilder(android.text.SpannableStringBuilder) NotificationWrapper(com.android.calendar.alerts.AlertService.NotificationWrapper)

Example 60 with RemoteViews

use of android.widget.RemoteViews in project Etar-Calendar by Etar-Group.

the class AlertReceiver method buildBasicNotification.

private static Notification buildBasicNotification(Notification.Builder notificationBuilder, Context context, String title, String summaryText, long startMillis, long endMillis, long eventId, int notificationId, boolean doPopup, int priority, boolean addActionButtons) {
    Resources resources = context.getResources();
    if (title == null || title.length() == 0) {
        title = resources.getString(R.string.no_title_label);
    }
    // Create an intent triggered by clicking on the status icon, that dismisses the
    // notification and shows the event.
    PendingIntent clickIntent = createClickEventIntent(context, eventId, startMillis, endMillis, notificationId);
    // Create a delete intent triggered by dismissing the notification.
    PendingIntent deleteIntent = createDeleteEventIntent(context, eventId, startMillis, endMillis, notificationId);
    // Create the base notification.
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(summaryText);
    notificationBuilder.setSmallIcon(R.drawable.stat_notify_calendar);
    notificationBuilder.setContentIntent(clickIntent);
    notificationBuilder.setDeleteIntent(deleteIntent);
    if (doPopup) {
        notificationBuilder.setFullScreenIntent(createAlertActivityIntent(context), true);
    }
    PendingIntent mapIntent = null, callIntent = null, snoozeIntent = null, emailIntent = null;
    if (addActionButtons) {
        // Send map, call, and email intent back to ourself first for a couple reasons:
        // 1) Workaround issue where clicking action button in notification does
        //    not automatically close the notification shade.
        // 2) Event information will always be up to date.
        // Create map and/or call intents.
        URLSpan[] urlSpans = getURLSpans(context, eventId);
        mapIntent = createMapBroadcastIntent(context, urlSpans, eventId);
        callIntent = createCallBroadcastIntent(context, urlSpans, eventId);
        // Create email intent for emailing attendees.
        emailIntent = createBroadcastMailIntent(context, eventId, title);
        // Create snooze intent.  TODO: change snooze to 10 minutes.
        snoozeIntent = createSnoozeIntent(context, eventId, startMillis, endMillis, notificationId);
    }
    if (Utils.isJellybeanOrLater()) {
        // Turn off timestamp.
        notificationBuilder.setWhen(0);
        // Should be one of the values in Notification (ie. Notification.PRIORITY_HIGH, etc).
        // A higher priority will encourage notification manager to expand it.
        notificationBuilder.setPriority(priority);
        // Add action buttons. Show at most three, using the following priority ordering:
        // 1. Map
        // 2. Call
        // 3. Email
        // 4. Snooze
        // Actions will only be shown if they are applicable; i.e. with no location, map will
        // not be shown, and with no recipients, snooze will not be shown.
        // TODO: Get icons, get strings. Maybe show preview of actual location/number?
        int numActions = 0;
        if (mapIntent != null && numActions < MAX_NOTIF_ACTIONS) {
            notificationBuilder.addAction(R.drawable.ic_map, resources.getString(R.string.map_label), mapIntent);
            numActions++;
        }
        if (callIntent != null && numActions < MAX_NOTIF_ACTIONS) {
            notificationBuilder.addAction(R.drawable.ic_call, resources.getString(R.string.call_label), callIntent);
            numActions++;
        }
        if (emailIntent != null && numActions < MAX_NOTIF_ACTIONS) {
            notificationBuilder.addAction(R.drawable.ic_menu_email_holo_dark, resources.getString(R.string.email_guests_label), emailIntent);
            numActions++;
        }
        if (snoozeIntent != null && numActions < MAX_NOTIF_ACTIONS) {
            notificationBuilder.addAction(R.drawable.ic_alarm_holo_dark, resources.getString(R.string.snooze_label), snoozeIntent);
            numActions++;
        }
        return notificationBuilder.getNotification();
    } else {
        // Old-style notification (pre-JB).  Use custom view with buttons to provide
        // JB-like functionality (snooze/email).
        Notification n = notificationBuilder.getNotification();
        // Use custom view with buttons to provide JB-like functionality (snooze/email).
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
        contentView.setImageViewResource(R.id.image, R.drawable.stat_notify_calendar);
        contentView.setTextViewText(R.id.title, title);
        contentView.setTextViewText(R.id.text, summaryText);
        int numActions = 0;
        if (mapIntent == null || numActions >= MAX_NOTIF_ACTIONS) {
            contentView.setViewVisibility(R.id.map_button, View.GONE);
        } else {
            contentView.setViewVisibility(R.id.map_button, View.VISIBLE);
            contentView.setOnClickPendingIntent(R.id.map_button, mapIntent);
            contentView.setViewVisibility(R.id.end_padding, View.GONE);
            numActions++;
        }
        if (callIntent == null || numActions >= MAX_NOTIF_ACTIONS) {
            contentView.setViewVisibility(R.id.call_button, View.GONE);
        } else {
            contentView.setViewVisibility(R.id.call_button, View.VISIBLE);
            contentView.setOnClickPendingIntent(R.id.call_button, callIntent);
            contentView.setViewVisibility(R.id.end_padding, View.GONE);
            numActions++;
        }
        if (emailIntent == null || numActions >= MAX_NOTIF_ACTIONS) {
            contentView.setViewVisibility(R.id.email_button, View.GONE);
        } else {
            contentView.setViewVisibility(R.id.email_button, View.VISIBLE);
            contentView.setOnClickPendingIntent(R.id.email_button, emailIntent);
            contentView.setViewVisibility(R.id.end_padding, View.GONE);
            numActions++;
        }
        if (snoozeIntent == null || numActions >= MAX_NOTIF_ACTIONS) {
            contentView.setViewVisibility(R.id.snooze_button, View.GONE);
        } else {
            contentView.setViewVisibility(R.id.snooze_button, View.VISIBLE);
            contentView.setOnClickPendingIntent(R.id.snooze_button, snoozeIntent);
            contentView.setViewVisibility(R.id.end_padding, View.GONE);
            numActions++;
        }
        n.contentView = contentView;
        return n;
    }
}
Also used : RemoteViews(android.widget.RemoteViews) Resources(android.content.res.Resources) PendingIntent(android.app.PendingIntent) URLSpan(android.text.style.URLSpan) Notification(android.app.Notification)

Aggregations

RemoteViews (android.widget.RemoteViews)217 Intent (android.content.Intent)86 PendingIntent (android.app.PendingIntent)77 TextView (android.widget.TextView)35 Test (org.junit.Test)35 Notification (android.app.Notification)30 SmallTest (android.support.test.filters.SmallTest)30 View (android.view.View)29 ImageView (android.widget.ImageView)28 AppWidgetManager (android.appwidget.AppWidgetManager)26 ComponentName (android.content.ComponentName)24 Bitmap (android.graphics.Bitmap)24 Bundle (android.os.Bundle)16 NavigationBarView (com.android.systemui.statusbar.phone.NavigationBarView)15 RemoteInputView (com.android.systemui.statusbar.policy.RemoteInputView)15 NotificationCompat (android.support.v4.app.NotificationCompat)14 Resources (android.content.res.Resources)12 StatusBarNotification (android.service.notification.StatusBarNotification)12 SharedPreferences (android.content.SharedPreferences)11 NotificationPanelView (com.android.systemui.statusbar.phone.NotificationPanelView)11