FreeRDP
Loading...
Searching...
No Matches
PrintProxyActivity.java
1/*
2 Print Proxy Activity
3
4 Copyright 2026 Ibrahim Sevinc
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.presentation;
12
13import android.content.Context;
14import android.net.Uri;
15import android.os.Bundle;
16import android.os.Handler;
17import android.os.Looper;
18import android.os.ParcelFileDescriptor;
19import android.print.PageRange;
20import android.print.PrintAttributes;
21import android.print.PrintDocumentAdapter;
22import android.print.PrintDocumentInfo;
23import android.print.PrintJob;
24import android.print.PrintManager;
25
26import android.app.Activity;
27
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.io.InputStream;
31
36public class PrintProxyActivity extends Activity
37{
38 private PrintJob printJob;
39 private final Handler handler = new Handler(Looper.getMainLooper());
40 private final Runnable pollJob = new Runnable() {
41 @Override public void run()
42 {
43 if (printJob == null || printJob.isCompleted() || printJob.isFailed() ||
44 printJob.isCancelled())
45 {
46 finish();
47 return;
48 }
49 handler.postDelayed(this, 500);
50 }
51 };
52
53 @Override protected void onCreate(Bundle savedInstanceState)
54 {
55 super.onCreate(savedInstanceState);
56
57 Uri uri = getIntent().getData();
58 if (uri == null)
59 {
60 finish();
61 return;
62 }
63
64 PrintManager pm = (PrintManager)getSystemService(Context.PRINT_SERVICE);
65 printJob = pm.print(uri.getLastPathSegment(), new PdfAdapter(this, uri), null);
66 handler.postDelayed(pollJob, 500);
67 }
68
69 @Override protected void onDestroy()
70 {
71 handler.removeCallbacks(pollJob);
72 super.onDestroy();
73 }
74
75 private static class PdfAdapter extends PrintDocumentAdapter
76 {
77 private final Context ctx;
78 private final Uri uri;
79
80 PdfAdapter(Context ctx, Uri uri)
81 {
82 this.ctx = ctx;
83 this.uri = uri;
84 }
85
86 @Override
87 public void onLayout(PrintAttributes oldAttr, PrintAttributes newAttr,
88 android.os.CancellationSignal signal, LayoutResultCallback cb,
89 Bundle extras)
90 {
91 if (signal.isCanceled())
92 {
93 cb.onLayoutCancelled();
94 return;
95 }
96 PrintDocumentInfo info = new PrintDocumentInfo.Builder(uri.getLastPathSegment())
97 .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
98 .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN)
99 .build();
100 cb.onLayoutFinished(info, !newAttr.equals(oldAttr));
101 }
102
103 @Override
104 public void onWrite(PageRange[] pages, ParcelFileDescriptor dest,
105 android.os.CancellationSignal signal, WriteResultCallback cb)
106 {
107 try (InputStream in = ctx.getContentResolver().openInputStream(uri);
108 FileOutputStream out = new FileOutputStream(dest.getFileDescriptor()))
109 {
110 if (in == null)
111 {
112 cb.onWriteFailed("Cannot open PDF");
113 return;
114 }
115 byte[] buf = new byte[8192];
116 int n;
117 while ((n = in.read(buf)) != -1)
118 {
119 if (signal.isCanceled())
120 {
121 cb.onWriteCancelled();
122 return;
123 }
124 out.write(buf, 0, n);
125 }
126 cb.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES });
127 }
128 catch (IOException e)
129 {
130 cb.onWriteFailed(e.getMessage());
131 }
132 }
133 }
134}