am c648ccd6: am 1b96abe8: am 9c5d6700: Implements Printing in the editor

* commit 'c648ccd6039465d95137bba6e0adc70e04e56c17':
  Implements Printing in the editor
diff --git a/res/menu/filtershow_activity_menu.xml b/res/menu/filtershow_activity_menu.xml
index 1316dd3..bb0b094 100644
--- a/res/menu/filtershow_activity_menu.xml
+++ b/res/menu/filtershow_activity_menu.xml
@@ -39,4 +39,9 @@
         android:showAsAction="never"
         android:visible="true"
         android:title="@string/export_image" />
+    <item
+        android:id="@+id/printButton"
+        android:showAsAction="never"
+        android:visible="true"
+        android:title="@string/print_image"/>
 </menu>
diff --git a/res/values/filtershow_strings.xml b/res/values/filtershow_strings.xml
index 9d4e796..8f4e961 100644
--- a/res/values/filtershow_strings.xml
+++ b/res/values/filtershow_strings.xml
@@ -49,6 +49,8 @@
     <string name="hide_imagestate_panel">Hide Applied Effects</string>
     <!--  Text for the menu item to export a flattened photo[CHAR LIMIT=30] -->
     <string name="export_image">Export</string>
+    <!-- Text for the print menu item [CHAR LIMIT=20] -->
+    <string name="print_image">Print</string>
 
     <!-- Export Dialog-->
 
diff --git a/src/com/android/gallery3d/filtershow/FilterShowActivity.java b/src/com/android/gallery3d/filtershow/FilterShowActivity.java
index af333b6..4f1f7f0 100644
--- a/src/com/android/gallery3d/filtershow/FilterShowActivity.java
+++ b/src/com/android/gallery3d/filtershow/FilterShowActivity.java
@@ -29,11 +29,14 @@
 import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
+import android.graphics.Matrix;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Bundle;
+import android.os.CancellationSignal;
 import android.os.Handler;
 import android.os.IBinder;
 import android.support.v4.app.DialogFragment;
@@ -106,10 +109,13 @@
 import com.android.gallery3d.filtershow.ui.ExportDialog;
 import com.android.gallery3d.filtershow.ui.FramedTextButton;
 import com.android.gallery3d.util.GalleryUtils;
+import com.android.gallery3d.util.PrintJob;
 import com.android.gallery3d.util.UsageStatistics;
 import com.android.photos.data.GalleryBitmapPool;
 
 import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileOutputStream;
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.Vector;
@@ -962,10 +968,19 @@
                 toggleInformationPanel();
                 return true;
             }
+            case R.id.printButton: {
+                print();
+                return true;
+            }
         }
         return false;
     }
 
+    public void print() {
+        Bitmap bitmap = MasterImage.getImage().getHighresImage();
+        PrintJob.printBitmap(this, "ImagePrint", bitmap);
+    }
+
     public void addNewPreset() {
         DialogFragment dialog = new PresetManagementDialog();
         dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
diff --git a/src/com/android/gallery3d/util/PrintJob.java b/src/com/android/gallery3d/util/PrintJob.java
new file mode 100644
index 0000000..d06466a
--- /dev/null
+++ b/src/com/android/gallery3d/util/PrintJob.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.gallery3d.util;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Matrix;
+import android.graphics.Rect;
+import android.graphics.RectF;
+import android.os.Bundle;
+import android.os.CancellationSignal;
+import android.print.PageRange;
+import android.print.PrintAttributes;
+import android.print.PrintDocumentAdapter;
+import android.print.PrintDocumentInfo;
+import android.print.PrintManager;
+import android.print.pdf.PdfDocument;
+import com.android.gallery3d.filtershow.imageshow.MasterImage;
+
+import java.io.FileDescriptor;
+import java.io.FileOutputStream;
+
+public class PrintJob {
+    public static void printBitmap(Context context, final String jobName, final Bitmap bitmap) {
+        PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
+        android.print.PrintJob printJob = printManager.print(jobName,
+                new PrintDocumentAdapter() {
+                    private Rect mPageRect;
+                    private Matrix mPrintMatrix;
+                    private float mDensity;
+                    @Override
+                    public void onLayout(PrintAttributes oldPrintAttributes,
+                                         PrintAttributes newPrintAttributes,
+                                         CancellationSignal cancellationSignal,
+                                         LayoutResultCallback layoutResultCallback, Bundle bundle) {
+
+                        mDensity = Math.max(newPrintAttributes.getResolution().getHorizontalDpi(),
+                                newPrintAttributes.getResolution().getVerticalDpi());
+
+                        float MILS_PER_INCH = 1000f;
+
+                        int pageWidth = (int) (mDensity
+                                * newPrintAttributes.getMediaSize().getWidthMils() / MILS_PER_INCH);
+                        int pageHeight = (int) (mDensity
+                                * newPrintAttributes.getMediaSize().getWidthMils() / MILS_PER_INCH);
+
+                        mPageRect = new Rect(0, 0, pageWidth, pageHeight);
+                        if (newPrintAttributes.getOrientation()
+                                == PrintAttributes.ORIENTATION_LANDSCAPE) {
+                            mPageRect = new Rect(0, 0, pageHeight, pageWidth);
+                        }
+
+                        PrintDocumentInfo info = new PrintDocumentInfo.Builder(jobName)
+                                .setContentType(PrintDocumentInfo.CONTENT_TYPE_PHOTO)
+                                .setPageCount(1)
+                                .create();
+                        layoutResultCallback.onLayoutFinished(info, false);
+                    }
+
+                    @Override
+                    public void onWrite(PageRange[] pageRanges, FileDescriptor fileDescriptor,
+                                        CancellationSignal cancellationSignal,
+                                        WriteResultCallback writeResultCallback) {
+                        PdfDocument mPdfDocument = PdfDocument.open();
+                        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(
+                                mPageRect, 1, (int) mDensity).create();
+                        PdfDocument.Page page = mPdfDocument.startPage(pageInfo);
+                        mPrintMatrix = new Matrix();
+                        mPrintMatrix.setRectToRect(
+                                new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()),
+                                new RectF(mPageRect),
+                                Matrix.ScaleToFit.CENTER);
+                        page.getCanvas().drawBitmap(bitmap, mPrintMatrix, null);
+                        mPdfDocument.finishPage(page);
+                        mPdfDocument.writeTo(new FileOutputStream(fileDescriptor));
+                        mPdfDocument.close();
+                        writeResultCallback.onWriteFinished(
+                                new PageRange[] { PageRange.ALL_PAGES });
+                    }
+                }, new PrintAttributes.Builder().create());
+
+    }
+
+}