PrintHelper.java revision b07ca601bba6ed850b7cf9fedcd44158d78a721f
1b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav/* 2b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Copyright (C) 2013 The Android Open Source Project 3b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 4b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Licensed under the Apache License, Version 2.0 (the "License"); 5b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * you may not use this file except in compliance with the License. 6b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * You may obtain a copy of the License at 7b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 8b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * http://www.apache.org/licenses/LICENSE-2.0 9b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 10b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Unless required by applicable law or agreed to in writing, software 11b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * distributed under the License is distributed on an "AS IS" BASIS, 12b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * See the License for the specific language governing permissions and 14b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * limitations under the License. 15b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 16b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 17b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavpackage android.support.v4.print; 18b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 19b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavimport android.content.Context; 20b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavimport android.graphics.Bitmap; 21b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavimport android.os.Build; 22b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavimport android.net.Uri; 23b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 24b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavimport java.io.FileNotFoundException; 25b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 26b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav/** 27b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Helper for printing bitmaps. 28b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 29b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavpublic final class PrintHelper { 30b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 31b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * image will be scaled but leave white space 32b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 33b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public static final int SCALE_MODE_FIT = 1; 34b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 35b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * image will fill the paper and be cropped (default) 36b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 37b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public static final int SCALE_MODE_FILL = 2; 38b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav PrintHelperVersionImpl mImpl; 39b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 40b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 41b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Gets whether the system supports printing. 42b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 43b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @return True if printing is supported. 44b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 45b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public static boolean systemSupportsPrint() { 46b07ca601bba6ed850b7cf9fedcd44158d78a721fDianne Hackborn if (Build.VERSION.SDK_INT >= 19) { 47b07ca601bba6ed850b7cf9fedcd44158d78a721fDianne Hackborn // Supported on Android 4.4 or later. 48b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return true; 49b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 50b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return false; 51b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 52b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 53b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 54b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Interface implemented by classes that support printing 55b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 56b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav static interface PrintHelperVersionImpl { 57b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 58b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void setScaleMode(int scaleMode); 59b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 60b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public int getScaleMode(); 61b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 62b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Bitmap bitmap); 63b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 64b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Uri imageFile) 65b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav throws FileNotFoundException; 66b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 67b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 68b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 69b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Implementation used when we do not support printing 70b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 71b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav private static final class PrintHelperStubImpl implements PrintHelperVersionImpl { 72b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav int mScaleMode; 73b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 74b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 75b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void setScaleMode(int scaleMode) { 76b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mScaleMode = scaleMode; 77b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 78b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 79b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 80b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public int getScaleMode() { 81b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return mScaleMode; 82b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 83b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 84b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 85b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Bitmap bitmap) { 86b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 87b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 88b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 89b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Uri imageFile) { 90b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 91b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 92b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 93b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 94b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Implementation used on KitKat (and above) 95b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 96b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav private static final class PrintHelperKitkatImpl implements PrintHelperVersionImpl { 97b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav private final PrintHelperKitkat printHelper; 98b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 99b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav PrintHelperKitkatImpl(Context context) { 100b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav printHelper = new PrintHelperKitkat(context); 101b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 102b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 103b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 104b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void setScaleMode(int scaleMode) { 105b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav printHelper.setScaleMode(scaleMode); 106b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 107b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 108b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 109b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public int getScaleMode() { 110b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return printHelper.getScaleMode(); 111b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 112b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 113b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 114b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Bitmap bitmap) { 115b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav printHelper.printBitmap(jobName, bitmap); 116b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 117b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 118b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 119b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException { 120b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav printHelper.printBitmap(jobName, imageFile); 121b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 122b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 123b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 124b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 125b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Returns the PrintHelper that can be used to print images. 126b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 127b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param context A context for accessing system resources. 128b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @return the <code>PrintHelper</code> to support printing images. 129b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 130b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public PrintHelper(Context context) { 131b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav if ("KeyLimePie".equals(Build.VERSION.CODENAME) // TODO recode to be KitKat and above 132b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav || Build.VERSION.SDK_INT > 18) { 133b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mImpl = new PrintHelperKitkatImpl(context); 134b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } else { 135b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mImpl = new PrintHelperStubImpl(); 136b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 137b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 138b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 139b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 140b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Selects whether the image will fill the paper and be cropped 141b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link #SCALE_MODE_FIT} 142b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * or whether the image will be scaled but leave white space 143b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link #SCALE_MODE_FILL}. 144b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 145b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param scaleMode {@link #SCALE_MODE_FIT} or 146b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link #SCALE_MODE_FILL} 147b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 148b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void setScaleMode(int scaleMode) { 149b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mImpl.setScaleMode(scaleMode); 150b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 151b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 152b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 153b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Returns the scale mode with which the image will fill the paper. 154b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 155b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @return The scale Mode: {@link #SCALE_MODE_FIT} or 156b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link #SCALE_MODE_FILL} 157b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 158b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public int getScaleMode() { 159b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return mImpl.getScaleMode(); 160b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 161b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 162b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 163b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Prints a bitmap. 164b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 165b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param jobName The print job name. 166b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param bitmap The bitmap to print. 167b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 168b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Bitmap bitmap) { 169b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mImpl.printBitmap(jobName, bitmap); 170b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 171b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 172b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 173b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Prints an image located at the Uri. Image types supported are those of 174b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream) 175b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * android.graphics.BitmapFactory.decodeStream(java.io.InputStream)} 176b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 177b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param jobName The print job name. 178b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param imageFile The <code>Uri</code> pointing to an image to print. 179b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @throws FileNotFoundException if <code>Uri</code> is not pointing to a valid image. 180b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 181b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException { 182b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mImpl.printBitmap(jobName, imageFile); 183b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 184b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav}