-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1b0561
commit 669da1a
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
app/src/main/java/org/grapheneos/pdfviewer/PdfDocumentAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.grapheneos.pdfviewer; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
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.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public class PdfDocumentAdapter extends PrintDocumentAdapter { | ||
|
||
private Context mContext; | ||
private String mPrintJobName; | ||
private Uri mUri; | ||
|
||
public PdfDocumentAdapter(Context ctx, String jobName, Uri uri) { | ||
this.mContext = ctx; | ||
this.mPrintJobName = jobName; | ||
this.mUri = uri; | ||
} | ||
|
||
@Override | ||
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback layoutResultCallback, Bundle bundle) { | ||
if (cancellationSignal.isCanceled()) { | ||
layoutResultCallback.onLayoutCancelled(); | ||
} else { | ||
PrintDocumentInfo.Builder pdinfoBuilder = new PrintDocumentInfo.Builder(mPrintJobName); | ||
pdinfoBuilder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT); | ||
pdinfoBuilder.setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN); | ||
PrintDocumentInfo pdInfo = pdinfoBuilder.build(); | ||
layoutResultCallback.onLayoutFinished(pdInfo, !newAttributes.equals(oldAttributes)); | ||
} | ||
} | ||
|
||
@Override | ||
public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor parcelFileDescriptor, CancellationSignal cancellationSignal, WriteResultCallback writeResultCallback) { | ||
InputStream inputStream = null; | ||
OutputStream outputStream = null; | ||
|
||
try { | ||
inputStream = mContext.getContentResolver().openInputStream(mUri);; | ||
outputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor()); | ||
byte[] buffer = new byte[mContext.getResources().getInteger(R.integer.print_adapter_buffer_size)]; | ||
|
||
int readSize = inputStream.read(buffer); | ||
while (readSize >= 0 && !cancellationSignal.isCanceled()) { | ||
outputStream.write(buffer, 0, readSize); | ||
readSize = inputStream.read(buffer); | ||
} | ||
|
||
if (cancellationSignal.isCanceled()) { | ||
writeResultCallback.onWriteCancelled(); | ||
} else { | ||
writeResultCallback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES }); | ||
} | ||
} catch (IOException ex) { | ||
writeResultCallback.onWriteFailed(ex.getMessage()); | ||
} finally { | ||
try { | ||
if (outputStream != null) | ||
outputStream.close(); | ||
if (inputStream != null) | ||
inputStream.close(); | ||
} catch (IOException ignored) { | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<integer name="print_adapter_buffer_size">16384</integer> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters