Print photos from photoviewer. b/11532709.

Could really use some fixes to the PrintHelper
library but for now it's "good enough". Bugs filed
for the PrintHelper issues.

Change-Id: I451d84ebac08804a50626d6e413b9b2f27efcf76
diff --git a/res/menu/photo_view_menu.xml b/res/menu/photo_view_menu.xml
index d654d40..86980aa 100644
--- a/res/menu/photo_view_menu.xml
+++ b/res/menu/photo_view_menu.xml
@@ -35,6 +35,10 @@
             android:showAsAction="never"
             android:title="@string/menu_photo_share_all"/>
         <item
+            android:id="@+id/menu_print"
+            android:showAsAction="never"
+            android:title="@string/menu_photo_print"/>
+        <item
             android:id="@+id/menu_download_again"
             android:showAsAction="never"
             android:title="@string/download_again"/>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1167a44..8c92287 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -244,6 +244,8 @@
     <string name="menu_photo_share">Share</string>
     <!-- Photo view screen, button name. Share all attachments. [CHAR LIMIT=20] -->
     <string name="menu_photo_share_all">Share all</string>
+    <!-- Photo view screen, button name. Print photo. [CHAR LIMIT=20] -->
+    <string name="menu_photo_print">Print</string>
     <!-- Displayed in the action bar as a subtitle. Save in progress. [CHAR LIMIT=15] -->
     <string name="saving">Saving&#8230;</string>
 
diff --git a/src/com/android/mail/photo/MailPhotoViewActivity.java b/src/com/android/mail/photo/MailPhotoViewActivity.java
index 560c817..510562f 100644
--- a/src/com/android/mail/photo/MailPhotoViewActivity.java
+++ b/src/com/android/mail/photo/MailPhotoViewActivity.java
@@ -23,6 +23,7 @@
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Parcelable;
+import android.support.v4.print.PrintHelper;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -38,14 +39,18 @@
 import com.android.mail.R;
 import com.android.mail.analytics.Analytics;
 import com.android.mail.browse.AttachmentActionHandler;
+import com.android.mail.print.PrintUtils;
 import com.android.mail.providers.Attachment;
 import com.android.mail.providers.UIProvider;
 import com.android.mail.providers.UIProvider.AttachmentDestination;
 import com.android.mail.providers.UIProvider.AttachmentState;
 import com.android.mail.utils.AttachmentUtils;
+import com.android.mail.utils.LogTag;
+import com.android.mail.utils.LogUtils;
 import com.android.mail.utils.Utils;
 import com.google.common.collect.Lists;
 
+import java.io.FileNotFoundException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -54,10 +59,14 @@
  * to the {@link ActionBar} from the default implementation.
  */
 public class MailPhotoViewActivity extends PhotoViewActivity {
+
+    private static final String LOG_TAG = LogTag.getLogTag();
+
     private MenuItem mSaveItem;
     private MenuItem mSaveAllItem;
     private MenuItem mShareItem;
     private MenuItem mShareAllItem;
+    private MenuItem mPrintItem;
     /**
      * Only for attachments that are currently downloading. Attachments that failed show the
      * retry button.
@@ -128,6 +137,7 @@
         mSaveAllItem = mMenu.findItem(R.id.menu_save_all);
         mShareItem = mMenu.findItem(R.id.menu_share);
         mShareAllItem = mMenu.findItem(R.id.menu_share_all);
+        mPrintItem = mMenu.findItem(R.id.menu_print);
         mDownloadAgainItem = mMenu.findItem(R.id.menu_download_again);
 
         return true;
@@ -146,13 +156,14 @@
      */
     @Override
     protected void updateActionItems() {
-        final boolean runningJellyBeanOrLater = Utils.isRunningJellybeanOrLater();
         final Attachment attachment = getCurrentAttachment();
 
         if (attachment != null && mSaveItem != null && mShareItem != null) {
             mSaveItem.setEnabled(!attachment.isDownloading()
                     && attachment.canSave() && !attachment.isSavedToExternal());
-            mShareItem.setEnabled(attachment.canShare());
+            final boolean canShare = attachment.canShare();
+            mShareItem.setEnabled(canShare);
+            mPrintItem.setEnabled(canShare);
             mDownloadAgainItem.setEnabled(attachment.canSave() && attachment.isDownloading());
         } else {
             if (mMenu != null) {
@@ -184,11 +195,16 @@
             mShareAllItem.setEnabled(enabled);
         }
 
-        // Turn off the functionality that only works on JellyBean.
-        if (!runningJellyBeanOrLater) {
+        // Turn off functionality that only works on JellyBean.
+        if (!Utils.isRunningJellybeanOrLater()) {
             mShareItem.setVisible(false);
             mShareAllItem.setVisible(false);
         }
+
+        // Turn off functionality that only works on KitKat.
+        if (!Utils.isRunningKitkatOrLater()) {
+            mPrintItem.setVisible(false);
+        }
     }
 
     @Override
@@ -214,6 +230,9 @@
         } else if (itemId == R.id.menu_share_all) { // share all of the photos
             shareAllAttachments();
             return true;
+        } else if (itemId == R.id.menu_print) { // print the current photo
+            printAttachment();
+            return true;
         } else if (itemId == R.id.menu_download_again) { // redownload the current photo
             redownloadAttachment();
             return true;
@@ -388,6 +407,20 @@
         mActionHandler.shareAttachments(uris);
     }
 
+    private void printAttachment() {
+        final Attachment attachment = getCurrentAttachment();
+        final Context context = this;
+        final PrintHelper printHelper = new PrintHelper(context);
+        try {
+            printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
+            printHelper.printBitmap(PrintUtils.buildPrintJobName(context, attachment.getName()),
+                    attachment.contentUri);
+        } catch (FileNotFoundException e) {
+            // couldn't print a photo at the particular Uri. Should we notify the user?
+            LogUtils.e(LOG_TAG, e, "Can't print photo");
+        }
+    }
+
     /**
      * Helper method to get the currently visible attachment.
      */
diff --git a/src/com/android/mail/print/PrintUtils.java b/src/com/android/mail/print/PrintUtils.java
index 530e73e..576b5ed 100644
--- a/src/com/android/mail/print/PrintUtils.java
+++ b/src/com/android/mail/print/PrintUtils.java
@@ -71,6 +71,12 @@
         printHtml(context, msgHtml, baseUri, subject, useJavascript);
     }
 
+    public static String buildPrintJobName(Context context, String name) {
+        return TextUtils.isEmpty(name)
+                ? context.getString(R.string.app_name)
+                : context.getString(R.string.print_job_name, name);
+    }
+
     /**
      * Prints the html provided using the framework printing APIs.
      *
@@ -88,9 +94,7 @@
         final PrintManager printManager =
                 (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
 
-        final String printJobName = TextUtils.isEmpty(subject)
-                ? context.getString(R.string.app_name)
-                : context.getString(R.string.print_job_name, subject);
+        final String printJobName = buildPrintJobName(context, subject);
         printManager.print(printJobName,
                 webView.createPrintDocumentAdapter(),
                 new PrintAttributes.Builder().build());