Example usage for com.google.common.collect Multimap containsKey

List of usage examples for com.google.common.collect Multimap containsKey

Introduction

In this page you can find the example usage for com.google.common.collect Multimap containsKey.

Prototype

boolean containsKey(@Nullable Object key);

Source Link

Document

Returns true if this multimap contains at least one key-value pair with the key key .

Usage

From source file:com.android.providers.transfers.TransferNotifier.java

private void updateWithLocked(Collection<TransferInfo> transfers) {
    final Resources res = mContext.getResources();

    // Cluster transfers together
    final Multimap<String, TransferInfo> clustered = ArrayListMultimap.create();
    for (TransferInfo info : transfers) {
        final String tag = buildNotificationTag(info);
        if (tag != null) {
            clustered.put(tag, info);//from w ww  . j av a 2  s  .  com
        }
    }

    // Build notification for each cluster
    for (String tag : clustered.keySet()) {
        final int type = getNotificationTagType(tag);
        final Collection<TransferInfo> cluster = clustered.get(tag);

        final Notification.Builder builder = new Notification.Builder(mContext);

        // Use time when cluster was first shown to avoid shuffling
        final long firstShown;
        if (mActiveNotifs.containsKey(tag)) {
            firstShown = mActiveNotifs.get(tag);
        } else {
            firstShown = System.currentTimeMillis();
            mActiveNotifs.put(tag, firstShown);
        }
        builder.setWhen(firstShown);

        // Show relevant icon
        if (type == TYPE_ACTIVE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download);
        } else if (type == TYPE_WAITING) {
            builder.setSmallIcon(android.R.drawable.stat_sys_warning);
        } else if (type == TYPE_COMPLETE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        }

        // Build action intents
        if (type == TYPE_ACTIVE || type == TYPE_WAITING) {
            // build a synthetic uri for intent identification purposes
            final Uri uri = new Uri.Builder().scheme("active-dl").appendPath(tag).build();
            final Intent intent = new Intent(Constants.ACTION_LIST, uri, mContext, TransferReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getTransferIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setOngoing(true);

        } else if (type == TYPE_COMPLETE) {
            final TransferInfo info = cluster.iterator().next();
            final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, info.mId);
            builder.setAutoCancel(true);

            final String action;
            if (Downloads.Impl.isStatusError(info.mStatus)) {
                action = Constants.ACTION_LIST;
            } else {
                if (info.mDestination != Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION) {
                    action = Constants.ACTION_OPEN;
                } else {
                    action = Constants.ACTION_LIST;
                }
            }

            final Intent intent = new Intent(action, uri, mContext, TransferReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getTransferIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

            final Intent hideIntent = new Intent(Constants.ACTION_HIDE, uri, mContext, TransferReceiver.class);
            builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, hideIntent, 0));
        }

        // Calculate and show progress
        String remainingText = null;
        String percentText = null;
        if (type == TYPE_ACTIVE) {
            long current = 0;
            long total = 0;
            long speed = 0;
            synchronized (mTransferSpeed) {
                for (TransferInfo info : cluster) {
                    if (info.mTotalBytes != -1) {
                        current += info.mCurrentBytes;
                        total += info.mTotalBytes;
                        speed += mTransferSpeed.get(info.mId);
                    }
                }
            }

            if (total > 0) {
                final int percent = (int) ((current * 100) / total);
                percentText = res.getString(R.string.download_percent, percent);

                if (speed > 0) {
                    final long remainingMillis = ((total - current) * 1000) / speed;
                    remainingText = res.getString(R.string.download_remaining,
                            DateUtils.formatDuration(remainingMillis));
                }

                builder.setProgress(100, percent, false);
            } else {
                builder.setProgress(100, 0, true);
            }
        }

        // Build titles and description
        final Notification notif;
        if (cluster.size() == 1) {
            final TransferInfo info = cluster.iterator().next();

            builder.setContentTitle(getTransferTitle(res, info));

            if (type == TYPE_ACTIVE) {
                if (!TextUtils.isEmpty(info.mDescription)) {
                    builder.setContentText(info.mDescription);
                } else {
                    builder.setContentText(remainingText);
                }
                builder.setContentInfo(percentText);

            } else if (type == TYPE_WAITING) {
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));

            } else if (type == TYPE_COMPLETE) {
                if (Downloads.Impl.isStatusError(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_failed));
                } else if (Downloads.Impl.isStatusSuccess(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_complete));
                }
            }

            notif = builder.build();

        } else {
            final Notification.InboxStyle inboxStyle = new Notification.InboxStyle(builder);

            for (TransferInfo info : cluster) {
                inboxStyle.addLine(getTransferTitle(res, info));
            }

            if (type == TYPE_ACTIVE) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_active, cluster.size(), cluster.size()));
                builder.setContentText(remainingText);
                builder.setContentInfo(percentText);
                inboxStyle.setSummaryText(remainingText);

            } else if (type == TYPE_WAITING) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));
                inboxStyle.setSummaryText(res.getString(R.string.notification_need_wifi_for_size));
            }

            notif = inboxStyle.build();
        }

        mNotifManager.notify(tag, 0, notif);
    }

    // Remove stale tags that weren't renewed
    final Iterator<String> it = mActiveNotifs.keySet().iterator();
    while (it.hasNext()) {
        final String tag = it.next();
        if (!clustered.containsKey(tag)) {
            mNotifManager.cancel(tag, 0);
            it.remove();
        }
    }
}

From source file:com.jio.appstore.download.DownloadNotifier.java

