Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -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 | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 18 | |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 19 | import static com.android.internal.print.DumpUtils.writeComponentName; |
| 20 | import static com.android.internal.print.DumpUtils.writePrintJobInfo; |
| 21 | |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 22 | import android.annotation.FloatRange; |
| 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.Nullable; |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 25 | import android.annotation.StringRes; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 26 | import android.app.Service; |
| 27 | import android.content.ComponentName; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 30 | import android.graphics.drawable.Icon; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 31 | import android.os.AsyncTask; |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 32 | import android.os.Binder; |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 34 | import android.os.IBinder; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 35 | import android.os.Message; |
| 36 | import android.os.ParcelFileDescriptor; |
| 37 | import android.os.RemoteException; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 38 | import android.print.IPrintSpooler; |
| 39 | import android.print.IPrintSpoolerCallbacks; |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 40 | import android.print.IPrintSpoolerClient; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 41 | import android.print.PageRange; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 42 | import android.print.PrintAttributes; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 43 | import android.print.PrintAttributes.Margins; |
| 44 | import android.print.PrintAttributes.MediaSize; |
| 45 | import android.print.PrintAttributes.Resolution; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 46 | import android.print.PrintDocumentInfo; |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 47 | import android.print.PrintJobId; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 48 | import android.print.PrintJobInfo; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 49 | import android.print.PrintManager; |
| 50 | import android.print.PrinterId; |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 51 | import android.service.print.PrintSpoolerInternalStateProto; |
Svetoslav | a76233a | 2013-09-05 09:38:02 -0700 | [diff] [blame] | 52 | import android.text.TextUtils; |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 53 | import android.util.ArrayMap; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 54 | import android.util.AtomicFile; |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 55 | import android.util.Log; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 56 | import android.util.Slog; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 57 | import android.util.Xml; |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 58 | import android.util.proto.ProtoOutputStream; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 59 | |
Chris Wren | dcc34fd | 2015-07-30 14:27:02 -0400 | [diff] [blame] | 60 | import com.android.internal.logging.MetricsLogger; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 61 | import com.android.internal.os.HandlerCaller; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 62 | import com.android.internal.util.FastXmlSerializer; |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 63 | import com.android.internal.util.Preconditions; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 64 | import com.android.printspooler.R; |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 65 | import com.android.printspooler.util.ApprovedPrintServices; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 66 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 67 | import libcore.io.IoUtils; |
| 68 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 69 | import org.xmlpull.v1.XmlPullParser; |
| 70 | import org.xmlpull.v1.XmlPullParserException; |
| 71 | import org.xmlpull.v1.XmlSerializer; |
| 72 | |
| 73 | import java.io.File; |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 74 | import java.io.FileDescriptor; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 75 | import java.io.FileInputStream; |
| 76 | import java.io.FileNotFoundException; |
| 77 | import java.io.FileOutputStream; |
| 78 | import java.io.IOException; |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 79 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 80 | import java.nio.charset.StandardCharsets; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 81 | import java.util.ArrayList; |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 82 | import java.util.List; |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 83 | import java.util.Set; |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 84 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Service for exposing some of the {@link PrintSpooler} functionality to |
| 87 | * another process. |
| 88 | */ |
| 89 | public final class PrintSpoolerService extends Service { |
| 90 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 91 | private static final String LOG_TAG = "PrintSpoolerService"; |
| 92 | |
Svetoslav | b5f1806 | 2013-09-23 18:48:34 -0700 | [diff] [blame] | 93 | private static final boolean DEBUG_PRINT_JOB_LIFECYCLE = false; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 94 | |
Svetoslav | c606679 | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 95 | private static final boolean DEBUG_PERSISTENCE = false; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 96 | |
Svet Ganov | 4237c92 | 2014-10-24 12:53:23 -0700 | [diff] [blame] | 97 | private static final boolean PERSISTENCE_MANAGER_ENABLED = true; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 98 | |
Philip P. Moltmann | 2e30126 | 2016-06-16 12:39:54 -0700 | [diff] [blame] | 99 | private static final String PRINT_JOB_STATE_HISTO = "print_job_state"; |
| 100 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 101 | private static final long CHECK_ALL_PRINTJOBS_HANDLED_DELAY = 5000; |
| 102 | |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 103 | private static final String PRINT_JOB_FILE_PREFIX = "print_job_"; |
| 104 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 105 | private static final String PRINT_FILE_EXTENSION = "pdf"; |
| 106 | |
| 107 | private static final Object sLock = new Object(); |
| 108 | |
| 109 | private final Object mLock = new Object(); |
| 110 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 111 | private final List<PrintJobInfo> mPrintJobs = new ArrayList<>(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 112 | |
| 113 | private static PrintSpoolerService sInstance; |
| 114 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 115 | private IPrintSpoolerClient mClient; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 116 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 117 | private HandlerCaller mHandlerCaller; |
| 118 | |
| 119 | private PersistenceManager mPersistanceManager; |
| 120 | |
| 121 | private NotificationController mNotificationController; |
| 122 | |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 123 | /** Cache for custom printer icons loaded from the print service */ |
| 124 | private CustomPrinterIconCache mCustomIconCache; |
| 125 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 126 | public static PrintSpoolerService peekInstance() { |
| 127 | synchronized (sLock) { |
| 128 | return sInstance; |
| 129 | } |
| 130 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 131 | |
| 132 | @Override |
| 133 | public void onCreate() { |
| 134 | super.onCreate(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 135 | mHandlerCaller = new HandlerCaller(this, getMainLooper(), |
| 136 | new HandlerCallerCallback(), false); |
| 137 | |
| 138 | mPersistanceManager = new PersistenceManager(); |
| 139 | mNotificationController = new NotificationController(PrintSpoolerService.this); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 140 | mCustomIconCache = new CustomPrinterIconCache(getCacheDir()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 141 | |
| 142 | synchronized (mLock) { |
| 143 | mPersistanceManager.readStateLocked(); |
| 144 | handleReadPrintJobsLocked(); |
| 145 | } |
| 146 | |
| 147 | synchronized (sLock) { |
| 148 | sInstance = this; |
| 149 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 153 | public void onDestroy() { |
| 154 | super.onDestroy(); |
| 155 | } |
| 156 | |
| 157 | @Override |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 158 | public IBinder onBind(Intent intent) { |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 159 | return new PrintSpooler(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 162 | private void dumpLocked(PrintWriter pw, String[] args) { |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 163 | String prefix = (args.length > 0) ? args[0] : ""; |
| 164 | String tab = " "; |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 165 | |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 166 | pw.append(prefix).append("print jobs:").println(); |
| 167 | final int printJobCount = mPrintJobs.size(); |
| 168 | for (int i = 0; i < printJobCount; i++) { |
| 169 | PrintJobInfo printJob = mPrintJobs.get(i); |
| 170 | pw.append(prefix).append(tab).append(printJob.toString()); |
| 171 | pw.println(); |
| 172 | } |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 173 | |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 174 | pw.append(prefix).append("print job files:").println(); |
| 175 | File[] files = getFilesDir().listFiles(); |
| 176 | if (files != null) { |
| 177 | final int fileCount = files.length; |
| 178 | for (int i = 0; i < fileCount; i++) { |
| 179 | File file = files[i]; |
| 180 | if (file.isFile() && file.getName().startsWith(PRINT_JOB_FILE_PREFIX)) { |
| 181 | pw.append(prefix).append(tab).append(file.getName()).println(); |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 185 | |
| 186 | pw.append(prefix).append("approved print services:").println(); |
| 187 | Set<String> approvedPrintServices = (new ApprovedPrintServices(this)).getApprovedServices(); |
| 188 | if (approvedPrintServices != null) { |
| 189 | for (String approvedService : approvedPrintServices) { |
| 190 | pw.append(prefix).append(tab).append(approvedService).println(); |
| 191 | } |
| 192 | } |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 195 | private void dumpLocked(@NonNull ProtoOutputStream proto) { |
| 196 | int numPrintJobs = mPrintJobs.size(); |
| 197 | for (int i = 0; i < numPrintJobs; i++) { |
| 198 | writePrintJobInfo(this, proto, PrintSpoolerInternalStateProto.PRINT_JOBS, |
| 199 | mPrintJobs.get(i)); |
| 200 | } |
| 201 | |
| 202 | File[] files = getFilesDir().listFiles(); |
| 203 | if (files != null) { |
| 204 | for (int i = 0; i < files.length; i++) { |
| 205 | File file = files[i]; |
| 206 | if (file.isFile() && file.getName().startsWith(PRINT_JOB_FILE_PREFIX)) { |
| 207 | proto.write(PrintSpoolerInternalStateProto.PRINT_JOB_FILES, file.getName()); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | Set<String> approvedPrintServices = (new ApprovedPrintServices(this)).getApprovedServices(); |
| 213 | if (approvedPrintServices != null) { |
| 214 | for (String approvedService : approvedPrintServices) { |
| 215 | ComponentName componentName = ComponentName.unflattenFromString(approvedService); |
| 216 | if (componentName != null) { |
| 217 | writeComponentName(proto, PrintSpoolerInternalStateProto.APPROVED_SERVICES, |
| 218 | componentName); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | proto.flush(); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 228 | fd = Preconditions.checkNotNull(fd); |
| 229 | |
| 230 | int opti = 0; |
| 231 | boolean dumpAsProto = false; |
| 232 | while (opti < args.length) { |
| 233 | String opt = args[opti]; |
| 234 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 235 | break; |
| 236 | } |
| 237 | opti++; |
| 238 | if ("--proto".equals(opt)) { |
| 239 | dumpAsProto = true; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | final long identity = Binder.clearCallingIdentity(); |
| 244 | try { |
| 245 | synchronized (mLock) { |
| 246 | if (dumpAsProto) { |
| 247 | dumpLocked(new ProtoOutputStream(fd)); |
| 248 | } else { |
| 249 | dumpLocked(pw, args); |
| 250 | } |
| 251 | } |
| 252 | } finally { |
| 253 | Binder.restoreCallingIdentity(identity); |
| 254 | } |
| 255 | } |
| 256 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 257 | private void sendOnPrintJobQueued(PrintJobInfo printJob) { |
| 258 | Message message = mHandlerCaller.obtainMessageO( |
| 259 | HandlerCallerCallback.MSG_ON_PRINT_JOB_QUEUED, printJob); |
| 260 | mHandlerCaller.executeOrSendMessage(message); |
| 261 | } |
| 262 | |
| 263 | private void sendOnAllPrintJobsForServiceHandled(ComponentName service) { |
| 264 | Message message = mHandlerCaller.obtainMessageO( |
| 265 | HandlerCallerCallback.MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED, service); |
| 266 | mHandlerCaller.executeOrSendMessage(message); |
| 267 | } |
| 268 | |
| 269 | private void sendOnAllPrintJobsHandled() { |
| 270 | Message message = mHandlerCaller.obtainMessage( |
| 271 | HandlerCallerCallback.MSG_ON_ALL_PRINT_JOBS_HANDLED); |
| 272 | mHandlerCaller.executeOrSendMessage(message); |
| 273 | } |
| 274 | |
| 275 | private final class HandlerCallerCallback implements HandlerCaller.Callback { |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 276 | public static final int MSG_SET_CLIENT = 1; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 277 | public static final int MSG_ON_PRINT_JOB_QUEUED = 2; |
| 278 | public static final int MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED = 3; |
| 279 | public static final int MSG_ON_ALL_PRINT_JOBS_HANDLED = 4; |
| 280 | public static final int MSG_CHECK_ALL_PRINTJOBS_HANDLED = 5; |
| 281 | public static final int MSG_ON_PRINT_JOB_STATE_CHANGED = 6; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 282 | |
| 283 | @Override |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 284 | public void executeMessage(Message message) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 285 | switch (message.what) { |
| 286 | case MSG_SET_CLIENT: { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 287 | synchronized (mLock) { |
| 288 | mClient = (IPrintSpoolerClient) message.obj; |
| 289 | if (mClient != null) { |
| 290 | Message msg = mHandlerCaller.obtainMessage( |
| 291 | HandlerCallerCallback.MSG_CHECK_ALL_PRINTJOBS_HANDLED); |
| 292 | mHandlerCaller.sendMessageDelayed(msg, |
| 293 | CHECK_ALL_PRINTJOBS_HANDLED_DELAY); |
| 294 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 295 | } |
| 296 | } break; |
| 297 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 298 | case MSG_ON_PRINT_JOB_QUEUED: { |
| 299 | PrintJobInfo printJob = (PrintJobInfo) message.obj; |
| 300 | if (mClient != null) { |
| 301 | try { |
| 302 | mClient.onPrintJobQueued(printJob); |
| 303 | } catch (RemoteException re) { |
| 304 | Slog.e(LOG_TAG, "Error notify for a queued print job.", re); |
| 305 | } |
| 306 | } |
| 307 | } break; |
| 308 | |
| 309 | case MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED: { |
| 310 | ComponentName service = (ComponentName) message.obj; |
| 311 | if (mClient != null) { |
| 312 | try { |
| 313 | mClient.onAllPrintJobsForServiceHandled(service); |
| 314 | } catch (RemoteException re) { |
| 315 | Slog.e(LOG_TAG, "Error notify for all print jobs per service" |
| 316 | + " handled.", re); |
| 317 | } |
| 318 | } |
| 319 | } break; |
| 320 | |
| 321 | case MSG_ON_ALL_PRINT_JOBS_HANDLED: { |
| 322 | if (mClient != null) { |
| 323 | try { |
| 324 | mClient.onAllPrintJobsHandled(); |
| 325 | } catch (RemoteException re) { |
| 326 | Slog.e(LOG_TAG, "Error notify for all print job handled.", re); |
| 327 | } |
| 328 | } |
| 329 | } break; |
| 330 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 331 | case MSG_CHECK_ALL_PRINTJOBS_HANDLED: { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 332 | checkAllPrintJobsHandled(); |
| 333 | } break; |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 334 | |
| 335 | case MSG_ON_PRINT_JOB_STATE_CHANGED: { |
| 336 | if (mClient != null) { |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 337 | PrintJobInfo printJob = (PrintJobInfo) message.obj; |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 338 | try { |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 339 | mClient.onPrintJobStateChanged(printJob); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 340 | } catch (RemoteException re) { |
| 341 | Slog.e(LOG_TAG, "Error notify for print job state change.", re); |
| 342 | } |
| 343 | } |
| 344 | } break; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 348 | |
| 349 | public List<PrintJobInfo> getPrintJobInfos(ComponentName componentName, |
| 350 | int state, int appId) { |
| 351 | List<PrintJobInfo> foundPrintJobs = null; |
| 352 | synchronized (mLock) { |
| 353 | final int printJobCount = mPrintJobs.size(); |
| 354 | for (int i = 0; i < printJobCount; i++) { |
| 355 | PrintJobInfo printJob = mPrintJobs.get(i); |
| 356 | PrinterId printerId = printJob.getPrinterId(); |
| 357 | final boolean sameComponent = (componentName == null |
| 358 | || (printerId != null |
| 359 | && componentName.equals(printerId.getServiceName()))); |
| 360 | final boolean sameAppId = appId == PrintManager.APP_ID_ANY |
| 361 | || printJob.getAppId() == appId; |
| 362 | final boolean sameState = (state == printJob.getState()) |
| 363 | || (state == PrintJobInfo.STATE_ANY) |
| 364 | || (state == PrintJobInfo.STATE_ANY_VISIBLE_TO_CLIENTS |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 365 | && isStateVisibleToUser(printJob.getState())) |
| 366 | || (state == PrintJobInfo.STATE_ANY_ACTIVE |
Svetoslav Ganov | 9b6d3a1 | 2013-10-12 12:35:41 -0700 | [diff] [blame] | 367 | && isActiveState(printJob.getState())) |
| 368 | || (state == PrintJobInfo.STATE_ANY_SCHEDULED |
| 369 | && isScheduledState(printJob.getState())); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 370 | if (sameComponent && sameAppId && sameState) { |
| 371 | if (foundPrintJobs == null) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 372 | foundPrintJobs = new ArrayList<>(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 373 | } |
| 374 | foundPrintJobs.add(printJob); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | return foundPrintJobs; |
| 379 | } |
| 380 | |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 381 | private boolean isStateVisibleToUser(int state) { |
| 382 | return (isActiveState(state) && (state == PrintJobInfo.STATE_FAILED |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 383 | || state == PrintJobInfo.STATE_COMPLETED || state == PrintJobInfo.STATE_CANCELED |
| 384 | || state == PrintJobInfo.STATE_BLOCKED)); |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 387 | public PrintJobInfo getPrintJobInfo(PrintJobId printJobId, int appId) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 388 | synchronized (mLock) { |
| 389 | final int printJobCount = mPrintJobs.size(); |
| 390 | for (int i = 0; i < printJobCount; i++) { |
| 391 | PrintJobInfo printJob = mPrintJobs.get(i); |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 392 | if (printJob.getId().equals(printJobId) |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 393 | && (appId == PrintManager.APP_ID_ANY |
| 394 | || appId == printJob.getAppId())) { |
| 395 | return printJob; |
| 396 | } |
| 397 | } |
| 398 | return null; |
| 399 | } |
| 400 | } |
| 401 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 402 | public void createPrintJob(PrintJobInfo printJob) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 403 | synchronized (mLock) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 404 | addPrintJobLocked(printJob); |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 405 | setPrintJobState(printJob.getId(), PrintJobInfo.STATE_CREATED, null); |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 406 | |
| 407 | Message message = mHandlerCaller.obtainMessageO( |
| 408 | HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED, |
| 409 | printJob); |
| 410 | mHandlerCaller.executeOrSendMessage(message); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | private void handleReadPrintJobsLocked() { |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 415 | // Make a map with the files for a print job since we may have |
| 416 | // to delete some. One example of getting orphan files if the |
| 417 | // spooler crashes while constructing a print job. We do not |
| 418 | // persist partially populated print jobs under construction to |
| 419 | // avoid special handling for various attributes missing. |
| 420 | ArrayMap<PrintJobId, File> fileForJobMap = null; |
| 421 | File[] files = getFilesDir().listFiles(); |
| 422 | if (files != null) { |
| 423 | final int fileCount = files.length; |
| 424 | for (int i = 0; i < fileCount; i++) { |
| 425 | File file = files[i]; |
| 426 | if (file.isFile() && file.getName().startsWith(PRINT_JOB_FILE_PREFIX)) { |
| 427 | if (fileForJobMap == null) { |
| 428 | fileForJobMap = new ArrayMap<PrintJobId, File>(); |
| 429 | } |
Svetoslav Ganov | 2b40c83 | 2013-10-04 08:49:03 -0700 | [diff] [blame] | 430 | String printJobIdString = file.getName().substring( |
| 431 | PRINT_JOB_FILE_PREFIX.length(), |
| 432 | file.getName().indexOf('.')); |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 433 | PrintJobId printJobId = PrintJobId.unflattenFromString( |
| 434 | printJobIdString); |
| 435 | fileForJobMap.put(printJobId, file); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 440 | final int printJobCount = mPrintJobs.size(); |
| 441 | for (int i = 0; i < printJobCount; i++) { |
| 442 | PrintJobInfo printJob = mPrintJobs.get(i); |
| 443 | |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 444 | // We want to have only the orphan files at the end. |
| 445 | if (fileForJobMap != null) { |
| 446 | fileForJobMap.remove(printJob.getId()); |
| 447 | } |
| 448 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 449 | switch (printJob.getState()) { |
| 450 | case PrintJobInfo.STATE_QUEUED: |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 451 | case PrintJobInfo.STATE_STARTED: |
| 452 | case PrintJobInfo.STATE_BLOCKED: { |
| 453 | // We have a print job that was queued or started or blocked in |
| 454 | // the past but the device battery died or a crash occurred. In |
| 455 | // this case we assume the print job failed and let the user |
| 456 | // decide whether to restart the job or just cancel it. |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 457 | setPrintJobState(printJob.getId(), PrintJobInfo.STATE_FAILED, |
| 458 | getString(R.string.no_connection_to_printer)); |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 459 | } break; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 462 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 463 | if (!mPrintJobs.isEmpty()) { |
| 464 | // Update the notification. |
| 465 | mNotificationController.onUpdateNotifications(mPrintJobs); |
| 466 | } |
| 467 | |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 468 | // Delete the orphan files. |
| 469 | if (fileForJobMap != null) { |
| 470 | final int orphanFileCount = fileForJobMap.size(); |
| 471 | for (int i = 0; i < orphanFileCount; i++) { |
| 472 | File file = fileForJobMap.valueAt(i); |
| 473 | file.delete(); |
| 474 | } |
| 475 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | public void checkAllPrintJobsHandled() { |
| 479 | synchronized (mLock) { |
| 480 | if (!hasActivePrintJobsLocked()) { |
| 481 | notifyOnAllPrintJobsHandled(); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 486 | public void writePrintJobData(final ParcelFileDescriptor fd, final PrintJobId printJobId) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 487 | final PrintJobInfo printJob; |
| 488 | synchronized (mLock) { |
| 489 | printJob = getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY); |
| 490 | } |
| 491 | new AsyncTask<Void, Void, Void>() { |
| 492 | @Override |
| 493 | protected Void doInBackground(Void... params) { |
| 494 | FileInputStream in = null; |
| 495 | FileOutputStream out = null; |
| 496 | try { |
| 497 | if (printJob != null) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 498 | File file = generateFileForPrintJob(PrintSpoolerService.this, printJobId); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 499 | in = new FileInputStream(file); |
| 500 | out = new FileOutputStream(fd.getFileDescriptor()); |
| 501 | } |
| 502 | final byte[] buffer = new byte[8192]; |
| 503 | while (true) { |
| 504 | final int readByteCount = in.read(buffer); |
| 505 | if (readByteCount < 0) { |
| 506 | return null; |
| 507 | } |
| 508 | out.write(buffer, 0, readByteCount); |
| 509 | } |
| 510 | } catch (FileNotFoundException fnfe) { |
| 511 | Log.e(LOG_TAG, "Error writing print job data!", fnfe); |
| 512 | } catch (IOException ioe) { |
| 513 | Log.e(LOG_TAG, "Error writing print job data!", ioe); |
| 514 | } finally { |
| 515 | IoUtils.closeQuietly(in); |
| 516 | IoUtils.closeQuietly(out); |
| 517 | IoUtils.closeQuietly(fd); |
| 518 | } |
| 519 | Log.i(LOG_TAG, "[END WRITE]"); |
| 520 | return null; |
| 521 | } |
| 522 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); |
| 523 | } |
| 524 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 525 | public static File generateFileForPrintJob(Context context, PrintJobId printJobId) { |
| 526 | return new File(context.getFilesDir(), PRINT_JOB_FILE_PREFIX |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 527 | + printJobId.flattenToString() + "." + PRINT_FILE_EXTENSION); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | private void addPrintJobLocked(PrintJobInfo printJob) { |
| 531 | mPrintJobs.add(printJob); |
| 532 | if (DEBUG_PRINT_JOB_LIFECYCLE) { |
| 533 | Slog.i(LOG_TAG, "[ADD] " + printJob); |
| 534 | } |
| 535 | } |
| 536 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 537 | private void removeObsoletePrintJobs() { |
| 538 | synchronized (mLock) { |
Svetoslav | 885810d | 2013-11-14 17:59:14 -0800 | [diff] [blame] | 539 | boolean persistState = false; |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 540 | final int printJobCount = mPrintJobs.size(); |
| 541 | for (int i = printJobCount - 1; i >= 0; i--) { |
| 542 | PrintJobInfo printJob = mPrintJobs.get(i); |
| 543 | if (isObsoleteState(printJob.getState())) { |
| 544 | mPrintJobs.remove(i); |
| 545 | if (DEBUG_PRINT_JOB_LIFECYCLE) { |
| 546 | Slog.i(LOG_TAG, "[REMOVE] " + printJob.getId().flattenToString()); |
| 547 | } |
| 548 | removePrintJobFileLocked(printJob.getId()); |
Svetoslav | 885810d | 2013-11-14 17:59:14 -0800 | [diff] [blame] | 549 | persistState = true; |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
Svetoslav | 885810d | 2013-11-14 17:59:14 -0800 | [diff] [blame] | 552 | if (persistState) { |
| 553 | mPersistanceManager.writeStateLocked(); |
| 554 | } |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | private void removePrintJobFileLocked(PrintJobId printJobId) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 559 | File file = generateFileForPrintJob(PrintSpoolerService.this, printJobId); |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 560 | if (file.exists()) { |
| 561 | file.delete(); |
| 562 | if (DEBUG_PRINT_JOB_LIFECYCLE) { |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 563 | Slog.i(LOG_TAG, "[REMOVE FILE FOR] " + printJobId); |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Philip P. Moltmann | 4bd8fac | 2016-04-21 15:17:58 -0700 | [diff] [blame] | 568 | /** |
| 569 | * Notify all interested parties that a print job has been updated. |
| 570 | * |
| 571 | * @param printJob The updated print job. |
| 572 | */ |
| 573 | private void notifyPrintJobUpdated(PrintJobInfo printJob) { |
| 574 | Message message = mHandlerCaller.obtainMessageO( |
| 575 | HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED, |
| 576 | printJob); |
| 577 | mHandlerCaller.executeOrSendMessage(message); |
| 578 | |
| 579 | mNotificationController.onUpdateNotifications(mPrintJobs); |
| 580 | } |
| 581 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 582 | public boolean setPrintJobState(PrintJobId printJobId, int state, String error) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 583 | boolean success = false; |
| 584 | |
| 585 | synchronized (mLock) { |
| 586 | PrintJobInfo printJob = getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY); |
| 587 | if (printJob != null) { |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 588 | final int oldState = printJob.getState(); |
| 589 | if (oldState == state) { |
| 590 | return false; |
| 591 | } |
| 592 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 593 | success = true; |
| 594 | |
| 595 | printJob.setState(state); |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 596 | printJob.setStatus(error); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 597 | printJob.setCancelling(false); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 598 | |
| 599 | if (DEBUG_PRINT_JOB_LIFECYCLE) { |
| 600 | Slog.i(LOG_TAG, "[STATE CHANGED] " + printJob); |
| 601 | } |
| 602 | |
Philip P. Moltmann | 2e30126 | 2016-06-16 12:39:54 -0700 | [diff] [blame] | 603 | MetricsLogger.histogram(this, PRINT_JOB_STATE_HISTO, state); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 604 | switch (state) { |
| 605 | case PrintJobInfo.STATE_COMPLETED: |
| 606 | case PrintJobInfo.STATE_CANCELED: |
Svetoslav Ganov | dd68da2 | 2013-09-27 10:48:31 -0700 | [diff] [blame] | 607 | mPrintJobs.remove(printJob); |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 608 | removePrintJobFileLocked(printJob.getId()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 609 | // $fall-through$ |
| 610 | |
| 611 | case PrintJobInfo.STATE_FAILED: { |
| 612 | PrinterId printerId = printJob.getPrinterId(); |
| 613 | if (printerId != null) { |
| 614 | ComponentName service = printerId.getServiceName(); |
| 615 | if (!hasActivePrintJobsForServiceLocked(service)) { |
| 616 | sendOnAllPrintJobsForServiceHandled(service); |
| 617 | } |
| 618 | } |
| 619 | } break; |
| 620 | |
| 621 | case PrintJobInfo.STATE_QUEUED: { |
| 622 | sendOnPrintJobQueued(new PrintJobInfo(printJob)); |
| 623 | } break; |
| 624 | } |
| 625 | |
| 626 | if (shouldPersistPrintJob(printJob)) { |
| 627 | mPersistanceManager.writeStateLocked(); |
| 628 | } |
| 629 | |
| 630 | if (!hasActivePrintJobsLocked()) { |
| 631 | notifyOnAllPrintJobsHandled(); |
| 632 | } |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 633 | |
Philip P. Moltmann | 4bd8fac | 2016-04-21 15:17:58 -0700 | [diff] [blame] | 634 | notifyPrintJobUpdated(printJob); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | return success; |
| 639 | } |
| 640 | |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 641 | /** |
| 642 | * Set the progress for a print job. |
| 643 | * |
| 644 | * @param printJobId ID of the print job to update |
| 645 | * @param progress the new progress |
| 646 | */ |
| 647 | public void setProgress(@NonNull PrintJobId printJobId, |
| 648 | @FloatRange(from=0.0, to=1.0) float progress) { |
| 649 | synchronized (mLock) { |
| 650 | getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY).setProgress(progress); |
| 651 | |
| 652 | mNotificationController.onUpdateNotifications(mPrintJobs); |
| 653 | } |
| 654 | } |
| 655 | |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 656 | /** |
| 657 | * Set the status for a print job. |
| 658 | * |
| 659 | * @param printJobId ID of the print job to update |
| 660 | * @param status the new status |
| 661 | */ |
| 662 | public void setStatus(@NonNull PrintJobId printJobId, @Nullable CharSequence status) { |
| 663 | synchronized (mLock) { |
Philip P. Moltmann | 4bd8fac | 2016-04-21 15:17:58 -0700 | [diff] [blame] | 664 | PrintJobInfo printJob = getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY); |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 665 | |
Philip P. Moltmann | 4bd8fac | 2016-04-21 15:17:58 -0700 | [diff] [blame] | 666 | if (printJob != null) { |
| 667 | printJob.setStatus(status); |
| 668 | notifyPrintJobUpdated(printJob); |
| 669 | } |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Set the status for a print job. |
| 675 | * |
| 676 | * @param printJobId ID of the print job to update |
| 677 | * @param status the new status as a string resource |
| 678 | * @param appPackageName app package the resource belongs to |
| 679 | */ |
| 680 | public void setStatus(@NonNull PrintJobId printJobId, @StringRes int status, |
| 681 | @Nullable CharSequence appPackageName) { |
| 682 | synchronized (mLock) { |
Philip P. Moltmann | 4bd8fac | 2016-04-21 15:17:58 -0700 | [diff] [blame] | 683 | PrintJobInfo printJob = getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY); |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 684 | |
Philip P. Moltmann | 4bd8fac | 2016-04-21 15:17:58 -0700 | [diff] [blame] | 685 | if (printJob != null) { |
| 686 | printJob.setStatus(status, appPackageName); |
| 687 | notifyPrintJobUpdated(printJob); |
| 688 | } |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 691 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 692 | public boolean hasActivePrintJobsLocked() { |
| 693 | final int printJobCount = mPrintJobs.size(); |
| 694 | for (int i = 0; i < printJobCount; i++) { |
| 695 | PrintJobInfo printJob = mPrintJobs.get(i); |
| 696 | if (isActiveState(printJob.getState())) { |
| 697 | return true; |
| 698 | } |
| 699 | } |
| 700 | return false; |
| 701 | } |
| 702 | |
| 703 | public boolean hasActivePrintJobsForServiceLocked(ComponentName service) { |
| 704 | final int printJobCount = mPrintJobs.size(); |
| 705 | for (int i = 0; i < printJobCount; i++) { |
| 706 | PrintJobInfo printJob = mPrintJobs.get(i); |
Svetoslav | 75d2850 | 2013-11-04 18:25:05 -0800 | [diff] [blame] | 707 | if (isActiveState(printJob.getState()) && printJob.getPrinterId() != null |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 708 | && printJob.getPrinterId().getServiceName().equals(service)) { |
| 709 | return true; |
| 710 | } |
| 711 | } |
| 712 | return false; |
| 713 | } |
| 714 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 715 | private boolean isObsoleteState(int printJobState) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 716 | return (isTerminalState(printJobState) |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 717 | || printJobState == PrintJobInfo.STATE_QUEUED); |
| 718 | } |
| 719 | |
Svetoslav Ganov | 9b6d3a1 | 2013-10-12 12:35:41 -0700 | [diff] [blame] | 720 | private boolean isScheduledState(int printJobState) { |
| 721 | return printJobState == PrintJobInfo.STATE_QUEUED |
| 722 | || printJobState == PrintJobInfo.STATE_STARTED |
| 723 | || printJobState == PrintJobInfo.STATE_BLOCKED; |
| 724 | } |
| 725 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 726 | private boolean isActiveState(int printJobState) { |
| 727 | return printJobState == PrintJobInfo.STATE_CREATED |
| 728 | || printJobState == PrintJobInfo.STATE_QUEUED |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 729 | || printJobState == PrintJobInfo.STATE_STARTED |
| 730 | || printJobState == PrintJobInfo.STATE_BLOCKED; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 733 | private boolean isTerminalState(int printJobState) { |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 734 | return printJobState == PrintJobInfo.STATE_COMPLETED |
| 735 | || printJobState == PrintJobInfo.STATE_CANCELED; |
| 736 | } |
| 737 | |
| 738 | public boolean setPrintJobTag(PrintJobId printJobId, String tag) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 739 | synchronized (mLock) { |
| 740 | PrintJobInfo printJob = getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY); |
| 741 | if (printJob != null) { |
| 742 | String printJobTag = printJob.getTag(); |
| 743 | if (printJobTag == null) { |
| 744 | if (tag == null) { |
| 745 | return false; |
| 746 | } |
| 747 | } else if (printJobTag.equals(tag)) { |
| 748 | return false; |
| 749 | } |
| 750 | printJob.setTag(tag); |
| 751 | if (shouldPersistPrintJob(printJob)) { |
| 752 | mPersistanceManager.writeStateLocked(); |
| 753 | } |
| 754 | return true; |
| 755 | } |
| 756 | } |
| 757 | return false; |
| 758 | } |
| 759 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 760 | public void setPrintJobCancelling(PrintJobId printJobId, boolean cancelling) { |
| 761 | synchronized (mLock) { |
| 762 | PrintJobInfo printJob = getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY); |
| 763 | if (printJob != null) { |
| 764 | printJob.setCancelling(cancelling); |
| 765 | if (shouldPersistPrintJob(printJob)) { |
| 766 | mPersistanceManager.writeStateLocked(); |
| 767 | } |
| 768 | mNotificationController.onUpdateNotifications(mPrintJobs); |
| 769 | |
| 770 | Message message = mHandlerCaller.obtainMessageO( |
| 771 | HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED, |
| 772 | printJob); |
| 773 | mHandlerCaller.executeOrSendMessage(message); |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 778 | public void updatePrintJobUserConfigurableOptionsNoPersistence(PrintJobInfo printJob) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 779 | synchronized (mLock) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 780 | final int printJobCount = mPrintJobs.size(); |
| 781 | for (int i = 0; i < printJobCount; i++) { |
| 782 | PrintJobInfo cachedPrintJob = mPrintJobs.get(i); |
| 783 | if (cachedPrintJob.getId().equals(printJob.getId())) { |
| 784 | cachedPrintJob.setPrinterId(printJob.getPrinterId()); |
| 785 | cachedPrintJob.setPrinterName(printJob.getPrinterName()); |
| 786 | cachedPrintJob.setCopies(printJob.getCopies()); |
| 787 | cachedPrintJob.setDocumentInfo(printJob.getDocumentInfo()); |
| 788 | cachedPrintJob.setPages(printJob.getPages()); |
| 789 | cachedPrintJob.setAttributes(printJob.getAttributes()); |
| 790 | cachedPrintJob.setAdvancedOptions(printJob.getAdvancedOptions()); |
| 791 | return; |
| 792 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 793 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 794 | throw new IllegalArgumentException("No print job with id:" + printJob.getId()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | private boolean shouldPersistPrintJob(PrintJobInfo printJob) { |
| 799 | return printJob.getState() >= PrintJobInfo.STATE_QUEUED; |
| 800 | } |
| 801 | |
| 802 | private void notifyOnAllPrintJobsHandled() { |
| 803 | // This has to run on the tread that is persisting the current state |
| 804 | // since this call may result in the system unbinding from the spooler |
| 805 | // and as a result the spooler process may get killed before the write |
| 806 | // completes. |
| 807 | new AsyncTask<Void, Void, Void>() { |
| 808 | @Override |
| 809 | protected Void doInBackground(Void... params) { |
| 810 | sendOnAllPrintJobsHandled(); |
| 811 | return null; |
| 812 | } |
| 813 | }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, (Void[]) null); |
| 814 | } |
| 815 | |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 816 | /** |
| 817 | * Handle that a custom icon for a printer was loaded. |
| 818 | * |
| 819 | * @param printerId the id of the printer the icon belongs to |
| 820 | * @param icon the icon that was loaded |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 821 | * @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 822 | */ |
| 823 | public void onCustomPrinterIconLoaded(PrinterId printerId, Icon icon) { |
| 824 | mCustomIconCache.onCustomPrinterIconLoaded(printerId, icon); |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Get the custom icon for a printer. If the icon is not cached, the icon is |
| 829 | * requested asynchronously. Once it is available the printer is updated. |
| 830 | * |
| 831 | * @param printerId the id of the printer the icon should be loaded for |
| 832 | * @return the custom icon to be used for the printer or null if the icon is |
| 833 | * not yet available |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 834 | * @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 835 | */ |
| 836 | public Icon getCustomPrinterIcon(PrinterId printerId) { |
| 837 | return mCustomIconCache.getIcon(printerId); |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Clear the custom printer icon cache. |
| 842 | */ |
| 843 | public void clearCustomPrinterIconCache() { |
| 844 | mCustomIconCache.clear(); |
| 845 | } |
| 846 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 847 | private final class PersistenceManager { |
| 848 | private static final String PERSIST_FILE_NAME = "print_spooler_state.xml"; |
| 849 | |
| 850 | private static final String TAG_SPOOLER = "spooler"; |
| 851 | private static final String TAG_JOB = "job"; |
| 852 | |
| 853 | private static final String TAG_PRINTER_ID = "printerId"; |
| 854 | private static final String TAG_PAGE_RANGE = "pageRange"; |
| 855 | private static final String TAG_ATTRIBUTES = "attributes"; |
| 856 | private static final String TAG_DOCUMENT_INFO = "documentInfo"; |
| 857 | |
| 858 | private static final String ATTR_ID = "id"; |
| 859 | private static final String ATTR_LABEL = "label"; |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 860 | private static final String ATTR_LABEL_RES_ID = "labelResId"; |
| 861 | private static final String ATTR_PACKAGE_NAME = "packageName"; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 862 | private static final String ATTR_STATE = "state"; |
| 863 | private static final String ATTR_APP_ID = "appId"; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 864 | private static final String ATTR_TAG = "tag"; |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 865 | private static final String ATTR_CREATION_TIME = "creationTime"; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 866 | private static final String ATTR_COPIES = "copies"; |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 867 | private static final String ATTR_PRINTER_NAME = "printerName"; |
| 868 | private static final String ATTR_STATE_REASON = "stateReason"; |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 869 | private static final String ATTR_STATUS = "status"; |
| 870 | private static final String ATTR_PROGRESS = "progress"; |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 871 | private static final String ATTR_CANCELLING = "cancelling"; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 872 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 873 | private static final String TAG_ADVANCED_OPTIONS = "advancedOptions"; |
| 874 | private static final String TAG_ADVANCED_OPTION = "advancedOption"; |
| 875 | private static final String ATTR_KEY = "key"; |
| 876 | private static final String ATTR_TYPE = "type"; |
| 877 | private static final String ATTR_VALUE = "value"; |
| 878 | private static final String TYPE_STRING = "string"; |
| 879 | private static final String TYPE_INT = "int"; |
| 880 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 881 | private static final String TAG_MEDIA_SIZE = "mediaSize"; |
| 882 | private static final String TAG_RESOLUTION = "resolution"; |
| 883 | private static final String TAG_MARGINS = "margins"; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 884 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 885 | private static final String ATTR_COLOR_MODE = "colorMode"; |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 886 | private static final String ATTR_DUPLEX_MODE = "duplexMode"; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 887 | |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 888 | private static final String ATTR_LOCAL_ID = "localId"; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 889 | private static final String ATTR_SERVICE_NAME = "serviceName"; |
| 890 | |
| 891 | private static final String ATTR_WIDTH_MILS = "widthMils"; |
| 892 | private static final String ATTR_HEIGHT_MILS = "heightMils"; |
| 893 | |
| 894 | private static final String ATTR_HORIZONTAL_DPI = "horizontalDip"; |
| 895 | private static final String ATTR_VERTICAL_DPI = "verticalDpi"; |
| 896 | |
| 897 | private static final String ATTR_LEFT_MILS = "leftMils"; |
| 898 | private static final String ATTR_TOP_MILS = "topMils"; |
| 899 | private static final String ATTR_RIGHT_MILS = "rightMils"; |
| 900 | private static final String ATTR_BOTTOM_MILS = "bottomMils"; |
| 901 | |
| 902 | private static final String ATTR_START = "start"; |
| 903 | private static final String ATTR_END = "end"; |
| 904 | |
| 905 | private static final String ATTR_NAME = "name"; |
| 906 | private static final String ATTR_PAGE_COUNT = "pageCount"; |
| 907 | private static final String ATTR_CONTENT_TYPE = "contentType"; |
Svetoslav Ganov | 7d7888d | 2013-10-12 13:18:12 -0700 | [diff] [blame] | 908 | private static final String ATTR_DATA_SIZE = "dataSize"; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 909 | |
| 910 | private final AtomicFile mStatePersistFile; |
| 911 | |
| 912 | private boolean mWriteStateScheduled; |
| 913 | |
| 914 | private PersistenceManager() { |
| 915 | mStatePersistFile = new AtomicFile(new File(getFilesDir(), |
| 916 | PERSIST_FILE_NAME)); |
| 917 | } |
| 918 | |
| 919 | public void writeStateLocked() { |
Svet Ganov | 4237c92 | 2014-10-24 12:53:23 -0700 | [diff] [blame] | 920 | if (!PERSISTENCE_MANAGER_ENABLED) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 921 | return; |
| 922 | } |
| 923 | if (mWriteStateScheduled) { |
| 924 | return; |
| 925 | } |
| 926 | mWriteStateScheduled = true; |
| 927 | new AsyncTask<Void, Void, Void>() { |
| 928 | @Override |
| 929 | protected Void doInBackground(Void... params) { |
| 930 | synchronized (mLock) { |
| 931 | mWriteStateScheduled = false; |
| 932 | doWriteStateLocked(); |
| 933 | } |
| 934 | return null; |
| 935 | } |
| 936 | }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, (Void[]) null); |
| 937 | } |
| 938 | |
| 939 | private void doWriteStateLocked() { |
| 940 | if (DEBUG_PERSISTENCE) { |
| 941 | Log.i(LOG_TAG, "[PERSIST START]"); |
| 942 | } |
| 943 | FileOutputStream out = null; |
| 944 | try { |
| 945 | out = mStatePersistFile.startWrite(); |
| 946 | |
| 947 | XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 948 | serializer.setOutput(out, StandardCharsets.UTF_8.name()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 949 | serializer.startDocument(null, true); |
| 950 | serializer.startTag(null, TAG_SPOOLER); |
| 951 | |
| 952 | List<PrintJobInfo> printJobs = mPrintJobs; |
| 953 | |
| 954 | final int printJobCount = printJobs.size(); |
| 955 | for (int j = 0; j < printJobCount; j++) { |
| 956 | PrintJobInfo printJob = printJobs.get(j); |
| 957 | |
Svetoslav | 885810d | 2013-11-14 17:59:14 -0800 | [diff] [blame] | 958 | if (!shouldPersistPrintJob(printJob)) { |
| 959 | continue; |
| 960 | } |
| 961 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 962 | serializer.startTag(null, TAG_JOB); |
| 963 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 964 | serializer.attribute(null, ATTR_ID, printJob.getId().flattenToString()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 965 | serializer.attribute(null, ATTR_LABEL, printJob.getLabel().toString()); |
| 966 | serializer.attribute(null, ATTR_STATE, String.valueOf(printJob.getState())); |
| 967 | serializer.attribute(null, ATTR_APP_ID, String.valueOf(printJob.getAppId())); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 968 | String tag = printJob.getTag(); |
| 969 | if (tag != null) { |
| 970 | serializer.attribute(null, ATTR_TAG, tag); |
| 971 | } |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 972 | serializer.attribute(null, ATTR_CREATION_TIME, String.valueOf( |
| 973 | printJob.getCreationTime())); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 974 | serializer.attribute(null, ATTR_COPIES, String.valueOf(printJob.getCopies())); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 975 | String printerName = printJob.getPrinterName(); |
| 976 | if (!TextUtils.isEmpty(printerName)) { |
| 977 | serializer.attribute(null, ATTR_PRINTER_NAME, printerName); |
| 978 | } |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 979 | serializer.attribute(null, ATTR_CANCELLING, String.valueOf( |
| 980 | printJob.isCancelling())); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 981 | |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 982 | float progress = printJob.getProgress(); |
Ian Rogers | 310ddc3 | 2016-05-27 13:50:05 -0700 | [diff] [blame] | 983 | if (!Float.isNaN(progress)) { |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 984 | serializer.attribute(null, ATTR_PROGRESS, String.valueOf(progress)); |
| 985 | } |
| 986 | |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 987 | CharSequence status = printJob.getStatus(getPackageManager()); |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 988 | if (!TextUtils.isEmpty(status)) { |
| 989 | serializer.attribute(null, ATTR_STATUS, status.toString()); |
| 990 | } |
| 991 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 992 | PrinterId printerId = printJob.getPrinterId(); |
| 993 | if (printerId != null) { |
| 994 | serializer.startTag(null, TAG_PRINTER_ID); |
| 995 | serializer.attribute(null, ATTR_LOCAL_ID, printerId.getLocalId()); |
| 996 | serializer.attribute(null, ATTR_SERVICE_NAME, printerId.getServiceName() |
| 997 | .flattenToString()); |
| 998 | serializer.endTag(null, TAG_PRINTER_ID); |
| 999 | } |
| 1000 | |
| 1001 | PageRange[] pages = printJob.getPages(); |
| 1002 | if (pages != null) { |
| 1003 | for (int i = 0; i < pages.length; i++) { |
| 1004 | serializer.startTag(null, TAG_PAGE_RANGE); |
| 1005 | serializer.attribute(null, ATTR_START, String.valueOf( |
| 1006 | pages[i].getStart())); |
| 1007 | serializer.attribute(null, ATTR_END, String.valueOf( |
| 1008 | pages[i].getEnd())); |
| 1009 | serializer.endTag(null, TAG_PAGE_RANGE); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | PrintAttributes attributes = printJob.getAttributes(); |
| 1014 | if (attributes != null) { |
| 1015 | serializer.startTag(null, TAG_ATTRIBUTES); |
| 1016 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1017 | final int colorMode = attributes.getColorMode(); |
| 1018 | serializer.attribute(null, ATTR_COLOR_MODE, |
| 1019 | String.valueOf(colorMode)); |
| 1020 | |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 1021 | final int duplexMode = attributes.getDuplexMode(); |
| 1022 | serializer.attribute(null, ATTR_DUPLEX_MODE, |
| 1023 | String.valueOf(duplexMode)); |
| 1024 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1025 | MediaSize mediaSize = attributes.getMediaSize(); |
| 1026 | if (mediaSize != null) { |
| 1027 | serializer.startTag(null, TAG_MEDIA_SIZE); |
| 1028 | serializer.attribute(null, ATTR_ID, mediaSize.getId()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1029 | serializer.attribute(null, ATTR_WIDTH_MILS, String.valueOf( |
| 1030 | mediaSize.getWidthMils())); |
| 1031 | serializer.attribute(null, ATTR_HEIGHT_MILS, String.valueOf( |
| 1032 | mediaSize.getHeightMils())); |
Svetoslav | a76233a | 2013-09-05 09:38:02 -0700 | [diff] [blame] | 1033 | // We prefer to store only the package name and |
| 1034 | // resource id and fallback to the label. |
| 1035 | if (!TextUtils.isEmpty(mediaSize.mPackageName) |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 1036 | && mediaSize.mLabelResId > 0) { |
| 1037 | serializer.attribute(null, ATTR_PACKAGE_NAME, |
| 1038 | mediaSize.mPackageName); |
| 1039 | serializer.attribute(null, ATTR_LABEL_RES_ID, |
| 1040 | String.valueOf(mediaSize.mLabelResId)); |
| 1041 | } else { |
| 1042 | serializer.attribute(null, ATTR_LABEL, |
| 1043 | mediaSize.getLabel(getPackageManager())); |
| 1044 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1045 | serializer.endTag(null, TAG_MEDIA_SIZE); |
| 1046 | } |
| 1047 | |
| 1048 | Resolution resolution = attributes.getResolution(); |
| 1049 | if (resolution != null) { |
| 1050 | serializer.startTag(null, TAG_RESOLUTION); |
| 1051 | serializer.attribute(null, ATTR_ID, resolution.getId()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1052 | serializer.attribute(null, ATTR_HORIZONTAL_DPI, String.valueOf( |
| 1053 | resolution.getHorizontalDpi())); |
| 1054 | serializer.attribute(null, ATTR_VERTICAL_DPI, String.valueOf( |
| 1055 | resolution.getVerticalDpi())); |
Svetoslav | c606679 | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1056 | serializer.attribute(null, ATTR_LABEL, |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1057 | resolution.getLabel()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1058 | serializer.endTag(null, TAG_RESOLUTION); |
| 1059 | } |
| 1060 | |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1061 | Margins margins = attributes.getMinMargins(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1062 | if (margins != null) { |
| 1063 | serializer.startTag(null, TAG_MARGINS); |
| 1064 | serializer.attribute(null, ATTR_LEFT_MILS, String.valueOf( |
| 1065 | margins.getLeftMils())); |
| 1066 | serializer.attribute(null, ATTR_TOP_MILS, String.valueOf( |
| 1067 | margins.getTopMils())); |
| 1068 | serializer.attribute(null, ATTR_RIGHT_MILS, String.valueOf( |
| 1069 | margins.getRightMils())); |
| 1070 | serializer.attribute(null, ATTR_BOTTOM_MILS, String.valueOf( |
| 1071 | margins.getBottomMils())); |
| 1072 | serializer.endTag(null, TAG_MARGINS); |
| 1073 | } |
| 1074 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1075 | serializer.endTag(null, TAG_ATTRIBUTES); |
| 1076 | } |
| 1077 | |
| 1078 | PrintDocumentInfo documentInfo = printJob.getDocumentInfo(); |
| 1079 | if (documentInfo != null) { |
| 1080 | serializer.startTag(null, TAG_DOCUMENT_INFO); |
| 1081 | serializer.attribute(null, ATTR_NAME, documentInfo.getName()); |
| 1082 | serializer.attribute(null, ATTR_CONTENT_TYPE, String.valueOf( |
| 1083 | documentInfo.getContentType())); |
| 1084 | serializer.attribute(null, ATTR_PAGE_COUNT, String.valueOf( |
| 1085 | documentInfo.getPageCount())); |
Svetoslav Ganov | 7d7888d | 2013-10-12 13:18:12 -0700 | [diff] [blame] | 1086 | serializer.attribute(null, ATTR_DATA_SIZE, String.valueOf( |
| 1087 | documentInfo.getDataSize())); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1088 | serializer.endTag(null, TAG_DOCUMENT_INFO); |
| 1089 | } |
| 1090 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1091 | Bundle advancedOptions = printJob.getAdvancedOptions(); |
| 1092 | if (advancedOptions != null) { |
| 1093 | serializer.startTag(null, TAG_ADVANCED_OPTIONS); |
| 1094 | for (String key : advancedOptions.keySet()) { |
| 1095 | Object value = advancedOptions.get(key); |
| 1096 | if (value instanceof String) { |
| 1097 | String stringValue = (String) value; |
| 1098 | serializer.startTag(null, TAG_ADVANCED_OPTION); |
| 1099 | serializer.attribute(null, ATTR_KEY, key); |
| 1100 | serializer.attribute(null, ATTR_TYPE, TYPE_STRING); |
| 1101 | serializer.attribute(null, ATTR_VALUE, stringValue); |
| 1102 | serializer.endTag(null, TAG_ADVANCED_OPTION); |
| 1103 | } else if (value instanceof Integer) { |
| 1104 | String intValue = Integer.toString((Integer) value); |
| 1105 | serializer.startTag(null, TAG_ADVANCED_OPTION); |
| 1106 | serializer.attribute(null, ATTR_KEY, key); |
| 1107 | serializer.attribute(null, ATTR_TYPE, TYPE_INT); |
| 1108 | serializer.attribute(null, ATTR_VALUE, intValue); |
| 1109 | serializer.endTag(null, TAG_ADVANCED_OPTION); |
| 1110 | } |
| 1111 | } |
| 1112 | serializer.endTag(null, TAG_ADVANCED_OPTIONS); |
| 1113 | } |
| 1114 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1115 | serializer.endTag(null, TAG_JOB); |
| 1116 | |
| 1117 | if (DEBUG_PERSISTENCE) { |
| 1118 | Log.i(LOG_TAG, "[PERSISTED] " + printJob); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | serializer.endTag(null, TAG_SPOOLER); |
| 1123 | serializer.endDocument(); |
| 1124 | mStatePersistFile.finishWrite(out); |
| 1125 | if (DEBUG_PERSISTENCE) { |
| 1126 | Log.i(LOG_TAG, "[PERSIST END]"); |
| 1127 | } |
| 1128 | } catch (IOException e) { |
| 1129 | Slog.w(LOG_TAG, "Failed to write state, restoring backup.", e); |
| 1130 | mStatePersistFile.failWrite(out); |
| 1131 | } finally { |
| 1132 | IoUtils.closeQuietly(out); |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | public void readStateLocked() { |
Svet Ganov | 4237c92 | 2014-10-24 12:53:23 -0700 | [diff] [blame] | 1137 | if (!PERSISTENCE_MANAGER_ENABLED) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1138 | return; |
| 1139 | } |
| 1140 | FileInputStream in = null; |
| 1141 | try { |
| 1142 | in = mStatePersistFile.openRead(); |
| 1143 | } catch (FileNotFoundException e) { |
Joe Onorato | 13460a6 | 2016-03-18 13:34:13 -0700 | [diff] [blame] | 1144 | if (DEBUG_PERSISTENCE) { |
| 1145 | Log.d(LOG_TAG, "No existing print spooler state."); |
| 1146 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1147 | return; |
| 1148 | } |
| 1149 | try { |
| 1150 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1151 | parser.setInput(in, StandardCharsets.UTF_8.name()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1152 | parseState(parser); |
| 1153 | } catch (IllegalStateException ise) { |
| 1154 | Slog.w(LOG_TAG, "Failed parsing ", ise); |
| 1155 | } catch (NullPointerException npe) { |
| 1156 | Slog.w(LOG_TAG, "Failed parsing ", npe); |
| 1157 | } catch (NumberFormatException nfe) { |
| 1158 | Slog.w(LOG_TAG, "Failed parsing ", nfe); |
| 1159 | } catch (XmlPullParserException xppe) { |
| 1160 | Slog.w(LOG_TAG, "Failed parsing ", xppe); |
| 1161 | } catch (IOException ioe) { |
| 1162 | Slog.w(LOG_TAG, "Failed parsing ", ioe); |
| 1163 | } catch (IndexOutOfBoundsException iobe) { |
| 1164 | Slog.w(LOG_TAG, "Failed parsing ", iobe); |
| 1165 | } finally { |
| 1166 | IoUtils.closeQuietly(in); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | private void parseState(XmlPullParser parser) |
| 1171 | throws IOException, XmlPullParserException { |
| 1172 | parser.next(); |
| 1173 | skipEmptyTextTags(parser); |
| 1174 | expect(parser, XmlPullParser.START_TAG, TAG_SPOOLER); |
| 1175 | parser.next(); |
| 1176 | |
| 1177 | while (parsePrintJob(parser)) { |
| 1178 | parser.next(); |
| 1179 | } |
| 1180 | |
| 1181 | skipEmptyTextTags(parser); |
| 1182 | expect(parser, XmlPullParser.END_TAG, TAG_SPOOLER); |
| 1183 | } |
| 1184 | |
| 1185 | private boolean parsePrintJob(XmlPullParser parser) |
| 1186 | throws IOException, XmlPullParserException { |
| 1187 | skipEmptyTextTags(parser); |
| 1188 | if (!accept(parser, XmlPullParser.START_TAG, TAG_JOB)) { |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | PrintJobInfo printJob = new PrintJobInfo(); |
| 1193 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 1194 | PrintJobId printJobId = PrintJobId.unflattenFromString( |
| 1195 | parser.getAttributeValue(null, ATTR_ID)); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1196 | printJob.setId(printJobId); |
| 1197 | String label = parser.getAttributeValue(null, ATTR_LABEL); |
| 1198 | printJob.setLabel(label); |
| 1199 | final int state = Integer.parseInt(parser.getAttributeValue(null, ATTR_STATE)); |
| 1200 | printJob.setState(state); |
| 1201 | final int appId = Integer.parseInt(parser.getAttributeValue(null, ATTR_APP_ID)); |
| 1202 | printJob.setAppId(appId); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1203 | String tag = parser.getAttributeValue(null, ATTR_TAG); |
| 1204 | printJob.setTag(tag); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1205 | String creationTime = parser.getAttributeValue(null, ATTR_CREATION_TIME); |
| 1206 | printJob.setCreationTime(Long.parseLong(creationTime)); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1207 | String copies = parser.getAttributeValue(null, ATTR_COPIES); |
| 1208 | printJob.setCopies(Integer.parseInt(copies)); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1209 | String printerName = parser.getAttributeValue(null, ATTR_PRINTER_NAME); |
| 1210 | printJob.setPrinterName(printerName); |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 1211 | |
| 1212 | String progressString = parser.getAttributeValue(null, ATTR_PROGRESS); |
| 1213 | if (progressString != null) { |
| 1214 | float progress = Float.parseFloat(progressString); |
| 1215 | |
Philip P. Moltmann | a958fc3 | 2015-12-02 15:39:09 -0800 | [diff] [blame] | 1216 | if (progress != -1) { |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 1217 | printJob.setProgress(progress); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | CharSequence status = parser.getAttributeValue(null, ATTR_STATUS); |
| 1222 | printJob.setStatus(status); |
| 1223 | |
| 1224 | // stateReason is deprecated, but might be used by old print jobs |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1225 | String stateReason = parser.getAttributeValue(null, ATTR_STATE_REASON); |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 1226 | if (stateReason != null) { |
| 1227 | printJob.setStatus(stateReason); |
| 1228 | } |
| 1229 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 1230 | String cancelling = parser.getAttributeValue(null, ATTR_CANCELLING); |
| 1231 | printJob.setCancelling(!TextUtils.isEmpty(cancelling) |
| 1232 | ? Boolean.parseBoolean(cancelling) : false); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1233 | |
| 1234 | parser.next(); |
| 1235 | |
| 1236 | skipEmptyTextTags(parser); |
| 1237 | if (accept(parser, XmlPullParser.START_TAG, TAG_PRINTER_ID)) { |
| 1238 | String localId = parser.getAttributeValue(null, ATTR_LOCAL_ID); |
| 1239 | ComponentName service = ComponentName.unflattenFromString(parser.getAttributeValue( |
| 1240 | null, ATTR_SERVICE_NAME)); |
| 1241 | printJob.setPrinterId(new PrinterId(service, localId)); |
| 1242 | parser.next(); |
| 1243 | skipEmptyTextTags(parser); |
| 1244 | expect(parser, XmlPullParser.END_TAG, TAG_PRINTER_ID); |
| 1245 | parser.next(); |
| 1246 | } |
| 1247 | |
| 1248 | skipEmptyTextTags(parser); |
| 1249 | List<PageRange> pageRanges = null; |
| 1250 | while (accept(parser, XmlPullParser.START_TAG, TAG_PAGE_RANGE)) { |
| 1251 | final int start = Integer.parseInt(parser.getAttributeValue(null, ATTR_START)); |
| 1252 | final int end = Integer.parseInt(parser.getAttributeValue(null, ATTR_END)); |
| 1253 | PageRange pageRange = new PageRange(start, end); |
| 1254 | if (pageRanges == null) { |
| 1255 | pageRanges = new ArrayList<PageRange>(); |
| 1256 | } |
| 1257 | pageRanges.add(pageRange); |
| 1258 | parser.next(); |
| 1259 | skipEmptyTextTags(parser); |
| 1260 | expect(parser, XmlPullParser.END_TAG, TAG_PAGE_RANGE); |
| 1261 | parser.next(); |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1262 | skipEmptyTextTags(parser); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1263 | } |
| 1264 | if (pageRanges != null) { |
| 1265 | PageRange[] pageRangesArray = new PageRange[pageRanges.size()]; |
| 1266 | pageRanges.toArray(pageRangesArray); |
| 1267 | printJob.setPages(pageRangesArray); |
| 1268 | } |
| 1269 | |
| 1270 | skipEmptyTextTags(parser); |
| 1271 | if (accept(parser, XmlPullParser.START_TAG, TAG_ATTRIBUTES)) { |
| 1272 | |
| 1273 | PrintAttributes.Builder builder = new PrintAttributes.Builder(); |
| 1274 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1275 | String colorMode = parser.getAttributeValue(null, ATTR_COLOR_MODE); |
| 1276 | builder.setColorMode(Integer.parseInt(colorMode)); |
| 1277 | |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 1278 | String duplexMode = parser.getAttributeValue(null, ATTR_DUPLEX_MODE); |
| 1279 | // Duplex mode was added later, so null check is needed. |
| 1280 | if (duplexMode != null) { |
| 1281 | builder.setDuplexMode(Integer.parseInt(duplexMode)); |
| 1282 | } |
| 1283 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1284 | parser.next(); |
| 1285 | |
| 1286 | skipEmptyTextTags(parser); |
| 1287 | if (accept(parser, XmlPullParser.START_TAG, TAG_MEDIA_SIZE)) { |
| 1288 | String id = parser.getAttributeValue(null, ATTR_ID); |
| 1289 | label = parser.getAttributeValue(null, ATTR_LABEL); |
| 1290 | final int widthMils = Integer.parseInt(parser.getAttributeValue(null, |
| 1291 | ATTR_WIDTH_MILS)); |
| 1292 | final int heightMils = Integer.parseInt(parser.getAttributeValue(null, |
| 1293 | ATTR_HEIGHT_MILS)); |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 1294 | String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME); |
Svetoslav | b206f12 | 2013-09-20 10:43:24 -0700 | [diff] [blame] | 1295 | String labelResIdString = parser.getAttributeValue(null, ATTR_LABEL_RES_ID); |
| 1296 | final int labelResId = (labelResIdString != null) |
| 1297 | ? Integer.parseInt(labelResIdString) : 0; |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 1298 | label = parser.getAttributeValue(null, ATTR_LABEL); |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1299 | MediaSize mediaSize = new MediaSize(id, label, packageName, |
| 1300 | widthMils, heightMils, labelResId); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1301 | builder.setMediaSize(mediaSize); |
| 1302 | parser.next(); |
| 1303 | skipEmptyTextTags(parser); |
| 1304 | expect(parser, XmlPullParser.END_TAG, TAG_MEDIA_SIZE); |
| 1305 | parser.next(); |
| 1306 | } |
| 1307 | |
| 1308 | skipEmptyTextTags(parser); |
| 1309 | if (accept(parser, XmlPullParser.START_TAG, TAG_RESOLUTION)) { |
| 1310 | String id = parser.getAttributeValue(null, ATTR_ID); |
| 1311 | label = parser.getAttributeValue(null, ATTR_LABEL); |
| 1312 | final int horizontalDpi = Integer.parseInt(parser.getAttributeValue(null, |
| 1313 | ATTR_HORIZONTAL_DPI)); |
| 1314 | final int verticalDpi = Integer.parseInt(parser.getAttributeValue(null, |
| 1315 | ATTR_VERTICAL_DPI)); |
Svetoslav | c606679 | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1316 | Resolution resolution = new Resolution(id, label, horizontalDpi, verticalDpi); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1317 | builder.setResolution(resolution); |
| 1318 | parser.next(); |
| 1319 | skipEmptyTextTags(parser); |
| 1320 | expect(parser, XmlPullParser.END_TAG, TAG_RESOLUTION); |
| 1321 | parser.next(); |
| 1322 | } |
| 1323 | |
| 1324 | skipEmptyTextTags(parser); |
| 1325 | if (accept(parser, XmlPullParser.START_TAG, TAG_MARGINS)) { |
| 1326 | final int leftMils = Integer.parseInt(parser.getAttributeValue(null, |
| 1327 | ATTR_LEFT_MILS)); |
| 1328 | final int topMils = Integer.parseInt(parser.getAttributeValue(null, |
| 1329 | ATTR_TOP_MILS)); |
| 1330 | final int rightMils = Integer.parseInt(parser.getAttributeValue(null, |
| 1331 | ATTR_RIGHT_MILS)); |
| 1332 | final int bottomMils = Integer.parseInt(parser.getAttributeValue(null, |
| 1333 | ATTR_BOTTOM_MILS)); |
| 1334 | Margins margins = new Margins(leftMils, topMils, rightMils, bottomMils); |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1335 | builder.setMinMargins(margins); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1336 | parser.next(); |
| 1337 | skipEmptyTextTags(parser); |
| 1338 | expect(parser, XmlPullParser.END_TAG, TAG_MARGINS); |
| 1339 | parser.next(); |
| 1340 | } |
| 1341 | |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1342 | printJob.setAttributes(builder.build()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1343 | |
| 1344 | skipEmptyTextTags(parser); |
| 1345 | expect(parser, XmlPullParser.END_TAG, TAG_ATTRIBUTES); |
| 1346 | parser.next(); |
| 1347 | } |
| 1348 | |
| 1349 | skipEmptyTextTags(parser); |
| 1350 | if (accept(parser, XmlPullParser.START_TAG, TAG_DOCUMENT_INFO)) { |
| 1351 | String name = parser.getAttributeValue(null, ATTR_NAME); |
| 1352 | final int pageCount = Integer.parseInt(parser.getAttributeValue(null, |
| 1353 | ATTR_PAGE_COUNT)); |
| 1354 | final int contentType = Integer.parseInt(parser.getAttributeValue(null, |
| 1355 | ATTR_CONTENT_TYPE)); |
Svetoslav Ganov | 7d7888d | 2013-10-12 13:18:12 -0700 | [diff] [blame] | 1356 | final int dataSize = Integer.parseInt(parser.getAttributeValue(null, |
| 1357 | ATTR_DATA_SIZE)); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1358 | PrintDocumentInfo info = new PrintDocumentInfo.Builder(name) |
| 1359 | .setPageCount(pageCount) |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1360 | .setContentType(contentType).build(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1361 | printJob.setDocumentInfo(info); |
Svetoslav Ganov | 7d7888d | 2013-10-12 13:18:12 -0700 | [diff] [blame] | 1362 | info.setDataSize(dataSize); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1363 | parser.next(); |
| 1364 | skipEmptyTextTags(parser); |
| 1365 | expect(parser, XmlPullParser.END_TAG, TAG_DOCUMENT_INFO); |
| 1366 | parser.next(); |
| 1367 | } |
| 1368 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1369 | skipEmptyTextTags(parser); |
| 1370 | if (accept(parser, XmlPullParser.START_TAG, TAG_ADVANCED_OPTIONS)) { |
| 1371 | parser.next(); |
| 1372 | skipEmptyTextTags(parser); |
| 1373 | Bundle advancedOptions = new Bundle(); |
| 1374 | while (accept(parser, XmlPullParser.START_TAG, TAG_ADVANCED_OPTION)) { |
| 1375 | String key = parser.getAttributeValue(null, ATTR_KEY); |
| 1376 | String value = parser.getAttributeValue(null, ATTR_VALUE); |
| 1377 | String type = parser.getAttributeValue(null, ATTR_TYPE); |
| 1378 | if (TYPE_STRING.equals(type)) { |
| 1379 | advancedOptions.putString(key, value); |
| 1380 | } else if (TYPE_INT.equals(type)) { |
Tobias Thierer | 9bbbf2b | 2016-04-21 12:02:10 +0100 | [diff] [blame] | 1381 | advancedOptions.putInt(key, Integer.parseInt(value)); |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1382 | } |
| 1383 | parser.next(); |
| 1384 | skipEmptyTextTags(parser); |
| 1385 | expect(parser, XmlPullParser.END_TAG, TAG_ADVANCED_OPTION); |
| 1386 | parser.next(); |
| 1387 | skipEmptyTextTags(parser); |
| 1388 | } |
| 1389 | printJob.setAdvancedOptions(advancedOptions); |
| 1390 | skipEmptyTextTags(parser); |
| 1391 | expect(parser, XmlPullParser.END_TAG, TAG_ADVANCED_OPTIONS); |
| 1392 | parser.next(); |
| 1393 | } |
| 1394 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1395 | mPrintJobs.add(printJob); |
| 1396 | |
| 1397 | if (DEBUG_PERSISTENCE) { |
| 1398 | Log.i(LOG_TAG, "[RESTORED] " + printJob); |
| 1399 | } |
| 1400 | |
| 1401 | skipEmptyTextTags(parser); |
| 1402 | expect(parser, XmlPullParser.END_TAG, TAG_JOB); |
| 1403 | |
| 1404 | return true; |
| 1405 | } |
| 1406 | |
| 1407 | private void expect(XmlPullParser parser, int type, String tag) |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 1408 | throws XmlPullParserException { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1409 | if (!accept(parser, type, tag)) { |
| 1410 | throw new XmlPullParserException("Exepected event: " + type |
| 1411 | + " and tag: " + tag + " but got event: " + parser.getEventType() |
| 1412 | + " and tag:" + parser.getName()); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | private void skipEmptyTextTags(XmlPullParser parser) |
| 1417 | throws IOException, XmlPullParserException { |
| 1418 | while (accept(parser, XmlPullParser.TEXT, null) |
| 1419 | && "\n".equals(parser.getText())) { |
| 1420 | parser.next(); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | private boolean accept(XmlPullParser parser, int type, String tag) |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 1425 | throws XmlPullParserException { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1426 | if (parser.getEventType() != type) { |
| 1427 | return false; |
| 1428 | } |
| 1429 | if (tag != null) { |
| 1430 | if (!tag.equals(parser.getName())) { |
| 1431 | return false; |
| 1432 | } |
| 1433 | } else if (parser.getName() != null) { |
| 1434 | return false; |
| 1435 | } |
| 1436 | return true; |
| 1437 | } |
| 1438 | } |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 1439 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1440 | public final class PrintSpooler extends IPrintSpooler.Stub { |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 1441 | @Override |
| 1442 | public void getPrintJobInfos(IPrintSpoolerCallbacks callback, |
| 1443 | ComponentName componentName, int state, int appId, int sequence) |
| 1444 | throws RemoteException { |
| 1445 | List<PrintJobInfo> printJobs = null; |
| 1446 | try { |
| 1447 | printJobs = PrintSpoolerService.this.getPrintJobInfos( |
| 1448 | componentName, state, appId); |
| 1449 | } finally { |
| 1450 | callback.onGetPrintJobInfosResult(printJobs, sequence); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | @Override |
| 1455 | public void getPrintJobInfo(PrintJobId printJobId, IPrintSpoolerCallbacks callback, |
| 1456 | int appId, int sequence) throws RemoteException { |
| 1457 | PrintJobInfo printJob = null; |
| 1458 | try { |
| 1459 | printJob = PrintSpoolerService.this.getPrintJobInfo(printJobId, appId); |
| 1460 | } finally { |
| 1461 | callback.onGetPrintJobInfoResult(printJob, sequence); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | @Override |
| 1466 | public void createPrintJob(PrintJobInfo printJob) { |
| 1467 | PrintSpoolerService.this.createPrintJob(printJob); |
| 1468 | } |
| 1469 | |
| 1470 | @Override |
| 1471 | public void setPrintJobState(PrintJobId printJobId, int state, String error, |
| 1472 | IPrintSpoolerCallbacks callback, int sequece) throws RemoteException { |
| 1473 | boolean success = false; |
| 1474 | try { |
| 1475 | success = PrintSpoolerService.this.setPrintJobState( |
| 1476 | printJobId, state, error); |
| 1477 | } finally { |
| 1478 | callback.onSetPrintJobStateResult(success, sequece); |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | @Override |
| 1483 | public void setPrintJobTag(PrintJobId printJobId, String tag, |
| 1484 | IPrintSpoolerCallbacks callback, int sequece) throws RemoteException { |
| 1485 | boolean success = false; |
| 1486 | try { |
| 1487 | success = PrintSpoolerService.this.setPrintJobTag(printJobId, tag); |
| 1488 | } finally { |
| 1489 | callback.onSetPrintJobTagResult(success, sequece); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | @Override |
| 1494 | public void writePrintJobData(ParcelFileDescriptor fd, PrintJobId printJobId) { |
| 1495 | PrintSpoolerService.this.writePrintJobData(fd, printJobId); |
| 1496 | } |
| 1497 | |
| 1498 | @Override |
| 1499 | public void setClient(IPrintSpoolerClient client) { |
| 1500 | Message message = mHandlerCaller.obtainMessageO( |
| 1501 | HandlerCallerCallback.MSG_SET_CLIENT, client); |
| 1502 | mHandlerCaller.executeOrSendMessage(message); |
| 1503 | } |
| 1504 | |
| 1505 | @Override |
| 1506 | public void removeObsoletePrintJobs() { |
| 1507 | PrintSpoolerService.this.removeObsoletePrintJobs(); |
| 1508 | } |
| 1509 | |
| 1510 | @Override |
| 1511 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1512 | PrintSpoolerService.this.dump(fd, writer, args); |
| 1513 | } |
| 1514 | |
| 1515 | @Override |
| 1516 | public void setPrintJobCancelling(PrintJobId printJobId, boolean cancelling) { |
| 1517 | PrintSpoolerService.this.setPrintJobCancelling(printJobId, cancelling); |
| 1518 | } |
| 1519 | |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 1520 | @Override |
Philip P. Moltmann | 8141bdf | 2015-12-21 17:03:05 -0800 | [diff] [blame] | 1521 | public void pruneApprovedPrintServices(List<ComponentName> servicesToKeep) { |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 1522 | (new ApprovedPrintServices(PrintSpoolerService.this)) |
Philip P. Moltmann | 8141bdf | 2015-12-21 17:03:05 -0800 | [diff] [blame] | 1523 | .pruneApprovedServices(servicesToKeep); |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 1524 | } |
| 1525 | |
Philip P. Moltmann | b3078c2 | 2015-11-23 16:12:39 -0800 | [diff] [blame] | 1526 | @Override |
| 1527 | public void setProgress(@NonNull PrintJobId printJobId, |
| 1528 | @FloatRange(from=0.0, to=1.0) float progress) throws RemoteException { |
| 1529 | PrintSpoolerService.this.setProgress(printJobId, progress); |
| 1530 | } |
| 1531 | |
| 1532 | @Override |
| 1533 | public void setStatus(@NonNull PrintJobId printJobId, |
| 1534 | @Nullable CharSequence status) throws RemoteException { |
| 1535 | PrintSpoolerService.this.setStatus(printJobId, status); |
| 1536 | } |
| 1537 | |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 1538 | @Override |
| 1539 | public void setStatusRes(@NonNull PrintJobId printJobId, @StringRes int status, |
| 1540 | @NonNull CharSequence appPackageName) throws RemoteException { |
| 1541 | PrintSpoolerService.this.setStatus(printJobId, status, appPackageName); |
| 1542 | } |
| 1543 | |
| 1544 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 1545 | public PrintSpoolerService getService() { |
| 1546 | return PrintSpoolerService.this; |
| 1547 | } |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 1548 | |
| 1549 | @Override |
| 1550 | public void onCustomPrinterIconLoaded(PrinterId printerId, Icon icon, |
| 1551 | IPrintSpoolerCallbacks callbacks, int sequence) |
| 1552 | throws RemoteException { |
| 1553 | try { |
| 1554 | PrintSpoolerService.this.onCustomPrinterIconLoaded(printerId, icon); |
| 1555 | } finally { |
| 1556 | callbacks.onCustomPrinterIconCached(sequence); |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | @Override |
| 1561 | public void getCustomPrinterIcon(PrinterId printerId, IPrintSpoolerCallbacks callbacks, |
| 1562 | int sequence) throws RemoteException { |
| 1563 | Icon icon = null; |
| 1564 | try { |
| 1565 | icon = PrintSpoolerService.this.getCustomPrinterIcon(printerId); |
| 1566 | } finally { |
| 1567 | callbacks.onGetCustomPrinterIconResult(icon, sequence); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | @Override |
| 1572 | public void clearCustomPrinterIconCache(IPrintSpoolerCallbacks callbacks, |
| 1573 | int sequence) throws RemoteException { |
| 1574 | try { |
| 1575 | PrintSpoolerService.this.clearCustomPrinterIconCache(); |
| 1576 | } finally { |
| 1577 | callbacks.customPrinterIconCacheCleared(sequence); |
| 1578 | } |
| 1579 | } |
| 1580 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 1581 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 1582 | } |