Home | History | Annotate | Download | only in print
      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 
     17 package android.support.v4.print;
     18 
     19 import android.content.Context;
     20 import android.graphics.Bitmap;
     21 import android.os.Build;
     22 import android.net.Uri;
     23 
     24 import java.io.FileNotFoundException;
     25 
     26 /**
     27  * Helper for printing bitmaps.
     28  */
     29 public 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     /**
     36      * image will fill the paper and be cropped (default)
     37      */
     38     public static final int SCALE_MODE_FILL = 2;
     39 
     40     /**
     41      * this is a black and white image
     42      */
     43     public static final int COLOR_MODE_MONOCHROME = 1;
     44 
     45     /**
     46      * this is a color image (default)
     47      */
     48     public static final int COLOR_MODE_COLOR = 2;
     49 
     50     /**
     51      * Print the image in landscape orientation (default).
     52      */
     53     public static final int ORIENTATION_LANDSCAPE = 1;
     54 
     55     /**
     56      * Print the image in  portrait orientation.
     57      */
     58     public static final int ORIENTATION_PORTRAIT = 2;
     59 
     60     PrintHelperVersionImpl mImpl;
     61 
     62     /**
     63      * Gets whether the system supports printing.
     64      *
     65      * @return True if printing is supported.
     66      */
     67     public static boolean systemSupportsPrint() {
     68         if (Build.VERSION.SDK_INT >= 19) {
     69             // Supported on Android 4.4 or later.
     70             return true;
     71         }
     72         return false;
     73     }
     74 
     75     /**
     76      * Interface implemented by classes that support printing
     77      */
     78     static interface PrintHelperVersionImpl {
     79 
     80         public void setScaleMode(int scaleMode);
     81 
     82         public int getScaleMode();
     83 
     84         public void setColorMode(int colorMode);
     85 
     86         public int getColorMode();
     87 
     88         public void setOrientation(int orientation);
     89 
     90         public int getOrientation();
     91 
     92         public void printBitmap(String jobName, Bitmap bitmap);
     93 
     94         public void printBitmap(String jobName, Uri imageFile)
     95                 throws FileNotFoundException;
     96     }
     97 
     98     /**
     99      * Implementation used when we do not support printing
    100      */
    101     private static final class PrintHelperStubImpl implements PrintHelperVersionImpl {
    102         int mScaleMode = SCALE_MODE_FILL;
    103         int mColorMode = COLOR_MODE_COLOR;
    104         int mOrientation = ORIENTATION_LANDSCAPE;
    105         @Override
    106         public void setScaleMode(int scaleMode) {
    107             mScaleMode = scaleMode;
    108         }
    109 
    110         @Override
    111         public int getColorMode() {
    112             return mColorMode;
    113         }
    114 
    115         @Override
    116         public void setColorMode(int colorMode) {
    117             mColorMode = colorMode;
    118         }
    119 
    120         @Override
    121         public void setOrientation(int orientation) { mOrientation = orientation; }
    122 
    123         @Override
    124         public int getOrientation() { return mOrientation; }
    125 
    126         @Override
    127         public int getScaleMode() {
    128             return mScaleMode;
    129         }
    130 
    131         @Override
    132         public void printBitmap(String jobName, Bitmap bitmap) {
    133         }
    134 
    135         @Override
    136         public void printBitmap(String jobName, Uri imageFile) {
    137         }
    138     }
    139 
    140     /**
    141      * Implementation used on KitKat (and above)
    142      */
    143     private static final class PrintHelperKitkatImpl implements PrintHelperVersionImpl {
    144         private final PrintHelperKitkat mPrintHelper;
    145 
    146         PrintHelperKitkatImpl(Context context) {
    147             mPrintHelper = new PrintHelperKitkat(context);
    148         }
    149 
    150         @Override
    151         public void setScaleMode(int scaleMode) {
    152             mPrintHelper.setScaleMode(scaleMode);
    153         }
    154 
    155         @Override
    156         public int getScaleMode() {
    157             return mPrintHelper.getScaleMode();
    158         }
    159 
    160         @Override
    161         public void setColorMode(int colorMode) {
    162             mPrintHelper.setColorMode(colorMode);
    163         }
    164 
    165         @Override
    166         public int getColorMode() {
    167             return mPrintHelper.getColorMode();
    168         }
    169 
    170         @Override
    171         public void setOrientation(int orientation) {
    172             mPrintHelper.setOrientation(orientation);
    173         }
    174 
    175         @Override
    176         public int getOrientation() {
    177             return mPrintHelper.getOrientation();
    178         }
    179 
    180         @Override
    181         public void printBitmap(String jobName, Bitmap bitmap) {
    182             mPrintHelper.printBitmap(jobName, bitmap);
    183         }
    184 
    185         @Override
    186         public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException {
    187             mPrintHelper.printBitmap(jobName, imageFile);
    188         }
    189     }
    190 
    191     /**
    192      * Returns the PrintHelper that can be used to print images.
    193      *
    194      * @param context A context for accessing system resources.
    195      * @return the <code>PrintHelper</code> to support printing images.
    196      */
    197     public PrintHelper(Context context) {
    198         if (systemSupportsPrint()) {
    199             mImpl = new PrintHelperKitkatImpl(context);
    200         } else {
    201             mImpl = new PrintHelperStubImpl();
    202         }
    203     }
    204 
    205     /**
    206      * Selects whether the image will fill the paper and be cropped
    207      * {@link #SCALE_MODE_FIT}
    208      * or whether the image will be scaled but leave white space
    209      * {@link #SCALE_MODE_FILL}.
    210      *
    211      * @param scaleMode {@link #SCALE_MODE_FIT} or
    212      *                  {@link #SCALE_MODE_FILL}
    213      */
    214     public void setScaleMode(int scaleMode) {
    215         mImpl.setScaleMode(scaleMode);
    216     }
    217 
    218     /**
    219      * Returns the scale mode with which the image will fill the paper.
    220      *
    221      * @return The scale Mode: {@link #SCALE_MODE_FIT} or
    222      * {@link #SCALE_MODE_FILL}
    223      */
    224     public int getScaleMode() {
    225         return mImpl.getScaleMode();
    226     }
    227 
    228     /**
    229      * Sets whether the image will be printed in color (default)
    230      * {@link #COLOR_MODE_COLOR} or in back and white
    231      * {@link #COLOR_MODE_MONOCHROME}.
    232      *
    233      * @param colorMode The color mode which is one of
    234      * {@link #COLOR_MODE_COLOR} and {@link #COLOR_MODE_MONOCHROME}.
    235      */
    236     public void setColorMode(int colorMode) {
    237         mImpl.setColorMode(colorMode);
    238     }
    239 
    240     /**
    241      * Gets the color mode with which the image will be printed.
    242      *
    243      * @return The color mode which is one of {@link #COLOR_MODE_COLOR}
    244      * and {@link #COLOR_MODE_MONOCHROME}.
    245      */
    246     public int getColorMode() {
    247         return mImpl.getColorMode();
    248     }
    249 
    250     /**
    251      * Sets whether the image will be printed in landscape {@link #ORIENTATION_LANDSCAPE} (default)
    252      * or portrait {@link #ORIENTATION_PORTRAIT}.
    253      *
    254      * @param orientation The page orientation which is one of
    255      *                    {@link #ORIENTATION_LANDSCAPE} or {@link #ORIENTATION_PORTRAIT}.
    256      */
    257     public void setOrientation(int orientation) {
    258         mImpl.setOrientation(orientation);
    259     }
    260 
    261     /**
    262      * Gets whether the image will be printed in landscape or portrait.
    263      *
    264      * @return The page orientation which is one of
    265      * {@link #ORIENTATION_LANDSCAPE} or {@link #ORIENTATION_PORTRAIT}.
    266      */
    267     public int getOrientation() {
    268         return mImpl.getOrientation();
    269     }
    270 
    271     /**
    272      * Prints a bitmap.
    273      *
    274      * @param jobName The print job name.
    275      * @param bitmap  The bitmap to print.
    276      */
    277     public void printBitmap(String jobName, Bitmap bitmap) {
    278         mImpl.printBitmap(jobName, bitmap);
    279     }
    280 
    281     /**
    282      * Prints an image located at the Uri. Image types supported are those of
    283      * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
    284      * android.graphics.BitmapFactory.decodeStream(java.io.InputStream)}
    285      *
    286      * @param jobName   The print job name.
    287      * @param imageFile The <code>Uri</code> pointing to an image to print.
    288      * @throws FileNotFoundException if <code>Uri</code> is not pointing to a valid image.
    289      */
    290     public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException {
    291         mImpl.printBitmap(jobName, imageFile);
    292     }
    293 }