blob: 0d3c5ba96782d815b67aed7e86cd186e88c86727 [file] [log] [blame]
/*
* 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.support.v4.print;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.net.Uri;
import java.io.FileNotFoundException;
/**
* Helper for printing bitmaps.
*/
public final class PrintHelper {
/**
* image will be scaled but leave white space
*/
public static final int SCALE_MODE_FIT = 1;
/**
* image will fill the paper and be cropped (default)
*/
public static final int SCALE_MODE_FILL = 2;
/**
* this is a black and white image
*/
public static final int COLOR_MODE_MONOCHROME = 1;
/**
* this is a color image (default)
*/
public static final int COLOR_MODE_COLOR = 2;
PrintHelperVersionImpl mImpl;
/**
* Gets whether the system supports printing.
*
* @return True if printing is supported.
*/
public static boolean systemSupportsPrint() {
if (Build.VERSION.SDK_INT >= 19) {
// Supported on Android 4.4 or later.
return true;
}
return false;
}
/**
* Interface implemented by classes that support printing
*/
static interface PrintHelperVersionImpl {
public void setScaleMode(int scaleMode);
public int getScaleMode();
public void setColorMode(int colorMode);
public int getColorMode();
public void printBitmap(String jobName, Bitmap bitmap);
public void printBitmap(String jobName, Uri imageFile)
throws FileNotFoundException;
}
/**
* Implementation used when we do not support printing
*/
private static final class PrintHelperStubImpl implements PrintHelperVersionImpl {
int mScaleMode = SCALE_MODE_FILL;
int mColorMode = COLOR_MODE_COLOR;
@Override
public void setScaleMode(int scaleMode) {
mScaleMode = scaleMode;
}
@Override
public int getColorMode() {
return mColorMode;
}
@Override
public void setColorMode(int colorMode) {
mColorMode = colorMode;
}
@Override
public int getScaleMode() {
return mScaleMode;
}
@Override
public void printBitmap(String jobName, Bitmap bitmap) {
}
@Override
public void printBitmap(String jobName, Uri imageFile) {
}
}
/**
* Implementation used on KitKat (and above)
*/
private static final class PrintHelperKitkatImpl implements PrintHelperVersionImpl {
private final PrintHelperKitkat printHelper;
PrintHelperKitkatImpl(Context context) {
printHelper = new PrintHelperKitkat(context);
}
@Override
public void setScaleMode(int scaleMode) {
printHelper.setScaleMode(scaleMode);
}
@Override
public int getScaleMode() {
return printHelper.getScaleMode();
}
@Override
public void setColorMode(int colorMode) {
printHelper.setColorMode(colorMode);
}
@Override
public int getColorMode() {
return printHelper.getColorMode();
}
@Override
public void printBitmap(String jobName, Bitmap bitmap) {
printHelper.printBitmap(jobName, bitmap);
}
@Override
public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException {
printHelper.printBitmap(jobName, imageFile);
}
}
/**
* Returns the PrintHelper that can be used to print images.
*
* @param context A context for accessing system resources.
* @return the <code>PrintHelper</code> to support printing images.
*/
public PrintHelper(Context context) {
if (systemSupportsPrint()) {
mImpl = new PrintHelperKitkatImpl(context);
} else {
mImpl = new PrintHelperStubImpl();
}
}
/**
* Selects whether the image will fill the paper and be cropped
* {@link #SCALE_MODE_FIT}
* or whether the image will be scaled but leave white space
* {@link #SCALE_MODE_FILL}.
*
* @param scaleMode {@link #SCALE_MODE_FIT} or
* {@link #SCALE_MODE_FILL}
*/
public void setScaleMode(int scaleMode) {
mImpl.setScaleMode(scaleMode);
}
/**
* Returns the scale mode with which the image will fill the paper.
*
* @return The scale Mode: {@link #SCALE_MODE_FIT} or
* {@link #SCALE_MODE_FILL}
*/
public int getScaleMode() {
return mImpl.getScaleMode();
}
/**
* Sets whether the image will be printed in color (default)
* {@link #COLOR_MODE_COLOR} or in back and white
* {@link #COLOR_MODE_MONOCHROME}.
*
* @param colorMode The color mode which is one of
* {@link #COLOR_MODE_COLOR} and {@link #COLOR_MODE_MONOCHROME}.
*/
public void setColorMode(int colorMode) {
mImpl.setColorMode(colorMode);
}
/**
* Gets the color mode with which the image will be printed.
*
* @return The color mode which is one of {@link #COLOR_MODE_COLOR}
* and {@link #COLOR_MODE_MONOCHROME}.
*/
public int getColorMode() {
return mImpl.getColorMode();
}
/**
* Prints a bitmap.
*
* @param jobName The print job name.
* @param bitmap The bitmap to print.
*/
public void printBitmap(String jobName, Bitmap bitmap) {
mImpl.printBitmap(jobName, bitmap);
}
/**
* Prints an image located at the Uri. Image types supported are those of
* {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
* android.graphics.BitmapFactory.decodeStream(java.io.InputStream)}
*
* @param jobName The print job name.
* @param imageFile The <code>Uri</code> pointing to an image to print.
* @throws FileNotFoundException if <code>Uri</code> is not pointing to a valid image.
*/
public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException {
mImpl.printBitmap(jobName, imageFile);
}
}