aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip P. Moltmann <moltmann@google.com>2015-11-30 15:29:55 -0800
committerPhilip P. Moltmann <moltmann@google.com>2015-11-30 15:29:55 -0800
commitf885d1211c6356a1978df6b99778b6caad75c8ea (patch)
tree3ba3518cc9c9b3b60099351dcc60f63dcdd636f5
parent3fc0030d2ab8b62e7d912e157dda715e7e0c1471 (diff)
downloadexperimental-f885d1211c6356a1978df6b99778b6caad75c8ea.tar.gz
Deal with weird labels in MyPrintService
The labels of the print jobs might contain all kind of characters that are not allowed in file paths. Hence Do not use it to create a file name. Change-Id: I68dfa897175357819d4923e8dd9989fbe08cc0aa
-rw-r--r--PrintService/src/foo/bar/printservice/MyPrintService.java124
1 files changed, 64 insertions, 60 deletions
diff --git a/PrintService/src/foo/bar/printservice/MyPrintService.java b/PrintService/src/foo/bar/printservice/MyPrintService.java
index ebf3cdf..ada61e9 100644
--- a/PrintService/src/foo/bar/printservice/MyPrintService.java
+++ b/PrintService/src/foo/bar/printservice/MyPrintService.java
@@ -223,78 +223,82 @@ public class MyPrintService extends PrintService {
printJob.start();
}
- final PrintJobInfo info = printJob.getInfo();
- final File file = new File(getFilesDir(), info.getLabel() + ".pdf");
-
- mFakePrintTask = new AsyncTask<ParcelFileDescriptor, Void, Void>() {
- @Override
- protected Void doInBackground(ParcelFileDescriptor... params) {
- InputStream in = new BufferedInputStream(new FileInputStream(
- params[0].getFileDescriptor()));
- OutputStream out = null;
- try {
- out = new BufferedOutputStream(new FileOutputStream(file));
- final byte[] buffer = new byte[8192];
- while (true) {
- if (isCancelled()) {
- break;
+ try {
+ final File file = File.createTempFile(this.getClass().getSimpleName(), ".pdf",
+ getFilesDir());
+ mFakePrintTask = new AsyncTask<ParcelFileDescriptor, Void, Void>() {
+ @Override
+ protected Void doInBackground(ParcelFileDescriptor... params) {
+ InputStream in = new BufferedInputStream(new FileInputStream(
+ params[0].getFileDescriptor()));
+ OutputStream out = null;
+ try {
+ out = new BufferedOutputStream(new FileOutputStream(file));
+ final byte[] buffer = new byte[8192];
+ while (true) {
+ if (isCancelled()) {
+ break;
+ }
+ final int readByteCount = in.read(buffer);
+ if (readByteCount < 0) {
+ break;
+ }
+ out.write(buffer, 0, readByteCount);
}
- final int readByteCount = in.read(buffer);
- if (readByteCount < 0) {
- break;
+ } catch (IOException ioe) {
+ throw new RuntimeException(ioe);
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException ioe) {
+ /* ignore */
+ }
}
- out.write(buffer, 0, readByteCount);
- }
- } catch (IOException ioe) {
- throw new RuntimeException(ioe);
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException ioe) {
- /* ignore */
+ if (out != null) {
+ try {
+ out.close();
+ } catch (IOException ioe) {
+ /* ignore */
+ }
}
- }
- if (out != null) {
- try {
- out.close();
- } catch (IOException ioe) {
- /* ignore */
+ if (isCancelled()) {
+ file.delete();
}
}
- if (isCancelled()) {
- file.delete();
- }
+ return null;
}
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- if (printJob.isStarted()) {
- printJob.complete();
- }
+ @Override
+ protected void onPostExecute(Void result) {
+ if (printJob.isStarted()) {
+ printJob.complete();
+ }
- file.setReadable(true, false);
+ file.setReadable(true, false);
- // Quick and dirty to show the file - use a content provider instead.
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(file), "application/pdf");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent, null);
+ // Quick and dirty to show the file - use a content provider instead.
+ Intent intent = new Intent(Intent.ACTION_VIEW);
+ intent.setDataAndType(Uri.fromFile(file), "application/pdf");
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ startActivity(intent, null);
- mFakePrintTask = null;
- }
+ mFakePrintTask = null;
+ }
- @Override
- protected void onCancelled(Void result) {
- if (printJob.isStarted()) {
- printJob.cancel();
+ @Override
+ protected void onCancelled(Void result) {
+ if (printJob.isStarted()) {
+ printJob.cancel();
+ }
}
- }
- };
- mFakePrintTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR,
- printJob.getDocument().getData());
+ };
+ mFakePrintTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR,
+ printJob.getDocument().getData());
+ } catch (IOException e) {
+ Log.e(LOG_TAG, "Could not create temporary file: %s", e);
+ return;
+ }
}
private final class MyHandler extends Handler {