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; 22215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann 23b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavimport android.net.Uri; 24b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 25b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavimport java.io.FileNotFoundException; 26b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 27b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav/** 28b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Helper for printing bitmaps. 29b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 30b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslavpublic final class PrintHelper { 31b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 32b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * image will be scaled but leave white space 33b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 34b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public static final int SCALE_MODE_FIT = 1; 3509ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 36b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 37b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * image will fill the paper and be cropped (default) 38b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 39b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public static final int SCALE_MODE_FILL = 2; 4009ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 4109ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov /** 4209ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * this is a black and white image 4309ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov */ 4409ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public static final int COLOR_MODE_MONOCHROME = 1; 4509ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 4609ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov /** 4709ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * this is a color image (default) 4809ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov */ 4909ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public static final int COLOR_MODE_COLOR = 2; 5009ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 51975c49f182d754dfe1a38ba0457d6e603b125570John Hoford /** 52975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * Print the image in landscape orientation (default). 53975c49f182d754dfe1a38ba0457d6e603b125570John Hoford */ 54975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public static final int ORIENTATION_LANDSCAPE = 1; 55975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 56975c49f182d754dfe1a38ba0457d6e603b125570John Hoford /** 57975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * Print the image in portrait orientation. 58975c49f182d754dfe1a38ba0457d6e603b125570John Hoford */ 59975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public static final int ORIENTATION_PORTRAIT = 2; 60975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 61b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov /** 62b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * Callback for observing when a print operation is completed. 63b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * When print is finished either the system acquired the 64b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * document to print or printing was cancelled. 65b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov */ 66b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public interface OnPrintFinishCallback { 67b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov 68b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov /** 69b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * Called when a print operation is finished. 70b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov */ 71b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void onFinish(); 72b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov } 73b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov 74b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav PrintHelperVersionImpl mImpl; 75b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 76b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 77b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Gets whether the system supports printing. 78b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 79b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @return True if printing is supported. 80b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 81b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public static boolean systemSupportsPrint() { 82b07ca601bba6ed850b7cf9fedcd44158d78a721fDianne Hackborn if (Build.VERSION.SDK_INT >= 19) { 83b07ca601bba6ed850b7cf9fedcd44158d78a721fDianne Hackborn // Supported on Android 4.4 or later. 84b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return true; 85b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 86b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return false; 87b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 88b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 89b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 90b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Interface implemented by classes that support printing 91b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 92b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav static interface PrintHelperVersionImpl { 93b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 94b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void setScaleMode(int scaleMode); 95b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 96b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public int getScaleMode(); 97b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 9809ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public void setColorMode(int colorMode); 9909ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 10009ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public int getColorMode(); 10109ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 102975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public void setOrientation(int orientation); 103975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 104975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public int getOrientation(); 105975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 106b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void printBitmap(String jobName, Bitmap bitmap, OnPrintFinishCallback callback); 107b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 108b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void printBitmap(String jobName, Uri imageFile, OnPrintFinishCallback callback) 109b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav throws FileNotFoundException; 110b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 111b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 112b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 113b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Implementation used when we do not support printing 114b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 115b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav private static final class PrintHelperStubImpl implements PrintHelperVersionImpl { 11609ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov int mScaleMode = SCALE_MODE_FILL; 11709ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov int mColorMode = COLOR_MODE_COLOR; 118975c49f182d754dfe1a38ba0457d6e603b125570John Hoford int mOrientation = ORIENTATION_LANDSCAPE; 119b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 120b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void setScaleMode(int scaleMode) { 121b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mScaleMode = scaleMode; 122b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 123b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 124b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 12509ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public int getColorMode() { 12609ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov return mColorMode; 12709ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov } 12809ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 12909ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov @Override 13009ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public void setColorMode(int colorMode) { 13109ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov mColorMode = colorMode; 13209ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov } 13309ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 13409ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov @Override 135975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public void setOrientation(int orientation) { mOrientation = orientation; } 136975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 137975c49f182d754dfe1a38ba0457d6e603b125570John Hoford @Override 138975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public int getOrientation() { return mOrientation; } 139975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 140975c49f182d754dfe1a38ba0457d6e603b125570John Hoford @Override 141b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public int getScaleMode() { 142b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return mScaleMode; 143b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 144b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 145b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 146b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void printBitmap(String jobName, Bitmap bitmap, OnPrintFinishCallback callback) { 147b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 148b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 149b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 150b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void printBitmap(String jobName, Uri imageFile, OnPrintFinishCallback callback) { 151b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 152b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 153b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 154b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 155215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann * Generic implementation for KitKat to Api24 156b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 157215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann private static class PrintHelperImpl<RealHelper extends PrintHelperKitkat> 158215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann implements PrintHelperVersionImpl { 159215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann private final RealHelper mPrintHelper; 160b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 161215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann protected PrintHelperImpl(RealHelper helper) { 162215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann mPrintHelper = helper; 163b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 164b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 165b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 166b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void setScaleMode(int scaleMode) { 167975c49f182d754dfe1a38ba0457d6e603b125570John Hoford mPrintHelper.setScaleMode(scaleMode); 168b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 169b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 170b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 171b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public int getScaleMode() { 172975c49f182d754dfe1a38ba0457d6e603b125570John Hoford return mPrintHelper.getScaleMode(); 173b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 174b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 175b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 17609ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public void setColorMode(int colorMode) { 177975c49f182d754dfe1a38ba0457d6e603b125570John Hoford mPrintHelper.setColorMode(colorMode); 17809ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov } 17909ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 18009ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov @Override 18109ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public int getColorMode() { 182975c49f182d754dfe1a38ba0457d6e603b125570John Hoford return mPrintHelper.getColorMode(); 183975c49f182d754dfe1a38ba0457d6e603b125570John Hoford } 184975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 185975c49f182d754dfe1a38ba0457d6e603b125570John Hoford @Override 186975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public void setOrientation(int orientation) { 187975c49f182d754dfe1a38ba0457d6e603b125570John Hoford mPrintHelper.setOrientation(orientation); 188975c49f182d754dfe1a38ba0457d6e603b125570John Hoford } 189975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 190975c49f182d754dfe1a38ba0457d6e603b125570John Hoford @Override 191975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public int getOrientation() { 192975c49f182d754dfe1a38ba0457d6e603b125570John Hoford return mPrintHelper.getOrientation(); 19309ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov } 19409ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 19509ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov @Override 196b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void printBitmap(String jobName, Bitmap bitmap, 197b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov final OnPrintFinishCallback callback) { 198215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann RealHelper.OnPrintFinishCallback delegateCallback = null; 199f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov if (callback != null) { 200215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann delegateCallback = new RealHelper.OnPrintFinishCallback() { 201f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov @Override 202f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov public void onFinish() { 203f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov callback.onFinish(); 204f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov } 205f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov }; 206f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov } 207f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov mPrintHelper.printBitmap(jobName, bitmap, delegateCallback); 208b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 209b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 210b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav @Override 211b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void printBitmap(String jobName, Uri imageFile, 212b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov final OnPrintFinishCallback callback) throws FileNotFoundException { 213215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann RealHelper.OnPrintFinishCallback delegateCallback = null; 214f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov if (callback != null) { 215215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann delegateCallback = new RealHelper.OnPrintFinishCallback() { 216f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov @Override 217f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov public void onFinish() { 218f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov callback.onFinish(); 219f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov } 220f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov }; 221f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov } 222f858b86418fc586d681e47c994d81f11c9d12ec2Svet Ganov mPrintHelper.printBitmap(jobName, imageFile, delegateCallback); 223b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 224b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 225b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 226b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 227215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann * Implementation used on KitKat 228215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann */ 229215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann private static final class PrintHelperKitkatImpl extends PrintHelperImpl<PrintHelperKitkat> { 230215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann PrintHelperKitkatImpl(Context context) { 231215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann super(new PrintHelperKitkat(context)); 232215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } 233215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } 234215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann 235215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann /** 23673cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann * Implementation used on Api20 to Api22 237215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann */ 238215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann private static final class PrintHelperApi20Impl extends PrintHelperImpl<PrintHelperApi20> { 239215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann PrintHelperApi20Impl(Context context) { 240215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann super(new PrintHelperApi20(context)); 241215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } 242215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } 243215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann 244215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann /** 24573cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann * Implementation used on Api23 24673cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann */ 24773cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann private static final class PrintHelperApi23Impl extends PrintHelperImpl<PrintHelperApi23> { 24873cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann PrintHelperApi23Impl(Context context) { 24973cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann super(new PrintHelperApi23(context)); 25073cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann } 25173cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann } 25273cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann 25373cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann 25473cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann /** 255215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann * Implementation used on Api24 and above 256215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann */ 257215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann private static final class PrintHelperApi24Impl extends PrintHelperImpl<PrintHelperApi24> { 258215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann PrintHelperApi24Impl(Context context) { 259215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann super(new PrintHelperApi24(context)); 260215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } 261215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } 262215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann 263215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann /** 264b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Returns the PrintHelper that can be used to print images. 265b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 266b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param context A context for accessing system resources. 267b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @return the <code>PrintHelper</code> to support printing images. 268b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 269b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public PrintHelper(Context context) { 2705030ecfe56689afe327c82bb8a197999389dee13Svetoslav Ganov if (systemSupportsPrint()) { 2719fd603efd337034bb96e9a8bcc69775665fc41eaPhilip P. Moltmann if (Build.VERSION.SDK_INT >= 24) { 272215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann mImpl = new PrintHelperApi24Impl(context); 27373cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann } else if (Build.VERSION.SDK_INT >= 23) { 27473cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann mImpl = new PrintHelperApi23Impl(context); 275215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } else if (Build.VERSION.SDK_INT >= 20) { 276215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann mImpl = new PrintHelperApi20Impl(context); 277215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } else { 278215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann mImpl = new PrintHelperKitkatImpl(context); 279215440785861d37af3c6056be63ec2dd8274c452Philip P. Moltmann } 280b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } else { 281b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mImpl = new PrintHelperStubImpl(); 282b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 283b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 284b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 285b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 286b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Selects whether the image will fill the paper and be cropped 287b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link #SCALE_MODE_FIT} 288b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * or whether the image will be scaled but leave white space 289b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link #SCALE_MODE_FILL}. 290b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 291b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param scaleMode {@link #SCALE_MODE_FIT} or 292b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link #SCALE_MODE_FILL} 293b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 294b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void setScaleMode(int scaleMode) { 295b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav mImpl.setScaleMode(scaleMode); 296b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 297b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 298b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 299b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Returns the scale mode with which the image will fill the paper. 300b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 301b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @return The scale Mode: {@link #SCALE_MODE_FIT} or 302b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link #SCALE_MODE_FILL} 303b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 304b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public int getScaleMode() { 305b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav return mImpl.getScaleMode(); 306b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 307b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 308b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 30909ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * Sets whether the image will be printed in color (default) 31009ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * {@link #COLOR_MODE_COLOR} or in back and white 31109ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * {@link #COLOR_MODE_MONOCHROME}. 31209ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * 31309ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * @param colorMode The color mode which is one of 31409ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * {@link #COLOR_MODE_COLOR} and {@link #COLOR_MODE_MONOCHROME}. 31509ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov */ 31609ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public void setColorMode(int colorMode) { 31709ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov mImpl.setColorMode(colorMode); 31809ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov } 31909ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 32009ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov /** 32109ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * Gets the color mode with which the image will be printed. 32209ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * 32309ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * @return The color mode which is one of {@link #COLOR_MODE_COLOR} 32409ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov * and {@link #COLOR_MODE_MONOCHROME}. 32509ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov */ 32609ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov public int getColorMode() { 32709ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov return mImpl.getColorMode(); 32809ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov } 32909ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov 33009ad64345349f27bdeb53c536f178e46bb7ce5acSvetoslav Ganov /** 331975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * Sets whether the image will be printed in landscape {@link #ORIENTATION_LANDSCAPE} (default) 332975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * or portrait {@link #ORIENTATION_PORTRAIT}. 333975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * 334975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * @param orientation The page orientation which is one of 335975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * {@link #ORIENTATION_LANDSCAPE} or {@link #ORIENTATION_PORTRAIT}. 336975c49f182d754dfe1a38ba0457d6e603b125570John Hoford */ 337975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public void setOrientation(int orientation) { 338975c49f182d754dfe1a38ba0457d6e603b125570John Hoford mImpl.setOrientation(orientation); 339975c49f182d754dfe1a38ba0457d6e603b125570John Hoford } 340975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 341975c49f182d754dfe1a38ba0457d6e603b125570John Hoford /** 342975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * Gets whether the image will be printed in landscape or portrait. 343975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * 344975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * @return The page orientation which is one of 345975c49f182d754dfe1a38ba0457d6e603b125570John Hoford * {@link #ORIENTATION_LANDSCAPE} or {@link #ORIENTATION_PORTRAIT}. 346975c49f182d754dfe1a38ba0457d6e603b125570John Hoford */ 347975c49f182d754dfe1a38ba0457d6e603b125570John Hoford public int getOrientation() { 348975c49f182d754dfe1a38ba0457d6e603b125570John Hoford return mImpl.getOrientation(); 349975c49f182d754dfe1a38ba0457d6e603b125570John Hoford } 350975c49f182d754dfe1a38ba0457d6e603b125570John Hoford 351b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov 352975c49f182d754dfe1a38ba0457d6e603b125570John Hoford /** 353b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Prints a bitmap. 354b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 355b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param jobName The print job name. 356b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param bitmap The bitmap to print. 357b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 358b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Bitmap bitmap) { 359b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov mImpl.printBitmap(jobName, bitmap, null); 360b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov } 361b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov 362b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov /** 363b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * Prints a bitmap. 364b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * 365b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * @param jobName The print job name. 366b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * @param bitmap The bitmap to print. 367b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * @param callback Optional callback to observe when printing is finished. 368b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov */ 369b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void printBitmap(String jobName, Bitmap bitmap, OnPrintFinishCallback callback) { 370b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov mImpl.printBitmap(jobName, bitmap, callback); 371b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 372b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav 373b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav /** 374b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * Prints an image located at the Uri. Image types supported are those of 375b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream) 376b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * android.graphics.BitmapFactory.decodeStream(java.io.InputStream)} 377b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * 378b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param jobName The print job name. 379b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @param imageFile The <code>Uri</code> pointing to an image to print. 380b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav * @throws FileNotFoundException if <code>Uri</code> is not pointing to a valid image. 381b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav */ 382b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException { 383b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov mImpl.printBitmap(jobName, imageFile, null); 384b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov } 385b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov 386b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov /** 387b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * Prints an image located at the Uri. Image types supported are those of 388b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream) 389b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * android.graphics.BitmapFactory.decodeStream(java.io.InputStream)} 390b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * 391b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * @param jobName The print job name. 392b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * @param imageFile The <code>Uri</code> pointing to an image to print. 393b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * @throws FileNotFoundException if <code>Uri</code> is not pointing to a valid image. 394b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov * @param callback Optional callback to observe when printing is finished. 395b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov */ 396b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov public void printBitmap(String jobName, Uri imageFile, OnPrintFinishCallback callback) 397b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov throws FileNotFoundException { 398b4eb9c984f1c6ac8007c74fab239437cf9f6b474Svet Ganov mImpl.printBitmap(jobName, imageFile, callback); 399b363776d911abae9d067b9ef77fccc1c3c56e652Svetoslav } 40073cad7b8c14ee0003a958e9a30be11e4ecaf304dPhilip P. Moltmann} 401