Merge "Make print failure notifications important" into oc-dev

This commit is contained in:
TreeHugger Robot
2017-04-06 01:53:53 +00:00
committed by Android (Google) Code Review
2 changed files with 38 additions and 52 deletions

View File

@@ -149,6 +149,12 @@
<!-- Description of printer info icon. [CHAR LIMIT=50] -->
<string name="printer_info_desc">More information about this printer</string>
<!-- Label for the notification channel that contains print jobs without problems. [CHAR LIMIT=40] -->
<string name="notification_channel_progress">Running print jobs</string>
<!-- Label for the notification channel that contains print jobs with problems. [CHAR LIMIT=40] -->
<string name="notification_channel_failure">Failed print jobs</string>
<!-- Notification that we could not create a file name for the printed PDF. [CHAR LIMIT=50] -->
<string name="could_not_create_file">Could not create file</string>

View File

@@ -20,13 +20,12 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.Notification;
import android.app.Notification.Action;
import android.app.Notification.InboxStyle;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.AsyncTask;
@@ -57,14 +56,14 @@ final class NotificationController {
public static final String LOG_TAG = "NotificationController";
private static final String NOTIFICATION_CHANNEL_PROGRESS = "PRINT_PROGRESS";
private static final String NOTIFICATION_CHANNEL_FAILURES = "PRINT_FAILURES";
private static final String INTENT_ACTION_CANCEL_PRINTJOB = "INTENT_ACTION_CANCEL_PRINTJOB";
private static final String INTENT_ACTION_RESTART_PRINTJOB = "INTENT_ACTION_RESTART_PRINTJOB";
private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
private static final String PRINT_JOB_NOTIFICATION_GROUP_KEY = "PRINT_JOB_NOTIFICATIONS";
private static final String PRINT_JOB_NOTIFICATION_SUMMARY = "PRINT_JOB_NOTIFICATIONS_SUMMARY";
private final Context mContext;
private final NotificationManager mNotificationManager;
@@ -78,6 +77,15 @@ final class NotificationController {
mNotificationManager = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifications = new ArraySet<>(0);
mNotificationManager.createNotificationChannel(
new NotificationChannel(NOTIFICATION_CHANNEL_PROGRESS,
context.getString(R.string.notification_channel_progress),
NotificationManager.IMPORTANCE_LOW));
mNotificationManager.createNotificationChannel(
new NotificationChannel(NOTIFICATION_CHANNEL_FAILURES,
context.getString(R.string.notification_channel_failure),
NotificationManager.IMPORTANCE_DEFAULT));
}
public void onUpdateNotifications(List<PrintJobInfo> printJobs) {
@@ -104,13 +112,6 @@ final class NotificationController {
final int numPrintJobs = printJobs.size();
// Create summary notification
if (numPrintJobs > 1) {
createStackedNotification(printJobs);
} else {
mNotificationManager.cancel(PRINT_JOB_NOTIFICATION_SUMMARY, 0);
}
// Create per print job notification
for (int i = 0; i < numPrintJobs; i++) {
PrintJobInfo printJob = printJobs.get(i);
@@ -178,16 +179,16 @@ final class NotificationController {
*/
private void createNotification(@NonNull PrintJobInfo printJob, @Nullable Action firstAction,
@Nullable Action secondAction) {
Notification.Builder builder = new Notification.Builder(mContext)
Notification.Builder builder = new Notification.Builder(mContext, computeChannel(printJob))
.setContentIntent(createContentIntent(printJob.getId()))
.setSmallIcon(computeNotificationIcon(printJob))
.setContentTitle(computeNotificationTitle(printJob))
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setShowWhen(true)
.setOnlyAlertOnce(true)
.setColor(mContext.getColor(
com.android.internal.R.color.system_notification_accent_color))
.setGroup(PRINT_JOB_NOTIFICATION_GROUP_KEY);
com.android.internal.R.color.system_notification_accent_color));
if (firstAction != null) {
builder.addAction(firstAction);
@@ -238,43 +239,6 @@ final class NotificationController {
createNotification(printJob, null, null);
}
private void createStackedNotification(List<PrintJobInfo> printJobs) {
Notification.Builder builder = new Notification.Builder(mContext)
.setContentIntent(createContentIntent(null))
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setShowWhen(true)
.setGroup(PRINT_JOB_NOTIFICATION_GROUP_KEY)
.setGroupSummary(true);
final int printJobCount = printJobs.size();
InboxStyle inboxStyle = new InboxStyle();
int icon = com.android.internal.R.drawable.ic_print;
for (int i = printJobCount - 1; i>= 0; i--) {
PrintJobInfo printJob = printJobs.get(i);
inboxStyle.addLine(computeNotificationTitle(printJob));
// if any print job is in an error state show an error icon for the summary
if (printJob.getState() == PrintJobInfo.STATE_FAILED
|| printJob.getState() == PrintJobInfo.STATE_BLOCKED) {
icon = com.android.internal.R.drawable.ic_print_error;
}
}
builder.setSmallIcon(icon);
builder.setLargeIcon(
((BitmapDrawable) mContext.getResources().getDrawable(icon, null)).getBitmap());
builder.setNumber(printJobCount);
builder.setStyle(inboxStyle);
builder.setColor(mContext.getColor(
com.android.internal.R.color.system_notification_accent_color));
mNotificationManager.notify(PRINT_JOB_NOTIFICATION_SUMMARY, 0, builder.build());
}
private String computeNotificationTitle(PrintJobInfo printJob) {
switch (printJob.getState()) {
case PrintJobInfo.STATE_FAILED: {
@@ -359,6 +323,22 @@ final class NotificationController {
}
}
private static String computeChannel(PrintJobInfo printJob) {
if (printJob.isCancelling()) {
return NOTIFICATION_CHANNEL_PROGRESS;
}
switch (printJob.getState()) {
case PrintJobInfo.STATE_FAILED:
case PrintJobInfo.STATE_BLOCKED: {
return NOTIFICATION_CHANNEL_FAILURES;
}
default: {
return NOTIFICATION_CHANNEL_PROGRESS;
}
}
}
public static final class NotificationBroadcastReceiver extends BroadcastReceiver {
@SuppressWarnings("hiding")
private static final String LOG_TAG = "NotificationBroadcastReceiver";