From 6ede90a790ce08330af58f81474c103ee1ca438f Mon Sep 17 00:00:00 2001 From: Mert Tumer Date: Sun, 12 Aug 2018 05:58:38 -0700 Subject: tdf#89860 ability to print from Android Viewer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I13c7f3e085095f1c0d88ab3668557fcc1c4cb23a Signed-off-by: Mert Tumer Reviewed-on: https://gerrit.libreoffice.org/58900 Reviewed-by: Tomaž Vajngerl Tested-by: Tomaž Vajngerl --- android/source/res/menu/main.xml | 5 ++ android/source/res/values/strings.xml | 1 + .../java/org/libreoffice/LOKitTileProvider.java | 23 +++++- .../java/org/libreoffice/PDFDocumentAdapter.java | 86 ++++++++++++++++++++++ .../java/org/libreoffice/ToolbarController.java | 3 + 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 android/source/src/java/org/libreoffice/PDFDocumentAdapter.java (limited to 'android') diff --git a/android/source/res/menu/main.xml b/android/source/res/menu/main.xml index 257a5844b3b3..d5f1ae8e4274 100644 --- a/android/source/res/menu/main.xml +++ b/android/source/res/menu/main.xml @@ -42,6 +42,11 @@ android:visible="true" /> + + diff --git a/android/source/res/values/strings.xml b/android/source/res/values/strings.xml index fa83cf4a74b3..17e52316bdb4 100644 --- a/android/source/res/values/strings.xml +++ b/android/source/res/values/strings.xml @@ -210,4 +210,5 @@ Value Parent Value Export To PDF + Print diff --git a/android/source/src/java/org/libreoffice/LOKitTileProvider.java b/android/source/src/java/org/libreoffice/LOKitTileProvider.java index 2815b839ad5c..7464f152698d 100644 --- a/android/source/src/java/org/libreoffice/LOKitTileProvider.java +++ b/android/source/src/java/org/libreoffice/LOKitTileProvider.java @@ -8,9 +8,14 @@ */ package org.libreoffice; +import android.content.Context; import android.graphics.Bitmap; import android.graphics.PointF; +import android.os.Build; import android.os.Environment; +import android.print.PrintAttributes; +import android.print.PrintDocumentAdapter; +import android.print.PrintManager; import android.util.Log; import android.view.KeyEvent; import android.widget.Toast; @@ -372,11 +377,27 @@ class LOKitTileProvider implements TileProvider { String cacheFile = mContext.getExternalCacheDir().getAbsolutePath() + "/" + file; mDocument.saveAs("file://"+cacheFile,"pdf",""); - //TODO PRINT + printDocument(cacheFile); }else{ saveDocumentAs(dir+"/"+file,"pdf"); } } + + private void printDocument(String cacheFile) { + if (Build.VERSION.SDK_INT >= 19) { + try { + PrintManager printManager = (PrintManager) mContext.getSystemService(Context.PRINT_SERVICE); + PrintDocumentAdapter printAdapter = new PDFDocumentAdapter(mContext, cacheFile); + printManager.print("Document", printAdapter, new PrintAttributes.Builder().build()); + + } catch (Exception e) { + e.printStackTrace(); + } + } else { + mContext.showCustomStatusMessage("Your device does not support printing"); + } + } + public boolean isDocumentCached(){ File input = new File(mInputFile); final String cacheFile = mContext.getExternalCacheDir().getAbsolutePath() + "/lo_cached_" + input.getName(); diff --git a/android/source/src/java/org/libreoffice/PDFDocumentAdapter.java b/android/source/src/java/org/libreoffice/PDFDocumentAdapter.java new file mode 100644 index 000000000000..2ce167ce3a32 --- /dev/null +++ b/android/source/src/java/org/libreoffice/PDFDocumentAdapter.java @@ -0,0 +1,86 @@ +package org.libreoffice; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Bundle; +import android.os.CancellationSignal; +import android.os.ParcelFileDescriptor; +import android.print.PageRange; +import android.print.PrintAttributes; +import android.print.PrintDocumentAdapter; +import android.print.PrintDocumentInfo; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +@TargetApi(19) +public class PDFDocumentAdapter extends PrintDocumentAdapter{ + Context mContext; + String pdfFile; + + public PDFDocumentAdapter(Context mContext, String pdfFile) { + this.mContext = mContext; + this.pdfFile = pdfFile; + } + + @Override + public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) { + if (cancellationSignal.isCanceled()) { + callback.onLayoutCancelled(); + } + else { + File f = new File(pdfFile); + PrintDocumentInfo.Builder builder= + new PrintDocumentInfo.Builder(f.getName()); + builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT) + .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN) + .build(); + callback.onLayoutFinished(builder.build(), + !newAttributes.equals(oldAttributes)); + } + } + + @Override + public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) { + InputStream in=null; + OutputStream out=null; + try { + File file = new File(pdfFile); + in = new FileInputStream(file); + out=new FileOutputStream(destination.getFileDescriptor()); + + byte[] buf=new byte[in.available()]; + int size; + + while ((size=in.read(buf)) >= 0 + && !cancellationSignal.isCanceled()) { + out.write(buf, 0, size); + } + + if (cancellationSignal.isCanceled()) { + callback.onWriteCancelled(); + } + else { + callback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES }); + } + } + catch (Exception e) { + callback.onWriteFailed(e.getMessage()); + e.printStackTrace(); + } + finally { + try { + in.close(); + out.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + } +} \ No newline at end of file diff --git a/android/source/src/java/org/libreoffice/ToolbarController.java b/android/source/src/java/org/libreoffice/ToolbarController.java index 8aa638e12b2f..29d5433df057 100644 --- a/android/source/src/java/org/libreoffice/ToolbarController.java +++ b/android/source/src/java/org/libreoffice/ToolbarController.java @@ -174,6 +174,9 @@ public class ToolbarController implements Toolbar.OnMenuItemClickListener { case R.id.action_exportToPDF: mContext.getTileProvider().exportToPDF(false); return true; + case R.id.action_print: + mContext.getTileProvider().exportToPDF(true); + return true; case R.id.action_settings: mContext.showSettings(); return true; -- cgit