Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -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 | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 19 | import android.os.Bundle; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 20 | import android.os.CancellationSignal; |
| 21 | |
| 22 | import java.io.FileDescriptor; |
| 23 | import java.util.List; |
| 24 | |
| 25 | /** |
| 26 | * Base class that provides the content of a document to be printed. |
| 27 | * |
| 28 | * <h3>Lifecycle</h3> |
| 29 | * <p> |
| 30 | * <ul> |
| 31 | * <li> |
| 32 | * Initially, you will receive a call to {@link #onStart()}. This callback |
| 33 | * can be used to allocate resources. |
| 34 | * </li> |
| 35 | * <li> |
| 36 | * Next, you will get one or more calls to {@link #onLayout(PrintAttributes, |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 37 | * PrintAttributes, CancellationSignal, LayoutResultCallback, Bundle)} to |
| 38 | * inform you that the print attributes (page size, density, etc) changed |
| 39 | * giving you an opportunity to layout the content to match the new constraints. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 40 | * </li> |
| 41 | * <li> |
| 42 | * After every call to {@link #onLayout(PrintAttributes, PrintAttributes, |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 43 | * CancellationSignal, LayoutResultCallback, Bundle)}, you may get a call to |
| 44 | * {@link #onWrite(List, FileDescriptor, CancellationSignal, WriteResultCallback)} |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 45 | * asking you to write a PDF file with the content for specific pages. |
| 46 | * </li> |
| 47 | * <li> |
| 48 | * Finally, you will receive a call to {@link #onFinish()}. You can use this |
| 49 | * callback to release resources allocated in {@link #onStart()}. |
| 50 | * </li> |
| 51 | * </ul> |
| 52 | * </p> |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 53 | * <h3>Implementation</h3> |
| 54 | * <p> |
| 55 | * The APIs defined in this class are designed to enable doing part or all |
| 56 | * of the work on an arbitrary thread. For example, if the printed content |
| 57 | * does not depend on the UI state, i.e. on what is shown on the screen, then |
| 58 | * you can off load the entire work on a dedicated thread, thus making your |
| 59 | * application interactive while the print work is being performed. |
| 60 | * </p> |
| 61 | * <p> |
| 62 | * You can also do work on different threads, for example if you print UI |
| 63 | * content, you can handle {@link #onStart()} and {@link #onLayout(PrintAttributes, |
| 64 | * PrintAttributes, CancellationSignal, LayoutResultCallback, Bundle)} on |
| 65 | * the UI thread (assuming onStart initializes resources needed for layout). |
| 66 | * This will ensure that the UI does not change while you are laying out the |
| 67 | * printed content. Then you can handle {@link #onWrite(List, FileDescriptor, |
| 68 | * CancellationSignal, WriteResultCallback)} and {@link #onFinish()} on another |
| 69 | * thread. This will ensure that the UI is frozen for the minimal amount of |
| 70 | * time. Also this assumes that you will generate the printed content in |
| 71 | * {@link #onLayout(PrintAttributes, PrintAttributes, CancellationSignal, |
| 72 | * LayoutResultCallback, Bundle)} which is not mandatory. If you use multiple |
| 73 | * threads, you are responsible for proper synchronization. |
| 74 | * </p> |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 75 | */ |
| 76 | public abstract class PrintDocumentAdapter { |
| 77 | |
| 78 | /** |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 79 | * Meta-data key: mapped to a boolean value that is <code>true</code> if |
| 80 | * the current layout is for a print preview, <code>false</code> otherwise. |
| 81 | */ |
| 82 | public static final String METADATA_KEY_PRINT_PREVIEW = "KEY_METADATA_PRINT_PREVIEW"; |
| 83 | |
| 84 | /** |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 85 | * Called when printing starts. You can use this callback to allocate |
| 86 | * resources. This method is invoked on the main thread. |
| 87 | */ |
| 88 | public void onStart() { |
| 89 | /* do nothing - stub */ |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Called when the print attributes (page size, density, etc) changed |
| 94 | * giving you a chance to layout the content such that it matches the |
| 95 | * new constraints. This method is invoked on the main thread. |
| 96 | * <p> |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 97 | * After you are done laying out, you <strong>must</strong> invoke: {@link |
| 98 | * LayoutResultCallback#onLayoutFinished(PrintDocumentInfo, boolean)} with |
| 99 | * the last argument <code>true</code> or <code>false</code> depending on |
| 100 | * whether the layout changed the content or not, respectively; and {@link |
| 101 | * LayoutResultCallback#onLayoutFailed(CharSequence)}, if an error occurred. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 102 | * </p> |
| 103 | * <p> |
| 104 | * <strong>Note:</strong> If the content is large and a layout will be |
| 105 | * performed, it is a good practice to schedule the work on a dedicated |
| 106 | * thread and register an observer in the provided {@link |
| 107 | * CancellationSignal} upon invocation of which you should stop the |
| 108 | * layout. The cancellation callback will not be made on the main |
| 109 | * thread. |
| 110 | * </p> |
| 111 | * |
| 112 | * @param oldAttributes The old print attributes. |
| 113 | * @param newAttributes The new print attributes. |
| 114 | * @param cancellationSignal Signal for observing cancel layout requests. |
| 115 | * @param callback Callback to inform the system for the layout result. |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 116 | * @param metadata Additional information about how layout the content. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 117 | * |
| 118 | * @see LayoutResultCallback |
| 119 | * @see CancellationSignal |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 120 | * @see #METADATA_KEY_PRINT_PREVIEW |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 121 | */ |
| 122 | public abstract void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 123 | CancellationSignal cancellationSignal, LayoutResultCallback callback, |
| 124 | Bundle metadata); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Called when specific pages of the content should be written in the |
| 128 | * from of a PDF file to the given file descriptor. This method is invoked |
| 129 | * on the main thread. |
| 130 | *<p> |
| 131 | * After you are done writing, you should <strong>not</strong> close the |
| 132 | * file descriptor, rather you must invoke: {@link WriteResultCallback |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 133 | * #onWriteFinished(List)}, if writing completed successfully; or {@link |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 134 | * WriteResultCallback#onWriteFailed(CharSequence)}, if an error occurred. |
| 135 | * </p> |
| 136 | * <p> |
| 137 | * <strong>Note:</strong> If the printed content is large, it is a good |
| 138 | * practice to schedule writing it on a dedicated thread and register an |
| 139 | * observer in the provided {@link CancellationSignal} upon invocation of |
| 140 | * which you should stop writing. The cancellation callback will not be |
| 141 | * made on the main thread. |
| 142 | * </p> |
| 143 | * |
| 144 | * @param pages The pages whose content to print. |
| 145 | * @param destination The destination file descriptor to which to write. |
| 146 | * @param cancellationSignal Signal for observing cancel writing requests. |
| 147 | * @param callback Callback to inform the system for the write result. |
| 148 | * |
| 149 | * @see WriteResultCallback |
| 150 | * @see CancellationSignal |
| 151 | */ |
| 152 | public abstract void onWrite(List<PageRange> pages, FileDescriptor destination, |
| 153 | CancellationSignal cancellationSignal, WriteResultCallback callback); |
| 154 | |
| 155 | /** |
| 156 | * Called when printing finishes. You can use this callback to release |
| 157 | * resources acquired in {@link #onStart()}. This method is invoked on |
| 158 | * the main thread. |
| 159 | */ |
| 160 | public void onFinish() { |
| 161 | /* do nothing - stub */ |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Base class for implementing a callback for the result of {@link |
| 166 | * PrintDocumentAdapter#onWrite(List, FileDescriptor, CancellationSignal, |
| 167 | * WriteResultCallback)}. |
| 168 | */ |
| 169 | public static abstract class WriteResultCallback { |
| 170 | |
| 171 | /** |
| 172 | * @hide |
| 173 | */ |
| 174 | public WriteResultCallback() { |
| 175 | /* do nothing - hide constructor */ |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Notifies that all the data was written. |
| 180 | * |
| 181 | * @param pages The pages that were written. |
| 182 | */ |
| 183 | public void onWriteFinished(List<PageRange> pages) { |
| 184 | /* do nothing - stub */ |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Notifies that an error occurred while writing the data. |
| 189 | * |
| 190 | * @param error Error message. May be null if error is unknown. |
| 191 | */ |
| 192 | public void onWriteFailed(CharSequence error) { |
| 193 | /* do nothing - stub */ |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Base class for implementing a callback for the result of {@link |
| 199 | * PrintDocumentAdapter#onLayout(PrintAttributes, PrintAttributes, |
Svetoslav | 6283608 | 2013-07-17 14:52:35 -0700 | [diff] [blame] | 200 | * CancellationSignal, LayoutResultCallback, Bundle)}. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 201 | */ |
| 202 | public static abstract class LayoutResultCallback { |
| 203 | |
| 204 | /** |
| 205 | * @hide |
| 206 | */ |
| 207 | public LayoutResultCallback() { |
| 208 | /* do nothing - hide constructor */ |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Notifies that the layout finished and whether the content changed. |
| 213 | * |
| 214 | * @param info An info object describing the document. |
| 215 | * @param changed Whether the layout changed. |
| 216 | * |
| 217 | * @see PrintDocumentInfo |
| 218 | */ |
| 219 | public void onLayoutFinished(PrintDocumentInfo info, boolean changed) { |
| 220 | /* do nothing - stub */ |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Notifies that an error occurred while laying out the document. |
| 225 | * |
| 226 | * @param error Error message. May be null if error is unknown. |
| 227 | */ |
| 228 | public void onLayoutFailed(CharSequence error) { |
| 229 | /* do nothing - stub */ |
| 230 | } |
| 231 | } |
| 232 | } |