private void updateWithLocked(Collection<DownloadInfo> downloads) {
    final Resources res = mContext.getResources();

    // Cluster downloads together
    final Multimap<String, DownloadInfo> clustered = ArrayListMultimap.create();
    for (DownloadInfo info : downloads) {
        final String tag = buildNotificationTag(info);
        if (tag != null) {
            clustered.put(tag, info);//  w w  w .  j av  a2s .  co m
        }
    }

    // Build notification for each cluster
    for (String tag : clustered.keySet()) {
        final int type = getNotificationTagType(tag);
        final Collection<DownloadInfo> cluster = clustered.get(tag);

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);

        // Use time when cluster was first shown to avoid shuffling
        final long firstShown;
        if (mActiveNotifs.containsKey(tag)) {
            firstShown = mActiveNotifs.get(tag);
        } else {
            firstShown = System.currentTimeMillis();
            mActiveNotifs.put(tag, firstShown);
        }
        builder.setWhen(firstShown);

        // Show relevant icon
        if (type == TYPE_ACTIVE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download);
        } else if (type == TYPE_WAITING) {
            builder.setSmallIcon(android.R.drawable.stat_sys_warning);
        } else if (type == TYPE_COMPLETE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        }

        // Build action intents
        if (type == TYPE_ACTIVE || type == TYPE_WAITING) {
            // build a synthetic uri for intent identification purposes
            final Uri uri = new Uri.Builder().scheme("active-dl").appendPath(tag).build();
            final Intent intent = new Intent(Constants.ACTION_LIST, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setOngoing(true);

        } else if (type == TYPE_COMPLETE) {
            final DownloadInfo info = cluster.iterator().next();
            final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, info.mId);
            builder.setAutoCancel(true);

            final String action;
            if (Downloads.Impl.isStatusError(info.mStatus)) {
                action = Constants.ACTION_LIST;
            } else {
                action = Constants.ACTION_OPEN;
            }

            final Intent intent = new Intent(action, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

            final Intent hideIntent = new Intent(Constants.ACTION_HIDE, uri, mContext, DownloadReceiver.class);
            builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, hideIntent, 0));
        }

        // Calculate and show progress
        String remainingText = null;
        String percentText = null;
        if (type == TYPE_ACTIVE) {
            long current = 0;
            long total = 0;
            long speed = 0;
            synchronized (mDownloadSpeed) {
                for (DownloadInfo info : cluster) {
                    if (info.mTotalBytes != -1) {
                        current += info.mCurrentBytes;
                        total += info.mTotalBytes;
                        Long spd = mDownloadSpeed.get(info.mId);
                        if (spd != null) {
                            speed += spd;
                        }
                        //                            speed += mDownloadSpeed.get(info.mId);
                    }
                }
            }

            if (total > 0) {
                final int percent = (int) ((current * 100) / total);
                percentText = res.getString(R.string.download_percent, percent);

                if (speed > 0) {
                    final long remainingMillis = ((total - current) * 1000) / speed;
                    remainingText = res.getString(R.string.download_remaining,
                            DateUtils.formatElapsedTime(remainingMillis));
                }

                builder.setProgress(100, percent, false);
            } else {
                builder.setProgress(100, 0, true);
            }
        }

        // Build titles and description
        final Notification notif;
        if (cluster.size() == 1) {
            final DownloadInfo info = cluster.iterator().next();

            builder.setContentTitle(getDownloadTitle(res, info));

            if (type == TYPE_ACTIVE) {
                if (!TextUtils.isEmpty(info.mDescription)) {
                    builder.setContentText(info.mDescription);
                } else {
                    builder.setContentText(remainingText);
                }
                builder.setContentInfo(percentText);

            } else if (type == TYPE_WAITING) {
                builder.setContentText(res.getString(R.string.notification_download_waiting));

            } else if (type == TYPE_COMPLETE) {
                if (Downloads.Impl.isStatusError(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_failed));
                } else if (Downloads.Impl.isStatusSuccess(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_complete));
                }
            }

            notif = builder.build();

        } else {
            final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(builder);

            for (DownloadInfo info : cluster) {
                inboxStyle.addLine(getDownloadTitle(res, info));
            }

            if (type == TYPE_ACTIVE) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_active, cluster.size(), cluster.size()));
                builder.setContentText(remainingText);
                builder.setContentInfo(percentText);
                inboxStyle.setSummaryText(remainingText);

            } else if (type == TYPE_WAITING) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                builder.setContentText(res.getString(R.string.notification_download_waiting));
                inboxStyle.setSummaryText(res.getString(R.string.notification_download_waiting));
            }

            notif = inboxStyle.build();
        }

        mNotifManager.notify(tag, 0, notif);
    }

    // Remove stale tags that weren't renewed
    final Iterator<String> it = mActiveNotifs.keySet().iterator();
    while (it.hasNext()) {
        final String tag = it.next();
        if (!clustered.containsKey(tag)) {
            mNotifManager.cancel(tag, 0);
            it.remove();
        }
    }
}

From source file:com.sdk.download.providers.downloads.DownloadNotifier.java

private void updateWithLocked(Collection<DownloadInfo> downloads) {
    final Resources res = mContext.getResources();

    // Cluster downloads together
    final Multimap<String, DownloadInfo> clustered = ArrayListMultimap.create();
    for (DownloadInfo info : downloads) {
        final String tag = buildNotificationTag(info);
        if (tag != null) {
            clustered.put(tag, info);/*from w  w  w  .ja  v a2 s.  c om*/
        }
    }

    // Build notification for each cluster
    for (String tag : clustered.keySet()) {
        final int type = getNotificationTagType(tag);
        final Collection<DownloadInfo> cluster = clustered.get(tag);

        final Notification.Builder builder = new Notification.Builder(mContext);
        builder.setColor(res.getColor(com.android.internal.R.color.system_notification_accent_color));

        // Use time when cluster was first shown to avoid shuffling
        final long firstShown;
        if (mActiveNotifs.containsKey(tag)) {
            firstShown = mActiveNotifs.get(tag);
        } else {
            firstShown = System.currentTimeMillis();
            mActiveNotifs.put(tag, firstShown);
        }
        builder.setWhen(firstShown);

        // Show relevant icon
        if (type == TYPE_ACTIVE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download);
        } else if (type == TYPE_WAITING) {
            builder.setSmallIcon(android.R.drawable.stat_sys_warning);
        } else if (type == TYPE_COMPLETE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        }

        // Build action intents
        if (type == TYPE_ACTIVE || type == TYPE_WAITING) {
            // build a synthetic uri for intent identification purposes
            final Uri uri = new Uri.Builder().scheme("active-dl").appendPath(tag).build();
            final Intent intent = new Intent(Constants.ACTION_LIST, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setOngoing(true);

        } else if (type == TYPE_COMPLETE) {
            final DownloadInfo info = cluster.iterator().next();
            final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, info.mId);
            builder.setAutoCancel(true);

            final String action;
            if (Downloads.Impl.isStatusError(info.mStatus)) {
                action = Constants.ACTION_LIST;
            } else {
                if (info.mDestination != Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION) {
                    action = Constants.ACTION_OPEN;
                } else {
                    action = Constants.ACTION_LIST;
                }
            }

            final Intent intent = new Intent(action, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

            final Intent hideIntent = new Intent(Constants.ACTION_HIDE, uri, mContext, DownloadReceiver.class);
            builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, hideIntent, 0));
        }

        // Calculate and show progress
        String remainingText = null;
        String percentText = null;
        if (type == TYPE_ACTIVE) {
            long current = 0;
            long total = 0;
            long speed = 0;
            synchronized (mDownloadSpeed) {
                for (DownloadInfo info : cluster) {
                    if (info.mTotalBytes != -1) {
                        current += info.mCurrentBytes;
                        total += info.mTotalBytes;
                        speed += mDownloadSpeed.get(info.mId);
                    }
                }
            }

            if (total > 0) {
                final int percent = (int) ((current * 100) / total);
                percentText = res.getString(R.string.download_percent, percent);

                if (speed > 0) {
                    final long remainingMillis = ((total - current) * 1000) / speed;
                    remainingText = res.getString(R.string.download_remaining,
                            DateUtils.formatDuration(remainingMillis));
                }

                builder.setProgress(100, percent, false);
            } else {
                builder.setProgress(100, 0, true);
            }
        }

        // Build titles and description
        final Notification notif;
        if (cluster.size() == 1) {
            final DownloadInfo info = cluster.iterator().next();

            builder.setContentTitle(getDownloadTitle(res, info));

            if (type == TYPE_ACTIVE) {
                if (!TextUtils.isEmpty(info.mDescription)) {
                    builder.setContentText(info.mDescription);
                } else {
                    builder.setContentText(remainingText);
                }
                builder.setContentInfo(percentText);

            } else if (type == TYPE_WAITING) {
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));

            } else if (type == TYPE_COMPLETE) {
                if (Downloads.Impl.isStatusError(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_failed));
                } else if (Downloads.Impl.isStatusSuccess(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_complete));
                }
            }

            notif = builder.build();

        } else {
            final Notification.InboxStyle inboxStyle = new Notification.InboxStyle(builder);

            for (DownloadInfo info : cluster) {
                inboxStyle.addLine(getDownloadTitle(res, info));
            }

            if (type == TYPE_ACTIVE) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_active, cluster.size(), cluster.size()));
                builder.setContentText(remainingText);
                builder.setContentInfo(percentText);
                inboxStyle.setSummaryText(remainingText);

            } else if (type == TYPE_WAITING) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));
                inboxStyle.setSummaryText(res.getString(R.string.notification_need_wifi_for_size));
            }

            notif = inboxStyle.build();
        }

        mNotifManager.notify(tag, 0, notif);
    }

    // Remove stale tags that weren't renewed
    final Iterator<String> it = mActiveNotifs.keySet().iterator();
    while (it.hasNext()) {
        final String tag = it.next();
        if (!clustered.containsKey(tag)) {
            mNotifManager.cancel(tag, 0);
            it.remove();
        }
    }
}

From source file:com.tcl.download.DownloadNotifier.java

private void updateWithLocked(Collection<DownloadInfo> downloads) {
    final Resources res = mContext.getResources();

    // Cluster downloads together
    final Multimap<String, DownloadInfo> clustered = ArrayListMultimap.create();
    for (DownloadInfo info : downloads) {
        final String tag = buildNotificationTag(info);
        if (tag != null) {
            clustered.put(tag, info);/*from w  ww  .j a  v  a  2s . co  m*/
        }
    }

    // Build notification for each cluster
    for (String tag : clustered.keySet()) {
        final int type = getNotificationTagType(tag);
        final Collection<DownloadInfo> cluster = clustered.get(tag);

        final Notification.Builder builder = new Notification.Builder(mContext);
        builder.setColor(Color.parseColor("#ff0000"));//kevint
        //                  res.getColor(com.android.internal.R.color.system_notification_accent_color));

        // Use time when cluster was first shown to avoid shuffling
        final long firstShown;
        if (mActiveNotifs.containsKey(tag)) {
            firstShown = mActiveNotifs.get(tag);
        } else {
            firstShown = System.currentTimeMillis();
            mActiveNotifs.put(tag, firstShown);
        }
        builder.setWhen(firstShown);

        // Show relevant icon
        if (type == TYPE_ACTIVE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download);
        } else if (type == TYPE_WAITING) {
            builder.setSmallIcon(android.R.drawable.stat_sys_warning);
        } else if (type == TYPE_COMPLETE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        }

        // Build action intents
        if (type == TYPE_ACTIVE || type == TYPE_WAITING) {
            // build a synthetic uri for intent identification purposes
            final Uri uri = new Uri.Builder().scheme("active-dl").appendPath(tag).build();
            final Intent intent = new Intent(Constants.ACTION_LIST, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setOngoing(true);

        } else if (type == TYPE_COMPLETE) {
            final DownloadInfo info = cluster.iterator().next();
            final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, info.mId);
            builder.setAutoCancel(true);

            final String action;
            if (Downloads.Impl.isStatusError(info.mStatus)) {
                action = Constants.ACTION_LIST;
            } else {
                if (info.mDestination != Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION) {
                    action = Constants.ACTION_OPEN;
                } else {
                    action = Constants.ACTION_LIST;
                }
            }

            final Intent intent = new Intent(action, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

            final Intent hideIntent = new Intent(Constants.ACTION_HIDE, uri, mContext, DownloadReceiver.class);
            builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, hideIntent, 0));
        }

        // Calculate and show progress
        String remainingText = null;
        String percentText = null;
        if (type == TYPE_ACTIVE) {
            long current = 0;
            long total = 0;
            long speed = 0;
            synchronized (mDownloadSpeed) {
                for (DownloadInfo info : cluster) {
                    if (info.mTotalBytes != -1) {
                        current += info.mCurrentBytes;
                        total += info.mTotalBytes;
                        speed += mDownloadSpeed.get(info.mId);
                    }
                }
            }

            if (total > 0) {
                percentText = NumberFormat.getPercentInstance().format((double) current / total);

                if (speed > 0) {
                    final long remainingMillis = ((total - current) * 1000) / speed;
                    remainingText = res.getString(R.string.download_remaining, formatDuration(remainingMillis));
                }

                final int percent = (int) ((current * 100) / total);
                builder.setProgress(100, percent, false);
            } else {
                builder.setProgress(100, 0, true);
            }
        }

        // Build titles and description
        final Notification notif;
        if (cluster.size() == 1) {
            final DownloadInfo info = cluster.iterator().next();

            builder.setContentTitle(getDownloadTitle(res, info));

            if (type == TYPE_ACTIVE) {
                if (!TextUtils.isEmpty(info.mDescription)) {
                    builder.setContentText(info.mDescription);
                } else {
                    builder.setContentText(remainingText);
                }
                builder.setContentInfo(percentText);

            } else if (type == TYPE_WAITING) {
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));

            } else if (type == TYPE_COMPLETE) {
                if (Downloads.Impl.isStatusError(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_failed));
                } else if (Downloads.Impl.isStatusSuccess(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_complete));
                }
            }

            notif = builder.build();

        } else {
            final Notification.InboxStyle inboxStyle = new Notification.InboxStyle(builder);

            for (DownloadInfo info : cluster) {
                inboxStyle.addLine(getDownloadTitle(res, info));
            }

            if (type == TYPE_ACTIVE) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_active, cluster.size(), cluster.size()));
                builder.setContentText(remainingText);
                builder.setContentInfo(percentText);
                inboxStyle.setSummaryText(remainingText);

            } else if (type == TYPE_WAITING) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));
                inboxStyle.setSummaryText(res.getString(R.string.notification_need_wifi_for_size));
            }

            notif = inboxStyle.build();
        }

        mNotifManager.notify(tag, 0, notif);
    }

    // Remove stale tags that weren't renewed
    final Iterator<String> it = mActiveNotifs.keySet().iterator();
    while (it.hasNext()) {
        final String tag = it.next();
        if (!clustered.containsKey(tag)) {
            mNotifManager.cancel(tag, 0);
            it.remove();
        }
    }
}

From source file:cm.android.download.providers.downloads.DownloadNotifier.java

private void updateWithLocked(Collection<DownloadInfo> downloads) {
    final Resources res = mContext.getResources();

    // Cluster downloads together
    final Multimap<String, DownloadInfo> clustered = ArrayListMultimap.create();
    for (DownloadInfo info : downloads) {
        final String tag = buildNotificationTag(info);
        if (tag != null) {
            clustered.put(tag, info);/* w  ww.  jav a 2 s . c  om*/
        }
    }

    // Build notification for each cluster
    for (String tag : clustered.keySet()) {
        final int type = getNotificationTagType(tag);
        final Collection<DownloadInfo> cluster = clustered.get(tag);

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);

        // Use time when cluster was first shown to avoid shuffling
        final long firstShown;
        if (mActiveNotifs.containsKey(tag)) {
            firstShown = mActiveNotifs.get(tag);
        } else {
            firstShown = System.currentTimeMillis();
            mActiveNotifs.put(tag, firstShown);
        }
        builder.setWhen(firstShown);

        // Show relevant icon
        if (type == TYPE_ACTIVE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download);
        } else if (type == TYPE_WAITING) {
            builder.setSmallIcon(android.R.drawable.stat_sys_warning);
        } else if (type == TYPE_COMPLETE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        }

        // Build action intents
        if (type == TYPE_ACTIVE || type == TYPE_WAITING) {
            // build a synthetic uri for intent identification purposes
            final Uri uri = new Uri.Builder().scheme("active-dl").appendPath(tag).build();
            final Intent intent = new Intent(Constants.ACTION_LIST, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setOngoing(true);

        } else if (type == TYPE_COMPLETE) {
            final DownloadInfo info = cluster.iterator().next();
            final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, info.mId);
            builder.setAutoCancel(true);

            final String action;
            if (Downloads.Impl.isStatusError(info.mStatus)) {
                action = Constants.ACTION_LIST;
            } else {
                if (info.mDestination != Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION) {
                    action = Constants.ACTION_OPEN;
                } else {
                    action = Constants.ACTION_LIST;
                }
            }

            final Intent intent = new Intent(action, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

            final Intent hideIntent = new Intent(Constants.ACTION_HIDE, uri, mContext, DownloadReceiver.class);
            builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, hideIntent, 0));
        }

        // Calculate and show progress
        String remainingText = null;
        String percentText = null;
        if (type == TYPE_ACTIVE) {
            long current = 0;
            long total = 0;
            long speed = 0;
            synchronized (mDownloadSpeed) {
                for (DownloadInfo info : cluster) {
                    if (info.mTotalBytes != -1) {
                        current += info.mCurrentBytes;
                        total += info.mTotalBytes;
                        Long l = mDownloadSpeed.get(info.mId);
                        if (l == null) {
                            l = 0L;
                        }
                        speed += l;
                    }
                }
            }

            if (total > 0) {
                final int percent = (int) ((current * 100) / total);
                percentText = res.getString(R.string.download_percent, percent);

                if (speed > 0) {
                    final long remainingMillis = ((total - current) * 1000) / speed;
                    remainingText = res.getString(R.string.download_remaining,
                            // DateUtils.formatDuration(remainingMillis));
                            // // FIXME
                            "" + Helpers.formatDuration(mContext, remainingMillis));
                }

                builder.setProgress(100, percent, false);
            } else {
                builder.setProgress(100, 0, true);
            }
        }

        // Build titles and description
        final Notification notif;
        if (cluster.size() == 1) {
            final DownloadInfo info = cluster.iterator().next();

            builder.setContentTitle(getDownloadTitle(res, info));

            if (type == TYPE_ACTIVE) {
                if (!TextUtils.isEmpty(info.mDescription)) {
                    builder.setContentText(info.mDescription);
                } else {
                    builder.setContentText(remainingText);
                }
                builder.setContentInfo(percentText);

            } else if (type == TYPE_WAITING) {
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));

            } else if (type == TYPE_COMPLETE) {
                if (Downloads.Impl.isStatusError(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_failed));
                } else if (Downloads.Impl.isStatusSuccess(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_complete));
                }
            }

            notif = builder.build();

        } else {
            final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(builder);

            for (DownloadInfo info : cluster) {
                inboxStyle.addLine(getDownloadTitle(res, info));
            }

            if (type == TYPE_ACTIVE) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_active, cluster.size(), cluster.size()));
                builder.setContentText(remainingText);
                builder.setContentInfo(percentText);
                inboxStyle.setSummaryText(remainingText);

            } else if (type == TYPE_WAITING) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));
                inboxStyle.setSummaryText(res.getString(R.string.notification_need_wifi_for_size));
            }

            notif = inboxStyle.build();
        }

        mNotifManager.notify(tag, 0, notif);
    }

    // Remove stale tags that weren't renewed
    final Iterator<String> it = mActiveNotifs.keySet().iterator();
    while (it.hasNext()) {
        final String tag = it.next();
        if (!clustered.containsKey(tag)) {
            mNotifManager.cancel(tag, 0);
            it.remove();
        }
    }
}

From source file:com.ziyou.selftravel.download.DownloadNotifier.java

