PrintHelper.java revision b363776d911abae9d067b9ef77fccc1c3c56e652
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 17package android.support.v4.print; 18 19import android.content.Context; 20import android.graphics.Bitmap; 21import android.os.Build; 22import android.net.Uri; 23 24import java.io.FileNotFoundException; 25 26/** 27 * Helper for printing bitmaps. 28 */ 29public final class PrintHelper { 30 /** 31 * image will be scaled but leave white space 32 */ 33 public static final int SCALE_MODE_FIT = 1; 34 /** 35 * image will fill the paper and be cropped (default) 36 */ 37 public static final int SCALE_MODE_FILL = 2; 38 PrintHelperVersionImpl mImpl; 39 40 /** 41 * Gets whether the system supports printing. 42 * 43 * @return True if printing is supported. 44 */ 45 public static boolean systemSupportsPrint() { 46 if ("KeyLimePie".equals(Build.VERSION.CODENAME) // TODO recode to be KitKat and above 47 || Build.VERSION.SDK_INT > 18) { 48 49 return true; 50 } 51 return false; 52 } 53 54 /** 55 * Interface implemented by classes that support printing 56 */ 57 static interface PrintHelperVersionImpl { 58 59 public void setScaleMode(int scaleMode); 60 61 public int getScaleMode(); 62 63 public void printBitmap(String jobName, Bitmap bitmap); 64 65 public void printBitmap(String jobName, Uri imageFile) 66 throws FileNotFoundException; 67 } 68 69 /** 70 * Implementation used when we do not support printing 71 */ 72 private static final class PrintHelperStubImpl implements PrintHelperVersionImpl { 73 int mScaleMode; 74 75 @Override 76 public void setScaleMode(int scaleMode) { 77 mScaleMode = scaleMode; 78 } 79 80 @Override 81 public int getScaleMode() { 82 return mScaleMode; 83 } 84 85 @Override 86 public void printBitmap(String jobName, Bitmap bitmap) { 87 } 88 89 @Override 90 public void printBitmap(String jobName, Uri imageFile) { 91 } 92 } 93 94 /** 95 * Implementation used on KitKat (and above) 96 */ 97 private static final class PrintHelperKitkatImpl implements PrintHelperVersionImpl { 98 private final PrintHelperKitkat printHelper; 99 100 PrintHelperKitkatImpl(Context context) { 101 printHelper = new PrintHelperKitkat(context); 102 } 103 104 @Override 105 public void setScaleMode(int scaleMode) { 106 printHelper.setScaleMode(scaleMode); 107 } 108 109 @Override 110 public int getScaleMode() { 111 return printHelper.getScaleMode(); 112 } 113 114 @Override 115 public void printBitmap(String jobName, Bitmap bitmap) { 116 printHelper.printBitmap(jobName, bitmap); 117 } 118 119 @Override 120 public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException { 121 printHelper.printBitmap(jobName, imageFile); 122 } 123 } 124 125 /** 126 * Returns the PrintHelper that can be used to print images. 127 * 128 * @param context A context for accessing system resources. 129 * @return the <code>PrintHelper</code> to support printing images. 130 */ 131 public PrintHelper(Context context) { 132 if ("KeyLimePie".equals(Build.VERSION.CODENAME) // TODO recode to be KitKat and above 133 || Build.VERSION.SDK_INT > 18) { 134 mImpl = new PrintHelperKitkatImpl(context); 135 } else { 136 mImpl = new PrintHelperStubImpl(); 137 } 138 } 139 140 /** 141 * Selects whether the image will fill the paper and be cropped 142 * {@link #SCALE_MODE_FIT} 143 * or whether the image will be scaled but leave white space 144 * {@link #SCALE_MODE_FILL}. 145 * 146 * @param scaleMode {@link #SCALE_MODE_FIT} or 147 * {@link #SCALE_MODE_FILL} 148 */ 149 public void setScaleMode(int scaleMode) { 150 mImpl.setScaleMode(scaleMode); 151 } 152 153 /** 154 * Returns the scale mode with which the image will fill the paper. 155 * 156 * @return The scale Mode: {@link #SCALE_MODE_FIT} or 157 * {@link #SCALE_MODE_FILL} 158 */ 159 public int getScaleMode() { 160 return mImpl.getScaleMode(); 161 } 162 163 /** 164 * Prints a bitmap. 165 * 166 * @param jobName The print job name. 167 * @param bitmap The bitmap to print. 168 */ 169 public void printBitmap(String jobName, Bitmap bitmap) { 170 mImpl.printBitmap(jobName, bitmap); 171 } 172 173 /** 174 * Prints an image located at the Uri. Image types supported are those of 175 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream) 176 * android.graphics.BitmapFactory.decodeStream(java.io.InputStream)} 177 * 178 * @param jobName The print job name. 179 * @param imageFile The <code>Uri</code> pointing to an image to print. 180 * @throws FileNotFoundException if <code>Uri</code> is not pointing to a valid image. 181 */ 182 public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException { 183 mImpl.printBitmap(jobName, imageFile); 184 } 185}