/* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.print; import java.io.FileDescriptor; import java.io.IOException; import java.util.List; import android.os.CancellationSignal; /** * Base class that provides data to be printed. * *

Lifecycle

*

*

*

*

*/ public abstract class PrintAdapter { /** * Called when printing started. You can use this callback to * allocate resources. *

* Note: Invoked on the main thread. *

*/ public void onStart() { /* do nothing - stub */ } /** * Called when the print job attributes (page size, density, etc) * changed giving you a chance to re-layout the content such that * it matches the new constraints. *

* Note: Invoked on the main thread. *

* * @param attributes The print job attributes. * @return Whether the content changed based on the provided attributes. */ public boolean onPrintAttributesChanged(PrintAttributes attributes) { return false; } /** * Called when specific pages of the content have to be printed in the from of * a PDF file to the given file descriptor. You should not * close the file descriptor instead you have to invoke {@link PrintProgressCallback * #onWriteFinished()} or {@link PrintProgressCallback#onPrintFailed(CharSequence)}. *

* Note: If the printed content is large, it is a good * practice to schedule writing it on a dedicated thread and register a * callback in the provided {@link CancellationSignal} upon which to stop * writing data. The cancellation callback will not be made on the main * thread. *

*

* Note: Invoked on the main thread. *

*

* * @param pages The pages whose content to write. * @param destination The destination file descriptor to which to start writing. * @param cancellationSignal Signal for observing cancel write requests. * @param progressListener Callback to inform the system with the write progress. * * @see CancellationSignal */ public abstract void onPrint(List pages, FileDescriptor destination, CancellationSignal cancellationSignal, PrintProgressCallback progressListener); /** * Called when printing finished. You can use this callback to release * resources. *

* Note: Invoked on the main thread. *

*/ public void onFinish() { /* do nothing - stub */ } /** * Gets a {@link PrinterInfo} object that contains metadata about the * printed content. *

* Note: Invoked on the main thread. *

* * @return The info object for this {@link PrintAdapter}. * * @see PrintAdapterInfo */ public abstract PrintAdapterInfo getInfo(); /** * Base class for implementing a listener for the printing progress * of a {@link PrintAdapter}. */ public static abstract class PrintProgressCallback { PrintProgressCallback() { /* do nothing - hide constructor */ } /** * Notifies that all the data was printed. * * @param pages The pages that were printed. */ public void onPrintFinished(List pages) { /* do nothing - stub */ } /** * Notifies that an error occurred while printing the data. * * @param error Error message. May be null if error is unknown. */ public void onPrintFailed(CharSequence error) { /* do nothing - stub */ } } }