diff options
author | Mert Tumer <merttumer@outlook.com> | 2018-08-12 05:58:38 -0700 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2018-08-15 21:02:31 +0200 |
commit | 6ede90a790ce08330af58f81474c103ee1ca438f (patch) | |
tree | 69e6a7edab80c690263cf396073d917a548cde83 | |
parent | a6a2cc8f0e6501b92988accee2c3674dcfb05a2f (diff) |
tdf#89860 ability to print from Android Viewer
Change-Id: I13c7f3e085095f1c0d88ab3668557fcc1c4cb23a
Signed-off-by: Mert Tumer <merttumer@outlook.com>
Reviewed-on: https://gerrit.libreoffice.org/58900
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Tested-by: Tomaž Vajngerl <quikee@gmail.com>
5 files changed, 117 insertions, 1 deletions
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" /> + <item android:id="@+id/action_print" + android:title="@string/action_print" + android:orderInCategory="100" + android:visible="true" /> + <item android:id="@+id/action_UNO_commands" android:title="@string/action_UNO_commands" android:orderInCategory="100" /> 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 @@ <string name="UNO_commands_string_value_hint">Value</string> <string name="UNO_commands_string_parent_value_hint">Parent Value</string> <string name="action_exportToPDF">Export To PDF</string> + <string name="action_print">Print</string> </resources> 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; |