Kaydet (Commit) 6ede90a7 authored tarafından Mert Tumer's avatar Mert Tumer Kaydeden (comit) Tomaž Vajngerl

tdf#89860 ability to print from Android Viewer

Change-Id: I13c7f3e085095f1c0d88ab3668557fcc1c4cb23a
Signed-off-by: 's avatarMert Tumer <merttumer@outlook.com>
Reviewed-on: https://gerrit.libreoffice.org/58900Reviewed-by: 's avatarTomaž Vajngerl <quikee@gmail.com>
Tested-by: 's avatarTomaž Vajngerl <quikee@gmail.com>
üst a6a2cc8f
......@@ -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" />
......
......@@ -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>
......@@ -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();
......
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
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment