DownloadNotificationpublic class DownloadNotification extends Object This class handles the updating of the Notification Manager for the
cases where there is an ongoing download. Once the download is complete
(be it successful or unsuccessful) it is no longer the responsibility
of this component to show the download in the notification manager. |
Fields Summary |
---|
android.content.Context | mContext | public android.app.NotificationManager | mNotificationMgr | HashMap | mNotifications | static final String | LOGTAG | static final String | WHERE_RUNNING | static final String | WHERE_COMPLETED |
Constructors Summary |
---|
DownloadNotification(android.content.Context ctx)Constructor
mContext = ctx;
mNotificationMgr = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifications = new HashMap<String, NotificationItem>();
|
Methods Summary |
---|
private java.lang.String | getDownloadingText(long totalBytes, long currentBytes)
if (totalBytes <= 0) {
return "";
}
long progress = currentBytes * 100 / totalBytes;
StringBuilder sb = new StringBuilder();
sb.append(progress);
sb.append('%");
return sb.toString();
| private void | updateActiveNotification()
// Active downloads
Cursor c = mContext.getContentResolver().query(
Downloads.CONTENT_URI, new String [] {
Downloads._ID, Downloads.TITLE, Downloads.DESCRIPTION,
Downloads.NOTIFICATION_PACKAGE,
Downloads.NOTIFICATION_CLASS,
Downloads.CURRENT_BYTES, Downloads.TOTAL_BYTES,
Downloads.STATUS, Downloads._DATA
},
WHERE_RUNNING, null, Downloads._ID);
if (c == null) {
return;
}
// Columns match projection in query above
final int idColumn = 0;
final int titleColumn = 1;
final int descColumn = 2;
final int ownerColumn = 3;
final int classOwnerColumn = 4;
final int currentBytesColumn = 5;
final int totalBytesColumn = 6;
final int statusColumn = 7;
final int filenameColumnId = 8;
// Collate the notifications
mNotifications.clear();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String packageName = c.getString(ownerColumn);
int max = c.getInt(totalBytesColumn);
int progress = c.getInt(currentBytesColumn);
String title = c.getString(titleColumn);
if (title == null || title.length() == 0) {
title = mContext.getResources().getString(
R.string.download_unknown_title);
}
if (mNotifications.containsKey(packageName)) {
mNotifications.get(packageName).addItem(title, progress, max);
} else {
NotificationItem item = new NotificationItem();
item.id = c.getInt(idColumn);
item.packageName = packageName;
item.description = c.getString(descColumn);
String className = c.getString(classOwnerColumn);
item.addItem(title, progress, max);
mNotifications.put(packageName, item);
}
}
c.close();
// Add the notifications
for (NotificationItem item : mNotifications.values()) {
// Build the notification object
Notification n = new Notification();
n.icon = android.R.drawable.stat_sys_download;
n.flags |= Notification.FLAG_ONGOING_EVENT;
// Build the RemoteView object
RemoteViews expandedView = new RemoteViews(
"com.android.providers.downloads",
R.layout.status_bar_ongoing_event_progress_bar);
StringBuilder title = new StringBuilder(item.titles[0]);
if (item.titleCount > 1) {
title.append(mContext.getString(R.string.notification_filename_separator));
title.append(item.titles[1]);
n.number = item.titleCount;
if (item.titleCount > 2) {
title.append(mContext.getString(R.string.notification_filename_extras,
new Object[] { Integer.valueOf(item.titleCount - 2) }));
}
} else {
expandedView.setTextViewText(R.id.description,
item.description);
}
expandedView.setTextViewText(R.id.title, title);
expandedView.setProgressBar(R.id.progress_bar,
item.totalTotal,
item.totalCurrent,
item.totalTotal == -1);
expandedView.setTextViewText(R.id.progress_text,
getDownloadingText(item.totalTotal, item.totalCurrent));
expandedView.setImageViewResource(R.id.appIcon,
android.R.drawable.stat_sys_download);
n.contentView = expandedView;
Intent intent = new Intent(Constants.ACTION_LIST);
intent.setClassName("com.android.providers.downloads",
DownloadReceiver.class.getName());
intent.setData(Uri.parse(Downloads.CONTENT_URI + "/" + item.id));
intent.putExtra("multiple", item.titleCount > 1);
n.contentIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
mNotificationMgr.notify(item.id, n);
}
| private void | updateCompletedNotification()
// Completed downloads
Cursor c = mContext.getContentResolver().query(
Downloads.CONTENT_URI, new String [] {
Downloads._ID, Downloads.TITLE, Downloads.DESCRIPTION,
Downloads.NOTIFICATION_PACKAGE,
Downloads.NOTIFICATION_CLASS,
Downloads.CURRENT_BYTES, Downloads.TOTAL_BYTES,
Downloads.STATUS, Downloads._DATA,
Downloads.LAST_MODIFICATION, Downloads.DESTINATION
},
WHERE_COMPLETED, null, Downloads._ID);
if (c == null) {
return;
}
// Columns match projection in query above
final int idColumn = 0;
final int titleColumn = 1;
final int descColumn = 2;
final int ownerColumn = 3;
final int classOwnerColumn = 4;
final int currentBytesColumn = 5;
final int totalBytesColumn = 6;
final int statusColumn = 7;
final int filenameColumnId = 8;
final int lastModColumnId = 9;
final int destinationColumnId = 10;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
// Add the notifications
Notification n = new Notification();
n.icon = android.R.drawable.stat_sys_download_done;
String title = c.getString(titleColumn);
if (title == null || title.length() == 0) {
title = mContext.getResources().getString(
R.string.download_unknown_title);
}
Uri contentUri = Uri.parse(Downloads.CONTENT_URI + "/" + c.getInt(idColumn));
String caption;
Intent intent;
if (Downloads.isStatusError(c.getInt(statusColumn))) {
caption = mContext.getResources()
.getString(R.string.notification_download_failed);
intent = new Intent(Constants.ACTION_LIST);
} else {
caption = mContext.getResources()
.getString(R.string.notification_download_complete);
if (c.getInt(destinationColumnId) == Downloads.DESTINATION_EXTERNAL) {
intent = new Intent(Constants.ACTION_OPEN);
} else {
intent = new Intent(Constants.ACTION_LIST);
}
}
intent.setClassName("com.android.providers.downloads",
DownloadReceiver.class.getName());
intent.setData(contentUri);
n.setLatestEventInfo(mContext, title, caption,
PendingIntent.getBroadcast(mContext, 0, intent, 0));
intent = new Intent(Constants.ACTION_HIDE);
intent.setClassName("com.android.providers.downloads",
DownloadReceiver.class.getName());
intent.setData(contentUri);
n.deleteIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
n.when = c.getLong(lastModColumnId);
mNotificationMgr.notify(c.getInt(idColumn), n);
}
c.close();
| public void | updateNotification()
updateActiveNotification();
updateCompletedNotification();
|
|