OSDN Git Service

Updated Gallery to avoid breaking the build - API change
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / util / PrintJob.java
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 com.android.gallery3d.util;
18
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Matrix;
22 import android.graphics.RectF;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.os.CancellationSignal;
26 import android.os.ParcelFileDescriptor;
27 import android.print.PageRange;
28 import android.print.PrintAttributes;
29 import android.print.PrintDocumentAdapter;
30 import android.print.PrintDocumentInfo;
31 import android.print.PrintManager;
32 import android.print.pdf.PdfDocument.Page;
33 import android.print.pdf.PrintedPdfDocument;
34
35 import com.android.gallery3d.filtershow.cache.ImageLoader;
36
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39
40 public class PrintJob {
41     private final static int MAX_PRINT_SIZE = 2048;
42
43     public static void printBitmap(final Context context, final String jobName,
44             final Bitmap bitmap) {
45         if (bitmap == null) {
46             return;
47         }
48         PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
49         printManager.print(jobName,
50                 new PrintDocumentAdapter() {
51                     private PrintAttributes mAttributes;
52
53                     @Override
54                     public void onLayout(PrintAttributes oldPrintAttributes,
55                                          PrintAttributes newPrintAttributes,
56                                          CancellationSignal cancellationSignal,
57                                          LayoutResultCallback layoutResultCallback,
58                                          Bundle bundle) {
59
60                         mAttributes = newPrintAttributes;
61
62                         PrintDocumentInfo info = new PrintDocumentInfo
63                                 .Builder(jobName, newPrintAttributes)
64                                 .setContentType(PrintDocumentInfo.CONTENT_TYPE_PHOTO)
65                                 .setColorMode(PrintAttributes.COLOR_MODE_COLOR)
66                                 .setPageCount(1)
67                                 .create();
68
69                         layoutResultCallback.onLayoutFinished(info, false);
70                     }
71
72                     @Override
73                     public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor fileDescriptor,
74                                         CancellationSignal cancellationSignal,
75                                         WriteResultCallback writeResultCallback) {
76                         try {
77                             PrintedPdfDocument pdfDocument = PrintedPdfDocument.open(context,
78                                     mAttributes);
79                             Page page = pdfDocument.startPage(1);
80
81                             RectF content = new RectF(page.getInfo().getContentSize());
82                             Matrix matrix = new Matrix();
83
84                             // Handle orientation.
85                             if (mAttributes.getOrientation()
86                                     == PrintAttributes.ORIENTATION_LANDSCAPE) {
87                                 // Compute and apply scale based on fitting mode.
88                                 final float scale;
89                                 switch (mAttributes.getFittingMode()) {
90                                     case PrintAttributes.FITTING_MODE_SCALE_TO_FILL: {
91                                         scale = Math.max(content.width() / bitmap.getHeight(),
92                                                 content.height() / bitmap.getWidth());
93                                     } break;
94
95                                     case PrintAttributes.FITTING_MODE_SCALE_TO_FIT: {
96                                         scale = Math.min(content.width() / bitmap.getHeight(),
97                                                 content.height() / bitmap.getWidth());
98                                     } break;
99
100                                     default: {
101                                         scale = 1.0f;
102                                     }
103                                 }
104                                 matrix.postScale(scale, scale);
105
106                                 // Apply the rotation.
107                                 matrix.postRotate(90);
108                                 matrix.postTranslate(bitmap.getHeight() * scale, 0);
109
110                                 // Center the content.
111                                 final float translateX = (content.width()
112                                         - bitmap.getHeight() * scale) / 2;
113                                 final float translateY = (content.height()
114                                         - bitmap.getWidth() * scale) / 2;
115                                 matrix.postTranslate(translateX, translateY);
116                             } else {
117                                 // Compute and apply scale based on fitting mode.
118                                 float scale = 1.0f;
119                                 switch (mAttributes.getFittingMode()) {
120                                     case PrintAttributes.FITTING_MODE_SCALE_TO_FILL: {
121                                         scale = Math.max(content.width() / bitmap.getWidth(),
122                                                 content.height() / bitmap.getHeight());
123                                     } break;
124
125                                     case PrintAttributes.FITTING_MODE_SCALE_TO_FIT: {
126                                         scale = Math.min(content.width() / bitmap.getWidth(),
127                                                 content.height() / bitmap.getHeight());
128                                     } break;
129                                 }
130                                 matrix.postScale(scale, scale);
131
132                                 // Center the content.
133                                 final float translateX = (content.width()
134                                         - bitmap.getWidth() * scale) / 2;
135                                 final float translateY = (content.height()
136                                         - bitmap.getHeight() * scale) / 2;
137                                 matrix.postTranslate(translateX, translateY);
138                             }
139
140                             // Draw the bitmap.
141                             page.getCanvas().drawBitmap(bitmap, matrix, null);
142
143                             // Write the document.
144                             pdfDocument.finishPage(page);
145                             pdfDocument.writeTo(new FileOutputStream(
146                                     fileDescriptor.getFileDescriptor()));
147                             pdfDocument.close();
148
149                             // Done.
150                             writeResultCallback.onWriteFinished(
151                                     new PageRange[] { PageRange.ALL_PAGES });
152                         } finally {
153                             if (fileDescriptor != null) {
154                                 try {
155                                     fileDescriptor.close();
156                                 } catch (IOException ioe) {
157                                     /* ignore */
158                                 }
159                             }
160                             writeResultCallback.onWriteFailed(null);
161                         }
162                     }
163                 }, new PrintAttributes.Builder().create());
164     }
165
166     public static void printBitmapAtUri(Context context, String imagePrint, Uri uri) {
167         // TODO: load full size images. For now, it's better to constrain ourselves.
168         Bitmap bitmap = ImageLoader.loadConstrainedBitmap(uri, context, MAX_PRINT_SIZE, null, false);
169         printBitmap(context, imagePrint, bitmap);
170     }
171 }