Example usage for android.widget RemoteViews apply

List of usage examples for android.widget RemoteViews apply

Introduction

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

Prototype

public View apply(Context context, ViewGroup parent) 

Source Link

Document

Inflates the view hierarchy represented by this object and applies all of the actions.

Usage

From source file:com.oasisfeng.nevo.decorators.media.MediaPlayerDecorator.java

@Override
protected void apply(final StatusBarNotificationEvo evolving) throws Exception {
    if (evolving.isClearable())
        return; // Just sticky notification, to reduce the overhead.
    final INotification n = evolving.notification();
    RemoteViews content_view = n.getBigContentView(); // Prefer big content view since it usually contains more actions
    if (content_view == null)
        content_view = n.getContentView();
    if (content_view == null)
        return;/*from  w  w w .  j  a  v  a 2 s.c  o  m*/
    final AtomicReference<IntentSender> sender_holder = new AtomicReference<>();
    final View views = content_view.apply(new ContextWrapper(this) {
        @Override
        public void startIntentSender(final IntentSender intent, final Intent fillInIntent, final int flagsMask,
                final int flagsValues, final int extraFlags) throws IntentSender.SendIntentException {
            startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
        }

        @Override
        public void startIntentSender(final IntentSender intent, final Intent fillInIntent, final int flagsMask,
                final int flagsValues, final int extraFlags, final Bundle options)
                throws IntentSender.SendIntentException {
            sender_holder.set(intent);
        }
    }, null);
    if (!(views instanceof ViewGroup))
        return;

    final List<NotificationCompat.Action> actions = new ArrayList<>(8);
    findClickable((ViewGroup) views, new Predicate<View>() {
        @Override
        public boolean apply(final View v) {
            sender_holder.set(null);
            v.performClick(); // trigger startIntentSender() above
            final IntentSender sender = sender_holder.get();
            if (sender == null) {
                Log.w(TAG, v + " has OnClickListener but no PendingIntent in it.");
                return true;
            }

            final PendingIntent pending_intent = getPendingIntent(sender);
            if (v instanceof TextView) {
                final CharSequence text = ((TextView) v).getText();
                actions.add(new NotificationCompat.Action(0, text, pending_intent));
            } else if (v instanceof ImageView) {
                final int res = getImageViewResource((ImageView) v);
                CharSequence title = v.getContentDescription();
                if (title == null)
                    title = "<" + System.identityHashCode(v);
                actions.add(new NotificationCompat.Action(res, title, pending_intent));
            } else {
                CharSequence title = v.getContentDescription();
                if (title == null)
                    title = "<" + System.identityHashCode(v);
                actions.add(new NotificationCompat.Action(0, title, pending_intent));
            }
            return true;
        }
    });

    final Notification mirror;
    final String pkg = evolving.getPackageName();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        final Notification.Builder b = new Notification.Builder(createPackageContext(pkg, 0))
                .setContentTitle(n.extras().getCharSequence(NotificationCompat.EXTRA_TITLE))
                .setLargeIcon(n.getLargeIcon()).setSmallIcon(fixIconPkg(n.getSmallIcon(), pkg));
        for (final NotificationCompat.Action action : actions)
            b.addAction(new Action.Builder(Icon.createWithResource(pkg, action.getIcon()), action.getTitle(),
                    action.getActionIntent()).build());
        mirror = b.build();
    } else {
        final NotificationCompat.Builder b = new NotificationCompat.Builder(createPackageContext(pkg, 0))
                .setContentTitle(n.extras().getCharSequence(NotificationCompat.EXTRA_TITLE))
                .setLargeIcon(n.extras().getParcelable(NotificationCompat.EXTRA_LARGE_ICON).<Bitmap>get())
                .setSmallIcon(android.R.drawable.ic_media_play);
        for (final NotificationCompat.Action action : actions)
            b.addAction(action);
        mirror = b.build();
    }
    final NotificationManagerCompat nm = NotificationManagerCompat.from(this);
    nm.notify("M<" + evolving.getPackageName() + (evolving.getTag() != null ? ">" + evolving.getTag() : ">"),
            evolving.getId(), mirror);
}