@SuppressLint("NewApi")
private void updateWithLocked(Collection<DownloadInfo> downloads) {
    final Resources res = mContext.getResources();

    // Cluster downloads together
    final Multimap<String, DownloadInfo> clustered = ArrayListMultimap.create();
    for (DownloadInfo info : downloads) {
        final String tag = buildNotificationTag(info);
        if (tag != null) {
            clustered.put(tag, info);/*  w  w w  . j av a 2s.com*/
        }
    }

    // Build notification for each cluster
    for (String tag : clustered.keySet()) {
        final int type = getNotificationTagType(tag);
        final Collection<DownloadInfo> cluster = clustered.get(tag);

        final Notification.Builder builder = new Notification.Builder(mContext);

        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.download_notification);
        builder.setContent(remoteViews);
        // Use time when cluster was first shown to avoid shuffling
        final long firstShown;
        if (mActiveNotifs.containsKey(tag)) {
            firstShown = mActiveNotifs.get(tag);
        } else {
            firstShown = System.currentTimeMillis();
            mActiveNotifs.put(tag, firstShown);
        }
        builder.setWhen(firstShown);

        // Show relevant icon
        if (type == TYPE_ACTIVE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download);
        } else if (type == TYPE_WAITING) {
            builder.setSmallIcon(android.R.drawable.stat_sys_warning);
        } else if (type == TYPE_COMPLETE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        }
        builder.setSmallIcon(R.drawable.ic_launcher);

        // Build action intents
        if (type == TYPE_ACTIVE || type == TYPE_WAITING) {
            // build a synthetic uri for intent identification purposes
            final Uri uri = new Uri.Builder().scheme("active-dl").appendPath(tag).build();
            final Intent intent = new Intent(Constants.ACTION_LIST, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setOngoing(true);

        } else if (type == TYPE_COMPLETE) {
            final DownloadInfo info = cluster.iterator().next();
            final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, info.mId);
            builder.setAutoCancel(true);

            final String action;
            if (Downloads.Impl.isStatusError(info.mStatus)) {
                action = Constants.ACTION_LIST;
            } else {
                if (info.mDestination != Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION) {
                    action = Constants.ACTION_OPEN;
                } else {
                    action = Constants.ACTION_LIST;
                }
            }

            final Intent intent = new Intent(action, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

            final Intent hideIntent = new Intent(Constants.ACTION_HIDE, uri, mContext, DownloadReceiver.class);
            builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, hideIntent, 0));
        }

        // Calculate and show progress
        String remainingText = null;
        String percentText = null;
        if (type == TYPE_ACTIVE) {
            long current = 0;
            long total = 0;
            long speed = 0;
            synchronized (mDownloadSpeed) {
                for (DownloadInfo info : cluster) {
                    if (info.mTotalBytes != -1) {
                        current += info.mCurrentBytes;
                        total += info.mTotalBytes;
                        Long l = mDownloadSpeed.get(info.mId);
                        speed += (l == null) ? 0l : l.longValue();
                    }
                }
            }

            if (total > 0) {
                final int percent = (int) ((current * 100) / total);
                percentText = res.getString(R.string.download_percent, percent);
                remoteViews.setTextViewText(R.id.progress_text, percentText);

                if (speed > 0) {
                    final long remainingMillis = ((total - current) * 1000) / speed;
                    //                        remainingText = res.getString(R.string.download_remaining, remainingMillis);
                    CharSequence remainingTime = TimeUtils.formatDuration(mContext, remainingMillis);
                    remoteViews.setTextViewText(R.id.remain_time, remainingTime);
                }

                remoteViews.setProgressBar(R.id.progress, 100, percent, false);
                //                    builder.setProgress(100, percent, false);
            } else {
                remoteViews.setProgressBar(R.id.progress, 100, 0, false);
                //                    builder.setProgress(100, 0, true);
            }
        }

        // Build titles and description
        final Notification notif;
        if (cluster.size() == 1) {
            final DownloadInfo info = cluster.iterator().next();

            //                builder.setContentTitle(getDownloadTitle(res, info));

            if (type == TYPE_ACTIVE) {
                if (!TextUtils.isEmpty(info.mDescription)) {
                    builder.setContentText(info.mDescription);
                } else {
                    builder.setContentText(remainingText);
                }
                builder.setContentInfo(percentText);

            } else if (type == TYPE_WAITING) {
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));

            } else if (type == TYPE_COMPLETE) {
                if (Downloads.Impl.isStatusError(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_failed));
                } else if (Downloads.Impl.isStatusSuccess(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_complete));
                }
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                notif = builder.build();
            } else {
                notif = builder.getNotification();
            }
            mNotifManager.notify(tag, 0, notif);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            final Notification.InboxStyle inboxStyle = new Notification.InboxStyle(builder);

            for (DownloadInfo info : cluster) {
                inboxStyle.addLine(getDownloadTitle(res, info));
            }

            if (type == TYPE_ACTIVE) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_active, cluster.size(), cluster.size()));
                builder.setContentText(remainingText);
                builder.setContentInfo(percentText);
                inboxStyle.setSummaryText(remainingText);

            } else if (type == TYPE_WAITING) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));
                inboxStyle.setSummaryText(res.getString(R.string.notification_need_wifi_for_size));
            }

            notif = inboxStyle.build();
            mNotifManager.notify(tag, 0, notif);
        }

    }

    // Remove stale tags that weren't renewed
    final Iterator<String> it = mActiveNotifs.keySet().iterator();
    while (it.hasNext()) {
        final String tag = it.next();
        if (!clustered.containsKey(tag)) {
            mNotifManager.cancel(tag, 0);
            it.remove();
        }
    }
}

From source file:org.occiware.clouddesigner.occi.docker.connector.dockerjava.DockerContainerManager.java

