List of usage examples for android.content.res Resources getText
@NonNull public CharSequence getText(@StringRes int id) throws NotFoundException
From source file:com.actionbarsherlock.internal.view.menu.MenuBuilder.java
private void setHeaderInternal(final int titleRes, final CharSequence title, final int iconRes, final Drawable icon, final View view) { final Resources r = getResources(); if (view != null) { mHeaderView = view;//from w ww. j a v a 2s . co m // If using a custom view, then the title and icon aren't used mHeaderTitle = null; mHeaderIcon = null; } else { if (titleRes > 0) { mHeaderTitle = r.getText(titleRes); } else if (title != null) { mHeaderTitle = title; } if (iconRes > 0) { mHeaderIcon = r.getDrawable(iconRes); } else if (icon != null) { mHeaderIcon = icon; } // If using the title or icon, then a custom view isn't used mHeaderView = null; } // Notify of change onItemsChanged(false); }
From source file:android.support.v7.internal.view.menu.MenuBuilder.java
private void setHeaderInternal(final int titleRes, final CharSequence title, final int iconRes, final Drawable icon, final View view) { final Resources r = getResources(); if (view != null) { mHeaderView = view;/* w ww .j av a2 s.c o m*/ // If using a custom view, then the title and icon aren't used mHeaderTitle = null; mHeaderIcon = null; } else { if (titleRes > 0) { mHeaderTitle = r.getText(titleRes); } else if (title != null) { mHeaderTitle = title; } if (iconRes > 0) { mHeaderIcon = ContextCompat.getDrawable(getContext(), iconRes); } else if (icon != null) { mHeaderIcon = icon; } // If using the title or icon, then a custom view isn't used mHeaderView = null; } // Notify of change onItemsChanged(false); }
From source file:com.wizardsofm.deskclock.data.TimerNotificationBuilderN.java
@Override public Notification build(Context context, NotificationModel nm, List<Timer> unexpired) { final Timer timer = unexpired.get(0); final int count = unexpired.size(); // Compute some values required below. final boolean running = timer.isRunning(); final Resources res = context.getResources(); final long base = getChronometerBase(timer); final String pname = context.getPackageName(); final RemoteViews content = new RemoteViews(pname, com.wizardsofm.deskclock.R.layout.chronometer_notif_content); content.setChronometerCountDown(com.wizardsofm.deskclock.R.id.chronometer, true); content.setChronometer(com.wizardsofm.deskclock.R.id.chronometer, base, null, running); final List<Notification.Action> actions = new ArrayList<>(2); final CharSequence stateText; if (count == 1) { if (running) { // Single timer is running. if (TextUtils.isEmpty(timer.getLabel())) { stateText = res.getString(com.wizardsofm.deskclock.R.string.timer_notification_label); } else { stateText = timer.getLabel(); }//from www. j a v a 2 s .co m // Left button: Pause final Intent pause = new Intent(context, TimerService.class) .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_TIMER) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()); final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_pause_24dp); final CharSequence title1 = res.getText(com.wizardsofm.deskclock.R.string.timer_pause); final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right Button: +1 Minute final Intent addMinute = new Intent(context, TimerService.class) .setAction(HandleDeskClockApiCalls.ACTION_ADD_MINUTE_TIMER) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()); final Icon icon2 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_add_24dp); final CharSequence title2 = res.getText(com.wizardsofm.deskclock.R.string.timer_plus_1_min); final PendingIntent intent2 = Utils.pendingServiceIntent(context, addMinute); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); } else { // Single timer is paused. stateText = res.getString(com.wizardsofm.deskclock.R.string.timer_paused); // Left button: Start final Intent start = new Intent(context, TimerService.class) .setAction(HandleDeskClockApiCalls.ACTION_START_TIMER) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()); final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_start_24dp); final CharSequence title1 = res.getText(com.wizardsofm.deskclock.R.string.sw_resume_button); final PendingIntent intent1 = Utils.pendingServiceIntent(context, start); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right Button: Reset final Intent reset = new Intent(context, TimerService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_TIMER) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()); final Icon icon2 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_reset_24dp); final CharSequence title2 = res.getText(com.wizardsofm.deskclock.R.string.sw_reset_button); final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); } } else { if (running) { // At least one timer is running. stateText = res.getString(com.wizardsofm.deskclock.R.string.timers_in_use, count); } else { // All timers are paused. stateText = res.getString(com.wizardsofm.deskclock.R.string.timers_stopped, count); } final Intent reset = TimerService.createResetUnexpiredTimersIntent(context); final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_reset_24dp); final CharSequence title1 = res.getText(com.wizardsofm.deskclock.R.string.timer_reset_all); final PendingIntent intent1 = Utils.pendingServiceIntent(context, reset); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); } content.setTextViewText(com.wizardsofm.deskclock.R.id.state, stateText); // Intent to load the app and show the timer when the notification is tapped. final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_TIMERS) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, com.wizardsofm.deskclock.R.string.label_notification); final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); return new Notification.Builder(context).setOngoing(true).setLocalOnly(true).setShowWhen(false) .setAutoCancel(false).setCustomContentView(content).setContentIntent(pendingShowApp) .setPriority(Notification.PRIORITY_HIGH).setCategory(Notification.CATEGORY_ALARM) .setSmallIcon(com.wizardsofm.deskclock.R.drawable.stat_notify_timer) .setGroup(nm.getTimerNotificationGroupKey()).setVisibility(Notification.VISIBILITY_PUBLIC) .setStyle(new Notification.DecoratedCustomViewStyle()) .setActions(actions.toArray(new Notification.Action[actions.size()])) .setColor(ContextCompat.getColor(context, com.wizardsofm.deskclock.R.color.default_background)) .build(); }
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);/*from w w w. j av a2 s . 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.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);//from ww w . jav a 2s . c o 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 { 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)); // FIXME "" + 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.snda.mymarket.providers.downloads.DownloadNotifier.java
private void updateWithLocked(LongSparseArray<DownloadInfo> downloads) { final Resources res = mContext.getResources(); // Cluster downloads together final Map<String, Collection<DownloadInfo>> clustered = new HashMap<String, Collection<DownloadInfo>>(); for (int index = 0; index < downloads.size(); index++) { final String tag = buildNotificationTag(downloads.valueAt(index)); if (tag != null) { Collection<DownloadInfo> arrDownloads = clustered.get(tag); if (arrDownloads == null) { arrDownloads = new ArrayList<DownloadInfo>(); clustered.put(tag, arrDownloads); }/*from ww w .j a v a 2 s .co m*/ arrDownloads.add(downloads.valueAt(index)); } } // 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.ALL_DOWNLOADS_CONTENT_URI, info.mId); builder.setAutoCancel(true); final String action; if (Downloads.isStatusError(info.mStatus)) { action = Constants.ACTION_LIST; } else { if (info.mDestination != Downloads.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) == null ? 0 : 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) / 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.isStatusError(info.mStatus)) { builder.setContentText(res.getText(R.string.notification_download_failed)); } else if (Downloads.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: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);/*from w w w . j ava 2 s . 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 { 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:tree.love.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 w w. j av a 2 s . c o m*/ // } final HashMap<String, List<DownloadInfo>> clustered = new HashMap<String, List<DownloadInfo>>(); for (DownloadInfo info : downloads) { final String tag = buildNotificationTag(info); if (tag != null) { List<DownloadInfo> collection = clustered.get(clustered); if (collection == null) { collection = new ArrayList<DownloadInfo>(); clustered.put(tag, collection); } collection.add(info); // clustered.put(tag, collection); } } // 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; 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, // TODO "" + remainingMillis); // 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 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(); } } }