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 | |
| 17 | package android.print; |
| 18 | |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.Application.ActivityLifecycleCallbacks; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.IntentSender; |
| 23 | import android.content.IntentSender.SendIntentException; |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 25 | import android.os.CancellationSignal; |
| 26 | import android.os.Handler; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 27 | import android.os.ICancellationSignal; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 28 | import android.os.Looper; |
| 29 | import android.os.Message; |
| 30 | import android.os.ParcelFileDescriptor; |
| 31 | import android.os.RemoteException; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 32 | import android.print.PrintDocumentAdapter.LayoutResultCallback; |
| 33 | import android.print.PrintDocumentAdapter.WriteResultCallback; |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 34 | import android.printservice.PrintServiceInfo; |
Svetoslav Ganov | 88d1991 | 2013-07-22 12:32:03 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 36 | import android.util.ArrayMap; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | |
| 39 | import com.android.internal.os.SomeArgs; |
| 40 | |
| 41 | import libcore.io.IoUtils; |
| 42 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 43 | import java.lang.ref.WeakReference; |
| 44 | import java.util.ArrayList; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 45 | import java.util.Arrays; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 46 | import java.util.Collections; |
| 47 | import java.util.List; |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 48 | import java.util.Map; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * System level service for accessing the printing capabilities of the platform. |
| 52 | * <p> |
| 53 | * To obtain a handle to the print manager do the following: |
| 54 | * </p> |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 55 | * |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 56 | * <pre> |
| 57 | * PrintManager printManager = |
| 58 | * (PrintManager) context.getSystemService(Context.PRINT_SERVICE); |
| 59 | * </pre> |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 60 | * |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 61 | * <h3>Print mechanics</h3> |
| 62 | * <p> |
| 63 | * The key idea behind printing on the platform is that the content to be printed |
| 64 | * should be laid out for the currently selected print options resulting in an |
| 65 | * optimized output and higher user satisfaction. To achieve this goal the platform |
| 66 | * declares a contract that the printing application has to follow which is defined |
| 67 | * by the {@link PrintDocumentAdapter} class. At a higher level the contract is that |
| 68 | * when the user selects some options from the print UI that may affect the way |
| 69 | * content is laid out, for example page size, the application receives a callback |
| 70 | * allowing it to layout the content to better fit these new constraints. After a |
| 71 | * layout pass the system may ask the application to render one or more pages one |
| 72 | * or more times. For example, an application may produce a single column list for |
| 73 | * smaller page sizes and a multi-column table for larger page sizes. |
| 74 | * </p> |
| 75 | * <h3>Print jobs</h3> |
| 76 | * <p> |
| 77 | * Print jobs are started by calling the {@link #print(String, PrintDocumentAdapter, |
| 78 | * PrintAttributes)} from an activity which results in bringing up the system print |
| 79 | * UI. Once the print UI is up, when the user changes a selected print option that |
| 80 | * affects the way content is laid out the system starts to interact with the |
| 81 | * application following the mechanics described the section above. |
| 82 | * </p> |
| 83 | * <p> |
| 84 | * Print jobs can be in {@link PrintJobInfo#STATE_CREATED created}, {@link |
| 85 | * PrintJobInfo#STATE_QUEUED queued}, {@link PrintJobInfo#STATE_STARTED started}, |
| 86 | * {@link PrintJobInfo#STATE_BLOCKED blocked}, {@link PrintJobInfo#STATE_COMPLETED |
| 87 | * completed}, {@link PrintJobInfo#STATE_FAILED failed}, and {@link |
| 88 | * PrintJobInfo#STATE_CANCELED canceled} state. Print jobs are stored in dedicated |
| 89 | * system spooler until they are handled which is they are cancelled or completed. |
| 90 | * Active print jobs, ones that are not cancelled or completed, are considered failed |
| 91 | * if the device reboots as the new boot may be after a very long time. The user may |
| 92 | * choose to restart such print jobs. Once a print job is queued all relevant content |
| 93 | * is stored in the system spooler and its lifecycle becomes detached from this of |
| 94 | * the application that created it. |
| 95 | * </p> |
| 96 | * <p> |
| 97 | * An applications can query the print spooler for current print jobs it created |
| 98 | * but not print jobs created by other applications. |
| 99 | * </p> |
| 100 | * |
| 101 | * @see PrintJob |
| 102 | * @see PrintJobInfo |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 103 | */ |
| 104 | public final class PrintManager { |
| 105 | |
| 106 | private static final String LOG_TAG = "PrintManager"; |
| 107 | |
Svetoslav | 15cbc8a | 2014-07-11 09:45:07 -0700 | [diff] [blame] | 108 | private static final boolean DEBUG = false; |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 109 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 110 | private static final int MSG_NOTIFY_PRINT_JOB_STATE_CHANGED = 1; |
| 111 | |
| 112 | /** |
| 113 | * The action for launching the print dialog activity. |
| 114 | * |
| 115 | * @hide |
| 116 | */ |
| 117 | public static final String ACTION_PRINT_DIALOG = "android.print.PRINT_DIALOG"; |
| 118 | |
| 119 | /** |
| 120 | * Extra with the intent for starting the print dialog. |
| 121 | * <p> |
| 122 | * <strong>Type:</strong> {@link android.content.IntentSender} |
| 123 | * </p> |
| 124 | * |
| 125 | * @hide |
| 126 | */ |
| 127 | public static final String EXTRA_PRINT_DIALOG_INTENT = |
| 128 | "android.print.intent.extra.EXTRA_PRINT_DIALOG_INTENT"; |
| 129 | |
| 130 | /** |
| 131 | * Extra with a print job. |
| 132 | * <p> |
| 133 | * <strong>Type:</strong> {@link android.print.PrintJobInfo} |
| 134 | * </p> |
| 135 | * |
| 136 | * @hide |
| 137 | */ |
| 138 | public static final String EXTRA_PRINT_JOB = |
| 139 | "android.print.intent.extra.EXTRA_PRINT_JOB"; |
| 140 | |
| 141 | /** |
| 142 | * Extra with the print document adapter to be printed. |
| 143 | * <p> |
| 144 | * <strong>Type:</strong> {@link android.print.IPrintDocumentAdapter} |
| 145 | * </p> |
| 146 | * |
| 147 | * @hide |
| 148 | */ |
| 149 | public static final String EXTRA_PRINT_DOCUMENT_ADAPTER = |
| 150 | "android.print.intent.extra.EXTRA_PRINT_DOCUMENT_ADAPTER"; |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 151 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 152 | /** @hide */ |
| 153 | public static final int APP_ID_ANY = -2; |
| 154 | |
| 155 | private final Context mContext; |
| 156 | |
| 157 | private final IPrintManager mService; |
| 158 | |
| 159 | private final int mUserId; |
| 160 | |
| 161 | private final int mAppId; |
| 162 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 163 | private final Handler mHandler; |
| 164 | |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 165 | private Map<PrintJobStateChangeListener, PrintJobStateChangeListenerWrapper> mPrintJobStateChangeListeners; |
| 166 | |
| 167 | /** @hide */ |
| 168 | public interface PrintJobStateChangeListener { |
| 169 | |
| 170 | /** |
| 171 | * Callback notifying that a print job state changed. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 172 | * |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 173 | * @param printJobId The print job id. |
| 174 | */ |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 175 | public void onPrintJobStateChanged(PrintJobId printJobId); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 178 | /** |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 179 | * Creates a new instance. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 180 | * |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 181 | * @param context The current context in which to operate. |
| 182 | * @param service The backing system service. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 183 | * @hide |
| 184 | */ |
| 185 | public PrintManager(Context context, IPrintManager service, int userId, int appId) { |
| 186 | mContext = context; |
| 187 | mService = service; |
| 188 | mUserId = userId; |
| 189 | mAppId = appId; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 190 | mHandler = new Handler(context.getMainLooper(), null, false) { |
| 191 | @Override |
| 192 | public void handleMessage(Message message) { |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 193 | switch (message.what) { |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 194 | case MSG_NOTIFY_PRINT_JOB_STATE_CHANGED: { |
| 195 | SomeArgs args = (SomeArgs) message.obj; |
Svetoslav Ganov | d91cb3e | 2013-10-12 15:44:42 -0700 | [diff] [blame] | 196 | PrintJobStateChangeListenerWrapper wrapper = |
| 197 | (PrintJobStateChangeListenerWrapper) args.arg1; |
| 198 | PrintJobStateChangeListener listener = wrapper.getListener(); |
| 199 | if (listener != null) { |
| 200 | PrintJobId printJobId = (PrintJobId) args.arg2; |
| 201 | listener.onPrintJobStateChanged(printJobId); |
| 202 | } |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 203 | args.recycle(); |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 204 | } break; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | }; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Creates an instance that can access all print jobs. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 212 | * |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 213 | * @param userId The user id for which to get all print jobs. |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 214 | * @return An instance if the caller has the permission to access all print |
| 215 | * jobs, null otherwise. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 216 | * @hide |
| 217 | */ |
| 218 | public PrintManager getGlobalPrintManagerForUser(int userId) { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 219 | if (mService == null) { |
| 220 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 221 | return null; |
| 222 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 223 | return new PrintManager(mContext, mService, userId, APP_ID_ANY); |
| 224 | } |
| 225 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 226 | PrintJobInfo getPrintJobInfo(PrintJobId printJobId) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 227 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 228 | return mService.getPrintJobInfo(printJobId, mAppId, mUserId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 229 | } catch (RemoteException re) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 230 | Log.e(LOG_TAG, "Error getting a print job info:" + printJobId, re); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 231 | } |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | /** |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 236 | * Adds a listener for observing the state of print jobs. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 237 | * |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 238 | * @param listener The listener to add. |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 239 | * @hide |
| 240 | */ |
| 241 | public void addPrintJobStateChangeListener(PrintJobStateChangeListener listener) { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 242 | if (mService == null) { |
| 243 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 244 | return; |
| 245 | } |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 246 | if (mPrintJobStateChangeListeners == null) { |
| 247 | mPrintJobStateChangeListeners = new ArrayMap<PrintJobStateChangeListener, |
| 248 | PrintJobStateChangeListenerWrapper>(); |
| 249 | } |
| 250 | PrintJobStateChangeListenerWrapper wrappedListener = |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 251 | new PrintJobStateChangeListenerWrapper(listener, mHandler); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 252 | try { |
| 253 | mService.addPrintJobStateChangeListener(wrappedListener, mAppId, mUserId); |
| 254 | mPrintJobStateChangeListeners.put(listener, wrappedListener); |
| 255 | } catch (RemoteException re) { |
| 256 | Log.e(LOG_TAG, "Error adding print job state change listener", re); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Removes a listener for observing the state of print jobs. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 262 | * |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 263 | * @param listener The listener to remove. |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 264 | * @hide |
| 265 | */ |
| 266 | public void removePrintJobStateChangeListener(PrintJobStateChangeListener listener) { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 267 | if (mService == null) { |
| 268 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 269 | return; |
| 270 | } |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 271 | if (mPrintJobStateChangeListeners == null) { |
| 272 | return; |
| 273 | } |
| 274 | PrintJobStateChangeListenerWrapper wrappedListener = |
| 275 | mPrintJobStateChangeListeners.remove(listener); |
| 276 | if (wrappedListener == null) { |
| 277 | return; |
| 278 | } |
| 279 | if (mPrintJobStateChangeListeners.isEmpty()) { |
| 280 | mPrintJobStateChangeListeners = null; |
| 281 | } |
Svetoslav Ganov | d91cb3e | 2013-10-12 15:44:42 -0700 | [diff] [blame] | 282 | wrappedListener.destroy(); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 283 | try { |
| 284 | mService.removePrintJobStateChangeListener(wrappedListener, mUserId); |
| 285 | } catch (RemoteException re) { |
| 286 | Log.e(LOG_TAG, "Error removing print job state change listener", re); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Gets a print job given its id. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 292 | * |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 293 | * @return The print job list. |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 294 | * @see PrintJob |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 295 | * @hide |
| 296 | */ |
| 297 | public PrintJob getPrintJob(PrintJobId printJobId) { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 298 | if (mService == null) { |
| 299 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 300 | return null; |
| 301 | } |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 302 | try { |
| 303 | PrintJobInfo printJob = mService.getPrintJobInfo(printJobId, mAppId, mUserId); |
| 304 | if (printJob != null) { |
| 305 | return new PrintJob(printJob, this); |
| 306 | } |
| 307 | } catch (RemoteException re) { |
| 308 | Log.e(LOG_TAG, "Error getting print job", re); |
| 309 | } |
| 310 | return null; |
| 311 | } |
| 312 | |
| 313 | /** |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 314 | * Gets the print jobs for this application. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 315 | * |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 316 | * @return The print job list. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 317 | * @see PrintJob |
| 318 | */ |
| 319 | public List<PrintJob> getPrintJobs() { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 320 | if (mService == null) { |
| 321 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 322 | return Collections.emptyList(); |
| 323 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 324 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 325 | List<PrintJobInfo> printJobInfos = mService.getPrintJobInfos(mAppId, mUserId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 326 | if (printJobInfos == null) { |
| 327 | return Collections.emptyList(); |
| 328 | } |
| 329 | final int printJobCount = printJobInfos.size(); |
| 330 | List<PrintJob> printJobs = new ArrayList<PrintJob>(printJobCount); |
| 331 | for (int i = 0; i < printJobCount; i++) { |
| 332 | printJobs.add(new PrintJob(printJobInfos.get(i), this)); |
| 333 | } |
| 334 | return printJobs; |
| 335 | } catch (RemoteException re) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 336 | Log.e(LOG_TAG, "Error getting print jobs", re); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 337 | } |
| 338 | return Collections.emptyList(); |
| 339 | } |
| 340 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 341 | void cancelPrintJob(PrintJobId printJobId) { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 342 | if (mService == null) { |
| 343 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 344 | return; |
| 345 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 346 | try { |
| 347 | mService.cancelPrintJob(printJobId, mAppId, mUserId); |
| 348 | } catch (RemoteException re) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 349 | Log.e(LOG_TAG, "Error canceling a print job: " + printJobId, re); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 350 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 353 | void restartPrintJob(PrintJobId printJobId) { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 354 | if (mService == null) { |
| 355 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 356 | return; |
| 357 | } |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 358 | try { |
| 359 | mService.restartPrintJob(printJobId, mAppId, mUserId); |
| 360 | } catch (RemoteException re) { |
| 361 | Log.e(LOG_TAG, "Error restarting a print job: " + printJobId, re); |
| 362 | } |
| 363 | } |
| 364 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 365 | /** |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 366 | * Creates a print job for printing a {@link PrintDocumentAdapter} with |
| 367 | * default print attributes. |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 368 | * <p> |
| 369 | * Calling this method brings the print UI allowing the user to customize |
| 370 | * the print job and returns a {@link PrintJob} object without waiting for the |
| 371 | * user to customize or confirm the print job. The returned print job instance |
| 372 | * is in a {@link PrintJobInfo#STATE_CREATED created} state. |
| 373 | * <p> |
| 374 | * This method can be called only from an {@link Activity}. The rationale is that |
| 375 | * printing from a service will create an inconsistent user experience as the print |
| 376 | * UI would appear without any context. |
| 377 | * </p> |
| 378 | * <p> |
| 379 | * Also the passed in {@link PrintDocumentAdapter} will be considered invalid if |
| 380 | * your activity is finished. The rationale is that once the activity that |
| 381 | * initiated printing is finished, the provided adapter may be in an inconsistent |
| 382 | * state as it may depend on the UI presented by the activity. |
| 383 | * </p> |
| 384 | * <p> |
| 385 | * The default print attributes are a hint to the system how the data is to |
| 386 | * be printed. For example, a photo editor may look at the photo aspect ratio |
| 387 | * to determine the default orientation and provide a hint whether the printing |
| 388 | * should be in portrait or landscape. The system will do a best effort to |
| 389 | * selected the hinted options in the print dialog, given the current printer |
| 390 | * supports them. |
| 391 | * </p> |
Svetoslav | 81f14b9 | 2014-03-18 10:37:20 -0700 | [diff] [blame] | 392 | * <p> |
| 393 | * <strong>Note:</strong> Calling this method will bring the print dialog and |
| 394 | * the system will connect to the provided {@link PrintDocumentAdapter}. If a |
Svetoslav | 85a85a0 | 2014-03-18 10:48:51 -0700 | [diff] [blame] | 395 | * configuration change occurs that you application does not handle, for example |
| 396 | * a rotation change, the system will drop the connection to the adapter as the |
| 397 | * activity has to be recreated and the old adapter may be invalid in this context, |
| 398 | * hence a new adapter instance is required. As a consequence, if your activity |
| 399 | * does not handle configuration changes (default behavior), you have to save the |
| 400 | * state that you were printing and call this method again when your activity |
| 401 | * is recreated. |
Svetoslav | 81f14b9 | 2014-03-18 10:37:20 -0700 | [diff] [blame] | 402 | * </p> |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 403 | * |
| 404 | * @param printJobName A name for the new print job which is shown to the user. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 405 | * @param documentAdapter An adapter that emits the document to print. |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 406 | * @param attributes The default print job attributes or <code>null</code>. |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 407 | * @return The created print job on success or null on failure. |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 408 | * @throws IllegalStateException If not called from an {@link Activity}. |
| 409 | * @throws IllegalArgumentException If the print job name is empty or the |
| 410 | * document adapter is null. |
| 411 | * |
Svetoslav | fd90651 | 2013-06-24 09:04:48 -0700 | [diff] [blame] | 412 | * @see PrintJob |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 413 | */ |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 414 | public PrintJob print(String printJobName, PrintDocumentAdapter documentAdapter, |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 415 | PrintAttributes attributes) { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 416 | if (mService == null) { |
| 417 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 418 | return null; |
| 419 | } |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 420 | if (!(mContext instanceof Activity)) { |
| 421 | throw new IllegalStateException("Can print only from an activity"); |
| 422 | } |
Svetoslav Ganov | 88d1991 | 2013-07-22 12:32:03 -0700 | [diff] [blame] | 423 | if (TextUtils.isEmpty(printJobName)) { |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 424 | throw new IllegalArgumentException("printJobName cannot be empty"); |
| 425 | } |
| 426 | if (documentAdapter == null) { |
| 427 | throw new IllegalArgumentException("documentAdapter cannot be null"); |
Svetoslav Ganov | 88d1991 | 2013-07-22 12:32:03 -0700 | [diff] [blame] | 428 | } |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 429 | PrintDocumentAdapterDelegate delegate = new PrintDocumentAdapterDelegate( |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 430 | (Activity) mContext, documentAdapter); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 431 | try { |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 432 | Bundle result = mService.print(printJobName, delegate, |
| 433 | attributes, mContext.getPackageName(), mAppId, mUserId); |
| 434 | if (result != null) { |
| 435 | PrintJobInfo printJob = result.getParcelable(EXTRA_PRINT_JOB); |
| 436 | IntentSender intent = result.getParcelable(EXTRA_PRINT_DIALOG_INTENT); |
| 437 | if (printJob == null || intent == null) { |
| 438 | return null; |
| 439 | } |
| 440 | try { |
| 441 | mContext.startIntentSender(intent, null, 0, 0, 0); |
| 442 | return new PrintJob(printJob, this); |
| 443 | } catch (SendIntentException sie) { |
| 444 | Log.e(LOG_TAG, "Couldn't start print job config activity.", sie); |
| 445 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 446 | } |
| 447 | } catch (RemoteException re) { |
| 448 | Log.e(LOG_TAG, "Error creating a print job", re); |
| 449 | } |
| 450 | return null; |
| 451 | } |
| 452 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 453 | /** |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 454 | * Gets the list of enabled print services. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 455 | * |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 456 | * @return The enabled service list or an empty list. |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 457 | * @hide |
| 458 | */ |
| 459 | public List<PrintServiceInfo> getEnabledPrintServices() { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 460 | if (mService == null) { |
| 461 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 462 | return Collections.emptyList(); |
| 463 | } |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 464 | try { |
| 465 | List<PrintServiceInfo> enabledServices = mService.getEnabledPrintServices(mUserId); |
| 466 | if (enabledServices != null) { |
| 467 | return enabledServices; |
| 468 | } |
| 469 | } catch (RemoteException re) { |
Svetoslav | d8dbc13 | 2013-09-27 18:29:53 -0700 | [diff] [blame] | 470 | Log.e(LOG_TAG, "Error getting the enabled print services", re); |
| 471 | } |
| 472 | return Collections.emptyList(); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Gets the list of installed print services. |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 477 | * |
Svetoslav | d8dbc13 | 2013-09-27 18:29:53 -0700 | [diff] [blame] | 478 | * @return The installed service list or an empty list. |
Svetoslav | d8dbc13 | 2013-09-27 18:29:53 -0700 | [diff] [blame] | 479 | * @hide |
| 480 | */ |
| 481 | public List<PrintServiceInfo> getInstalledPrintServices() { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 482 | if (mService == null) { |
| 483 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 484 | return Collections.emptyList(); |
| 485 | } |
Svetoslav | d8dbc13 | 2013-09-27 18:29:53 -0700 | [diff] [blame] | 486 | try { |
| 487 | List<PrintServiceInfo> installedServices = mService.getInstalledPrintServices(mUserId); |
| 488 | if (installedServices != null) { |
| 489 | return installedServices; |
| 490 | } |
| 491 | } catch (RemoteException re) { |
| 492 | Log.e(LOG_TAG, "Error getting the installed print services", re); |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 493 | } |
| 494 | return Collections.emptyList(); |
| 495 | } |
| 496 | |
| 497 | /** |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 498 | * @hide |
| 499 | */ |
| 500 | public PrinterDiscoverySession createPrinterDiscoverySession() { |
Svetoslav | 2235a17 | 2014-05-08 14:19:01 -0700 | [diff] [blame] | 501 | if (mService == null) { |
| 502 | Log.w(LOG_TAG, "Feature android.software.print not available"); |
| 503 | return null; |
| 504 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 505 | return new PrinterDiscoverySession(mService, mContext, mUserId); |
| 506 | } |
| 507 | |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 508 | private static final class PrintDocumentAdapterDelegate extends IPrintDocumentAdapter.Stub |
| 509 | implements ActivityLifecycleCallbacks { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 510 | private final Object mLock = new Object(); |
| 511 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 512 | private Activity mActivity; // Strong reference OK - cleared in destroy |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 513 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 514 | private PrintDocumentAdapter mDocumentAdapter; // Strong reference OK - cleared in destroy |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 515 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 516 | private Handler mHandler; // Strong reference OK - cleared in destroy |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 517 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 518 | private IPrintDocumentAdapterObserver mObserver; // Strong reference OK - cleared in destroy |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 519 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 520 | private DestroyableCallback mPendingCallback; |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 521 | |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 522 | public PrintDocumentAdapterDelegate(Activity activity, |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 523 | PrintDocumentAdapter documentAdapter) { |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 524 | mActivity = activity; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 525 | mDocumentAdapter = documentAdapter; |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 526 | mHandler = new MyHandler(mActivity.getMainLooper()); |
| 527 | mActivity.getApplication().registerActivityLifecycleCallbacks(this); |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public void setObserver(IPrintDocumentAdapterObserver observer) { |
| 532 | final boolean destroyed; |
| 533 | synchronized (mLock) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 534 | mObserver = observer; |
| 535 | destroyed = isDestroyedLocked(); |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 536 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 537 | |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 538 | if (destroyed && observer != null) { |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 539 | try { |
| 540 | observer.onDestroy(); |
| 541 | } catch (RemoteException re) { |
| 542 | Log.e(LOG_TAG, "Error announcing destroyed state", re); |
| 543 | } |
| 544 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public void start() { |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 549 | synchronized (mLock) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 550 | // If destroyed the handler is null. |
| 551 | if (!isDestroyedLocked()) { |
| 552 | mHandler.obtainMessage(MyHandler.MSG_ON_START, |
| 553 | mDocumentAdapter).sendToTarget(); |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 554 | } |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 555 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | @Override |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 559 | public void layout(PrintAttributes oldAttributes, PrintAttributes newAttributes, |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 560 | ILayoutResultCallback callback, Bundle metadata, int sequence) { |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 561 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 562 | ICancellationSignal cancellationTransport = CancellationSignal.createTransport(); |
| 563 | try { |
| 564 | callback.onLayoutStarted(cancellationTransport, sequence); |
| 565 | } catch (RemoteException re) { |
| 566 | // The spooler is dead - can't recover. |
| 567 | Log.e(LOG_TAG, "Error notifying for layout start", re); |
| 568 | return; |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 569 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 570 | |
| 571 | synchronized (mLock) { |
| 572 | // If destroyed the handler is null. |
| 573 | if (isDestroyedLocked()) { |
| 574 | return; |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 575 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 576 | |
| 577 | CancellationSignal cancellationSignal = CancellationSignal.fromTransport( |
| 578 | cancellationTransport); |
| 579 | |
| 580 | SomeArgs args = SomeArgs.obtain(); |
| 581 | args.arg1 = mDocumentAdapter; |
| 582 | args.arg2 = oldAttributes; |
| 583 | args.arg3 = newAttributes; |
| 584 | args.arg4 = cancellationSignal; |
| 585 | args.arg5 = new MyLayoutResultCallback(callback, sequence); |
| 586 | args.arg6 = metadata; |
| 587 | |
| 588 | mHandler.obtainMessage(MyHandler.MSG_ON_LAYOUT, args).sendToTarget(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 589 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | @Override |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 593 | public void write(PageRange[] pages, ParcelFileDescriptor fd, |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 594 | IWriteResultCallback callback, int sequence) { |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 595 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 596 | ICancellationSignal cancellationTransport = CancellationSignal.createTransport(); |
| 597 | try { |
| 598 | callback.onWriteStarted(cancellationTransport, sequence); |
| 599 | } catch (RemoteException re) { |
| 600 | // The spooler is dead - can't recover. |
| 601 | Log.e(LOG_TAG, "Error notifying for write start", re); |
| 602 | return; |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 603 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 604 | |
| 605 | synchronized (mLock) { |
| 606 | // If destroyed the handler is null. |
| 607 | if (isDestroyedLocked()) { |
| 608 | return; |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 609 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 610 | |
| 611 | CancellationSignal cancellationSignal = CancellationSignal.fromTransport( |
| 612 | cancellationTransport); |
| 613 | |
| 614 | SomeArgs args = SomeArgs.obtain(); |
| 615 | args.arg1 = mDocumentAdapter; |
| 616 | args.arg2 = pages; |
| 617 | args.arg3 = fd; |
| 618 | args.arg4 = cancellationSignal; |
| 619 | args.arg5 = new MyWriteResultCallback(callback, fd, sequence); |
| 620 | |
| 621 | mHandler.obtainMessage(MyHandler.MSG_ON_WRITE, args).sendToTarget(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 622 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public void finish() { |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 627 | synchronized (mLock) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 628 | // If destroyed the handler is null. |
| 629 | if (!isDestroyedLocked()) { |
| 630 | mHandler.obtainMessage(MyHandler.MSG_ON_FINISH, |
| 631 | mDocumentAdapter).sendToTarget(); |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 632 | } |
Svetoslav | d270cb9 | 2013-10-31 14:27:16 -0700 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
| 636 | @Override |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 637 | public void kill(String reason) { |
| 638 | synchronized (mLock) { |
| 639 | // If destroyed the handler is null. |
| 640 | if (!isDestroyedLocked()) { |
| 641 | mHandler.obtainMessage(MyHandler.MSG_ON_KILL, |
| 642 | reason).sendToTarget(); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | @Override |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 648 | public void onActivityPaused(Activity activity) { |
| 649 | /* do nothing */ |
| 650 | } |
| 651 | |
| 652 | @Override |
| 653 | public void onActivityCreated(Activity activity, Bundle savedInstanceState) { |
| 654 | /* do nothing */ |
| 655 | } |
| 656 | |
| 657 | @Override |
| 658 | public void onActivityStarted(Activity activity) { |
| 659 | /* do nothing */ |
| 660 | } |
| 661 | |
| 662 | @Override |
| 663 | public void onActivityResumed(Activity activity) { |
| 664 | /* do nothing */ |
| 665 | } |
| 666 | |
| 667 | @Override |
| 668 | public void onActivityStopped(Activity activity) { |
| 669 | /* do nothing */ |
| 670 | } |
| 671 | |
| 672 | @Override |
| 673 | public void onActivitySaveInstanceState(Activity activity, Bundle outState) { |
| 674 | /* do nothing */ |
| 675 | } |
| 676 | |
| 677 | @Override |
| 678 | public void onActivityDestroyed(Activity activity) { |
| 679 | // We really care only if the activity is being destroyed to |
| 680 | // notify the the print spooler so it can close the print dialog. |
| 681 | // Note the the spooler has a death recipient that observes if |
| 682 | // this process gets killed so we cover the case of onDestroy not |
| 683 | // being called due to this process being killed to reclaim memory. |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 684 | IPrintDocumentAdapterObserver observer = null; |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 685 | synchronized (mLock) { |
| 686 | if (activity == mActivity) { |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 687 | observer = mObserver; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 688 | destroyLocked(); |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | if (observer != null) { |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 692 | try { |
| 693 | observer.onDestroy(); |
| 694 | } catch (RemoteException re) { |
| 695 | Log.e(LOG_TAG, "Error announcing destroyed state", re); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 700 | private boolean isDestroyedLocked() { |
| 701 | return (mActivity == null); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 704 | private void destroyLocked() { |
| 705 | mActivity.getApplication().unregisterActivityLifecycleCallbacks( |
| 706 | PrintDocumentAdapterDelegate.this); |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 707 | mActivity = null; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 708 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 709 | mDocumentAdapter = null; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 710 | |
| 711 | // This method is only called from the main thread, so |
| 712 | // clearing the messages guarantees that any time a |
| 713 | // message is handled we are not in a destroyed state. |
| 714 | mHandler.removeMessages(MyHandler.MSG_ON_START); |
| 715 | mHandler.removeMessages(MyHandler.MSG_ON_LAYOUT); |
| 716 | mHandler.removeMessages(MyHandler.MSG_ON_WRITE); |
| 717 | mHandler.removeMessages(MyHandler.MSG_ON_FINISH); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 718 | mHandler = null; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 719 | |
| 720 | mObserver = null; |
| 721 | |
| 722 | if (mPendingCallback != null) { |
| 723 | mPendingCallback.destroy(); |
| 724 | mPendingCallback = null; |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 728 | private final class MyHandler extends Handler { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 729 | public static final int MSG_ON_START = 1; |
| 730 | public static final int MSG_ON_LAYOUT = 2; |
| 731 | public static final int MSG_ON_WRITE = 3; |
| 732 | public static final int MSG_ON_FINISH = 4; |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 733 | public static final int MSG_ON_KILL = 5; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 734 | |
| 735 | public MyHandler(Looper looper) { |
| 736 | super(looper, null, true); |
| 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public void handleMessage(Message message) { |
| 741 | switch (message.what) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 742 | case MSG_ON_START: { |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 743 | if (DEBUG) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 744 | Log.i(LOG_TAG, "onStart()"); |
Svetoslav Ganov | db63677 | 2013-10-07 14:31:18 -0700 | [diff] [blame] | 745 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 746 | |
| 747 | ((PrintDocumentAdapter) message.obj).onStart(); |
| 748 | } break; |
| 749 | |
| 750 | case MSG_ON_LAYOUT: { |
| 751 | SomeArgs args = (SomeArgs) message.obj; |
| 752 | PrintDocumentAdapter adapter = (PrintDocumentAdapter) args.arg1; |
| 753 | PrintAttributes oldAttributes = (PrintAttributes) args.arg2; |
| 754 | PrintAttributes newAttributes = (PrintAttributes) args.arg3; |
| 755 | CancellationSignal cancellation = (CancellationSignal) args.arg4; |
| 756 | LayoutResultCallback callback = (LayoutResultCallback) args.arg5; |
| 757 | Bundle metadata = (Bundle) args.arg6; |
| 758 | args.recycle(); |
| 759 | |
| 760 | if (DEBUG) { |
| 761 | StringBuilder builder = new StringBuilder(); |
| 762 | builder.append("PrintDocumentAdapter#onLayout() {\n"); |
| 763 | builder.append("\n oldAttributes:").append(oldAttributes); |
| 764 | builder.append("\n newAttributes:").append(newAttributes); |
| 765 | builder.append("\n preview:").append(metadata.getBoolean( |
| 766 | PrintDocumentAdapter.EXTRA_PRINT_PREVIEW)); |
| 767 | builder.append("\n}"); |
| 768 | Log.i(LOG_TAG, builder.toString()); |
| 769 | } |
| 770 | |
| 771 | adapter.onLayout(oldAttributes, newAttributes, cancellation, |
| 772 | callback, metadata); |
| 773 | } break; |
| 774 | |
| 775 | case MSG_ON_WRITE: { |
| 776 | SomeArgs args = (SomeArgs) message.obj; |
| 777 | PrintDocumentAdapter adapter = (PrintDocumentAdapter) args.arg1; |
| 778 | PageRange[] pages = (PageRange[]) args.arg2; |
| 779 | ParcelFileDescriptor fd = (ParcelFileDescriptor) args.arg3; |
| 780 | CancellationSignal cancellation = (CancellationSignal) args.arg4; |
| 781 | WriteResultCallback callback = (WriteResultCallback) args.arg5; |
| 782 | args.recycle(); |
| 783 | |
| 784 | if (DEBUG) { |
| 785 | StringBuilder builder = new StringBuilder(); |
| 786 | builder.append("PrintDocumentAdapter#onWrite() {\n"); |
| 787 | builder.append("\n pages:").append(Arrays.toString(pages)); |
| 788 | builder.append("\n}"); |
| 789 | Log.i(LOG_TAG, builder.toString()); |
| 790 | } |
| 791 | |
| 792 | adapter.onWrite(pages, fd, cancellation, callback); |
| 793 | } break; |
| 794 | |
| 795 | case MSG_ON_FINISH: { |
| 796 | if (DEBUG) { |
| 797 | Log.i(LOG_TAG, "onFinish()"); |
| 798 | } |
| 799 | |
| 800 | ((PrintDocumentAdapter) message.obj).onFinish(); |
| 801 | |
| 802 | // Done printing, so destroy this instance as it |
| 803 | // should not be used anymore. |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 804 | synchronized (mLock) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 805 | destroyLocked(); |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 806 | } |
| 807 | } break; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 808 | |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 809 | case MSG_ON_KILL: { |
| 810 | if (DEBUG) { |
| 811 | Log.i(LOG_TAG, "onKill()"); |
| 812 | } |
| 813 | |
| 814 | String reason = (String) message.obj; |
| 815 | throw new RuntimeException(reason); |
| 816 | } |
| 817 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 818 | default: { |
| 819 | throw new IllegalArgumentException("Unknown message: " |
| 820 | + message.what); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | } |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 825 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 826 | private interface DestroyableCallback { |
| 827 | public void destroy(); |
| 828 | } |
| 829 | |
| 830 | private final class MyLayoutResultCallback extends LayoutResultCallback |
| 831 | implements DestroyableCallback { |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 832 | private ILayoutResultCallback mCallback; |
| 833 | private final int mSequence; |
| 834 | |
| 835 | public MyLayoutResultCallback(ILayoutResultCallback callback, |
| 836 | int sequence) { |
| 837 | mCallback = callback; |
| 838 | mSequence = sequence; |
| 839 | } |
| 840 | |
| 841 | @Override |
| 842 | public void onLayoutFinished(PrintDocumentInfo info, boolean changed) { |
| 843 | final ILayoutResultCallback callback; |
| 844 | synchronized (mLock) { |
| 845 | callback = mCallback; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 846 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 847 | |
| 848 | // If the callback is null we are destroyed. |
| 849 | if (callback == null) { |
| 850 | Log.e(LOG_TAG, "PrintDocumentAdapter is destroyed. Did you " |
| 851 | + "finish the printing activity before print completion " |
| 852 | + "or did you invoke a callback after finish?"); |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | try { |
| 857 | if (info == null) { |
| 858 | throw new NullPointerException("document info cannot be null"); |
| 859 | } |
| 860 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 861 | try { |
| 862 | callback.onLayoutFinished(info, changed, mSequence); |
| 863 | } catch (RemoteException re) { |
| 864 | Log.e(LOG_TAG, "Error calling onLayoutFinished", re); |
| 865 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 866 | } finally { |
| 867 | destroy(); |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
| 871 | @Override |
| 872 | public void onLayoutFailed(CharSequence error) { |
| 873 | final ILayoutResultCallback callback; |
| 874 | synchronized (mLock) { |
| 875 | callback = mCallback; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 876 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 877 | |
| 878 | // If the callback is null we are destroyed. |
| 879 | if (callback == null) { |
| 880 | Log.e(LOG_TAG, "PrintDocumentAdapter is destroyed. Did you " |
| 881 | + "finish the printing activity before print completion " |
| 882 | + "or did you invoke a callback after finish?"); |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | try { |
| 887 | callback.onLayoutFailed(error, mSequence); |
| 888 | } catch (RemoteException re) { |
| 889 | Log.e(LOG_TAG, "Error calling onLayoutFailed", re); |
| 890 | } finally { |
| 891 | destroy(); |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
| 895 | @Override |
| 896 | public void onLayoutCancelled() { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 897 | final ILayoutResultCallback callback; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 898 | synchronized (mLock) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 899 | callback = mCallback; |
| 900 | } |
| 901 | |
| 902 | // If the callback is null we are destroyed. |
| 903 | if (callback == null) { |
| 904 | Log.e(LOG_TAG, "PrintDocumentAdapter is destroyed. Did you " |
| 905 | + "finish the printing activity before print completion " |
| 906 | + "or did you invoke a callback after finish?"); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | try { |
| 911 | callback.onLayoutCanceled(mSequence); |
| 912 | } catch (RemoteException re) { |
| 913 | Log.e(LOG_TAG, "Error calling onLayoutFailed", re); |
| 914 | } finally { |
| 915 | destroy(); |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 919 | @Override |
| 920 | public void destroy() { |
| 921 | synchronized (mLock) { |
| 922 | mCallback = null; |
| 923 | mPendingCallback = null; |
| 924 | } |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 928 | private final class MyWriteResultCallback extends WriteResultCallback |
| 929 | implements DestroyableCallback { |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 930 | private ParcelFileDescriptor mFd; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 931 | private IWriteResultCallback mCallback; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 932 | private final int mSequence; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 933 | |
| 934 | public MyWriteResultCallback(IWriteResultCallback callback, |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 935 | ParcelFileDescriptor fd, int sequence) { |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 936 | mFd = fd; |
| 937 | mSequence = sequence; |
| 938 | mCallback = callback; |
| 939 | } |
| 940 | |
| 941 | @Override |
| 942 | public void onWriteFinished(PageRange[] pages) { |
| 943 | final IWriteResultCallback callback; |
| 944 | synchronized (mLock) { |
| 945 | callback = mCallback; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 946 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 947 | |
| 948 | // If the callback is null we are destroyed. |
| 949 | if (callback == null) { |
| 950 | Log.e(LOG_TAG, "PrintDocumentAdapter is destroyed. Did you " |
| 951 | + "finish the printing activity before print completion " |
| 952 | + "or did you invoke a callback after finish?"); |
| 953 | return; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 954 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 955 | |
| 956 | try { |
| 957 | if (pages == null) { |
| 958 | throw new IllegalArgumentException("pages cannot be null"); |
| 959 | } |
| 960 | if (pages.length == 0) { |
| 961 | throw new IllegalArgumentException("pages cannot be empty"); |
| 962 | } |
| 963 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 964 | try { |
| 965 | callback.onWriteFinished(pages, mSequence); |
| 966 | } catch (RemoteException re) { |
| 967 | Log.e(LOG_TAG, "Error calling onWriteFinished", re); |
| 968 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 969 | } finally { |
| 970 | destroy(); |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
| 974 | @Override |
| 975 | public void onWriteFailed(CharSequence error) { |
| 976 | final IWriteResultCallback callback; |
| 977 | synchronized (mLock) { |
| 978 | callback = mCallback; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 979 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 980 | |
| 981 | // If the callback is null we are destroyed. |
| 982 | if (callback == null) { |
| 983 | Log.e(LOG_TAG, "PrintDocumentAdapter is destroyed. Did you " |
| 984 | + "finish the printing activity before print completion " |
| 985 | + "or did you invoke a callback after finish?"); |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | try { |
| 990 | callback.onWriteFailed(error, mSequence); |
| 991 | } catch (RemoteException re) { |
| 992 | Log.e(LOG_TAG, "Error calling onWriteFailed", re); |
| 993 | } finally { |
| 994 | destroy(); |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
| 998 | @Override |
| 999 | public void onWriteCancelled() { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1000 | final IWriteResultCallback callback; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 1001 | synchronized (mLock) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1002 | callback = mCallback; |
| 1003 | } |
| 1004 | |
| 1005 | // If the callback is null we are destroyed. |
| 1006 | if (callback == null) { |
| 1007 | Log.e(LOG_TAG, "PrintDocumentAdapter is destroyed. Did you " |
| 1008 | + "finish the printing activity before print completion " |
| 1009 | + "or did you invoke a callback after finish?"); |
| 1010 | return; |
| 1011 | } |
| 1012 | |
| 1013 | try { |
| 1014 | callback.onWriteCanceled(mSequence); |
| 1015 | } catch (RemoteException re) { |
| 1016 | Log.e(LOG_TAG, "Error calling onWriteCanceled", re); |
| 1017 | } finally { |
| 1018 | destroy(); |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1022 | @Override |
| 1023 | public void destroy() { |
| 1024 | synchronized (mLock) { |
| 1025 | IoUtils.closeQuietly(mFd); |
| 1026 | mCallback = null; |
| 1027 | mFd = null; |
| 1028 | mPendingCallback = null; |
| 1029 | } |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 1032 | } |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1033 | |
| 1034 | private static final class PrintJobStateChangeListenerWrapper extends |
| 1035 | IPrintJobStateChangeListener.Stub { |
| 1036 | private final WeakReference<PrintJobStateChangeListener> mWeakListener; |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 1037 | private final WeakReference<Handler> mWeakHandler; |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1038 | |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 1039 | public PrintJobStateChangeListenerWrapper(PrintJobStateChangeListener listener, |
| 1040 | Handler handler) { |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1041 | mWeakListener = new WeakReference<PrintJobStateChangeListener>(listener); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 1042 | mWeakHandler = new WeakReference<Handler>(handler); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | @Override |
| 1046 | public void onPrintJobStateChanged(PrintJobId printJobId) { |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 1047 | Handler handler = mWeakHandler.get(); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1048 | PrintJobStateChangeListener listener = mWeakListener.get(); |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 1049 | if (handler != null && listener != null) { |
| 1050 | SomeArgs args = SomeArgs.obtain(); |
Svetoslav Ganov | d91cb3e | 2013-10-12 15:44:42 -0700 | [diff] [blame] | 1051 | args.arg1 = this; |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 1052 | args.arg2 = printJobId; |
| 1053 | handler.obtainMessage(MSG_NOTIFY_PRINT_JOB_STATE_CHANGED, |
| 1054 | args).sendToTarget(); |
Svetoslav Ganov | 704697b | 2013-09-21 20:30:24 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
Svetoslav Ganov | a18661d | 2013-10-09 22:55:49 -0700 | [diff] [blame] | 1057 | |
Svetoslav Ganov | d91cb3e | 2013-10-12 15:44:42 -0700 | [diff] [blame] | 1058 | public void destroy() { |
| 1059 | mWeakListener.clear(); |
| 1060 | } |
| 1061 | |
| 1062 | public PrintJobStateChangeListener getListener() { |
| 1063 | return mWeakListener.get(); |
| 1064 | } |
| 1065 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 1066 | } |