public CreateContainerCmd containerFactory(final Container container, final DockerClient dockerClient,
        final Multimap<String, String> containerDependency) {
    CreateContainerCmd create = null;//w  ww .jav a 2  s .  c om
    String _image = container.getImage();
    boolean _notEquals = (!Objects.equal(_image, null));
    if (_notEquals) {
        String _image_1 = container.getImage();
        String _trim = _image_1.trim();
        CreateContainerCmd _createContainerCmd = dockerClient.createContainerCmd(_trim);
        create = _createContainerCmd;
    } else {
        String _image_2 = container.getImage();
        boolean _equals = Objects.equal(_image_2, null);
        if (_equals) {
            CreateContainerCmd _createContainerCmd_1 = dockerClient.createContainerCmd("busybox");
            create = _createContainerCmd_1;
        }
    }
    String _command = container.getCommand();
    boolean _isNotBlank = StringUtils.isNotBlank(_command);
    if (_isNotBlank) {
        String _command_1 = container.getCommand();
        String _deleteWhitespace = StringUtils.deleteWhitespace(_command_1);
        final String[] cmd = _deleteWhitespace.split(",");
        create.withCmd(cmd);
    } else {
        create.withCmd("sleep", "9999");
    }
    int _cpu_shares = container.getCpu_shares();
    boolean _greaterThan = (_cpu_shares > 0);
    if (_greaterThan) {
        int _cpu_shares_1 = container.getCpu_shares();
        create.withCpuShares(Integer.valueOf(_cpu_shares_1));
    }
    String _add_host = container.getAdd_host();
    boolean _isNotBlank_1 = StringUtils.isNotBlank(_add_host);
    if (_isNotBlank_1) {
        String _hostname = container.getHostname();
        String _deleteWhitespace_1 = StringUtils.deleteWhitespace(_hostname);
        create.withHostName(_deleteWhitespace_1);
    }
    String _cpuset = container.getCpuset();
    boolean _isNotBlank_2 = StringUtils.isNotBlank(_cpuset);
    if (_isNotBlank_2) {
        String _cpuset_1 = container.getCpuset();
        String _deleteWhitespace_2 = StringUtils.deleteWhitespace(_cpuset_1);
        create.withCpusetCpus(_deleteWhitespace_2);
    }
    boolean _isPrivileged = container.isPrivileged();
    if (_isPrivileged) {
        boolean _isPrivileged_1 = container.isPrivileged();
        create.withPrivileged(Boolean.valueOf(_isPrivileged_1));
    }
    String _dns = container.getDns();
    boolean _isBlank = StringUtils.isBlank(_dns);
    boolean _not = (!_isBlank);
    if (_not) {
        String _dns_1 = container.getDns();
        String _deleteWhitespace_3 = StringUtils.deleteWhitespace(_dns_1);
        create.withDns(_deleteWhitespace_3);
    }
    String _environment = container.getEnvironment();
    boolean _isBlank_1 = StringUtils.isBlank(_environment);
    boolean _not_1 = (!_isBlank_1);
    if (_not_1) {
        String _environment_1 = container.getEnvironment();
        String _deleteWhitespace_4 = StringUtils.deleteWhitespace(_environment_1);
        create.withEnv(_deleteWhitespace_4);
    }
    String _ports = container.getPorts();
    boolean _isBlank_2 = StringUtils.isBlank(_ports);
    boolean _not_2 = (!_isBlank_2);
    if (_not_2) {
        String _ports_1 = container.getPorts();
        final String[] ports = _ports_1.split(":");
        String _get = ports[0];
        int _parseInt = Integer.parseInt(_get);
        ExposedPort port = ExposedPort.tcp(_parseInt);
        final Ports portBindings = new Ports();
        int _size = ((List<String>) Conversions.doWrapArray(ports)).size();
        boolean _equals_1 = (_size == 2);
        if (_equals_1) {
            String _get_1 = ports[1];
            int _parseInt_1 = Integer.parseInt(_get_1);
            Ports.Binding _bindPort = Ports.Binding.bindPort(_parseInt_1);
            portBindings.bind(port, _bindPort);
        } else {
            int _size_1 = ((List<String>) Conversions.doWrapArray(ports)).size();
            boolean _equals_2 = (_size_1 == 1);
            if (_equals_2) {
                String _get_2 = ports[0];
                int _parseInt_2 = Integer.parseInt(_get_2);
                Ports.Binding _bindPort_1 = Ports.Binding.bindPort(_parseInt_2);
                portBindings.bind(port, _bindPort_1);
            }
        }
        create.withPortBindings(portBindings);
    }
    String _name = container.getName();
    boolean _isBlank_3 = StringUtils.isBlank(_name);
    boolean _not_3 = (!_isBlank_3);
    if (_not_3) {
        String _name_1 = container.getName();
        String _deleteWhitespace_5 = StringUtils.deleteWhitespace(_name_1);
        create.withName(_deleteWhitespace_5);
    }
    String _hostname_1 = container.getHostname();
    boolean _isBlank_4 = StringUtils.isBlank(_hostname_1);
    boolean _not_4 = (!_isBlank_4);
    if (_not_4) {
        String _hostname_2 = container.getHostname();
        String _deleteWhitespace_6 = StringUtils.deleteWhitespace(_hostname_2);
        create.withName(_deleteWhitespace_6);
    }
    String _net = container.getNet();
    boolean _isBlank_5 = StringUtils.isBlank(_net);
    boolean _not_5 = (!_isBlank_5);
    if (_not_5) {
        String _net_1 = container.getNet();
        String _deleteWhitespace_7 = StringUtils.deleteWhitespace(_net_1);
        create.withNetworkMode(_deleteWhitespace_7);
    }
    boolean _isPublish_all = container.isPublish_all();
    if (_isPublish_all) {
        boolean _isPublish_all_1 = container.isPublish_all();
        create.withPublishAllPorts(Boolean.valueOf(_isPublish_all_1));
    }
    boolean _isTty = container.isTty();
    if (_isTty) {
        boolean _isTty_1 = container.isTty();
        create.withTty(Boolean.valueOf(_isTty_1));
    }
    boolean _isStdin_open = container.isStdin_open();
    if (_isStdin_open) {
        boolean _isStdin_open_1 = container.isStdin_open();
        create.withStdInOnce(Boolean.valueOf(_isStdin_open_1));
    }
    String _user = container.getUser();
    boolean _isBlank_6 = StringUtils.isBlank(_user);
    boolean _not_6 = (!_isBlank_6);
    if (_not_6) {
        String _user_1 = container.getUser();
        String _deleteWhitespace_8 = StringUtils.deleteWhitespace(_user_1);
        create.withUser(_deleteWhitespace_8);
    }
    String _volumes = container.getVolumes();
    boolean _isBlank_7 = StringUtils.isBlank(_volumes);
    boolean _not_7 = (!_isBlank_7);
    if (_not_7) {
        String _volumes_1 = container.getVolumes();
        String _deleteWhitespace_9 = StringUtils.deleteWhitespace(_volumes_1);
        Volume _volume = new Volume(_deleteWhitespace_9);
        create.withVolumes(_volume);
    }
    int _mem_limit = container.getMem_limit();
    boolean _greaterThan_1 = (_mem_limit > 0);
    if (_greaterThan_1) {
        int _mem_limit_1 = container.getMem_limit();
        Long _valueOf = Long.valueOf(_mem_limit_1);
        create.withMemory(_valueOf);
    }
    int _memory_swap = container.getMemory_swap();
    boolean _greaterThan_2 = (_memory_swap > 0);
    if (_greaterThan_2) {
        int _memory_swap_1 = container.getMemory_swap();
        Long _valueOf_1 = Long.valueOf(_memory_swap_1);
        create.withMemorySwap(_valueOf_1);
    }
    String _lxc_conf = container.getLxc_conf();
    boolean _isBlank_8 = StringUtils.isBlank(_lxc_conf);
    boolean _not_8 = (!_isBlank_8);
    if (_not_8) {
        final LxcConf lxcCon = new LxcConf("key", "value");
        create.withLxcConf(lxcCon);
    }
    String _name_2 = container.getName();
    boolean _containsKey = containerDependency.containsKey(_name_2);
    if (_containsKey) {
        String _name_3 = container.getName();
        Collection<String> _get_3 = containerDependency.get(_name_3);
        LinkedHashSet<String> _linkedHashSet = new LinkedHashSet<String>(_get_3);
        final List<String> depdupeContainers = new ArrayList<String>(_linkedHashSet);
        List<Link> dockeClientlinks = new ArrayList<Link>();
        Link dockeClientlink = null;
        for (final String entry : depdupeContainers) {
            {
                String _name_4 = container.getName();
                String _plus = (_name_4 + "LinkTo");
                String _plus_1 = (_plus + entry);
                Link _link = new Link(entry, _plus_1);
                dockeClientlink = _link;
                dockeClientlinks.add(dockeClientlink);
            }
        }
        int _size_2 = depdupeContainers.size();
        boolean _greaterThan_3 = (_size_2 > 1);
        if (_greaterThan_3) {
            create.withLinks(dockeClientlinks);
        } else {
            int _size_3 = depdupeContainers.size();
            boolean _equals_3 = (_size_3 == 1);
            if (_equals_3) {
                create.withLinks(dockeClientlink);
            }
        }
    }
    return create;
}

From source file:com.android.providers.downloads.DownloadNotifier.java

private void updateWithLocked(Collection<DownloadInfo> downloads) {
    final Resources res = mContext.getResources();

    // Cluster downloads together
    final Multimap<String, DownloadInfo> clustered = ArrayListMultimap.create();
    for (DownloadInfo info : downloads) {
        final String tag = buildNotificationTag(info);
        if (tag != null) {
            clustered.put(tag, info);// www  . j  a v a2s .  co  m
        }
    }

    // Build notification for each cluster
    for (String tag : clustered.keySet()) {
        final int type = getNotificationTagType(tag);
        final Collection<DownloadInfo> cluster = clustered.get(tag);

        final Notification.Builder builder = new Notification.Builder(mContext);

        // Use time when cluster was first shown to avoid shuffling
        final long firstShown;
        if (mActiveNotifs.containsKey(tag)) {
            firstShown = mActiveNotifs.get(tag);
        } else {
            firstShown = System.currentTimeMillis();
            mActiveNotifs.put(tag, firstShown);
        }
        builder.setWhen(firstShown);

        // Check error status about downloads. If error exists, will
        // update icon and content title/content text in notification.
        boolean hasErrorStatus = false;
        for (DownloadInfo info : cluster) {
            if (isErrorStatus(info.mStatus)) {
                hasErrorStatus = true;
                break;
            }
        }

        // Show relevant icon
        if (type == TYPE_ACTIVE) {
            if (hasErrorStatus) {
                builder.setSmallIcon(R.drawable.ic_stat_download_error);
            } else {
                builder.setSmallIcon(android.R.drawable.stat_sys_download);
            }
        } else if (type == TYPE_WAITING) {
            builder.setSmallIcon(android.R.drawable.stat_sys_warning);
        } else if (type == TYPE_PAUSED) {
            builder.setSmallIcon(com.android.internal.R.drawable.ic_media_pause);
        } else if (type == TYPE_COMPLETE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        }

        // Build action intents
        if (type == TYPE_ACTIVE || type == TYPE_WAITING || type == TYPE_PAUSED) {
            // build a synthetic uri for intent identification purposes
            final Uri uri = new Uri.Builder().scheme("active-dl").appendPath(tag).build();
            final Intent intent = new Intent(Constants.ACTION_LIST, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setOngoing(true);

        } else if (type == TYPE_COMPLETE) {
            final DownloadInfo info = cluster.iterator().next();
            final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, info.mId);
            builder.setAutoCancel(true);

            final String action;
            if (hasErrorStatus) {
                action = Constants.ACTION_LIST;
            } else {
                if (info.mDestination != Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION) {
                    action = Constants.ACTION_OPEN;
                } else {
                    action = Constants.ACTION_LIST;
                }
            }

            final Intent intent = new Intent(action, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

            final Intent hideIntent = new Intent(Constants.ACTION_HIDE, uri, mContext, DownloadReceiver.class);
            builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, hideIntent, 0));
        }

        // Calculate and show progress
        String remainingText = null;
        String durationText = null;
        String percentText = null;
        String speedText = null;
        if (type == TYPE_ACTIVE) {
            long current = 0;
            long total = 0;
            long speed = 0;
            synchronized (mDownloadSpeed) {
                for (DownloadInfo info : cluster) {
                    if (info.mTotalBytes != -1) {
                        current += info.mCurrentBytes;
                        total += info.mTotalBytes;
                        speed += mDownloadSpeed.get(info.mId);
                    }
                }
            }

            if (total > 0) {
                final int percent = (int) ((current * 100) / total);
                percentText = res.getString(R.string.download_percent, percent);

                if (speed > 0) {
                    // Decide prefix character for speed string
                    char preFix;
                    double speedNormalized = speed;

                    if (speed < SPEED_KB) {
                        preFix = '\0';
                    } else if (speed < SPEED_MB) {
                        preFix = (res.getString(R.string.kilo_bytes)).charAt(0);
                        speedNormalized /= SPEED_KB;
                    } else if (speed < SPEED_GB) {
                        preFix = (res.getString(R.string.mega_bytes)).charAt(0);
                        speedNormalized /= SPEED_MB;
                    } else {
                        preFix = (res.getString(R.string.giga_bytes)).charAt(0);
                        speedNormalized /= SPEED_GB;
                    }

                    // Format the String
                    speedText = String.format(SPEED_PLACEHOLDER, mFormatter.format(speedNormalized).toString(),
                            preFix);

                    final long remainingMillis = ((total - current) * 1000) / speed;
                    if (remainingMillis >= DateUtils.HOUR_IN_MILLIS) {
                        final int hours = (int) ((remainingMillis + 1800000) / DateUtils.HOUR_IN_MILLIS);
                        durationText = res.getQuantityString(R.plurals.duration_hours, hours, hours);
                    } else if (remainingMillis >= DateUtils.MINUTE_IN_MILLIS) {
                        final int minutes = (int) ((remainingMillis + 30000) / DateUtils.MINUTE_IN_MILLIS);
                        durationText = res.getQuantityString(R.plurals.duration_minutes, minutes, minutes);
                    } else {
                        final int seconds = (int) ((remainingMillis + 500) / DateUtils.SECOND_IN_MILLIS);
                        durationText = res.getQuantityString(R.plurals.duration_seconds, seconds, seconds);
                    }
                    remainingText = res.getString(R.string.download_remaining, durationText);
                }

                builder.setProgress(100, percent, false);
            } else {
                builder.setProgress(100, 0, true);
            }
        }

        // Build titles and description
        final Notification notif;
        String contentText = null;
        if (cluster.size() == 1) {
            final Notification.InboxStyle inboxStyle = new Notification.InboxStyle(builder);

            final DownloadInfo info = cluster.iterator().next();
            final Uri uris = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, info.mId);

            builder.setContentTitle(getDownloadTitle(res, info));

            final Intent stopIntent = new Intent(Constants.ACTION_NOTIFICATION_STOP, uris, mContext,
                    DownloadReceiver.class);
            stopIntent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));

            final Intent pauseIntent = new Intent(Constants.ACTION_NOTIFICATION_PAUSE, uris, mContext,
                    DownloadReceiver.class);
            pauseIntent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS,
                    getDownloadIds(cluster));

            final Intent resumeIntent = new Intent(Constants.ACTION_NOTIFICATION_RESUME, uris, mContext,
                    DownloadReceiver.class);
            resumeIntent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS,
                    getDownloadIds(cluster));

            final Intent retryIntent = new Intent(Constants.ACTION_NOTIFICATION_RETRY, uris, mContext,
                    DownloadReceiver.class);
            retryIntent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS,
                    getDownloadIds(cluster));

            if (!TextUtils.isEmpty(info.mDescription)) {
                inboxStyle.addLine(info.mDescription);
            } else if (!TextUtils.isEmpty(info.mPackage)) {
                final PackageManager pm = mContext.getApplicationContext().getPackageManager();
                ApplicationInfo ai;
                try {
                    ai = pm.getApplicationInfo(info.mPackage, 0);
                } catch (final PackageManager.NameNotFoundException e) {
                    ai = null;
                }
                final String packageName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
                if (!TextUtils.isEmpty(packageName)) {
                    inboxStyle.addLine(packageName);
                }
            }

            if (type == TYPE_ACTIVE) {
                if (hasErrorStatus) {
                    contentText = res.getString(R.string.notification_download_failed);
                } else if (TextUtils.isEmpty(speedText) && TextUtils.isEmpty(remainingText)) {
                    contentText = res.getString(R.string.download_running);
                } else if (!TextUtils.isEmpty(remainingText) && TextUtils.isEmpty(speedText)) {
                    contentText = remainingText;
                } else if (TextUtils.isEmpty(remainingText) && !TextUtils.isEmpty(speedText)) {
                    contentText = speedText;
                } else {
                    contentText = speedText + ", " + remainingText;
                }

                if (hasErrorStatus) {
                    builder.addAction(com.android.internal.R.drawable.ic_media_play,
                            res.getString(R.string.download_retry), PendingIntent.getBroadcast(mContext, 0,
                                    retryIntent, PendingIntent.FLAG_UPDATE_CURRENT));
                } else {
                    builder.addAction(com.android.internal.R.drawable.ic_media_pause,
                            res.getString(R.string.download_pause), PendingIntent.getBroadcast(mContext, 0,
                                    pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT));
                }
                builder.addAction(com.android.internal.R.drawable.ic_media_stop,
                        res.getString(R.string.download_stop),
                        PendingIntent.getBroadcast(mContext, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT));
            } else if (type == TYPE_WAITING) {
                contentText = res.getString(R.string.notification_need_wifi_for_size);
                builder.addAction(com.android.internal.R.drawable.ic_media_stop,
                        res.getString(R.string.download_stop),
                        PendingIntent.getBroadcast(mContext, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT));
            } else if (type == TYPE_PAUSED) {
                contentText = res.getString(R.string.notification_paused_in_background);
                builder.addAction(com.android.internal.R.drawable.ic_media_play,
                        res.getString(R.string.download_resume),
                        PendingIntent.getBroadcast(mContext, 0, resumeIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT))
                        .addAction(com.android.internal.R.drawable.ic_media_stop,
                                res.getString(R.string.download_stop), PendingIntent.getBroadcast(mContext, 0,
                                        stopIntent, PendingIntent.FLAG_UPDATE_CURRENT));
            } else if (type == TYPE_COMPLETE) {
                if (hasErrorStatus) {
                    contentText = res.getString(R.string.notification_download_failed);
                    builder.addAction(com.android.internal.R.drawable.ic_media_play,
                            res.getString(R.string.download_retry),
                            PendingIntent.getBroadcast(mContext, 0, retryIntent,
                                    PendingIntent.FLAG_UPDATE_CURRENT))
                            .addAction(com.android.internal.R.drawable.ic_media_stop,
                                    res.getString(R.string.download_stop), PendingIntent.getBroadcast(mContext,
                                            0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT));
                } else if (Downloads.Impl.isStatusSuccess(info.mStatus)) {
                    contentText = res.getString(R.string.notification_download_complete);
                }
            }

            inboxStyle.setSummaryText(contentText);
            builder.setContentText(contentText);
            builder.setContentInfo(percentText);
            notif = inboxStyle.build();

        } else {
            final Notification.InboxStyle inboxStyle = new Notification.InboxStyle(builder);

            final Uri uris = new Uri.Builder().scheme("active-dl").appendPath(tag).build();

            for (DownloadInfo info : cluster) {
                inboxStyle.addLine(getDownloadTitle(res, info));
            }

            final Intent stopAllIntent = new Intent(Constants.ACTION_NOTIFICATION_STOP_ALL, uris, mContext,
                    DownloadReceiver.class);
            stopAllIntent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS,
                    getDownloadIds(cluster));

            final Intent pauseAllIntent = new Intent(Constants.ACTION_NOTIFICATION_PAUSE_ALL, uris, mContext,
                    DownloadReceiver.class);
            pauseAllIntent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS,
                    getDownloadIds(cluster));

            final Intent resumeAllIntent = new Intent(Constants.ACTION_NOTIFICATION_RESUME_ALL, uris, mContext,
                    DownloadReceiver.class);
            resumeAllIntent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS,
                    getDownloadIds(cluster));

            final Intent retryAllIntent = new Intent(Constants.ACTION_NOTIFICATION_RETRY_ALL, uris, mContext,
                    DownloadReceiver.class);
            retryAllIntent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS,
                    getDownloadIds(cluster));

            if (type == TYPE_ACTIVE) {
                if (hasErrorStatus) {
                    builder.setContentTitle(res.getString(R.string.notification_download_failed));
                } else {
                    builder.setContentTitle(res.getQuantityString(R.plurals.notif_summary_active,
                            cluster.size(), cluster.size()));
                }
                if (TextUtils.isEmpty(speedText) && TextUtils.isEmpty(remainingText)) {
                    contentText = res.getString(R.string.download_running);
                } else if (!TextUtils.isEmpty(remainingText) && TextUtils.isEmpty(speedText)) {
                    contentText = remainingText;
                } else if (TextUtils.isEmpty(remainingText) && !TextUtils.isEmpty(speedText)) {
                    contentText = speedText;
                } else {
                    contentText = speedText + ", " + remainingText;
                }
                if (hasErrorStatus) {
                    builder.addAction(com.android.internal.R.drawable.ic_media_play,
                            res.getString(R.string.download_retry_all), PendingIntent.getBroadcast(mContext, 0,
                                    retryAllIntent, PendingIntent.FLAG_UPDATE_CURRENT));
                } else {
                    builder.addAction(com.android.internal.R.drawable.ic_media_pause,
                            res.getString(R.string.download_pause_all), PendingIntent.getBroadcast(mContext, 0,
                                    pauseAllIntent, PendingIntent.FLAG_UPDATE_CURRENT));
                }
                builder.addAction(com.android.internal.R.drawable.ic_media_stop,
                        res.getString(R.string.download_stop), PendingIntent.getBroadcast(mContext, 0,
                                stopAllIntent, PendingIntent.FLAG_UPDATE_CURRENT));
            } else if (type == TYPE_WAITING) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                contentText = res.getString(R.string.notification_need_wifi_for_size);
                builder.addAction(com.android.internal.R.drawable.ic_media_stop,
                        res.getString(R.string.download_stop_all), PendingIntent.getBroadcast(mContext, 0,
                                stopAllIntent, PendingIntent.FLAG_UPDATE_CURRENT));
            } else if (type == TYPE_PAUSED) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                contentText = res.getString(R.string.notification_paused_in_background);
                builder.addAction(com.android.internal.R.drawable.ic_media_play,
                        res.getString(R.string.download_resume_all),
                        PendingIntent.getBroadcast(mContext, 0, resumeAllIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT))
                        .addAction(com.android.internal.R.drawable.ic_media_stop,
                                res.getString(R.string.download_stop_all), PendingIntent.getBroadcast(mContext,
                                        0, stopAllIntent, PendingIntent.FLAG_UPDATE_CURRENT));
            } else if (type == TYPE_COMPLETE) {
                if (hasErrorStatus) {
                    contentText = res.getString(R.string.notification_download_failed);
                    builder.addAction(com.android.internal.R.drawable.ic_media_play,
                            res.getString(R.string.download_retry_all),
                            PendingIntent.getBroadcast(mContext, 0, retryAllIntent,
                                    PendingIntent.FLAG_UPDATE_CURRENT))
                            .addAction(com.android.internal.R.drawable.ic_media_stop,
                                    res.getString(R.string.download_stop_all), PendingIntent.getBroadcast(
                                            mContext, 0, stopAllIntent, PendingIntent.FLAG_UPDATE_CURRENT));
                }
            }

            inboxStyle.setSummaryText(contentText);
            builder.setContentText(contentText);
            builder.setContentInfo(percentText);
            notif = inboxStyle.build();
        }

        mNotifManager.notify(tag, 0, notif);
    }

    // Remove stale tags that weren't renewed
    final Iterator<String> it = mActiveNotifs.keySet().iterator();
    while (it.hasNext()) {
        final String tag = it.next();
        if (!clustered.containsKey(tag)) {
            mNotifManager.cancel(tag, 0);
            it.remove();
        }
    }
}