Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AndroidX #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions src/android/PrintAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.support.annotation.NonNull;
import android.support.v4.print.PrintHelper;
import androidx.annotation.NonNull;
import androidx.print.PrintHelper;

import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -41,8 +41,7 @@ Licensed to the Apache Software Foundation (ASF) under one
/**
* Document adapter to render and print PDF files.
*/
class PrintAdapter extends PrintDocumentAdapter
{
class PrintAdapter extends PrintDocumentAdapter {
// The name of the print job
private final @NonNull String jobName;

Expand All @@ -63,31 +62,23 @@ class PrintAdapter extends PrintDocumentAdapter
* @param input The input stream to render.
* @param callback The callback to inform once the job is done.
*/
PrintAdapter (@NonNull String jobName, int pageCount,
@NonNull InputStream input,
@NonNull PrintHelper.OnPrintFinishCallback callback)
{
this.jobName = jobName;
PrintAdapter(@NonNull String jobName, int pageCount, @NonNull InputStream input,
@NonNull PrintHelper.OnPrintFinishCallback callback) {
this.jobName = jobName;
this.pageCount = pageCount;
this.input = input;
this.callback = callback;
this.input = input;
this.callback = callback;
}

@Override
public void onLayout (PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
LayoutResultCallback callback,
Bundle bundle)
{
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle bundle) {
PrintDocumentInfo pdi;

if (cancellationSignal.isCanceled())
return;

pdi = new PrintDocumentInfo.Builder(jobName)
.setContentType(CONTENT_TYPE_DOCUMENT)
.setPageCount(pageCount)
pdi = new PrintDocumentInfo.Builder(jobName).setContentType(CONTENT_TYPE_DOCUMENT).setPageCount(pageCount)
.build();

boolean changed = !newAttributes.equals(oldAttributes);
Expand All @@ -96,11 +87,8 @@ public void onLayout (PrintAttributes oldAttributes,
}

@Override
public void onWrite (PageRange[] range,
ParcelFileDescriptor dest,
CancellationSignal cancellationSignal,
WriteResultCallback callback)
{
public void onWrite(PageRange[] range, ParcelFileDescriptor dest, CancellationSignal cancellationSignal,
WriteResultCallback callback) {
if (cancellationSignal.isCanceled())
return;

Expand All @@ -113,15 +101,14 @@ public void onWrite (PageRange[] range,
return;
}

callback.onWriteFinished(new PageRange[]{ PageRange.ALL_PAGES });
callback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES });
}

/**
* Closes the input stream and invokes the callback.
*/
@Override
public void onFinish ()
{
public void onFinish() {
super.onFinish();

PrintIO.close(input);
Expand Down
94 changes: 31 additions & 63 deletions src/android/PrintContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.BufferedInputStream;
import java.io.IOException;
Expand All @@ -35,10 +35,11 @@ Licensed to the Apache Software Foundation (ASF) under one
/**
* Knows how to convert a resource URL into an io stream.
*/
class PrintContent
{
class PrintContent {
// List of supported content types
enum ContentType { PLAIN, HTML, IMAGE, PDF, UNSUPPORTED }
enum ContentType {
PLAIN, HTML, IMAGE, PDF, UNSUPPORTED
}

// Helper class to deal with io operations
private final @NonNull PrintIO io;
Expand All @@ -48,7 +49,7 @@ enum ContentType { PLAIN, HTML, IMAGE, PDF, UNSUPPORTED }
*
* @param ctx The application context.
*/
private PrintContent (@NonNull Context ctx) {
private PrintContent(@NonNull Context ctx) {
io = new PrintIO(ctx);
}

Expand All @@ -60,9 +61,7 @@ private PrintContent (@NonNull Context ctx) {
* @return The content type even the file does not exist.
*/
@NonNull
static ContentType getContentType (@Nullable String path,
@NonNull Context context)
{
static ContentType getContentType(@Nullable String path, @NonNull Context context) {
return new PrintContent(context).getContentType(path);
}

Expand All @@ -74,36 +73,25 @@ static ContentType getContentType (@Nullable String path,
* @return The content type even the file does not exist.
*/
@NonNull
private ContentType getContentType (@Nullable String path)
{
private ContentType getContentType(@Nullable String path) {
ContentType type = ContentType.PLAIN;

if (path == null || path.isEmpty() || path.charAt(0) == '<')
{
if (path == null || path.isEmpty() || path.charAt(0) == '<') {
type = ContentType.HTML;
}
else if (path.matches("^[a-z0-9]+://.+"))
{
} else if (path.matches("^[a-z0-9]+://.+")) {
String mime;

if (path.startsWith("base64:"))
{
try
{
if (path.startsWith("base64:")) {
try {
mime = URLConnection.guessContentTypeFromStream(io.openBase64(path));
}
catch (IOException e)
{
} catch (IOException e) {
return ContentType.UNSUPPORTED;
}
}
else
{
} else {
mime = URLConnection.guessContentTypeFromName(path);
}

switch (mime)
{
switch (mime) {
case "image/bmp":
case "image/png":
case "image/jpeg":
Expand All @@ -127,15 +115,13 @@ else if (path.matches("^[a-z0-9]+://.+"))
/**
* Opens a file://, res:// or base64:// Uri as a stream.
*
* @param path The file path to decode.
* @param path The file path to decode.
* @param context The application context.
*
* @return An open IO stream or null if the file does not exist.
*/
@Nullable
static BufferedInputStream open (@NonNull String path,
@NonNull Context context)
{
static BufferedInputStream open(@NonNull String path, @NonNull Context context) {
return new PrintContent(context).open(path);
}

Expand All @@ -147,24 +133,16 @@ static BufferedInputStream open (@NonNull String path,
* @return An open IO stream or null if the file does not exist.
*/
@Nullable
private BufferedInputStream open (@NonNull String path)
{
private BufferedInputStream open(@NonNull String path) {
InputStream stream = null;

if (path.startsWith("res:"))
{
if (path.startsWith("res:")) {
stream = io.openResource(path);
}
else if (path.startsWith("file:///"))
{
} else if (path.startsWith("file:///")) {
stream = io.openFile(path);
}
else if (path.startsWith("file://"))
{
} else if (path.startsWith("file://")) {
stream = io.openAsset(path);
}
else if (path.startsWith("base64:"))
{
} else if (path.startsWith("base64:")) {
stream = io.openBase64(path);
}

Expand All @@ -180,8 +158,7 @@ else if (path.startsWith("base64:"))
* @return A bitmap or null if the path is not valid
*/
@Nullable
static Bitmap decode (@NonNull String path, @NonNull Context context)
{
static Bitmap decode(@NonNull String path, @NonNull Context context) {
return new PrintContent(context).decode(path);
}

Expand All @@ -193,30 +170,21 @@ static Bitmap decode (@NonNull String path, @NonNull Context context)
* @return A bitmap or null if the path is not valid
*/
@Nullable
private Bitmap decode (@NonNull String path)
{
private Bitmap decode(@NonNull String path) {
Bitmap bitmap;

if (path.startsWith("res:"))
{
if (path.startsWith("res:")) {
bitmap = io.decodeResource(path);
}
else if (path.startsWith("file:///"))
{
} else if (path.startsWith("file:///")) {
bitmap = io.decodeFile(path);
}
else if (path.startsWith("file://"))
{
} else if (path.startsWith("file://")) {
bitmap = io.decodeAsset(path);
}
else if (path.startsWith("base64:"))
{
} else if (path.startsWith("base64:")) {
bitmap = io.decodeBase64(path);
}
else {
} else {
bitmap = BitmapFactory.decodeFile(path);
}

return bitmap;
}
}
}
Loading