Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 17 | package com.android.printspooler.model; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.Notification; |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 20 | import android.app.Notification.Action; |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 21 | import android.app.Notification.InboxStyle; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 22 | import android.app.NotificationManager; |
| 23 | import android.app.PendingIntent; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.BitmapDrawable; |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 28 | import android.graphics.drawable.Icon; |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 30 | import android.os.AsyncTask; |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 31 | import android.os.PowerManager; |
| 32 | import android.os.PowerManager.WakeLock; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
| 34 | import android.os.ServiceManager; |
| 35 | import android.os.UserHandle; |
| 36 | import android.print.IPrintManager; |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 37 | import android.print.PrintJobId; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 38 | import android.print.PrintJobInfo; |
| 39 | import android.print.PrintManager; |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 41 | import android.util.Log; |
| 42 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 43 | import com.android.printspooler.R; |
| 44 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
| 47 | |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 48 | /** |
| 49 | * This class is responsible for updating the print notifications |
| 50 | * based on print job state transitions. |
| 51 | */ |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 52 | final class NotificationController { |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 53 | public static final boolean DEBUG = false; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 54 | |
| 55 | public static final String LOG_TAG = "NotificationController"; |
| 56 | |
| 57 | private static final String INTENT_ACTION_CANCEL_PRINTJOB = "INTENT_ACTION_CANCEL_PRINTJOB"; |
| 58 | private static final String INTENT_ACTION_RESTART_PRINTJOB = "INTENT_ACTION_RESTART_PRINTJOB"; |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 59 | |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 60 | private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID"; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 61 | |
| 62 | private final Context mContext; |
| 63 | private final NotificationManager mNotificationManager; |
| 64 | |
| 65 | public NotificationController(Context context) { |
| 66 | mContext = context; |
| 67 | mNotificationManager = (NotificationManager) |
| 68 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 69 | } |
| 70 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 71 | public void onUpdateNotifications(List<PrintJobInfo> printJobs) { |
Svet Ganov | a4ab7808 | 2014-07-14 08:40:12 -0700 | [diff] [blame] | 72 | List<PrintJobInfo> notifyPrintJobs = new ArrayList<>(); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 73 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 74 | final int printJobCount = printJobs.size(); |
| 75 | for (int i = 0; i < printJobCount; i++) { |
| 76 | PrintJobInfo printJob = printJobs.get(i); |
| 77 | if (shouldNotifyForState(printJob.getState())) { |
| 78 | notifyPrintJobs.add(printJob); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | updateNotification(notifyPrintJobs); |
| 83 | } |
| 84 | |
| 85 | private void updateNotification(List<PrintJobInfo> printJobs) { |
| 86 | if (printJobs.size() <= 0) { |
| 87 | removeNotification(); |
| 88 | } else if (printJobs.size() == 1) { |
| 89 | createSimpleNotification(printJobs.get(0)); |
| 90 | } else { |
| 91 | createStackedNotification(printJobs); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private void createSimpleNotification(PrintJobInfo printJob) { |
| 96 | switch (printJob.getState()) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 97 | case PrintJobInfo.STATE_FAILED: { |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 98 | createFailedNotification(printJob); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 99 | } break; |
| 100 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 101 | case PrintJobInfo.STATE_BLOCKED: { |
| 102 | if (!printJob.isCancelling()) { |
| 103 | createBlockedNotification(printJob); |
| 104 | } else { |
| 105 | createCancellingNotification(printJob); |
| 106 | } |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 107 | } break; |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 108 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 109 | default: { |
| 110 | if (!printJob.isCancelling()) { |
| 111 | createPrintingNotification(printJob); |
| 112 | } else { |
| 113 | createCancellingNotification(printJob); |
| 114 | } |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 115 | } break; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Create an {@link Action} that cancels a {@link PrintJobInfo print job}. |
| 121 | * |
| 122 | * @param printJob The {@link PrintJobInfo print job} to cancel |
| 123 | * |
| 124 | * @return An {@link Action} that will cancel a print job |
| 125 | */ |
| 126 | private Action createCancelAction(PrintJobInfo printJob) { |
| 127 | return new Action.Builder( |
| 128 | Icon.createWithResource(mContext, R.drawable.stat_notify_cancelling), |
| 129 | mContext.getString(R.string.cancel), createCancelIntent(printJob)).build(); |
| 130 | } |
| 131 | |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 132 | private void createPrintingNotification(PrintJobInfo printJob) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 133 | Notification.Builder builder = new Notification.Builder(mContext) |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 134 | .setContentIntent(createContentIntent(printJob.getId())) |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 135 | .setSmallIcon(computeNotificationIcon(printJob)) |
| 136 | .setContentTitle(computeNotificationTitle(printJob)) |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 137 | .addAction(createCancelAction(printJob)) |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 138 | .setContentText(printJob.getPrinterName()) |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 139 | .setWhen(System.currentTimeMillis()) |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 140 | .setOngoing(true) |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 141 | .setShowWhen(true) |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 142 | .setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 143 | com.android.internal.R.color.system_notification_accent_color)); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 144 | mNotificationManager.notify(0, builder.build()); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 147 | private void createFailedNotification(PrintJobInfo printJob) { |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 148 | Action.Builder restartActionBuilder = new Action.Builder( |
| 149 | Icon.createWithResource(mContext, R.drawable.ic_restart), |
| 150 | mContext.getString(R.string.restart), createRestartIntent(printJob.getId())); |
| 151 | |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 152 | Notification.Builder builder = new Notification.Builder(mContext) |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 153 | .setContentIntent(createContentIntent(printJob.getId())) |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 154 | .setSmallIcon(computeNotificationIcon(printJob)) |
| 155 | .setContentTitle(computeNotificationTitle(printJob)) |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 156 | .addAction(createCancelAction(printJob)) |
| 157 | .addAction(restartActionBuilder.build()) |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 158 | .setContentText(printJob.getPrinterName()) |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 159 | .setWhen(System.currentTimeMillis()) |
| 160 | .setOngoing(true) |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 161 | .setShowWhen(true) |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 162 | .setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 163 | com.android.internal.R.color.system_notification_accent_color)); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 164 | mNotificationManager.notify(0, builder.build()); |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | private void createBlockedNotification(PrintJobInfo printJob) { |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 168 | Notification.Builder builder = new Notification.Builder(mContext) |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 169 | .setContentIntent(createContentIntent(printJob.getId())) |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 170 | .setSmallIcon(computeNotificationIcon(printJob)) |
| 171 | .setContentTitle(computeNotificationTitle(printJob)) |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 172 | .addAction(createCancelAction(printJob)) |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 173 | .setContentText(printJob.getPrinterName()) |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 174 | .setWhen(System.currentTimeMillis()) |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 175 | .setOngoing(true) |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 176 | .setShowWhen(true) |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 177 | .setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 178 | com.android.internal.R.color.system_notification_accent_color)); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 179 | mNotificationManager.notify(0, builder.build()); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 182 | private void createCancellingNotification(PrintJobInfo printJob) { |
| 183 | Notification.Builder builder = new Notification.Builder(mContext) |
| 184 | .setContentIntent(createContentIntent(printJob.getId())) |
| 185 | .setSmallIcon(computeNotificationIcon(printJob)) |
| 186 | .setContentTitle(computeNotificationTitle(printJob)) |
| 187 | .setContentText(printJob.getPrinterName()) |
| 188 | .setWhen(System.currentTimeMillis()) |
| 189 | .setOngoing(true) |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 190 | .setShowWhen(true) |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 191 | .setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 192 | com.android.internal.R.color.system_notification_accent_color)); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 193 | mNotificationManager.notify(0, builder.build()); |
| 194 | } |
| 195 | |
| 196 | private void createStackedNotification(List<PrintJobInfo> printJobs) { |
| 197 | Notification.Builder builder = new Notification.Builder(mContext) |
| 198 | .setContentIntent(createContentIntent(null)) |
| 199 | .setWhen(System.currentTimeMillis()) |
| 200 | .setOngoing(true) |
| 201 | .setShowWhen(true); |
| 202 | |
| 203 | final int printJobCount = printJobs.size(); |
| 204 | |
| 205 | InboxStyle inboxStyle = new InboxStyle(); |
| 206 | inboxStyle.setBigContentTitle(String.format(mContext.getResources().getQuantityText( |
| 207 | R.plurals.composite_notification_title_template, |
| 208 | printJobCount).toString(), printJobCount)); |
| 209 | |
| 210 | for (int i = printJobCount - 1; i>= 0; i--) { |
| 211 | PrintJobInfo printJob = printJobs.get(i); |
| 212 | if (i == printJobCount - 1) { |
| 213 | builder.setLargeIcon(((BitmapDrawable) mContext.getResources().getDrawable( |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 214 | computeNotificationIcon(printJob), null)).getBitmap()); |
Svetoslav Ganov | d91cb3e | 2013-10-12 15:44:42 -0700 | [diff] [blame] | 215 | builder.setSmallIcon(computeNotificationIcon(printJob)); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 216 | builder.setContentTitle(computeNotificationTitle(printJob)); |
| 217 | builder.setContentText(printJob.getPrinterName()); |
| 218 | } |
| 219 | inboxStyle.addLine(computeNotificationTitle(printJob)); |
| 220 | } |
| 221 | |
| 222 | builder.setNumber(printJobCount); |
| 223 | builder.setStyle(inboxStyle); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 224 | builder.setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 225 | com.android.internal.R.color.system_notification_accent_color)); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 226 | |
| 227 | mNotificationManager.notify(0, builder.build()); |
| 228 | } |
| 229 | |
| 230 | private String computeNotificationTitle(PrintJobInfo printJob) { |
| 231 | switch (printJob.getState()) { |
| 232 | case PrintJobInfo.STATE_FAILED: { |
| 233 | return mContext.getString(R.string.failed_notification_title_template, |
| 234 | printJob.getLabel()); |
| 235 | } |
| 236 | |
| 237 | case PrintJobInfo.STATE_BLOCKED: { |
| 238 | if (!printJob.isCancelling()) { |
| 239 | return mContext.getString(R.string.blocked_notification_title_template, |
| 240 | printJob.getLabel()); |
| 241 | } else { |
| 242 | return mContext.getString( |
| 243 | R.string.cancelling_notification_title_template, |
| 244 | printJob.getLabel()); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | default: { |
| 249 | if (!printJob.isCancelling()) { |
| 250 | return mContext.getString(R.string.printing_notification_title_template, |
| 251 | printJob.getLabel()); |
| 252 | } else { |
| 253 | return mContext.getString( |
| 254 | R.string.cancelling_notification_title_template, |
| 255 | printJob.getLabel()); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private void removeNotification() { |
| 262 | mNotificationManager.cancel(0); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 265 | private PendingIntent createContentIntent(PrintJobId printJobId) { |
| 266 | Intent intent = new Intent(Settings.ACTION_PRINT_SETTINGS); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 267 | if (printJobId != null) { |
| 268 | intent.putExtra(EXTRA_PRINT_JOB_ID, printJobId.flattenToString()); |
| 269 | intent.setData(Uri.fromParts("printjob", printJobId.flattenToString(), null)); |
| 270 | } |
| 271 | return PendingIntent.getActivity(mContext, 0, intent, 0); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 274 | private PendingIntent createCancelIntent(PrintJobInfo printJob) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 275 | Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class); |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 276 | intent.setAction(INTENT_ACTION_CANCEL_PRINTJOB + "_" + printJob.getId().flattenToString()); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 277 | intent.putExtra(EXTRA_PRINT_JOB_ID, printJob.getId()); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 278 | return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT); |
| 279 | } |
| 280 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 281 | private PendingIntent createRestartIntent(PrintJobId printJobId) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 282 | Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class); |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 283 | intent.setAction(INTENT_ACTION_RESTART_PRINTJOB + "_" + printJobId.flattenToString()); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 284 | intent.putExtra(EXTRA_PRINT_JOB_ID, printJobId); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 285 | return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT); |
| 286 | } |
| 287 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 288 | private static boolean shouldNotifyForState(int state) { |
| 289 | switch (state) { |
| 290 | case PrintJobInfo.STATE_QUEUED: |
| 291 | case PrintJobInfo.STATE_STARTED: |
| 292 | case PrintJobInfo.STATE_FAILED: |
| 293 | case PrintJobInfo.STATE_COMPLETED: |
| 294 | case PrintJobInfo.STATE_CANCELED: |
| 295 | case PrintJobInfo.STATE_BLOCKED: { |
| 296 | return true; |
| 297 | } |
| 298 | } |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | private static int computeNotificationIcon(PrintJobInfo printJob) { |
| 303 | switch (printJob.getState()) { |
| 304 | case PrintJobInfo.STATE_FAILED: |
| 305 | case PrintJobInfo.STATE_BLOCKED: { |
| 306 | return com.android.internal.R.drawable.ic_print_error; |
| 307 | } |
| 308 | default: { |
| 309 | if (!printJob.isCancelling()) { |
| 310 | return com.android.internal.R.drawable.ic_print; |
| 311 | } else { |
| 312 | return R.drawable.stat_notify_cancelling; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 318 | public static final class NotificationBroadcastReceiver extends BroadcastReceiver { |
Philip P. Moltmann | dd635ce | 2015-11-20 16:35:05 -0800 | [diff] [blame] | 319 | @SuppressWarnings("hiding") |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 320 | private static final String LOG_TAG = "NotificationBroadcastReceiver"; |
| 321 | |
| 322 | @Override |
| 323 | public void onReceive(Context context, Intent intent) { |
| 324 | String action = intent.getAction(); |
| 325 | if (action != null && action.startsWith(INTENT_ACTION_CANCEL_PRINTJOB)) { |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 326 | PrintJobId printJobId = intent.getExtras().getParcelable(EXTRA_PRINT_JOB_ID); |
Svet Ganov | a4ab7808 | 2014-07-14 08:40:12 -0700 | [diff] [blame] | 327 | handleCancelPrintJob(context, printJobId); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 328 | } else if (action != null && action.startsWith(INTENT_ACTION_RESTART_PRINTJOB)) { |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 329 | PrintJobId printJobId = intent.getExtras().getParcelable(EXTRA_PRINT_JOB_ID); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 330 | handleRestartPrintJob(context, printJobId); |
| 331 | } |
| 332 | } |
| 333 | |
Svet Ganov | a4ab7808 | 2014-07-14 08:40:12 -0700 | [diff] [blame] | 334 | private void handleCancelPrintJob(final Context context, final PrintJobId printJobId) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 335 | if (DEBUG) { |
| 336 | Log.i(LOG_TAG, "handleCancelPrintJob() printJobId:" + printJobId); |
| 337 | } |
| 338 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 339 | // Call into the print manager service off the main thread since |
| 340 | // the print manager service may end up binding to the print spooler |
| 341 | // service which binding is handled on the main thread. |
| 342 | PowerManager powerManager = (PowerManager) |
| 343 | context.getSystemService(Context.POWER_SERVICE); |
| 344 | final WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 345 | LOG_TAG); |
| 346 | wakeLock.acquire(); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 347 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 348 | new AsyncTask<Void, Void, Void>() { |
| 349 | @Override |
| 350 | protected Void doInBackground(Void... params) { |
| 351 | // We need to request the cancellation to be done by the print |
| 352 | // manager service since it has to communicate with the managing |
| 353 | // print service to request the cancellation. Also we need the |
| 354 | // system service to be bound to the spooler since canceling a |
| 355 | // print job will trigger persistence of current jobs which is |
| 356 | // done on another thread and until it finishes the spooler has |
| 357 | // to be kept around. |
| 358 | try { |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 359 | IPrintManager printManager = IPrintManager.Stub.asInterface( |
| 360 | ServiceManager.getService(Context.PRINT_SERVICE)); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 361 | printManager.cancelPrintJob(printJobId, PrintManager.APP_ID_ANY, |
| 362 | UserHandle.myUserId()); |
| 363 | } catch (RemoteException re) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 364 | Log.i(LOG_TAG, "Error requesting print job cancellation", re); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 365 | } finally { |
| 366 | wakeLock.release(); |
| 367 | } |
| 368 | return null; |
| 369 | } |
| 370 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 373 | private void handleRestartPrintJob(final Context context, final PrintJobId printJobId) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 374 | if (DEBUG) { |
| 375 | Log.i(LOG_TAG, "handleRestartPrintJob() printJobId:" + printJobId); |
| 376 | } |
| 377 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 378 | // Call into the print manager service off the main thread since |
| 379 | // the print manager service may end up binding to the print spooler |
| 380 | // service which binding is handled on the main thread. |
| 381 | PowerManager powerManager = (PowerManager) |
| 382 | context.getSystemService(Context.POWER_SERVICE); |
| 383 | final WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 384 | LOG_TAG); |
| 385 | wakeLock.acquire(); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 386 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 387 | new AsyncTask<Void, Void, Void>() { |
| 388 | @Override |
| 389 | protected Void doInBackground(Void... params) { |
| 390 | // We need to request the restart to be done by the print manager |
| 391 | // service since the latter must be bound to the spooler because |
| 392 | // restarting a print job will trigger persistence of current jobs |
| 393 | // which is done on another thread and until it finishes the spooler has |
| 394 | // to be kept around. |
| 395 | try { |
| 396 | IPrintManager printManager = IPrintManager.Stub.asInterface( |
| 397 | ServiceManager.getService(Context.PRINT_SERVICE)); |
| 398 | printManager.restartPrintJob(printJobId, PrintManager.APP_ID_ANY, |
| 399 | UserHandle.myUserId()); |
| 400 | } catch (RemoteException re) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 401 | Log.i(LOG_TAG, "Error requesting print job restart", re); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 402 | } finally { |
| 403 | wakeLock.release(); |
| 404 | } |
| 405 | return null; |
| 406 | } |
| 407 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |