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 
17 package com.android.printspooler.model;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.Notification;
22 import android.app.Notification.Action;
23 import android.app.NotificationChannel;
24 import android.app.NotificationManager;
25 import android.app.PendingIntent;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.graphics.drawable.Icon;
30 import android.net.Uri;
31 import android.os.AsyncTask;
32 import android.os.PowerManager;
33 import android.os.PowerManager.WakeLock;
34 import android.os.RemoteException;
35 import android.os.ServiceManager;
36 import android.os.UserHandle;
37 import android.print.IPrintManager;
38 import android.print.PrintJobId;
39 import android.print.PrintJobInfo;
40 import android.print.PrintManager;
41 import android.provider.Settings;
42 import android.util.ArraySet;
43 import android.util.Log;
44 
45 import com.android.printspooler.R;
46 
47 import java.util.ArrayList;
48 import java.util.List;
49 
50 /**
51  * This class is responsible for updating the print notifications
52  * based on print job state transitions.
53  */
54 final class NotificationController {
55     public static final boolean DEBUG = false;
56 
57     public static final String LOG_TAG = "NotificationController";
58 
59     private static final String NOTIFICATION_CHANNEL_PROGRESS = "PRINT_PROGRESS";
60     private static final String NOTIFICATION_CHANNEL_FAILURES = "PRINT_FAILURES";
61 
62     private static final String INTENT_ACTION_CANCEL_PRINTJOB = "INTENT_ACTION_CANCEL_PRINTJOB";
63     private static final String INTENT_ACTION_RESTART_PRINTJOB = "INTENT_ACTION_RESTART_PRINTJOB";
64 
65     private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
66 
67     private final Context mContext;
68     private final NotificationManager mNotificationManager;
69 
70     /**
71      * Mapping from printJobIds to their notification Ids.
72      */
73     private final ArraySet<PrintJobId> mNotifications;
74 
NotificationController(Context context)75     public NotificationController(Context context) {
76         mContext = context;
77         mNotificationManager = (NotificationManager)
78                 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
79         mNotifications = new ArraySet<>(0);
80 
81         mNotificationManager.createNotificationChannel(
82                 new NotificationChannel(NOTIFICATION_CHANNEL_PROGRESS,
83                         context.getString(R.string.notification_channel_progress),
84                         NotificationManager.IMPORTANCE_LOW));
85         mNotificationManager.createNotificationChannel(
86                 new NotificationChannel(NOTIFICATION_CHANNEL_FAILURES,
87                         context.getString(R.string.notification_channel_failure),
88                         NotificationManager.IMPORTANCE_DEFAULT));
89     }
90 
onUpdateNotifications(List<PrintJobInfo> printJobs)91     public void onUpdateNotifications(List<PrintJobInfo> printJobs) {
92         List<PrintJobInfo> notifyPrintJobs = new ArrayList<>();
93 
94         final int printJobCount = printJobs.size();
95         for (int i = 0; i < printJobCount; i++) {
96             PrintJobInfo printJob = printJobs.get(i);
97             if (shouldNotifyForState(printJob.getState())) {
98                 notifyPrintJobs.add(printJob);
99             }
100         }
101 
102         updateNotifications(notifyPrintJobs);
103     }
104 
105     /**
106      * Update notifications for the given print jobs, remove all other notifications.
107      *
108      * @param printJobs The print job that we want to create notifications for.
109      */
updateNotifications(List<PrintJobInfo> printJobs)110     private void updateNotifications(List<PrintJobInfo> printJobs) {
111         ArraySet<PrintJobId> removedPrintJobs = new ArraySet<>(mNotifications);
112 
113         final int numPrintJobs = printJobs.size();
114 
115         // Create per print job notification
116         for (int i = 0; i < numPrintJobs; i++) {
117             PrintJobInfo printJob = printJobs.get(i);
118             PrintJobId printJobId = printJob.getId();
119 
120             removedPrintJobs.remove(printJobId);
121             mNotifications.add(printJobId);
122 
123             createSimpleNotification(printJob);
124         }
125 
126         // Remove notifications for print jobs that do not exist anymore
127         final int numRemovedPrintJobs = removedPrintJobs.size();
128         for (int i = 0; i < numRemovedPrintJobs; i++) {
129             PrintJobId removedPrintJob = removedPrintJobs.valueAt(i);
130 
131             mNotificationManager.cancel(removedPrintJob.flattenToString(), 0);
132             mNotifications.remove(removedPrintJob);
133         }
134     }
135 
createSimpleNotification(PrintJobInfo printJob)136     private void createSimpleNotification(PrintJobInfo printJob) {
137         switch (printJob.getState()) {
138             case PrintJobInfo.STATE_FAILED: {
139                 createFailedNotification(printJob);
140             } break;
141 
142             case PrintJobInfo.STATE_BLOCKED: {
143                 if (!printJob.isCancelling()) {
144                     createBlockedNotification(printJob);
145                 } else {
146                     createCancellingNotification(printJob);
147                 }
148             } break;
149 
150             default: {
151                 if (!printJob.isCancelling()) {
152                     createPrintingNotification(printJob);
153                 } else {
154                     createCancellingNotification(printJob);
155                 }
156             } break;
157         }
158     }
159 
160     /**
161      * Create an {@link Action} that cancels a {@link PrintJobInfo print job}.
162      *
163      * @param printJob The {@link PrintJobInfo print job} to cancel
164      *
165      * @return An {@link Action} that will cancel a print job
166      */
createCancelAction(PrintJobInfo printJob)167     private Action createCancelAction(PrintJobInfo printJob) {
168         return new Action.Builder(
169                 Icon.createWithResource(mContext, R.drawable.ic_clear),
170                 mContext.getString(R.string.cancel), createCancelIntent(printJob)).build();
171     }
172 
173     /**
174      * Create a notification for a print job.
175      *
176      * @param printJob the job the notification is for
177      * @param firstAction the first action shown in the notification
178      * @param secondAction the second action shown in the notification
179      */
createNotification(@onNull PrintJobInfo printJob, @Nullable Action firstAction, @Nullable Action secondAction)180     private void createNotification(@NonNull PrintJobInfo printJob, @Nullable Action firstAction,
181             @Nullable Action secondAction) {
182         Notification.Builder builder = new Notification.Builder(mContext, computeChannel(printJob))
183                 .setContentIntent(createContentIntent(printJob.getId()))
184                 .setSmallIcon(computeNotificationIcon(printJob))
185                 .setContentTitle(computeNotificationTitle(printJob))
186                 .setWhen(System.currentTimeMillis())
187                 .setOngoing(true)
188                 .setShowWhen(true)
189                 .setOnlyAlertOnce(true)
190                 .setColor(mContext.getColor(
191                         com.android.internal.R.color.system_notification_accent_color));
192 
193         if (firstAction != null) {
194             builder.addAction(firstAction);
195         }
196 
197         if (secondAction != null) {
198             builder.addAction(secondAction);
199         }
200 
201         if (printJob.getState() == PrintJobInfo.STATE_STARTED
202                 || printJob.getState() == PrintJobInfo.STATE_QUEUED) {
203             float progress = printJob.getProgress();
204             if (progress >= 0) {
205                 builder.setProgress(Integer.MAX_VALUE, (int) (Integer.MAX_VALUE * progress),
206                         false);
207             } else {
208                 builder.setProgress(Integer.MAX_VALUE, 0, true);
209             }
210         }
211 
212         CharSequence status = printJob.getStatus(mContext.getPackageManager());
213         if (status != null) {
214             builder.setContentText(status);
215         } else {
216             builder.setContentText(printJob.getPrinterName());
217         }
218 
219         mNotificationManager.notify(printJob.getId().flattenToString(), 0, builder.build());
220     }
221 
createPrintingNotification(PrintJobInfo printJob)222     private void createPrintingNotification(PrintJobInfo printJob) {
223         createNotification(printJob, createCancelAction(printJob), null);
224     }
225 
createFailedNotification(PrintJobInfo printJob)226     private void createFailedNotification(PrintJobInfo printJob) {
227         Action.Builder restartActionBuilder = new Action.Builder(
228                 Icon.createWithResource(mContext, com.android.internal.R.drawable.ic_restart),
229                 mContext.getString(R.string.restart), createRestartIntent(printJob.getId()));
230 
231         createNotification(printJob, createCancelAction(printJob), restartActionBuilder.build());
232     }
233 
createBlockedNotification(PrintJobInfo printJob)234     private void createBlockedNotification(PrintJobInfo printJob) {
235         createNotification(printJob, createCancelAction(printJob), null);
236     }
237 
createCancellingNotification(PrintJobInfo printJob)238     private void createCancellingNotification(PrintJobInfo printJob) {
239         createNotification(printJob, null, null);
240     }
241 
computeNotificationTitle(PrintJobInfo printJob)242     private String computeNotificationTitle(PrintJobInfo printJob) {
243         switch (printJob.getState()) {
244             case PrintJobInfo.STATE_FAILED: {
245                 return mContext.getString(R.string.failed_notification_title_template,
246                         printJob.getLabel());
247             }
248 
249             case PrintJobInfo.STATE_BLOCKED: {
250                 if (!printJob.isCancelling()) {
251                     return mContext.getString(R.string.blocked_notification_title_template,
252                             printJob.getLabel());
253                 } else {
254                     return mContext.getString(
255                             R.string.cancelling_notification_title_template,
256                             printJob.getLabel());
257                 }
258             }
259 
260             default: {
261                 if (!printJob.isCancelling()) {
262                     return mContext.getString(R.string.printing_notification_title_template,
263                             printJob.getLabel());
264                 } else {
265                     return mContext.getString(
266                             R.string.cancelling_notification_title_template,
267                             printJob.getLabel());
268                 }
269             }
270         }
271     }
272 
createContentIntent(PrintJobId printJobId)273     private PendingIntent createContentIntent(PrintJobId printJobId) {
274         Intent intent = new Intent(Settings.ACTION_PRINT_SETTINGS);
275         if (printJobId != null) {
276             intent.putExtra(EXTRA_PRINT_JOB_ID, printJobId.flattenToString());
277             intent.setData(Uri.fromParts("printjob", printJobId.flattenToString(), null));
278         }
279         return PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE);
280     }
281 
createCancelIntent(PrintJobInfo printJob)282     private PendingIntent createCancelIntent(PrintJobInfo printJob) {
283         Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class);
284         intent.setAction(INTENT_ACTION_CANCEL_PRINTJOB + "_" + printJob.getId().flattenToString());
285         intent.putExtra(EXTRA_PRINT_JOB_ID, printJob.getId());
286         return PendingIntent.getBroadcast(mContext, 0, intent,
287                 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
288     }
289 
createRestartIntent(PrintJobId printJobId)290     private PendingIntent createRestartIntent(PrintJobId printJobId) {
291         Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class);
292         intent.setAction(INTENT_ACTION_RESTART_PRINTJOB + "_" + printJobId.flattenToString());
293         intent.putExtra(EXTRA_PRINT_JOB_ID, printJobId);
294         return PendingIntent.getBroadcast(mContext, 0, intent,
295                 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
296     }
297 
shouldNotifyForState(int state)298     private static boolean shouldNotifyForState(int state) {
299         switch (state) {
300             case PrintJobInfo.STATE_QUEUED:
301             case PrintJobInfo.STATE_STARTED:
302             case PrintJobInfo.STATE_FAILED:
303             case PrintJobInfo.STATE_COMPLETED:
304             case PrintJobInfo.STATE_CANCELED:
305             case PrintJobInfo.STATE_BLOCKED: {
306                 return true;
307             }
308         }
309         return false;
310     }
311 
computeNotificationIcon(PrintJobInfo printJob)312     private static int computeNotificationIcon(PrintJobInfo printJob) {
313         switch (printJob.getState()) {
314             case PrintJobInfo.STATE_FAILED:
315             case PrintJobInfo.STATE_BLOCKED: {
316                 return com.android.internal.R.drawable.ic_print_error;
317             }
318             default: {
319                 if (!printJob.isCancelling()) {
320                     return com.android.internal.R.drawable.ic_print;
321                 } else {
322                     return R.drawable.ic_clear;
323                 }
324             }
325         }
326     }
327 
computeChannel(PrintJobInfo printJob)328     private static String computeChannel(PrintJobInfo printJob) {
329         if (printJob.isCancelling()) {
330             return NOTIFICATION_CHANNEL_PROGRESS;
331         }
332 
333         switch (printJob.getState()) {
334             case PrintJobInfo.STATE_FAILED:
335             case PrintJobInfo.STATE_BLOCKED: {
336                 return NOTIFICATION_CHANNEL_FAILURES;
337             }
338             default: {
339                 return NOTIFICATION_CHANNEL_PROGRESS;
340             }
341         }
342     }
343 
344     public static final class NotificationBroadcastReceiver extends BroadcastReceiver {
345         @SuppressWarnings("hiding")
346         private static final String LOG_TAG = "NotificationBroadcastReceiver";
347 
348         @Override
onReceive(Context context, Intent intent)349         public void onReceive(Context context, Intent intent) {
350             String action = intent.getAction();
351             if (action != null && action.startsWith(INTENT_ACTION_CANCEL_PRINTJOB)) {
352                 PrintJobId printJobId = intent.getExtras().getParcelable(EXTRA_PRINT_JOB_ID);
353                 handleCancelPrintJob(context, printJobId);
354             } else if (action != null && action.startsWith(INTENT_ACTION_RESTART_PRINTJOB)) {
355                 PrintJobId printJobId = intent.getExtras().getParcelable(EXTRA_PRINT_JOB_ID);
356                 handleRestartPrintJob(context, printJobId);
357             }
358         }
359 
handleCancelPrintJob(final Context context, final PrintJobId printJobId)360         private void handleCancelPrintJob(final Context context, final PrintJobId printJobId) {
361             if (DEBUG) {
362                 Log.i(LOG_TAG, "handleCancelPrintJob() printJobId:" + printJobId);
363             }
364 
365             // Call into the print manager service off the main thread since
366             // the print manager service may end up binding to the print spooler
367             // service which binding is handled on the main thread.
368             PowerManager powerManager = (PowerManager)
369                     context.getSystemService(Context.POWER_SERVICE);
370             final WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
371                     LOG_TAG);
372             wakeLock.acquire();
373 
374             new AsyncTask<Void, Void, Void>() {
375                 @Override
376                 protected Void doInBackground(Void... params) {
377                     // We need to request the cancellation to be done by the print
378                     // manager service since it has to communicate with the managing
379                     // print service to request the cancellation. Also we need the
380                     // system service to be bound to the spooler since canceling a
381                     // print job will trigger persistence of current jobs which is
382                     // done on another thread and until it finishes the spooler has
383                     // to be kept around.
384                     try {
385                         IPrintManager printManager = IPrintManager.Stub.asInterface(
386                                 ServiceManager.getService(Context.PRINT_SERVICE));
387                         printManager.cancelPrintJob(printJobId, PrintManager.APP_ID_ANY,
388                                 UserHandle.myUserId());
389                     } catch (RemoteException re) {
390                         Log.i(LOG_TAG, "Error requesting print job cancellation", re);
391                     } finally {
392                         wakeLock.release();
393                     }
394                     return null;
395                 }
396             }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
397         }
398 
handleRestartPrintJob(final Context context, final PrintJobId printJobId)399         private void handleRestartPrintJob(final Context context, final PrintJobId printJobId) {
400             if (DEBUG) {
401                 Log.i(LOG_TAG, "handleRestartPrintJob() printJobId:" + printJobId);
402             }
403 
404             // Call into the print manager service off the main thread since
405             // the print manager service may end up binding to the print spooler
406             // service which binding is handled on the main thread.
407             PowerManager powerManager = (PowerManager)
408                     context.getSystemService(Context.POWER_SERVICE);
409             final WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
410                     LOG_TAG);
411             wakeLock.acquire();
412 
413             new AsyncTask<Void, Void, Void>() {
414                 @Override
415                 protected Void doInBackground(Void... params) {
416                     // We need to request the restart to be done by the print manager
417                     // service since the latter must be bound to the spooler because
418                     // restarting a print job will trigger persistence of current jobs
419                     // which is done on another thread and until it finishes the spooler has
420                     // to be kept around.
421                     try {
422                         IPrintManager printManager = IPrintManager.Stub.asInterface(
423                                 ServiceManager.getService(Context.PRINT_SERVICE));
424                         printManager.restartPrintJob(printJobId, PrintManager.APP_ID_ANY,
425                                 UserHandle.myUserId());
426                     } catch (RemoteException re) {
427                         Log.i(LOG_TAG, "Error requesting print job restart", re);
428                     } finally {
429                         wakeLock.release();
430                     }
431                     return null;
432                 }
433             }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
434         }
435     }
436 }
437